| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- InstructionCombining.cpp - Combine multiple instructions -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // InstructionCombining - Combine instructions to form fewer, simple |
| Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 11 | // instructions. This pass does not modify the CFG. This pass is where |
| 12 | // algebraic simplification happens. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13 | // |
| 14 | // This pass combines things like: |
| 15 | // %Y = add i32 %X, 1 |
| 16 | // %Z = add i32 %Y, 1 |
| 17 | // into: |
| 18 | // %Z = add i32 %X, 2 |
| 19 | // |
| 20 | // This is a simple worklist driven algorithm. |
| 21 | // |
| 22 | // This pass guarantees that the following canonicalizations are performed on |
| 23 | // the program: |
| 24 | // 1. If a binary operator has a constant operand, it is moved to the RHS |
| 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. |
| 27 | // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible |
| 28 | // 4. All cmp instructions on boolean values are replaced with logical ops |
| 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. |
| 32 | // ... etc. |
| 33 | // |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #define DEBUG_TYPE "instcombine" |
| 37 | #include "llvm/Transforms/Scalar.h" |
| 38 | #include "llvm/IntrinsicInst.h" |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 39 | #include "llvm/LLVMContext.h" |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | #include "llvm/Pass.h" |
| 41 | #include "llvm/DerivedTypes.h" |
| 42 | #include "llvm/GlobalVariable.h" |
| Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 43 | #include "llvm/Operator.h" |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/ConstantFolding.h" |
| Chris Lattner | a432bc7 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ValueTracking.h" |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | #include "llvm/Target/TargetData.h" |
| 47 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 48 | #include "llvm/Transforms/Utils/Local.h" |
| 49 | #include "llvm/Support/CallSite.h" |
| Nick Lewycky | 0185bbf | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 50 | #include "llvm/Support/ConstantRange.h" |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
| Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 52 | #include "llvm/Support/ErrorHandling.h" |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 53 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 54 | #include "llvm/Support/InstVisitor.h" |
| 55 | #include "llvm/Support/MathExtras.h" |
| 56 | #include "llvm/Support/PatternMatch.h" |
| 57 | #include "llvm/Support/Compiler.h" |
| 58 | #include "llvm/ADT/DenseMap.h" |
| 59 | #include "llvm/ADT/SmallVector.h" |
| 60 | #include "llvm/ADT/SmallPtrSet.h" |
| 61 | #include "llvm/ADT/Statistic.h" |
| 62 | #include "llvm/ADT/STLExtras.h" |
| 63 | #include <algorithm> |
| Edwin Török | a0e6fce | 2008-04-20 08:33:11 +0000 | [diff] [blame] | 64 | #include <climits> |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 65 | #include <sstream> |
| 66 | using namespace llvm; |
| 67 | using namespace llvm::PatternMatch; |
| 68 | |
| 69 | STATISTIC(NumCombined , "Number of insts combined"); |
| 70 | STATISTIC(NumConstProp, "Number of constant folds"); |
| 71 | STATISTIC(NumDeadInst , "Number of dead inst eliminated"); |
| 72 | STATISTIC(NumDeadStore, "Number of dead stores eliminated"); |
| 73 | STATISTIC(NumSunkInst , "Number of instructions sunk"); |
| 74 | |
| 75 | namespace { |
| 76 | class VISIBILITY_HIDDEN InstCombiner |
| 77 | : public FunctionPass, |
| 78 | public InstVisitor<InstCombiner, Instruction*> { |
| 79 | // Worklist of all of the instructions that need to be simplified. |
| Chris Lattner | a06291a | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 80 | SmallVector<Instruction*, 256> Worklist; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | DenseMap<Instruction*, unsigned> WorklistMap; |
| 82 | TargetData *TD; |
| 83 | bool MustPreserveLCSSA; |
| 84 | public: |
| 85 | static char ID; // Pass identification, replacement for typeid |
| Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 86 | InstCombiner() : FunctionPass(&ID) {} |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | |
| Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 88 | LLVMContext *Context; |
| 89 | LLVMContext *getContext() const { return Context; } |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 90 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 91 | /// AddToWorkList - Add the specified instruction to the worklist if it |
| 92 | /// isn't already in it. |
| 93 | void AddToWorkList(Instruction *I) { |
| Dan Gohman | 55d1966 | 2008-07-07 17:46:23 +0000 | [diff] [blame] | 94 | if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 95 | Worklist.push_back(I); |
| 96 | } |
| 97 | |
| 98 | // RemoveFromWorkList - remove I from the worklist if it exists. |
| 99 | void RemoveFromWorkList(Instruction *I) { |
| 100 | DenseMap<Instruction*, unsigned>::iterator It = WorklistMap.find(I); |
| 101 | if (It == WorklistMap.end()) return; // Not in worklist. |
| 102 | |
| 103 | // Don't bother moving everything down, just null out the slot. |
| 104 | Worklist[It->second] = 0; |
| 105 | |
| 106 | WorklistMap.erase(It); |
| 107 | } |
| 108 | |
| 109 | Instruction *RemoveOneFromWorkList() { |
| 110 | Instruction *I = Worklist.back(); |
| 111 | Worklist.pop_back(); |
| 112 | WorklistMap.erase(I); |
| 113 | return I; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /// AddUsersToWorkList - When an instruction is simplified, add all users of |
| 118 | /// the instruction to the work lists because they might get more simplified |
| 119 | /// now. |
| 120 | /// |
| 121 | void AddUsersToWorkList(Value &I) { |
| 122 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
| 123 | UI != UE; ++UI) |
| 124 | AddToWorkList(cast<Instruction>(*UI)); |
| 125 | } |
| 126 | |
| 127 | /// AddUsesToWorkList - When an instruction is simplified, add operands to |
| 128 | /// the work lists because they might get more simplified now. |
| 129 | /// |
| 130 | void AddUsesToWorkList(Instruction &I) { |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 131 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 132 | if (Instruction *Op = dyn_cast<Instruction>(*i)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 133 | AddToWorkList(Op); |
| 134 | } |
| 135 | |
| 136 | /// AddSoonDeadInstToWorklist - The specified instruction is about to become |
| 137 | /// dead. Add all of its operands to the worklist, turning them into |
| 138 | /// undef's to reduce the number of uses of those instructions. |
| 139 | /// |
| 140 | /// Return the specified operand before it is turned into an undef. |
| 141 | /// |
| 142 | Value *AddSoonDeadInstToWorklist(Instruction &I, unsigned op) { |
| 143 | Value *R = I.getOperand(op); |
| 144 | |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 145 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 146 | if (Instruction *Op = dyn_cast<Instruction>(*i)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 147 | AddToWorkList(Op); |
| 148 | // Set the operand to undef to drop the use. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 149 | *i = Context->getUndef(Op->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return R; |
| 153 | } |
| 154 | |
| 155 | public: |
| 156 | virtual bool runOnFunction(Function &F); |
| 157 | |
| 158 | bool DoOneIteration(Function &F, unsigned ItNum); |
| 159 | |
| 160 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 161 | AU.addPreservedID(LCSSAID); |
| 162 | AU.setPreservesCFG(); |
| 163 | } |
| 164 | |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 165 | TargetData *getTargetData() const { return TD; } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 166 | |
| 167 | // Visitation implementation - Implement instruction combining for different |
| 168 | // instruction types. The semantics are as follows: |
| 169 | // Return Value: |
| 170 | // null - No change was made |
| 171 | // I - Change was made, I is still valid, I may be dead though |
| 172 | // otherwise - Change was made, replace I with returned instruction |
| 173 | // |
| 174 | Instruction *visitAdd(BinaryOperator &I); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 175 | Instruction *visitFAdd(BinaryOperator &I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 176 | Instruction *visitSub(BinaryOperator &I); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 177 | Instruction *visitFSub(BinaryOperator &I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 178 | Instruction *visitMul(BinaryOperator &I); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 179 | Instruction *visitFMul(BinaryOperator &I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 180 | Instruction *visitURem(BinaryOperator &I); |
| 181 | Instruction *visitSRem(BinaryOperator &I); |
| 182 | Instruction *visitFRem(BinaryOperator &I); |
| Chris Lattner | 76972db | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 183 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 184 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 185 | Instruction *commonIRemTransforms(BinaryOperator &I); |
| 186 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 187 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 188 | Instruction *visitUDiv(BinaryOperator &I); |
| 189 | Instruction *visitSDiv(BinaryOperator &I); |
| 190 | Instruction *visitFDiv(BinaryOperator &I); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 191 | Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); |
| Chris Lattner | 93a359a | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 192 | Instruction *FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 193 | Instruction *visitAnd(BinaryOperator &I); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 194 | Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); |
| Chris Lattner | 57e66fa | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 195 | Instruction *FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS); |
| Bill Wendling | 9912f71 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 196 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 197 | Value *A, Value *B, Value *C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 198 | Instruction *visitOr (BinaryOperator &I); |
| 199 | Instruction *visitXor(BinaryOperator &I); |
| 200 | Instruction *visitShl(BinaryOperator &I); |
| 201 | Instruction *visitAShr(BinaryOperator &I); |
| 202 | Instruction *visitLShr(BinaryOperator &I); |
| 203 | Instruction *commonShiftTransforms(BinaryOperator &I); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 204 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
| 205 | Constant *RHSC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | Instruction *visitFCmpInst(FCmpInst &I); |
| 207 | Instruction *visitICmpInst(ICmpInst &I); |
| 208 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
| 209 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 210 | Instruction *LHS, |
| 211 | ConstantInt *RHS); |
| 212 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 213 | ConstantInt *DivRHS); |
| 214 | |
| 215 | Instruction *FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 216 | ICmpInst::Predicate Cond, Instruction &I); |
| 217 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
| 218 | BinaryOperator &I); |
| 219 | Instruction *commonCastTransforms(CastInst &CI); |
| 220 | Instruction *commonIntCastTransforms(CastInst &CI); |
| 221 | Instruction *commonPointerCastTransforms(CastInst &CI); |
| 222 | Instruction *visitTrunc(TruncInst &CI); |
| 223 | Instruction *visitZExt(ZExtInst &CI); |
| 224 | Instruction *visitSExt(SExtInst &CI); |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 225 | Instruction *visitFPTrunc(FPTruncInst &CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 226 | Instruction *visitFPExt(CastInst &CI); |
| Chris Lattner | deef1a7 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 227 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 228 | Instruction *visitFPToSI(FPToSIInst &FI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 229 | Instruction *visitUIToFP(CastInst &CI); |
| 230 | Instruction *visitSIToFP(CastInst &CI); |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 231 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
| Chris Lattner | 7c162648 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 232 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | Instruction *visitBitCast(BitCastInst &CI); |
| 234 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 235 | Instruction *FI); |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 236 | Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*); |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 237 | Instruction *visitSelectInst(SelectInst &SI); |
| 238 | Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | Instruction *visitCallInst(CallInst &CI); |
| 240 | Instruction *visitInvokeInst(InvokeInst &II); |
| 241 | Instruction *visitPHINode(PHINode &PN); |
| 242 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 243 | Instruction *visitAllocationInst(AllocationInst &AI); |
| 244 | Instruction *visitFreeInst(FreeInst &FI); |
| 245 | Instruction *visitLoadInst(LoadInst &LI); |
| 246 | Instruction *visitStoreInst(StoreInst &SI); |
| 247 | Instruction *visitBranchInst(BranchInst &BI); |
| 248 | Instruction *visitSwitchInst(SwitchInst &SI); |
| 249 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
| 250 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
| 251 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
| Matthijs Kooijman | da9ef70 | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 252 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 253 | |
| 254 | // visitInstruction - Specify what to return for unhandled instructions... |
| 255 | Instruction *visitInstruction(Instruction &I) { return 0; } |
| 256 | |
| 257 | private: |
| 258 | Instruction *visitCallSite(CallSite CS); |
| 259 | bool transformConstExprCastCall(CallSite CS); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 260 | Instruction *transformCallThroughTrampoline(CallSite CS); |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 261 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 262 | bool DoXform = true); |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 263 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS); |
| Dale Johannesen | 2c11fe2 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 264 | DbgDeclareInst *hasOneUsePlusDeclare(Value *V); |
| 265 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 266 | |
| 267 | public: |
| 268 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 269 | // in the program. Add the new instruction to the worklist. |
| 270 | // |
| 271 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
| 272 | assert(New && New->getParent() == 0 && |
| 273 | "New instruction already inserted into a basic block!"); |
| 274 | BasicBlock *BB = Old.getParent(); |
| 275 | BB->getInstList().insert(&Old, New); // Insert inst |
| 276 | AddToWorkList(New); |
| 277 | return New; |
| 278 | } |
| 279 | |
| 280 | /// InsertCastBefore - Insert a cast of V to TY before the instruction POS. |
| 281 | /// This also adds the cast to the worklist. Finally, this returns the |
| 282 | /// cast. |
| 283 | Value *InsertCastBefore(Instruction::CastOps opc, Value *V, const Type *Ty, |
| 284 | Instruction &Pos) { |
| 285 | if (V->getType() == Ty) return V; |
| 286 | |
| 287 | if (Constant *CV = dyn_cast<Constant>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 288 | return Context->getConstantExprCast(opc, CV, Ty); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 289 | |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 290 | Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 291 | AddToWorkList(C); |
| 292 | return C; |
| 293 | } |
| Chris Lattner | 13c2d6e | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 294 | |
| 295 | Value *InsertBitCastBefore(Value *V, const Type *Ty, Instruction &Pos) { |
| 296 | return InsertCastBefore(Instruction::BitCast, V, Ty, Pos); |
| 297 | } |
| 298 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 299 | |
| 300 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 301 | // found to be dead, replacable with another preexisting expression. Here |
| 302 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 303 | // value, then return I, so that the inst combiner will know that I was |
| 304 | // modified. |
| 305 | // |
| 306 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
| 307 | AddUsersToWorkList(I); // Add all modified instrs to worklist |
| 308 | if (&I != V) { |
| 309 | I.replaceAllUsesWith(V); |
| 310 | return &I; |
| 311 | } else { |
| 312 | // If we are replacing the instruction with itself, this must be in a |
| 313 | // segment of unreachable code, so just clobber the instruction. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 314 | I.replaceAllUsesWith(Context->getUndef(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 315 | return &I; |
| 316 | } |
| 317 | } |
| 318 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 319 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 320 | // effects or produces a void value, we can't rely on DCE to delete the |
| 321 | // instruction. Instead, visit methods should return the value returned by |
| 322 | // this function. |
| 323 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 324 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 325 | AddUsesToWorkList(I); |
| 326 | RemoveFromWorkList(&I); |
| 327 | I.eraseFromParent(); |
| 328 | return 0; // Don't do anything with FI |
| 329 | } |
| Chris Lattner | a432bc7 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 330 | |
| 331 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero, |
| 332 | APInt &KnownOne, unsigned Depth = 0) const { |
| 333 | return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); |
| 334 | } |
| 335 | |
| 336 | bool MaskedValueIsZero(Value *V, const APInt &Mask, |
| 337 | unsigned Depth = 0) const { |
| 338 | return llvm::MaskedValueIsZero(V, Mask, TD, Depth); |
| 339 | } |
| 340 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const { |
| 341 | return llvm::ComputeNumSignBits(Op, TD, Depth); |
| 342 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 343 | |
| 344 | private: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 345 | |
| 346 | /// SimplifyCommutative - This performs a few simplifications for |
| 347 | /// commutative operators. |
| 348 | bool SimplifyCommutative(BinaryOperator &I); |
| 349 | |
| 350 | /// SimplifyCompare - This reorders the operands of a CmpInst to get them in |
| 351 | /// most-complex to least-complex order. |
| 352 | bool SimplifyCompare(CmpInst &I); |
| 353 | |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 354 | /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value |
| 355 | /// based on the demanded bits. |
| 356 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
| 357 | APInt& KnownZero, APInt& KnownOne, |
| 358 | unsigned Depth); |
| 359 | bool SimplifyDemandedBits(Use &U, APInt DemandedMask, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 360 | APInt& KnownZero, APInt& KnownOne, |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 361 | unsigned Depth=0); |
| 362 | |
| 363 | /// SimplifyDemandedInstructionBits - Inst is an integer instruction that |
| 364 | /// SimplifyDemandedBits knows about. See if the instruction has any |
| 365 | /// properties that allow us to simplify its operands. |
| 366 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
| 367 | |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 368 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
| 369 | APInt& UndefElts, unsigned Depth = 0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 370 | |
| 371 | // FoldOpIntoPhi - Given a binary operator or cast instruction which has a |
| 372 | // PHI node as operand #0, see if we can fold the instruction into the PHI |
| 373 | // (which is only possible if all operands to the PHI are constants). |
| 374 | Instruction *FoldOpIntoPhi(Instruction &I); |
| 375 | |
| 376 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 377 | // operator and they all are only used by the PHI, PHI together their |
| 378 | // inputs, and do the operation once, to the result of the PHI. |
| 379 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 380 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 381 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 382 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 383 | |
| 384 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 385 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
| 386 | |
| 387 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
| 388 | bool isSub, Instruction &I); |
| 389 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 390 | bool isSigned, bool Inside, Instruction &IB); |
| 391 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocationInst &AI); |
| 392 | Instruction *MatchBSwap(BinaryOperator &I); |
| 393 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 394 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 395 | Instruction *SimplifyMemSet(MemSetInst *MI); |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 396 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 397 | |
| 398 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 399 | |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 400 | bool CanEvaluateInDifferentType(Value *V, const Type *Ty, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 401 | unsigned CastOpc, int &NumCastsRemoved); |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 402 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 403 | unsigned PrefAlign = 0); |
| Matthijs Kooijman | da9ef70 | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 404 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 405 | }; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 408 | char InstCombiner::ID = 0; |
| 409 | static RegisterPass<InstCombiner> |
| 410 | X("instcombine", "Combine redundant instructions"); |
| 411 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 412 | // getComplexity: Assign a complexity or rank value to LLVM Values... |
| 413 | // 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 414 | static unsigned getComplexity(LLVMContext *Context, Value *V) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 415 | if (isa<Instruction>(V)) { |
| Owen Anderson | 76f4925 | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 416 | if (BinaryOperator::isNeg(V) || |
| 417 | BinaryOperator::isFNeg(V) || |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 418 | BinaryOperator::isNot(V)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 419 | return 3; |
| 420 | return 4; |
| 421 | } |
| 422 | if (isa<Argument>(V)) return 3; |
| 423 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
| 424 | } |
| 425 | |
| 426 | // isOnlyUse - Return true if this instruction will be deleted if we stop using |
| 427 | // it. |
| 428 | static bool isOnlyUse(Value *V) { |
| 429 | return V->hasOneUse() || isa<Constant>(V); |
| 430 | } |
| 431 | |
| 432 | // getPromotedType - Return the specified type promoted as it would be to pass |
| 433 | // though a va_arg area... |
| 434 | static const Type *getPromotedType(const Type *Ty) { |
| 435 | if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { |
| 436 | if (ITy->getBitWidth() < 32) |
| 437 | return Type::Int32Ty; |
| 438 | } |
| 439 | return Ty; |
| 440 | } |
| 441 | |
| Matthijs Kooijman | 5e2a318 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 442 | /// getBitCastOperand - If the specified operand is a CastInst, a constant |
| 443 | /// expression bitcast, or a GetElementPtrInst with all zero indices, return the |
| 444 | /// operand value, otherwise return null. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 445 | static Value *getBitCastOperand(Value *V) { |
| Dan Gohman | ae402b0 | 2009-07-17 23:55:56 +0000 | [diff] [blame] | 446 | if (Operator *O = dyn_cast<Operator>(V)) { |
| 447 | if (O->getOpcode() == Instruction::BitCast) |
| 448 | return O->getOperand(0); |
| 449 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
| 450 | if (GEP->hasAllZeroIndices()) |
| 451 | return GEP->getPointerOperand(); |
| Matthijs Kooijman | 5e2a318 | 2008-10-13 15:17:01 +0000 | [diff] [blame] | 452 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | /// This function is a wrapper around CastInst::isEliminableCastPair. It |
| 457 | /// simply extracts arguments and returns what that function returns. |
| 458 | static Instruction::CastOps |
| 459 | isEliminableCastPair( |
| 460 | const CastInst *CI, ///< The first cast instruction |
| 461 | unsigned opcode, ///< The opcode of the second cast instruction |
| 462 | const Type *DstTy, ///< The target type for the second cast instruction |
| 463 | TargetData *TD ///< The target data for pointer size |
| 464 | ) { |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 465 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 466 | const Type *SrcTy = CI->getOperand(0)->getType(); // A from above |
| 467 | const Type *MidTy = CI->getType(); // B from above |
| 468 | |
| 469 | // Get the opcodes of the two Cast instructions |
| 470 | Instruction::CastOps firstOp = Instruction::CastOps(CI->getOpcode()); |
| 471 | Instruction::CastOps secondOp = Instruction::CastOps(opcode); |
| 472 | |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 473 | unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 474 | DstTy, |
| 475 | TD ? TD->getIntPtrType() : 0); |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 476 | |
| 477 | // We don't want to form an inttoptr or ptrtoint that converts to an integer |
| 478 | // type that differs from the pointer size. |
| 479 | if ((Res == Instruction::IntToPtr && SrcTy != TD->getIntPtrType()) || |
| 480 | (Res == Instruction::PtrToInt && DstTy != TD->getIntPtrType())) |
| 481 | Res = 0; |
| 482 | |
| 483 | return Instruction::CastOps(Res); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /// ValueRequiresCast - Return true if the cast from "V to Ty" actually results |
| 487 | /// in any code being generated. It does not require codegen if V is simple |
| 488 | /// enough or if the cast can be folded into other casts. |
| 489 | static bool ValueRequiresCast(Instruction::CastOps opcode, const Value *V, |
| 490 | const Type *Ty, TargetData *TD) { |
| 491 | if (V->getType() == Ty || isa<Constant>(V)) return false; |
| 492 | |
| 493 | // If this is another cast that can be eliminated, it isn't codegen either. |
| 494 | if (const CastInst *CI = dyn_cast<CastInst>(V)) |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 495 | if (isEliminableCastPair(CI, opcode, Ty, TD)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 496 | return false; |
| 497 | return true; |
| 498 | } |
| 499 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 500 | // SimplifyCommutative - This performs a few simplifications for commutative |
| 501 | // operators: |
| 502 | // |
| 503 | // 1. Order operands such that they are listed from right (least complex) to |
| 504 | // left (most complex). This puts constants before unary operators before |
| 505 | // binary operators. |
| 506 | // |
| 507 | // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2)) |
| 508 | // 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
| 509 | // |
| 510 | bool InstCombiner::SimplifyCommutative(BinaryOperator &I) { |
| 511 | bool Changed = false; |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 512 | if (getComplexity(Context, I.getOperand(0)) < |
| 513 | getComplexity(Context, I.getOperand(1))) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 514 | Changed = !I.swapOperands(); |
| 515 | |
| 516 | if (!I.isAssociative()) return Changed; |
| 517 | Instruction::BinaryOps Opcode = I.getOpcode(); |
| 518 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0))) |
| 519 | if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) { |
| 520 | if (isa<Constant>(I.getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 521 | Constant *Folded = Context->getConstantExpr(I.getOpcode(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 522 | cast<Constant>(I.getOperand(1)), |
| 523 | cast<Constant>(Op->getOperand(1))); |
| 524 | I.setOperand(0, Op->getOperand(0)); |
| 525 | I.setOperand(1, Folded); |
| 526 | return true; |
| 527 | } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1))) |
| 528 | if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) && |
| 529 | isOnlyUse(Op) && isOnlyUse(Op1)) { |
| 530 | Constant *C1 = cast<Constant>(Op->getOperand(1)); |
| 531 | Constant *C2 = cast<Constant>(Op1->getOperand(1)); |
| 532 | |
| 533 | // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 534 | Constant *Folded = Context->getConstantExpr(I.getOpcode(), C1, C2); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 535 | Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 536 | Op1->getOperand(0), |
| 537 | Op1->getName(), &I); |
| 538 | AddToWorkList(New); |
| 539 | I.setOperand(0, New); |
| 540 | I.setOperand(1, Folded); |
| 541 | return true; |
| 542 | } |
| 543 | } |
| 544 | return Changed; |
| 545 | } |
| 546 | |
| 547 | /// SimplifyCompare - For a CmpInst this function just orders the operands |
| 548 | /// so that theyare listed from right (least complex) to left (most complex). |
| 549 | /// This puts constants before unary operators before binary operators. |
| 550 | bool InstCombiner::SimplifyCompare(CmpInst &I) { |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 551 | if (getComplexity(Context, I.getOperand(0)) >= |
| 552 | getComplexity(Context, I.getOperand(1))) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 553 | return false; |
| 554 | I.swapOperands(); |
| 555 | // Compare instructions are not associative so there's nothing else we can do. |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction |
| 560 | // if the LHS is a constant zero (which is the 'negate' form). |
| 561 | // |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 562 | static inline Value *dyn_castNegVal(Value *V, LLVMContext *Context) { |
| Owen Anderson | 76f4925 | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 563 | if (BinaryOperator::isNeg(V)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 564 | return BinaryOperator::getNegArgument(V); |
| 565 | |
| 566 | // Constants can be considered to be negated values if they can be folded. |
| 567 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 568 | return Context->getConstantExprNeg(C); |
| Nick Lewycky | 58867bc | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 569 | |
| 570 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
| 571 | if (C->getType()->getElementType()->isInteger()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 572 | return Context->getConstantExprNeg(C); |
| Nick Lewycky | 58867bc | 2008-05-23 04:54:45 +0000 | [diff] [blame] | 573 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 577 | // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the |
| 578 | // instruction if the LHS is a constant negative zero (which is the 'negate' |
| 579 | // form). |
| 580 | // |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 581 | static inline Value *dyn_castFNegVal(Value *V, LLVMContext *Context) { |
| Owen Anderson | 76f4925 | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 582 | if (BinaryOperator::isFNeg(V)) |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 583 | return BinaryOperator::getFNegArgument(V); |
| 584 | |
| 585 | // Constants can be considered to be negated values if they can be folded. |
| 586 | if (ConstantFP *C = dyn_cast<ConstantFP>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 587 | return Context->getConstantExprFNeg(C); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 588 | |
| 589 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) |
| 590 | if (C->getType()->getElementType()->isFloatingPoint()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 591 | return Context->getConstantExprFNeg(C); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 596 | static inline Value *dyn_castNotVal(Value *V, LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 597 | if (BinaryOperator::isNot(V)) |
| 598 | return BinaryOperator::getNotArgument(V); |
| 599 | |
| 600 | // Constants can be considered to be not'ed values... |
| 601 | if (ConstantInt *C = dyn_cast<ConstantInt>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 602 | return Context->getConstantInt(~C->getValue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | // dyn_castFoldableMul - If this value is a multiply that can be folded into |
| 607 | // other computations (because it has a constant operand), return the |
| 608 | // non-constant operand of the multiply, and set CST to point to the multiplier. |
| 609 | // Otherwise, return null. |
| 610 | // |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 611 | static inline Value *dyn_castFoldableMul(Value *V, ConstantInt *&CST, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 612 | LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 613 | if (V->hasOneUse() && V->getType()->isInteger()) |
| 614 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 615 | if (I->getOpcode() == Instruction::Mul) |
| 616 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) |
| 617 | return I->getOperand(0); |
| 618 | if (I->getOpcode() == Instruction::Shl) |
| 619 | if ((CST = dyn_cast<ConstantInt>(I->getOperand(1)))) { |
| 620 | // The multiplier is really 1 << CST. |
| 621 | uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); |
| 622 | uint32_t CSTVal = CST->getLimitedValue(BitWidth); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 623 | CST = Context->getConstantInt(APInt(BitWidth, 1).shl(CSTVal)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 624 | return I->getOperand(0); |
| 625 | } |
| 626 | } |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | /// dyn_castGetElementPtr - If this is a getelementptr instruction or constant |
| 631 | /// expression, return it. |
| 632 | static User *dyn_castGetElementPtr(Value *V) { |
| 633 | if (isa<GetElementPtrInst>(V)) return cast<User>(V); |
| 634 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 635 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 636 | return cast<User>(V); |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | /// AddOne - Add one to a ConstantInt |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 641 | static Constant *AddOne(Constant *C, LLVMContext *Context) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 642 | return Context->getConstantExprAdd(C, |
| 643 | Context->getConstantInt(C->getType(), 1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 644 | } |
| 645 | /// SubOne - Subtract one from a ConstantInt |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 646 | static Constant *SubOne(ConstantInt *C, LLVMContext *Context) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 647 | return Context->getConstantExprSub(C, |
| 648 | Context->getConstantInt(C->getType(), 1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 649 | } |
| Nick Lewycky | 9d798f9 | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 650 | /// MultiplyOverflows - True if the multiply can not be expressed in an int |
| 651 | /// this size. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 652 | static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2, bool sign, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 653 | LLVMContext *Context) { |
| Nick Lewycky | 9d798f9 | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 654 | uint32_t W = C1->getBitWidth(); |
| 655 | APInt LHSExt = C1->getValue(), RHSExt = C2->getValue(); |
| 656 | if (sign) { |
| 657 | LHSExt.sext(W * 2); |
| 658 | RHSExt.sext(W * 2); |
| 659 | } else { |
| 660 | LHSExt.zext(W * 2); |
| 661 | RHSExt.zext(W * 2); |
| 662 | } |
| 663 | |
| 664 | APInt MulExt = LHSExt * RHSExt; |
| 665 | |
| 666 | if (sign) { |
| 667 | APInt Min = APInt::getSignedMinValue(W).sext(W * 2); |
| 668 | APInt Max = APInt::getSignedMaxValue(W).sext(W * 2); |
| 669 | return MulExt.slt(Min) || MulExt.sgt(Max); |
| 670 | } else |
| 671 | return MulExt.ugt(APInt::getLowBitsSet(W * 2, W)); |
| 672 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 673 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 674 | |
| 675 | /// ShrinkDemandedConstant - Check to see if the specified operand of the |
| 676 | /// specified instruction is a constant integer. If so, check to see if there |
| 677 | /// are any bits set in the constant that are not demanded. If so, shrink the |
| 678 | /// constant and return true. |
| 679 | static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 680 | APInt Demanded, LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 681 | assert(I && "No instruction?"); |
| 682 | assert(OpNo < I->getNumOperands() && "Operand index too large"); |
| 683 | |
| 684 | // If the operand is not a constant integer, nothing to do. |
| 685 | ConstantInt *OpC = dyn_cast<ConstantInt>(I->getOperand(OpNo)); |
| 686 | if (!OpC) return false; |
| 687 | |
| 688 | // If there are no bits set that aren't demanded, nothing to do. |
| 689 | Demanded.zextOrTrunc(OpC->getValue().getBitWidth()); |
| 690 | if ((~Demanded & OpC->getValue()) == 0) |
| 691 | return false; |
| 692 | |
| 693 | // This instruction is producing bits that are not demanded. Shrink the RHS. |
| 694 | Demanded &= OpC->getValue(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 695 | I->setOperand(OpNo, Context->getConstantInt(Demanded)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 696 | return true; |
| 697 | } |
| 698 | |
| 699 | // ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a |
| 700 | // set of known zero and one bits, compute the maximum and minimum values that |
| 701 | // could have the specified known zero and known one bits, returning them in |
| 702 | // min/max. |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 703 | static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 704 | const APInt& KnownOne, |
| 705 | APInt& Min, APInt& Max) { |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 706 | assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() && |
| 707 | KnownZero.getBitWidth() == Min.getBitWidth() && |
| 708 | KnownZero.getBitWidth() == Max.getBitWidth() && |
| 709 | "KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 710 | APInt UnknownBits = ~(KnownZero|KnownOne); |
| 711 | |
| 712 | // The minimum value is when all unknown bits are zeros, EXCEPT for the sign |
| 713 | // bit if it is unknown. |
| 714 | Min = KnownOne; |
| 715 | Max = KnownOne|UnknownBits; |
| 716 | |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 717 | if (UnknownBits.isNegative()) { // Sign bit is unknown |
| 718 | Min.set(Min.getBitWidth()-1); |
| 719 | Max.clear(Max.getBitWidth()-1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and |
| 724 | // a set of known zero and one bits, compute the maximum and minimum values that |
| 725 | // could have the specified known zero and known one bits, returning them in |
| 726 | // min/max. |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 727 | static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero, |
| Chris Lattner | b933ea6 | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 728 | const APInt &KnownOne, |
| 729 | APInt &Min, APInt &Max) { |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 730 | assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() && |
| 731 | KnownZero.getBitWidth() == Min.getBitWidth() && |
| 732 | KnownZero.getBitWidth() == Max.getBitWidth() && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 733 | "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth."); |
| 734 | APInt UnknownBits = ~(KnownZero|KnownOne); |
| 735 | |
| 736 | // The minimum value is when the unknown bits are all zeros. |
| 737 | Min = KnownOne; |
| 738 | // The maximum value is when the unknown bits are all ones. |
| 739 | Max = KnownOne|UnknownBits; |
| 740 | } |
| 741 | |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 742 | /// SimplifyDemandedInstructionBits - Inst is an integer instruction that |
| 743 | /// SimplifyDemandedBits knows about. See if the instruction has any |
| 744 | /// properties that allow us to simplify its operands. |
| 745 | bool InstCombiner::SimplifyDemandedInstructionBits(Instruction &Inst) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 746 | unsigned BitWidth = Inst.getType()->getScalarSizeInBits(); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 747 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 748 | APInt DemandedMask(APInt::getAllOnesValue(BitWidth)); |
| 749 | |
| 750 | Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, |
| 751 | KnownZero, KnownOne, 0); |
| 752 | if (V == 0) return false; |
| 753 | if (V == &Inst) return true; |
| 754 | ReplaceInstUsesWith(Inst, V); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | /// SimplifyDemandedBits - This form of SimplifyDemandedBits simplifies the |
| 759 | /// specified instruction operand if possible, updating it in place. It returns |
| 760 | /// true if it made any change and false otherwise. |
| 761 | bool InstCombiner::SimplifyDemandedBits(Use &U, APInt DemandedMask, |
| 762 | APInt &KnownZero, APInt &KnownOne, |
| 763 | unsigned Depth) { |
| 764 | Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, |
| 765 | KnownZero, KnownOne, Depth); |
| 766 | if (NewVal == 0) return false; |
| 767 | U.set(NewVal); |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | |
| 772 | /// SimplifyDemandedUseBits - This function attempts to replace V with a simpler |
| 773 | /// value based on the demanded bits. When this function is called, it is known |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 774 | /// that only the bits set in DemandedMask of the result of V are ever used |
| 775 | /// downstream. Consequently, depending on the mask and V, it may be possible |
| 776 | /// to replace V with a constant or one of its operands. In such cases, this |
| 777 | /// function does the replacement and returns true. In all other cases, it |
| 778 | /// returns false after analyzing the expression and setting KnownOne and known |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 779 | /// to be one in the expression. KnownZero contains all the bits that are known |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 780 | /// to be zero in the expression. These are provided to potentially allow the |
| 781 | /// caller (which might recursively be SimplifyDemandedBits itself) to simplify |
| 782 | /// the expression. KnownOne and KnownZero always follow the invariant that |
| 783 | /// KnownOne & KnownZero == 0. That is, a bit can't be both 1 and 0. Note that |
| 784 | /// the bits in KnownOne and KnownZero may only be accurate for those bits set |
| 785 | /// in DemandedMask. Note also that the bitwidth of V, DemandedMask, KnownZero |
| 786 | /// and KnownOne must all be the same. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 787 | /// |
| 788 | /// This returns null if it did not change anything and it permits no |
| 789 | /// simplification. This returns V itself if it did some simplification of V's |
| 790 | /// operands based on the information about what bits are demanded. This returns |
| 791 | /// some other non-null value if it found out that V is equal to another value |
| 792 | /// in the context where the specified bits are demanded, but not for all users. |
| 793 | Value *InstCombiner::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
| 794 | APInt &KnownZero, APInt &KnownOne, |
| 795 | unsigned Depth) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 796 | assert(V != 0 && "Null pointer of Value???"); |
| 797 | assert(Depth <= 6 && "Limit Search Depth"); |
| 798 | uint32_t BitWidth = DemandedMask.getBitWidth(); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 799 | const Type *VTy = V->getType(); |
| 800 | assert((TD || !isa<PointerType>(VTy)) && |
| 801 | "SimplifyDemandedBits needs to know bit widths!"); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 802 | assert((!TD || TD->getTypeSizeInBits(VTy->getScalarType()) == BitWidth) && |
| 803 | (!VTy->isIntOrIntVector() || |
| 804 | VTy->getScalarSizeInBits() == BitWidth) && |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 805 | KnownZero.getBitWidth() == BitWidth && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 806 | KnownOne.getBitWidth() == BitWidth && |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 807 | "Value *V, DemandedMask, KnownZero and KnownOne " |
| 808 | "must have same BitWidth"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 809 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 810 | // We know all of the bits for a constant! |
| 811 | KnownOne = CI->getValue() & DemandedMask; |
| 812 | KnownZero = ~KnownOne & DemandedMask; |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 813 | return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 814 | } |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 815 | if (isa<ConstantPointerNull>(V)) { |
| 816 | // We know all of the bits for a constant! |
| 817 | KnownOne.clear(); |
| 818 | KnownZero = DemandedMask; |
| 819 | return 0; |
| 820 | } |
| 821 | |
| Chris Lattner | c5d7e4e | 2009-01-31 07:26:06 +0000 | [diff] [blame] | 822 | KnownZero.clear(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 823 | KnownOne.clear(); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 824 | if (DemandedMask == 0) { // Not demanding any bits from V. |
| 825 | if (isa<UndefValue>(V)) |
| 826 | return 0; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 827 | return Context->getUndef(VTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| Chris Lattner | 0881733 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 830 | if (Depth == 6) // Limit search depth. |
| 831 | return 0; |
| 832 | |
| Chris Lattner | cd8d44c | 2009-01-31 08:40:03 +0000 | [diff] [blame] | 833 | APInt LHSKnownZero(BitWidth, 0), LHSKnownOne(BitWidth, 0); |
| 834 | APInt &RHSKnownZero = KnownZero, &RHSKnownOne = KnownOne; |
| 835 | |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 836 | Instruction *I = dyn_cast<Instruction>(V); |
| 837 | if (!I) { |
| 838 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
| 839 | return 0; // Only analyze instructions. |
| 840 | } |
| 841 | |
| Chris Lattner | 0881733 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 842 | // If there are multiple uses of this value and we aren't at the root, then |
| 843 | // we can't do any simplifications of the operands, because DemandedMask |
| 844 | // only reflects the bits demanded by *one* of the users. |
| 845 | if (Depth != 0 && !I->hasOneUse()) { |
| Chris Lattner | cd8d44c | 2009-01-31 08:40:03 +0000 | [diff] [blame] | 846 | // Despite the fact that we can't simplify this instruction in all User's |
| 847 | // context, we can at least compute the knownzero/knownone bits, and we can |
| 848 | // do simplifications that apply to *just* the one user if we know that |
| 849 | // this instruction has a simpler value in that context. |
| 850 | if (I->getOpcode() == Instruction::And) { |
| 851 | // If either the LHS or the RHS are Zero, the result is zero. |
| 852 | ComputeMaskedBits(I->getOperand(1), DemandedMask, |
| 853 | RHSKnownZero, RHSKnownOne, Depth+1); |
| 854 | ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownZero, |
| 855 | LHSKnownZero, LHSKnownOne, Depth+1); |
| 856 | |
| 857 | // If all of the demanded bits are known 1 on one side, return the other. |
| 858 | // These bits cannot contribute to the result of the 'and' in this |
| 859 | // context. |
| 860 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 861 | (DemandedMask & ~LHSKnownZero)) |
| 862 | return I->getOperand(0); |
| 863 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 864 | (DemandedMask & ~RHSKnownZero)) |
| 865 | return I->getOperand(1); |
| 866 | |
| 867 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 868 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 869 | return Context->getNullValue(VTy); |
| Chris Lattner | cd8d44c | 2009-01-31 08:40:03 +0000 | [diff] [blame] | 870 | |
| 871 | } else if (I->getOpcode() == Instruction::Or) { |
| 872 | // We can simplify (X|Y) -> X or Y in the user's context if we know that |
| 873 | // only bits from X or Y are demanded. |
| 874 | |
| 875 | // If either the LHS or the RHS are One, the result is One. |
| 876 | ComputeMaskedBits(I->getOperand(1), DemandedMask, |
| 877 | RHSKnownZero, RHSKnownOne, Depth+1); |
| 878 | ComputeMaskedBits(I->getOperand(0), DemandedMask & ~RHSKnownOne, |
| 879 | LHSKnownZero, LHSKnownOne, Depth+1); |
| 880 | |
| 881 | // If all of the demanded bits are known zero on one side, return the |
| 882 | // other. These bits cannot contribute to the result of the 'or' in this |
| 883 | // context. |
| 884 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 885 | (DemandedMask & ~LHSKnownOne)) |
| 886 | return I->getOperand(0); |
| 887 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 888 | (DemandedMask & ~RHSKnownOne)) |
| 889 | return I->getOperand(1); |
| 890 | |
| 891 | // If all of the potentially set bits on one side are known to be set on |
| 892 | // the other side, just use the 'other' side. |
| 893 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 894 | (DemandedMask & (~RHSKnownZero))) |
| 895 | return I->getOperand(0); |
| 896 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 897 | (DemandedMask & (~LHSKnownZero))) |
| 898 | return I->getOperand(1); |
| 899 | } |
| 900 | |
| Chris Lattner | 0881733 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 901 | // Compute the KnownZero/KnownOne bits to simplify things downstream. |
| 902 | ComputeMaskedBits(I, DemandedMask, KnownZero, KnownOne, Depth); |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | // If this is the root being simplified, allow it to have multiple uses, |
| 907 | // just set the DemandedMask to all bits so that we can try to simplify the |
| 908 | // operands. This allows visitTruncInst (for example) to simplify the |
| 909 | // operand of a trunc without duplicating all the logic below. |
| 910 | if (Depth == 0 && !V->hasOneUse()) |
| 911 | DemandedMask = APInt::getAllOnesValue(BitWidth); |
| 912 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 913 | switch (I->getOpcode()) { |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 914 | default: |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 915 | ComputeMaskedBits(I, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 916 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 917 | case Instruction::And: |
| 918 | // If either the LHS or the RHS are Zero, the result is zero. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 919 | if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| 920 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 921 | SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownZero, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 922 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 923 | return I; |
| 924 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 925 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 926 | |
| 927 | // If all of the demanded bits are known 1 on one side, return the other. |
| 928 | // These bits cannot contribute to the result of the 'and'. |
| 929 | if ((DemandedMask & ~LHSKnownZero & RHSKnownOne) == |
| 930 | (DemandedMask & ~LHSKnownZero)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 931 | return I->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 932 | if ((DemandedMask & ~RHSKnownZero & LHSKnownOne) == |
| 933 | (DemandedMask & ~RHSKnownZero)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 934 | return I->getOperand(1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 935 | |
| 936 | // If all of the demanded bits in the inputs are known zeros, return zero. |
| 937 | if ((DemandedMask & (RHSKnownZero|LHSKnownZero)) == DemandedMask) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 938 | return Context->getNullValue(VTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 939 | |
| 940 | // If the RHS is a constant, see if we can simplify it. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 941 | if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnownZero, Context)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 942 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 943 | |
| 944 | // Output known-1 bits are only known if set in both the LHS & RHS. |
| 945 | RHSKnownOne &= LHSKnownOne; |
| 946 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
| 947 | RHSKnownZero |= LHSKnownZero; |
| 948 | break; |
| 949 | case Instruction::Or: |
| 950 | // If either the LHS or the RHS are One, the result is One. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 951 | if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| 952 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 953 | SimplifyDemandedBits(I->getOperandUse(0), DemandedMask & ~RHSKnownOne, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 954 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 955 | return I; |
| 956 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 957 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 958 | |
| 959 | // If all of the demanded bits are known zero on one side, return the other. |
| 960 | // These bits cannot contribute to the result of the 'or'. |
| 961 | if ((DemandedMask & ~LHSKnownOne & RHSKnownZero) == |
| 962 | (DemandedMask & ~LHSKnownOne)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 963 | return I->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 964 | if ((DemandedMask & ~RHSKnownOne & LHSKnownZero) == |
| 965 | (DemandedMask & ~RHSKnownOne)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 966 | return I->getOperand(1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 967 | |
| 968 | // If all of the potentially set bits on one side are known to be set on |
| 969 | // the other side, just use the 'other' side. |
| 970 | if ((DemandedMask & (~RHSKnownZero) & LHSKnownOne) == |
| 971 | (DemandedMask & (~RHSKnownZero))) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 972 | return I->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 973 | if ((DemandedMask & (~LHSKnownZero) & RHSKnownOne) == |
| 974 | (DemandedMask & (~LHSKnownZero))) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 975 | return I->getOperand(1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 976 | |
| 977 | // If the RHS is a constant, see if we can simplify it. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 978 | if (ShrinkDemandedConstant(I, 1, DemandedMask, Context)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 979 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 980 | |
| 981 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
| 982 | RHSKnownZero &= LHSKnownZero; |
| 983 | // Output known-1 are known to be set if set in either the LHS | RHS. |
| 984 | RHSKnownOne |= LHSKnownOne; |
| 985 | break; |
| 986 | case Instruction::Xor: { |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 987 | if (SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| 988 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 989 | SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 990 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 991 | return I; |
| 992 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 993 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 994 | |
| 995 | // If all of the demanded bits are known zero on one side, return the other. |
| 996 | // These bits cannot contribute to the result of the 'xor'. |
| 997 | if ((DemandedMask & RHSKnownZero) == DemandedMask) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 998 | return I->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 999 | if ((DemandedMask & LHSKnownZero) == DemandedMask) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1000 | return I->getOperand(1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
| 1003 | APInt KnownZeroOut = (RHSKnownZero & LHSKnownZero) | |
| 1004 | (RHSKnownOne & LHSKnownOne); |
| 1005 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
| 1006 | APInt KnownOneOut = (RHSKnownZero & LHSKnownOne) | |
| 1007 | (RHSKnownOne & LHSKnownZero); |
| 1008 | |
| 1009 | // If all of the demanded bits are known to be zero on one side or the |
| 1010 | // other, turn this into an *inclusive* or. |
| 1011 | // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 |
| 1012 | if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) { |
| 1013 | Instruction *Or = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1014 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1015 | I->getName()); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1016 | return InsertNewInstBefore(Or, *I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | // If all of the demanded bits on one side are known, and all of the set |
| 1020 | // bits on that side are also known to be set on the other side, turn this |
| 1021 | // into an AND, as we know the bits will be cleared. |
| 1022 | // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 |
| 1023 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| 1024 | // all known |
| 1025 | if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1026 | Constant *AndC = Context->getConstantInt(~RHSKnownOne & DemandedMask); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1027 | Instruction *And = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1028 | BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp"); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1029 | return InsertNewInstBefore(And, *I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | // If the RHS is a constant, see if we can simplify it. |
| 1034 | // FIXME: for XOR, we prefer to force bits to 1 if they will make a -1. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1035 | if (ShrinkDemandedConstant(I, 1, DemandedMask, Context)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1036 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1037 | |
| 1038 | RHSKnownZero = KnownZeroOut; |
| 1039 | RHSKnownOne = KnownOneOut; |
| 1040 | break; |
| 1041 | } |
| 1042 | case Instruction::Select: |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1043 | if (SimplifyDemandedBits(I->getOperandUse(2), DemandedMask, |
| 1044 | RHSKnownZero, RHSKnownOne, Depth+1) || |
| 1045 | SimplifyDemandedBits(I->getOperandUse(1), DemandedMask, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1046 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1047 | return I; |
| 1048 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| 1049 | assert(!(LHSKnownZero & LHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1050 | |
| 1051 | // If the operands are constants, see if we can simplify them. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1052 | if (ShrinkDemandedConstant(I, 1, DemandedMask, Context) || |
| 1053 | ShrinkDemandedConstant(I, 2, DemandedMask, Context)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1054 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1055 | |
| 1056 | // Only known if known in both the LHS and RHS. |
| 1057 | RHSKnownOne &= LHSKnownOne; |
| 1058 | RHSKnownZero &= LHSKnownZero; |
| 1059 | break; |
| 1060 | case Instruction::Trunc: { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1061 | unsigned truncBf = I->getOperand(0)->getType()->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1062 | DemandedMask.zext(truncBf); |
| 1063 | RHSKnownZero.zext(truncBf); |
| 1064 | RHSKnownOne.zext(truncBf); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1065 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1066 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1067 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1068 | DemandedMask.trunc(BitWidth); |
| 1069 | RHSKnownZero.trunc(BitWidth); |
| 1070 | RHSKnownOne.trunc(BitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1071 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | case Instruction::BitCast: |
| Dan Gohman | 72d5fbb | 2009-07-01 21:38:46 +0000 | [diff] [blame] | 1075 | if (!I->getOperand(0)->getType()->isIntOrIntVector()) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1076 | return false; // vector->int or fp->int? |
| Dan Gohman | 72d5fbb | 2009-07-01 21:38:46 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (const VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) { |
| 1079 | if (const VectorType *SrcVTy = |
| 1080 | dyn_cast<VectorType>(I->getOperand(0)->getType())) { |
| 1081 | if (DstVTy->getNumElements() != SrcVTy->getNumElements()) |
| 1082 | // Don't touch a bitcast between vectors of different element counts. |
| 1083 | return false; |
| 1084 | } else |
| 1085 | // Don't touch a scalar-to-vector bitcast. |
| 1086 | return false; |
| 1087 | } else if (isa<VectorType>(I->getOperand(0)->getType())) |
| 1088 | // Don't touch a vector-to-scalar bitcast. |
| 1089 | return false; |
| 1090 | |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1091 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1092 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1093 | return I; |
| 1094 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1095 | break; |
| 1096 | case Instruction::ZExt: { |
| 1097 | // Compute the bits in the result that are not present in the input. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1098 | unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1099 | |
| 1100 | DemandedMask.trunc(SrcBitWidth); |
| 1101 | RHSKnownZero.trunc(SrcBitWidth); |
| 1102 | RHSKnownOne.trunc(SrcBitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1103 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMask, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1104 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1105 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1106 | DemandedMask.zext(BitWidth); |
| 1107 | RHSKnownZero.zext(BitWidth); |
| 1108 | RHSKnownOne.zext(BitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1109 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1110 | // The top bits are known to be zero. |
| 1111 | RHSKnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth); |
| 1112 | break; |
| 1113 | } |
| 1114 | case Instruction::SExt: { |
| 1115 | // Compute the bits in the result that are not present in the input. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1116 | unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1117 | |
| 1118 | APInt InputDemandedBits = DemandedMask & |
| 1119 | APInt::getLowBitsSet(BitWidth, SrcBitWidth); |
| 1120 | |
| 1121 | APInt NewBits(APInt::getHighBitsSet(BitWidth, BitWidth - SrcBitWidth)); |
| 1122 | // If any of the sign extended bits are demanded, we know that the sign |
| 1123 | // bit is demanded. |
| 1124 | if ((NewBits & DemandedMask) != 0) |
| 1125 | InputDemandedBits.set(SrcBitWidth-1); |
| 1126 | |
| 1127 | InputDemandedBits.trunc(SrcBitWidth); |
| 1128 | RHSKnownZero.trunc(SrcBitWidth); |
| 1129 | RHSKnownOne.trunc(SrcBitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1130 | if (SimplifyDemandedBits(I->getOperandUse(0), InputDemandedBits, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1131 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1132 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1133 | InputDemandedBits.zext(BitWidth); |
| 1134 | RHSKnownZero.zext(BitWidth); |
| 1135 | RHSKnownOne.zext(BitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1136 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1137 | |
| 1138 | // If the sign bit of the input is known set or clear, then we know the |
| 1139 | // top bits of the result. |
| 1140 | |
| 1141 | // If the input sign bit is known zero, or if the NewBits are not demanded |
| 1142 | // convert this into a zero extension. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1143 | if (RHSKnownZero[SrcBitWidth-1] || (NewBits & ~DemandedMask) == NewBits) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1144 | // Convert to ZExt cast |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1145 | CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName()); |
| 1146 | return InsertNewInstBefore(NewCast, *I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1147 | } else if (RHSKnownOne[SrcBitWidth-1]) { // Input sign bit known set |
| 1148 | RHSKnownOne |= NewBits; |
| 1149 | } |
| 1150 | break; |
| 1151 | } |
| 1152 | case Instruction::Add: { |
| 1153 | // Figure out what the input bits are. If the top bits of the and result |
| 1154 | // are not demanded, then the add doesn't demand them from its input |
| 1155 | // either. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1156 | unsigned NLZ = DemandedMask.countLeadingZeros(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1157 | |
| 1158 | // If there is a constant on the RHS, there are a variety of xformations |
| 1159 | // we can do. |
| 1160 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1161 | // If null, this should be simplified elsewhere. Some of the xforms here |
| 1162 | // won't work if the RHS is zero. |
| 1163 | if (RHS->isZero()) |
| 1164 | break; |
| 1165 | |
| 1166 | // If the top bit of the output is demanded, demand everything from the |
| 1167 | // input. Otherwise, we demand all the input bits except NLZ top bits. |
| 1168 | APInt InDemandedBits(APInt::getLowBitsSet(BitWidth, BitWidth - NLZ)); |
| 1169 | |
| 1170 | // Find information about known zero/one bits in the input. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1171 | if (SimplifyDemandedBits(I->getOperandUse(0), InDemandedBits, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1172 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1173 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1174 | |
| 1175 | // If the RHS of the add has bits set that can't affect the input, reduce |
| 1176 | // the constant. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1177 | if (ShrinkDemandedConstant(I, 1, InDemandedBits, Context)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1178 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1179 | |
| 1180 | // Avoid excess work. |
| 1181 | if (LHSKnownZero == 0 && LHSKnownOne == 0) |
| 1182 | break; |
| 1183 | |
| 1184 | // Turn it into OR if input bits are zero. |
| 1185 | if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) { |
| 1186 | Instruction *Or = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1187 | BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1188 | I->getName()); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1189 | return InsertNewInstBefore(Or, *I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | // We can say something about the output known-zero and known-one bits, |
| 1193 | // depending on potential carries from the input constant and the |
| 1194 | // unknowns. For example if the LHS is known to have at most the 0x0F0F0 |
| 1195 | // bits set and the RHS constant is 0x01001, then we know we have a known |
| 1196 | // one mask of 0x00001 and a known zero mask of 0xE0F0E. |
| 1197 | |
| 1198 | // To compute this, we first compute the potential carry bits. These are |
| 1199 | // the bits which may be modified. I'm not aware of a better way to do |
| 1200 | // this scan. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1201 | const APInt &RHSVal = RHS->getValue(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1202 | APInt CarryBits((~LHSKnownZero + RHSVal) ^ (~LHSKnownZero ^ RHSVal)); |
| 1203 | |
| 1204 | // Now that we know which bits have carries, compute the known-1/0 sets. |
| 1205 | |
| 1206 | // Bits are known one if they are known zero in one operand and one in the |
| 1207 | // other, and there is no input carry. |
| 1208 | RHSKnownOne = ((LHSKnownZero & RHSVal) | |
| 1209 | (LHSKnownOne & ~RHSVal)) & ~CarryBits; |
| 1210 | |
| 1211 | // Bits are known zero if they are known zero in both operands and there |
| 1212 | // is no input carry. |
| 1213 | RHSKnownZero = LHSKnownZero & ~RHSVal & ~CarryBits; |
| 1214 | } else { |
| 1215 | // If the high-bits of this ADD are not demanded, then it does not demand |
| 1216 | // the high bits of its LHS or RHS. |
| 1217 | if (DemandedMask[BitWidth-1] == 0) { |
| 1218 | // Right fill the mask of bits for this ADD to demand the most |
| 1219 | // significant bit and all those below it. |
| 1220 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1221 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps, |
| 1222 | LHSKnownZero, LHSKnownOne, Depth+1) || |
| 1223 | SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1224 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1225 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | break; |
| 1229 | } |
| 1230 | case Instruction::Sub: |
| 1231 | // If the high-bits of this SUB are not demanded, then it does not demand |
| 1232 | // the high bits of its LHS or RHS. |
| 1233 | if (DemandedMask[BitWidth-1] == 0) { |
| 1234 | // Right fill the mask of bits for this SUB to demand the most |
| 1235 | // significant bit and all those below it. |
| 1236 | uint32_t NLZ = DemandedMask.countLeadingZeros(); |
| 1237 | APInt DemandedFromOps(APInt::getLowBitsSet(BitWidth, BitWidth-NLZ)); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1238 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedFromOps, |
| 1239 | LHSKnownZero, LHSKnownOne, Depth+1) || |
| 1240 | SimplifyDemandedBits(I->getOperandUse(1), DemandedFromOps, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1241 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1242 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1243 | } |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1244 | // Otherwise just hand the sub off to ComputeMaskedBits to fill in |
| 1245 | // the known zeros and ones. |
| 1246 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1247 | break; |
| 1248 | case Instruction::Shl: |
| 1249 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1250 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
| 1251 | APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1252 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1253 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1254 | return I; |
| 1255 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1256 | RHSKnownZero <<= ShiftAmt; |
| 1257 | RHSKnownOne <<= ShiftAmt; |
| 1258 | // low bits known zero. |
| 1259 | if (ShiftAmt) |
| 1260 | RHSKnownZero |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
| 1261 | } |
| 1262 | break; |
| 1263 | case Instruction::LShr: |
| 1264 | // For a logical shift right |
| 1265 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1266 | uint64_t ShiftAmt = SA->getLimitedValue(BitWidth); |
| 1267 | |
| 1268 | // Unsigned shift right. |
| 1269 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1270 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1271 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1272 | return I; |
| 1273 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1274 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1275 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1276 | if (ShiftAmt) { |
| 1277 | // Compute the new bits that are at the top now. |
| 1278 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 1279 | RHSKnownZero |= HighBits; // high bits known zero. |
| 1280 | } |
| 1281 | } |
| 1282 | break; |
| 1283 | case Instruction::AShr: |
| 1284 | // If this is an arithmetic shift right and only the low-bit is set, we can |
| 1285 | // always convert this into a logical shr, even if the shift amount is |
| 1286 | // variable. The low bit of the shift cannot be an input sign bit unless |
| 1287 | // the shift amount is >= the size of the datatype, which is undefined. |
| 1288 | if (DemandedMask == 1) { |
| 1289 | // Perform the logical shift right. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1290 | Instruction *NewVal = BinaryOperator::CreateLShr( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1291 | I->getOperand(0), I->getOperand(1), I->getName()); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1292 | return InsertNewInstBefore(NewVal, *I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | // If the sign bit is the only bit demanded by this ashr, then there is no |
| 1296 | // need to do it, the shift doesn't change the high bit. |
| 1297 | if (DemandedMask.isSignBit()) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1298 | return I->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 1301 | uint32_t ShiftAmt = SA->getLimitedValue(BitWidth); |
| 1302 | |
| 1303 | // Signed shift right. |
| 1304 | APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); |
| 1305 | // If any of the "high bits" are demanded, we should set the sign bit as |
| 1306 | // demanded. |
| 1307 | if (DemandedMask.countLeadingZeros() <= ShiftAmt) |
| 1308 | DemandedMaskIn.set(BitWidth-1); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1309 | if (SimplifyDemandedBits(I->getOperandUse(0), DemandedMaskIn, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1310 | RHSKnownZero, RHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1311 | return I; |
| 1312 | assert(!(RHSKnownZero & RHSKnownOne) && "Bits known to be one AND zero?"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1313 | // Compute the new bits that are at the top now. |
| 1314 | APInt HighBits(APInt::getHighBitsSet(BitWidth, ShiftAmt)); |
| 1315 | RHSKnownZero = APIntOps::lshr(RHSKnownZero, ShiftAmt); |
| 1316 | RHSKnownOne = APIntOps::lshr(RHSKnownOne, ShiftAmt); |
| 1317 | |
| 1318 | // Handle the sign bits. |
| 1319 | APInt SignBit(APInt::getSignBit(BitWidth)); |
| 1320 | // Adjust to where it is now in the mask. |
| 1321 | SignBit = APIntOps::lshr(SignBit, ShiftAmt); |
| 1322 | |
| 1323 | // If the input sign bit is known to be zero, or if none of the top bits |
| 1324 | // are demanded, turn this into an unsigned shift right. |
| Zhou Sheng | 533604e | 2008-06-06 08:32:05 +0000 | [diff] [blame] | 1325 | if (BitWidth <= ShiftAmt || RHSKnownZero[BitWidth-ShiftAmt-1] || |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1326 | (HighBits & ~DemandedMask) == HighBits) { |
| 1327 | // Perform the logical shift right. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1328 | Instruction *NewVal = BinaryOperator::CreateLShr( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1329 | I->getOperand(0), SA, I->getName()); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1330 | return InsertNewInstBefore(NewVal, *I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1331 | } else if ((RHSKnownOne & SignBit) != 0) { // New bits are known one. |
| 1332 | RHSKnownOne |= HighBits; |
| 1333 | } |
| 1334 | } |
| 1335 | break; |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1336 | case Instruction::SRem: |
| 1337 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| Nick Lewycky | cfaaece | 2008-11-02 02:41:50 +0000 | [diff] [blame] | 1338 | APInt RA = Rem->getValue().abs(); |
| 1339 | if (RA.isPowerOf2()) { |
| Eli Friedman | 579c572 | 2009-06-17 02:57:36 +0000 | [diff] [blame] | 1340 | if (DemandedMask.ult(RA)) // srem won't affect demanded bits |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1341 | return I->getOperand(0); |
| Nick Lewycky | 245de42 | 2008-07-12 05:04:38 +0000 | [diff] [blame] | 1342 | |
| Nick Lewycky | cfaaece | 2008-11-02 02:41:50 +0000 | [diff] [blame] | 1343 | APInt LowBits = RA - 1; |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1344 | APInt Mask2 = LowBits | APInt::getSignBit(BitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1345 | if (SimplifyDemandedBits(I->getOperandUse(0), Mask2, |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1346 | LHSKnownZero, LHSKnownOne, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1347 | return I; |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1348 | |
| 1349 | if (LHSKnownZero[BitWidth-1] || ((LHSKnownZero & LowBits) == LowBits)) |
| 1350 | LHSKnownZero |= ~LowBits; |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1351 | |
| 1352 | KnownZero |= LHSKnownZero & DemandedMask; |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1353 | |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1354 | assert(!(KnownZero & KnownOne) && "Bits known to be one AND zero?"); |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | break; |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1358 | case Instruction::URem: { |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1359 | APInt KnownZero2(BitWidth, 0), KnownOne2(BitWidth, 0); |
| 1360 | APInt AllOnes = APInt::getAllOnesValue(BitWidth); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1361 | if (SimplifyDemandedBits(I->getOperandUse(0), AllOnes, |
| 1362 | KnownZero2, KnownOne2, Depth+1) || |
| 1363 | SimplifyDemandedBits(I->getOperandUse(1), AllOnes, |
| Dan Gohman | 23ea06d | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1364 | KnownZero2, KnownOne2, Depth+1)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1365 | return I; |
| Dan Gohman | 23ea06d | 2008-05-01 19:13:24 +0000 | [diff] [blame] | 1366 | |
| Chris Lattner | ee5417c | 2009-01-21 18:09:24 +0000 | [diff] [blame] | 1367 | unsigned Leaders = KnownZero2.countLeadingOnes(); |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1368 | Leaders = std::max(Leaders, |
| 1369 | KnownZero2.countLeadingOnes()); |
| 1370 | KnownZero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 1371 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1372 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1373 | case Instruction::Call: |
| 1374 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
| 1375 | switch (II->getIntrinsicID()) { |
| 1376 | default: break; |
| 1377 | case Intrinsic::bswap: { |
| 1378 | // If the only bits demanded come from one byte of the bswap result, |
| 1379 | // just shift the input byte into position to eliminate the bswap. |
| 1380 | unsigned NLZ = DemandedMask.countLeadingZeros(); |
| 1381 | unsigned NTZ = DemandedMask.countTrailingZeros(); |
| 1382 | |
| 1383 | // Round NTZ down to the next byte. If we have 11 trailing zeros, then |
| 1384 | // we need all the bits down to bit 8. Likewise, round NLZ. If we |
| 1385 | // have 14 leading zeros, round to 8. |
| 1386 | NLZ &= ~7; |
| 1387 | NTZ &= ~7; |
| 1388 | // If we need exactly one byte, we can do this transformation. |
| 1389 | if (BitWidth-NLZ-NTZ == 8) { |
| 1390 | unsigned ResultBit = NTZ; |
| 1391 | unsigned InputBit = BitWidth-NTZ-8; |
| 1392 | |
| 1393 | // Replace this with either a left or right shift to get the byte into |
| 1394 | // the right place. |
| 1395 | Instruction *NewVal; |
| 1396 | if (InputBit > ResultBit) |
| 1397 | NewVal = BinaryOperator::CreateLShr(I->getOperand(1), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1398 | Context->getConstantInt(I->getType(), InputBit-ResultBit)); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1399 | else |
| 1400 | NewVal = BinaryOperator::CreateShl(I->getOperand(1), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1401 | Context->getConstantInt(I->getType(), ResultBit-InputBit)); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1402 | NewVal->takeName(I); |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 1403 | return InsertNewInstBefore(NewVal, *I); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | // TODO: Could compute known zero/one bits based on the input. |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| Chris Lattner | 4946e22 | 2008-06-18 18:11:55 +0000 | [diff] [blame] | 1411 | ComputeMaskedBits(V, DemandedMask, RHSKnownZero, RHSKnownOne, Depth); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 1412 | break; |
| Dan Gohman | bec1605 | 2008-04-28 17:02:21 +0000 | [diff] [blame] | 1413 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1414 | |
| 1415 | // If the client is only demanding bits that we know, return the known |
| 1416 | // constant. |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 1417 | if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1418 | Constant *C = Context->getConstantInt(RHSKnownOne); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 1419 | if (isa<PointerType>(V->getType())) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1420 | C = Context->getConstantExprIntToPtr(C, V->getType()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 1421 | return C; |
| 1422 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1427 | /// SimplifyDemandedVectorElts - The specified value produces a vector with |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1428 | /// any number of elements. DemandedElts contains the set of elements that are |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1429 | /// actually used by the caller. This method analyzes which elements of the |
| 1430 | /// operand are undef and returns that information in UndefElts. |
| 1431 | /// |
| 1432 | /// If the information about demanded elements can be used to simplify the |
| 1433 | /// operation, the operation is simplified, then the resultant value is |
| 1434 | /// returned. This returns null if no change was made. |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1435 | Value *InstCombiner::SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
| 1436 | APInt& UndefElts, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1437 | unsigned Depth) { |
| 1438 | unsigned VWidth = cast<VectorType>(V->getType())->getNumElements(); |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1439 | APInt EltMask(APInt::getAllOnesValue(VWidth)); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1440 | assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1441 | |
| 1442 | if (isa<UndefValue>(V)) { |
| 1443 | // If the entire vector is undefined, just return this info. |
| 1444 | UndefElts = EltMask; |
| 1445 | return 0; |
| 1446 | } else if (DemandedElts == 0) { // If nothing is demanded, provide undef. |
| 1447 | UndefElts = EltMask; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1448 | return Context->getUndef(V->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1449 | } |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1450 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1451 | UndefElts = 0; |
| 1452 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) { |
| 1453 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1454 | Constant *Undef = Context->getUndef(EltTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1455 | |
| 1456 | std::vector<Constant*> Elts; |
| 1457 | for (unsigned i = 0; i != VWidth; ++i) |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1458 | if (!DemandedElts[i]) { // If not demanded, set to undef. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1459 | Elts.push_back(Undef); |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1460 | UndefElts.set(i); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1461 | } else if (isa<UndefValue>(CP->getOperand(i))) { // Already undef. |
| 1462 | Elts.push_back(Undef); |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1463 | UndefElts.set(i); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1464 | } else { // Otherwise, defined. |
| 1465 | Elts.push_back(CP->getOperand(i)); |
| 1466 | } |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1467 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1468 | // If we changed the constant, return it. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1469 | Constant *NewCP = Context->getConstantVector(Elts); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1470 | return NewCP != CP ? NewCP : 0; |
| 1471 | } else if (isa<ConstantAggregateZero>(V)) { |
| 1472 | // Simplify the CAZ to a ConstantVector where the non-demanded elements are |
| 1473 | // set to undef. |
| Mon P Wang | 927daf5 | 2008-11-06 22:52:21 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Check if this is identity. If so, return 0 since we are not simplifying |
| 1476 | // anything. |
| 1477 | if (DemandedElts == ((1ULL << VWidth) -1)) |
| 1478 | return 0; |
| 1479 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1480 | const Type *EltTy = cast<VectorType>(V->getType())->getElementType(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1481 | Constant *Zero = Context->getNullValue(EltTy); |
| 1482 | Constant *Undef = Context->getUndef(EltTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1483 | std::vector<Constant*> Elts; |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1484 | for (unsigned i = 0; i != VWidth; ++i) { |
| 1485 | Constant *Elt = DemandedElts[i] ? Zero : Undef; |
| 1486 | Elts.push_back(Elt); |
| 1487 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1488 | UndefElts = DemandedElts ^ EltMask; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1489 | return Context->getConstantVector(Elts); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1492 | // Limit search depth. |
| 1493 | if (Depth == 10) |
| Dan Gohman | d5f85af | 2009-04-25 17:28:45 +0000 | [diff] [blame] | 1494 | return 0; |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1495 | |
| 1496 | // If multiple users are using the root value, procede with |
| 1497 | // simplification conservatively assuming that all elements |
| 1498 | // are needed. |
| 1499 | if (!V->hasOneUse()) { |
| 1500 | // Quit if we find multiple users of a non-root value though. |
| 1501 | // They'll be handled when it's their turn to be visited by |
| 1502 | // the main instcombine process. |
| 1503 | if (Depth != 0) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1504 | // TODO: Just compute the UndefElts information recursively. |
| Dan Gohman | d5f85af | 2009-04-25 17:28:45 +0000 | [diff] [blame] | 1505 | return 0; |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1506 | |
| 1507 | // Conservatively assume that all elements are needed. |
| 1508 | DemandedElts = EltMask; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | Instruction *I = dyn_cast<Instruction>(V); |
| Dan Gohman | d5f85af | 2009-04-25 17:28:45 +0000 | [diff] [blame] | 1512 | if (!I) return 0; // Only analyze instructions. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1513 | |
| 1514 | bool MadeChange = false; |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1515 | APInt UndefElts2(VWidth, 0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1516 | Value *TmpV; |
| 1517 | switch (I->getOpcode()) { |
| 1518 | default: break; |
| 1519 | |
| 1520 | case Instruction::InsertElement: { |
| 1521 | // If this is a variable index, we don't know which element it overwrites. |
| 1522 | // demand exactly the same input as we produce. |
| 1523 | ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); |
| 1524 | if (Idx == 0) { |
| 1525 | // Note that we can't propagate undef elt info, because we don't know |
| 1526 | // which elt is getting updated. |
| 1527 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1528 | UndefElts2, Depth+1); |
| 1529 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1530 | break; |
| 1531 | } |
| 1532 | |
| 1533 | // If this is inserting an element that isn't demanded, remove this |
| 1534 | // insertelement. |
| 1535 | unsigned IdxNo = Idx->getZExtValue(); |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1536 | if (IdxNo >= VWidth || !DemandedElts[IdxNo]) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1537 | return AddSoonDeadInstToWorklist(*I, 0); |
| 1538 | |
| 1539 | // Otherwise, the element inserted overwrites whatever was there, so the |
| 1540 | // input demanded set is simpler than the output set. |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1541 | APInt DemandedElts2 = DemandedElts; |
| 1542 | DemandedElts2.clear(IdxNo); |
| 1543 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts2, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1544 | UndefElts, Depth+1); |
| 1545 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1546 | |
| 1547 | // The inserted element is defined. |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1548 | UndefElts.clear(IdxNo); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1549 | break; |
| 1550 | } |
| 1551 | case Instruction::ShuffleVector: { |
| 1552 | ShuffleVectorInst *Shuffle = cast<ShuffleVectorInst>(I); |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1553 | uint64_t LHSVWidth = |
| 1554 | cast<VectorType>(Shuffle->getOperand(0)->getType())->getNumElements(); |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1555 | APInt LeftDemanded(LHSVWidth, 0), RightDemanded(LHSVWidth, 0); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1556 | for (unsigned i = 0; i < VWidth; i++) { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1557 | if (DemandedElts[i]) { |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1558 | unsigned MaskVal = Shuffle->getMaskValue(i); |
| 1559 | if (MaskVal != -1u) { |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1560 | assert(MaskVal < LHSVWidth * 2 && |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1561 | "shufflevector mask index out of range!"); |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1562 | if (MaskVal < LHSVWidth) |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1563 | LeftDemanded.set(MaskVal); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1564 | else |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1565 | RightDemanded.set(MaskVal - LHSVWidth); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
| Nate Begeman | b4d176f | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1570 | APInt UndefElts4(LHSVWidth, 0); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1571 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), LeftDemanded, |
| Nate Begeman | b4d176f | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1572 | UndefElts4, Depth+1); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1573 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1574 | |
| Nate Begeman | b4d176f | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1575 | APInt UndefElts3(LHSVWidth, 0); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1576 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), RightDemanded, |
| 1577 | UndefElts3, Depth+1); |
| 1578 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1579 | |
| 1580 | bool NewUndefElts = false; |
| 1581 | for (unsigned i = 0; i < VWidth; i++) { |
| 1582 | unsigned MaskVal = Shuffle->getMaskValue(i); |
| Dan Gohman | 24f6ee2 | 2008-09-10 01:09:32 +0000 | [diff] [blame] | 1583 | if (MaskVal == -1u) { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1584 | UndefElts.set(i); |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1585 | } else if (MaskVal < LHSVWidth) { |
| Nate Begeman | b4d176f | 2009-02-11 22:36:25 +0000 | [diff] [blame] | 1586 | if (UndefElts4[MaskVal]) { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1587 | NewUndefElts = true; |
| 1588 | UndefElts.set(i); |
| 1589 | } |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1590 | } else { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1591 | if (UndefElts3[MaskVal - LHSVWidth]) { |
| 1592 | NewUndefElts = true; |
| 1593 | UndefElts.set(i); |
| 1594 | } |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | if (NewUndefElts) { |
| 1599 | // Add additional discovered undefs. |
| 1600 | std::vector<Constant*> Elts; |
| 1601 | for (unsigned i = 0; i < VWidth; ++i) { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1602 | if (UndefElts[i]) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1603 | Elts.push_back(Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1604 | else |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1605 | Elts.push_back(Context->getConstantInt(Type::Int32Ty, |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1606 | Shuffle->getMaskValue(i))); |
| 1607 | } |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1608 | I->setOperand(2, Context->getConstantVector(Elts)); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 1609 | MadeChange = true; |
| 1610 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | } |
| 1613 | case Instruction::BitCast: { |
| 1614 | // Vector->vector casts only. |
| 1615 | const VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); |
| 1616 | if (!VTy) break; |
| 1617 | unsigned InVWidth = VTy->getNumElements(); |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1618 | APInt InputDemandedElts(InVWidth, 0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1619 | unsigned Ratio; |
| 1620 | |
| 1621 | if (VWidth == InVWidth) { |
| 1622 | // If we are converting from <4 x i32> -> <4 x f32>, we demand the same |
| 1623 | // elements as are demanded of us. |
| 1624 | Ratio = 1; |
| 1625 | InputDemandedElts = DemandedElts; |
| 1626 | } else if (VWidth > InVWidth) { |
| 1627 | // Untested so far. |
| 1628 | break; |
| 1629 | |
| 1630 | // If there are more elements in the result than there are in the source, |
| 1631 | // then an input element is live if any of the corresponding output |
| 1632 | // elements are live. |
| 1633 | Ratio = VWidth/InVWidth; |
| 1634 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1635 | if (DemandedElts[OutIdx]) |
| 1636 | InputDemandedElts.set(OutIdx/Ratio); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } else { |
| 1639 | // Untested so far. |
| 1640 | break; |
| 1641 | |
| 1642 | // If there are more elements in the source than there are in the result, |
| 1643 | // then an input element is live if the corresponding output element is |
| 1644 | // live. |
| 1645 | Ratio = InVWidth/VWidth; |
| 1646 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1647 | if (DemandedElts[InIdx/Ratio]) |
| 1648 | InputDemandedElts.set(InIdx); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1652 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), InputDemandedElts, |
| 1653 | UndefElts2, Depth+1); |
| 1654 | if (TmpV) { |
| 1655 | I->setOperand(0, TmpV); |
| 1656 | MadeChange = true; |
| 1657 | } |
| 1658 | |
| 1659 | UndefElts = UndefElts2; |
| 1660 | if (VWidth > InVWidth) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1661 | llvm_unreachable("Unimp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1662 | // If there are more elements in the result than there are in the source, |
| 1663 | // then an output element is undef if the corresponding input element is |
| 1664 | // undef. |
| 1665 | for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1666 | if (UndefElts2[OutIdx/Ratio]) |
| 1667 | UndefElts.set(OutIdx); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1668 | } else if (VWidth < InVWidth) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1669 | llvm_unreachable("Unimp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1670 | // If there are more elements in the source than there are in the result, |
| 1671 | // then a result element is undef if all of the corresponding input |
| 1672 | // elements are undef. |
| 1673 | UndefElts = ~0ULL >> (64-VWidth); // Start out all undef. |
| 1674 | for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 1675 | if (!UndefElts2[InIdx]) // Not undef? |
| 1676 | UndefElts.clear(InIdx/Ratio); // Clear undef bit. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1677 | } |
| 1678 | break; |
| 1679 | } |
| 1680 | case Instruction::And: |
| 1681 | case Instruction::Or: |
| 1682 | case Instruction::Xor: |
| 1683 | case Instruction::Add: |
| 1684 | case Instruction::Sub: |
| 1685 | case Instruction::Mul: |
| 1686 | // div/rem demand all inputs, because they don't want divide by zero. |
| 1687 | TmpV = SimplifyDemandedVectorElts(I->getOperand(0), DemandedElts, |
| 1688 | UndefElts, Depth+1); |
| 1689 | if (TmpV) { I->setOperand(0, TmpV); MadeChange = true; } |
| 1690 | TmpV = SimplifyDemandedVectorElts(I->getOperand(1), DemandedElts, |
| 1691 | UndefElts2, Depth+1); |
| 1692 | if (TmpV) { I->setOperand(1, TmpV); MadeChange = true; } |
| 1693 | |
| 1694 | // Output elements are undefined if both are undefined. Consider things |
| 1695 | // like undef&0. The result is known zero, not undef. |
| 1696 | UndefElts &= UndefElts2; |
| 1697 | break; |
| 1698 | |
| 1699 | case Instruction::Call: { |
| 1700 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); |
| 1701 | if (!II) break; |
| 1702 | switch (II->getIntrinsicID()) { |
| 1703 | default: break; |
| 1704 | |
| 1705 | // Binary vector operations that work column-wise. A dest element is a |
| 1706 | // function of the corresponding input elements from the two inputs. |
| 1707 | case Intrinsic::x86_sse_sub_ss: |
| 1708 | case Intrinsic::x86_sse_mul_ss: |
| 1709 | case Intrinsic::x86_sse_min_ss: |
| 1710 | case Intrinsic::x86_sse_max_ss: |
| 1711 | case Intrinsic::x86_sse2_sub_sd: |
| 1712 | case Intrinsic::x86_sse2_mul_sd: |
| 1713 | case Intrinsic::x86_sse2_min_sd: |
| 1714 | case Intrinsic::x86_sse2_max_sd: |
| 1715 | TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| 1716 | UndefElts, Depth+1); |
| 1717 | if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; } |
| 1718 | TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts, |
| 1719 | UndefElts2, Depth+1); |
| 1720 | if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; } |
| 1721 | |
| 1722 | // If only the low elt is demanded and this is a scalarizable intrinsic, |
| 1723 | // scalarize it now. |
| 1724 | if (DemandedElts == 1) { |
| 1725 | switch (II->getIntrinsicID()) { |
| 1726 | default: break; |
| 1727 | case Intrinsic::x86_sse_sub_ss: |
| 1728 | case Intrinsic::x86_sse_mul_ss: |
| 1729 | case Intrinsic::x86_sse2_sub_sd: |
| 1730 | case Intrinsic::x86_sse2_mul_sd: |
| 1731 | // TODO: Lower MIN/MAX/ABS/etc |
| 1732 | Value *LHS = II->getOperand(1); |
| 1733 | Value *RHS = II->getOperand(2); |
| 1734 | // Extract the element as scalars. |
| Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1735 | LHS = InsertNewInstBefore(new ExtractElementInst(LHS, |
| 1736 | Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II); |
| 1737 | RHS = InsertNewInstBefore(new ExtractElementInst(RHS, |
| 1738 | Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1739 | |
| 1740 | switch (II->getIntrinsicID()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1741 | default: llvm_unreachable("Case stmts out of sync!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1742 | case Intrinsic::x86_sse_sub_ss: |
| 1743 | case Intrinsic::x86_sse2_sub_sd: |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1744 | TmpV = InsertNewInstBefore(BinaryOperator::CreateFSub(LHS, RHS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1745 | II->getName()), *II); |
| 1746 | break; |
| 1747 | case Intrinsic::x86_sse_mul_ss: |
| 1748 | case Intrinsic::x86_sse2_mul_sd: |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1749 | TmpV = InsertNewInstBefore(BinaryOperator::CreateFMul(LHS, RHS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1750 | II->getName()), *II); |
| 1751 | break; |
| 1752 | } |
| 1753 | |
| 1754 | Instruction *New = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1755 | InsertElementInst::Create( |
| Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1756 | Context->getUndef(II->getType()), TmpV, |
| 1757 | Context->getConstantInt(Type::Int32Ty, 0U, false), II->getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1758 | InsertNewInstBefore(New, *II); |
| 1759 | AddSoonDeadInstToWorklist(*II, 0); |
| 1760 | return New; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // Output elements are undefined if both are undefined. Consider things |
| 1765 | // like undef&0. The result is known zero, not undef. |
| 1766 | UndefElts &= UndefElts2; |
| 1767 | break; |
| 1768 | } |
| 1769 | break; |
| 1770 | } |
| 1771 | } |
| 1772 | return MadeChange ? I : 0; |
| 1773 | } |
| 1774 | |
| Dan Gohman | 5d56fd4 | 2008-05-19 22:14:15 +0000 | [diff] [blame] | 1775 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1776 | /// AssociativeOpt - Perform an optimization on an associative operator. This |
| 1777 | /// function is designed to check a chain of associative operators for a |
| 1778 | /// potential to apply a certain optimization. Since the optimization may be |
| 1779 | /// applicable if the expression was reassociated, this checks the chain, then |
| 1780 | /// reassociates the expression as necessary to expose the optimization |
| 1781 | /// opportunity. This makes use of a special Functor, which must define |
| 1782 | /// 'shouldApply' and 'apply' methods. |
| 1783 | /// |
| 1784 | template<typename Functor> |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1785 | static Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 1786 | LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1787 | unsigned Opcode = Root.getOpcode(); |
| 1788 | Value *LHS = Root.getOperand(0); |
| 1789 | |
| 1790 | // Quick check, see if the immediate LHS matches... |
| 1791 | if (F.shouldApply(LHS)) |
| 1792 | return F.apply(Root); |
| 1793 | |
| 1794 | // Otherwise, if the LHS is not of the same opcode as the root, return. |
| 1795 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 1796 | while (LHSI && LHSI->getOpcode() == Opcode && LHSI->hasOneUse()) { |
| 1797 | // Should we apply this transform to the RHS? |
| 1798 | bool ShouldApply = F.shouldApply(LHSI->getOperand(1)); |
| 1799 | |
| 1800 | // If not to the RHS, check to see if we should apply to the LHS... |
| 1801 | if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) { |
| 1802 | cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS |
| 1803 | ShouldApply = true; |
| 1804 | } |
| 1805 | |
| 1806 | // If the functor wants to apply the optimization to the RHS of LHSI, |
| 1807 | // reassociate the expression from ((? op A) op B) to (? op (A op B)) |
| 1808 | if (ShouldApply) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1809 | // Now all of the instructions are in the current basic block, go ahead |
| 1810 | // and perform the reassociation. |
| 1811 | Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0)); |
| 1812 | |
| 1813 | // First move the selected RHS to the LHS of the root... |
| 1814 | Root.setOperand(0, LHSI->getOperand(1)); |
| 1815 | |
| 1816 | // Make what used to be the LHS of the root be the user of the root... |
| 1817 | Value *ExtraOperand = TmpLHSI->getOperand(1); |
| 1818 | if (&Root == TmpLHSI) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1819 | Root.replaceAllUsesWith(Context->getNullValue(TmpLHSI->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1820 | return 0; |
| 1821 | } |
| 1822 | Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI |
| 1823 | TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1824 | BasicBlock::iterator ARI = &Root; ++ARI; |
| Dan Gohman | 0bb9a3d | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1825 | TmpLHSI->moveBefore(ARI); // Move TmpLHSI to after Root |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1826 | ARI = Root; |
| 1827 | |
| 1828 | // Now propagate the ExtraOperand down the chain of instructions until we |
| 1829 | // get to LHSI. |
| 1830 | while (TmpLHSI != LHSI) { |
| 1831 | Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0)); |
| 1832 | // Move the instruction to immediately before the chain we are |
| 1833 | // constructing to avoid breaking dominance properties. |
| Dan Gohman | 0bb9a3d | 2008-06-19 17:47:47 +0000 | [diff] [blame] | 1834 | NextLHSI->moveBefore(ARI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1835 | ARI = NextLHSI; |
| 1836 | |
| 1837 | Value *NextOp = NextLHSI->getOperand(1); |
| 1838 | NextLHSI->setOperand(1, ExtraOperand); |
| 1839 | TmpLHSI = NextLHSI; |
| 1840 | ExtraOperand = NextOp; |
| 1841 | } |
| 1842 | |
| 1843 | // Now that the instructions are reassociated, have the functor perform |
| 1844 | // the transformation... |
| 1845 | return F.apply(Root); |
| 1846 | } |
| 1847 | |
| 1848 | LHSI = dyn_cast<Instruction>(LHSI->getOperand(0)); |
| 1849 | } |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1853 | namespace { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1854 | |
| Nick Lewycky | 27f6c13 | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1855 | // AddRHS - Implements: X + X --> X << 1 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1856 | struct AddRHS { |
| 1857 | Value *RHS; |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 1858 | LLVMContext *Context; |
| 1859 | AddRHS(Value *rhs, LLVMContext *C) : RHS(rhs), Context(C) {} |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1860 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 1861 | Instruction *apply(BinaryOperator &Add) const { |
| Nick Lewycky | 27f6c13 | 2008-05-23 04:34:58 +0000 | [diff] [blame] | 1862 | return BinaryOperator::CreateShl(Add.getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1863 | Context->getConstantInt(Add.getType(), 1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1864 | } |
| 1865 | }; |
| 1866 | |
| 1867 | // AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2) |
| 1868 | // iff C1&C2 == 0 |
| 1869 | struct AddMaskingAnd { |
| 1870 | Constant *C2; |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 1871 | LLVMContext *Context; |
| 1872 | AddMaskingAnd(Constant *c, LLVMContext *C) : C2(c), Context(C) {} |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1873 | bool shouldApply(Value *LHS) const { |
| 1874 | ConstantInt *C1; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 1875 | return match(LHS, m_And(m_Value(), m_ConstantInt(C1)), *Context) && |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1876 | Context->getConstantExprAnd(C1, C2)->isNullValue(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1877 | } |
| 1878 | Instruction *apply(BinaryOperator &Add) const { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1879 | return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1880 | } |
| 1881 | }; |
| 1882 | |
| Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1885 | static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO, |
| 1886 | InstCombiner *IC) { |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 1887 | LLVMContext *Context = IC->getContext(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1888 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1889 | if (CastInst *CI = dyn_cast<CastInst>(&I)) { |
| Eli Friedman | 722b479 | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 1890 | return IC->InsertCastBefore(CI->getOpcode(), SO, I.getType(), I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | // Figure out if the constant is the left or the right argument. |
| 1894 | bool ConstIsRHS = isa<Constant>(I.getOperand(1)); |
| 1895 | Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS)); |
| 1896 | |
| 1897 | if (Constant *SOC = dyn_cast<Constant>(SO)) { |
| 1898 | if (ConstIsRHS) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1899 | return Context->getConstantExpr(I.getOpcode(), SOC, ConstOperand); |
| 1900 | return Context->getConstantExpr(I.getOpcode(), ConstOperand, SOC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | Value *Op0 = SO, *Op1 = ConstOperand; |
| 1904 | if (!ConstIsRHS) |
| 1905 | std::swap(Op0, Op1); |
| 1906 | Instruction *New; |
| 1907 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1908 | New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1909 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1910 | New = CmpInst::Create(*Context, CI->getOpcode(), CI->getPredicate(), |
| 1911 | Op0, Op1, SO->getName()+".cmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1912 | else { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1913 | llvm_unreachable("Unknown binary instruction type!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1914 | } |
| 1915 | return IC->InsertNewInstBefore(New, I); |
| 1916 | } |
| 1917 | |
| 1918 | // FoldOpIntoSelect - Given an instruction with a select as one operand and a |
| 1919 | // constant as the other operand, try to fold the binary operator into the |
| 1920 | // select arguments. This also works for Cast instructions, which obviously do |
| 1921 | // not have a second operand. |
| 1922 | static Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI, |
| 1923 | InstCombiner *IC) { |
| 1924 | // Don't modify shared select instructions |
| 1925 | if (!SI->hasOneUse()) return 0; |
| 1926 | Value *TV = SI->getOperand(1); |
| 1927 | Value *FV = SI->getOperand(2); |
| 1928 | |
| 1929 | if (isa<Constant>(TV) || isa<Constant>(FV)) { |
| 1930 | // Bool selects with constant operands can be folded to logical ops. |
| 1931 | if (SI->getType() == Type::Int1Ty) return 0; |
| 1932 | |
| 1933 | Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, IC); |
| 1934 | Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, IC); |
| 1935 | |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1936 | return SelectInst::Create(SI->getCondition(), SelectTrueVal, |
| 1937 | SelectFalseVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1938 | } |
| 1939 | return 0; |
| 1940 | } |
| 1941 | |
| 1942 | |
| 1943 | /// FoldOpIntoPhi - Given a binary operator or cast instruction which has a PHI |
| 1944 | /// node as operand #0, see if we can fold the instruction into the PHI (which |
| 1945 | /// is only possible if all operands to the PHI are constants). |
| 1946 | Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I) { |
| 1947 | PHINode *PN = cast<PHINode>(I.getOperand(0)); |
| 1948 | unsigned NumPHIValues = PN->getNumIncomingValues(); |
| 1949 | if (!PN->hasOneUse() || NumPHIValues == 0) return 0; |
| 1950 | |
| 1951 | // Check to see if all of the operands of the PHI are constants. If there is |
| 1952 | // one non-constant value, remember the BB it is. If there is more than one |
| 1953 | // or if *it* is a PHI, bail out. |
| 1954 | BasicBlock *NonConstBB = 0; |
| 1955 | for (unsigned i = 0; i != NumPHIValues; ++i) |
| 1956 | if (!isa<Constant>(PN->getIncomingValue(i))) { |
| 1957 | if (NonConstBB) return 0; // More than one non-const value. |
| 1958 | if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi. |
| 1959 | NonConstBB = PN->getIncomingBlock(i); |
| 1960 | |
| 1961 | // If the incoming non-constant value is in I's block, we have an infinite |
| 1962 | // loop. |
| 1963 | if (NonConstBB == I.getParent()) |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | // If there is exactly one non-constant value, we can insert a copy of the |
| 1968 | // operation in that block. However, if this is a critical edge, we would be |
| 1969 | // inserting the computation one some other paths (e.g. inside a loop). Only |
| 1970 | // do this if the pred block is unconditionally branching into the phi block. |
| 1971 | if (NonConstBB) { |
| 1972 | BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator()); |
| 1973 | if (!BI || !BI->isUnconditional()) return 0; |
| 1974 | } |
| 1975 | |
| 1976 | // Okay, we can do the transformation: create the new PHI node. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1977 | PHINode *NewPN = PHINode::Create(I.getType(), ""); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1978 | NewPN->reserveOperandSpace(PN->getNumOperands()/2); |
| 1979 | InsertNewInstBefore(NewPN, *PN); |
| 1980 | NewPN->takeName(PN); |
| 1981 | |
| 1982 | // Next, add all of the operands to the PHI. |
| 1983 | if (I.getNumOperands() == 2) { |
| 1984 | Constant *C = cast<Constant>(I.getOperand(1)); |
| 1985 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
| Chris Lattner | b933ea6 | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 1986 | Value *InV = 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1987 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
| 1988 | if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1989 | InV = Context->getConstantExprCompare(CI->getPredicate(), InC, C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1990 | else |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 1991 | InV = Context->getConstantExpr(I.getOpcode(), InC, C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1992 | } else { |
| 1993 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| 1994 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1995 | InV = BinaryOperator::Create(BO->getOpcode(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1996 | PN->getIncomingValue(i), C, "phitmp", |
| 1997 | NonConstBB->getTerminator()); |
| 1998 | else if (CmpInst *CI = dyn_cast<CmpInst>(&I)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 1999 | InV = CmpInst::Create(*Context, CI->getOpcode(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2000 | CI->getPredicate(), |
| 2001 | PN->getIncomingValue(i), C, "phitmp", |
| 2002 | NonConstBB->getTerminator()); |
| 2003 | else |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2004 | llvm_unreachable("Unknown binop!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2005 | |
| 2006 | AddToWorkList(cast<Instruction>(InV)); |
| 2007 | } |
| 2008 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
| 2009 | } |
| 2010 | } else { |
| 2011 | CastInst *CI = cast<CastInst>(&I); |
| 2012 | const Type *RetTy = CI->getType(); |
| 2013 | for (unsigned i = 0; i != NumPHIValues; ++i) { |
| 2014 | Value *InV; |
| 2015 | if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2016 | InV = Context->getConstantExprCast(CI->getOpcode(), InC, RetTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2017 | } else { |
| 2018 | assert(PN->getIncomingBlock(i) == NonConstBB); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2019 | InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2020 | I.getType(), "phitmp", |
| 2021 | NonConstBB->getTerminator()); |
| 2022 | AddToWorkList(cast<Instruction>(InV)); |
| 2023 | } |
| 2024 | NewPN->addIncoming(InV, PN->getIncomingBlock(i)); |
| 2025 | } |
| 2026 | } |
| 2027 | return ReplaceInstUsesWith(I, NewPN); |
| 2028 | } |
| 2029 | |
| Chris Lattner | 5547616 | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2030 | |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2031 | /// WillNotOverflowSignedAdd - Return true if we can prove that: |
| 2032 | /// (sext (add LHS, RHS)) === (add (sext LHS), (sext RHS)) |
| 2033 | /// This basically requires proving that the add in the original type would not |
| 2034 | /// overflow to change the sign bit or have a carry out. |
| 2035 | bool InstCombiner::WillNotOverflowSignedAdd(Value *LHS, Value *RHS) { |
| 2036 | // There are different heuristics we can use for this. Here are some simple |
| 2037 | // ones. |
| 2038 | |
| 2039 | // Add has the property that adding any two 2's complement numbers can only |
| 2040 | // have one carry bit which can change a sign. As such, if LHS and RHS each |
| 2041 | // have at least two sign bits, we know that the addition of the two values will |
| 2042 | // sign extend fine. |
| 2043 | if (ComputeNumSignBits(LHS) > 1 && ComputeNumSignBits(RHS) > 1) |
| 2044 | return true; |
| 2045 | |
| 2046 | |
| 2047 | // If one of the operands only has one non-zero bit, and if the other operand |
| 2048 | // has a known-zero bit in a more significant place than it (not including the |
| 2049 | // sign bit) the ripple may go up to and fill the zero, but won't change the |
| 2050 | // sign. For example, (X & ~4) + 1. |
| 2051 | |
| 2052 | // TODO: Implement. |
| 2053 | |
| 2054 | return false; |
| 2055 | } |
| 2056 | |
| Chris Lattner | 5547616 | 2008-01-29 06:52:45 +0000 | [diff] [blame] | 2057 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2058 | Instruction *InstCombiner::visitAdd(BinaryOperator &I) { |
| 2059 | bool Changed = SimplifyCommutative(I); |
| 2060 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 2061 | |
| 2062 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2063 | // X + undef -> undef |
| 2064 | if (isa<UndefValue>(RHS)) |
| 2065 | return ReplaceInstUsesWith(I, RHS); |
| 2066 | |
| 2067 | // X + 0 --> X |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2068 | if (RHSC->isNullValue()) |
| 2069 | return ReplaceInstUsesWith(I, LHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2070 | |
| 2071 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHSC)) { |
| 2072 | // X + (signbit) --> X ^ signbit |
| 2073 | const APInt& Val = CI->getValue(); |
| 2074 | uint32_t BitWidth = Val.getBitWidth(); |
| 2075 | if (Val == APInt::getSignBit(BitWidth)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2076 | return BinaryOperator::CreateXor(LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2077 | |
| 2078 | // See if SimplifyDemandedBits can simplify this. This handles stuff like |
| 2079 | // (X & 254)+1 -> (X&254)|1 |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2080 | if (SimplifyDemandedInstructionBits(I)) |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 2081 | return &I; |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 2082 | |
| Eli Friedman | a21526d | 2009-07-13 22:27:52 +0000 | [diff] [blame] | 2083 | // zext(bool) + C -> bool ? C + 1 : C |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 2084 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(LHS)) |
| Eli Friedman | a21526d | 2009-07-13 22:27:52 +0000 | [diff] [blame] | 2085 | if (ZI->getSrcTy() == Type::Int1Ty) |
| 2086 | return SelectInst::Create(ZI->getOperand(0), AddOne(CI, Context), CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | if (isa<PHINode>(LHS)) |
| 2090 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2091 | return NV; |
| 2092 | |
| 2093 | ConstantInt *XorRHS = 0; |
| 2094 | Value *XorLHS = 0; |
| 2095 | if (isa<ConstantInt>(RHSC) && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2096 | match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)), *Context)) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2097 | uint32_t TySizeBits = I.getType()->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2098 | const APInt& RHSVal = cast<ConstantInt>(RHSC)->getValue(); |
| 2099 | |
| 2100 | uint32_t Size = TySizeBits / 2; |
| 2101 | APInt C0080Val(APInt(TySizeBits, 1ULL).shl(Size - 1)); |
| 2102 | APInt CFF80Val(-C0080Val); |
| 2103 | do { |
| 2104 | if (TySizeBits > Size) { |
| 2105 | // If we have ADD(XOR(AND(X, 0xFF), 0x80), 0xF..F80), it's a sext. |
| 2106 | // If we have ADD(XOR(AND(X, 0xFF), 0xF..F80), 0x80), it's a sext. |
| 2107 | if ((RHSVal == CFF80Val && XorRHS->getValue() == C0080Val) || |
| 2108 | (RHSVal == C0080Val && XorRHS->getValue() == CFF80Val)) { |
| 2109 | // This is a sign extend if the top bits are known zero. |
| 2110 | if (!MaskedValueIsZero(XorLHS, |
| 2111 | APInt::getHighBitsSet(TySizeBits, TySizeBits - Size))) |
| 2112 | Size = 0; // Not a sign ext, but can't be any others either. |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
| 2116 | Size >>= 1; |
| 2117 | C0080Val = APIntOps::lshr(C0080Val, Size); |
| 2118 | CFF80Val = APIntOps::ashr(CFF80Val, Size); |
| 2119 | } while (Size >= 1); |
| 2120 | |
| 2121 | // FIXME: This shouldn't be necessary. When the backends can handle types |
| Chris Lattner | deef1a7 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 2122 | // with funny bit widths then this switch statement should be removed. It |
| 2123 | // is just here to get the size of the "middle" type back up to something |
| 2124 | // that the back ends can handle. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2125 | const Type *MiddleType = 0; |
| 2126 | switch (Size) { |
| 2127 | default: break; |
| 2128 | case 32: MiddleType = Type::Int32Ty; break; |
| 2129 | case 16: MiddleType = Type::Int16Ty; break; |
| 2130 | case 8: MiddleType = Type::Int8Ty; break; |
| 2131 | } |
| 2132 | if (MiddleType) { |
| 2133 | Instruction *NewTrunc = new TruncInst(XorLHS, MiddleType, "sext"); |
| 2134 | InsertNewInstBefore(NewTrunc, I); |
| 2135 | return new SExtInst(NewTrunc, I.getType(), I.getName()); |
| 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | |
| Nick Lewycky | d4b6367 | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2140 | if (I.getType() == Type::Int1Ty) |
| 2141 | return BinaryOperator::CreateXor(LHS, RHS); |
| 2142 | |
| Nick Lewycky | 4d474cd | 2008-05-23 04:39:38 +0000 | [diff] [blame] | 2143 | // X + X --> X << 1 |
| Nick Lewycky | d4b6367 | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2144 | if (I.getType()->isInteger()) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2145 | if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS, Context), Context)) |
| 2146 | return Result; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2147 | |
| 2148 | if (Instruction *RHSI = dyn_cast<Instruction>(RHS)) { |
| 2149 | if (RHSI->getOpcode() == Instruction::Sub) |
| 2150 | if (LHS == RHSI->getOperand(1)) // A + (B - A) --> B |
| 2151 | return ReplaceInstUsesWith(I, RHSI->getOperand(0)); |
| 2152 | } |
| 2153 | if (Instruction *LHSI = dyn_cast<Instruction>(LHS)) { |
| 2154 | if (LHSI->getOpcode() == Instruction::Sub) |
| 2155 | if (RHS == LHSI->getOperand(1)) // (B - A) + A --> B |
| 2156 | return ReplaceInstUsesWith(I, LHSI->getOperand(0)); |
| 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | // -A + B --> B - A |
| Chris Lattner | 53c9fbf | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2161 | // -A + -B --> -(A + B) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2162 | if (Value *LHSV = dyn_castNegVal(LHS, Context)) { |
| Chris Lattner | 322a919 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2163 | if (LHS->getType()->isIntOrIntVector()) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2164 | if (Value *RHSV = dyn_castNegVal(RHS, Context)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2165 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum"); |
| Chris Lattner | 322a919 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2166 | InsertNewInstBefore(NewAdd, I); |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2167 | return BinaryOperator::CreateNeg(*Context, NewAdd); |
| Chris Lattner | 322a919 | 2008-02-18 17:50:16 +0000 | [diff] [blame] | 2168 | } |
| Chris Lattner | 53c9fbf | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2171 | return BinaryOperator::CreateSub(RHS, LHSV); |
| Chris Lattner | 53c9fbf | 2008-02-17 21:03:36 +0000 | [diff] [blame] | 2172 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2173 | |
| 2174 | // A + -B --> A - B |
| 2175 | if (!isa<Constant>(RHS)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2176 | if (Value *V = dyn_castNegVal(RHS, Context)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2177 | return BinaryOperator::CreateSub(LHS, V); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2178 | |
| 2179 | |
| 2180 | ConstantInt *C2; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2181 | if (Value *X = dyn_castFoldableMul(LHS, C2, Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2182 | if (X == RHS) // X*C + X --> X * (C+1) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2183 | return BinaryOperator::CreateMul(RHS, AddOne(C2, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2184 | |
| 2185 | // X*C1 + X*C2 --> X * (C1+C2) |
| 2186 | ConstantInt *C1; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2187 | if (X == dyn_castFoldableMul(RHS, C1, Context)) |
| 2188 | return BinaryOperator::CreateMul(X, Context->getConstantExprAdd(C1, C2)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | // X + X*C --> X * (C+1) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2192 | if (dyn_castFoldableMul(RHS, C2, Context) == LHS) |
| 2193 | return BinaryOperator::CreateMul(LHS, AddOne(C2, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2194 | |
| 2195 | // X + ~X --> -1 since ~X = -X-1 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2196 | if (dyn_castNotVal(LHS, Context) == RHS || |
| 2197 | dyn_castNotVal(RHS, Context) == LHS) |
| 2198 | return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2199 | |
| 2200 | |
| 2201 | // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2202 | if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)), *Context)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2203 | if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2, Context), Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2204 | return R; |
| Chris Lattner | c1575ce | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2205 | |
| 2206 | // A+B --> A|B iff A and B have no bits set in common. |
| 2207 | if (const IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
| 2208 | APInt Mask = APInt::getAllOnesValue(IT->getBitWidth()); |
| 2209 | APInt LHSKnownOne(IT->getBitWidth(), 0); |
| 2210 | APInt LHSKnownZero(IT->getBitWidth(), 0); |
| 2211 | ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); |
| 2212 | if (LHSKnownZero != 0) { |
| 2213 | APInt RHSKnownOne(IT->getBitWidth(), 0); |
| 2214 | APInt RHSKnownZero(IT->getBitWidth(), 0); |
| 2215 | ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); |
| 2216 | |
| 2217 | // No bits in common -> bitwise or. |
| Chris Lattner | 130443c | 2008-05-19 20:03:53 +0000 | [diff] [blame] | 2218 | if ((LHSKnownZero|RHSKnownZero).isAllOnesValue()) |
| Chris Lattner | c1575ce | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2219 | return BinaryOperator::CreateOr(LHS, RHS); |
| Chris Lattner | c1575ce | 2008-05-19 20:01:56 +0000 | [diff] [blame] | 2220 | } |
| 2221 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2222 | |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2223 | // W*X + Y*Z --> W * (X+Z) iff W == Y |
| Nick Lewycky | 5d03b51 | 2008-02-03 08:19:11 +0000 | [diff] [blame] | 2224 | if (I.getType()->isIntOrIntVector()) { |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2225 | Value *W, *X, *Y, *Z; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2226 | if (match(LHS, m_Mul(m_Value(W), m_Value(X)), *Context) && |
| 2227 | match(RHS, m_Mul(m_Value(Y), m_Value(Z)), *Context)) { |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2228 | if (W != Y) { |
| 2229 | if (W == Z) { |
| Bill Wendling | 44a36ea | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2230 | std::swap(Y, Z); |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2231 | } else if (Y == X) { |
| Bill Wendling | 44a36ea | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 2232 | std::swap(W, X); |
| 2233 | } else if (X == Z) { |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2234 | std::swap(Y, Z); |
| 2235 | std::swap(W, X); |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | if (W == Y) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2240 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z, |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2241 | LHS->getName()), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2242 | return BinaryOperator::CreateMul(W, NewAdd); |
| Nick Lewycky | 83598a7 | 2008-02-03 07:42:09 +0000 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2247 | if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) { |
| 2248 | Value *X = 0; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2249 | if (match(LHS, m_Not(m_Value(X)), *Context)) // ~X + C --> (C-1) - X |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2250 | return BinaryOperator::CreateSub(SubOne(CRHS, Context), X); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2251 | |
| 2252 | // (X & FF00) + xx00 -> (X+xx00) & FF00 |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2253 | if (LHS->hasOneUse() && |
| 2254 | match(LHS, m_And(m_Value(X), m_ConstantInt(C2)), *Context)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2255 | Constant *Anded = Context->getConstantExprAnd(CRHS, C2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2256 | if (Anded == CRHS) { |
| 2257 | // See if all bits from the first bit set in the Add RHS up are included |
| 2258 | // in the mask. First, get the rightmost bit. |
| 2259 | const APInt& AddRHSV = CRHS->getValue(); |
| 2260 | |
| 2261 | // Form a mask of all bits from the lowest bit added through the top. |
| 2262 | APInt AddRHSHighBits(~((AddRHSV & -AddRHSV)-1)); |
| 2263 | |
| 2264 | // See if the and mask includes all of these bits. |
| 2265 | APInt AddRHSHighBitsAnd(AddRHSHighBits & C2->getValue()); |
| 2266 | |
| 2267 | if (AddRHSHighBits == AddRHSHighBitsAnd) { |
| 2268 | // Okay, the xform is safe. Insert the new add pronto. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2269 | Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2270 | LHS->getName()), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2271 | return BinaryOperator::CreateAnd(NewAdd, C2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | // Try to fold constant add into select arguments. |
| 2277 | if (SelectInst *SI = dyn_cast<SelectInst>(LHS)) |
| 2278 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2279 | return R; |
| 2280 | } |
| 2281 | |
| Chris Lattner | bf0c5f3 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2282 | // add (select X 0 (sub n A)) A --> select X A n |
| Christopher Lamb | 244ec28 | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2283 | { |
| 2284 | SelectInst *SI = dyn_cast<SelectInst>(LHS); |
| Chris Lattner | 641ea46 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2285 | Value *A = RHS; |
| Christopher Lamb | 244ec28 | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2286 | if (!SI) { |
| 2287 | SI = dyn_cast<SelectInst>(RHS); |
| Chris Lattner | 641ea46 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2288 | A = LHS; |
| Christopher Lamb | 244ec28 | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2289 | } |
| Chris Lattner | bf0c5f3 | 2007-12-20 01:56:58 +0000 | [diff] [blame] | 2290 | if (SI && SI->hasOneUse()) { |
| Christopher Lamb | 244ec28 | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2291 | Value *TV = SI->getTrueValue(); |
| 2292 | Value *FV = SI->getFalseValue(); |
| Chris Lattner | 641ea46 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2293 | Value *N; |
| Christopher Lamb | 244ec28 | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2294 | |
| 2295 | // Can we fold the add into the argument of the select? |
| 2296 | // We check both true and false select arguments for a matching subtract. |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2297 | if (match(FV, m_Zero(), *Context) && |
| 2298 | match(TV, m_Sub(m_Value(N), m_Specific(A)), *Context)) |
| Chris Lattner | 641ea46 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2299 | // Fold the add into the true select value. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2300 | return SelectInst::Create(SI->getCondition(), N, A); |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2301 | if (match(TV, m_Zero(), *Context) && |
| 2302 | match(FV, m_Sub(m_Value(N), m_Specific(A)), *Context)) |
| Chris Lattner | 641ea46 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 2303 | // Fold the add into the false select value. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 2304 | return SelectInst::Create(SI->getCondition(), A, N); |
| Christopher Lamb | 244ec28 | 2007-12-18 09:34:41 +0000 | [diff] [blame] | 2305 | } |
| 2306 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2307 | |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2308 | // Check for (add (sext x), y), see if we can merge this into an |
| 2309 | // integer add followed by a sext. |
| 2310 | if (SExtInst *LHSConv = dyn_cast<SExtInst>(LHS)) { |
| 2311 | // (add (sext x), cst) --> (sext (add x, cst')) |
| 2312 | if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) { |
| 2313 | Constant *CI = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2314 | Context->getConstantExprTrunc(RHSC, LHSConv->getOperand(0)->getType()); |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2315 | if (LHSConv->hasOneUse() && |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2316 | Context->getConstantExprSExt(CI, I.getType()) == RHSC && |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2317 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 2318 | // Insert the new, smaller add. |
| 2319 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2320 | CI, "addconv"); |
| 2321 | InsertNewInstBefore(NewAdd, I); |
| 2322 | return new SExtInst(NewAdd, I.getType()); |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | // (add (sext x), (sext y)) --> (sext (add int x, y)) |
| 2327 | if (SExtInst *RHSConv = dyn_cast<SExtInst>(RHS)) { |
| 2328 | // Only do this if x/y have the same type, if at last one of them has a |
| 2329 | // single use (so we don't increase the number of sexts), and if the |
| 2330 | // integer add will not overflow. |
| 2331 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 2332 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 2333 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 2334 | RHSConv->getOperand(0))) { |
| 2335 | // Insert the new integer add. |
| 2336 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2337 | RHSConv->getOperand(0), |
| 2338 | "addconv"); |
| 2339 | InsertNewInstBefore(NewAdd, I); |
| 2340 | return new SExtInst(NewAdd, I.getType()); |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2344 | |
| 2345 | return Changed ? &I : 0; |
| 2346 | } |
| 2347 | |
| 2348 | Instruction *InstCombiner::visitFAdd(BinaryOperator &I) { |
| 2349 | bool Changed = SimplifyCommutative(I); |
| 2350 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 2351 | |
| 2352 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 2353 | // X + 0 --> X |
| 2354 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2355 | if (CFP->isExactlyValue(Context->getConstantFPNegativeZero |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2356 | (I.getType())->getValueAPF())) |
| 2357 | return ReplaceInstUsesWith(I, LHS); |
| 2358 | } |
| 2359 | |
| 2360 | if (isa<PHINode>(LHS)) |
| 2361 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2362 | return NV; |
| 2363 | } |
| 2364 | |
| 2365 | // -A + B --> B - A |
| 2366 | // -A + -B --> -(A + B) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2367 | if (Value *LHSV = dyn_castFNegVal(LHS, Context)) |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2368 | return BinaryOperator::CreateFSub(RHS, LHSV); |
| 2369 | |
| 2370 | // A + -B --> A - B |
| 2371 | if (!isa<Constant>(RHS)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2372 | if (Value *V = dyn_castFNegVal(RHS, Context)) |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2373 | return BinaryOperator::CreateFSub(LHS, V); |
| 2374 | |
| 2375 | // Check for X+0.0. Simplify it to X if we know X is not -0.0. |
| 2376 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) |
| 2377 | if (CFP->getValueAPF().isPosZero() && CannotBeNegativeZero(LHS)) |
| 2378 | return ReplaceInstUsesWith(I, LHS); |
| 2379 | |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2380 | // Check for (add double (sitofp x), y), see if we can merge this into an |
| 2381 | // integer add followed by a promotion. |
| 2382 | if (SIToFPInst *LHSConv = dyn_cast<SIToFPInst>(LHS)) { |
| 2383 | // (add double (sitofp x), fpcst) --> (sitofp (add int x, intcst)) |
| 2384 | // ... if the constant fits in the integer value. This is useful for things |
| 2385 | // like (double)(x & 1234) + 4.0 -> (double)((X & 1234)+4) which no longer |
| 2386 | // requires a constant pool load, and generally allows the add to be better |
| 2387 | // instcombined. |
| 2388 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHS)) { |
| 2389 | Constant *CI = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2390 | Context->getConstantExprFPToSI(CFP, LHSConv->getOperand(0)->getType()); |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2391 | if (LHSConv->hasOneUse() && |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2392 | Context->getConstantExprSIToFP(CI, I.getType()) == CFP && |
| Chris Lattner | 3554f97 | 2008-05-20 05:46:13 +0000 | [diff] [blame] | 2393 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), CI)) { |
| 2394 | // Insert the new integer add. |
| 2395 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2396 | CI, "addconv"); |
| 2397 | InsertNewInstBefore(NewAdd, I); |
| 2398 | return new SIToFPInst(NewAdd, I.getType()); |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | // (add double (sitofp x), (sitofp y)) --> (sitofp (add int x, y)) |
| 2403 | if (SIToFPInst *RHSConv = dyn_cast<SIToFPInst>(RHS)) { |
| 2404 | // Only do this if x/y have the same type, if at last one of them has a |
| 2405 | // single use (so we don't increase the number of int->fp conversions), |
| 2406 | // and if the integer add will not overflow. |
| 2407 | if (LHSConv->getOperand(0)->getType()==RHSConv->getOperand(0)->getType()&& |
| 2408 | (LHSConv->hasOneUse() || RHSConv->hasOneUse()) && |
| 2409 | WillNotOverflowSignedAdd(LHSConv->getOperand(0), |
| 2410 | RHSConv->getOperand(0))) { |
| 2411 | // Insert the new integer add. |
| 2412 | Instruction *NewAdd = BinaryOperator::CreateAdd(LHSConv->getOperand(0), |
| 2413 | RHSConv->getOperand(0), |
| 2414 | "addconv"); |
| 2415 | InsertNewInstBefore(NewAdd, I); |
| 2416 | return new SIToFPInst(NewAdd, I.getType()); |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2421 | return Changed ? &I : 0; |
| 2422 | } |
| 2423 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2424 | Instruction *InstCombiner::visitSub(BinaryOperator &I) { |
| 2425 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2426 | |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2427 | if (Op0 == Op1) // sub X, X -> 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2428 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2429 | |
| 2430 | // If this is a 'B = x-(-A)', change to B = x+A... |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2431 | if (Value *V = dyn_castNegVal(Op1, Context)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2432 | return BinaryOperator::CreateAdd(Op0, V); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2433 | |
| 2434 | if (isa<UndefValue>(Op0)) |
| 2435 | return ReplaceInstUsesWith(I, Op0); // undef - X -> undef |
| 2436 | if (isa<UndefValue>(Op1)) |
| 2437 | return ReplaceInstUsesWith(I, Op1); // X - undef -> undef |
| 2438 | |
| 2439 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op0)) { |
| 2440 | // Replace (-1 - A) with (~A)... |
| 2441 | if (C->isAllOnesValue()) |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2442 | return BinaryOperator::CreateNot(*Context, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2443 | |
| 2444 | // C - ~X == X + (1+C) |
| 2445 | Value *X = 0; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 2446 | if (match(Op1, m_Not(m_Value(X)), *Context)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2447 | return BinaryOperator::CreateAdd(X, AddOne(C, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2448 | |
| 2449 | // -(X >>u 31) -> (X >>s 31) |
| 2450 | // -(X >>s 31) -> (X >>u 31) |
| 2451 | if (C->isZero()) { |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2452 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op1)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2453 | if (SI->getOpcode() == Instruction::LShr) { |
| 2454 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2455 | // Check to see if we are shifting out everything but the sign bit. |
| 2456 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
| 2457 | SI->getType()->getPrimitiveSizeInBits()-1) { |
| 2458 | // Ok, the transformation is safe. Insert AShr. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2459 | return BinaryOperator::Create(Instruction::AShr, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2460 | SI->getOperand(0), CU, SI->getName()); |
| 2461 | } |
| 2462 | } |
| 2463 | } |
| 2464 | else if (SI->getOpcode() == Instruction::AShr) { |
| 2465 | if (ConstantInt *CU = dyn_cast<ConstantInt>(SI->getOperand(1))) { |
| 2466 | // Check to see if we are shifting out everything but the sign bit. |
| 2467 | if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) == |
| 2468 | SI->getType()->getPrimitiveSizeInBits()-1) { |
| 2469 | // Ok, the transformation is safe. Insert LShr. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2470 | return BinaryOperator::CreateLShr( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2471 | SI->getOperand(0), CU, SI->getName()); |
| 2472 | } |
| 2473 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2474 | } |
| 2475 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | // Try to fold constant sub into select arguments. |
| 2479 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 2480 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2481 | return R; |
| Eli Friedman | a21526d | 2009-07-13 22:27:52 +0000 | [diff] [blame] | 2482 | |
| 2483 | // C - zext(bool) -> bool ? C - 1 : C |
| 2484 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(Op1)) |
| 2485 | if (ZI->getSrcTy() == Type::Int1Ty) |
| 2486 | return SelectInst::Create(ZI->getOperand(0), SubOne(C, Context), C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| Nick Lewycky | d4b6367 | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2489 | if (I.getType() == Type::Int1Ty) |
| 2490 | return BinaryOperator::CreateXor(Op0, Op1); |
| 2491 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2492 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2493 | if (Op1I->getOpcode() == Instruction::Add) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2494 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2495 | return BinaryOperator::CreateNeg(*Context, Op1I->getOperand(1), |
| 2496 | I.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2497 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2498 | return BinaryOperator::CreateNeg(*Context, Op1I->getOperand(0), |
| 2499 | I.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2500 | else if (ConstantInt *CI1 = dyn_cast<ConstantInt>(I.getOperand(0))) { |
| 2501 | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(Op1I->getOperand(1))) |
| 2502 | // C1-(X+C2) --> (C1-C2)-X |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2503 | return BinaryOperator::CreateSub( |
| 2504 | Context->getConstantExprSub(CI1, CI2), Op1I->getOperand(0)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | if (Op1I->hasOneUse()) { |
| 2509 | // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression |
| 2510 | // is not used by anyone else... |
| 2511 | // |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2512 | if (Op1I->getOpcode() == Instruction::Sub) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2513 | // Swap the two operands of the subexpr... |
| 2514 | Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1); |
| 2515 | Op1I->setOperand(0, IIOp1); |
| 2516 | Op1I->setOperand(1, IIOp0); |
| 2517 | |
| 2518 | // Create the new top level add instruction... |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2519 | return BinaryOperator::CreateAdd(Op0, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)... |
| 2523 | // |
| 2524 | if (Op1I->getOpcode() == Instruction::And && |
| 2525 | (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) { |
| 2526 | Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0); |
| 2527 | |
| 2528 | Value *NewNot = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 2529 | InsertNewInstBefore(BinaryOperator::CreateNot(*Context, |
| 2530 | OtherOp, "B.not"), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2531 | return BinaryOperator::CreateAnd(Op0, NewNot); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | // 0 - (X sdiv C) -> (X sdiv -C) |
| 2535 | if (Op1I->getOpcode() == Instruction::SDiv) |
| 2536 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 2537 | if (CSI->isZero()) |
| 2538 | if (Constant *DivRHS = dyn_cast<Constant>(Op1I->getOperand(1))) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2539 | return BinaryOperator::CreateSDiv(Op1I->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2540 | Context->getConstantExprNeg(DivRHS)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2541 | |
| 2542 | // X - X*C --> X * (1-C) |
| 2543 | ConstantInt *C2 = 0; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2544 | if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) { |
| 2545 | Constant *CP1 = |
| 2546 | Context->getConstantExprSub(Context->getConstantInt(I.getType(), 1), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2547 | C2); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2548 | return BinaryOperator::CreateMul(Op0, CP1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2549 | } |
| 2550 | } |
| 2551 | } |
| 2552 | |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2553 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 2554 | if (Op0I->getOpcode() == Instruction::Add) { |
| 2555 | if (Op0I->getOperand(0) == Op1) // (Y+X)-Y == X |
| 2556 | return ReplaceInstUsesWith(I, Op0I->getOperand(1)); |
| 2557 | else if (Op0I->getOperand(1) == Op1) // (X+Y)-Y == X |
| 2558 | return ReplaceInstUsesWith(I, Op0I->getOperand(0)); |
| 2559 | } else if (Op0I->getOpcode() == Instruction::Sub) { |
| 2560 | if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2561 | return BinaryOperator::CreateNeg(*Context, Op0I->getOperand(1), |
| 2562 | I.getName()); |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 2563 | } |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2564 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2565 | |
| 2566 | ConstantInt *C1; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2567 | if (Value *X = dyn_castFoldableMul(Op0, C1, Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2568 | if (X == Op1) // X*C - X --> X * (C-1) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2569 | return BinaryOperator::CreateMul(Op1, SubOne(C1, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2570 | |
| 2571 | ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2572 | if (X == dyn_castFoldableMul(Op1, C2, Context)) |
| 2573 | return BinaryOperator::CreateMul(X, Context->getConstantExprSub(C1, C2)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2574 | } |
| 2575 | return 0; |
| 2576 | } |
| 2577 | |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2578 | Instruction *InstCombiner::visitFSub(BinaryOperator &I) { |
| 2579 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2580 | |
| 2581 | // If this is a 'B = x-(-A)', change to B = x+A... |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2582 | if (Value *V = dyn_castFNegVal(Op1, Context)) |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2583 | return BinaryOperator::CreateFAdd(Op0, V); |
| 2584 | |
| 2585 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| 2586 | if (Op1I->getOpcode() == Instruction::FAdd) { |
| 2587 | if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2588 | return BinaryOperator::CreateFNeg(*Context, Op1I->getOperand(1), |
| 2589 | I.getName()); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2590 | else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2591 | return BinaryOperator::CreateFNeg(*Context, Op1I->getOperand(0), |
| 2592 | I.getName()); |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2593 | } |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2599 | /// isSignBitCheck - Given an exploded icmp instruction, return true if the |
| 2600 | /// comparison only checks the sign bit. If it only checks the sign bit, set |
| 2601 | /// TrueIfSigned if the result of the comparison is true when the input value is |
| 2602 | /// signed. |
| 2603 | static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS, |
| 2604 | bool &TrueIfSigned) { |
| 2605 | switch (pred) { |
| 2606 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 |
| 2607 | TrueIfSigned = true; |
| 2608 | return RHS->isZero(); |
| 2609 | case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1 |
| 2610 | TrueIfSigned = true; |
| 2611 | return RHS->isAllOnesValue(); |
| 2612 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 |
| 2613 | TrueIfSigned = false; |
| 2614 | return RHS->isAllOnesValue(); |
| 2615 | case ICmpInst::ICMP_UGT: |
| 2616 | // True if LHS u> RHS and RHS == high-bit-mask - 1 |
| 2617 | TrueIfSigned = true; |
| 2618 | return RHS->getValue() == |
| 2619 | APInt::getSignedMaxValue(RHS->getType()->getPrimitiveSizeInBits()); |
| 2620 | case ICmpInst::ICMP_UGE: |
| 2621 | // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc) |
| 2622 | TrueIfSigned = true; |
| Chris Lattner | 60813c2 | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2623 | return RHS->getValue().isSignBit(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2624 | default: |
| 2625 | return false; |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | Instruction *InstCombiner::visitMul(BinaryOperator &I) { |
| 2630 | bool Changed = SimplifyCommutative(I); |
| 2631 | Value *Op0 = I.getOperand(0); |
| 2632 | |
| Eli Friedman | e426ded | 2009-07-18 09:12:15 +0000 | [diff] [blame] | 2633 | if (isa<UndefValue>(I.getOperand(1))) // undef * X -> 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2634 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2635 | |
| 2636 | // Simplify mul instructions with a constant RHS... |
| 2637 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2638 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 2639 | |
| 2640 | // ((X << C1)*C2) == (X * (C2 << C1)) |
| 2641 | if (BinaryOperator *SI = dyn_cast<BinaryOperator>(Op0)) |
| 2642 | if (SI->getOpcode() == Instruction::Shl) |
| 2643 | if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1))) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2644 | return BinaryOperator::CreateMul(SI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2645 | Context->getConstantExprShl(CI, ShOp)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2646 | |
| 2647 | if (CI->isZero()) |
| 2648 | return ReplaceInstUsesWith(I, Op1); // X * 0 == 0 |
| 2649 | if (CI->equalsInt(1)) // X * 1 == X |
| 2650 | return ReplaceInstUsesWith(I, Op0); |
| 2651 | if (CI->isAllOnesValue()) // X * -1 == 0 - X |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2652 | return BinaryOperator::CreateNeg(*Context, Op0, I.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2653 | |
| 2654 | const APInt& Val = cast<ConstantInt>(CI)->getValue(); |
| 2655 | if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2656 | return BinaryOperator::CreateShl(Op0, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2657 | Context->getConstantInt(Op0->getType(), Val.logBase2())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2658 | } |
| Chris Lattner | 6297fc7 | 2008-08-11 22:06:05 +0000 | [diff] [blame] | 2659 | } else if (isa<VectorType>(Op1->getType())) { |
| Eli Friedman | 6e05840 | 2009-07-14 02:01:53 +0000 | [diff] [blame] | 2660 | if (Op1->isNullValue()) |
| 2661 | return ReplaceInstUsesWith(I, Op1); |
| Nick Lewycky | 9441873 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 2662 | |
| 2663 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 2664 | if (Op1V->isAllOnesValue()) // X * -1 == 0 - X |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 2665 | return BinaryOperator::CreateNeg(*Context, Op0, I.getName()); |
| Nick Lewycky | 9441873 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 2666 | |
| 2667 | // As above, vector X*splat(1.0) -> X in all defined cases. |
| 2668 | if (Constant *Splat = Op1V->getSplatValue()) { |
| Nick Lewycky | 9441873 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 2669 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Splat)) |
| 2670 | if (CI->equalsInt(1)) |
| 2671 | return ReplaceInstUsesWith(I, Op0); |
| 2672 | } |
| 2673 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) |
| 2677 | if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() && |
| Chris Lattner | 5819408 | 2008-05-18 04:11:26 +0000 | [diff] [blame] | 2678 | isa<ConstantInt>(Op0I->getOperand(1)) && isa<ConstantInt>(Op1)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2679 | // Canonicalize (X+C1)*C2 -> X*C2+C1*C2. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2680 | Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2681 | Op1, "tmp"); |
| 2682 | InsertNewInstBefore(Add, I); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2683 | Value *C1C2 = Context->getConstantExprMul(Op1, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2684 | cast<Constant>(Op0I->getOperand(1))); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2685 | return BinaryOperator::CreateAdd(Add, C1C2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2686 | |
| 2687 | } |
| 2688 | |
| 2689 | // Try to fold constant mul into select arguments. |
| 2690 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2691 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2692 | return R; |
| 2693 | |
| 2694 | if (isa<PHINode>(Op0)) |
| 2695 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2696 | return NV; |
| 2697 | } |
| 2698 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2699 | if (Value *Op0v = dyn_castNegVal(Op0, Context)) // -X * -Y = X*Y |
| 2700 | if (Value *Op1v = dyn_castNegVal(I.getOperand(1), Context)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2701 | return BinaryOperator::CreateMul(Op0v, Op1v); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2702 | |
| Nick Lewycky | 1c24640 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 2703 | // (X / Y) * Y = X - (X % Y) |
| 2704 | // (X / Y) * -Y = (X % Y) - X |
| 2705 | { |
| 2706 | Value *Op1 = I.getOperand(1); |
| 2707 | BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0); |
| 2708 | if (!BO || |
| 2709 | (BO->getOpcode() != Instruction::UDiv && |
| 2710 | BO->getOpcode() != Instruction::SDiv)) { |
| 2711 | Op1 = Op0; |
| 2712 | BO = dyn_cast<BinaryOperator>(I.getOperand(1)); |
| 2713 | } |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2714 | Value *Neg = dyn_castNegVal(Op1, Context); |
| Nick Lewycky | 1c24640 | 2008-11-21 07:33:58 +0000 | [diff] [blame] | 2715 | if (BO && BO->hasOneUse() && |
| 2716 | (BO->getOperand(1) == Op1 || BO->getOperand(1) == Neg) && |
| 2717 | (BO->getOpcode() == Instruction::UDiv || |
| 2718 | BO->getOpcode() == Instruction::SDiv)) { |
| 2719 | Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1); |
| 2720 | |
| 2721 | Instruction *Rem; |
| 2722 | if (BO->getOpcode() == Instruction::UDiv) |
| 2723 | Rem = BinaryOperator::CreateURem(Op0BO, Op1BO); |
| 2724 | else |
| 2725 | Rem = BinaryOperator::CreateSRem(Op0BO, Op1BO); |
| 2726 | |
| 2727 | InsertNewInstBefore(Rem, I); |
| 2728 | Rem->takeName(BO); |
| 2729 | |
| 2730 | if (Op1BO == Op1) |
| 2731 | return BinaryOperator::CreateSub(Op0BO, Rem); |
| 2732 | else |
| 2733 | return BinaryOperator::CreateSub(Rem, Op0BO); |
| 2734 | } |
| 2735 | } |
| 2736 | |
| Nick Lewycky | d4b6367 | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2737 | if (I.getType() == Type::Int1Ty) |
| 2738 | return BinaryOperator::CreateAnd(Op0, I.getOperand(1)); |
| 2739 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2740 | // If one of the operands of the multiply is a cast from a boolean value, then |
| 2741 | // we know the bool is either zero or one, so this is a 'masking' multiply. |
| 2742 | // See if we can simplify things based on how the boolean was originally |
| 2743 | // formed. |
| 2744 | CastInst *BoolCast = 0; |
| Nick Lewycky | d4b6367 | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2745 | if (ZExtInst *CI = dyn_cast<ZExtInst>(Op0)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2746 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
| 2747 | BoolCast = CI; |
| 2748 | if (!BoolCast) |
| 2749 | if (ZExtInst *CI = dyn_cast<ZExtInst>(I.getOperand(1))) |
| 2750 | if (CI->getOperand(0)->getType() == Type::Int1Ty) |
| 2751 | BoolCast = CI; |
| 2752 | if (BoolCast) { |
| 2753 | if (ICmpInst *SCI = dyn_cast<ICmpInst>(BoolCast->getOperand(0))) { |
| 2754 | Value *SCIOp0 = SCI->getOperand(0), *SCIOp1 = SCI->getOperand(1); |
| 2755 | const Type *SCOpTy = SCIOp0->getType(); |
| 2756 | bool TIS = false; |
| 2757 | |
| 2758 | // If the icmp is true iff the sign bit of X is set, then convert this |
| 2759 | // multiply into a shift/and combination. |
| 2760 | if (isa<ConstantInt>(SCIOp1) && |
| 2761 | isSignBitCheck(SCI->getPredicate(), cast<ConstantInt>(SCIOp1), TIS) && |
| 2762 | TIS) { |
| 2763 | // Shift the X value right to turn it into "all signbits". |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2764 | Constant *Amt = Context->getConstantInt(SCIOp0->getType(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2765 | SCOpTy->getPrimitiveSizeInBits()-1); |
| 2766 | Value *V = |
| 2767 | InsertNewInstBefore( |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2768 | BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2769 | BoolCast->getOperand(0)->getName()+ |
| 2770 | ".mask"), I); |
| 2771 | |
| 2772 | // If the multiply type is not the same as the source type, sign extend |
| 2773 | // or truncate to the multiply type. |
| 2774 | if (I.getType() != V->getType()) { |
| 2775 | uint32_t SrcBits = V->getType()->getPrimitiveSizeInBits(); |
| 2776 | uint32_t DstBits = I.getType()->getPrimitiveSizeInBits(); |
| 2777 | Instruction::CastOps opcode = |
| 2778 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2779 | (SrcBits < DstBits ? Instruction::SExt : Instruction::Trunc)); |
| 2780 | V = InsertCastBefore(opcode, V, I.getType(), I); |
| 2781 | } |
| 2782 | |
| 2783 | Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0; |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2784 | return BinaryOperator::CreateAnd(V, OtherOp); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | return Changed ? &I : 0; |
| 2790 | } |
| 2791 | |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2792 | Instruction *InstCombiner::visitFMul(BinaryOperator &I) { |
| 2793 | bool Changed = SimplifyCommutative(I); |
| 2794 | Value *Op0 = I.getOperand(0); |
| 2795 | |
| 2796 | // Simplify mul instructions with a constant RHS... |
| 2797 | if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) { |
| 2798 | if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { |
| 2799 | // "In IEEE floating point, x*1 is not equivalent to x for nans. However, |
| 2800 | // ANSI says we can drop signals, so we can do this anyway." (from GCC) |
| 2801 | if (Op1F->isExactlyValue(1.0)) |
| 2802 | return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0' |
| 2803 | } else if (isa<VectorType>(Op1->getType())) { |
| 2804 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 2805 | // As above, vector X*splat(1.0) -> X in all defined cases. |
| 2806 | if (Constant *Splat = Op1V->getSplatValue()) { |
| 2807 | if (ConstantFP *F = dyn_cast<ConstantFP>(Splat)) |
| 2808 | if (F->isExactlyValue(1.0)) |
| 2809 | return ReplaceInstUsesWith(I, Op0); |
| 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | // Try to fold constant mul into select arguments. |
| 2815 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2816 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2817 | return R; |
| 2818 | |
| 2819 | if (isa<PHINode>(Op0)) |
| 2820 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2821 | return NV; |
| 2822 | } |
| 2823 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2824 | if (Value *Op0v = dyn_castFNegVal(Op0, Context)) // -X * -Y = X*Y |
| 2825 | if (Value *Op1v = dyn_castFNegVal(I.getOperand(1), Context)) |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2826 | return BinaryOperator::CreateFMul(Op0v, Op1v); |
| 2827 | |
| 2828 | return Changed ? &I : 0; |
| 2829 | } |
| 2830 | |
| Chris Lattner | 76972db | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2831 | /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select |
| 2832 | /// instruction. |
| 2833 | bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) { |
| 2834 | SelectInst *SI = cast<SelectInst>(I.getOperand(1)); |
| 2835 | |
| 2836 | // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y |
| 2837 | int NonNullOperand = -1; |
| 2838 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1))) |
| 2839 | if (ST->isNullValue()) |
| 2840 | NonNullOperand = 2; |
| 2841 | // div/rem X, (Cond ? Y : 0) -> div/rem X, Y |
| 2842 | if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2))) |
| 2843 | if (ST->isNullValue()) |
| 2844 | NonNullOperand = 1; |
| 2845 | |
| 2846 | if (NonNullOperand == -1) |
| 2847 | return false; |
| 2848 | |
| 2849 | Value *SelectCond = SI->getOperand(0); |
| 2850 | |
| 2851 | // Change the div/rem to use 'Y' instead of the select. |
| 2852 | I.setOperand(1, SI->getOperand(NonNullOperand)); |
| 2853 | |
| 2854 | // Okay, we know we replace the operand of the div/rem with 'Y' with no |
| 2855 | // problem. However, the select, or the condition of the select may have |
| 2856 | // multiple uses. Based on our knowledge that the operand must be non-zero, |
| 2857 | // propagate the known value for the select into other uses of it, and |
| 2858 | // propagate a known value of the condition into its other users. |
| 2859 | |
| 2860 | // If the select and condition only have a single use, don't bother with this, |
| 2861 | // early exit. |
| 2862 | if (SI->use_empty() && SelectCond->hasOneUse()) |
| 2863 | return true; |
| 2864 | |
| 2865 | // Scan the current block backward, looking for other uses of SI. |
| 2866 | BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin(); |
| 2867 | |
| 2868 | while (BBI != BBFront) { |
| 2869 | --BBI; |
| 2870 | // If we found a call to a function, we can't assume it will return, so |
| 2871 | // information from below it cannot be propagated above it. |
| 2872 | if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI)) |
| 2873 | break; |
| 2874 | |
| 2875 | // Replace uses of the select or its condition with the known values. |
| 2876 | for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end(); |
| 2877 | I != E; ++I) { |
| 2878 | if (*I == SI) { |
| 2879 | *I = SI->getOperand(NonNullOperand); |
| 2880 | AddToWorkList(BBI); |
| 2881 | } else if (*I == SelectCond) { |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 2882 | *I = NonNullOperand == 1 ? Context->getTrue() : |
| 2883 | Context->getFalse(); |
| Chris Lattner | 76972db | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2884 | AddToWorkList(BBI); |
| 2885 | } |
| 2886 | } |
| 2887 | |
| 2888 | // If we past the instruction, quit looking for it. |
| 2889 | if (&*BBI == SI) |
| 2890 | SI = 0; |
| 2891 | if (&*BBI == SelectCond) |
| 2892 | SelectCond = 0; |
| 2893 | |
| 2894 | // If we ran out of things to eliminate, break out of the loop. |
| 2895 | if (SelectCond == 0 && SI == 0) |
| 2896 | break; |
| 2897 | |
| 2898 | } |
| 2899 | return true; |
| 2900 | } |
| 2901 | |
| 2902 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2903 | /// This function implements the transforms on div instructions that work |
| 2904 | /// regardless of the kind of div instruction it is (udiv, sdiv, or fdiv). It is |
| 2905 | /// used by the visitors to those instructions. |
| 2906 | /// @brief Transforms common to all three div instructions |
| 2907 | Instruction *InstCombiner::commonDivTransforms(BinaryOperator &I) { |
| 2908 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2909 | |
| Chris Lattner | 653ef3c | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2910 | // undef / X -> 0 for integer. |
| 2911 | // undef / X -> undef for FP (the undef could be a snan). |
| 2912 | if (isa<UndefValue>(Op0)) { |
| 2913 | if (Op0->getType()->isFPOrFPVector()) |
| 2914 | return ReplaceInstUsesWith(I, Op0); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2915 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Chris Lattner | 653ef3c | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 2916 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2917 | |
| 2918 | // X / undef -> undef |
| 2919 | if (isa<UndefValue>(Op1)) |
| 2920 | return ReplaceInstUsesWith(I, Op1); |
| 2921 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | /// This function implements the transforms common to both integer division |
| 2926 | /// instructions (udiv and sdiv). It is called by the visitors to those integer |
| 2927 | /// division instructions. |
| 2928 | /// @brief Common integer divide transforms |
| 2929 | Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { |
| 2930 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2931 | |
| Chris Lattner | cefb36c | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2932 | // (sdiv X, X) --> 1 (udiv X, X) --> 1 |
| Nick Lewycky | 386c013 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2933 | if (Op0 == Op1) { |
| 2934 | if (const VectorType *Ty = dyn_cast<VectorType>(I.getType())) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2935 | Constant *CI = Context->getConstantInt(Ty->getElementType(), 1); |
| Nick Lewycky | 386c013 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2936 | std::vector<Constant*> Elts(Ty->getNumElements(), CI); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2937 | return ReplaceInstUsesWith(I, Context->getConstantVector(Elts)); |
| Nick Lewycky | 386c013 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2940 | Constant *CI = Context->getConstantInt(I.getType(), 1); |
| Nick Lewycky | 386c013 | 2008-05-23 03:26:47 +0000 | [diff] [blame] | 2941 | return ReplaceInstUsesWith(I, CI); |
| 2942 | } |
| Chris Lattner | cefb36c | 2008-05-16 02:59:42 +0000 | [diff] [blame] | 2943 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2944 | if (Instruction *Common = commonDivTransforms(I)) |
| 2945 | return Common; |
| Chris Lattner | 76972db | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 2946 | |
| 2947 | // Handle cases involving: [su]div X, (select Cond, Y, Z) |
| 2948 | // This does not apply for fdiv. |
| 2949 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 2950 | return &I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2951 | |
| 2952 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 2953 | // div X, 1 == X |
| 2954 | if (RHS->equalsInt(1)) |
| 2955 | return ReplaceInstUsesWith(I, Op0); |
| 2956 | |
| 2957 | // (X / C1) / C2 -> X / (C1*C2) |
| 2958 | if (Instruction *LHS = dyn_cast<Instruction>(Op0)) |
| 2959 | if (Instruction::BinaryOps(LHS->getOpcode()) == I.getOpcode()) |
| 2960 | if (ConstantInt *LHSRHS = dyn_cast<ConstantInt>(LHS->getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2961 | if (MultiplyOverflows(RHS, LHSRHS, |
| 2962 | I.getOpcode()==Instruction::SDiv, Context)) |
| 2963 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Nick Lewycky | 9d798f9 | 2008-02-18 22:48:05 +0000 | [diff] [blame] | 2964 | else |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2965 | return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2966 | Context->getConstantExprMul(RHS, LHSRHS)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | if (!RHS->isZero()) { // avoid X udiv 0 |
| 2970 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 2971 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 2972 | return R; |
| 2973 | if (isa<PHINode>(Op0)) |
| 2974 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 2975 | return NV; |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | // 0 / X == 0, we don't need to preserve faults! |
| 2980 | if (ConstantInt *LHS = dyn_cast<ConstantInt>(Op0)) |
| 2981 | if (LHS->equalsInt(0)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 2982 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2983 | |
| Nick Lewycky | d4b6367 | 2008-05-31 17:59:52 +0000 | [diff] [blame] | 2984 | // It can't be division by zero, hence it must be division by one. |
| 2985 | if (I.getType() == Type::Int1Ty) |
| 2986 | return ReplaceInstUsesWith(I, Op0); |
| 2987 | |
| Nick Lewycky | 9441873 | 2008-11-27 20:21:08 +0000 | [diff] [blame] | 2988 | if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) { |
| 2989 | if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue())) |
| 2990 | // div X, 1 == X |
| 2991 | if (X->isOne()) |
| 2992 | return ReplaceInstUsesWith(I, Op0); |
| 2993 | } |
| 2994 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 2995 | return 0; |
| 2996 | } |
| 2997 | |
| 2998 | Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { |
| 2999 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3000 | |
| 3001 | // Handle the integer div common cases |
| 3002 | if (Instruction *Common = commonIDivTransforms(I)) |
| 3003 | return Common; |
| 3004 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3005 | if (ConstantInt *C = dyn_cast<ConstantInt>(Op1)) { |
| Nick Lewycky | 240182a | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 3006 | // X udiv C^2 -> X >> C |
| 3007 | // Check to see if this is an unsigned division with an exact power of 2, |
| 3008 | // if so, convert to a right shift. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3009 | if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2 |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3010 | return BinaryOperator::CreateLShr(Op0, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3011 | Context->getConstantInt(Op0->getType(), C->getValue().logBase2())); |
| Nick Lewycky | 240182a | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 3012 | |
| 3013 | // X udiv C, where C >= signbit |
| 3014 | if (C->getValue().isNegative()) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3015 | Value *IC = InsertNewInstBefore(new ICmpInst(*Context, |
| 3016 | ICmpInst::ICMP_ULT, Op0, C), |
| Nick Lewycky | 240182a | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 3017 | I); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3018 | return SelectInst::Create(IC, Context->getNullValue(I.getType()), |
| 3019 | Context->getConstantInt(I.getType(), 1)); |
| Nick Lewycky | 240182a | 2008-11-27 22:41:10 +0000 | [diff] [blame] | 3020 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2) |
| 3024 | if (BinaryOperator *RHSI = dyn_cast<BinaryOperator>(I.getOperand(1))) { |
| 3025 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3026 | isa<ConstantInt>(RHSI->getOperand(0))) { |
| 3027 | const APInt& C1 = cast<ConstantInt>(RHSI->getOperand(0))->getValue(); |
| 3028 | if (C1.isPowerOf2()) { |
| 3029 | Value *N = RHSI->getOperand(1); |
| 3030 | const Type *NTy = N->getType(); |
| 3031 | if (uint32_t C2 = C1.logBase2()) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3032 | Constant *C2V = Context->getConstantInt(NTy, C2); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3033 | N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3034 | } |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3035 | return BinaryOperator::CreateLShr(Op0, N); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | // udiv X, (Select Cond, C1, C2) --> Select Cond, (shr X, C1), (shr X, C2) |
| 3041 | // where C1&C2 are powers of two. |
| 3042 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 3043 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 3044 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 3045 | const APInt &TVA = STO->getValue(), &FVA = SFO->getValue(); |
| 3046 | if (TVA.isPowerOf2() && FVA.isPowerOf2()) { |
| 3047 | // Compute the shift amounts |
| 3048 | uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2(); |
| 3049 | // Construct the "on true" case of the select |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3050 | Constant *TC = Context->getConstantInt(Op0->getType(), TSA); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3051 | Instruction *TSI = BinaryOperator::CreateLShr( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3052 | Op0, TC, SI->getName()+".t"); |
| 3053 | TSI = InsertNewInstBefore(TSI, I); |
| 3054 | |
| 3055 | // Construct the "on false" case of the select |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3056 | Constant *FC = Context->getConstantInt(Op0->getType(), FSA); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3057 | Instruction *FSI = BinaryOperator::CreateLShr( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3058 | Op0, FC, SI->getName()+".f"); |
| 3059 | FSI = InsertNewInstBefore(FSI, I); |
| 3060 | |
| 3061 | // construct the select instruction and return it. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3062 | return SelectInst::Create(SI->getOperand(0), TSI, FSI, SI->getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | return 0; |
| 3066 | } |
| 3067 | |
| 3068 | Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { |
| 3069 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3070 | |
| 3071 | // Handle the integer div common cases |
| 3072 | if (Instruction *Common = commonIDivTransforms(I)) |
| 3073 | return Common; |
| 3074 | |
| 3075 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3076 | // sdiv X, -1 == -X |
| 3077 | if (RHS->isAllOnesValue()) |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 3078 | return BinaryOperator::CreateNeg(*Context, Op0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| 3082 | // unsigned inputs), turn this into a udiv. |
| 3083 | if (I.getType()->isInteger()) { |
| 3084 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| Eli Friedman | a17b85f | 2009-07-18 09:53:21 +0000 | [diff] [blame] | 3085 | if (MaskedValueIsZero(Op0, Mask)) { |
| 3086 | if (MaskedValueIsZero(Op1, Mask)) { |
| 3087 | // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set |
| 3088 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 3089 | } |
| 3090 | ConstantInt *ShiftedInt; |
| 3091 | if (match(Op1, m_Shl(m_ConstantInt(ShiftedInt), m_Value()), *Context) && |
| 3092 | ShiftedInt->getValue().isPowerOf2()) { |
| 3093 | // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y) |
| 3094 | // Safe because the only negative value (1 << Y) can take on is |
| 3095 | // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have |
| 3096 | // the sign bit set. |
| 3097 | return BinaryOperator::CreateUDiv(Op0, Op1, I.getName()); |
| 3098 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3099 | } |
| Eli Friedman | a17b85f | 2009-07-18 09:53:21 +0000 | [diff] [blame] | 3100 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3101 | |
| 3102 | return 0; |
| 3103 | } |
| 3104 | |
| 3105 | Instruction *InstCombiner::visitFDiv(BinaryOperator &I) { |
| 3106 | return commonDivTransforms(I); |
| 3107 | } |
| 3108 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3109 | /// This function implements the transforms on rem instructions that work |
| 3110 | /// regardless of the kind of rem instruction it is (urem, srem, or frem). It |
| 3111 | /// is used by the visitors to those instructions. |
| 3112 | /// @brief Transforms common to all three rem instructions |
| 3113 | Instruction *InstCombiner::commonRemTransforms(BinaryOperator &I) { |
| 3114 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3115 | |
| Chris Lattner | 653ef3c | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3116 | if (isa<UndefValue>(Op0)) { // undef % X -> 0 |
| 3117 | if (I.getType()->isFPOrFPVector()) |
| 3118 | return ReplaceInstUsesWith(I, Op0); // X % undef -> undef (could be SNaN) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3119 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Chris Lattner | 653ef3c | 2008-02-19 06:12:18 +0000 | [diff] [blame] | 3120 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3121 | if (isa<UndefValue>(Op1)) |
| 3122 | return ReplaceInstUsesWith(I, Op1); // X % undef -> undef |
| 3123 | |
| 3124 | // Handle cases involving: rem X, (select Cond, Y, Z) |
| Chris Lattner | 76972db | 2008-07-14 00:15:52 +0000 | [diff] [blame] | 3125 | if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I)) |
| 3126 | return &I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3127 | |
| 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | /// This function implements the transforms common to both integer remainder |
| 3132 | /// instructions (urem and srem). It is called by the visitors to those integer |
| 3133 | /// remainder instructions. |
| 3134 | /// @brief Common integer remainder transforms |
| 3135 | Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) { |
| 3136 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3137 | |
| 3138 | if (Instruction *common = commonRemTransforms(I)) |
| 3139 | return common; |
| 3140 | |
| Dale Johannesen | a51f737 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 3141 | // 0 % X == 0 for integer, we don't need to preserve faults! |
| 3142 | if (Constant *LHS = dyn_cast<Constant>(Op0)) |
| 3143 | if (LHS->isNullValue()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3144 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dale Johannesen | a51f737 | 2009-01-21 00:35:19 +0000 | [diff] [blame] | 3145 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3146 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3147 | // X % 0 == undef, we don't need to preserve faults! |
| 3148 | if (RHS->equalsInt(0)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3149 | return ReplaceInstUsesWith(I, Context->getUndef(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3150 | |
| 3151 | if (RHS->equalsInt(1)) // X % 1 == 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3152 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3153 | |
| 3154 | if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) { |
| 3155 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) { |
| 3156 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 3157 | return R; |
| 3158 | } else if (isa<PHINode>(Op0I)) { |
| 3159 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 3160 | return NV; |
| 3161 | } |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3162 | |
| 3163 | // See if we can fold away this rem instruction. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 3164 | if (SimplifyDemandedInstructionBits(I)) |
| Nick Lewycky | c1372c8 | 2008-03-06 06:48:30 +0000 | [diff] [blame] | 3165 | return &I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | return 0; |
| 3170 | } |
| 3171 | |
| 3172 | Instruction *InstCombiner::visitURem(BinaryOperator &I) { |
| 3173 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3174 | |
| 3175 | if (Instruction *common = commonIRemTransforms(I)) |
| 3176 | return common; |
| 3177 | |
| 3178 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 3179 | // X urem C^2 -> X and C |
| 3180 | // Check to see if this is an unsigned remainder with an exact power of 2, |
| 3181 | // if so, convert to a bitwise and. |
| 3182 | if (ConstantInt *C = dyn_cast<ConstantInt>(RHS)) |
| 3183 | if (C->getValue().isPowerOf2()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3184 | return BinaryOperator::CreateAnd(Op0, SubOne(C, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | if (Instruction *RHSI = dyn_cast<Instruction>(I.getOperand(1))) { |
| 3188 | // Turn A % (C << N), where C is 2^k, into A & ((C << N)-1) |
| 3189 | if (RHSI->getOpcode() == Instruction::Shl && |
| 3190 | isa<ConstantInt>(RHSI->getOperand(0))) { |
| 3191 | if (cast<ConstantInt>(RHSI->getOperand(0))->getValue().isPowerOf2()) { |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 3192 | Constant *N1 = Context->getAllOnesValue(I.getType()); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3193 | Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3194 | "tmp"), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3195 | return BinaryOperator::CreateAnd(Op0, Add); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3196 | } |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | // urem X, (select Cond, 2^C1, 2^C2) --> select Cond, (and X, C1), (and X, C2) |
| 3201 | // where C1&C2 are powers of two. |
| 3202 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) { |
| 3203 | if (ConstantInt *STO = dyn_cast<ConstantInt>(SI->getOperand(1))) |
| 3204 | if (ConstantInt *SFO = dyn_cast<ConstantInt>(SI->getOperand(2))) { |
| 3205 | // STO == 0 and SFO == 0 handled above. |
| 3206 | if ((STO->getValue().isPowerOf2()) && |
| 3207 | (SFO->getValue().isPowerOf2())) { |
| 3208 | Value *TrueAnd = InsertNewInstBefore( |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3209 | BinaryOperator::CreateAnd(Op0, SubOne(STO, Context), |
| 3210 | SI->getName()+".t"), I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3211 | Value *FalseAnd = InsertNewInstBefore( |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3212 | BinaryOperator::CreateAnd(Op0, SubOne(SFO, Context), |
| 3213 | SI->getName()+".f"), I); |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 3214 | return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3215 | } |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
| 3222 | Instruction *InstCombiner::visitSRem(BinaryOperator &I) { |
| 3223 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 3224 | |
| Dan Gohman | db3dd96 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3225 | // Handle the integer rem common cases |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3226 | if (Instruction *common = commonIRemTransforms(I)) |
| 3227 | return common; |
| 3228 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3229 | if (Value *RHSNeg = dyn_castNegVal(Op1, Context)) |
| Nick Lewycky | cfadfbd | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 3230 | if (!isa<Constant>(RHSNeg) || |
| 3231 | (isa<ConstantInt>(RHSNeg) && |
| 3232 | cast<ConstantInt>(RHSNeg)->getValue().isStrictlyPositive())) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3233 | // X % -Y -> X % Y |
| 3234 | AddUsesToWorkList(I); |
| 3235 | I.setOperand(1, RHSNeg); |
| 3236 | return &I; |
| 3237 | } |
| Nick Lewycky | 5515c7a | 2008-09-30 06:08:34 +0000 | [diff] [blame] | 3238 | |
| Dan Gohman | db3dd96 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3239 | // If the sign bits of both operands are zero (i.e. we can prove they are |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3240 | // unsigned inputs), turn this into a urem. |
| Dan Gohman | db3dd96 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3241 | if (I.getType()->isInteger()) { |
| 3242 | APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())); |
| 3243 | if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) { |
| 3244 | // X srem Y -> X urem Y, iff X and Y don't have sign bit set |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3245 | return BinaryOperator::CreateURem(Op0, Op1, I.getName()); |
| Dan Gohman | db3dd96 | 2007-11-05 23:16:33 +0000 | [diff] [blame] | 3246 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| Nick Lewycky | da9fa43 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3249 | // If it's a constant vector, flip any negative values positive. |
| Nick Lewycky | fd74683 | 2008-12-20 16:48:00 +0000 | [diff] [blame] | 3250 | if (ConstantVector *RHSV = dyn_cast<ConstantVector>(Op1)) { |
| 3251 | unsigned VWidth = RHSV->getNumOperands(); |
| Nick Lewycky | da9fa43 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3252 | |
| Nick Lewycky | fd74683 | 2008-12-20 16:48:00 +0000 | [diff] [blame] | 3253 | bool hasNegative = false; |
| 3254 | for (unsigned i = 0; !hasNegative && i != VWidth; ++i) |
| 3255 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) |
| 3256 | if (RHS->getValue().isNegative()) |
| 3257 | hasNegative = true; |
| 3258 | |
| 3259 | if (hasNegative) { |
| 3260 | std::vector<Constant *> Elts(VWidth); |
| Nick Lewycky | da9fa43 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3261 | for (unsigned i = 0; i != VWidth; ++i) { |
| 3262 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV->getOperand(i))) { |
| 3263 | if (RHS->getValue().isNegative()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3264 | Elts[i] = cast<ConstantInt>(Context->getConstantExprNeg(RHS)); |
| Nick Lewycky | da9fa43 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3265 | else |
| 3266 | Elts[i] = RHS; |
| 3267 | } |
| 3268 | } |
| 3269 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3270 | Constant *NewRHSV = Context->getConstantVector(Elts); |
| Nick Lewycky | da9fa43 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3271 | if (NewRHSV != RHSV) { |
| Nick Lewycky | 338ecd5 | 2008-12-18 06:42:28 +0000 | [diff] [blame] | 3272 | AddUsesToWorkList(I); |
| Nick Lewycky | da9fa43 | 2008-12-18 06:31:11 +0000 | [diff] [blame] | 3273 | I.setOperand(1, NewRHSV); |
| 3274 | return &I; |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3279 | return 0; |
| 3280 | } |
| 3281 | |
| 3282 | Instruction *InstCombiner::visitFRem(BinaryOperator &I) { |
| 3283 | return commonRemTransforms(I); |
| 3284 | } |
| 3285 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3286 | // isOneBitSet - Return true if there is exactly one bit set in the specified |
| 3287 | // constant. |
| 3288 | static bool isOneBitSet(const ConstantInt *CI) { |
| 3289 | return CI->getValue().isPowerOf2(); |
| 3290 | } |
| 3291 | |
| 3292 | // isHighOnes - Return true if the constant is of the form 1+0+. |
| 3293 | // This is the same as lowones(~X). |
| 3294 | static bool isHighOnes(const ConstantInt *CI) { |
| 3295 | return (~CI->getValue() + 1).isPowerOf2(); |
| 3296 | } |
| 3297 | |
| 3298 | /// getICmpCode - Encode a icmp predicate into a three bit mask. These bits |
| 3299 | /// are carefully arranged to allow folding of expressions such as: |
| 3300 | /// |
| 3301 | /// (A < B) | (A > B) --> (A != B) |
| 3302 | /// |
| 3303 | /// Note that this is only valid if the first and second predicates have the |
| 3304 | /// same sign. Is illegal to do: (A u< B) | (A s> B) |
| 3305 | /// |
| 3306 | /// Three bits are used to represent the condition, as follows: |
| 3307 | /// 0 A > B |
| 3308 | /// 1 A == B |
| 3309 | /// 2 A < B |
| 3310 | /// |
| 3311 | /// <=> Value Definition |
| 3312 | /// 000 0 Always false |
| 3313 | /// 001 1 A > B |
| 3314 | /// 010 2 A == B |
| 3315 | /// 011 3 A >= B |
| 3316 | /// 100 4 A < B |
| 3317 | /// 101 5 A != B |
| 3318 | /// 110 6 A <= B |
| 3319 | /// 111 7 Always true |
| 3320 | /// |
| 3321 | static unsigned getICmpCode(const ICmpInst *ICI) { |
| 3322 | switch (ICI->getPredicate()) { |
| 3323 | // False -> 0 |
| 3324 | case ICmpInst::ICMP_UGT: return 1; // 001 |
| 3325 | case ICmpInst::ICMP_SGT: return 1; // 001 |
| 3326 | case ICmpInst::ICMP_EQ: return 2; // 010 |
| 3327 | case ICmpInst::ICMP_UGE: return 3; // 011 |
| 3328 | case ICmpInst::ICMP_SGE: return 3; // 011 |
| 3329 | case ICmpInst::ICMP_ULT: return 4; // 100 |
| 3330 | case ICmpInst::ICMP_SLT: return 4; // 100 |
| 3331 | case ICmpInst::ICMP_NE: return 5; // 101 |
| 3332 | case ICmpInst::ICMP_ULE: return 6; // 110 |
| 3333 | case ICmpInst::ICMP_SLE: return 6; // 110 |
| 3334 | // True -> 7 |
| 3335 | default: |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3336 | llvm_unreachable("Invalid ICmp predicate!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3337 | return 0; |
| 3338 | } |
| 3339 | } |
| 3340 | |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3341 | /// getFCmpCode - Similar to getICmpCode but for FCmpInst. This encodes a fcmp |
| 3342 | /// predicate into a three bit mask. It also returns whether it is an ordered |
| 3343 | /// predicate by reference. |
| 3344 | static unsigned getFCmpCode(FCmpInst::Predicate CC, bool &isOrdered) { |
| 3345 | isOrdered = false; |
| 3346 | switch (CC) { |
| 3347 | case FCmpInst::FCMP_ORD: isOrdered = true; return 0; // 000 |
| 3348 | case FCmpInst::FCMP_UNO: return 0; // 000 |
| Evan Cheng | f1f2cea | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3349 | case FCmpInst::FCMP_OGT: isOrdered = true; return 1; // 001 |
| 3350 | case FCmpInst::FCMP_UGT: return 1; // 001 |
| 3351 | case FCmpInst::FCMP_OEQ: isOrdered = true; return 2; // 010 |
| 3352 | case FCmpInst::FCMP_UEQ: return 2; // 010 |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3353 | case FCmpInst::FCMP_OGE: isOrdered = true; return 3; // 011 |
| 3354 | case FCmpInst::FCMP_UGE: return 3; // 011 |
| 3355 | case FCmpInst::FCMP_OLT: isOrdered = true; return 4; // 100 |
| 3356 | case FCmpInst::FCMP_ULT: return 4; // 100 |
| Evan Cheng | f1f2cea | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3357 | case FCmpInst::FCMP_ONE: isOrdered = true; return 5; // 101 |
| 3358 | case FCmpInst::FCMP_UNE: return 5; // 101 |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3359 | case FCmpInst::FCMP_OLE: isOrdered = true; return 6; // 110 |
| 3360 | case FCmpInst::FCMP_ULE: return 6; // 110 |
| Evan Cheng | 7298805 | 2008-10-14 18:44:08 +0000 | [diff] [blame] | 3361 | // True -> 7 |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3362 | default: |
| 3363 | // Not expecting FCMP_FALSE and FCMP_TRUE; |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3364 | llvm_unreachable("Unexpected FCmp predicate!"); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3365 | return 0; |
| 3366 | } |
| 3367 | } |
| 3368 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3369 | /// getICmpValue - This is the complement of getICmpCode, which turns an |
| 3370 | /// opcode and two operands into either a constant true or false, or a brand |
| Dan Gohman | da33874 | 2007-09-17 17:31:57 +0000 | [diff] [blame] | 3371 | /// new ICmp instruction. The sign is passed in to determine which kind |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3372 | /// of predicate to use in the new icmp instruction. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3373 | static Value *getICmpValue(bool sign, unsigned code, Value *LHS, Value *RHS, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 3374 | LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3375 | switch (code) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3376 | default: llvm_unreachable("Illegal ICmp code!"); |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 3377 | case 0: return Context->getFalse(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3378 | case 1: |
| 3379 | if (sign) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3380 | return new ICmpInst(*Context, ICmpInst::ICMP_SGT, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3381 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3382 | return new ICmpInst(*Context, ICmpInst::ICMP_UGT, LHS, RHS); |
| 3383 | case 2: return new ICmpInst(*Context, ICmpInst::ICMP_EQ, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3384 | case 3: |
| 3385 | if (sign) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3386 | return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3387 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3388 | return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3389 | case 4: |
| 3390 | if (sign) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3391 | return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3392 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3393 | return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHS, RHS); |
| 3394 | case 5: return new ICmpInst(*Context, ICmpInst::ICMP_NE, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3395 | case 6: |
| 3396 | if (sign) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3397 | return new ICmpInst(*Context, ICmpInst::ICMP_SLE, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3398 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3399 | return new ICmpInst(*Context, ICmpInst::ICMP_ULE, LHS, RHS); |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 3400 | case 7: return Context->getTrue(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3401 | } |
| 3402 | } |
| 3403 | |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3404 | /// getFCmpValue - This is the complement of getFCmpCode, which turns an |
| 3405 | /// opcode and two operands into either a FCmp instruction. isordered is passed |
| 3406 | /// in to determine which kind of predicate to use in the new fcmp instruction. |
| 3407 | static Value *getFCmpValue(bool isordered, unsigned code, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 3408 | Value *LHS, Value *RHS, LLVMContext *Context) { |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3409 | switch (code) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3410 | default: llvm_unreachable("Illegal FCmp code!"); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3411 | case 0: |
| 3412 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3413 | return new FCmpInst(*Context, FCmpInst::FCMP_ORD, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3414 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3415 | return new FCmpInst(*Context, FCmpInst::FCMP_UNO, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3416 | case 1: |
| 3417 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3418 | return new FCmpInst(*Context, FCmpInst::FCMP_OGT, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3419 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3420 | return new FCmpInst(*Context, FCmpInst::FCMP_UGT, LHS, RHS); |
| Evan Cheng | f1f2cea | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3421 | case 2: |
| 3422 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3423 | return new FCmpInst(*Context, FCmpInst::FCMP_OEQ, LHS, RHS); |
| Evan Cheng | f1f2cea | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3424 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3425 | return new FCmpInst(*Context, FCmpInst::FCMP_UEQ, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3426 | case 3: |
| 3427 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3428 | return new FCmpInst(*Context, FCmpInst::FCMP_OGE, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3429 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3430 | return new FCmpInst(*Context, FCmpInst::FCMP_UGE, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3431 | case 4: |
| 3432 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3433 | return new FCmpInst(*Context, FCmpInst::FCMP_OLT, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3434 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3435 | return new FCmpInst(*Context, FCmpInst::FCMP_ULT, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3436 | case 5: |
| 3437 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3438 | return new FCmpInst(*Context, FCmpInst::FCMP_ONE, LHS, RHS); |
| Evan Cheng | f1f2cea | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3439 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3440 | return new FCmpInst(*Context, FCmpInst::FCMP_UNE, LHS, RHS); |
| Evan Cheng | f1f2cea | 2008-10-14 18:13:38 +0000 | [diff] [blame] | 3441 | case 6: |
| 3442 | if (isordered) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3443 | return new FCmpInst(*Context, FCmpInst::FCMP_OLE, LHS, RHS); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3444 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3445 | return new FCmpInst(*Context, FCmpInst::FCMP_ULE, LHS, RHS); |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 3446 | case 7: return Context->getTrue(); |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 3447 | } |
| 3448 | } |
| 3449 | |
| Chris Lattner | 2972b82 | 2008-11-16 04:55:20 +0000 | [diff] [blame] | 3450 | /// PredicatesFoldable - Return true if both predicates match sign or if at |
| 3451 | /// least one of them is an equality comparison (which is signless). |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3452 | static bool PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) { |
| 3453 | return (ICmpInst::isSignedPredicate(p1) == ICmpInst::isSignedPredicate(p2)) || |
| Chris Lattner | 2972b82 | 2008-11-16 04:55:20 +0000 | [diff] [blame] | 3454 | (ICmpInst::isSignedPredicate(p1) && ICmpInst::isEquality(p2)) || |
| 3455 | (ICmpInst::isSignedPredicate(p2) && ICmpInst::isEquality(p1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | namespace { |
| 3459 | // FoldICmpLogical - Implements (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| 3460 | struct FoldICmpLogical { |
| 3461 | InstCombiner &IC; |
| 3462 | Value *LHS, *RHS; |
| 3463 | ICmpInst::Predicate pred; |
| 3464 | FoldICmpLogical(InstCombiner &ic, ICmpInst *ICI) |
| 3465 | : IC(ic), LHS(ICI->getOperand(0)), RHS(ICI->getOperand(1)), |
| 3466 | pred(ICI->getPredicate()) {} |
| 3467 | bool shouldApply(Value *V) const { |
| 3468 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(V)) |
| 3469 | if (PredicatesFoldable(pred, ICI->getPredicate())) |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 3470 | return ((ICI->getOperand(0) == LHS && ICI->getOperand(1) == RHS) || |
| 3471 | (ICI->getOperand(0) == RHS && ICI->getOperand(1) == LHS)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3472 | return false; |
| 3473 | } |
| 3474 | Instruction *apply(Instruction &Log) const { |
| 3475 | ICmpInst *ICI = cast<ICmpInst>(Log.getOperand(0)); |
| 3476 | if (ICI->getOperand(0) != LHS) { |
| 3477 | assert(ICI->getOperand(1) == LHS); |
| 3478 | ICI->swapOperands(); // Swap the LHS and RHS of the ICmp |
| 3479 | } |
| 3480 | |
| 3481 | ICmpInst *RHSICI = cast<ICmpInst>(Log.getOperand(1)); |
| 3482 | unsigned LHSCode = getICmpCode(ICI); |
| 3483 | unsigned RHSCode = getICmpCode(RHSICI); |
| 3484 | unsigned Code; |
| 3485 | switch (Log.getOpcode()) { |
| 3486 | case Instruction::And: Code = LHSCode & RHSCode; break; |
| 3487 | case Instruction::Or: Code = LHSCode | RHSCode; break; |
| 3488 | case Instruction::Xor: Code = LHSCode ^ RHSCode; break; |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3489 | default: llvm_unreachable("Illegal logical opcode!"); return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | bool isSigned = ICmpInst::isSignedPredicate(RHSICI->getPredicate()) || |
| 3493 | ICmpInst::isSignedPredicate(ICI->getPredicate()); |
| 3494 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3495 | Value *RV = getICmpValue(isSigned, Code, LHS, RHS, IC.getContext()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3496 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 3497 | return I; |
| 3498 | // Otherwise, it's a constant boolean value... |
| 3499 | return IC.ReplaceInstUsesWith(Log, RV); |
| 3500 | } |
| 3501 | }; |
| 3502 | } // end anonymous namespace |
| 3503 | |
| 3504 | // OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where |
| 3505 | // the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is |
| 3506 | // guaranteed to be a binary operator. |
| 3507 | Instruction *InstCombiner::OptAndOp(Instruction *Op, |
| 3508 | ConstantInt *OpRHS, |
| 3509 | ConstantInt *AndRHS, |
| 3510 | BinaryOperator &TheAnd) { |
| 3511 | Value *X = Op->getOperand(0); |
| 3512 | Constant *Together = 0; |
| 3513 | if (!Op->isShift()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3514 | Together = Context->getConstantExprAnd(AndRHS, OpRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3515 | |
| 3516 | switch (Op->getOpcode()) { |
| 3517 | case Instruction::Xor: |
| 3518 | if (Op->hasOneUse()) { |
| 3519 | // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3520 | Instruction *And = BinaryOperator::CreateAnd(X, AndRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3521 | InsertNewInstBefore(And, TheAnd); |
| 3522 | And->takeName(Op); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3523 | return BinaryOperator::CreateXor(And, Together); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3524 | } |
| 3525 | break; |
| 3526 | case Instruction::Or: |
| 3527 | if (Together == AndRHS) // (X | C) & C --> C |
| 3528 | return ReplaceInstUsesWith(TheAnd, AndRHS); |
| 3529 | |
| 3530 | if (Op->hasOneUse() && Together != OpRHS) { |
| 3531 | // (X | C1) & C2 --> (X | (C1&C2)) & C2 |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3532 | Instruction *Or = BinaryOperator::CreateOr(X, Together); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3533 | InsertNewInstBefore(Or, TheAnd); |
| 3534 | Or->takeName(Op); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3535 | return BinaryOperator::CreateAnd(Or, AndRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3536 | } |
| 3537 | break; |
| 3538 | case Instruction::Add: |
| 3539 | if (Op->hasOneUse()) { |
| 3540 | // Adding a one to a single bit bit-field should be turned into an XOR |
| 3541 | // of the bit. First thing to check is to see if this AND is with a |
| 3542 | // single bit constant. |
| 3543 | const APInt& AndRHSV = cast<ConstantInt>(AndRHS)->getValue(); |
| 3544 | |
| 3545 | // If there is only one bit set... |
| 3546 | if (isOneBitSet(cast<ConstantInt>(AndRHS))) { |
| 3547 | // Ok, at this point, we know that we are masking the result of the |
| 3548 | // ADD down to exactly one bit. If the constant we are adding has |
| 3549 | // no bits set below this bit, then we can eliminate the ADD. |
| 3550 | const APInt& AddRHS = cast<ConstantInt>(OpRHS)->getValue(); |
| 3551 | |
| 3552 | // Check to see if any bits below the one bit set in AndRHSV are set. |
| 3553 | if ((AddRHS & (AndRHSV-1)) == 0) { |
| 3554 | // If not, the only thing that can effect the output of the AND is |
| 3555 | // the bit specified by AndRHSV. If that bit is set, the effect of |
| 3556 | // the XOR is to toggle the bit. If it is clear, then the ADD has |
| 3557 | // no effect. |
| 3558 | if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop |
| 3559 | TheAnd.setOperand(0, X); |
| 3560 | return &TheAnd; |
| 3561 | } else { |
| 3562 | // Pull the XOR out of the AND. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3563 | Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3564 | InsertNewInstBefore(NewAnd, TheAnd); |
| 3565 | NewAnd->takeName(Op); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3566 | return BinaryOperator::CreateXor(NewAnd, AndRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3567 | } |
| 3568 | } |
| 3569 | } |
| 3570 | } |
| 3571 | break; |
| 3572 | |
| 3573 | case Instruction::Shl: { |
| 3574 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3575 | // the anded constant includes them, clear them now! |
| 3576 | // |
| 3577 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
| 3578 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
| 3579 | APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3580 | ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShlMask); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3581 | |
| 3582 | if (CI->getValue() == ShlMask) { |
| 3583 | // Masking out bits that the shift already masks |
| 3584 | return ReplaceInstUsesWith(TheAnd, Op); // No need for the and. |
| 3585 | } else if (CI != AndRHS) { // Reducing bits set in and. |
| 3586 | TheAnd.setOperand(1, CI); |
| 3587 | return &TheAnd; |
| 3588 | } |
| 3589 | break; |
| 3590 | } |
| 3591 | case Instruction::LShr: |
| 3592 | { |
| 3593 | // We know that the AND will not produce any of the bits shifted in, so if |
| 3594 | // the anded constant includes them, clear them now! This only applies to |
| 3595 | // unsigned shifts, because a signed shr may bring in set bits! |
| 3596 | // |
| 3597 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
| 3598 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
| 3599 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3600 | ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShrMask); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3601 | |
| 3602 | if (CI->getValue() == ShrMask) { |
| 3603 | // Masking out bits that the shift already masks. |
| 3604 | return ReplaceInstUsesWith(TheAnd, Op); |
| 3605 | } else if (CI != AndRHS) { |
| 3606 | TheAnd.setOperand(1, CI); // Reduce bits set in and cst. |
| 3607 | return &TheAnd; |
| 3608 | } |
| 3609 | break; |
| 3610 | } |
| 3611 | case Instruction::AShr: |
| 3612 | // Signed shr. |
| 3613 | // See if this is shifting in some sign extension, then masking it out |
| 3614 | // with an and. |
| 3615 | if (Op->hasOneUse()) { |
| 3616 | uint32_t BitWidth = AndRHS->getType()->getBitWidth(); |
| 3617 | uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth); |
| 3618 | APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3619 | Constant *C = Context->getConstantInt(AndRHS->getValue() & ShrMask); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3620 | if (C == AndRHS) { // Masking out bits shifted in. |
| 3621 | // (Val ashr C1) & C2 -> (Val lshr C1) & C2 |
| 3622 | // Make the argument unsigned. |
| 3623 | Value *ShVal = Op->getOperand(0); |
| 3624 | ShVal = InsertNewInstBefore( |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3625 | BinaryOperator::CreateLShr(ShVal, OpRHS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3626 | Op->getName()), TheAnd); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3627 | return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | break; |
| 3631 | } |
| 3632 | return 0; |
| 3633 | } |
| 3634 | |
| 3635 | |
| 3636 | /// InsertRangeTest - Emit a computation of: (V >= Lo && V < Hi) if Inside is |
| 3637 | /// true, otherwise (V < Lo || V >= Hi). In pratice, we emit the more efficient |
| 3638 | /// (V-Lo) <u Hi-Lo. This method expects that Lo <= Hi. isSigned indicates |
| 3639 | /// whether to treat the V, Lo and HI as signed or not. IB is the location to |
| 3640 | /// insert new instructions. |
| 3641 | Instruction *InstCombiner::InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 3642 | bool isSigned, bool Inside, |
| 3643 | Instruction &IB) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3644 | assert(cast<ConstantInt>(Context->getConstantExprICmp((isSigned ? |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3645 | ICmpInst::ICMP_SLE:ICmpInst::ICMP_ULE), Lo, Hi))->getZExtValue() && |
| 3646 | "Lo is not <= Hi in range emission code!"); |
| 3647 | |
| 3648 | if (Inside) { |
| 3649 | if (Lo == Hi) // Trivially false. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3650 | return new ICmpInst(*Context, ICmpInst::ICMP_NE, V, V); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3651 | |
| 3652 | // V >= Min && V < Hi --> V < Hi |
| 3653 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
| 3654 | ICmpInst::Predicate pred = (isSigned ? |
| 3655 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3656 | return new ICmpInst(*Context, pred, V, Hi); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | // Emit V-Lo <u Hi-Lo |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3660 | Constant *NegLo = Context->getConstantExprNeg(Lo); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3661 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3662 | InsertNewInstBefore(Add, IB); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3663 | Constant *UpperBound = Context->getConstantExprAdd(NegLo, Hi); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3664 | return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, UpperBound); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | if (Lo == Hi) // Trivially true. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3668 | return new ICmpInst(*Context, ICmpInst::ICMP_EQ, V, V); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3669 | |
| 3670 | // V < Min || V >= Hi -> V > Hi-1 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3671 | Hi = SubOne(cast<ConstantInt>(Hi), Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3672 | if (cast<ConstantInt>(Lo)->isMinValue(isSigned)) { |
| 3673 | ICmpInst::Predicate pred = (isSigned ? |
| 3674 | ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3675 | return new ICmpInst(*Context, pred, V, Hi); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | // Emit V-Lo >u Hi-1-Lo |
| 3679 | // Note that Hi has already had one subtracted from it, above. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3680 | ConstantInt *NegLo = cast<ConstantInt>(Context->getConstantExprNeg(Lo)); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3681 | Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3682 | InsertNewInstBefore(Add, IB); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3683 | Constant *LowerBound = Context->getConstantExprAdd(NegLo, Hi); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3684 | return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add, LowerBound); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 3688 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 3689 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 3690 | // not, since all 1s are not contiguous. |
| 3691 | static bool isRunOfOnes(ConstantInt *Val, uint32_t &MB, uint32_t &ME) { |
| 3692 | const APInt& V = Val->getValue(); |
| 3693 | uint32_t BitWidth = Val->getType()->getBitWidth(); |
| 3694 | if (!APIntOps::isShiftedMask(BitWidth, V)) return false; |
| 3695 | |
| 3696 | // look for the first zero bit after the run of ones |
| 3697 | MB = BitWidth - ((V - 1) ^ V).countLeadingZeros(); |
| 3698 | // look for the first non-zero bit |
| 3699 | ME = V.getActiveBits(); |
| 3700 | return true; |
| 3701 | } |
| 3702 | |
| 3703 | /// FoldLogicalPlusAnd - This is part of an expression (LHS +/- RHS) & Mask, |
| 3704 | /// where isSub determines whether the operator is a sub. If we can fold one of |
| 3705 | /// the following xforms: |
| 3706 | /// |
| 3707 | /// ((A & N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == Mask |
| 3708 | /// ((A | N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3709 | /// ((A ^ N) +/- B) & Mask -> (A +/- B) & Mask iff N&Mask == 0 |
| 3710 | /// |
| 3711 | /// return (A +/- B). |
| 3712 | /// |
| 3713 | Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, |
| 3714 | ConstantInt *Mask, bool isSub, |
| 3715 | Instruction &I) { |
| 3716 | Instruction *LHSI = dyn_cast<Instruction>(LHS); |
| 3717 | if (!LHSI || LHSI->getNumOperands() != 2 || |
| 3718 | !isa<ConstantInt>(LHSI->getOperand(1))) return 0; |
| 3719 | |
| 3720 | ConstantInt *N = cast<ConstantInt>(LHSI->getOperand(1)); |
| 3721 | |
| 3722 | switch (LHSI->getOpcode()) { |
| 3723 | default: return 0; |
| 3724 | case Instruction::And: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3725 | if (Context->getConstantExprAnd(N, Mask) == Mask) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3726 | // If the AndRHS is a power of two minus one (0+1+), this is simple. |
| 3727 | if ((Mask->getValue().countLeadingZeros() + |
| 3728 | Mask->getValue().countPopulation()) == |
| 3729 | Mask->getValue().getBitWidth()) |
| 3730 | break; |
| 3731 | |
| 3732 | // Otherwise, if Mask is 0+1+0+, and if B is known to have the low 0+ |
| 3733 | // part, we don't need any explicit masks to take them out of A. If that |
| 3734 | // is all N is, ignore it. |
| 3735 | uint32_t MB = 0, ME = 0; |
| 3736 | if (isRunOfOnes(Mask, MB, ME)) { // begin/end bit of run, inclusive |
| 3737 | uint32_t BitWidth = cast<IntegerType>(RHS->getType())->getBitWidth(); |
| 3738 | APInt Mask(APInt::getLowBitsSet(BitWidth, MB-1)); |
| 3739 | if (MaskedValueIsZero(RHS, Mask)) |
| 3740 | break; |
| 3741 | } |
| 3742 | } |
| 3743 | return 0; |
| 3744 | case Instruction::Or: |
| 3745 | case Instruction::Xor: |
| 3746 | // If the AndRHS is a power of two minus one (0+1+), and N&Mask == 0 |
| 3747 | if ((Mask->getValue().countLeadingZeros() + |
| 3748 | Mask->getValue().countPopulation()) == Mask->getValue().getBitWidth() |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3749 | && Context->getConstantExprAnd(N, Mask)->isNullValue()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3750 | break; |
| 3751 | return 0; |
| 3752 | } |
| 3753 | |
| 3754 | Instruction *New; |
| 3755 | if (isSub) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3756 | New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3757 | else |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3758 | New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 3759 | return InsertNewInstBefore(New, I); |
| 3760 | } |
| 3761 | |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3762 | /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible. |
| 3763 | Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, |
| 3764 | ICmpInst *LHS, ICmpInst *RHS) { |
| Chris Lattner | f380348 | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3765 | Value *Val, *Val2; |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3766 | ConstantInt *LHSCst, *RHSCst; |
| 3767 | ICmpInst::Predicate LHSCC, RHSCC; |
| 3768 | |
| Chris Lattner | f380348 | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3769 | // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 3770 | if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), |
| 3771 | m_ConstantInt(LHSCst)), *Context) || |
| 3772 | !match(RHS, m_ICmp(RHSCC, m_Value(Val2), |
| 3773 | m_ConstantInt(RHSCst)), *Context)) |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3774 | return 0; |
| Chris Lattner | f380348 | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3775 | |
| 3776 | // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) |
| 3777 | // where C is a power of 2 |
| 3778 | if (LHSCst == RHSCst && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT && |
| 3779 | LHSCst->getValue().isPowerOf2()) { |
| 3780 | Instruction *NewOr = BinaryOperator::CreateOr(Val, Val2); |
| 3781 | InsertNewInstBefore(NewOr, I); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3782 | return new ICmpInst(*Context, LHSCC, NewOr, LHSCst); |
| Chris Lattner | f380348 | 2008-11-16 05:10:52 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | // From here on, we only handle: |
| 3786 | // (icmp1 A, C1) & (icmp2 A, C2) --> something simpler. |
| 3787 | if (Val != Val2) return 0; |
| 3788 | |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3789 | // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. |
| 3790 | if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || |
| 3791 | RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || |
| 3792 | LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || |
| 3793 | RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) |
| 3794 | return 0; |
| 3795 | |
| 3796 | // We can't fold (ugt x, C) & (sgt x, C2). |
| 3797 | if (!PredicatesFoldable(LHSCC, RHSCC)) |
| 3798 | return 0; |
| 3799 | |
| 3800 | // Ensure that the larger constant is on the RHS. |
| Chris Lattner | 665298f | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 3801 | bool ShouldSwap; |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3802 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 3803 | (ICmpInst::isEquality(LHSCC) && |
| 3804 | ICmpInst::isSignedPredicate(RHSCC))) |
| Chris Lattner | 665298f | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 3805 | ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3806 | else |
| Chris Lattner | 665298f | 2008-11-16 05:14:43 +0000 | [diff] [blame] | 3807 | ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
| 3808 | |
| 3809 | if (ShouldSwap) { |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3810 | std::swap(LHS, RHS); |
| 3811 | std::swap(LHSCst, RHSCst); |
| 3812 | std::swap(LHSCC, RHSCC); |
| 3813 | } |
| 3814 | |
| 3815 | // At this point, we know we have have two icmp instructions |
| 3816 | // comparing a value against two constants and and'ing the result |
| 3817 | // together. Because of the above check, we know that we only have |
| 3818 | // icmp eq, icmp ne, icmp [su]lt, and icmp [SU]gt here. We also know |
| 3819 | // (from the FoldICmpLogical check above), that the two constants |
| 3820 | // are not equal and that the larger constant is on the RHS |
| 3821 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 3822 | |
| 3823 | switch (LHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3824 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3825 | case ICmpInst::ICMP_EQ: |
| 3826 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3827 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3828 | case ICmpInst::ICMP_EQ: // (X == 13 & X == 15) -> false |
| 3829 | case ICmpInst::ICMP_UGT: // (X == 13 & X > 15) -> false |
| 3830 | case ICmpInst::ICMP_SGT: // (X == 13 & X > 15) -> false |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 3831 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3832 | case ICmpInst::ICMP_NE: // (X == 13 & X != 15) -> X == 13 |
| 3833 | case ICmpInst::ICMP_ULT: // (X == 13 & X < 15) -> X == 13 |
| 3834 | case ICmpInst::ICMP_SLT: // (X == 13 & X < 15) -> X == 13 |
| 3835 | return ReplaceInstUsesWith(I, LHS); |
| 3836 | } |
| 3837 | case ICmpInst::ICMP_NE: |
| 3838 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3839 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3840 | case ICmpInst::ICMP_ULT: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3841 | if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X u< 14) -> X < 13 |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3842 | return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Val, LHSCst); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3843 | break; // (X != 13 & X u< 15) -> no change |
| 3844 | case ICmpInst::ICMP_SLT: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3845 | if (LHSCst == SubOne(RHSCst, Context)) // (X != 13 & X s< 14) -> X < 13 |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3846 | return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Val, LHSCst); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3847 | break; // (X != 13 & X s< 15) -> no change |
| 3848 | case ICmpInst::ICMP_EQ: // (X != 13 & X == 15) -> X == 15 |
| 3849 | case ICmpInst::ICMP_UGT: // (X != 13 & X u> 15) -> X u> 15 |
| 3850 | case ICmpInst::ICMP_SGT: // (X != 13 & X s> 15) -> X s> 15 |
| 3851 | return ReplaceInstUsesWith(I, RHS); |
| 3852 | case ICmpInst::ICMP_NE: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3853 | if (LHSCst == SubOne(RHSCst, Context)){// (X != 13 & X != 14) -> X-13 >u 1 |
| 3854 | Constant *AddCST = Context->getConstantExprNeg(LHSCst); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3855 | Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST, |
| 3856 | Val->getName()+".off"); |
| 3857 | InsertNewInstBefore(Add, I); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3858 | return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3859 | Context->getConstantInt(Add->getType(), 1)); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3860 | } |
| 3861 | break; // (X != 13 & X != 15) -> no change |
| 3862 | } |
| 3863 | break; |
| 3864 | case ICmpInst::ICMP_ULT: |
| 3865 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3866 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3867 | case ICmpInst::ICMP_EQ: // (X u< 13 & X == 15) -> false |
| 3868 | case ICmpInst::ICMP_UGT: // (X u< 13 & X u> 15) -> false |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 3869 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3870 | case ICmpInst::ICMP_SGT: // (X u< 13 & X s> 15) -> no change |
| 3871 | break; |
| 3872 | case ICmpInst::ICMP_NE: // (X u< 13 & X != 15) -> X u< 13 |
| 3873 | case ICmpInst::ICMP_ULT: // (X u< 13 & X u< 15) -> X u< 13 |
| 3874 | return ReplaceInstUsesWith(I, LHS); |
| 3875 | case ICmpInst::ICMP_SLT: // (X u< 13 & X s< 15) -> no change |
| 3876 | break; |
| 3877 | } |
| 3878 | break; |
| 3879 | case ICmpInst::ICMP_SLT: |
| 3880 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3881 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3882 | case ICmpInst::ICMP_EQ: // (X s< 13 & X == 15) -> false |
| 3883 | case ICmpInst::ICMP_SGT: // (X s< 13 & X s> 15) -> false |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 3884 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3885 | case ICmpInst::ICMP_UGT: // (X s< 13 & X u> 15) -> no change |
| 3886 | break; |
| 3887 | case ICmpInst::ICMP_NE: // (X s< 13 & X != 15) -> X < 13 |
| 3888 | case ICmpInst::ICMP_SLT: // (X s< 13 & X s< 15) -> X < 13 |
| 3889 | return ReplaceInstUsesWith(I, LHS); |
| 3890 | case ICmpInst::ICMP_ULT: // (X s< 13 & X u< 15) -> no change |
| 3891 | break; |
| 3892 | } |
| 3893 | break; |
| 3894 | case ICmpInst::ICMP_UGT: |
| 3895 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3896 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3897 | case ICmpInst::ICMP_EQ: // (X u> 13 & X == 15) -> X == 15 |
| 3898 | case ICmpInst::ICMP_UGT: // (X u> 13 & X u> 15) -> X u> 15 |
| 3899 | return ReplaceInstUsesWith(I, RHS); |
| 3900 | case ICmpInst::ICMP_SGT: // (X u> 13 & X s> 15) -> no change |
| 3901 | break; |
| 3902 | case ICmpInst::ICMP_NE: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3903 | if (RHSCst == AddOne(LHSCst, Context)) // (X u> 13 & X != 14) -> X u> 14 |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3904 | return new ICmpInst(*Context, LHSCC, Val, RHSCst); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3905 | break; // (X u> 13 & X != 15) -> no change |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3906 | case ICmpInst::ICMP_ULT: // (X u> 13 & X u< 15) -> (X-14) <u 1 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3907 | return InsertRangeTest(Val, AddOne(LHSCst, Context), |
| 3908 | RHSCst, false, true, I); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3909 | case ICmpInst::ICMP_SLT: // (X u> 13 & X s< 15) -> no change |
| 3910 | break; |
| 3911 | } |
| 3912 | break; |
| 3913 | case ICmpInst::ICMP_SGT: |
| 3914 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3915 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3916 | case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15 |
| 3917 | case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15 |
| 3918 | return ReplaceInstUsesWith(I, RHS); |
| 3919 | case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change |
| 3920 | break; |
| 3921 | case ICmpInst::ICMP_NE: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3922 | if (RHSCst == AddOne(LHSCst, Context)) // (X s> 13 & X != 14) -> X s> 14 |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3923 | return new ICmpInst(*Context, LHSCC, Val, RHSCst); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3924 | break; // (X s> 13 & X != 15) -> no change |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 3925 | case ICmpInst::ICMP_SLT: // (X s> 13 & X s< 15) -> (X-14) s< 1 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 3926 | return InsertRangeTest(Val, AddOne(LHSCst, Context), |
| 3927 | RHSCst, true, true, I); |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3928 | case ICmpInst::ICMP_ULT: // (X s> 13 & X u< 15) -> no change |
| 3929 | break; |
| 3930 | } |
| 3931 | break; |
| 3932 | } |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 3933 | |
| 3934 | return 0; |
| 3935 | } |
| 3936 | |
| Chris Lattner | 93a359a | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 3937 | Instruction *InstCombiner::FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, |
| 3938 | FCmpInst *RHS) { |
| 3939 | |
| 3940 | if (LHS->getPredicate() == FCmpInst::FCMP_ORD && |
| 3941 | RHS->getPredicate() == FCmpInst::FCMP_ORD) { |
| 3942 | // (fcmp ord x, c) & (fcmp ord y, c) -> (fcmp ord x, y) |
| 3943 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 3944 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 3945 | // If either of the constants are nans, then the whole thing returns |
| 3946 | // false. |
| 3947 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
| 3948 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| 3949 | return new FCmpInst(*Context, FCmpInst::FCMP_ORD, |
| 3950 | LHS->getOperand(0), RHS->getOperand(0)); |
| 3951 | } |
| Chris Lattner | cf37355 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 3952 | |
| 3953 | // Handle vector zeros. This occurs because the canonical form of |
| 3954 | // "fcmp ord x,x" is "fcmp ord x, 0". |
| 3955 | if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && |
| 3956 | isa<ConstantAggregateZero>(RHS->getOperand(1))) |
| 3957 | return new FCmpInst(*Context, FCmpInst::FCMP_ORD, |
| 3958 | LHS->getOperand(0), RHS->getOperand(0)); |
| Chris Lattner | 93a359a | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 3959 | return 0; |
| 3960 | } |
| 3961 | |
| 3962 | Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); |
| 3963 | Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); |
| 3964 | FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); |
| 3965 | |
| 3966 | |
| 3967 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 3968 | // Swap RHS operands to match LHS. |
| 3969 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 3970 | std::swap(Op1LHS, Op1RHS); |
| 3971 | } |
| 3972 | |
| 3973 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { |
| 3974 | // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y). |
| 3975 | if (Op0CC == Op1CC) |
| 3976 | return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC, Op0LHS, Op0RHS); |
| 3977 | |
| 3978 | if (Op0CC == FCmpInst::FCMP_FALSE || Op1CC == FCmpInst::FCMP_FALSE) |
| 3979 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| 3980 | if (Op0CC == FCmpInst::FCMP_TRUE) |
| 3981 | return ReplaceInstUsesWith(I, RHS); |
| 3982 | if (Op1CC == FCmpInst::FCMP_TRUE) |
| 3983 | return ReplaceInstUsesWith(I, LHS); |
| 3984 | |
| 3985 | bool Op0Ordered; |
| 3986 | bool Op1Ordered; |
| 3987 | unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); |
| 3988 | unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); |
| 3989 | if (Op1Pred == 0) { |
| 3990 | std::swap(LHS, RHS); |
| 3991 | std::swap(Op0Pred, Op1Pred); |
| 3992 | std::swap(Op0Ordered, Op1Ordered); |
| 3993 | } |
| 3994 | if (Op0Pred == 0) { |
| 3995 | // uno && ueq -> uno && (uno || eq) -> ueq |
| 3996 | // ord && olt -> ord && (ord && lt) -> olt |
| 3997 | if (Op0Ordered == Op1Ordered) |
| 3998 | return ReplaceInstUsesWith(I, RHS); |
| 3999 | |
| 4000 | // uno && oeq -> uno && (ord && eq) -> false |
| 4001 | // uno && ord -> false |
| 4002 | if (!Op0Ordered) |
| 4003 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| 4004 | // ord && ueq -> ord && (uno || eq) -> oeq |
| 4005 | return cast<Instruction>(getFCmpValue(true, Op1Pred, |
| 4006 | Op0LHS, Op0RHS, Context)); |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 4013 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4014 | Instruction *InstCombiner::visitAnd(BinaryOperator &I) { |
| 4015 | bool Changed = SimplifyCommutative(I); |
| 4016 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4017 | |
| 4018 | if (isa<UndefValue>(Op1)) // X & undef -> 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4019 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4020 | |
| 4021 | // and X, X = X |
| 4022 | if (Op0 == Op1) |
| 4023 | return ReplaceInstUsesWith(I, Op1); |
| 4024 | |
| 4025 | // See if we can simplify any instructions used by the instruction whose sole |
| 4026 | // purpose is to compute bits we don't care about. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4027 | if (SimplifyDemandedInstructionBits(I)) |
| 4028 | return &I; |
| 4029 | if (isa<VectorType>(I.getType())) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4030 | if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 4031 | if (CP->isAllOnesValue()) // X & <-1,-1> -> X |
| 4032 | return ReplaceInstUsesWith(I, I.getOperand(0)); |
| 4033 | } else if (isa<ConstantAggregateZero>(Op1)) { |
| 4034 | return ReplaceInstUsesWith(I, Op1); // X & <0,0> -> <0,0> |
| 4035 | } |
| 4036 | } |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4037 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4038 | if (ConstantInt *AndRHS = dyn_cast<ConstantInt>(Op1)) { |
| 4039 | const APInt& AndRHSMask = AndRHS->getValue(); |
| 4040 | APInt NotAndRHS(~AndRHSMask); |
| 4041 | |
| 4042 | // Optimize a variety of ((val OP C1) & C2) combinations... |
| 4043 | if (isa<BinaryOperator>(Op0)) { |
| 4044 | Instruction *Op0I = cast<Instruction>(Op0); |
| 4045 | Value *Op0LHS = Op0I->getOperand(0); |
| 4046 | Value *Op0RHS = Op0I->getOperand(1); |
| 4047 | switch (Op0I->getOpcode()) { |
| 4048 | case Instruction::Xor: |
| 4049 | case Instruction::Or: |
| 4050 | // If the mask is only needed on one incoming arm, push it up. |
| 4051 | if (Op0I->hasOneUse()) { |
| 4052 | if (MaskedValueIsZero(Op0LHS, NotAndRHS)) { |
| 4053 | // Not masking anything out for the LHS, move to RHS. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4054 | Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4055 | Op0RHS->getName()+".masked"); |
| 4056 | InsertNewInstBefore(NewRHS, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4057 | return BinaryOperator::Create( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4058 | cast<BinaryOperator>(Op0I)->getOpcode(), Op0LHS, NewRHS); |
| 4059 | } |
| 4060 | if (!isa<Constant>(Op0RHS) && |
| 4061 | MaskedValueIsZero(Op0RHS, NotAndRHS)) { |
| 4062 | // Not masking anything out for the RHS, move to LHS. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4063 | Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4064 | Op0LHS->getName()+".masked"); |
| 4065 | InsertNewInstBefore(NewLHS, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4066 | return BinaryOperator::Create( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4067 | cast<BinaryOperator>(Op0I)->getOpcode(), NewLHS, Op0RHS); |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | break; |
| 4072 | case Instruction::Add: |
| 4073 | // ((A & N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == AndRHS. |
| 4074 | // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 4075 | // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0 |
| 4076 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4077 | return BinaryOperator::CreateAnd(V, AndRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4078 | if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4079 | return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4080 | break; |
| 4081 | |
| 4082 | case Instruction::Sub: |
| 4083 | // ((A & N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == AndRHS. |
| 4084 | // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 4085 | // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0 |
| 4086 | if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4087 | return BinaryOperator::CreateAnd(V, AndRHS); |
| Nick Lewycky | ffed71b | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 4088 | |
| Nick Lewycky | a349ba4 | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 4089 | // (A - N) & AndRHS -> -N & AndRHS iff A&AndRHS==0 and AndRHS |
| 4090 | // has 1's for all bits that the subtraction with A might affect. |
| 4091 | if (Op0I->hasOneUse()) { |
| 4092 | uint32_t BitWidth = AndRHSMask.getBitWidth(); |
| 4093 | uint32_t Zeros = AndRHSMask.countLeadingZeros(); |
| 4094 | APInt Mask = APInt::getLowBitsSet(BitWidth, BitWidth - Zeros); |
| 4095 | |
| Nick Lewycky | ffed71b | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 4096 | ConstantInt *A = dyn_cast<ConstantInt>(Op0LHS); |
| Nick Lewycky | a349ba4 | 2008-07-10 05:51:40 +0000 | [diff] [blame] | 4097 | if (!(A && A->isZero()) && // avoid infinite recursion. |
| 4098 | MaskedValueIsZero(Op0LHS, Mask)) { |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 4099 | Instruction *NewNeg = BinaryOperator::CreateNeg(*Context, Op0RHS); |
| Nick Lewycky | ffed71b | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 4100 | InsertNewInstBefore(NewNeg, I); |
| 4101 | return BinaryOperator::CreateAnd(NewNeg, AndRHS); |
| 4102 | } |
| 4103 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4104 | break; |
| Nick Lewycky | 659ed4d | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 4105 | |
| 4106 | case Instruction::Shl: |
| 4107 | case Instruction::LShr: |
| 4108 | // (1 << x) & 1 --> zext(x == 0) |
| 4109 | // (1 >> x) & 1 --> zext(x == 0) |
| Nick Lewycky | f1b1222 | 2008-07-09 07:35:26 +0000 | [diff] [blame] | 4110 | if (AndRHSMask == 1 && Op0LHS == AndRHS) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4111 | Instruction *NewICmp = new ICmpInst(*Context, ICmpInst::ICMP_EQ, |
| 4112 | Op0RHS, Context->getNullValue(I.getType())); |
| Nick Lewycky | 659ed4d | 2008-07-09 05:20:13 +0000 | [diff] [blame] | 4113 | InsertNewInstBefore(NewICmp, I); |
| 4114 | return new ZExtInst(NewICmp, I.getType()); |
| 4115 | } |
| 4116 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) |
| 4120 | if (Instruction *Res = OptAndOp(Op0I, Op0CI, AndRHS, I)) |
| 4121 | return Res; |
| 4122 | } else if (CastInst *CI = dyn_cast<CastInst>(Op0)) { |
| 4123 | // If this is an integer truncation or change from signed-to-unsigned, and |
| 4124 | // if the source is an and/or with immediate, transform it. This |
| 4125 | // frequently occurs for bitfield accesses. |
| 4126 | if (Instruction *CastOp = dyn_cast<Instruction>(CI->getOperand(0))) { |
| 4127 | if ((isa<TruncInst>(CI) || isa<BitCastInst>(CI)) && |
| 4128 | CastOp->getNumOperands() == 2) |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4129 | if (ConstantInt *AndCI = dyn_cast<ConstantInt>(CastOp->getOperand(1))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4130 | if (CastOp->getOpcode() == Instruction::And) { |
| 4131 | // Change: and (cast (and X, C1) to T), C2 |
| 4132 | // into : and (cast X to T), trunc_or_bitcast(C1)&C2 |
| 4133 | // This will fold the two constants together, which may allow |
| 4134 | // other simplifications. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4135 | Instruction *NewCast = CastInst::CreateTruncOrBitCast( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4136 | CastOp->getOperand(0), I.getType(), |
| 4137 | CastOp->getName()+".shrunk"); |
| 4138 | NewCast = InsertNewInstBefore(NewCast, I); |
| 4139 | // trunc_or_bitcast(C1)&C2 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4140 | Constant *C3 = |
| 4141 | Context->getConstantExprTruncOrBitCast(AndCI,I.getType()); |
| 4142 | C3 = Context->getConstantExprAnd(C3, AndRHS); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4143 | return BinaryOperator::CreateAnd(NewCast, C3); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4144 | } else if (CastOp->getOpcode() == Instruction::Or) { |
| 4145 | // Change: and (cast (or X, C1) to T), C2 |
| 4146 | // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4147 | Constant *C3 = |
| 4148 | Context->getConstantExprTruncOrBitCast(AndCI,I.getType()); |
| 4149 | if (Context->getConstantExprAnd(C3, AndRHS) == AndRHS) |
| 4150 | // trunc(C1)&C2 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4151 | return ReplaceInstUsesWith(I, AndRHS); |
| 4152 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 4153 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | // Try to fold constant and into select arguments. |
| 4158 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 4159 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 4160 | return R; |
| 4161 | if (isa<PHINode>(Op0)) |
| 4162 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4163 | return NV; |
| 4164 | } |
| 4165 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4166 | Value *Op0NotVal = dyn_castNotVal(Op0, Context); |
| 4167 | Value *Op1NotVal = dyn_castNotVal(Op1, Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4168 | |
| 4169 | if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4170 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4171 | |
| 4172 | // (~A & ~B) == (~(A | B)) - De Morgan's Law |
| 4173 | if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4174 | Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4175 | I.getName()+".demorgan"); |
| 4176 | InsertNewInstBefore(Or, I); |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 4177 | return BinaryOperator::CreateNot(*Context, Or); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | { |
| 4181 | Value *A = 0, *B = 0, *C = 0, *D = 0; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4182 | if (match(Op0, m_Or(m_Value(A), m_Value(B)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4183 | if (A == Op1 || B == Op1) // (A | ?) & A --> A |
| 4184 | return ReplaceInstUsesWith(I, Op1); |
| 4185 | |
| 4186 | // (A|B) & ~(A&B) -> A^B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4187 | if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4188 | if ((A == C && B == D) || (A == D && B == C)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4189 | return BinaryOperator::CreateXor(A, B); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4190 | } |
| 4191 | } |
| 4192 | |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4193 | if (match(Op1, m_Or(m_Value(A), m_Value(B)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4194 | if (A == Op0 || B == Op0) // A & (A | ?) --> A |
| 4195 | return ReplaceInstUsesWith(I, Op0); |
| 4196 | |
| 4197 | // ~(A&B) & (A|B) -> A^B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4198 | if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4199 | if ((A == C && B == D) || (A == D && B == C)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4200 | return BinaryOperator::CreateXor(A, B); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | |
| 4204 | if (Op0->hasOneUse() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4205 | match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4206 | if (A == Op1) { // (A^B)&A -> A&(A^B) |
| 4207 | I.swapOperands(); // Simplify below |
| 4208 | std::swap(Op0, Op1); |
| 4209 | } else if (B == Op1) { // (A^B)&B -> B&(B^A) |
| 4210 | cast<BinaryOperator>(Op0)->swapOperands(); |
| 4211 | I.swapOperands(); // Simplify below |
| 4212 | std::swap(Op0, Op1); |
| 4213 | } |
| 4214 | } |
| Bill Wendling | ce5e0af | 2008-11-30 13:08:13 +0000 | [diff] [blame] | 4215 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4216 | if (Op1->hasOneUse() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4217 | match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4218 | if (B == Op0) { // B&(A^B) -> B&(B^A) |
| 4219 | cast<BinaryOperator>(Op1)->swapOperands(); |
| 4220 | std::swap(A, B); |
| 4221 | } |
| 4222 | if (A == Op0) { // A&(A^B) -> A & ~B |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 4223 | Instruction *NotB = BinaryOperator::CreateNot(*Context, B, "tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4224 | InsertNewInstBefore(NotB, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4225 | return BinaryOperator::CreateAnd(A, NotB); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4226 | } |
| 4227 | } |
| Bill Wendling | ce5e0af | 2008-11-30 13:08:13 +0000 | [diff] [blame] | 4228 | |
| 4229 | // (A&((~A)|B)) -> A&B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4230 | if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A)), *Context) || |
| 4231 | match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))), *Context)) |
| Chris Lattner | 9db479f | 2008-12-01 05:16:26 +0000 | [diff] [blame] | 4232 | return BinaryOperator::CreateAnd(A, Op1); |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4233 | if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A)), *Context) || |
| 4234 | match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))), *Context)) |
| Chris Lattner | 9db479f | 2008-12-01 05:16:26 +0000 | [diff] [blame] | 4235 | return BinaryOperator::CreateAnd(A, Op0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(Op1)) { |
| 4239 | // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4240 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4241 | return R; |
| 4242 | |
| Chris Lattner | 0631ea7 | 2008-11-16 05:06:21 +0000 | [diff] [blame] | 4243 | if (ICmpInst *LHS = dyn_cast<ICmpInst>(Op0)) |
| 4244 | if (Instruction *Res = FoldAndOfICmps(I, LHS, RHS)) |
| 4245 | return Res; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | // fold (and (cast A), (cast B)) -> (cast (and A, B)) |
| 4249 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) |
| 4250 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 4251 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind ? |
| 4252 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| Chris Lattner | cf37355 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 4253 | if (SrcTy == Op1C->getOperand(0)->getType() && |
| 4254 | SrcTy->isIntOrIntVector() && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4255 | // Only do this if the casts both really cause code to be generated. |
| 4256 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4257 | I.getType(), TD) && |
| 4258 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4259 | I.getType(), TD)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4260 | Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4261 | Op1C->getOperand(0), |
| 4262 | I.getName()); |
| 4263 | InsertNewInstBefore(NewOp, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4264 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | // (X >> Z) & (Y >> Z) -> (X&Y) >> Z for all shifts. |
| 4269 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4270 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4271 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
| 4272 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4273 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4274 | Instruction *NewOp = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4275 | InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4276 | SI1->getOperand(0), |
| 4277 | SI0->getName()), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4278 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4279 | SI1->getOperand(1)); |
| 4280 | } |
| 4281 | } |
| 4282 | |
| Evan Cheng | 0ac3a4d | 2008-10-14 17:15:11 +0000 | [diff] [blame] | 4283 | // If and'ing two fcmp, try combine them into one. |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4284 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| Chris Lattner | 93a359a | 2009-07-23 05:14:02 +0000 | [diff] [blame] | 4285 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
| 4286 | if (Instruction *Res = FoldAndOfFCmps(I, LHS, RHS)) |
| 4287 | return Res; |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4288 | } |
| Nick Lewycky | ffed71b | 2008-07-09 04:32:37 +0000 | [diff] [blame] | 4289 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4290 | return Changed ? &I : 0; |
| 4291 | } |
| 4292 | |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4293 | /// CollectBSwapParts - Analyze the specified subexpression and see if it is |
| 4294 | /// capable of providing pieces of a bswap. The subexpression provides pieces |
| 4295 | /// of a bswap if it is proven that each of the non-zero bytes in the output of |
| 4296 | /// the expression came from the corresponding "byte swapped" byte in some other |
| 4297 | /// value. For example, if the current subexpression is "(shl i32 %X, 24)" then |
| 4298 | /// we know that the expression deposits the low byte of %X into the high byte |
| 4299 | /// of the bswap result and that all other bytes are zero. This expression is |
| 4300 | /// accepted, the high byte of ByteValues is set to X to indicate a correct |
| 4301 | /// match. |
| 4302 | /// |
| 4303 | /// This function returns true if the match was unsuccessful and false if so. |
| 4304 | /// On entry to the function the "OverallLeftShift" is a signed integer value |
| 4305 | /// indicating the number of bytes that the subexpression is later shifted. For |
| 4306 | /// example, if the expression is later right shifted by 16 bits, the |
| 4307 | /// OverallLeftShift value would be -2 on entry. This is used to specify which |
| 4308 | /// byte of ByteValues is actually being set. |
| 4309 | /// |
| 4310 | /// Similarly, ByteMask is a bitmask where a bit is clear if its corresponding |
| 4311 | /// byte is masked to zero by a user. For example, in (X & 255), X will be |
| 4312 | /// processed with a bytemask of 1. Because bytemask is 32-bits, this limits |
| 4313 | /// this function to working on up to 32-byte (256 bit) values. ByteMask is |
| 4314 | /// always in the local (OverallLeftShift) coordinate space. |
| 4315 | /// |
| 4316 | static bool CollectBSwapParts(Value *V, int OverallLeftShift, uint32_t ByteMask, |
| 4317 | SmallVector<Value*, 8> &ByteValues) { |
| 4318 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 4319 | // If this is an or instruction, it may be an inner node of the bswap. |
| 4320 | if (I->getOpcode() == Instruction::Or) { |
| 4321 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 4322 | ByteValues) || |
| 4323 | CollectBSwapParts(I->getOperand(1), OverallLeftShift, ByteMask, |
| 4324 | ByteValues); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4325 | } |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4326 | |
| 4327 | // If this is a logical shift by a constant multiple of 8, recurse with |
| 4328 | // OverallLeftShift and ByteMask adjusted. |
| 4329 | if (I->isLogicalShift() && isa<ConstantInt>(I->getOperand(1))) { |
| 4330 | unsigned ShAmt = |
| 4331 | cast<ConstantInt>(I->getOperand(1))->getLimitedValue(~0U); |
| 4332 | // Ensure the shift amount is defined and of a byte value. |
| 4333 | if ((ShAmt & 7) || (ShAmt > 8*ByteValues.size())) |
| 4334 | return true; |
| 4335 | |
| 4336 | unsigned ByteShift = ShAmt >> 3; |
| 4337 | if (I->getOpcode() == Instruction::Shl) { |
| 4338 | // X << 2 -> collect(X, +2) |
| 4339 | OverallLeftShift += ByteShift; |
| 4340 | ByteMask >>= ByteShift; |
| 4341 | } else { |
| 4342 | // X >>u 2 -> collect(X, -2) |
| 4343 | OverallLeftShift -= ByteShift; |
| 4344 | ByteMask <<= ByteShift; |
| Chris Lattner | 4444859 | 2008-10-08 06:42:28 +0000 | [diff] [blame] | 4345 | ByteMask &= (~0U >> (32-ByteValues.size())); |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
| 4348 | if (OverallLeftShift >= (int)ByteValues.size()) return true; |
| 4349 | if (OverallLeftShift <= -(int)ByteValues.size()) return true; |
| 4350 | |
| 4351 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 4352 | ByteValues); |
| 4353 | } |
| 4354 | |
| 4355 | // If this is a logical 'and' with a mask that clears bytes, clear the |
| 4356 | // corresponding bytes in ByteMask. |
| 4357 | if (I->getOpcode() == Instruction::And && |
| 4358 | isa<ConstantInt>(I->getOperand(1))) { |
| 4359 | // Scan every byte of the and mask, seeing if the byte is either 0 or 255. |
| 4360 | unsigned NumBytes = ByteValues.size(); |
| 4361 | APInt Byte(I->getType()->getPrimitiveSizeInBits(), 255); |
| 4362 | const APInt &AndMask = cast<ConstantInt>(I->getOperand(1))->getValue(); |
| 4363 | |
| 4364 | for (unsigned i = 0; i != NumBytes; ++i, Byte <<= 8) { |
| 4365 | // If this byte is masked out by a later operation, we don't care what |
| 4366 | // the and mask is. |
| 4367 | if ((ByteMask & (1 << i)) == 0) |
| 4368 | continue; |
| 4369 | |
| 4370 | // If the AndMask is all zeros for this byte, clear the bit. |
| 4371 | APInt MaskB = AndMask & Byte; |
| 4372 | if (MaskB == 0) { |
| 4373 | ByteMask &= ~(1U << i); |
| 4374 | continue; |
| 4375 | } |
| 4376 | |
| 4377 | // If the AndMask is not all ones for this byte, it's not a bytezap. |
| 4378 | if (MaskB != Byte) |
| 4379 | return true; |
| 4380 | |
| 4381 | // Otherwise, this byte is kept. |
| 4382 | } |
| 4383 | |
| 4384 | return CollectBSwapParts(I->getOperand(0), OverallLeftShift, ByteMask, |
| 4385 | ByteValues); |
| 4386 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4387 | } |
| 4388 | |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4389 | // Okay, we got to something that isn't a shift, 'or' or 'and'. This must be |
| 4390 | // the input value to the bswap. Some observations: 1) if more than one byte |
| 4391 | // is demanded from this input, then it could not be successfully assembled |
| 4392 | // into a byteswap. At least one of the two bytes would not be aligned with |
| 4393 | // their ultimate destination. |
| 4394 | if (!isPowerOf2_32(ByteMask)) return true; |
| 4395 | unsigned InputByteNo = CountTrailingZeros_32(ByteMask); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4396 | |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4397 | // 2) The input and ultimate destinations must line up: if byte 3 of an i32 |
| 4398 | // is demanded, it needs to go into byte 0 of the result. This means that the |
| 4399 | // byte needs to be shifted until it lands in the right byte bucket. The |
| 4400 | // shift amount depends on the position: if the byte is coming from the high |
| 4401 | // part of the value (e.g. byte 3) then it must be shifted right. If from the |
| 4402 | // low part, it must be shifted left. |
| 4403 | unsigned DestByteNo = InputByteNo + OverallLeftShift; |
| 4404 | if (InputByteNo < ByteValues.size()/2) { |
| 4405 | if (ByteValues.size()-1-DestByteNo != InputByteNo) |
| 4406 | return true; |
| 4407 | } else { |
| 4408 | if (ByteValues.size()-1-DestByteNo != InputByteNo) |
| 4409 | return true; |
| 4410 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4411 | |
| 4412 | // If the destination byte value is already defined, the values are or'd |
| 4413 | // together, which isn't a bswap (unless it's an or of the same bits). |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4414 | if (ByteValues[DestByteNo] && ByteValues[DestByteNo] != V) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4415 | return true; |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4416 | ByteValues[DestByteNo] = V; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4417 | return false; |
| 4418 | } |
| 4419 | |
| 4420 | /// MatchBSwap - Given an OR instruction, check to see if this is a bswap idiom. |
| 4421 | /// If so, insert the new bswap intrinsic and return it. |
| 4422 | Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { |
| 4423 | const IntegerType *ITy = dyn_cast<IntegerType>(I.getType()); |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4424 | if (!ITy || ITy->getBitWidth() % 16 || |
| 4425 | // ByteMask only allows up to 32-byte values. |
| 4426 | ITy->getBitWidth() > 32*8) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4427 | return 0; // Can only bswap pairs of bytes. Can't do vectors. |
| 4428 | |
| 4429 | /// ByteValues - For each byte of the result, we keep track of which value |
| 4430 | /// defines each byte. |
| 4431 | SmallVector<Value*, 8> ByteValues; |
| 4432 | ByteValues.resize(ITy->getBitWidth()/8); |
| 4433 | |
| 4434 | // Try to find all the pieces corresponding to the bswap. |
| Chris Lattner | 567f511 | 2008-10-05 02:13:19 +0000 | [diff] [blame] | 4435 | uint32_t ByteMask = ~0U >> (32-ByteValues.size()); |
| 4436 | if (CollectBSwapParts(&I, 0, ByteMask, ByteValues)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4437 | return 0; |
| 4438 | |
| 4439 | // Check to see if all of the bytes come from the same value. |
| 4440 | Value *V = ByteValues[0]; |
| 4441 | if (V == 0) return 0; // Didn't find a byte? Must be zero. |
| 4442 | |
| 4443 | // Check to make sure that all of the bytes come from the same value. |
| 4444 | for (unsigned i = 1, e = ByteValues.size(); i != e; ++i) |
| 4445 | if (ByteValues[i] != V) |
| 4446 | return 0; |
| Chandler Carruth | a228e39 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4447 | const Type *Tys[] = { ITy }; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4448 | Module *M = I.getParent()->getParent()->getParent(); |
| Chandler Carruth | a228e39 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 4449 | Function *F = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1); |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 4450 | return CallInst::Create(F, V); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4453 | /// MatchSelectFromAndOr - We have an expression of the form (A&C)|(B&D). Check |
| 4454 | /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then |
| 4455 | /// we can simplify this expression to "cond ? C : D or B". |
| 4456 | static Instruction *MatchSelectFromAndOr(Value *A, Value *B, |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4457 | Value *C, Value *D, |
| 4458 | LLVMContext *Context) { |
| Chris Lattner | d09b5ba | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4459 | // If A is not a select of -1/0, this cannot match. |
| Chris Lattner | 641ea46 | 2008-11-16 04:46:19 +0000 | [diff] [blame] | 4460 | Value *Cond = 0; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4461 | if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)), *Context)) |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4462 | return 0; |
| 4463 | |
| Chris Lattner | d09b5ba | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4464 | // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B. |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4465 | if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)), *Context)) |
| Chris Lattner | d09b5ba | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4466 | return SelectInst::Create(Cond, C, B); |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4467 | if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context)) |
| Chris Lattner | d09b5ba | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4468 | return SelectInst::Create(Cond, C, B); |
| 4469 | // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D. |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4470 | if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)), *Context)) |
| Chris Lattner | d09b5ba | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4471 | return SelectInst::Create(Cond, C, D); |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4472 | if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context)) |
| Chris Lattner | d09b5ba | 2008-11-16 04:26:55 +0000 | [diff] [blame] | 4473 | return SelectInst::Create(Cond, C, D); |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4474 | return 0; |
| 4475 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4476 | |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4477 | /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible. |
| 4478 | Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, |
| 4479 | ICmpInst *LHS, ICmpInst *RHS) { |
| 4480 | Value *Val, *Val2; |
| 4481 | ConstantInt *LHSCst, *RHSCst; |
| 4482 | ICmpInst::Predicate LHSCC, RHSCC; |
| 4483 | |
| 4484 | // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4485 | if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), |
| 4486 | m_ConstantInt(LHSCst)), *Context) || |
| 4487 | !match(RHS, m_ICmp(RHSCC, m_Value(Val2), |
| 4488 | m_ConstantInt(RHSCst)), *Context)) |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4489 | return 0; |
| 4490 | |
| 4491 | // From here on, we only handle: |
| 4492 | // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. |
| 4493 | if (Val != Val2) return 0; |
| 4494 | |
| 4495 | // ICMP_[US][GL]E X, CST is folded to ICMP_[US][GL]T elsewhere. |
| 4496 | if (LHSCC == ICmpInst::ICMP_UGE || LHSCC == ICmpInst::ICMP_ULE || |
| 4497 | RHSCC == ICmpInst::ICMP_UGE || RHSCC == ICmpInst::ICMP_ULE || |
| 4498 | LHSCC == ICmpInst::ICMP_SGE || LHSCC == ICmpInst::ICMP_SLE || |
| 4499 | RHSCC == ICmpInst::ICMP_SGE || RHSCC == ICmpInst::ICMP_SLE) |
| 4500 | return 0; |
| 4501 | |
| 4502 | // We can't fold (ugt x, C) | (sgt x, C2). |
| 4503 | if (!PredicatesFoldable(LHSCC, RHSCC)) |
| 4504 | return 0; |
| 4505 | |
| 4506 | // Ensure that the larger constant is on the RHS. |
| 4507 | bool ShouldSwap; |
| 4508 | if (ICmpInst::isSignedPredicate(LHSCC) || |
| 4509 | (ICmpInst::isEquality(LHSCC) && |
| 4510 | ICmpInst::isSignedPredicate(RHSCC))) |
| 4511 | ShouldSwap = LHSCst->getValue().sgt(RHSCst->getValue()); |
| 4512 | else |
| 4513 | ShouldSwap = LHSCst->getValue().ugt(RHSCst->getValue()); |
| 4514 | |
| 4515 | if (ShouldSwap) { |
| 4516 | std::swap(LHS, RHS); |
| 4517 | std::swap(LHSCst, RHSCst); |
| 4518 | std::swap(LHSCC, RHSCC); |
| 4519 | } |
| 4520 | |
| 4521 | // At this point, we know we have have two icmp instructions |
| 4522 | // comparing a value against two constants and or'ing the result |
| 4523 | // together. Because of the above check, we know that we only have |
| 4524 | // ICMP_EQ, ICMP_NE, ICMP_LT, and ICMP_GT here. We also know (from the |
| 4525 | // FoldICmpLogical check above), that the two constants are not |
| 4526 | // equal. |
| 4527 | assert(LHSCst != RHSCst && "Compares not folded above?"); |
| 4528 | |
| 4529 | switch (LHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4530 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4531 | case ICmpInst::ICMP_EQ: |
| 4532 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4533 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4534 | case ICmpInst::ICMP_EQ: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4535 | if (LHSCst == SubOne(RHSCst, Context)) { |
| 4536 | // (X == 13 | X == 14) -> X-13 <u 2 |
| 4537 | Constant *AddCST = Context->getConstantExprNeg(LHSCst); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4538 | Instruction *Add = BinaryOperator::CreateAdd(Val, AddCST, |
| 4539 | Val->getName()+".off"); |
| 4540 | InsertNewInstBefore(Add, I); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4541 | AddCST = Context->getConstantExprSub(AddOne(RHSCst, Context), LHSCst); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4542 | return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Add, AddCST); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4543 | } |
| 4544 | break; // (X == 13 | X == 15) -> no change |
| 4545 | case ICmpInst::ICMP_UGT: // (X == 13 | X u> 14) -> no change |
| 4546 | case ICmpInst::ICMP_SGT: // (X == 13 | X s> 14) -> no change |
| 4547 | break; |
| 4548 | case ICmpInst::ICMP_NE: // (X == 13 | X != 15) -> X != 15 |
| 4549 | case ICmpInst::ICMP_ULT: // (X == 13 | X u< 15) -> X u< 15 |
| 4550 | case ICmpInst::ICMP_SLT: // (X == 13 | X s< 15) -> X s< 15 |
| 4551 | return ReplaceInstUsesWith(I, RHS); |
| 4552 | } |
| 4553 | break; |
| 4554 | case ICmpInst::ICMP_NE: |
| 4555 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4556 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4557 | case ICmpInst::ICMP_EQ: // (X != 13 | X == 15) -> X != 13 |
| 4558 | case ICmpInst::ICMP_UGT: // (X != 13 | X u> 15) -> X != 13 |
| 4559 | case ICmpInst::ICMP_SGT: // (X != 13 | X s> 15) -> X != 13 |
| 4560 | return ReplaceInstUsesWith(I, LHS); |
| 4561 | case ICmpInst::ICMP_NE: // (X != 13 | X != 15) -> true |
| 4562 | case ICmpInst::ICMP_ULT: // (X != 13 | X u< 15) -> true |
| 4563 | case ICmpInst::ICMP_SLT: // (X != 13 | X s< 15) -> true |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 4564 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4565 | } |
| 4566 | break; |
| 4567 | case ICmpInst::ICMP_ULT: |
| 4568 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4569 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4570 | case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change |
| 4571 | break; |
| 4572 | case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) -> (X-13) u> 2 |
| 4573 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4574 | // this can cause overflow. |
| 4575 | if (RHSCst->isMaxValue(false)) |
| 4576 | return ReplaceInstUsesWith(I, LHS); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4577 | return InsertRangeTest(Val, LHSCst, AddOne(RHSCst, Context), |
| 4578 | false, false, I); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4579 | case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change |
| 4580 | break; |
| 4581 | case ICmpInst::ICMP_NE: // (X u< 13 | X != 15) -> X != 15 |
| 4582 | case ICmpInst::ICMP_ULT: // (X u< 13 | X u< 15) -> X u< 15 |
| 4583 | return ReplaceInstUsesWith(I, RHS); |
| 4584 | case ICmpInst::ICMP_SLT: // (X u< 13 | X s< 15) -> no change |
| 4585 | break; |
| 4586 | } |
| 4587 | break; |
| 4588 | case ICmpInst::ICMP_SLT: |
| 4589 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4590 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4591 | case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change |
| 4592 | break; |
| 4593 | case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) -> (X-13) s> 2 |
| 4594 | // If RHSCst is [us]MAXINT, it is always false. Not handling |
| 4595 | // this can cause overflow. |
| 4596 | if (RHSCst->isMaxValue(true)) |
| 4597 | return ReplaceInstUsesWith(I, LHS); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4598 | return InsertRangeTest(Val, LHSCst, AddOne(RHSCst, Context), |
| 4599 | true, false, I); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4600 | case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change |
| 4601 | break; |
| 4602 | case ICmpInst::ICMP_NE: // (X s< 13 | X != 15) -> X != 15 |
| 4603 | case ICmpInst::ICMP_SLT: // (X s< 13 | X s< 15) -> X s< 15 |
| 4604 | return ReplaceInstUsesWith(I, RHS); |
| 4605 | case ICmpInst::ICMP_ULT: // (X s< 13 | X u< 15) -> no change |
| 4606 | break; |
| 4607 | } |
| 4608 | break; |
| 4609 | case ICmpInst::ICMP_UGT: |
| 4610 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4611 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4612 | case ICmpInst::ICMP_EQ: // (X u> 13 | X == 15) -> X u> 13 |
| 4613 | case ICmpInst::ICMP_UGT: // (X u> 13 | X u> 15) -> X u> 13 |
| 4614 | return ReplaceInstUsesWith(I, LHS); |
| 4615 | case ICmpInst::ICMP_SGT: // (X u> 13 | X s> 15) -> no change |
| 4616 | break; |
| 4617 | case ICmpInst::ICMP_NE: // (X u> 13 | X != 15) -> true |
| 4618 | case ICmpInst::ICMP_ULT: // (X u> 13 | X u< 15) -> true |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 4619 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4620 | case ICmpInst::ICMP_SLT: // (X u> 13 | X s< 15) -> no change |
| 4621 | break; |
| 4622 | } |
| 4623 | break; |
| 4624 | case ICmpInst::ICMP_SGT: |
| 4625 | switch (RHSCC) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4626 | default: llvm_unreachable("Unknown integer condition code!"); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4627 | case ICmpInst::ICMP_EQ: // (X s> 13 | X == 15) -> X > 13 |
| 4628 | case ICmpInst::ICMP_SGT: // (X s> 13 | X s> 15) -> X > 13 |
| 4629 | return ReplaceInstUsesWith(I, LHS); |
| 4630 | case ICmpInst::ICMP_UGT: // (X s> 13 | X u> 15) -> no change |
| 4631 | break; |
| 4632 | case ICmpInst::ICMP_NE: // (X s> 13 | X != 15) -> true |
| 4633 | case ICmpInst::ICMP_SLT: // (X s> 13 | X s< 15) -> true |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 4634 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4635 | case ICmpInst::ICMP_ULT: // (X s> 13 | X u< 15) -> no change |
| 4636 | break; |
| 4637 | } |
| 4638 | break; |
| 4639 | } |
| 4640 | return 0; |
| 4641 | } |
| 4642 | |
| Chris Lattner | 57e66fa | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 4643 | Instruction *InstCombiner::FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, |
| 4644 | FCmpInst *RHS) { |
| 4645 | if (LHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4646 | RHS->getPredicate() == FCmpInst::FCMP_UNO && |
| 4647 | LHS->getOperand(0)->getType() == RHS->getOperand(0)->getType()) { |
| 4648 | if (ConstantFP *LHSC = dyn_cast<ConstantFP>(LHS->getOperand(1))) |
| 4649 | if (ConstantFP *RHSC = dyn_cast<ConstantFP>(RHS->getOperand(1))) { |
| 4650 | // If either of the constants are nans, then the whole thing returns |
| 4651 | // true. |
| 4652 | if (LHSC->getValueAPF().isNaN() || RHSC->getValueAPF().isNaN()) |
| 4653 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| 4654 | |
| 4655 | // Otherwise, no need to compare the two constants, compare the |
| 4656 | // rest. |
| 4657 | return new FCmpInst(*Context, FCmpInst::FCMP_UNO, |
| 4658 | LHS->getOperand(0), RHS->getOperand(0)); |
| 4659 | } |
| 4660 | |
| 4661 | // Handle vector zeros. This occurs because the canonical form of |
| 4662 | // "fcmp uno x,x" is "fcmp uno x, 0". |
| 4663 | if (isa<ConstantAggregateZero>(LHS->getOperand(1)) && |
| 4664 | isa<ConstantAggregateZero>(RHS->getOperand(1))) |
| 4665 | return new FCmpInst(*Context, FCmpInst::FCMP_UNO, |
| 4666 | LHS->getOperand(0), RHS->getOperand(0)); |
| 4667 | |
| 4668 | return 0; |
| 4669 | } |
| 4670 | |
| 4671 | Value *Op0LHS = LHS->getOperand(0), *Op0RHS = LHS->getOperand(1); |
| 4672 | Value *Op1LHS = RHS->getOperand(0), *Op1RHS = RHS->getOperand(1); |
| 4673 | FCmpInst::Predicate Op0CC = LHS->getPredicate(), Op1CC = RHS->getPredicate(); |
| 4674 | |
| 4675 | if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { |
| 4676 | // Swap RHS operands to match LHS. |
| 4677 | Op1CC = FCmpInst::getSwappedPredicate(Op1CC); |
| 4678 | std::swap(Op1LHS, Op1RHS); |
| 4679 | } |
| 4680 | if (Op0LHS == Op1LHS && Op0RHS == Op1RHS) { |
| 4681 | // Simplify (fcmp cc0 x, y) | (fcmp cc1 x, y). |
| 4682 | if (Op0CC == Op1CC) |
| 4683 | return new FCmpInst(*Context, (FCmpInst::Predicate)Op0CC, |
| 4684 | Op0LHS, Op0RHS); |
| 4685 | if (Op0CC == FCmpInst::FCMP_TRUE || Op1CC == FCmpInst::FCMP_TRUE) |
| 4686 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| 4687 | if (Op0CC == FCmpInst::FCMP_FALSE) |
| 4688 | return ReplaceInstUsesWith(I, RHS); |
| 4689 | if (Op1CC == FCmpInst::FCMP_FALSE) |
| 4690 | return ReplaceInstUsesWith(I, LHS); |
| 4691 | bool Op0Ordered; |
| 4692 | bool Op1Ordered; |
| 4693 | unsigned Op0Pred = getFCmpCode(Op0CC, Op0Ordered); |
| 4694 | unsigned Op1Pred = getFCmpCode(Op1CC, Op1Ordered); |
| 4695 | if (Op0Ordered == Op1Ordered) { |
| 4696 | // If both are ordered or unordered, return a new fcmp with |
| 4697 | // or'ed predicates. |
| 4698 | Value *RV = getFCmpValue(Op0Ordered, Op0Pred|Op1Pred, |
| 4699 | Op0LHS, Op0RHS, Context); |
| 4700 | if (Instruction *I = dyn_cast<Instruction>(RV)) |
| 4701 | return I; |
| 4702 | // Otherwise, it's a constant boolean value... |
| 4703 | return ReplaceInstUsesWith(I, RV); |
| 4704 | } |
| 4705 | } |
| 4706 | return 0; |
| 4707 | } |
| 4708 | |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4709 | /// FoldOrWithConstants - This helper function folds: |
| 4710 | /// |
| Bill Wendling | 236a119 | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 4711 | /// ((A | B) & C1) | (B & C2) |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4712 | /// |
| 4713 | /// into: |
| 4714 | /// |
| Bill Wendling | 236a119 | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 4715 | /// (A & C1) | B |
| Bill Wendling | 9912f71 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4716 | /// |
| Bill Wendling | 236a119 | 2008-12-02 05:09:00 +0000 | [diff] [blame] | 4717 | /// when the XOR of the two constants is "all ones" (-1). |
| Bill Wendling | 9912f71 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4718 | Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op, |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4719 | Value *A, Value *B, Value *C) { |
| Bill Wendling | fc5b8e6 | 2008-12-02 05:06:43 +0000 | [diff] [blame] | 4720 | ConstantInt *CI1 = dyn_cast<ConstantInt>(C); |
| 4721 | if (!CI1) return 0; |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4722 | |
| Bill Wendling | 0a0dcaf | 2008-12-02 06:24:20 +0000 | [diff] [blame] | 4723 | Value *V1 = 0; |
| 4724 | ConstantInt *CI2 = 0; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4725 | if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)), *Context)) return 0; |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4726 | |
| Bill Wendling | 86ee316 | 2008-12-02 06:18:11 +0000 | [diff] [blame] | 4727 | APInt Xor = CI1->getValue() ^ CI2->getValue(); |
| 4728 | if (!Xor.isAllOnesValue()) return 0; |
| 4729 | |
| Bill Wendling | 0a0dcaf | 2008-12-02 06:24:20 +0000 | [diff] [blame] | 4730 | if (V1 == A || V1 == B) { |
| Bill Wendling | 86ee316 | 2008-12-02 06:18:11 +0000 | [diff] [blame] | 4731 | Instruction *NewOp = |
| Bill Wendling | 6c8ecbb | 2008-12-02 06:22:04 +0000 | [diff] [blame] | 4732 | InsertNewInstBefore(BinaryOperator::CreateAnd((V1 == A) ? B : A, CI1), I); |
| 4733 | return BinaryOperator::CreateOr(NewOp, V1); |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | return 0; |
| 4737 | } |
| 4738 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4739 | Instruction *InstCombiner::visitOr(BinaryOperator &I) { |
| 4740 | bool Changed = SimplifyCommutative(I); |
| 4741 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 4742 | |
| 4743 | if (isa<UndefValue>(Op1)) // X | undef -> -1 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4744 | return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4745 | |
| 4746 | // or X, X = X |
| 4747 | if (Op0 == Op1) |
| 4748 | return ReplaceInstUsesWith(I, Op0); |
| 4749 | |
| 4750 | // See if we can simplify any instructions used by the instruction whose sole |
| 4751 | // purpose is to compute bits we don't care about. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 4752 | if (SimplifyDemandedInstructionBits(I)) |
| 4753 | return &I; |
| 4754 | if (isa<VectorType>(I.getType())) { |
| 4755 | if (isa<ConstantAggregateZero>(Op1)) { |
| 4756 | return ReplaceInstUsesWith(I, Op0); // X | <0,0> -> X |
| 4757 | } else if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1)) { |
| 4758 | if (CP->isAllOnesValue()) // X | <-1,-1> -> <-1,-1> |
| 4759 | return ReplaceInstUsesWith(I, I.getOperand(1)); |
| 4760 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4761 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4762 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4763 | // or X, -1 == -1 |
| 4764 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| 4765 | ConstantInt *C1 = 0; Value *X = 0; |
| 4766 | // (X & C1) | C2 --> (X | C2) & (C1|C2) |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4767 | if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1)), *Context) && |
| 4768 | isOnlyUse(Op0)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4769 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4770 | InsertNewInstBefore(Or, I); |
| 4771 | Or->takeName(Op0); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4772 | return BinaryOperator::CreateAnd(Or, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4773 | Context->getConstantInt(RHS->getValue() | C1->getValue())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4777 | if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1)), *Context) && |
| 4778 | isOnlyUse(Op0)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4779 | Instruction *Or = BinaryOperator::CreateOr(X, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4780 | InsertNewInstBefore(Or, I); |
| 4781 | Or->takeName(Op0); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4782 | return BinaryOperator::CreateXor(Or, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4783 | Context->getConstantInt(C1->getValue() & ~RHS->getValue())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | // Try to fold constant and into select arguments. |
| 4787 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 4788 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 4789 | return R; |
| 4790 | if (isa<PHINode>(Op0)) |
| 4791 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 4792 | return NV; |
| 4793 | } |
| 4794 | |
| 4795 | Value *A = 0, *B = 0; |
| 4796 | ConstantInt *C1 = 0, *C2 = 0; |
| 4797 | |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4798 | if (match(Op0, m_And(m_Value(A), m_Value(B)), *Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4799 | if (A == Op1 || B == Op1) // (A & ?) | A --> A |
| 4800 | return ReplaceInstUsesWith(I, Op1); |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4801 | if (match(Op1, m_And(m_Value(A), m_Value(B)), *Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4802 | if (A == Op0 || B == Op0) // A | (A & ?) --> A |
| 4803 | return ReplaceInstUsesWith(I, Op0); |
| 4804 | |
| 4805 | // (A | B) | C and A | (B | C) -> bswap if possible. |
| 4806 | // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4807 | if (match(Op0, m_Or(m_Value(), m_Value()), *Context) || |
| 4808 | match(Op1, m_Or(m_Value(), m_Value()), *Context) || |
| 4809 | (match(Op0, m_Shift(m_Value(), m_Value()), *Context) && |
| 4810 | match(Op1, m_Shift(m_Value(), m_Value()), *Context))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4811 | if (Instruction *BSwap = MatchBSwap(I)) |
| 4812 | return BSwap; |
| 4813 | } |
| 4814 | |
| 4815 | // (X^C)|Y -> (X|Y)^C iff Y&C == 0 |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4816 | if (Op0->hasOneUse() && |
| 4817 | match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4818 | MaskedValueIsZero(Op1, C1->getValue())) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4819 | Instruction *NOr = BinaryOperator::CreateOr(A, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4820 | InsertNewInstBefore(NOr, I); |
| 4821 | NOr->takeName(Op0); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4822 | return BinaryOperator::CreateXor(NOr, C1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | // Y|(X^C) -> (X|Y)^C iff Y&C == 0 |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4826 | if (Op1->hasOneUse() && |
| 4827 | match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4828 | MaskedValueIsZero(Op0, C1->getValue())) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4829 | Instruction *NOr = BinaryOperator::CreateOr(A, Op0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4830 | InsertNewInstBefore(NOr, I); |
| 4831 | NOr->takeName(Op0); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4832 | return BinaryOperator::CreateXor(NOr, C1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | // (A & C)|(B & D) |
| 4836 | Value *C = 0, *D = 0; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4837 | if (match(Op0, m_And(m_Value(A), m_Value(C)), *Context) && |
| 4838 | match(Op1, m_And(m_Value(B), m_Value(D)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4839 | Value *V1 = 0, *V2 = 0, *V3 = 0; |
| 4840 | C1 = dyn_cast<ConstantInt>(C); |
| 4841 | C2 = dyn_cast<ConstantInt>(D); |
| 4842 | if (C1 && C2) { // (A & C1)|(B & C2) |
| 4843 | // If we have: ((V + N) & C1) | (V & C2) |
| 4844 | // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0 |
| 4845 | // replace with V+N. |
| 4846 | if (C1->getValue() == ~C2->getValue()) { |
| 4847 | if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4848 | match(A, m_Add(m_Value(V1), m_Value(V2)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4849 | // Add commutes, try both ways. |
| 4850 | if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) |
| 4851 | return ReplaceInstUsesWith(I, A); |
| 4852 | if (V2 == B && MaskedValueIsZero(V1, C2->getValue())) |
| 4853 | return ReplaceInstUsesWith(I, A); |
| 4854 | } |
| 4855 | // Or commutes, try both ways. |
| 4856 | if ((C1->getValue() & (C1->getValue()+1)) == 0 && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4857 | match(B, m_Add(m_Value(V1), m_Value(V2)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4858 | // Add commutes, try both ways. |
| 4859 | if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) |
| 4860 | return ReplaceInstUsesWith(I, B); |
| 4861 | if (V2 == A && MaskedValueIsZero(V1, C1->getValue())) |
| 4862 | return ReplaceInstUsesWith(I, B); |
| 4863 | } |
| 4864 | } |
| 4865 | V1 = 0; V2 = 0; V3 = 0; |
| 4866 | } |
| 4867 | |
| 4868 | // Check to see if we have any common things being and'ed. If so, find the |
| 4869 | // terms for V1 & (V2|V3). |
| 4870 | if (isOnlyUse(Op0) || isOnlyUse(Op1)) { |
| 4871 | if (A == B) // (A & C)|(A & D) == A & (C|D) |
| 4872 | V1 = A, V2 = C, V3 = D; |
| 4873 | else if (A == D) // (A & C)|(B & A) == A & (B|C) |
| 4874 | V1 = A, V2 = B, V3 = C; |
| 4875 | else if (C == B) // (A & C)|(C & D) == C & (A|D) |
| 4876 | V1 = C, V2 = A, V3 = D; |
| 4877 | else if (C == D) // (A & C)|(B & C) == C & (A|B) |
| 4878 | V1 = C, V2 = A, V3 = B; |
| 4879 | |
| 4880 | if (V1) { |
| 4881 | Value *Or = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4882 | InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I); |
| 4883 | return BinaryOperator::CreateAnd(V1, Or); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4884 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4885 | } |
| Dan Gohman | 279952c | 2008-10-28 22:38:57 +0000 | [diff] [blame] | 4886 | |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 4887 | // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4888 | if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D, Context)) |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4889 | return Match; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4890 | if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C, Context)) |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4891 | return Match; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4892 | if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D, Context)) |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4893 | return Match; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4894 | if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C, Context)) |
| Chris Lattner | dd7772b | 2008-11-16 04:24:12 +0000 | [diff] [blame] | 4895 | return Match; |
| Bill Wendling | 22ca835 | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4896 | |
| Bill Wendling | 22ca835 | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4897 | // ((A&~B)|(~A&B)) -> A^B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4898 | if ((match(C, m_Not(m_Specific(D)), *Context) && |
| 4899 | match(B, m_Not(m_Specific(A)), *Context))) |
| Bill Wendling | c1f3113 | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4900 | return BinaryOperator::CreateXor(A, D); |
| Bill Wendling | 22ca835 | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4901 | // ((~B&A)|(~A&B)) -> A^B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4902 | if ((match(A, m_Not(m_Specific(D)), *Context) && |
| 4903 | match(B, m_Not(m_Specific(C)), *Context))) |
| Bill Wendling | c1f3113 | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4904 | return BinaryOperator::CreateXor(C, D); |
| Bill Wendling | 22ca835 | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4905 | // ((A&~B)|(B&~A)) -> A^B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4906 | if ((match(C, m_Not(m_Specific(B)), *Context) && |
| 4907 | match(D, m_Not(m_Specific(A)), *Context))) |
| Bill Wendling | c1f3113 | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4908 | return BinaryOperator::CreateXor(A, B); |
| Bill Wendling | 22ca835 | 2008-11-30 13:52:49 +0000 | [diff] [blame] | 4909 | // ((~B&A)|(B&~A)) -> A^B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4910 | if ((match(A, m_Not(m_Specific(B)), *Context) && |
| 4911 | match(D, m_Not(m_Specific(C)), *Context))) |
| Bill Wendling | c1f3113 | 2008-12-01 08:09:47 +0000 | [diff] [blame] | 4912 | return BinaryOperator::CreateXor(C, B); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | // (X >> Z) | (Y >> Z) -> (X|Y) >> Z for all shifts. |
| 4916 | if (BinaryOperator *SI1 = dyn_cast<BinaryOperator>(Op1)) { |
| 4917 | if (BinaryOperator *SI0 = dyn_cast<BinaryOperator>(Op0)) |
| 4918 | if (SI0->isShift() && SI0->getOpcode() == SI1->getOpcode() && |
| 4919 | SI0->getOperand(1) == SI1->getOperand(1) && |
| 4920 | (SI0->hasOneUse() || SI1->hasOneUse())) { |
| 4921 | Instruction *NewOp = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4922 | InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4923 | SI1->getOperand(0), |
| 4924 | SI0->getName()), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4925 | return BinaryOperator::Create(SI1->getOpcode(), NewOp, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4926 | SI1->getOperand(1)); |
| 4927 | } |
| 4928 | } |
| 4929 | |
| Bill Wendling | d8ce237 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 4930 | // ((A|B)&1)|(B&-2) -> (A&1) | B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4931 | if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) || |
| 4932 | match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) { |
| Bill Wendling | 9912f71 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4933 | Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C); |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4934 | if (Ret) return Ret; |
| Bill Wendling | d8ce237 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 4935 | } |
| 4936 | // (B&-2)|((A|B)&1) -> (A&1) | B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4937 | if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) || |
| 4938 | match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) { |
| Bill Wendling | 9912f71 | 2008-12-01 08:32:40 +0000 | [diff] [blame] | 4939 | Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C); |
| Bill Wendling | dae376a | 2008-12-01 08:23:25 +0000 | [diff] [blame] | 4940 | if (Ret) return Ret; |
| Bill Wendling | d8ce237 | 2008-12-01 01:07:11 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4943 | if (match(Op0, m_Not(m_Value(A)), *Context)) { // ~A | Op1 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4944 | if (A == Op1) // ~A | A == -1 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4945 | return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4946 | } else { |
| 4947 | A = 0; |
| 4948 | } |
| 4949 | // Note, A is still live here! |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 4950 | if (match(Op1, m_Not(m_Value(B)), *Context)) { // Op0 | ~B |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4951 | if (Op0 == B) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4952 | return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4953 | |
| 4954 | // (~A | ~B) == (~(A & B)) - De Morgan's Law |
| 4955 | if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4956 | Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4957 | I.getName()+".demorgan"), I); |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 4958 | return BinaryOperator::CreateNot(*Context, And); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B) |
| 4963 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 4964 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4965 | return R; |
| 4966 | |
| Chris Lattner | 0c678e5 | 2008-11-16 05:20:07 +0000 | [diff] [blame] | 4967 | if (ICmpInst *LHS = dyn_cast<ICmpInst>(I.getOperand(0))) |
| 4968 | if (Instruction *Res = FoldOrOfICmps(I, LHS, RHS)) |
| 4969 | return Res; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
| 4972 | // fold (or (cast A), (cast B)) -> (cast (or A, B)) |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4973 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4974 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 4975 | if (Op0C->getOpcode() == Op1C->getOpcode()) {// same cast kind ? |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4976 | if (!isa<ICmpInst>(Op0C->getOperand(0)) || |
| 4977 | !isa<ICmpInst>(Op1C->getOperand(0))) { |
| 4978 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| Chris Lattner | cf37355 | 2009-07-23 05:32:17 +0000 | [diff] [blame] | 4979 | if (SrcTy == Op1C->getOperand(0)->getType() && |
| 4980 | SrcTy->isIntOrIntVector() && |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4981 | // Only do this if the casts both really cause code to be |
| 4982 | // generated. |
| 4983 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 4984 | I.getType(), TD) && |
| 4985 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 4986 | I.getType(), TD)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4987 | Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0), |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4988 | Op1C->getOperand(0), |
| 4989 | I.getName()); |
| 4990 | InsertNewInstBefore(NewOp, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 4991 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 4992 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 4993 | } |
| 4994 | } |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | |
| 4998 | // (fcmp uno x, c) | (fcmp uno y, c) -> (fcmp uno x, y) |
| 4999 | if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0))) { |
| Chris Lattner | 57e66fa | 2009-07-23 05:46:22 +0000 | [diff] [blame] | 5000 | if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1))) |
| 5001 | if (Instruction *Res = FoldOrOfFCmps(I, LHS, RHS)) |
| 5002 | return Res; |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 5003 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5004 | |
| 5005 | return Changed ? &I : 0; |
| 5006 | } |
| 5007 | |
| Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 5008 | namespace { |
| 5009 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5010 | // XorSelf - Implements: X ^ X --> 0 |
| 5011 | struct XorSelf { |
| 5012 | Value *RHS; |
| 5013 | XorSelf(Value *rhs) : RHS(rhs) {} |
| 5014 | bool shouldApply(Value *LHS) const { return LHS == RHS; } |
| 5015 | Instruction *apply(BinaryOperator &Xor) const { |
| 5016 | return &Xor; |
| 5017 | } |
| 5018 | }; |
| 5019 | |
| Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 5020 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5021 | |
| 5022 | Instruction *InstCombiner::visitXor(BinaryOperator &I) { |
| 5023 | bool Changed = SimplifyCommutative(I); |
| 5024 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5025 | |
| Evan Cheng | e5cd803 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 5026 | if (isa<UndefValue>(Op1)) { |
| 5027 | if (isa<UndefValue>(Op0)) |
| 5028 | // Handle undef ^ undef -> 0 special case. This is a common |
| 5029 | // idiom (misuse). |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5030 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5031 | return ReplaceInstUsesWith(I, Op1); // X ^ undef -> undef |
| Evan Cheng | e5cd803 | 2008-03-25 20:07:13 +0000 | [diff] [blame] | 5032 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5033 | |
| 5034 | // xor X, X = 0, even if X is nested in a sequence of Xor's. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5035 | if (Instruction *Result = AssociativeOpt(I, XorSelf(Op1), Context)) { |
| Chris Lattner | b933ea6 | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 5036 | assert(Result == &I && "AssociativeOpt didn't work?"); Result=Result; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5037 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | // See if we can simplify any instructions used by the instruction whose sole |
| 5041 | // purpose is to compute bits we don't care about. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5042 | if (SimplifyDemandedInstructionBits(I)) |
| 5043 | return &I; |
| 5044 | if (isa<VectorType>(I.getType())) |
| 5045 | if (isa<ConstantAggregateZero>(Op1)) |
| 5046 | return ReplaceInstUsesWith(I, Op0); // X ^ <0,0> -> X |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5047 | |
| 5048 | // Is this a ~ operation? |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5049 | if (Value *NotOp = dyn_castNotVal(&I, Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5050 | // ~(~X & Y) --> (X | ~Y) - De Morgan's Law |
| 5051 | // ~(~X | Y) === (X & ~Y) - De Morgan's Law |
| 5052 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(NotOp)) { |
| 5053 | if (Op0I->getOpcode() == Instruction::And || |
| 5054 | Op0I->getOpcode() == Instruction::Or) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5055 | if (dyn_castNotVal(Op0I->getOperand(1), Context)) Op0I->swapOperands(); |
| 5056 | if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0), Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5057 | Instruction *NotY = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 5058 | BinaryOperator::CreateNot(*Context, Op0I->getOperand(1), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5059 | Op0I->getOperand(1)->getName()+".not"); |
| 5060 | InsertNewInstBefore(NotY, I); |
| 5061 | if (Op0I->getOpcode() == Instruction::And) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5062 | return BinaryOperator::CreateOr(Op0NotVal, NotY); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5063 | else |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5064 | return BinaryOperator::CreateAnd(Op0NotVal, NotY); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5065 | } |
| 5066 | } |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | |
| 5071 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) { |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5072 | if (RHS == Context->getTrue() && Op0->hasOneUse()) { |
| Bill Wendling | 6174195 | 2009-01-01 01:18:23 +0000 | [diff] [blame] | 5073 | // xor (cmp A, B), true = not (cmp A, B) = !cmp A, B |
| Nick Lewycky | 1405e92 | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 5074 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Op0)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5075 | return new ICmpInst(*Context, ICI->getInversePredicate(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5076 | ICI->getOperand(0), ICI->getOperand(1)); |
| 5077 | |
| Nick Lewycky | 1405e92 | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 5078 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(Op0)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5079 | return new FCmpInst(*Context, FCI->getInversePredicate(), |
| Nick Lewycky | 1405e92 | 2007-08-06 20:04:16 +0000 | [diff] [blame] | 5080 | FCI->getOperand(0), FCI->getOperand(1)); |
| 5081 | } |
| 5082 | |
| Nick Lewycky | 0aa63aa | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 5083 | // fold (xor(zext(cmp)), 1) and (xor(sext(cmp)), -1) to ext(!cmp). |
| 5084 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| 5085 | if (CmpInst *CI = dyn_cast<CmpInst>(Op0C->getOperand(0))) { |
| 5086 | if (CI->hasOneUse() && Op0C->hasOneUse()) { |
| 5087 | Instruction::CastOps Opcode = Op0C->getOpcode(); |
| 5088 | if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5089 | if (RHS == Context->getConstantExprCast(Opcode, |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5090 | Context->getTrue(), |
| Nick Lewycky | 0aa63aa | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 5091 | Op0C->getDestTy())) { |
| 5092 | Instruction *NewCI = InsertNewInstBefore(CmpInst::Create( |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5093 | *Context, |
| Nick Lewycky | 0aa63aa | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 5094 | CI->getOpcode(), CI->getInversePredicate(), |
| 5095 | CI->getOperand(0), CI->getOperand(1)), I); |
| 5096 | NewCI->takeName(CI); |
| 5097 | return CastInst::Create(Opcode, NewCI, Op0C->getType()); |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | } |
| 5102 | } |
| 5103 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5104 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 5105 | // ~(c-X) == X-c-1 == X+(-c-1) |
| 5106 | if (Op0I->getOpcode() == Instruction::Sub && RHS->isAllOnesValue()) |
| 5107 | if (Constant *Op0I0C = dyn_cast<Constant>(Op0I->getOperand(0))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5108 | Constant *NegOp0I0C = Context->getConstantExprNeg(Op0I0C); |
| 5109 | Constant *ConstantRHS = Context->getConstantExprSub(NegOp0I0C, |
| 5110 | Context->getConstantInt(I.getType(), 1)); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5111 | return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5114 | if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5115 | if (Op0I->getOpcode() == Instruction::Add) { |
| 5116 | // ~(X-c) --> (-c-1)-X |
| 5117 | if (RHS->isAllOnesValue()) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5118 | Constant *NegOp0CI = Context->getConstantExprNeg(Op0CI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5119 | return BinaryOperator::CreateSub( |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5120 | Context->getConstantExprSub(NegOp0CI, |
| 5121 | Context->getConstantInt(I.getType(), 1)), |
| 5122 | Op0I->getOperand(0)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5123 | } else if (RHS->getValue().isSignBit()) { |
| 5124 | // (X + C) ^ signbit -> (X + C + signbit) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5125 | Constant *C = |
| 5126 | Context->getConstantInt(RHS->getValue() + Op0CI->getValue()); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5127 | return BinaryOperator::CreateAdd(Op0I->getOperand(0), C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5128 | |
| 5129 | } |
| 5130 | } else if (Op0I->getOpcode() == Instruction::Or) { |
| 5131 | // (X|C1)^C2 -> X^(C1|C2) iff X&~C1 == 0 |
| 5132 | if (MaskedValueIsZero(Op0I->getOperand(0), Op0CI->getValue())) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5133 | Constant *NewRHS = Context->getConstantExprOr(Op0CI, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5134 | // Anything in both C1 and C2 is known to be zero, remove it from |
| 5135 | // NewRHS. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5136 | Constant *CommonBits = Context->getConstantExprAnd(Op0CI, RHS); |
| 5137 | NewRHS = Context->getConstantExprAnd(NewRHS, |
| 5138 | Context->getConstantExprNot(CommonBits)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5139 | AddToWorkList(Op0I); |
| 5140 | I.setOperand(0, Op0I->getOperand(0)); |
| 5141 | I.setOperand(1, NewRHS); |
| 5142 | return &I; |
| 5143 | } |
| 5144 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 5145 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | // Try to fold constant and into select arguments. |
| 5149 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 5150 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 5151 | return R; |
| 5152 | if (isa<PHINode>(Op0)) |
| 5153 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5154 | return NV; |
| 5155 | } |
| 5156 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5157 | if (Value *X = dyn_castNotVal(Op0, Context)) // ~A ^ A == -1 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5158 | if (X == Op1) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5159 | return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5160 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5161 | if (Value *X = dyn_castNotVal(Op1, Context)) // A ^ ~A == -1 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5162 | if (X == Op0) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5163 | return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5164 | |
| 5165 | |
| 5166 | BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1); |
| 5167 | if (Op1I) { |
| 5168 | Value *A, *B; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5169 | if (match(Op1I, m_Or(m_Value(A), m_Value(B)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5170 | if (A == Op0) { // B^(B|A) == (A|B)^B |
| 5171 | Op1I->swapOperands(); |
| 5172 | I.swapOperands(); |
| 5173 | std::swap(Op0, Op1); |
| 5174 | } else if (B == Op0) { // B^(A|B) == (A|B)^B |
| 5175 | I.swapOperands(); // Simplified below. |
| 5176 | std::swap(Op0, Op1); |
| 5177 | } |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5178 | } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)), *Context)) { |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5179 | return ReplaceInstUsesWith(I, B); // A^(A^B) == B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5180 | } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)), *Context)) { |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5181 | return ReplaceInstUsesWith(I, A); // A^(B^A) == B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5182 | } else if (match(Op1I, m_And(m_Value(A), m_Value(B)), *Context) && |
| 5183 | Op1I->hasOneUse()){ |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5184 | if (A == Op0) { // A^(A&B) -> A^(B&A) |
| 5185 | Op1I->swapOperands(); |
| 5186 | std::swap(A, B); |
| 5187 | } |
| 5188 | if (B == Op0) { // A^(B&A) -> (B&A)^A |
| 5189 | I.swapOperands(); // Simplified below. |
| 5190 | std::swap(Op0, Op1); |
| 5191 | } |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0); |
| 5196 | if (Op0I) { |
| 5197 | Value *A, *B; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5198 | if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) && |
| 5199 | Op0I->hasOneUse()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5200 | if (A == Op1) // (B|A)^B == (A|B)^B |
| 5201 | std::swap(A, B); |
| 5202 | if (B == Op1) { // (A|B)^B == A & ~B |
| 5203 | Instruction *NotB = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 5204 | InsertNewInstBefore(BinaryOperator::CreateNot(*Context, |
| 5205 | Op1, "tmp"), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5206 | return BinaryOperator::CreateAnd(A, NotB); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5207 | } |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5208 | } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)), *Context)) { |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5209 | return ReplaceInstUsesWith(I, B); // (A^B)^A == B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5210 | } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)), *Context)) { |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 5211 | return ReplaceInstUsesWith(I, A); // (B^A)^A == B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5212 | } else if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) && |
| 5213 | Op0I->hasOneUse()){ |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5214 | if (A == Op1) // (A&B)^A -> (B&A)^A |
| 5215 | std::swap(A, B); |
| 5216 | if (B == Op1 && // (B&A)^A == ~B & A |
| 5217 | !isa<ConstantInt>(Op1)) { // Canonical form is (B&C)^C |
| 5218 | Instruction *N = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 5219 | InsertNewInstBefore(BinaryOperator::CreateNot(*Context, A, "tmp"), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5220 | return BinaryOperator::CreateAnd(N, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5221 | } |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | // (X >> Z) ^ (Y >> Z) -> (X^Y) >> Z for all shifts. |
| 5226 | if (Op0I && Op1I && Op0I->isShift() && |
| 5227 | Op0I->getOpcode() == Op1I->getOpcode() && |
| 5228 | Op0I->getOperand(1) == Op1I->getOperand(1) && |
| 5229 | (Op1I->hasOneUse() || Op1I->hasOneUse())) { |
| 5230 | Instruction *NewOp = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5231 | InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5232 | Op1I->getOperand(0), |
| 5233 | Op0I->getName()), I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5234 | return BinaryOperator::Create(Op1I->getOpcode(), NewOp, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5235 | Op1I->getOperand(1)); |
| 5236 | } |
| 5237 | |
| 5238 | if (Op0I && Op1I) { |
| 5239 | Value *A, *B, *C, *D; |
| 5240 | // (A & B)^(A | B) -> A ^ B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5241 | if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) && |
| 5242 | match(Op1I, m_Or(m_Value(C), m_Value(D)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5243 | if ((A == C && B == D) || (A == D && B == C)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5244 | return BinaryOperator::CreateXor(A, B); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5245 | } |
| 5246 | // (A | B)^(A & B) -> A ^ B |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5247 | if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) && |
| 5248 | match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5249 | if ((A == C && B == D) || (A == D && B == C)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5250 | return BinaryOperator::CreateXor(A, B); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
| 5253 | // (A & B)^(C & D) |
| 5254 | if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 5255 | match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) && |
| 5256 | match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5257 | // (X & Y)^(X & Y) -> (Y^Z) & X |
| 5258 | Value *X = 0, *Y = 0, *Z = 0; |
| 5259 | if (A == C) |
| 5260 | X = A, Y = B, Z = D; |
| 5261 | else if (A == D) |
| 5262 | X = A, Y = B, Z = C; |
| 5263 | else if (B == C) |
| 5264 | X = B, Y = A, Z = D; |
| 5265 | else if (B == D) |
| 5266 | X = B, Y = A, Z = C; |
| 5267 | |
| 5268 | if (X) { |
| 5269 | Instruction *NewOp = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5270 | InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I); |
| 5271 | return BinaryOperator::CreateAnd(NewOp, X); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5272 | } |
| 5273 | } |
| 5274 | } |
| 5275 | |
| 5276 | // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B) |
| 5277 | if (ICmpInst *RHS = dyn_cast<ICmpInst>(I.getOperand(1))) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5278 | if (Instruction *R = AssociativeOpt(I, FoldICmpLogical(*this, RHS),Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5279 | return R; |
| 5280 | |
| 5281 | // fold (xor (cast A), (cast B)) -> (cast (xor A, B)) |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 5282 | if (CastInst *Op0C = dyn_cast<CastInst>(Op0)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5283 | if (CastInst *Op1C = dyn_cast<CastInst>(Op1)) |
| 5284 | if (Op0C->getOpcode() == Op1C->getOpcode()) { // same cast kind? |
| 5285 | const Type *SrcTy = Op0C->getOperand(0)->getType(); |
| 5286 | if (SrcTy == Op1C->getOperand(0)->getType() && SrcTy->isInteger() && |
| 5287 | // Only do this if the casts both really cause code to be generated. |
| 5288 | ValueRequiresCast(Op0C->getOpcode(), Op0C->getOperand(0), |
| 5289 | I.getType(), TD) && |
| 5290 | ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0), |
| 5291 | I.getType(), TD)) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5292 | Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5293 | Op1C->getOperand(0), |
| 5294 | I.getName()); |
| 5295 | InsertNewInstBefore(NewOp, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5296 | return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5297 | } |
| 5298 | } |
| Chris Lattner | 9188243 | 2007-10-24 05:38:08 +0000 | [diff] [blame] | 5299 | } |
| Nick Lewycky | 0aa63aa | 2008-05-31 19:01:33 +0000 | [diff] [blame] | 5300 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5301 | return Changed ? &I : 0; |
| 5302 | } |
| 5303 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5304 | static ConstantInt *ExtractElement(Constant *V, Constant *Idx, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 5305 | LLVMContext *Context) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5306 | return cast<ConstantInt>(Context->getConstantExprExtractElement(V, Idx)); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5307 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5308 | |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5309 | static bool HasAddOverflow(ConstantInt *Result, |
| 5310 | ConstantInt *In1, ConstantInt *In2, |
| 5311 | bool IsSigned) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5312 | if (IsSigned) |
| 5313 | if (In2->getValue().isNegative()) |
| 5314 | return Result->getValue().sgt(In1->getValue()); |
| 5315 | else |
| 5316 | return Result->getValue().slt(In1->getValue()); |
| 5317 | else |
| 5318 | return Result->getValue().ult(In1->getValue()); |
| 5319 | } |
| 5320 | |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5321 | /// AddWithOverflow - Compute Result = In1+In2, returning true if the result |
| Dan Gohman | b80d561 | 2008-09-10 23:30:57 +0000 | [diff] [blame] | 5322 | /// overflowed for this type. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5323 | static bool AddWithOverflow(Constant *&Result, Constant *In1, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 5324 | Constant *In2, LLVMContext *Context, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5325 | bool IsSigned = false) { |
| 5326 | Result = Context->getConstantExprAdd(In1, In2); |
| Dan Gohman | b80d561 | 2008-09-10 23:30:57 +0000 | [diff] [blame] | 5327 | |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5328 | if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
| 5329 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5330 | Constant *Idx = Context->getConstantInt(Type::Int32Ty, i); |
| 5331 | if (HasAddOverflow(ExtractElement(Result, Idx, Context), |
| 5332 | ExtractElement(In1, Idx, Context), |
| 5333 | ExtractElement(In2, Idx, Context), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5334 | IsSigned)) |
| 5335 | return true; |
| 5336 | } |
| 5337 | return false; |
| 5338 | } |
| 5339 | |
| 5340 | return HasAddOverflow(cast<ConstantInt>(Result), |
| 5341 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 5342 | IsSigned); |
| 5343 | } |
| 5344 | |
| 5345 | static bool HasSubOverflow(ConstantInt *Result, |
| 5346 | ConstantInt *In1, ConstantInt *In2, |
| 5347 | bool IsSigned) { |
| Dan Gohman | b80d561 | 2008-09-10 23:30:57 +0000 | [diff] [blame] | 5348 | if (IsSigned) |
| 5349 | if (In2->getValue().isNegative()) |
| 5350 | return Result->getValue().slt(In1->getValue()); |
| 5351 | else |
| 5352 | return Result->getValue().sgt(In1->getValue()); |
| 5353 | else |
| 5354 | return Result->getValue().ugt(In1->getValue()); |
| 5355 | } |
| 5356 | |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5357 | /// SubWithOverflow - Compute Result = In1-In2, returning true if the result |
| 5358 | /// overflowed for this type. |
| 5359 | static bool SubWithOverflow(Constant *&Result, Constant *In1, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 5360 | Constant *In2, LLVMContext *Context, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5361 | bool IsSigned = false) { |
| 5362 | Result = Context->getConstantExprSub(In1, In2); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5363 | |
| 5364 | if (const VectorType *VTy = dyn_cast<VectorType>(In1->getType())) { |
| 5365 | for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5366 | Constant *Idx = Context->getConstantInt(Type::Int32Ty, i); |
| 5367 | if (HasSubOverflow(ExtractElement(Result, Idx, Context), |
| 5368 | ExtractElement(In1, Idx, Context), |
| 5369 | ExtractElement(In2, Idx, Context), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5370 | IsSigned)) |
| 5371 | return true; |
| 5372 | } |
| 5373 | return false; |
| 5374 | } |
| 5375 | |
| 5376 | return HasSubOverflow(cast<ConstantInt>(Result), |
| 5377 | cast<ConstantInt>(In1), cast<ConstantInt>(In2), |
| 5378 | IsSigned); |
| 5379 | } |
| 5380 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5381 | /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the |
| 5382 | /// code necessary to compute the offset from the base pointer (without adding |
| 5383 | /// in the base pointer). Return the result as a signed integer of intptr size. |
| 5384 | static Value *EmitGEPOffset(User *GEP, Instruction &I, InstCombiner &IC) { |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 5385 | TargetData &TD = *IC.getTargetData(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5386 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 5387 | const Type *IntPtrTy = TD.getIntPtrType(); |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 5388 | LLVMContext *Context = IC.getContext(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5389 | Value *Result = Context->getNullValue(IntPtrTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5390 | |
| 5391 | // Build a mask for high order bits. |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5392 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5393 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 5394 | |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 5395 | for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e; |
| 5396 | ++i, ++GTI) { |
| 5397 | Value *Op = *i; |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5398 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5399 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) { |
| 5400 | if (OpC->isZero()) continue; |
| 5401 | |
| 5402 | // Handle a struct index, which adds its field offset to the pointer. |
| 5403 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5404 | Size = TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue()); |
| 5405 | |
| 5406 | if (ConstantInt *RC = dyn_cast<ConstantInt>(Result)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5407 | Result = |
| 5408 | Context->getConstantInt(RC->getValue() + APInt(IntPtrWidth, Size)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5409 | else |
| 5410 | Result = IC.InsertNewInstBefore( |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5411 | BinaryOperator::CreateAdd(Result, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5412 | Context->getConstantInt(IntPtrTy, Size), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5413 | GEP->getName()+".offs"), I); |
| 5414 | continue; |
| 5415 | } |
| 5416 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5417 | Constant *Scale = Context->getConstantInt(IntPtrTy, Size); |
| 5418 | Constant *OC = |
| 5419 | Context->getConstantExprIntegerCast(OpC, IntPtrTy, true /*SExt*/); |
| 5420 | Scale = Context->getConstantExprMul(OC, Scale); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5421 | if (Constant *RC = dyn_cast<Constant>(Result)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5422 | Result = Context->getConstantExprAdd(RC, Scale); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5423 | else { |
| 5424 | // Emit an add instruction. |
| 5425 | Result = IC.InsertNewInstBefore( |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5426 | BinaryOperator::CreateAdd(Result, Scale, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5427 | GEP->getName()+".offs"), I); |
| 5428 | } |
| 5429 | continue; |
| 5430 | } |
| 5431 | // Convert to correct type. |
| 5432 | if (Op->getType() != IntPtrTy) { |
| 5433 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5434 | Op = Context->getConstantExprIntegerCast(OpC, IntPtrTy, true); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5435 | else |
| Chris Lattner | 2941a65 | 2009-04-07 05:03:34 +0000 | [diff] [blame] | 5436 | Op = IC.InsertNewInstBefore(CastInst::CreateIntegerCast(Op, IntPtrTy, |
| 5437 | true, |
| 5438 | Op->getName()+".c"), I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5439 | } |
| 5440 | if (Size != 1) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5441 | Constant *Scale = Context->getConstantInt(IntPtrTy, Size); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5442 | if (Constant *OpC = dyn_cast<Constant>(Op)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5443 | Op = Context->getConstantExprMul(OpC, Scale); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5444 | else // We'll let instcombine(mul) convert this to a shl if possible. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5445 | Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5446 | GEP->getName()+".idx"), I); |
| 5447 | } |
| 5448 | |
| 5449 | // Emit an add instruction. |
| 5450 | if (isa<Constant>(Op) && isa<Constant>(Result)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5451 | Result = Context->getConstantExprAdd(cast<Constant>(Op), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5452 | cast<Constant>(Result)); |
| 5453 | else |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5454 | Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5455 | GEP->getName()+".offs"), I); |
| 5456 | } |
| 5457 | return Result; |
| 5458 | } |
| 5459 | |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5460 | |
| Dan Gohman | ff9b473 | 2009-07-17 22:16:21 +0000 | [diff] [blame] | 5461 | /// EvaluateGEPOffsetExpression - Return a value that can be used to compare |
| 5462 | /// the *offset* implied by a GEP to zero. For example, if we have &A[i], we |
| 5463 | /// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can |
| 5464 | /// be complex, and scales are involved. The above expression would also be |
| 5465 | /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32). |
| 5466 | /// This later form is less amenable to optimization though, and we are allowed |
| 5467 | /// to generate the first by knowing that pointer arithmetic doesn't overflow. |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5468 | /// |
| 5469 | /// If we can't emit an optimized form for this expression, this returns null. |
| 5470 | /// |
| 5471 | static Value *EvaluateGEPOffsetExpression(User *GEP, Instruction &I, |
| 5472 | InstCombiner &IC) { |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 5473 | TargetData &TD = *IC.getTargetData(); |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5474 | gep_type_iterator GTI = gep_type_begin(GEP); |
| 5475 | |
| 5476 | // Check to see if this gep only has a single variable index. If so, and if |
| 5477 | // any constant indices are a multiple of its scale, then we can compute this |
| 5478 | // in terms of the scale of the variable index. For example, if the GEP |
| 5479 | // implies an offset of "12 + i*4", then we can codegen this as "3 + i", |
| 5480 | // because the expression will cross zero at the same point. |
| 5481 | unsigned i, e = GEP->getNumOperands(); |
| 5482 | int64_t Offset = 0; |
| 5483 | for (i = 1; i != e; ++i, ++GTI) { |
| 5484 | if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { |
| 5485 | // Compute the aggregate offset of constant indices. |
| 5486 | if (CI->isZero()) continue; |
| 5487 | |
| 5488 | // Handle a struct index, which adds its field offset to the pointer. |
| 5489 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5490 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5491 | } else { |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5492 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5493 | Offset += Size*CI->getSExtValue(); |
| 5494 | } |
| 5495 | } else { |
| 5496 | // Found our variable index. |
| 5497 | break; |
| 5498 | } |
| 5499 | } |
| 5500 | |
| 5501 | // If there are no variable indices, we must have a constant offset, just |
| 5502 | // evaluate it the general way. |
| 5503 | if (i == e) return 0; |
| 5504 | |
| 5505 | Value *VariableIdx = GEP->getOperand(i); |
| 5506 | // Determine the scale factor of the variable element. For example, this is |
| 5507 | // 4 if the variable index is into an array of i32. |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5508 | uint64_t VariableScale = TD.getTypeAllocSize(GTI.getIndexedType()); |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5509 | |
| 5510 | // Verify that there are no other variable indices. If so, emit the hard way. |
| 5511 | for (++i, ++GTI; i != e; ++i, ++GTI) { |
| 5512 | ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i)); |
| 5513 | if (!CI) return 0; |
| 5514 | |
| 5515 | // Compute the aggregate offset of constant indices. |
| 5516 | if (CI->isZero()) continue; |
| 5517 | |
| 5518 | // Handle a struct index, which adds its field offset to the pointer. |
| 5519 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 5520 | Offset += TD.getStructLayout(STy)->getElementOffset(CI->getZExtValue()); |
| 5521 | } else { |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 5522 | uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType()); |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5523 | Offset += Size*CI->getSExtValue(); |
| 5524 | } |
| 5525 | } |
| 5526 | |
| 5527 | // Okay, we know we have a single variable index, which must be a |
| 5528 | // pointer/array/vector index. If there is no offset, life is simple, return |
| 5529 | // the index. |
| 5530 | unsigned IntPtrWidth = TD.getPointerSizeInBits(); |
| 5531 | if (Offset == 0) { |
| 5532 | // Cast to intptrty in case a truncation occurs. If an extension is needed, |
| 5533 | // we don't need to bother extending: the extension won't affect where the |
| 5534 | // computation crosses zero. |
| 5535 | if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) |
| 5536 | VariableIdx = new TruncInst(VariableIdx, TD.getIntPtrType(), |
| 5537 | VariableIdx->getNameStart(), &I); |
| 5538 | return VariableIdx; |
| 5539 | } |
| 5540 | |
| 5541 | // Otherwise, there is an index. The computation we will do will be modulo |
| 5542 | // the pointer size, so get it. |
| 5543 | uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth); |
| 5544 | |
| 5545 | Offset &= PtrSizeMask; |
| 5546 | VariableScale &= PtrSizeMask; |
| 5547 | |
| 5548 | // To do this transformation, any constant index must be a multiple of the |
| 5549 | // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i", |
| 5550 | // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a |
| 5551 | // multiple of the variable scale. |
| 5552 | int64_t NewOffs = Offset / (int64_t)VariableScale; |
| 5553 | if (Offset != NewOffs*(int64_t)VariableScale) |
| 5554 | return 0; |
| 5555 | |
| 5556 | // Okay, we can do this evaluation. Start by converting the index to intptr. |
| 5557 | const Type *IntPtrTy = TD.getIntPtrType(); |
| 5558 | if (VariableIdx->getType() != IntPtrTy) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5559 | VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy, |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5560 | true /*SExt*/, |
| 5561 | VariableIdx->getNameStart(), &I); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5562 | Constant *OffsetVal = IC.getContext()->getConstantInt(IntPtrTy, NewOffs); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 5563 | return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I); |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5567 | /// FoldGEPICmp - Fold comparisons between a GEP instruction and something |
| 5568 | /// else. At this point we know that the GEP is on the LHS of the comparison. |
| 5569 | Instruction *InstCombiner::FoldGEPICmp(User *GEPLHS, Value *RHS, |
| 5570 | ICmpInst::Predicate Cond, |
| 5571 | Instruction &I) { |
| 5572 | assert(dyn_castGetElementPtr(GEPLHS) && "LHS is not a getelementptr!"); |
| 5573 | |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5574 | // Look through bitcasts. |
| 5575 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(RHS)) |
| 5576 | RHS = BCI->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5577 | |
| 5578 | Value *PtrBase = GEPLHS->getOperand(0); |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 5579 | if (TD && PtrBase == RHS) { |
| Chris Lattner | af97d02 | 2008-02-05 04:45:32 +0000 | [diff] [blame] | 5580 | // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0). |
| Chris Lattner | eba7586 | 2008-04-22 02:53:33 +0000 | [diff] [blame] | 5581 | // This transformation (ignoring the base and scales) is valid because we |
| 5582 | // know pointers can't overflow. See if we can output an optimized form. |
| 5583 | Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, I, *this); |
| 5584 | |
| 5585 | // If not, synthesize the offset the hard way. |
| 5586 | if (Offset == 0) |
| 5587 | Offset = EmitGEPOffset(GEPLHS, I, *this); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5588 | return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), Offset, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5589 | Context->getNullValue(Offset->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5590 | } else if (User *GEPRHS = dyn_castGetElementPtr(RHS)) { |
| 5591 | // If the base pointers are different, but the indices are the same, just |
| 5592 | // compare the base pointer. |
| 5593 | if (PtrBase != GEPRHS->getOperand(0)) { |
| 5594 | bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands(); |
| 5595 | IndicesTheSame &= GEPLHS->getOperand(0)->getType() == |
| 5596 | GEPRHS->getOperand(0)->getType(); |
| 5597 | if (IndicesTheSame) |
| 5598 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5599 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 5600 | IndicesTheSame = false; |
| 5601 | break; |
| 5602 | } |
| 5603 | |
| 5604 | // If all indices are the same, just compare the base pointers. |
| 5605 | if (IndicesTheSame) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5606 | return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5607 | GEPLHS->getOperand(0), GEPRHS->getOperand(0)); |
| 5608 | |
| 5609 | // Otherwise, the base pointers are different and the indices are |
| 5610 | // different, bail out. |
| 5611 | return 0; |
| 5612 | } |
| 5613 | |
| 5614 | // If one of the GEPs has all zero indices, recurse. |
| 5615 | bool AllZeros = true; |
| 5616 | for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i) |
| 5617 | if (!isa<Constant>(GEPLHS->getOperand(i)) || |
| 5618 | !cast<Constant>(GEPLHS->getOperand(i))->isNullValue()) { |
| 5619 | AllZeros = false; |
| 5620 | break; |
| 5621 | } |
| 5622 | if (AllZeros) |
| 5623 | return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0), |
| 5624 | ICmpInst::getSwappedPredicate(Cond), I); |
| 5625 | |
| 5626 | // If the other GEP has all zero indices, recurse. |
| 5627 | AllZeros = true; |
| 5628 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5629 | if (!isa<Constant>(GEPRHS->getOperand(i)) || |
| 5630 | !cast<Constant>(GEPRHS->getOperand(i))->isNullValue()) { |
| 5631 | AllZeros = false; |
| 5632 | break; |
| 5633 | } |
| 5634 | if (AllZeros) |
| 5635 | return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I); |
| 5636 | |
| 5637 | if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) { |
| 5638 | // If the GEPs only differ by one index, compare it. |
| 5639 | unsigned NumDifferences = 0; // Keep track of # differences. |
| 5640 | unsigned DiffOperand = 0; // The operand that differs. |
| 5641 | for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i) |
| 5642 | if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) { |
| 5643 | if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() != |
| 5644 | GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) { |
| 5645 | // Irreconcilable differences. |
| 5646 | NumDifferences = 2; |
| 5647 | break; |
| 5648 | } else { |
| 5649 | if (NumDifferences++) break; |
| 5650 | DiffOperand = i; |
| 5651 | } |
| 5652 | } |
| 5653 | |
| 5654 | if (NumDifferences == 0) // SAME GEP? |
| 5655 | return ReplaceInstUsesWith(I, // No comparison is needed here. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5656 | Context->getConstantInt(Type::Int1Ty, |
| Nick Lewycky | 09284cf | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5657 | ICmpInst::isTrueWhenEqual(Cond))); |
| Nick Lewycky | 2de09a9 | 2007-09-06 02:40:25 +0000 | [diff] [blame] | 5658 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5659 | else if (NumDifferences == 1) { |
| 5660 | Value *LHSV = GEPLHS->getOperand(DiffOperand); |
| 5661 | Value *RHSV = GEPRHS->getOperand(DiffOperand); |
| 5662 | // Make sure we do a signed comparison here. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5663 | return new ICmpInst(*Context, |
| 5664 | ICmpInst::getSignedPredicate(Cond), LHSV, RHSV); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5665 | } |
| 5666 | } |
| 5667 | |
| 5668 | // Only lower this if the icmp is the only user of the GEP or if we expect |
| 5669 | // the result to fold to a constant! |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 5670 | if (TD && |
| 5671 | (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5672 | (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) { |
| 5673 | // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2) |
| 5674 | Value *L = EmitGEPOffset(GEPLHS, I, *this); |
| 5675 | Value *R = EmitGEPOffset(GEPRHS, I, *this); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5676 | return new ICmpInst(*Context, ICmpInst::getSignedPredicate(Cond), L, R); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5677 | } |
| 5678 | } |
| 5679 | return 0; |
| 5680 | } |
| 5681 | |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5682 | /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible. |
| 5683 | /// |
| 5684 | Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I, |
| 5685 | Instruction *LHSI, |
| 5686 | Constant *RHSC) { |
| 5687 | if (!isa<ConstantFP>(RHSC)) return 0; |
| 5688 | const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF(); |
| 5689 | |
| 5690 | // Get the width of the mantissa. We don't want to hack on conversions that |
| 5691 | // might lose information from the integer, e.g. "i64 -> float" |
| Chris Lattner | 9ce836b | 2008-05-19 21:17:23 +0000 | [diff] [blame] | 5692 | int MantissaWidth = LHSI->getType()->getFPMantissaWidth(); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5693 | if (MantissaWidth == -1) return 0; // Unknown. |
| 5694 | |
| 5695 | // Check to see that the input is converted from an integer type that is small |
| 5696 | // enough that preserves all bits. TODO: check here for "known" sign bits. |
| 5697 | // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5698 | unsigned InputSize = LHSI->getOperand(0)->getType()->getScalarSizeInBits(); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5699 | |
| 5700 | // If this is a uitofp instruction, we need an extra bit to hold the sign. |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5701 | bool LHSUnsigned = isa<UIToFPInst>(LHSI); |
| 5702 | if (LHSUnsigned) |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5703 | ++InputSize; |
| 5704 | |
| 5705 | // If the conversion would lose info, don't hack on this. |
| 5706 | if ((int)InputSize > MantissaWidth) |
| 5707 | return 0; |
| 5708 | |
| 5709 | // Otherwise, we can potentially simplify the comparison. We know that it |
| 5710 | // will always come through as an integer value and we know the constant is |
| 5711 | // not a NAN (it would have been previously simplified). |
| 5712 | assert(!RHS.isNaN() && "NaN comparison not already folded!"); |
| 5713 | |
| 5714 | ICmpInst::Predicate Pred; |
| 5715 | switch (I.getPredicate()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5716 | default: llvm_unreachable("Unexpected predicate!"); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5717 | case FCmpInst::FCMP_UEQ: |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5718 | case FCmpInst::FCMP_OEQ: |
| 5719 | Pred = ICmpInst::ICMP_EQ; |
| 5720 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5721 | case FCmpInst::FCMP_UGT: |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5722 | case FCmpInst::FCMP_OGT: |
| 5723 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT; |
| 5724 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5725 | case FCmpInst::FCMP_UGE: |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5726 | case FCmpInst::FCMP_OGE: |
| 5727 | Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE; |
| 5728 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5729 | case FCmpInst::FCMP_ULT: |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5730 | case FCmpInst::FCMP_OLT: |
| 5731 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT; |
| 5732 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5733 | case FCmpInst::FCMP_ULE: |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5734 | case FCmpInst::FCMP_OLE: |
| 5735 | Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE; |
| 5736 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5737 | case FCmpInst::FCMP_UNE: |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5738 | case FCmpInst::FCMP_ONE: |
| 5739 | Pred = ICmpInst::ICMP_NE; |
| 5740 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5741 | case FCmpInst::FCMP_ORD: |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5742 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5743 | case FCmpInst::FCMP_UNO: |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5744 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | const IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType()); |
| 5748 | |
| 5749 | // Now we know that the APFloat is a normal number, zero or inf. |
| 5750 | |
| Chris Lattner | f13ff49 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5751 | // See if the FP constant is too large for the integer. For example, |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5752 | // comparing an i8 to 300.0. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 5753 | unsigned IntWidth = IntTy->getScalarSizeInBits(); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5754 | |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5755 | if (!LHSUnsigned) { |
| 5756 | // If the RHS value is > SignedMax, fold the comparison. This handles +INF |
| 5757 | // and large values. |
| 5758 | APFloat SMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 5759 | SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true, |
| 5760 | APFloat::rmNearestTiesToEven); |
| 5761 | if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0 |
| 5762 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT || |
| 5763 | Pred == ICmpInst::ICMP_SLE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5764 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| 5765 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5766 | } |
| 5767 | } else { |
| 5768 | // If the RHS value is > UnsignedMax, fold the comparison. This handles |
| 5769 | // +INF and large values. |
| 5770 | APFloat UMax(RHS.getSemantics(), APFloat::fcZero, false); |
| 5771 | UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false, |
| 5772 | APFloat::rmNearestTiesToEven); |
| 5773 | if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0 |
| 5774 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT || |
| 5775 | Pred == ICmpInst::ICMP_ULE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5776 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| 5777 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5778 | } |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5779 | } |
| 5780 | |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5781 | if (!LHSUnsigned) { |
| 5782 | // See if the RHS value is < SignedMin. |
| 5783 | APFloat SMin(RHS.getSemantics(), APFloat::fcZero, false); |
| 5784 | SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true, |
| 5785 | APFloat::rmNearestTiesToEven); |
| 5786 | if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0 |
| 5787 | if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT || |
| 5788 | Pred == ICmpInst::ICMP_SGE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5789 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| 5790 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5791 | } |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
| Bill Wendling | 20636df | 2008-11-09 04:26:50 +0000 | [diff] [blame] | 5794 | // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or |
| 5795 | // [0, UMAX], but it may still be fractional. See if it is fractional by |
| 5796 | // casting the FP value to the integer value and back, checking for equality. |
| 5797 | // Don't do this for zero, because -0.0 is not fractional. |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5798 | Constant *RHSInt = LHSUnsigned |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5799 | ? Context->getConstantExprFPToUI(RHSC, IntTy) |
| 5800 | : Context->getConstantExprFPToSI(RHSC, IntTy); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5801 | if (!RHS.isZero()) { |
| 5802 | bool Equal = LHSUnsigned |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5803 | ? Context->getConstantExprUIToFP(RHSInt, RHSC->getType()) == RHSC |
| 5804 | : Context->getConstantExprSIToFP(RHSInt, RHSC->getType()) == RHSC; |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5805 | if (!Equal) { |
| 5806 | // If we had a comparison against a fractional value, we have to adjust |
| 5807 | // the compare predicate and sometimes the value. RHSC is rounded towards |
| 5808 | // zero at this point. |
| 5809 | switch (Pred) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5810 | default: llvm_unreachable("Unexpected integer comparison!"); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5811 | case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5812 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5813 | case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5814 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5815 | case ICmpInst::ICMP_ULE: |
| 5816 | // (float)int <= 4.4 --> int <= 4 |
| 5817 | // (float)int <= -4.4 --> false |
| 5818 | if (RHS.isNegative()) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5819 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5820 | break; |
| 5821 | case ICmpInst::ICMP_SLE: |
| 5822 | // (float)int <= 4.4 --> int <= 4 |
| 5823 | // (float)int <= -4.4 --> int < -4 |
| 5824 | if (RHS.isNegative()) |
| 5825 | Pred = ICmpInst::ICMP_SLT; |
| 5826 | break; |
| 5827 | case ICmpInst::ICMP_ULT: |
| 5828 | // (float)int < -4.4 --> false |
| 5829 | // (float)int < 4.4 --> int <= 4 |
| 5830 | if (RHS.isNegative()) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5831 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5832 | Pred = ICmpInst::ICMP_ULE; |
| 5833 | break; |
| 5834 | case ICmpInst::ICMP_SLT: |
| 5835 | // (float)int < -4.4 --> int < -4 |
| 5836 | // (float)int < 4.4 --> int <= 4 |
| 5837 | if (!RHS.isNegative()) |
| 5838 | Pred = ICmpInst::ICMP_SLE; |
| 5839 | break; |
| 5840 | case ICmpInst::ICMP_UGT: |
| 5841 | // (float)int > 4.4 --> int > 4 |
| 5842 | // (float)int > -4.4 --> true |
| 5843 | if (RHS.isNegative()) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5844 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5845 | break; |
| 5846 | case ICmpInst::ICMP_SGT: |
| 5847 | // (float)int > 4.4 --> int > 4 |
| 5848 | // (float)int > -4.4 --> int >= -4 |
| 5849 | if (RHS.isNegative()) |
| 5850 | Pred = ICmpInst::ICMP_SGE; |
| 5851 | break; |
| 5852 | case ICmpInst::ICMP_UGE: |
| 5853 | // (float)int >= -4.4 --> true |
| 5854 | // (float)int >= 4.4 --> int > 4 |
| 5855 | if (!RHS.isNegative()) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5856 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Evan Cheng | 1411813 | 2009-05-22 23:10:53 +0000 | [diff] [blame] | 5857 | Pred = ICmpInst::ICMP_UGT; |
| 5858 | break; |
| 5859 | case ICmpInst::ICMP_SGE: |
| 5860 | // (float)int >= -4.4 --> int >= -4 |
| 5861 | // (float)int >= 4.4 --> int > 4 |
| 5862 | if (!RHS.isNegative()) |
| 5863 | Pred = ICmpInst::ICMP_SGT; |
| 5864 | break; |
| 5865 | } |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5866 | } |
| 5867 | } |
| 5868 | |
| 5869 | // Lower this FP comparison into an appropriate integer version of the |
| 5870 | // comparison. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5871 | return new ICmpInst(*Context, Pred, LHSI->getOperand(0), RHSInt); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5872 | } |
| 5873 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5874 | Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) { |
| 5875 | bool Changed = SimplifyCompare(I); |
| 5876 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5877 | |
| 5878 | // Fold trivial predicates. |
| 5879 | if (I.getPredicate() == FCmpInst::FCMP_FALSE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5880 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5881 | if (I.getPredicate() == FCmpInst::FCMP_TRUE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5882 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5883 | |
| 5884 | // Simplify 'fcmp pred X, X' |
| 5885 | if (Op0 == Op1) { |
| 5886 | switch (I.getPredicate()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5887 | default: llvm_unreachable("Unknown predicate!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5888 | case FCmpInst::FCMP_UEQ: // True if unordered or equal |
| 5889 | case FCmpInst::FCMP_UGE: // True if unordered, greater than, or equal |
| 5890 | case FCmpInst::FCMP_ULE: // True if unordered, less than, or equal |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5891 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5892 | case FCmpInst::FCMP_OGT: // True if ordered and greater than |
| 5893 | case FCmpInst::FCMP_OLT: // True if ordered and less than |
| 5894 | case FCmpInst::FCMP_ONE: // True if ordered and operands are unequal |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5895 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5896 | |
| 5897 | case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y) |
| 5898 | case FCmpInst::FCMP_ULT: // True if unordered or less than |
| 5899 | case FCmpInst::FCMP_UGT: // True if unordered or greater than |
| 5900 | case FCmpInst::FCMP_UNE: // True if unordered or not equal |
| 5901 | // Canonicalize these to be 'fcmp uno %X, 0.0'. |
| 5902 | I.setPredicate(FCmpInst::FCMP_UNO); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5903 | I.setOperand(1, Context->getNullValue(Op0->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5904 | return &I; |
| 5905 | |
| 5906 | case FCmpInst::FCMP_ORD: // True if ordered (no nans) |
| 5907 | case FCmpInst::FCMP_OEQ: // True if ordered and equal |
| 5908 | case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal |
| 5909 | case FCmpInst::FCMP_OLE: // True if ordered and less than or equal |
| 5910 | // Canonicalize these to be 'fcmp ord %X, 0.0'. |
| 5911 | I.setPredicate(FCmpInst::FCMP_ORD); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5912 | I.setOperand(1, Context->getNullValue(Op0->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5913 | return &I; |
| 5914 | } |
| 5915 | } |
| 5916 | |
| 5917 | if (isa<UndefValue>(Op1)) // fcmp pred X, undef -> undef |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5918 | return ReplaceInstUsesWith(I, Context->getUndef(Type::Int1Ty)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5919 | |
| 5920 | // Handle fcmp with constant RHS |
| 5921 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5922 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 5923 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 5924 | if (CFP->getValueAPF().isNaN()) { |
| 5925 | if (FCmpInst::isOrdered(I.getPredicate())) // True if ordered and... |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5926 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | f13ff49 | 2008-05-20 03:50:52 +0000 | [diff] [blame] | 5927 | assert(FCmpInst::isUnordered(I.getPredicate()) && |
| 5928 | "Comparison must be either ordered or unordered!"); |
| 5929 | // True if unordered. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 5930 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5931 | } |
| 5932 | } |
| 5933 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5934 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 5935 | switch (LHSI->getOpcode()) { |
| 5936 | case Instruction::PHI: |
| Chris Lattner | a2417ba | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 5937 | // Only fold fcmp into the PHI if the phi and fcmp are in the same |
| 5938 | // block. If in the same block, we're encouraging jump threading. If |
| 5939 | // not, we are just pessimizing the code by making an i1 phi. |
| 5940 | if (LHSI->getParent() == I.getParent()) |
| 5941 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 5942 | return NV; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5943 | break; |
| Chris Lattner | e6b62d9 | 2008-05-19 20:18:56 +0000 | [diff] [blame] | 5944 | case Instruction::SIToFP: |
| 5945 | case Instruction::UIToFP: |
| 5946 | if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC)) |
| 5947 | return NV; |
| 5948 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5949 | case Instruction::Select: |
| 5950 | // If either operand of the select is a constant, we can fold the |
| 5951 | // comparison into the select arms, which will cause one to be |
| 5952 | // constant folded and the select turned into a bitwise or. |
| 5953 | Value *Op1 = 0, *Op2 = 0; |
| 5954 | if (LHSI->hasOneUse()) { |
| 5955 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 5956 | // Fold the known value into the constant operand. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5957 | Op1 = Context->getConstantExprCompare(I.getPredicate(), C, RHSC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5958 | // Insert a new FCmp of the other select operand. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5959 | Op2 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5960 | LHSI->getOperand(2), RHSC, |
| 5961 | I.getName()), I); |
| 5962 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 5963 | // Fold the known value into the constant operand. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5964 | Op2 = Context->getConstantExprCompare(I.getPredicate(), C, RHSC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5965 | // Insert a new FCmp of the other select operand. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 5966 | Op1 = InsertNewInstBefore(new FCmpInst(*Context, I.getPredicate(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5967 | LHSI->getOperand(1), RHSC, |
| 5968 | I.getName()), I); |
| 5969 | } |
| 5970 | } |
| 5971 | |
| 5972 | if (Op1) |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 5973 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5974 | break; |
| 5975 | } |
| 5976 | } |
| 5977 | |
| 5978 | return Changed ? &I : 0; |
| 5979 | } |
| 5980 | |
| 5981 | Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { |
| 5982 | bool Changed = SimplifyCompare(I); |
| 5983 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 5984 | const Type *Ty = Op0->getType(); |
| 5985 | |
| 5986 | // icmp X, X |
| 5987 | if (Op0 == Op1) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5988 | return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty, |
| Nick Lewycky | 09284cf | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 5989 | I.isTrueWhenEqual())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5990 | |
| 5991 | if (isa<UndefValue>(Op1)) // X icmp undef -> undef |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 5992 | return ReplaceInstUsesWith(I, Context->getUndef(Type::Int1Ty)); |
| Christopher Lamb | f78cd32 | 2007-12-18 21:32:20 +0000 | [diff] [blame] | 5993 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 5994 | // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value |
| 5995 | // addresses never equal each other! We already know that Op0 != Op1. |
| 5996 | if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) || |
| 5997 | isa<ConstantPointerNull>(Op0)) && |
| 5998 | (isa<GlobalValue>(Op1) || isa<AllocaInst>(Op1) || |
| 5999 | isa<ConstantPointerNull>(Op1))) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6000 | return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty, |
| Nick Lewycky | 09284cf | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 6001 | !I.isTrueWhenEqual())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6002 | |
| 6003 | // icmp's with boolean values can always be turned into bitwise operations |
| 6004 | if (Ty == Type::Int1Ty) { |
| 6005 | switch (I.getPredicate()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6006 | default: llvm_unreachable("Invalid icmp instruction!"); |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6007 | case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6008 | Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6009 | InsertNewInstBefore(Xor, I); |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 6010 | return BinaryOperator::CreateNot(*Context, Xor); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6011 | } |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6012 | case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6013 | return BinaryOperator::CreateXor(Op0, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6014 | |
| 6015 | case ICmpInst::ICMP_UGT: |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6016 | std::swap(Op0, Op1); // Change icmp ugt -> icmp ult |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6017 | // FALL THROUGH |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6018 | case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 6019 | Instruction *Not = BinaryOperator::CreateNot(*Context, |
| 6020 | Op0, I.getName()+"tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6021 | InsertNewInstBefore(Not, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6022 | return BinaryOperator::CreateAnd(Not, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6023 | } |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6024 | case ICmpInst::ICMP_SGT: |
| 6025 | std::swap(Op0, Op1); // Change icmp sgt -> icmp slt |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6026 | // FALL THROUGH |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6027 | case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 6028 | Instruction *Not = BinaryOperator::CreateNot(*Context, |
| 6029 | Op1, I.getName()+"tmp"); |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6030 | InsertNewInstBefore(Not, I); |
| 6031 | return BinaryOperator::CreateAnd(Not, Op0); |
| 6032 | } |
| 6033 | case ICmpInst::ICMP_UGE: |
| 6034 | std::swap(Op0, Op1); // Change icmp uge -> icmp ule |
| 6035 | // FALL THROUGH |
| 6036 | case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 6037 | Instruction *Not = BinaryOperator::CreateNot(*Context, |
| 6038 | Op0, I.getName()+"tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6039 | InsertNewInstBefore(Not, I); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6040 | return BinaryOperator::CreateOr(Not, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6041 | } |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6042 | case ICmpInst::ICMP_SGE: |
| 6043 | std::swap(Op0, Op1); // Change icmp sge -> icmp sle |
| 6044 | // FALL THROUGH |
| 6045 | case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 6046 | Instruction *Not = BinaryOperator::CreateNot(*Context, |
| 6047 | Op1, I.getName()+"tmp"); |
| Chris Lattner | a02893d | 2008-07-11 04:20:58 +0000 | [diff] [blame] | 6048 | InsertNewInstBefore(Not, I); |
| 6049 | return BinaryOperator::CreateOr(Not, Op0); |
| 6050 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } |
| 6053 | |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6054 | unsigned BitWidth = 0; |
| 6055 | if (TD) |
| Dan Gohman | 2526aea | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 6056 | BitWidth = TD->getTypeSizeInBits(Ty->getScalarType()); |
| 6057 | else if (Ty->isIntOrIntVector()) |
| 6058 | BitWidth = Ty->getScalarSizeInBits(); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6059 | |
| 6060 | bool isSignBit = false; |
| 6061 | |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 6062 | // See if we are doing a comparison with a constant. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6063 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| Nick Lewycky | 7c5c237 | 2009-02-27 06:37:39 +0000 | [diff] [blame] | 6064 | Value *A = 0, *B = 0; |
| Christopher Lamb | fa6b310 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 6065 | |
| Chris Lattner | be6c54a | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 6066 | // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) |
| 6067 | if (I.isEquality() && CI->isNullValue() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6068 | match(Op0, m_Sub(m_Value(A), m_Value(B)), *Context)) { |
| Chris Lattner | be6c54a | 2008-01-05 01:18:20 +0000 | [diff] [blame] | 6069 | // (icmp cond A B) if cond is equality |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6070 | return new ICmpInst(*Context, I.getPredicate(), A, B); |
| Owen Anderson | 42f61ed | 2007-12-28 07:42:12 +0000 | [diff] [blame] | 6071 | } |
| Christopher Lamb | fa6b310 | 2007-12-20 07:21:11 +0000 | [diff] [blame] | 6072 | |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 6073 | // If we have an icmp le or icmp ge instruction, turn it into the |
| 6074 | // appropriate icmp lt or icmp gt instruction. This allows us to rely on |
| 6075 | // them being folded in the code below. |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6076 | switch (I.getPredicate()) { |
| 6077 | default: break; |
| 6078 | case ICmpInst::ICMP_ULE: |
| 6079 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6080 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6081 | return new ICmpInst(*Context, ICmpInst::ICMP_ULT, Op0, |
| 6082 | AddOne(CI, Context)); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6083 | case ICmpInst::ICMP_SLE: |
| 6084 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6085 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6086 | return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0, |
| 6087 | AddOne(CI, Context)); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6088 | case ICmpInst::ICMP_UGE: |
| 6089 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6090 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6091 | return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Op0, |
| 6092 | SubOne(CI, Context)); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6093 | case ICmpInst::ICMP_SGE: |
| 6094 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6095 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6096 | return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0, |
| 6097 | SubOne(CI, Context)); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6098 | } |
| 6099 | |
| Chris Lattner | a130865 | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 6100 | // If this comparison is a normal comparison, it demands all |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6101 | // bits, if it is a sign bit comparison, it only demands the sign bit. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6102 | bool UnusedBit; |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6103 | isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit); |
| 6104 | } |
| 6105 | |
| 6106 | // See if we can fold the comparison based on range information we can get |
| 6107 | // by checking whether bits are known to be zero or one in the input. |
| 6108 | if (BitWidth != 0) { |
| 6109 | APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0); |
| 6110 | APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0); |
| 6111 | |
| 6112 | if (SimplifyDemandedBits(I.getOperandUse(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6113 | isSignBit ? APInt::getSignBit(BitWidth) |
| 6114 | : APInt::getAllOnesValue(BitWidth), |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6115 | Op0KnownZero, Op0KnownOne, 0)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6116 | return &I; |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6117 | if (SimplifyDemandedBits(I.getOperandUse(1), |
| 6118 | APInt::getAllOnesValue(BitWidth), |
| 6119 | Op1KnownZero, Op1KnownOne, 0)) |
| 6120 | return &I; |
| 6121 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6122 | // Given the known and unknown bits, compute a range that the LHS could be |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6123 | // in. Compute the Min, Max and RHS values based on the known bits. For the |
| 6124 | // EQ and NE we use unsigned values. |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6125 | APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0); |
| 6126 | APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0); |
| 6127 | if (ICmpInst::isSignedPredicate(I.getPredicate())) { |
| 6128 | ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, |
| 6129 | Op0Min, Op0Max); |
| 6130 | ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, |
| 6131 | Op1Min, Op1Max); |
| 6132 | } else { |
| 6133 | ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne, |
| 6134 | Op0Min, Op0Max); |
| 6135 | ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne, |
| 6136 | Op1Min, Op1Max); |
| 6137 | } |
| 6138 | |
| Chris Lattner | a130865 | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 6139 | // If Min and Max are known to be the same, then SimplifyDemandedBits |
| 6140 | // figured out that the LHS is a constant. Just constant fold this now so |
| 6141 | // that code below can assume that Min != Max. |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6142 | if (!isa<Constant>(Op0) && Op0Min == Op0Max) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6143 | return new ICmpInst(*Context, I.getPredicate(), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6144 | Context->getConstantInt(Op0Min), Op1); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6145 | if (!isa<Constant>(Op1) && Op1Min == Op1Max) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6146 | return new ICmpInst(*Context, I.getPredicate(), Op0, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6147 | Context->getConstantInt(Op1Min)); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6148 | |
| Chris Lattner | a130865 | 2008-07-11 05:40:05 +0000 | [diff] [blame] | 6149 | // Based on the range information we know about the LHS, see if we can |
| 6150 | // simplify this comparison. For example, (x&4) < 8 is always true. |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6151 | switch (I.getPredicate()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6152 | default: llvm_unreachable("Unknown icmp opcode!"); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6153 | case ICmpInst::ICMP_EQ: |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6154 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6155 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6156 | break; |
| 6157 | case ICmpInst::ICMP_NE: |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6158 | if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max)) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6159 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6160 | break; |
| 6161 | case ICmpInst::ICMP_ULT: |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6162 | if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6163 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6164 | if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6165 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6166 | if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6167 | return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6168 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 6169 | if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6170 | return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, |
| 6171 | SubOne(CI, Context)); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6172 | |
| 6173 | // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear |
| 6174 | if (CI->isMinValue(true)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6175 | return new ICmpInst(*Context, ICmpInst::ICMP_SGT, Op0, |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 6176 | Context->getAllOnesValue(Op0->getType())); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6177 | } |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6178 | break; |
| 6179 | case ICmpInst::ICMP_UGT: |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6180 | if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6181 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6182 | if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6183 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6184 | |
| 6185 | if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6186 | return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6187 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 6188 | if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6189 | return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, |
| 6190 | AddOne(CI, Context)); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6191 | |
| 6192 | // (x >u 2147483647) -> (x <s 0) -> true if sign bit set |
| 6193 | if (CI->isMaxValue(true)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6194 | return new ICmpInst(*Context, ICmpInst::ICMP_SLT, Op0, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6195 | Context->getNullValue(Op0->getType())); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6196 | } |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6197 | break; |
| 6198 | case ICmpInst::ICMP_SLT: |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6199 | if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6200 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6201 | if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6202 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6203 | if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6204 | return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6205 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 6206 | if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6207 | return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, |
| 6208 | SubOne(CI, Context)); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6209 | } |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6210 | break; |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6211 | case ICmpInst::ICMP_SGT: |
| 6212 | if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6213 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6214 | if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6215 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6216 | |
| 6217 | if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6218 | return new ICmpInst(*Context, ICmpInst::ICMP_NE, Op0, Op1); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6219 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| 6220 | if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6221 | return new ICmpInst(*Context, ICmpInst::ICMP_EQ, Op0, |
| 6222 | AddOne(CI, Context)); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6223 | } |
| 6224 | break; |
| 6225 | case ICmpInst::ICMP_SGE: |
| 6226 | assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!"); |
| 6227 | if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6228 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6229 | if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6230 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6231 | break; |
| 6232 | case ICmpInst::ICMP_SLE: |
| 6233 | assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!"); |
| 6234 | if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6235 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6236 | if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6237 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6238 | break; |
| 6239 | case ICmpInst::ICMP_UGE: |
| 6240 | assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!"); |
| 6241 | if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6242 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6243 | if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6244 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6245 | break; |
| 6246 | case ICmpInst::ICMP_ULE: |
| 6247 | assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!"); |
| 6248 | if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6249 | return ReplaceInstUsesWith(I, Context->getTrue()); |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6250 | if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6251 | return ReplaceInstUsesWith(I, Context->getFalse()); |
| Chris Lattner | 62d0f23 | 2008-07-11 05:08:55 +0000 | [diff] [blame] | 6252 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6253 | } |
| Dan Gohman | 7934d59 | 2009-04-25 17:12:48 +0000 | [diff] [blame] | 6254 | |
| 6255 | // Turn a signed comparison into an unsigned one if both operands |
| 6256 | // are known to have the same sign. |
| 6257 | if (I.isSignedPredicate() && |
| 6258 | ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) || |
| 6259 | (Op0KnownOne.isNegative() && Op1KnownOne.isNegative()))) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6260 | return new ICmpInst(*Context, I.getUnsignedPredicate(), Op0, Op1); |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 6261 | } |
| 6262 | |
| 6263 | // Test if the ICmpInst instruction is used exclusively by a select as |
| 6264 | // part of a minimum or maximum operation. If so, refrain from doing |
| 6265 | // any other folding. This helps out other analyses which understand |
| 6266 | // non-obfuscated minimum and maximum idioms, such as ScalarEvolution |
| 6267 | // and CodeGen. And in this case, at least one of the comparison |
| 6268 | // operands has at least one user besides the compare (the select), |
| 6269 | // which would often largely negate the benefit of folding anyway. |
| 6270 | if (I.hasOneUse()) |
| 6271 | if (SelectInst *SI = dyn_cast<SelectInst>(*I.use_begin())) |
| 6272 | if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) || |
| 6273 | (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1)) |
| 6274 | return 0; |
| 6275 | |
| 6276 | // See if we are doing a comparison between a constant and an instruction that |
| 6277 | // can be folded into the comparison. |
| 6278 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6279 | // Since the RHS is a ConstantInt (CI), if the left hand side is an |
| 6280 | // instruction, see if that instruction also has constants so that the |
| 6281 | // instruction can be folded into the icmp |
| 6282 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 6283 | if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI)) |
| 6284 | return Res; |
| 6285 | } |
| 6286 | |
| 6287 | // Handle icmp with constant (but not simple integer constant) RHS |
| 6288 | if (Constant *RHSC = dyn_cast<Constant>(Op1)) { |
| 6289 | if (Instruction *LHSI = dyn_cast<Instruction>(Op0)) |
| 6290 | switch (LHSI->getOpcode()) { |
| 6291 | case Instruction::GetElementPtr: |
| 6292 | if (RHSC->isNullValue()) { |
| 6293 | // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null |
| 6294 | bool isAllZeros = true; |
| 6295 | for (unsigned i = 1, e = LHSI->getNumOperands(); i != e; ++i) |
| 6296 | if (!isa<Constant>(LHSI->getOperand(i)) || |
| 6297 | !cast<Constant>(LHSI->getOperand(i))->isNullValue()) { |
| 6298 | isAllZeros = false; |
| 6299 | break; |
| 6300 | } |
| 6301 | if (isAllZeros) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6302 | return new ICmpInst(*Context, I.getPredicate(), LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6303 | Context->getNullValue(LHSI->getOperand(0)->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6304 | } |
| 6305 | break; |
| 6306 | |
| 6307 | case Instruction::PHI: |
| Chris Lattner | a2417ba | 2008-06-08 20:52:11 +0000 | [diff] [blame] | 6308 | // Only fold icmp into the PHI if the phi and fcmp are in the same |
| 6309 | // block. If in the same block, we're encouraging jump threading. If |
| 6310 | // not, we are just pessimizing the code by making an i1 phi. |
| 6311 | if (LHSI->getParent() == I.getParent()) |
| 6312 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 6313 | return NV; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6314 | break; |
| 6315 | case Instruction::Select: { |
| 6316 | // If either operand of the select is a constant, we can fold the |
| 6317 | // comparison into the select arms, which will cause one to be |
| 6318 | // constant folded and the select turned into a bitwise or. |
| 6319 | Value *Op1 = 0, *Op2 = 0; |
| 6320 | if (LHSI->hasOneUse()) { |
| 6321 | if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) { |
| 6322 | // Fold the known value into the constant operand. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6323 | Op1 = Context->getConstantExprICmp(I.getPredicate(), C, RHSC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6324 | // Insert a new ICmp of the other select operand. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6325 | Op2 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6326 | LHSI->getOperand(2), RHSC, |
| 6327 | I.getName()), I); |
| 6328 | } else if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) { |
| 6329 | // Fold the known value into the constant operand. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6330 | Op2 = Context->getConstantExprICmp(I.getPredicate(), C, RHSC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6331 | // Insert a new ICmp of the other select operand. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6332 | Op1 = InsertNewInstBefore(new ICmpInst(*Context, I.getPredicate(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6333 | LHSI->getOperand(1), RHSC, |
| 6334 | I.getName()), I); |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | if (Op1) |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 6339 | return SelectInst::Create(LHSI->getOperand(0), Op1, Op2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6340 | break; |
| 6341 | } |
| 6342 | case Instruction::Malloc: |
| 6343 | // If we have (malloc != null), and if the malloc has a single use, we |
| 6344 | // can assume it is successful and remove the malloc. |
| 6345 | if (LHSI->hasOneUse() && isa<ConstantPointerNull>(RHSC)) { |
| 6346 | AddToWorkList(LHSI); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6347 | return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty, |
| Nick Lewycky | 09284cf | 2008-05-17 07:33:39 +0000 | [diff] [blame] | 6348 | !I.isTrueWhenEqual())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6349 | } |
| 6350 | break; |
| 6351 | } |
| 6352 | } |
| 6353 | |
| 6354 | // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now. |
| 6355 | if (User *GEP = dyn_castGetElementPtr(Op0)) |
| 6356 | if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I)) |
| 6357 | return NI; |
| 6358 | if (User *GEP = dyn_castGetElementPtr(Op1)) |
| 6359 | if (Instruction *NI = FoldGEPICmp(GEP, Op0, |
| 6360 | ICmpInst::getSwappedPredicate(I.getPredicate()), I)) |
| 6361 | return NI; |
| 6362 | |
| 6363 | // Test to see if the operands of the icmp are casted versions of other |
| 6364 | // values. If the ptr->ptr cast can be stripped off both arguments, we do so |
| 6365 | // now. |
| 6366 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) { |
| 6367 | if (isa<PointerType>(Op0->getType()) && |
| 6368 | (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) { |
| 6369 | // We keep moving the cast from the left operand over to the right |
| 6370 | // operand, where it can often be eliminated completely. |
| 6371 | Op0 = CI->getOperand(0); |
| 6372 | |
| 6373 | // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast |
| 6374 | // so eliminate it as well. |
| 6375 | if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1)) |
| 6376 | Op1 = CI2->getOperand(0); |
| 6377 | |
| 6378 | // If Op1 is a constant, we can fold the cast into the constant. |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6379 | if (Op0->getType() != Op1->getType()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6380 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6381 | Op1 = Context->getConstantExprBitCast(Op1C, Op0->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6382 | } else { |
| 6383 | // Otherwise, cast the RHS right before the icmp |
| Chris Lattner | 13c2d6e | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 6384 | Op1 = InsertBitCastBefore(Op1, Op0->getType(), I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6385 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6386 | } |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6387 | return new ICmpInst(*Context, I.getPredicate(), Op0, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6388 | } |
| 6389 | } |
| 6390 | |
| 6391 | if (isa<CastInst>(Op0)) { |
| 6392 | // Handle the special case of: icmp (cast bool to X), <cst> |
| 6393 | // This comes up when you have code like |
| 6394 | // int X = A < B; |
| 6395 | // if (X) ... |
| 6396 | // For generality, we handle any zero-extension of any operand comparison |
| 6397 | // with a constant or another cast from the same type. |
| 6398 | if (isa<ConstantInt>(Op1) || isa<CastInst>(Op1)) |
| 6399 | if (Instruction *R = visitICmpInstWithCastAndCast(I)) |
| 6400 | return R; |
| 6401 | } |
| 6402 | |
| Nick Lewycky | d4c5ea0 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6403 | // See if it's the same type of instruction on the left and right. |
| 6404 | if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) { |
| 6405 | if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1)) { |
| Nick Lewycky | 58ecfb2 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6406 | if (Op0I->getOpcode() == Op1I->getOpcode() && Op0I->hasOneUse() && |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6407 | Op1I->hasOneUse() && Op0I->getOperand(1) == Op1I->getOperand(1)) { |
| Nick Lewycky | cfadfbd | 2008-09-03 06:24:21 +0000 | [diff] [blame] | 6408 | switch (Op0I->getOpcode()) { |
| Nick Lewycky | d4c5ea0 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6409 | default: break; |
| 6410 | case Instruction::Add: |
| 6411 | case Instruction::Sub: |
| 6412 | case Instruction::Xor: |
| Chris Lattner | f3b445e | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6413 | if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6414 | return new ICmpInst(*Context, I.getPredicate(), Op0I->getOperand(0), |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6415 | Op1I->getOperand(0)); |
| Chris Lattner | f3b445e | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6416 | // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b |
| 6417 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 6418 | if (CI->getValue().isSignBit()) { |
| 6419 | ICmpInst::Predicate Pred = I.isSignedPredicate() |
| 6420 | ? I.getUnsignedPredicate() |
| 6421 | : I.getSignedPredicate(); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6422 | return new ICmpInst(*Context, Pred, Op0I->getOperand(0), |
| Chris Lattner | f3b445e | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6423 | Op1I->getOperand(0)); |
| 6424 | } |
| 6425 | |
| 6426 | if (CI->getValue().isMaxSignedValue()) { |
| 6427 | ICmpInst::Predicate Pred = I.isSignedPredicate() |
| 6428 | ? I.getUnsignedPredicate() |
| 6429 | : I.getSignedPredicate(); |
| 6430 | Pred = I.getSwappedPredicate(Pred); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6431 | return new ICmpInst(*Context, Pred, Op0I->getOperand(0), |
| Chris Lattner | f3b445e | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6432 | Op1I->getOperand(0)); |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6433 | } |
| 6434 | } |
| Nick Lewycky | d4c5ea0 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6435 | break; |
| 6436 | case Instruction::Mul: |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6437 | if (!I.isEquality()) |
| 6438 | break; |
| 6439 | |
| Nick Lewycky | 58ecfb2 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6440 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) { |
| 6441 | // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask |
| 6442 | // Mask = -1 >> count-trailing-zeros(Cst). |
| 6443 | if (!CI->isZero() && !CI->isOne()) { |
| 6444 | const APInt &AP = CI->getValue(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6445 | ConstantInt *Mask = Context->getConstantInt( |
| Nick Lewycky | 58ecfb2 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6446 | APInt::getLowBitsSet(AP.getBitWidth(), |
| 6447 | AP.getBitWidth() - |
| Nick Lewycky | d4c5ea0 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6448 | AP.countTrailingZeros())); |
| Nick Lewycky | 58ecfb2 | 2008-08-21 05:56:10 +0000 | [diff] [blame] | 6449 | Instruction *And1 = BinaryOperator::CreateAnd(Op0I->getOperand(0), |
| 6450 | Mask); |
| 6451 | Instruction *And2 = BinaryOperator::CreateAnd(Op1I->getOperand(0), |
| 6452 | Mask); |
| 6453 | InsertNewInstBefore(And1, I); |
| 6454 | InsertNewInstBefore(And2, I); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6455 | return new ICmpInst(*Context, I.getPredicate(), And1, And2); |
| Nick Lewycky | d4c5ea0 | 2008-07-11 07:20:53 +0000 | [diff] [blame] | 6456 | } |
| 6457 | } |
| 6458 | break; |
| 6459 | } |
| 6460 | } |
| 6461 | } |
| 6462 | } |
| 6463 | |
| Chris Lattner | a4e1eef | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 6464 | // ~x < ~y --> y < x |
| 6465 | { Value *A, *B; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6466 | if (match(Op0, m_Not(m_Value(A)), *Context) && |
| 6467 | match(Op1, m_Not(m_Value(B)), *Context)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6468 | return new ICmpInst(*Context, I.getPredicate(), B, A); |
| Chris Lattner | a4e1eef | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6471 | if (I.isEquality()) { |
| 6472 | Value *A, *B, *C, *D; |
| Chris Lattner | a4e1eef | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 6473 | |
| 6474 | // -x == -y --> x == y |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6475 | if (match(Op0, m_Neg(m_Value(A)), *Context) && |
| 6476 | match(Op1, m_Neg(m_Value(B)), *Context)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6477 | return new ICmpInst(*Context, I.getPredicate(), A, B); |
| Chris Lattner | a4e1eef | 2008-05-09 05:19:28 +0000 | [diff] [blame] | 6478 | |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6479 | if (match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6480 | if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 |
| 6481 | Value *OtherVal = A == Op1 ? B : A; |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6482 | return new ICmpInst(*Context, I.getPredicate(), OtherVal, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6483 | Context->getNullValue(A->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6484 | } |
| 6485 | |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6486 | if (match(Op1, m_Xor(m_Value(C), m_Value(D)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6487 | // A^c1 == C^c2 --> A == C^(c1^c2) |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6488 | ConstantInt *C1, *C2; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6489 | if (match(B, m_ConstantInt(C1), *Context) && |
| 6490 | match(D, m_ConstantInt(C2), *Context) && Op1->hasOneUse()) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6491 | Constant *NC = |
| 6492 | Context->getConstantInt(C1->getValue() ^ C2->getValue()); |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6493 | Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6494 | return new ICmpInst(*Context, I.getPredicate(), A, |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6495 | InsertNewInstBefore(Xor, I)); |
| 6496 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6497 | |
| 6498 | // A^B == A^D -> B == D |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6499 | if (A == C) return new ICmpInst(*Context, I.getPredicate(), B, D); |
| 6500 | if (A == D) return new ICmpInst(*Context, I.getPredicate(), B, C); |
| 6501 | if (B == C) return new ICmpInst(*Context, I.getPredicate(), A, D); |
| 6502 | if (B == D) return new ICmpInst(*Context, I.getPredicate(), A, C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6503 | } |
| 6504 | } |
| 6505 | |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6506 | if (match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context) && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6507 | (A == Op0 || B == Op0)) { |
| 6508 | // A == (A^B) -> B == 0 |
| 6509 | Value *OtherVal = A == Op0 ? B : A; |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6510 | return new ICmpInst(*Context, I.getPredicate(), OtherVal, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6511 | Context->getNullValue(A->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6512 | } |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6513 | |
| 6514 | // (A-B) == A -> B == 0 |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6515 | if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6516 | return new ICmpInst(*Context, I.getPredicate(), B, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6517 | Context->getNullValue(B->getType())); |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 6518 | |
| 6519 | // A == (A-B) -> B == 0 |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6520 | if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6521 | return new ICmpInst(*Context, I.getPredicate(), B, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6522 | Context->getNullValue(B->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6523 | |
| 6524 | // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 |
| 6525 | if (Op0->hasOneUse() && Op1->hasOneUse() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 6526 | match(Op0, m_And(m_Value(A), m_Value(B)), *Context) && |
| 6527 | match(Op1, m_And(m_Value(C), m_Value(D)), *Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6528 | Value *X = 0, *Y = 0, *Z = 0; |
| 6529 | |
| 6530 | if (A == C) { |
| 6531 | X = B; Y = D; Z = A; |
| 6532 | } else if (A == D) { |
| 6533 | X = B; Y = C; Z = A; |
| 6534 | } else if (B == C) { |
| 6535 | X = A; Y = D; Z = B; |
| 6536 | } else if (B == D) { |
| 6537 | X = A; Y = C; Z = B; |
| 6538 | } |
| 6539 | |
| 6540 | if (X) { // Build (X^Y) & Z |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6541 | Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I); |
| 6542 | Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6543 | I.setOperand(0, Op1); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6544 | I.setOperand(1, Context->getNullValue(Op1->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6545 | return &I; |
| 6546 | } |
| 6547 | } |
| 6548 | } |
| 6549 | return Changed ? &I : 0; |
| 6550 | } |
| 6551 | |
| 6552 | |
| 6553 | /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS |
| 6554 | /// and CmpRHS are both known to be integer constants. |
| 6555 | Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 6556 | ConstantInt *DivRHS) { |
| 6557 | ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1)); |
| 6558 | const APInt &CmpRHSV = CmpRHS->getValue(); |
| 6559 | |
| 6560 | // FIXME: If the operand types don't match the type of the divide |
| 6561 | // then don't attempt this transform. The code below doesn't have the |
| 6562 | // logic to deal with a signed divide and an unsigned compare (and |
| 6563 | // vice versa). This is because (x /s C1) <s C2 produces different |
| 6564 | // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even |
| 6565 | // (x /u C1) <u C2. Simply casting the operands and result won't |
| 6566 | // work. :( The if statement below tests that condition and bails |
| 6567 | // if it finds it. |
| 6568 | bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv; |
| 6569 | if (!ICI.isEquality() && DivIsSigned != ICI.isSignedPredicate()) |
| 6570 | return 0; |
| 6571 | if (DivRHS->isZero()) |
| 6572 | return 0; // The ProdOV computation fails on divide by zero. |
| Chris Lattner | bd85a5f | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6573 | if (DivIsSigned && DivRHS->isAllOnesValue()) |
| 6574 | return 0; // The overflow computation also screws up here |
| 6575 | if (DivRHS->isOne()) |
| 6576 | return 0; // Not worth bothering, and eliminates some funny cases |
| 6577 | // with INT_MIN. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6578 | |
| 6579 | // Compute Prod = CI * DivRHS. We are essentially solving an equation |
| 6580 | // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and |
| 6581 | // C2 (CI). By solving for X we can turn this into a range check |
| 6582 | // instead of computing a divide. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6583 | Constant *Prod = Context->getConstantExprMul(CmpRHS, DivRHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6584 | |
| 6585 | // Determine if the product overflows by seeing if the product is |
| 6586 | // not equal to the divide. Make sure we do the same kind of divide |
| 6587 | // as in the LHS instruction that we're folding. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6588 | bool ProdOV = (DivIsSigned ? Context->getConstantExprSDiv(Prod, DivRHS) : |
| 6589 | Context->getConstantExprUDiv(Prod, DivRHS)) != CmpRHS; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6590 | |
| 6591 | // Get the ICmp opcode |
| 6592 | ICmpInst::Predicate Pred = ICI.getPredicate(); |
| 6593 | |
| 6594 | // Figure out the interval that is being checked. For example, a comparison |
| 6595 | // like "X /u 5 == 0" is really checking that X is in the interval [0, 5). |
| 6596 | // Compute this interval based on the constants involved and the signedness of |
| 6597 | // the compare/divide. This computes a half-open interval, keeping track of |
| 6598 | // whether either value in the interval overflows. After analysis each |
| 6599 | // overflow variable is set to 0 if it's corresponding bound variable is valid |
| 6600 | // -1 if overflowed off the bottom end, or +1 if overflowed off the top end. |
| 6601 | int LoOverflow = 0, HiOverflow = 0; |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 6602 | Constant *LoBound = 0, *HiBound = 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6603 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6604 | if (!DivIsSigned) { // udiv |
| 6605 | // e.g. X/5 op 3 --> [15, 20) |
| 6606 | LoBound = Prod; |
| 6607 | HiOverflow = LoOverflow = ProdOV; |
| 6608 | if (!HiOverflow) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6609 | HiOverflow = AddWithOverflow(HiBound, LoBound, DivRHS, Context, false); |
| Dan Gohman | 5dceed1 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6610 | } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6611 | if (CmpRHSV == 0) { // (X / pos) op 0 |
| 6612 | // Can't overflow. e.g. X/2 op 0 --> [-1, 2) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6613 | LoBound = cast<ConstantInt>(Context->getConstantExprNeg(SubOne(DivRHS, |
| 6614 | Context))); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6615 | HiBound = DivRHS; |
| Dan Gohman | 5dceed1 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6616 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6617 | LoBound = Prod; // e.g. X/5 op 3 --> [15, 20) |
| 6618 | HiOverflow = LoOverflow = ProdOV; |
| 6619 | if (!HiOverflow) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6620 | HiOverflow = AddWithOverflow(HiBound, Prod, DivRHS, Context, true); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6621 | } else { // (X / pos) op neg |
| 6622 | // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6623 | HiBound = AddOne(Prod, Context); |
| Chris Lattner | bd85a5f | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6624 | LoOverflow = HiOverflow = ProdOV ? -1 : 0; |
| 6625 | if (!LoOverflow) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6626 | ConstantInt* DivNeg = |
| 6627 | cast<ConstantInt>(Context->getConstantExprNeg(DivRHS)); |
| 6628 | LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, Context, |
| Chris Lattner | bd85a5f | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6629 | true) ? -1 : 0; |
| 6630 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6631 | } |
| Dan Gohman | 5dceed1 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6632 | } else if (DivRHS->getValue().isNegative()) { // Divisor is < 0. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6633 | if (CmpRHSV == 0) { // (X / neg) op 0 |
| 6634 | // e.g. X/-5 op 0 --> [-4, 5) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6635 | LoBound = AddOne(DivRHS, Context); |
| 6636 | HiBound = cast<ConstantInt>(Context->getConstantExprNeg(DivRHS)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6637 | if (HiBound == DivRHS) { // -INTMIN = INTMIN |
| 6638 | HiOverflow = 1; // [INTMIN+1, overflow) |
| 6639 | HiBound = 0; // e.g. X/INTMIN = 0 --> X > INTMIN |
| 6640 | } |
| Dan Gohman | 5dceed1 | 2008-02-13 22:09:18 +0000 | [diff] [blame] | 6641 | } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6642 | // e.g. X/-5 op 3 --> [-19, -14) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6643 | HiBound = AddOne(Prod, Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6644 | HiOverflow = LoOverflow = ProdOV ? -1 : 0; |
| 6645 | if (!LoOverflow) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6646 | LoOverflow = AddWithOverflow(LoBound, HiBound, |
| 6647 | DivRHS, Context, true) ? -1 : 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6648 | } else { // (X / neg) op neg |
| Chris Lattner | bd85a5f | 2008-10-11 22:55:00 +0000 | [diff] [blame] | 6649 | LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20) |
| 6650 | LoOverflow = HiOverflow = ProdOV; |
| Dan Gohman | 45408ea | 2008-09-11 00:25:00 +0000 | [diff] [blame] | 6651 | if (!HiOverflow) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6652 | HiOverflow = SubWithOverflow(HiBound, Prod, DivRHS, Context, true); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6653 | } |
| 6654 | |
| 6655 | // Dividing by a negative swaps the condition. LT <-> GT |
| 6656 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 6657 | } |
| 6658 | |
| 6659 | Value *X = DivI->getOperand(0); |
| 6660 | switch (Pred) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6661 | default: llvm_unreachable("Unhandled icmp opcode!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6662 | case ICmpInst::ICMP_EQ: |
| 6663 | if (LoOverflow && HiOverflow) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6664 | return ReplaceInstUsesWith(ICI, Context->getFalse()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6665 | else if (HiOverflow) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6666 | return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE : |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6667 | ICmpInst::ICMP_UGE, X, LoBound); |
| 6668 | else if (LoOverflow) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6669 | return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT : |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6670 | ICmpInst::ICMP_ULT, X, HiBound); |
| 6671 | else |
| 6672 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, true, ICI); |
| 6673 | case ICmpInst::ICMP_NE: |
| 6674 | if (LoOverflow && HiOverflow) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6675 | return ReplaceInstUsesWith(ICI, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6676 | else if (HiOverflow) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6677 | return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SLT : |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6678 | ICmpInst::ICMP_ULT, X, LoBound); |
| 6679 | else if (LoOverflow) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6680 | return new ICmpInst(*Context, DivIsSigned ? ICmpInst::ICMP_SGE : |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6681 | ICmpInst::ICMP_UGE, X, HiBound); |
| 6682 | else |
| 6683 | return InsertRangeTest(X, LoBound, HiBound, DivIsSigned, false, ICI); |
| 6684 | case ICmpInst::ICMP_ULT: |
| 6685 | case ICmpInst::ICMP_SLT: |
| 6686 | if (LoOverflow == +1) // Low bound is greater than input range. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6687 | return ReplaceInstUsesWith(ICI, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6688 | if (LoOverflow == -1) // Low bound is less than input range. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6689 | return ReplaceInstUsesWith(ICI, Context->getFalse()); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6690 | return new ICmpInst(*Context, Pred, X, LoBound); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6691 | case ICmpInst::ICMP_UGT: |
| 6692 | case ICmpInst::ICMP_SGT: |
| 6693 | if (HiOverflow == +1) // High bound greater than input range. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6694 | return ReplaceInstUsesWith(ICI, Context->getFalse()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6695 | else if (HiOverflow == -1) // High bound less than input range. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6696 | return ReplaceInstUsesWith(ICI, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6697 | if (Pred == ICmpInst::ICMP_UGT) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6698 | return new ICmpInst(*Context, ICmpInst::ICMP_UGE, X, HiBound); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6699 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6700 | return new ICmpInst(*Context, ICmpInst::ICMP_SGE, X, HiBound); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6701 | } |
| 6702 | } |
| 6703 | |
| 6704 | |
| 6705 | /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)". |
| 6706 | /// |
| 6707 | Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 6708 | Instruction *LHSI, |
| 6709 | ConstantInt *RHS) { |
| 6710 | const APInt &RHSV = RHS->getValue(); |
| 6711 | |
| 6712 | switch (LHSI->getOpcode()) { |
| Chris Lattner | 56be123 | 2009-01-09 07:47:06 +0000 | [diff] [blame] | 6713 | case Instruction::Trunc: |
| 6714 | if (ICI.isEquality() && LHSI->hasOneUse()) { |
| 6715 | // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all |
| 6716 | // of the high bits truncated out of x are known. |
| 6717 | unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(), |
| 6718 | SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits(); |
| 6719 | APInt Mask(APInt::getHighBitsSet(SrcBits, SrcBits-DstBits)); |
| 6720 | APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0); |
| 6721 | ComputeMaskedBits(LHSI->getOperand(0), Mask, KnownZero, KnownOne); |
| 6722 | |
| 6723 | // If all the high bits are known, we can do this xform. |
| 6724 | if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) { |
| 6725 | // Pull in the high bits from known-ones set. |
| 6726 | APInt NewRHS(RHS->getValue()); |
| 6727 | NewRHS.zext(SrcBits); |
| 6728 | NewRHS |= KnownOne; |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6729 | return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6730 | Context->getConstantInt(NewRHS)); |
| Chris Lattner | 56be123 | 2009-01-09 07:47:06 +0000 | [diff] [blame] | 6731 | } |
| 6732 | } |
| 6733 | break; |
| 6734 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6735 | case Instruction::Xor: // (icmp pred (xor X, XorCST), CI) |
| 6736 | if (ConstantInt *XorCST = dyn_cast<ConstantInt>(LHSI->getOperand(1))) { |
| 6737 | // If this is a comparison that tests the signbit (X < 0) or (x > -1), |
| 6738 | // fold the xor. |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 6739 | if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) || |
| 6740 | (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6741 | Value *CompareVal = LHSI->getOperand(0); |
| 6742 | |
| 6743 | // If the sign bit of the XorCST is not set, there is no change to |
| 6744 | // the operation, just stop using the Xor. |
| 6745 | if (!XorCST->getValue().isNegative()) { |
| 6746 | ICI.setOperand(0, CompareVal); |
| 6747 | AddToWorkList(LHSI); |
| 6748 | return &ICI; |
| 6749 | } |
| 6750 | |
| 6751 | // Was the old condition true if the operand is positive? |
| 6752 | bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT; |
| 6753 | |
| 6754 | // If so, the new one isn't. |
| 6755 | isTrueIfPositive ^= true; |
| 6756 | |
| 6757 | if (isTrueIfPositive) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6758 | return new ICmpInst(*Context, ICmpInst::ICMP_SGT, CompareVal, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6759 | SubOne(RHS, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6760 | else |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6761 | return new ICmpInst(*Context, ICmpInst::ICMP_SLT, CompareVal, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6762 | AddOne(RHS, Context)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6763 | } |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6764 | |
| 6765 | if (LHSI->hasOneUse()) { |
| 6766 | // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit)) |
| 6767 | if (!ICI.isEquality() && XorCST->getValue().isSignBit()) { |
| 6768 | const APInt &SignBit = XorCST->getValue(); |
| 6769 | ICmpInst::Predicate Pred = ICI.isSignedPredicate() |
| 6770 | ? ICI.getUnsignedPredicate() |
| 6771 | : ICI.getSignedPredicate(); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6772 | return new ICmpInst(*Context, Pred, LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6773 | Context->getConstantInt(RHSV ^ SignBit)); |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A) |
| Chris Lattner | f3b445e | 2009-02-02 07:15:30 +0000 | [diff] [blame] | 6777 | if (!ICI.isEquality() && XorCST->getValue().isMaxSignedValue()) { |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6778 | const APInt &NotSignBit = XorCST->getValue(); |
| 6779 | ICmpInst::Predicate Pred = ICI.isSignedPredicate() |
| 6780 | ? ICI.getUnsignedPredicate() |
| 6781 | : ICI.getSignedPredicate(); |
| 6782 | Pred = ICI.getSwappedPredicate(Pred); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6783 | return new ICmpInst(*Context, Pred, LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6784 | Context->getConstantInt(RHSV ^ NotSignBit)); |
| Nick Lewycky | dac8433 | 2009-01-31 21:30:05 +0000 | [diff] [blame] | 6785 | } |
| 6786 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6787 | } |
| 6788 | break; |
| 6789 | case Instruction::And: // (icmp pred (and X, AndCST), RHS) |
| 6790 | if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) && |
| 6791 | LHSI->getOperand(0)->hasOneUse()) { |
| 6792 | ConstantInt *AndCST = cast<ConstantInt>(LHSI->getOperand(1)); |
| 6793 | |
| 6794 | // If the LHS is an AND of a truncating cast, we can widen the |
| 6795 | // and/compare to be the input width without changing the value |
| 6796 | // produced, eliminating a cast. |
| 6797 | if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) { |
| 6798 | // We can do this transformation if either the AND constant does not |
| 6799 | // have its sign bit set or if it is an equality comparison. |
| 6800 | // Extending a relational comparison when we're checking the sign |
| 6801 | // bit would not work. |
| 6802 | if (Cast->hasOneUse() && |
| Anton Korobeynikov | 6a4a933 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 6803 | (ICI.isEquality() || |
| 6804 | (AndCST->getValue().isNonNegative() && RHSV.isNonNegative()))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6805 | uint32_t BitWidth = |
| 6806 | cast<IntegerType>(Cast->getOperand(0)->getType())->getBitWidth(); |
| 6807 | APInt NewCST = AndCST->getValue(); |
| 6808 | NewCST.zext(BitWidth); |
| 6809 | APInt NewCI = RHSV; |
| 6810 | NewCI.zext(BitWidth); |
| 6811 | Instruction *NewAnd = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6812 | BinaryOperator::CreateAnd(Cast->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6813 | Context->getConstantInt(NewCST),LHSI->getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6814 | InsertNewInstBefore(NewAnd, ICI); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6815 | return new ICmpInst(*Context, ICI.getPredicate(), NewAnd, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6816 | Context->getConstantInt(NewCI)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6817 | } |
| 6818 | } |
| 6819 | |
| 6820 | // If this is: (X >> C1) & C2 != C3 (where any shift and any compare |
| 6821 | // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This |
| 6822 | // happens a LOT in code produced by the C front-end, for bitfield |
| 6823 | // access. |
| 6824 | BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0)); |
| 6825 | if (Shift && !Shift->isShift()) |
| 6826 | Shift = 0; |
| 6827 | |
| 6828 | ConstantInt *ShAmt; |
| 6829 | ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : 0; |
| 6830 | const Type *Ty = Shift ? Shift->getType() : 0; // Type of the shift. |
| 6831 | const Type *AndTy = AndCST->getType(); // Type of the and. |
| 6832 | |
| 6833 | // We can fold this as long as we can't shift unknown bits |
| 6834 | // into the mask. This can only happen with signed shift |
| 6835 | // rights, as they sign-extend. |
| 6836 | if (ShAmt) { |
| 6837 | bool CanFold = Shift->isLogicalShift(); |
| 6838 | if (!CanFold) { |
| 6839 | // To test for the bad case of the signed shr, see if any |
| 6840 | // of the bits shifted in could be tested after the mask. |
| 6841 | uint32_t TyBits = Ty->getPrimitiveSizeInBits(); |
| 6842 | int ShAmtVal = TyBits - ShAmt->getLimitedValue(TyBits); |
| 6843 | |
| 6844 | uint32_t BitWidth = AndTy->getPrimitiveSizeInBits(); |
| 6845 | if ((APInt::getHighBitsSet(BitWidth, BitWidth-ShAmtVal) & |
| 6846 | AndCST->getValue()) == 0) |
| 6847 | CanFold = true; |
| 6848 | } |
| 6849 | |
| 6850 | if (CanFold) { |
| 6851 | Constant *NewCst; |
| 6852 | if (Shift->getOpcode() == Instruction::Shl) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6853 | NewCst = Context->getConstantExprLShr(RHS, ShAmt); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6854 | else |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6855 | NewCst = Context->getConstantExprShl(RHS, ShAmt); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6856 | |
| 6857 | // Check to see if we are shifting out any of the bits being |
| 6858 | // compared. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6859 | if (Context->getConstantExpr(Shift->getOpcode(), |
| 6860 | NewCst, ShAmt) != RHS) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6861 | // If we shifted bits out, the fold is not going to work out. |
| 6862 | // As a special case, check to see if this means that the |
| 6863 | // result is always true or false now. |
| 6864 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6865 | return ReplaceInstUsesWith(ICI, Context->getFalse()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6866 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 6867 | return ReplaceInstUsesWith(ICI, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6868 | } else { |
| 6869 | ICI.setOperand(1, NewCst); |
| 6870 | Constant *NewAndCST; |
| 6871 | if (Shift->getOpcode() == Instruction::Shl) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6872 | NewAndCST = Context->getConstantExprLShr(AndCST, ShAmt); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6873 | else |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6874 | NewAndCST = Context->getConstantExprShl(AndCST, ShAmt); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6875 | LHSI->setOperand(1, NewAndCST); |
| 6876 | LHSI->setOperand(0, Shift->getOperand(0)); |
| 6877 | AddToWorkList(Shift); // Shift is dead. |
| 6878 | AddUsesToWorkList(ICI); |
| 6879 | return &ICI; |
| 6880 | } |
| 6881 | } |
| 6882 | } |
| 6883 | |
| 6884 | // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is |
| 6885 | // preferable because it allows the C<<Y expression to be hoisted out |
| 6886 | // of a loop if Y is invariant and X is not. |
| 6887 | if (Shift && Shift->hasOneUse() && RHSV == 0 && |
| Chris Lattner | ffd9526 | 2009-03-25 00:28:58 +0000 | [diff] [blame] | 6888 | ICI.isEquality() && !Shift->isArithmeticShift() && |
| 6889 | !isa<Constant>(Shift->getOperand(0))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6890 | // Compute C << Y. |
| 6891 | Value *NS; |
| 6892 | if (Shift->getOpcode() == Instruction::LShr) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6893 | NS = BinaryOperator::CreateShl(AndCST, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6894 | Shift->getOperand(1), "tmp"); |
| 6895 | } else { |
| 6896 | // Insert a logical shift. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6897 | NS = BinaryOperator::CreateLShr(AndCST, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6898 | Shift->getOperand(1), "tmp"); |
| 6899 | } |
| 6900 | InsertNewInstBefore(cast<Instruction>(NS), ICI); |
| 6901 | |
| 6902 | // Compute X & (C << Y). |
| 6903 | Instruction *NewAnd = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6904 | BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6905 | InsertNewInstBefore(NewAnd, ICI); |
| 6906 | |
| 6907 | ICI.setOperand(0, NewAnd); |
| 6908 | return &ICI; |
| 6909 | } |
| 6910 | } |
| 6911 | break; |
| 6912 | |
| 6913 | case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI) |
| 6914 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 6915 | if (!ShAmt) break; |
| 6916 | |
| 6917 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6918 | |
| 6919 | // Check that the shift amount is in range. If not, don't perform |
| 6920 | // undefined shifts. When the shift is visited it will be |
| 6921 | // simplified. |
| 6922 | if (ShAmt->uge(TypeBits)) |
| 6923 | break; |
| 6924 | |
| 6925 | if (ICI.isEquality()) { |
| 6926 | // If we are comparing against bits always shifted out, the |
| 6927 | // comparison cannot succeed. |
| 6928 | Constant *Comp = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6929 | Context->getConstantExprShl(Context->getConstantExprLShr(RHS, ShAmt), |
| 6930 | ShAmt); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6931 | if (Comp != RHS) {// Comparing against a bit that we know is zero. |
| 6932 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6933 | Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6934 | return ReplaceInstUsesWith(ICI, Cst); |
| 6935 | } |
| 6936 | |
| 6937 | if (LHSI->hasOneUse()) { |
| 6938 | // Otherwise strength reduce the shift into an and. |
| 6939 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| 6940 | Constant *Mask = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6941 | Context->getConstantInt(APInt::getLowBitsSet(TypeBits, |
| 6942 | TypeBits-ShAmtVal)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6943 | |
| 6944 | Instruction *AndI = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6945 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6946 | Mask, LHSI->getName()+".mask"); |
| 6947 | Value *And = InsertNewInstBefore(AndI, ICI); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6948 | return new ICmpInst(*Context, ICI.getPredicate(), And, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6949 | Context->getConstantInt(RHSV.lshr(ShAmtVal))); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6950 | } |
| 6951 | } |
| 6952 | |
| 6953 | // Otherwise, if this is a comparison of the sign bit, simplify to and/test. |
| 6954 | bool TrueIfSigned = false; |
| 6955 | if (LHSI->hasOneUse() && |
| 6956 | isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) { |
| 6957 | // (X << 31) <s 0 --> (X&1) != 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6958 | Constant *Mask = Context->getConstantInt(APInt(TypeBits, 1) << |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6959 | (TypeBits-ShAmt->getZExtValue()-1)); |
| 6960 | Instruction *AndI = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 6961 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6962 | Mask, LHSI->getName()+".mask"); |
| 6963 | Value *And = InsertNewInstBefore(AndI, ICI); |
| 6964 | |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 6965 | return new ICmpInst(*Context, |
| 6966 | TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6967 | And, Context->getNullValue(And->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6968 | } |
| 6969 | break; |
| 6970 | } |
| 6971 | |
| 6972 | case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI) |
| 6973 | case Instruction::AShr: { |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6974 | // Only handle equality comparisons of shift-by-constant. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6975 | ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6976 | if (!ShAmt || !ICI.isEquality()) break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6977 | |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6978 | // Check that the shift amount is in range. If not, don't perform |
| 6979 | // undefined shifts. When the shift is visited it will be |
| 6980 | // simplified. |
| 6981 | uint32_t TypeBits = RHSV.getBitWidth(); |
| 6982 | if (ShAmt->uge(TypeBits)) |
| 6983 | break; |
| 6984 | |
| 6985 | uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 6986 | |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6987 | // If we are comparing against bits always shifted out, the |
| 6988 | // comparison cannot succeed. |
| 6989 | APInt Comp = RHSV << ShAmtVal; |
| 6990 | if (LHSI->getOpcode() == Instruction::LShr) |
| 6991 | Comp = Comp.lshr(ShAmtVal); |
| 6992 | else |
| 6993 | Comp = Comp.ashr(ShAmtVal); |
| 6994 | |
| 6995 | if (Comp != RHSV) { // Comparing against a bit that we know is zero. |
| 6996 | bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 6997 | Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE); |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 6998 | return ReplaceInstUsesWith(ICI, Cst); |
| 6999 | } |
| 7000 | |
| 7001 | // Otherwise, check to see if the bits shifted out are known to be zero. |
| 7002 | // If so, we can compare against the unshifted value: |
| 7003 | // (X & 4) >> 1 == 2 --> (X & 4) == 4. |
| Evan Cheng | fb9292a | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 7004 | if (LHSI->hasOneUse() && |
| 7005 | MaskedValueIsZero(LHSI->getOperand(0), |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 7006 | APInt::getLowBitsSet(Comp.getBitWidth(), ShAmtVal))) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7007 | return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7008 | Context->getConstantExprShl(RHS, ShAmt)); |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 7009 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7010 | |
| Evan Cheng | fb9292a | 2008-04-23 00:38:06 +0000 | [diff] [blame] | 7011 | if (LHSI->hasOneUse()) { |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 7012 | // Otherwise strength reduce the shift into an and. |
| 7013 | APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7014 | Constant *Mask = Context->getConstantInt(Val); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7015 | |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 7016 | Instruction *AndI = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7017 | BinaryOperator::CreateAnd(LHSI->getOperand(0), |
| Chris Lattner | 5ee84f8 | 2008-03-21 05:19:58 +0000 | [diff] [blame] | 7018 | Mask, LHSI->getName()+".mask"); |
| 7019 | Value *And = InsertNewInstBefore(AndI, ICI); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7020 | return new ICmpInst(*Context, ICI.getPredicate(), And, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7021 | Context->getConstantExprShl(RHS, ShAmt)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7022 | } |
| 7023 | break; |
| 7024 | } |
| 7025 | |
| 7026 | case Instruction::SDiv: |
| 7027 | case Instruction::UDiv: |
| 7028 | // Fold: icmp pred ([us]div X, C1), C2 -> range test |
| 7029 | // Fold this div into the comparison, producing a range check. |
| 7030 | // Determine, based on the divide type, what the range is being |
| 7031 | // checked. If there is an overflow on the low or high side, remember |
| 7032 | // it, otherwise compute the range [low, hi) bounding the new value. |
| 7033 | // See: InsertRangeTest above for the kinds of replacements possible. |
| 7034 | if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1))) |
| 7035 | if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI), |
| 7036 | DivRHS)) |
| 7037 | return R; |
| 7038 | break; |
| Nick Lewycky | 0185bbf | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 7039 | |
| 7040 | case Instruction::Add: |
| 7041 | // Fold: icmp pred (add, X, C1), C2 |
| 7042 | |
| 7043 | if (!ICI.isEquality()) { |
| 7044 | ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1)); |
| 7045 | if (!LHSC) break; |
| 7046 | const APInt &LHSV = LHSC->getValue(); |
| 7047 | |
| 7048 | ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV) |
| 7049 | .subtract(LHSV); |
| 7050 | |
| 7051 | if (ICI.isSignedPredicate()) { |
| 7052 | if (CR.getLower().isSignBit()) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7053 | return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7054 | Context->getConstantInt(CR.getUpper())); |
| Nick Lewycky | 0185bbf | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 7055 | } else if (CR.getUpper().isSignBit()) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7056 | return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7057 | Context->getConstantInt(CR.getLower())); |
| Nick Lewycky | 0185bbf | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 7058 | } |
| 7059 | } else { |
| 7060 | if (CR.getLower().isMinValue()) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7061 | return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7062 | Context->getConstantInt(CR.getUpper())); |
| Nick Lewycky | 0185bbf | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 7063 | } else if (CR.getUpper().isMinValue()) { |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7064 | return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7065 | Context->getConstantInt(CR.getLower())); |
| Nick Lewycky | 0185bbf | 2008-02-03 16:33:09 +0000 | [diff] [blame] | 7066 | } |
| 7067 | } |
| 7068 | } |
| 7069 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7070 | } |
| 7071 | |
| 7072 | // Simplify icmp_eq and icmp_ne instructions with integer constant RHS. |
| 7073 | if (ICI.isEquality()) { |
| 7074 | bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE; |
| 7075 | |
| 7076 | // If the first operand is (add|sub|and|or|xor|rem) with a constant, and |
| 7077 | // the second operand is a constant, simplify a bit. |
| 7078 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) { |
| 7079 | switch (BO->getOpcode()) { |
| 7080 | case Instruction::SRem: |
| 7081 | // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. |
| 7082 | if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){ |
| 7083 | const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue(); |
| 7084 | if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) { |
| 7085 | Instruction *NewRem = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7086 | BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7087 | BO->getName()); |
| 7088 | InsertNewInstBefore(NewRem, ICI); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7089 | return new ICmpInst(*Context, ICI.getPredicate(), NewRem, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7090 | Context->getNullValue(BO->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7091 | } |
| 7092 | } |
| 7093 | break; |
| 7094 | case Instruction::Add: |
| 7095 | // Replace ((add A, B) != C) with (A != C-B) if B & C are constants. |
| 7096 | if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 7097 | if (BO->hasOneUse()) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7098 | return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7099 | Context->getConstantExprSub(RHS, BOp1C)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7100 | } else if (RHSV == 0) { |
| 7101 | // Replace ((add A, B) != 0) with (A != -B) if A or B is |
| 7102 | // efficiently invertible, or if the add has just this one use. |
| 7103 | Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1); |
| 7104 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7105 | if (Value *NegVal = dyn_castNegVal(BOp1, Context)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7106 | return new ICmpInst(*Context, ICI.getPredicate(), BOp0, NegVal); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7107 | else if (Value *NegVal = dyn_castNegVal(BOp0, Context)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7108 | return new ICmpInst(*Context, ICI.getPredicate(), NegVal, BOp1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7109 | else if (BO->hasOneUse()) { |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 7110 | Instruction *Neg = BinaryOperator::CreateNeg(*Context, BOp1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7111 | InsertNewInstBefore(Neg, ICI); |
| 7112 | Neg->takeName(BO); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7113 | return new ICmpInst(*Context, ICI.getPredicate(), BOp0, Neg); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7114 | } |
| 7115 | } |
| 7116 | break; |
| 7117 | case Instruction::Xor: |
| 7118 | // For the xor case, we can xor two constants together, eliminating |
| 7119 | // the explicit xor. |
| 7120 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7121 | return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7122 | Context->getConstantExprXor(RHS, BOC)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7123 | |
| 7124 | // FALLTHROUGH |
| 7125 | case Instruction::Sub: |
| 7126 | // Replace (([sub|xor] A, B) != 0) with (A != B) |
| 7127 | if (RHSV == 0) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7128 | return new ICmpInst(*Context, ICI.getPredicate(), BO->getOperand(0), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7129 | BO->getOperand(1)); |
| 7130 | break; |
| 7131 | |
| 7132 | case Instruction::Or: |
| 7133 | // If bits are being or'd in that are not present in the constant we |
| 7134 | // are comparing against, then the comparison could never succeed! |
| 7135 | if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7136 | Constant *NotCI = Context->getConstantExprNot(RHS); |
| 7137 | if (!Context->getConstantExprAnd(BOC, NotCI)->isNullValue()) |
| 7138 | return ReplaceInstUsesWith(ICI, |
| 7139 | Context->getConstantInt(Type::Int1Ty, |
| 7140 | isICMP_NE)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7141 | } |
| 7142 | break; |
| 7143 | |
| 7144 | case Instruction::And: |
| 7145 | if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) { |
| 7146 | // If bits are being compared against that are and'd out, then the |
| 7147 | // comparison can never succeed! |
| 7148 | if ((RHSV & ~BOC->getValue()) != 0) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7149 | return ReplaceInstUsesWith(ICI, |
| 7150 | Context->getConstantInt(Type::Int1Ty, |
| 7151 | isICMP_NE)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7152 | |
| 7153 | // If we have ((X & C) == C), turn it into ((X & C) != 0). |
| 7154 | if (RHS == BOC && RHSV.isPowerOf2()) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7155 | return new ICmpInst(*Context, isICMP_NE ? ICmpInst::ICMP_EQ : |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7156 | ICmpInst::ICMP_NE, LHSI, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7157 | Context->getNullValue(RHS->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7158 | |
| 7159 | // Replace (and X, (1 << size(X)-1) != 0) with x s< 0 |
| Chris Lattner | 60813c2 | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 7160 | if (BOC->getValue().isSignBit()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7161 | Value *X = BO->getOperand(0); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7162 | Constant *Zero = Context->getNullValue(X->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7163 | ICmpInst::Predicate pred = isICMP_NE ? |
| 7164 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE; |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7165 | return new ICmpInst(*Context, pred, X, Zero); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | // ((X & ~7) == 0) --> X < 8 |
| 7169 | if (RHSV == 0 && isHighOnes(BOC)) { |
| 7170 | Value *X = BO->getOperand(0); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7171 | Constant *NegX = Context->getConstantExprNeg(BOC); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7172 | ICmpInst::Predicate pred = isICMP_NE ? |
| 7173 | ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT; |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7174 | return new ICmpInst(*Context, pred, X, NegX); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7175 | } |
| 7176 | } |
| 7177 | default: break; |
| 7178 | } |
| 7179 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) { |
| 7180 | // Handle icmp {eq|ne} <intrinsic>, intcst. |
| 7181 | if (II->getIntrinsicID() == Intrinsic::bswap) { |
| 7182 | AddToWorkList(II); |
| 7183 | ICI.setOperand(0, II->getOperand(1)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7184 | ICI.setOperand(1, Context->getConstantInt(RHSV.byteSwap())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7185 | return &ICI; |
| 7186 | } |
| 7187 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7188 | } |
| 7189 | return 0; |
| 7190 | } |
| 7191 | |
| 7192 | /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst). |
| 7193 | /// We only handle extending casts so far. |
| 7194 | /// |
| 7195 | Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) { |
| 7196 | const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0)); |
| 7197 | Value *LHSCIOp = LHSCI->getOperand(0); |
| 7198 | const Type *SrcTy = LHSCIOp->getType(); |
| 7199 | const Type *DestTy = LHSCI->getType(); |
| 7200 | Value *RHSCIOp; |
| 7201 | |
| 7202 | // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the |
| 7203 | // integer type is the same size as the pointer type. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7204 | if (TD && LHSCI->getOpcode() == Instruction::PtrToInt && |
| 7205 | TD->getPointerSizeInBits() == |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7206 | cast<IntegerType>(DestTy)->getBitWidth()) { |
| 7207 | Value *RHSOp = 0; |
| 7208 | if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7209 | RHSOp = Context->getConstantExprIntToPtr(RHSC, SrcTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7210 | } else if (PtrToIntInst *RHSC = dyn_cast<PtrToIntInst>(ICI.getOperand(1))) { |
| 7211 | RHSOp = RHSC->getOperand(0); |
| 7212 | // If the pointer types don't match, insert a bitcast. |
| 7213 | if (LHSCIOp->getType() != RHSOp->getType()) |
| Chris Lattner | 13c2d6e | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 7214 | RHSOp = InsertBitCastBefore(RHSOp, LHSCIOp->getType(), ICI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
| 7217 | if (RHSOp) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7218 | return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSOp); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7219 | } |
| 7220 | |
| 7221 | // The code below only handles extension cast instructions, so far. |
| 7222 | // Enforce this. |
| 7223 | if (LHSCI->getOpcode() != Instruction::ZExt && |
| 7224 | LHSCI->getOpcode() != Instruction::SExt) |
| 7225 | return 0; |
| 7226 | |
| 7227 | bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt; |
| 7228 | bool isSignedCmp = ICI.isSignedPredicate(); |
| 7229 | |
| 7230 | if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) { |
| 7231 | // Not an extension from the same type? |
| 7232 | RHSCIOp = CI->getOperand(0); |
| 7233 | if (RHSCIOp->getType() != LHSCIOp->getType()) |
| 7234 | return 0; |
| 7235 | |
| Nick Lewycky | d4264dc | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 7236 | // If the signedness of the two casts doesn't agree (i.e. one is a sext |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7237 | // and the other is a zext), then we can't handle this. |
| 7238 | if (CI->getOpcode() != LHSCI->getOpcode()) |
| 7239 | return 0; |
| 7240 | |
| Nick Lewycky | d4264dc | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 7241 | // Deal with equality cases early. |
| 7242 | if (ICI.isEquality()) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7243 | return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| Nick Lewycky | d4264dc | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 7244 | |
| 7245 | // A signed comparison of sign extended values simplifies into a |
| 7246 | // signed comparison. |
| 7247 | if (isSignedCmp && isSignedExt) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7248 | return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, RHSCIOp); |
| Nick Lewycky | d4264dc | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 7249 | |
| 7250 | // The other three cases all fold into an unsigned comparison. |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7251 | return new ICmpInst(*Context, ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7252 | } |
| 7253 | |
| 7254 | // If we aren't dealing with a constant on the RHS, exit early |
| 7255 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1)); |
| 7256 | if (!CI) |
| 7257 | return 0; |
| 7258 | |
| 7259 | // Compute the constant that would happen if we truncated to SrcTy then |
| 7260 | // reextended to DestTy. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7261 | Constant *Res1 = Context->getConstantExprTrunc(CI, SrcTy); |
| 7262 | Constant *Res2 = Context->getConstantExprCast(LHSCI->getOpcode(), |
| 7263 | Res1, DestTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7264 | |
| 7265 | // If the re-extended constant didn't change... |
| 7266 | if (Res2 == CI) { |
| 7267 | // Make sure that sign of the Cmp and the sign of the Cast are the same. |
| 7268 | // For example, we might have: |
| Dan Gohman | 9e1657f | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 7269 | // %A = sext i16 %X to i32 |
| 7270 | // %B = icmp ugt i32 %A, 1330 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7271 | // It is incorrect to transform this into |
| Dan Gohman | 9e1657f | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 7272 | // %B = icmp ugt i16 %X, 1330 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7273 | // because %A may have negative value. |
| 7274 | // |
| Chris Lattner | 3d81653 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 7275 | // However, we allow this when the compare is EQ/NE, because they are |
| 7276 | // signless. |
| 7277 | if (isSignedExt == isSignedCmp || ICI.isEquality()) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7278 | return new ICmpInst(*Context, ICI.getPredicate(), LHSCIOp, Res1); |
| Chris Lattner | 3d81653 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 7279 | return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
| 7282 | // The re-extended constant changed so the constant cannot be represented |
| 7283 | // in the shorter type. Consequently, we cannot emit a simple comparison. |
| 7284 | |
| 7285 | // First, handle some easy cases. We know the result cannot be equal at this |
| 7286 | // point so handle the ICI.isEquality() cases |
| 7287 | if (ICI.getPredicate() == ICmpInst::ICMP_EQ) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 7288 | return ReplaceInstUsesWith(ICI, Context->getFalse()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7289 | if (ICI.getPredicate() == ICmpInst::ICMP_NE) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 7290 | return ReplaceInstUsesWith(ICI, Context->getTrue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7291 | |
| 7292 | // Evaluate the comparison for LT (we invert for GT below). LE and GE cases |
| 7293 | // should have been folded away previously and not enter in here. |
| 7294 | Value *Result; |
| 7295 | if (isSignedCmp) { |
| 7296 | // We're performing a signed comparison. |
| 7297 | if (cast<ConstantInt>(CI)->getValue().isNegative()) |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 7298 | Result = Context->getFalse(); // X < (small) --> false |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7299 | else |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 7300 | Result = Context->getTrue(); // X < (large) --> true |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7301 | } else { |
| 7302 | // We're performing an unsigned comparison. |
| 7303 | if (isSignedExt) { |
| 7304 | // We're performing an unsigned comp with a sign extended value. |
| 7305 | // This is true if the input is >= 0. [aka >s -1] |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 7306 | Constant *NegOne = Context->getAllOnesValue(SrcTy); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 7307 | Result = InsertNewInstBefore(new ICmpInst(*Context, ICmpInst::ICMP_SGT, |
| 7308 | LHSCIOp, NegOne, ICI.getName()), ICI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7309 | } else { |
| 7310 | // Unsigned extend & unsigned compare -> always true. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 7311 | Result = Context->getTrue(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7312 | } |
| 7313 | } |
| 7314 | |
| 7315 | // Finally, return the value computed. |
| 7316 | if (ICI.getPredicate() == ICmpInst::ICMP_ULT || |
| Chris Lattner | 3d81653 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 7317 | ICI.getPredicate() == ICmpInst::ICMP_SLT) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7318 | return ReplaceInstUsesWith(ICI, Result); |
| Chris Lattner | 3d81653 | 2008-07-11 04:09:09 +0000 | [diff] [blame] | 7319 | |
| 7320 | assert((ICI.getPredicate()==ICmpInst::ICMP_UGT || |
| 7321 | ICI.getPredicate()==ICmpInst::ICMP_SGT) && |
| 7322 | "ICmp should be folded!"); |
| 7323 | if (Constant *CI = dyn_cast<Constant>(Result)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7324 | return ReplaceInstUsesWith(ICI, Context->getConstantExprNot(CI)); |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 7325 | return BinaryOperator::CreateNot(*Context, Result); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
| 7328 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
| 7329 | return commonShiftTransforms(I); |
| 7330 | } |
| 7331 | |
| 7332 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
| 7333 | return commonShiftTransforms(I); |
| 7334 | } |
| 7335 | |
| 7336 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
| Chris Lattner | e3c504f | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 7337 | if (Instruction *R = commonShiftTransforms(I)) |
| 7338 | return R; |
| 7339 | |
| 7340 | Value *Op0 = I.getOperand(0); |
| 7341 | |
| 7342 | // ashr int -1, X = -1 (for any arithmetic shift rights of ~0) |
| 7343 | if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0)) |
| 7344 | if (CSI->isAllOnesValue()) |
| 7345 | return ReplaceInstUsesWith(I, CSI); |
| Dan Gohman | 843649e | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 7346 | |
| Dan Gohman | 2526aea | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 7347 | // See if we can turn a signed shr into an unsigned shr. |
| 7348 | if (MaskedValueIsZero(Op0, |
| 7349 | APInt::getSignBit(I.getType()->getScalarSizeInBits()))) |
| 7350 | return BinaryOperator::CreateLShr(Op0, I.getOperand(1)); |
| 7351 | |
| 7352 | // Arithmetic shifting an all-sign-bit value is a no-op. |
| 7353 | unsigned NumSignBits = ComputeNumSignBits(Op0); |
| 7354 | if (NumSignBits == Op0->getType()->getScalarSizeInBits()) |
| 7355 | return ReplaceInstUsesWith(I, Op0); |
| Dan Gohman | 843649e | 2009-02-24 02:00:40 +0000 | [diff] [blame] | 7356 | |
| Chris Lattner | e3c504f | 2007-12-06 01:59:46 +0000 | [diff] [blame] | 7357 | return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7358 | } |
| 7359 | |
| 7360 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
| 7361 | assert(I.getOperand(1)->getType() == I.getOperand(0)->getType()); |
| 7362 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 7363 | |
| 7364 | // shl X, 0 == X and shr X, 0 == X |
| 7365 | // shl 0, X == 0 and shr 0, X == 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7366 | if (Op1 == Context->getNullValue(Op1->getType()) || |
| 7367 | Op0 == Context->getNullValue(Op0->getType())) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7368 | return ReplaceInstUsesWith(I, Op0); |
| 7369 | |
| 7370 | if (isa<UndefValue>(Op0)) { |
| 7371 | if (I.getOpcode() == Instruction::AShr) // undef >>s X -> undef |
| 7372 | return ReplaceInstUsesWith(I, Op0); |
| 7373 | else // undef << X -> 0, undef >>u X -> 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7374 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7375 | } |
| 7376 | if (isa<UndefValue>(Op1)) { |
| 7377 | if (I.getOpcode() == Instruction::AShr) // X >>s undef -> X |
| 7378 | return ReplaceInstUsesWith(I, Op0); |
| 7379 | else // X << undef, X >>u undef -> 0 |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7380 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
| Dan Gohman | 2bc2156 | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 7383 | // See if we can fold away this shift. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7384 | if (SimplifyDemandedInstructionBits(I)) |
| Dan Gohman | 2bc2156 | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 7385 | return &I; |
| 7386 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7387 | // Try to fold constant and into select arguments. |
| 7388 | if (isa<Constant>(Op0)) |
| 7389 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 7390 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 7391 | return R; |
| 7392 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7393 | if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1)) |
| 7394 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 7395 | return Res; |
| 7396 | return 0; |
| 7397 | } |
| 7398 | |
| 7399 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
| 7400 | BinaryOperator &I) { |
| Chris Lattner | 0881733 | 2009-01-31 08:24:16 +0000 | [diff] [blame] | 7401 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7402 | |
| 7403 | // See if we can simplify any instructions used by the instruction whose sole |
| 7404 | // purpose is to compute bits we don't care about. |
| Dan Gohman | 2526aea | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 7405 | uint32_t TypeBits = Op0->getType()->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7406 | |
| Dan Gohman | 9e1657f | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 7407 | // shl i32 X, 32 = 0 and srl i8 Y, 9 = 0, ... just don't eliminate |
| 7408 | // a signed shift. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7409 | // |
| 7410 | if (Op1->uge(TypeBits)) { |
| 7411 | if (I.getOpcode() != Instruction::AShr) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7412 | return ReplaceInstUsesWith(I, Context->getNullValue(Op0->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7413 | else { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7414 | I.setOperand(1, Context->getConstantInt(I.getType(), TypeBits-1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7415 | return &I; |
| 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 7420 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 7421 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 7422 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7423 | return BinaryOperator::CreateMul(BO->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7424 | Context->getConstantExprShl(BOOp, Op1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7425 | |
| 7426 | // Try to fold constant and into select arguments. |
| 7427 | if (SelectInst *SI = dyn_cast<SelectInst>(Op0)) |
| 7428 | if (Instruction *R = FoldOpIntoSelect(I, SI, this)) |
| 7429 | return R; |
| 7430 | if (isa<PHINode>(Op0)) |
| 7431 | if (Instruction *NV = FoldOpIntoPhi(I)) |
| 7432 | return NV; |
| 7433 | |
| Chris Lattner | c6d1f64 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7434 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 7435 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 7436 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 7437 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 7438 | // place. Don't try to do this transformation in this case. Also, we |
| 7439 | // require that the input operand is a shift-by-constant so that we have |
| 7440 | // confidence that the shifts will get folded together. We could do this |
| 7441 | // xform in more cases, but it is unlikely to be profitable. |
| 7442 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
| 7443 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 7444 | // Okay, we'll do this xform. Make the shift of shift. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7445 | Constant *ShAmt = Context->getConstantExprZExt(Op1, TrOp->getType()); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7446 | Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt, |
| Chris Lattner | c6d1f64 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7447 | I.getName()); |
| 7448 | InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2) |
| 7449 | |
| 7450 | // For logical shifts, the truncation has the effect of making the high |
| 7451 | // part of the register be zeros. Emulate this by inserting an AND to |
| 7452 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 7453 | // other xforms later if dead. |
| Dan Gohman | 2526aea | 2009-06-16 19:55:29 +0000 | [diff] [blame] | 7454 | unsigned SrcSize = TrOp->getType()->getScalarSizeInBits(); |
| 7455 | unsigned DstSize = TI->getType()->getScalarSizeInBits(); |
| Chris Lattner | c6d1f64 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7456 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
| 7457 | |
| 7458 | // The mask we constructed says what the trunc would do if occurring |
| 7459 | // between the shifts. We want to know the effect *after* the second |
| 7460 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 7461 | // mask as appropriate. |
| 7462 | if (I.getOpcode() == Instruction::Shl) |
| 7463 | MaskV <<= Op1->getZExtValue(); |
| 7464 | else { |
| 7465 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
| 7466 | MaskV = MaskV.lshr(Op1->getZExtValue()); |
| 7467 | } |
| 7468 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7469 | Instruction *And = |
| 7470 | BinaryOperator::CreateAnd(NSh, Context->getConstantInt(MaskV), |
| 7471 | TI->getName()); |
| Chris Lattner | c6d1f64 | 2007-12-22 09:07:47 +0000 | [diff] [blame] | 7472 | InsertNewInstBefore(And, I); // shift1 & 0x00FF |
| 7473 | |
| 7474 | // Return the value truncated to the interesting size. |
| 7475 | return new TruncInst(And, I.getType()); |
| 7476 | } |
| 7477 | } |
| 7478 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7479 | if (Op0->hasOneUse()) { |
| 7480 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 7481 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 7482 | Value *V1, *V2; |
| 7483 | ConstantInt *CC; |
| 7484 | switch (Op0BO->getOpcode()) { |
| 7485 | default: break; |
| 7486 | case Instruction::Add: |
| 7487 | case Instruction::And: |
| 7488 | case Instruction::Or: |
| 7489 | case Instruction::Xor: { |
| 7490 | // These operators commute. |
| 7491 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
| 7492 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 7493 | match(Op0BO->getOperand(1), m_Shr(m_Value(V1), |
| 7494 | m_Specific(Op1)), *Context)){ |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7495 | Instruction *YS = BinaryOperator::CreateShl( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7496 | Op0BO->getOperand(0), Op1, |
| 7497 | Op0BO->getName()); |
| 7498 | InsertNewInstBefore(YS, I); // (Y << C) |
| 7499 | Instruction *X = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7500 | BinaryOperator::Create(Op0BO->getOpcode(), YS, V1, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7501 | Op0BO->getOperand(1)->getName()); |
| 7502 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 7503 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7504 | return BinaryOperator::CreateAnd(X, Context->getConstantInt( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7505 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
| 7506 | } |
| 7507 | |
| 7508 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 7509 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
| 7510 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
| 7511 | match(Op0BOOp1, |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 7512 | m_And(m_Shr(m_Value(V1), m_Specific(Op1)), |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 7513 | m_ConstantInt(CC)), *Context) && |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 7514 | cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7515 | Instruction *YS = BinaryOperator::CreateShl( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7516 | Op0BO->getOperand(0), Op1, |
| 7517 | Op0BO->getName()); |
| 7518 | InsertNewInstBefore(YS, I); // (Y << C) |
| 7519 | Instruction *XM = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7520 | BinaryOperator::CreateAnd(V1, |
| 7521 | Context->getConstantExprShl(CC, Op1), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7522 | V1->getName()+".mask"); |
| 7523 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 7524 | |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7525 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7526 | } |
| 7527 | } |
| 7528 | |
| 7529 | // FALL THROUGH. |
| 7530 | case Instruction::Sub: { |
| 7531 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 7532 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 7533 | match(Op0BO->getOperand(0), m_Shr(m_Value(V1), |
| 7534 | m_Specific(Op1)), *Context)){ |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7535 | Instruction *YS = BinaryOperator::CreateShl( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7536 | Op0BO->getOperand(1), Op1, |
| 7537 | Op0BO->getName()); |
| 7538 | InsertNewInstBefore(YS, I); // (Y << C) |
| 7539 | Instruction *X = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7540 | BinaryOperator::Create(Op0BO->getOpcode(), V1, YS, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7541 | Op0BO->getOperand(0)->getName()); |
| 7542 | InsertNewInstBefore(X, I); // (X + (Y << C)) |
| 7543 | uint32_t Op1Val = Op1->getLimitedValue(TypeBits); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7544 | return BinaryOperator::CreateAnd(X, Context->getConstantInt( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7545 | APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val))); |
| 7546 | } |
| 7547 | |
| 7548 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
| 7549 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 7550 | match(Op0BO->getOperand(0), |
| 7551 | m_And(m_Shr(m_Value(V1), m_Value(V2)), |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 7552 | m_ConstantInt(CC)), *Context) && V2 == Op1 && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7553 | cast<BinaryOperator>(Op0BO->getOperand(0)) |
| 7554 | ->getOperand(0)->hasOneUse()) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7555 | Instruction *YS = BinaryOperator::CreateShl( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7556 | Op0BO->getOperand(1), Op1, |
| 7557 | Op0BO->getName()); |
| 7558 | InsertNewInstBefore(YS, I); // (Y << C) |
| 7559 | Instruction *XM = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7560 | BinaryOperator::CreateAnd(V1, |
| 7561 | Context->getConstantExprShl(CC, Op1), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7562 | V1->getName()+".mask"); |
| 7563 | InsertNewInstBefore(XM, I); // X & (CC << C) |
| 7564 | |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7565 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7566 | } |
| 7567 | |
| 7568 | break; |
| 7569 | } |
| 7570 | } |
| 7571 | |
| 7572 | |
| 7573 | // If the operand is an bitwise operator with a constant RHS, and the |
| 7574 | // shift is the only use, we can pull it out of the shift. |
| 7575 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 7576 | bool isValid = true; // Valid only for And, Or, Xor |
| 7577 | bool highBitSet = false; // Transform if high bit of constant set? |
| 7578 | |
| 7579 | switch (Op0BO->getOpcode()) { |
| 7580 | default: isValid = false; break; // Do not perform transform! |
| 7581 | case Instruction::Add: |
| 7582 | isValid = isLeftShift; |
| 7583 | break; |
| 7584 | case Instruction::Or: |
| 7585 | case Instruction::Xor: |
| 7586 | highBitSet = false; |
| 7587 | break; |
| 7588 | case Instruction::And: |
| 7589 | highBitSet = true; |
| 7590 | break; |
| 7591 | } |
| 7592 | |
| 7593 | // If this is a signed shift right, and the high bit is modified |
| 7594 | // by the logical operation, do not perform the transformation. |
| 7595 | // The highBitSet boolean indicates the value of the high bit of |
| 7596 | // the constant which would cause it to be modified for this |
| 7597 | // operation. |
| 7598 | // |
| Chris Lattner | 15b76e3 | 2007-12-06 06:25:04 +0000 | [diff] [blame] | 7599 | if (isValid && I.getOpcode() == Instruction::AShr) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7600 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7601 | |
| 7602 | if (isValid) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7603 | Constant *NewRHS = Context->getConstantExpr(I.getOpcode(), Op0C, Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7604 | |
| 7605 | Instruction *NewShift = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7606 | BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7607 | InsertNewInstBefore(NewShift, I); |
| 7608 | NewShift->takeName(Op0BO); |
| 7609 | |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7610 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7611 | NewRHS); |
| 7612 | } |
| 7613 | } |
| 7614 | } |
| 7615 | } |
| 7616 | |
| 7617 | // Find out if this is a shift of a shift by a constant. |
| 7618 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 7619 | if (ShiftOp && !ShiftOp->isShift()) |
| 7620 | ShiftOp = 0; |
| 7621 | |
| 7622 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
| 7623 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
| 7624 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 7625 | uint32_t ShiftAmt2 = Op1->getLimitedValue(TypeBits); |
| 7626 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 7627 | if (ShiftAmt1 == 0) return 0; // Will be simplified in the future. |
| 7628 | Value *X = ShiftOp->getOperand(0); |
| 7629 | |
| 7630 | uint32_t AmtSum = ShiftAmt1+ShiftAmt2; // Fold into one big shift. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7631 | |
| 7632 | const IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 7633 | |
| 7634 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
| 7635 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
| Chris Lattner | b36c701 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7636 | // If this is oversized composite shift, then unsigned shifts get 0, ashr |
| 7637 | // saturates. |
| 7638 | if (AmtSum >= TypeBits) { |
| 7639 | if (I.getOpcode() != Instruction::AShr) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7640 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Chris Lattner | b36c701 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7641 | AmtSum = TypeBits-1; // Saturate to 31 for i32 ashr. |
| 7642 | } |
| 7643 | |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7644 | return BinaryOperator::Create(I.getOpcode(), X, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7645 | Context->getConstantInt(Ty, AmtSum)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7646 | } else if (ShiftOp->getOpcode() == Instruction::LShr && |
| 7647 | I.getOpcode() == Instruction::AShr) { |
| Chris Lattner | b36c701 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7648 | if (AmtSum >= TypeBits) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7649 | return ReplaceInstUsesWith(I, Context->getNullValue(I.getType())); |
| Chris Lattner | b36c701 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7650 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7651 | // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7652 | return BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, AmtSum)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7653 | } else if (ShiftOp->getOpcode() == Instruction::AShr && |
| 7654 | I.getOpcode() == Instruction::LShr) { |
| 7655 | // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0. |
| Chris Lattner | b36c701 | 2009-03-20 22:41:15 +0000 | [diff] [blame] | 7656 | if (AmtSum >= TypeBits) |
| 7657 | AmtSum = TypeBits-1; |
| 7658 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7659 | Instruction *Shift = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7660 | BinaryOperator::CreateAShr(X, Context->getConstantInt(Ty, AmtSum)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7661 | InsertNewInstBefore(Shift, I); |
| 7662 | |
| 7663 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7664 | return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7665 | } |
| 7666 | |
| 7667 | // Okay, if we get here, one shift must be left, and the other shift must be |
| 7668 | // right. See if the amounts are equal. |
| 7669 | if (ShiftAmt1 == ShiftAmt2) { |
| 7670 | // If we have ((X >>? C) << C), turn this into X & (-1 << C). |
| 7671 | if (I.getOpcode() == Instruction::Shl) { |
| 7672 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7673 | return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7674 | } |
| 7675 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 7676 | if (I.getOpcode() == Instruction::LShr) { |
| 7677 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7678 | return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7679 | } |
| 7680 | // We can simplify ((X << C) >>s C) into a trunc + sext. |
| 7681 | // NOTE: we could do this for any C, but that would make 'unusual' integer |
| 7682 | // types. For now, just stick to ones well-supported by the code |
| 7683 | // generators. |
| 7684 | const Type *SExtType = 0; |
| 7685 | switch (Ty->getBitWidth() - ShiftAmt1) { |
| 7686 | case 1 : |
| 7687 | case 8 : |
| 7688 | case 16 : |
| 7689 | case 32 : |
| 7690 | case 64 : |
| 7691 | case 128: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7692 | SExtType = Context->getIntegerType(Ty->getBitWidth() - ShiftAmt1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7693 | break; |
| 7694 | default: break; |
| 7695 | } |
| 7696 | if (SExtType) { |
| 7697 | Instruction *NewTrunc = new TruncInst(X, SExtType, "sext"); |
| 7698 | InsertNewInstBefore(NewTrunc, I); |
| 7699 | return new SExtInst(NewTrunc, Ty); |
| 7700 | } |
| 7701 | // Otherwise, we can't handle it yet. |
| 7702 | } else if (ShiftAmt1 < ShiftAmt2) { |
| 7703 | uint32_t ShiftDiff = ShiftAmt2-ShiftAmt1; |
| 7704 | |
| 7705 | // (X >>? C1) << C2 --> X << (C2-C1) & (-1 << C2) |
| 7706 | if (I.getOpcode() == Instruction::Shl) { |
| 7707 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 7708 | ShiftOp->getOpcode() == Instruction::AShr); |
| 7709 | Instruction *Shift = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7710 | BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7711 | InsertNewInstBefore(Shift, I); |
| 7712 | |
| 7713 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7714 | return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7715 | } |
| 7716 | |
| 7717 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
| 7718 | if (I.getOpcode() == Instruction::LShr) { |
| 7719 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 7720 | Instruction *Shift = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7721 | BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, ShiftDiff)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7722 | InsertNewInstBefore(Shift, I); |
| 7723 | |
| 7724 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7725 | return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7726 | } |
| 7727 | |
| 7728 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. |
| 7729 | } else { |
| 7730 | assert(ShiftAmt2 < ShiftAmt1); |
| 7731 | uint32_t ShiftDiff = ShiftAmt1-ShiftAmt2; |
| 7732 | |
| 7733 | // (X >>? C1) << C2 --> X >>? (C1-C2) & (-1 << C2) |
| 7734 | if (I.getOpcode() == Instruction::Shl) { |
| 7735 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 7736 | ShiftOp->getOpcode() == Instruction::AShr); |
| 7737 | Instruction *Shift = |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7738 | BinaryOperator::Create(ShiftOp->getOpcode(), X, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7739 | Context->getConstantInt(Ty, ShiftDiff)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7740 | InsertNewInstBefore(Shift, I); |
| 7741 | |
| 7742 | APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7743 | return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7744 | } |
| 7745 | |
| 7746 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
| 7747 | if (I.getOpcode() == Instruction::LShr) { |
| 7748 | assert(ShiftOp->getOpcode() == Instruction::Shl); |
| 7749 | Instruction *Shift = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7750 | BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7751 | InsertNewInstBefore(Shift, I); |
| 7752 | |
| 7753 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7754 | return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in. |
| 7758 | } |
| 7759 | } |
| 7760 | return 0; |
| 7761 | } |
| 7762 | |
| 7763 | |
| 7764 | /// DecomposeSimpleLinearExpr - Analyze 'Val', seeing if it is a simple linear |
| 7765 | /// expression. If so, decompose it, returning some value X, such that Val is |
| 7766 | /// X*Scale+Offset. |
| 7767 | /// |
| 7768 | static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 7769 | int &Offset, LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7770 | assert(Val->getType() == Type::Int32Ty && "Unexpected allocation size type!"); |
| 7771 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
| 7772 | Offset = CI->getZExtValue(); |
| Chris Lattner | c59171a | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 7773 | Scale = 0; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7774 | return Context->getConstantInt(Type::Int32Ty, 0); |
| Chris Lattner | c59171a | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 7775 | } else if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { |
| 7776 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 7777 | if (I->getOpcode() == Instruction::Shl) { |
| 7778 | // This is a value scaled by '1 << the shift amt'. |
| 7779 | Scale = 1U << RHS->getZExtValue(); |
| 7780 | Offset = 0; |
| 7781 | return I->getOperand(0); |
| 7782 | } else if (I->getOpcode() == Instruction::Mul) { |
| 7783 | // This value is scaled by 'RHS'. |
| 7784 | Scale = RHS->getZExtValue(); |
| 7785 | Offset = 0; |
| 7786 | return I->getOperand(0); |
| 7787 | } else if (I->getOpcode() == Instruction::Add) { |
| 7788 | // We have X+C. Check to see if we really have (X*C2)+C1, |
| 7789 | // where C1 is divisible by C2. |
| 7790 | unsigned SubScale; |
| 7791 | Value *SubVal = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7792 | DecomposeSimpleLinearExpr(I->getOperand(0), SubScale, |
| 7793 | Offset, Context); |
| Chris Lattner | c59171a | 2007-10-12 05:30:59 +0000 | [diff] [blame] | 7794 | Offset += RHS->getZExtValue(); |
| 7795 | Scale = SubScale; |
| 7796 | return SubVal; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7797 | } |
| 7798 | } |
| 7799 | } |
| 7800 | |
| 7801 | // Otherwise, we can't look past this. |
| 7802 | Scale = 1; |
| 7803 | Offset = 0; |
| 7804 | return Val; |
| 7805 | } |
| 7806 | |
| 7807 | |
| 7808 | /// PromoteCastOfAllocation - If we find a cast of an allocation instruction, |
| 7809 | /// try to eliminate the cast by moving the type information into the alloc. |
| 7810 | Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI, |
| 7811 | AllocationInst &AI) { |
| 7812 | const PointerType *PTy = cast<PointerType>(CI.getType()); |
| 7813 | |
| 7814 | // Remove any uses of AI that are dead. |
| 7815 | assert(!CI.use_empty() && "Dead instructions should be removed earlier!"); |
| 7816 | |
| 7817 | for (Value::use_iterator UI = AI.use_begin(), E = AI.use_end(); UI != E; ) { |
| 7818 | Instruction *User = cast<Instruction>(*UI++); |
| 7819 | if (isInstructionTriviallyDead(User)) { |
| 7820 | while (UI != E && *UI == User) |
| 7821 | ++UI; // If this instruction uses AI more than once, don't break UI. |
| 7822 | |
| 7823 | ++NumDeadInst; |
| 7824 | DOUT << "IC: DCE: " << *User; |
| 7825 | EraseInstFromFunction(*User); |
| 7826 | } |
| 7827 | } |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 7828 | |
| 7829 | // This requires TargetData to get the alloca alignment and size information. |
| 7830 | if (!TD) return 0; |
| 7831 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7832 | // Get the type really allocated and the type casted to. |
| 7833 | const Type *AllocElTy = AI.getAllocatedType(); |
| 7834 | const Type *CastElTy = PTy->getElementType(); |
| 7835 | if (!AllocElTy->isSized() || !CastElTy->isSized()) return 0; |
| 7836 | |
| 7837 | unsigned AllocElTyAlign = TD->getABITypeAlignment(AllocElTy); |
| 7838 | unsigned CastElTyAlign = TD->getABITypeAlignment(CastElTy); |
| 7839 | if (CastElTyAlign < AllocElTyAlign) return 0; |
| 7840 | |
| 7841 | // If the allocation has multiple uses, only promote it if we are strictly |
| 7842 | // increasing the alignment of the resultant allocation. If we keep it the |
| Dale Johannesen | 1ef9dc1 | 2009-03-05 00:39:02 +0000 | [diff] [blame] | 7843 | // same, we open the door to infinite loops of various kinds. (A reference |
| 7844 | // from a dbg.declare doesn't count as a use for this purpose.) |
| 7845 | if (!AI.hasOneUse() && !hasOneUsePlusDeclare(&AI) && |
| 7846 | CastElTyAlign == AllocElTyAlign) return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7847 | |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 7848 | uint64_t AllocElTySize = TD->getTypeAllocSize(AllocElTy); |
| 7849 | uint64_t CastElTySize = TD->getTypeAllocSize(CastElTy); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7850 | if (CastElTySize == 0 || AllocElTySize == 0) return 0; |
| 7851 | |
| 7852 | // See if we can satisfy the modulus by pulling a scale out of the array |
| 7853 | // size argument. |
| 7854 | unsigned ArraySizeScale; |
| 7855 | int ArrayOffset; |
| 7856 | Value *NumElements = // See if the array size is a decomposable linear expr. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7857 | DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, |
| 7858 | ArrayOffset, Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7859 | |
| 7860 | // If we can now satisfy the modulus, by using a non-1 scale, we really can |
| 7861 | // do the xform. |
| 7862 | if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || |
| 7863 | (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return 0; |
| 7864 | |
| 7865 | unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; |
| 7866 | Value *Amt = 0; |
| 7867 | if (Scale == 1) { |
| 7868 | Amt = NumElements; |
| 7869 | } else { |
| 7870 | // If the allocation size is constant, form a constant mul expression |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7871 | Amt = Context->getConstantInt(Type::Int32Ty, Scale); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7872 | if (isa<ConstantInt>(NumElements)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7873 | Amt = Context->getConstantExprMul(cast<ConstantInt>(NumElements), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7874 | cast<ConstantInt>(Amt)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7875 | // otherwise multiply the amount and the number of elements |
| Chris Lattner | 27cc547 | 2009-03-17 17:55:15 +0000 | [diff] [blame] | 7876 | else { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7877 | Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7878 | Amt = InsertNewInstBefore(Tmp, AI); |
| 7879 | } |
| 7880 | } |
| 7881 | |
| 7882 | if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 7883 | Value *Off = Context->getConstantInt(Type::Int32Ty, Offset, true); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 7884 | Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7885 | Amt = InsertNewInstBefore(Tmp, AI); |
| 7886 | } |
| 7887 | |
| 7888 | AllocationInst *New; |
| 7889 | if (isa<MallocInst>(AI)) |
| Owen Anderson | 140166d | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 7890 | New = new MallocInst(CastElTy, Amt, AI.getAlignment()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7891 | else |
| Owen Anderson | 140166d | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 7892 | New = new AllocaInst(CastElTy, Amt, AI.getAlignment()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7893 | InsertNewInstBefore(New, AI); |
| 7894 | New->takeName(&AI); |
| 7895 | |
| Dale Johannesen | 1ef9dc1 | 2009-03-05 00:39:02 +0000 | [diff] [blame] | 7896 | // If the allocation has one real use plus a dbg.declare, just remove the |
| 7897 | // declare. |
| 7898 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(&AI)) { |
| 7899 | EraseInstFromFunction(*DI); |
| 7900 | } |
| 7901 | // If the allocation has multiple real uses, insert a cast and change all |
| 7902 | // things that used it to use the new cast. This will also hack on CI, but it |
| 7903 | // will die soon. |
| 7904 | else if (!AI.hasOneUse()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7905 | AddUsesToWorkList(AI); |
| 7906 | // New is the allocation instruction, pointer typed. AI is the original |
| 7907 | // allocation instruction, also pointer typed. Thus, cast to use is BitCast. |
| 7908 | CastInst *NewCast = new BitCastInst(New, AI.getType(), "tmpcast"); |
| 7909 | InsertNewInstBefore(NewCast, AI); |
| 7910 | AI.replaceAllUsesWith(NewCast); |
| 7911 | } |
| 7912 | return ReplaceInstUsesWith(CI, New); |
| 7913 | } |
| 7914 | |
| 7915 | /// CanEvaluateInDifferentType - Return true if we can take the specified value |
| 7916 | /// and return it as type Ty without inserting any new casts and without |
| 7917 | /// changing the computed value. This is used by code that tries to decide |
| 7918 | /// whether promoting or shrinking integer operations to wider or smaller types |
| 7919 | /// will allow us to eliminate a truncate or extend. |
| 7920 | /// |
| 7921 | /// This is a truncation operation if Ty is smaller than V->getType(), or an |
| 7922 | /// extension operation if Ty is larger. |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7923 | /// |
| 7924 | /// If CastOpc is a truncation, then Ty will be a type smaller than V. We |
| 7925 | /// should return true if trunc(V) can be computed by computing V in the smaller |
| 7926 | /// type. If V is an instruction, then trunc(inst(x,y)) can be computed as |
| 7927 | /// inst(trunc(x),trunc(y)), which only makes sense if x and y can be |
| 7928 | /// efficiently truncated. |
| 7929 | /// |
| 7930 | /// If CastOpc is a sext or zext, we are asking if the low bits of the value can |
| 7931 | /// bit computed in a larger type, which is then and'd or sext_in_reg'd to get |
| 7932 | /// the final result. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7933 | bool InstCombiner::CanEvaluateInDifferentType(Value *V, const Type *Ty, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7934 | unsigned CastOpc, |
| 7935 | int &NumCastsRemoved){ |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7936 | // We can always evaluate constants in another type. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7937 | if (isa<Constant>(V)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7938 | return true; |
| 7939 | |
| 7940 | Instruction *I = dyn_cast<Instruction>(V); |
| 7941 | if (!I) return false; |
| 7942 | |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7943 | const Type *OrigTy = V->getType(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7944 | |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7945 | // If this is an extension or truncate, we can often eliminate it. |
| 7946 | if (isa<TruncInst>(I) || isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 7947 | // If this is a cast from the destination type, we can trivially eliminate |
| 7948 | // it, and this will remove a cast overall. |
| 7949 | if (I->getOperand(0)->getType() == Ty) { |
| 7950 | // If the first operand is itself a cast, and is eliminable, do not count |
| 7951 | // this as an eliminable cast. We would prefer to eliminate those two |
| 7952 | // casts first. |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 7953 | if (!isa<CastInst>(I->getOperand(0)) && I->hasOneUse()) |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7954 | ++NumCastsRemoved; |
| 7955 | return true; |
| 7956 | } |
| 7957 | } |
| 7958 | |
| 7959 | // We can't extend or shrink something that has multiple uses: doing so would |
| 7960 | // require duplicating the instruction in general, which isn't profitable. |
| 7961 | if (!I->hasOneUse()) return false; |
| 7962 | |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 7963 | unsigned Opc = I->getOpcode(); |
| 7964 | switch (Opc) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7965 | case Instruction::Add: |
| 7966 | case Instruction::Sub: |
| Nick Lewycky | 1265a7d | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 7967 | case Instruction::Mul: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7968 | case Instruction::And: |
| 7969 | case Instruction::Or: |
| 7970 | case Instruction::Xor: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7971 | // These operators can all arbitrarily be extended or truncated. |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7972 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7973 | NumCastsRemoved) && |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 7974 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 7975 | NumCastsRemoved); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7976 | |
| Eli Friedman | 08c45bc | 2009-07-13 22:46:01 +0000 | [diff] [blame] | 7977 | case Instruction::UDiv: |
| 7978 | case Instruction::URem: { |
| 7979 | // UDiv and URem can be truncated if all the truncated bits are zero. |
| 7980 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 7981 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 7982 | if (BitWidth < OrigBitWidth) { |
| 7983 | APInt Mask = APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth); |
| 7984 | if (MaskedValueIsZero(I->getOperand(0), Mask) && |
| 7985 | MaskedValueIsZero(I->getOperand(1), Mask)) { |
| 7986 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| 7987 | NumCastsRemoved) && |
| 7988 | CanEvaluateInDifferentType(I->getOperand(1), Ty, CastOpc, |
| 7989 | NumCastsRemoved); |
| 7990 | } |
| 7991 | } |
| 7992 | break; |
| 7993 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7994 | case Instruction::Shl: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7995 | // If we are truncating the result of this SHL, and if it's a shift of a |
| 7996 | // constant amount, we can always perform a SHL in a smaller type. |
| 7997 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 7998 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 7999 | if (BitWidth < OrigTy->getScalarSizeInBits() && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8000 | CI->getLimitedValue(BitWidth) < BitWidth) |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 8001 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8002 | NumCastsRemoved); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8003 | } |
| 8004 | break; |
| 8005 | case Instruction::LShr: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8006 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
| 8007 | // lshr iff we know that the bits we would otherwise be shifting in are |
| 8008 | // already zeros. |
| 8009 | if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8010 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 8011 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8012 | if (BitWidth < OrigBitWidth && |
| 8013 | MaskedValueIsZero(I->getOperand(0), |
| 8014 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 8015 | CI->getLimitedValue(BitWidth) < BitWidth) { |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 8016 | return CanEvaluateInDifferentType(I->getOperand(0), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8017 | NumCastsRemoved); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8018 | } |
| 8019 | } |
| 8020 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8021 | case Instruction::ZExt: |
| 8022 | case Instruction::SExt: |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 8023 | case Instruction::Trunc: |
| 8024 | // If this is the same kind of case as our original (e.g. zext+zext), we |
| Chris Lattner | 9c909d2 | 2007-08-02 17:23:38 +0000 | [diff] [blame] | 8025 | // can safely replace it. Note that replacing it does not reduce the number |
| 8026 | // of casts in the input. |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8027 | if (Opc == CastOpc) |
| 8028 | return true; |
| 8029 | |
| 8030 | // sext (zext ty1), ty2 -> zext ty2 |
| Evan Cheng | 7bb0d95 | 2009-01-15 17:09:07 +0000 | [diff] [blame] | 8031 | if (CastOpc == Instruction::SExt && Opc == Instruction::ZExt) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8032 | return true; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8033 | break; |
| Nick Lewycky | 1265a7d | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 8034 | case Instruction::Select: { |
| 8035 | SelectInst *SI = cast<SelectInst>(I); |
| 8036 | return CanEvaluateInDifferentType(SI->getTrueValue(), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8037 | NumCastsRemoved) && |
| Nick Lewycky | 1265a7d | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 8038 | CanEvaluateInDifferentType(SI->getFalseValue(), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8039 | NumCastsRemoved); |
| Nick Lewycky | 1265a7d | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 8040 | } |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 8041 | case Instruction::PHI: { |
| 8042 | // We can change a phi if we can change all operands. |
| 8043 | PHINode *PN = cast<PHINode>(I); |
| 8044 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 8045 | if (!CanEvaluateInDifferentType(PN->getIncomingValue(i), Ty, CastOpc, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8046 | NumCastsRemoved)) |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 8047 | return false; |
| 8048 | return true; |
| 8049 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8050 | default: |
| 8051 | // TODO: Can handle more cases here. |
| 8052 | break; |
| 8053 | } |
| 8054 | |
| 8055 | return false; |
| 8056 | } |
| 8057 | |
| 8058 | /// EvaluateInDifferentType - Given an expression that |
| 8059 | /// CanEvaluateInDifferentType returns true for, actually insert the code to |
| 8060 | /// evaluate the expression. |
| 8061 | Value *InstCombiner::EvaluateInDifferentType(Value *V, const Type *Ty, |
| 8062 | bool isSigned) { |
| 8063 | if (Constant *C = dyn_cast<Constant>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8064 | return Context->getConstantExprIntegerCast(C, Ty, |
| 8065 | isSigned /*Sext or ZExt*/); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8066 | |
| 8067 | // Otherwise, it must be an instruction. |
| 8068 | Instruction *I = cast<Instruction>(V); |
| 8069 | Instruction *Res = 0; |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8070 | unsigned Opc = I->getOpcode(); |
| 8071 | switch (Opc) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8072 | case Instruction::Add: |
| 8073 | case Instruction::Sub: |
| Nick Lewycky | c52646a | 2008-01-22 05:08:48 +0000 | [diff] [blame] | 8074 | case Instruction::Mul: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8075 | case Instruction::And: |
| 8076 | case Instruction::Or: |
| 8077 | case Instruction::Xor: |
| 8078 | case Instruction::AShr: |
| 8079 | case Instruction::LShr: |
| Eli Friedman | 08c45bc | 2009-07-13 22:46:01 +0000 | [diff] [blame] | 8080 | case Instruction::Shl: |
| 8081 | case Instruction::UDiv: |
| 8082 | case Instruction::URem: { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8083 | Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); |
| 8084 | Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8085 | Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8086 | break; |
| 8087 | } |
| 8088 | case Instruction::Trunc: |
| 8089 | case Instruction::ZExt: |
| 8090 | case Instruction::SExt: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8091 | // If the source type of the cast is the type we're trying for then we can |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 8092 | // just return the source. There's no need to insert it because it is not |
| 8093 | // new. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8094 | if (I->getOperand(0)->getType() == Ty) |
| 8095 | return I->getOperand(0); |
| 8096 | |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 8097 | // Otherwise, must be the same type of cast, so just reinsert a new one. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8098 | Res = CastInst::Create(cast<CastInst>(I)->getOpcode(), I->getOperand(0), |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 8099 | Ty); |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 8100 | break; |
| Nick Lewycky | 1265a7d | 2008-07-05 21:19:34 +0000 | [diff] [blame] | 8101 | case Instruction::Select: { |
| 8102 | Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); |
| 8103 | Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); |
| 8104 | Res = SelectInst::Create(I->getOperand(0), True, False); |
| 8105 | break; |
| 8106 | } |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 8107 | case Instruction::PHI: { |
| 8108 | PHINode *OPN = cast<PHINode>(I); |
| 8109 | PHINode *NPN = PHINode::Create(Ty); |
| 8110 | for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { |
| 8111 | Value *V =EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); |
| 8112 | NPN->addIncoming(V, OPN->getIncomingBlock(i)); |
| 8113 | } |
| 8114 | Res = NPN; |
| 8115 | break; |
| 8116 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8117 | default: |
| 8118 | // TODO: Can handle more cases here. |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8119 | llvm_unreachable("Unreachable!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8120 | break; |
| 8121 | } |
| 8122 | |
| Chris Lattner | 4200c206 | 2008-06-18 04:00:49 +0000 | [diff] [blame] | 8123 | Res->takeName(I); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8124 | return InsertNewInstBefore(Res, *I); |
| 8125 | } |
| 8126 | |
| 8127 | /// @brief Implement the transforms common to all CastInst visitors. |
| 8128 | Instruction *InstCombiner::commonCastTransforms(CastInst &CI) { |
| 8129 | Value *Src = CI.getOperand(0); |
| 8130 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8131 | // Many cases of "cast of a cast" are eliminable. If it's eliminable we just |
| 8132 | // eliminate it now. |
| 8133 | if (CastInst *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast |
| 8134 | if (Instruction::CastOps opc = |
| 8135 | isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) { |
| 8136 | // The first cast (CSrc) is eliminable so we need to fix up or replace |
| 8137 | // the second cast (CI). CSrc will then have a good chance of being dead. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8138 | return CastInst::Create(opc, CSrc->getOperand(0), CI.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8139 | } |
| 8140 | } |
| 8141 | |
| 8142 | // If we are casting a select then fold the cast into the select |
| 8143 | if (SelectInst *SI = dyn_cast<SelectInst>(Src)) |
| 8144 | if (Instruction *NV = FoldOpIntoSelect(CI, SI, this)) |
| 8145 | return NV; |
| 8146 | |
| 8147 | // If we are casting a PHI then fold the cast into the PHI |
| 8148 | if (isa<PHINode>(Src)) |
| 8149 | if (Instruction *NV = FoldOpIntoPhi(CI)) |
| 8150 | return NV; |
| 8151 | |
| 8152 | return 0; |
| 8153 | } |
| 8154 | |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8155 | /// FindElementAtOffset - Given a type and a constant offset, determine whether |
| 8156 | /// or not there is a sequence of GEP indices into the type that will land us at |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 8157 | /// the specified offset. If so, fill them into NewIndices and return the |
| 8158 | /// resultant element type, otherwise return null. |
| 8159 | static const Type *FindElementAtOffset(const Type *Ty, int64_t Offset, |
| 8160 | SmallVectorImpl<Value*> &NewIndices, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8161 | const TargetData *TD, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 8162 | LLVMContext *Context) { |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 8163 | if (!TD) return 0; |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 8164 | if (!Ty->isSized()) return 0; |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8165 | |
| 8166 | // Start with the index over the outer type. Note that the type size |
| 8167 | // might be zero (even if the offset isn't zero) if the indexed type |
| 8168 | // is something like [0 x {int, int}] |
| 8169 | const Type *IntPtrTy = TD->getIntPtrType(); |
| 8170 | int64_t FirstIdx = 0; |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 8171 | if (int64_t TySize = TD->getTypeAllocSize(Ty)) { |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8172 | FirstIdx = Offset/TySize; |
| Chris Lattner | 0bd6f2b | 2009-01-11 20:41:36 +0000 | [diff] [blame] | 8173 | Offset -= FirstIdx*TySize; |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8174 | |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8175 | // Handle hosts where % returns negative instead of values [0..TySize). |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8176 | if (Offset < 0) { |
| 8177 | --FirstIdx; |
| 8178 | Offset += TySize; |
| 8179 | assert(Offset >= 0); |
| 8180 | } |
| 8181 | assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset"); |
| 8182 | } |
| 8183 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8184 | NewIndices.push_back(Context->getConstantInt(IntPtrTy, FirstIdx)); |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8185 | |
| 8186 | // Index into the types. If we fail, set OrigBase to null. |
| 8187 | while (Offset) { |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8188 | // Indexing into tail padding between struct/array elements. |
| 8189 | if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty)) |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 8190 | return 0; |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8191 | |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8192 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 8193 | const StructLayout *SL = TD->getStructLayout(STy); |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8194 | assert(Offset < (int64_t)SL->getSizeInBytes() && |
| 8195 | "Offset must stay within the indexed type"); |
| 8196 | |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8197 | unsigned Elt = SL->getElementContainingOffset(Offset); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8198 | NewIndices.push_back(Context->getConstantInt(Type::Int32Ty, Elt)); |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8199 | |
| 8200 | Offset -= SL->getElementOffset(Elt); |
| 8201 | Ty = STy->getElementType(Elt); |
| Chris Lattner | d35ce6a | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 8202 | } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) { |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 8203 | uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType()); |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8204 | assert(EltSize && "Cannot index into a zero-sized array"); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8205 | NewIndices.push_back(Context->getConstantInt(IntPtrTy,Offset/EltSize)); |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8206 | Offset %= EltSize; |
| Chris Lattner | d35ce6a | 2009-01-11 20:23:52 +0000 | [diff] [blame] | 8207 | Ty = AT->getElementType(); |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8208 | } else { |
| Chris Lattner | ce48c46 | 2009-01-11 20:15:20 +0000 | [diff] [blame] | 8209 | // Otherwise, we can't index into the middle of this atomic type, bail. |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 8210 | return 0; |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8211 | } |
| 8212 | } |
| 8213 | |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 8214 | return Ty; |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8215 | } |
| 8216 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8217 | /// @brief Implement the transforms for cast of pointer (bitcast/ptrtoint) |
| 8218 | Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { |
| 8219 | Value *Src = CI.getOperand(0); |
| 8220 | |
| 8221 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { |
| 8222 | // If casting the result of a getelementptr instruction with no offset, turn |
| 8223 | // this into a cast of the original pointer! |
| 8224 | if (GEP->hasAllZeroIndices()) { |
| 8225 | // Changing the cast operand is usually not a good idea but it is safe |
| 8226 | // here because the pointer operand is being replaced with another |
| 8227 | // pointer operand so the opcode doesn't need to change. |
| 8228 | AddToWorkList(GEP); |
| 8229 | CI.setOperand(0, GEP->getOperand(0)); |
| 8230 | return &CI; |
| 8231 | } |
| 8232 | |
| 8233 | // If the GEP has a single use, and the base pointer is a bitcast, and the |
| 8234 | // GEP computes a constant offset, see if we can convert these three |
| 8235 | // instructions into fewer. This typically happens with unions and other |
| 8236 | // non-type-safe code. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 8237 | if (TD && GEP->hasOneUse() && isa<BitCastInst>(GEP->getOperand(0))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8238 | if (GEP->hasAllConstantIndices()) { |
| 8239 | // We are guaranteed to get a constant from EmitGEPOffset. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8240 | ConstantInt *OffsetV = |
| 8241 | cast<ConstantInt>(EmitGEPOffset(GEP, CI, *this)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8242 | int64_t Offset = OffsetV->getSExtValue(); |
| 8243 | |
| 8244 | // Get the base pointer input of the bitcast, and the type it points to. |
| 8245 | Value *OrigBase = cast<BitCastInst>(GEP->getOperand(0))->getOperand(0); |
| 8246 | const Type *GEPIdxTy = |
| 8247 | cast<PointerType>(OrigBase->getType())->getElementType(); |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8248 | SmallVector<Value*, 8> NewIndices; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8249 | if (FindElementAtOffset(GEPIdxTy, Offset, NewIndices, TD, Context)) { |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8250 | // If we were able to index down into an element, create the GEP |
| 8251 | // and bitcast the result. This eliminates one bitcast, potentially |
| 8252 | // two. |
| 8253 | Instruction *NGEP = GetElementPtrInst::Create(OrigBase, |
| 8254 | NewIndices.begin(), |
| 8255 | NewIndices.end(), ""); |
| 8256 | InsertNewInstBefore(NGEP, CI); |
| 8257 | NGEP->takeName(GEP); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8258 | |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 8259 | if (isa<BitCastInst>(CI)) |
| 8260 | return new BitCastInst(NGEP, CI.getType()); |
| 8261 | assert(isa<PtrToIntInst>(CI)); |
| 8262 | return new PtrToIntInst(NGEP, CI.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8263 | } |
| 8264 | } |
| 8265 | } |
| 8266 | } |
| 8267 | |
| 8268 | return commonCastTransforms(CI); |
| 8269 | } |
| 8270 | |
| Chris Lattner | 8d8ce9b | 2009-04-08 05:41:03 +0000 | [diff] [blame] | 8271 | /// isSafeIntegerType - Return true if this is a basic integer type, not a crazy |
| 8272 | /// type like i42. We don't want to introduce operations on random non-legal |
| 8273 | /// integer types where they don't already exist in the code. In the future, |
| 8274 | /// we should consider making this based off target-data, so that 32-bit targets |
| 8275 | /// won't get i64 operations etc. |
| 8276 | static bool isSafeIntegerType(const Type *Ty) { |
| 8277 | switch (Ty->getPrimitiveSizeInBits()) { |
| 8278 | case 8: |
| 8279 | case 16: |
| 8280 | case 32: |
| 8281 | case 64: |
| 8282 | return true; |
| 8283 | default: |
| 8284 | return false; |
| 8285 | } |
| 8286 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8287 | |
| Eli Friedman | 827e37a | 2009-07-13 20:58:59 +0000 | [diff] [blame] | 8288 | /// commonIntCastTransforms - This function implements the common transforms |
| 8289 | /// for trunc, zext, and sext. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8290 | Instruction *InstCombiner::commonIntCastTransforms(CastInst &CI) { |
| 8291 | if (Instruction *Result = commonCastTransforms(CI)) |
| 8292 | return Result; |
| 8293 | |
| 8294 | Value *Src = CI.getOperand(0); |
| 8295 | const Type *SrcTy = Src->getType(); |
| 8296 | const Type *DestTy = CI.getType(); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8297 | uint32_t SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 8298 | uint32_t DestBitSize = DestTy->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8299 | |
| 8300 | // See if we can simplify any instructions used by the LHS whose sole |
| 8301 | // purpose is to compute bits we don't care about. |
| Chris Lattner | 676c78e | 2009-01-31 08:15:18 +0000 | [diff] [blame] | 8302 | if (SimplifyDemandedInstructionBits(CI)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8303 | return &CI; |
| 8304 | |
| 8305 | // If the source isn't an instruction or has more than one use then we |
| 8306 | // can't do anything more. |
| 8307 | Instruction *SrcI = dyn_cast<Instruction>(Src); |
| 8308 | if (!SrcI || !Src->hasOneUse()) |
| 8309 | return 0; |
| 8310 | |
| 8311 | // Attempt to propagate the cast into the instruction for int->int casts. |
| 8312 | int NumCastsRemoved = 0; |
| Eli Friedman | 1cfc6b4 | 2009-07-13 21:45:57 +0000 | [diff] [blame] | 8313 | // Only do this if the dest type is a simple type, don't convert the |
| 8314 | // expression tree to something weird like i93 unless the source is also |
| 8315 | // strange. |
| 8316 | if ((isSafeIntegerType(DestTy->getScalarType()) || |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8317 | !isSafeIntegerType(SrcI->getType()->getScalarType())) && |
| 8318 | CanEvaluateInDifferentType(SrcI, DestTy, |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8319 | CI.getOpcode(), NumCastsRemoved)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8320 | // If this cast is a truncate, evaluting in a different type always |
| Chris Lattner | ef70bb8 | 2007-08-02 06:11:14 +0000 | [diff] [blame] | 8321 | // eliminates the cast, so it is always a win. If this is a zero-extension, |
| 8322 | // we need to do an AND to maintain the clear top-part of the computation, |
| 8323 | // so we require that the input have eliminated at least one cast. If this |
| 8324 | // is a sign extension, we insert two new casts (to do the extension) so we |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8325 | // require that two casts have been eliminated. |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8326 | bool DoXForm = false; |
| 8327 | bool JustReplace = false; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8328 | switch (CI.getOpcode()) { |
| 8329 | default: |
| 8330 | // All the others use floating point so we shouldn't actually |
| 8331 | // get here because of the check above. |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8332 | llvm_unreachable("Unknown cast type"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8333 | case Instruction::Trunc: |
| 8334 | DoXForm = true; |
| 8335 | break; |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8336 | case Instruction::ZExt: { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8337 | DoXForm = NumCastsRemoved >= 1; |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8338 | if (!DoXForm && 0) { |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8339 | // If it's unnecessary to issue an AND to clear the high bits, it's |
| 8340 | // always profitable to do this xform. |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8341 | Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, false); |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8342 | APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize)); |
| 8343 | if (MaskedValueIsZero(TryRes, Mask)) |
| 8344 | return ReplaceInstUsesWith(CI, TryRes); |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8345 | |
| 8346 | if (Instruction *TryI = dyn_cast<Instruction>(TryRes)) |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8347 | if (TryI->use_empty()) |
| 8348 | EraseInstFromFunction(*TryI); |
| 8349 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8350 | break; |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8351 | } |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8352 | case Instruction::SExt: { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8353 | DoXForm = NumCastsRemoved >= 2; |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8354 | if (!DoXForm && !isa<TruncInst>(SrcI) && 0) { |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8355 | // If we do not have to emit the truncate + sext pair, then it's always |
| 8356 | // profitable to do this xform. |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8357 | // |
| 8358 | // It's not safe to eliminate the trunc + sext pair if one of the |
| 8359 | // eliminated cast is a truncate. e.g. |
| 8360 | // t2 = trunc i32 t1 to i16 |
| 8361 | // t3 = sext i16 t2 to i32 |
| 8362 | // != |
| 8363 | // i32 t1 |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8364 | Value *TryRes = EvaluateInDifferentType(SrcI, DestTy, true); |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8365 | unsigned NumSignBits = ComputeNumSignBits(TryRes); |
| 8366 | if (NumSignBits > (DestBitSize - SrcBitSize)) |
| 8367 | return ReplaceInstUsesWith(CI, TryRes); |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8368 | |
| 8369 | if (Instruction *TryI = dyn_cast<Instruction>(TryRes)) |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8370 | if (TryI->use_empty()) |
| 8371 | EraseInstFromFunction(*TryI); |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8372 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8373 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8374 | } |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8375 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8376 | |
| 8377 | if (DoXForm) { |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8378 | DOUT << "ICE: EvaluateInDifferentType converting expression type to avoid" |
| 8379 | << " cast: " << CI; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8380 | Value *Res = EvaluateInDifferentType(SrcI, DestTy, |
| 8381 | CI.getOpcode() == Instruction::SExt); |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8382 | if (JustReplace) |
| Chris Lattner | 3c0e6f4 | 2009-01-31 19:05:27 +0000 | [diff] [blame] | 8383 | // Just replace this cast with the result. |
| 8384 | return ReplaceInstUsesWith(CI, Res); |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8385 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8386 | assert(Res->getType() == DestTy); |
| 8387 | switch (CI.getOpcode()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 8388 | default: llvm_unreachable("Unknown cast type!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8389 | case Instruction::Trunc: |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8390 | // Just replace this cast with the result. |
| 8391 | return ReplaceInstUsesWith(CI, Res); |
| 8392 | case Instruction::ZExt: { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8393 | assert(SrcBitSize < DestBitSize && "Not a zext?"); |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8394 | |
| 8395 | // If the high bits are already zero, just replace this cast with the |
| 8396 | // result. |
| 8397 | APInt Mask(APInt::getBitsSet(DestBitSize, SrcBitSize, DestBitSize)); |
| 8398 | if (MaskedValueIsZero(Res, Mask)) |
| 8399 | return ReplaceInstUsesWith(CI, Res); |
| 8400 | |
| 8401 | // We need to emit an AND to clear the high bits. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8402 | Constant *C = Context->getConstantInt(APInt::getLowBitsSet(DestBitSize, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8403 | SrcBitSize)); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8404 | return BinaryOperator::CreateAnd(Res, C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8405 | } |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8406 | case Instruction::SExt: { |
| 8407 | // If the high bits are already filled with sign bit, just replace this |
| 8408 | // cast with the result. |
| 8409 | unsigned NumSignBits = ComputeNumSignBits(Res); |
| 8410 | if (NumSignBits > (DestBitSize - SrcBitSize)) |
| Evan Cheng | 9ca34ab | 2009-01-15 17:01:23 +0000 | [diff] [blame] | 8411 | return ReplaceInstUsesWith(CI, Res); |
| 8412 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8413 | // We need to emit a cast to truncate, then a cast to sext. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8414 | return CastInst::Create(Instruction::SExt, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8415 | InsertCastBefore(Instruction::Trunc, Res, Src->getType(), |
| 8416 | CI), DestTy); |
| 8417 | } |
| Evan Cheng | 814a00c | 2009-01-16 02:11:43 +0000 | [diff] [blame] | 8418 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8419 | } |
| 8420 | } |
| 8421 | |
| 8422 | Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0; |
| 8423 | Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0; |
| 8424 | |
| 8425 | switch (SrcI->getOpcode()) { |
| 8426 | case Instruction::Add: |
| 8427 | case Instruction::Mul: |
| 8428 | case Instruction::And: |
| 8429 | case Instruction::Or: |
| 8430 | case Instruction::Xor: |
| 8431 | // If we are discarding information, rewrite. |
| Eli Friedman | 1cfc6b4 | 2009-07-13 21:45:57 +0000 | [diff] [blame] | 8432 | if (DestBitSize < SrcBitSize && DestBitSize != 1) { |
| 8433 | // Don't insert two casts unless at least one can be eliminated. |
| 8434 | if (!ValueRequiresCast(CI.getOpcode(), Op1, DestTy, TD) || |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8435 | !ValueRequiresCast(CI.getOpcode(), Op0, DestTy, TD)) { |
| Eli Friedman | 1cfc6b4 | 2009-07-13 21:45:57 +0000 | [diff] [blame] | 8436 | Value *Op0c = InsertCastBefore(Instruction::Trunc, Op0, DestTy, *SrcI); |
| 8437 | Value *Op1c = InsertCastBefore(Instruction::Trunc, Op1, DestTy, *SrcI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8438 | return BinaryOperator::Create( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8439 | cast<BinaryOperator>(SrcI)->getOpcode(), Op0c, Op1c); |
| 8440 | } |
| 8441 | } |
| 8442 | |
| 8443 | // cast (xor bool X, true) to int --> xor (cast bool X to int), 1 |
| 8444 | if (isa<ZExtInst>(CI) && SrcBitSize == 1 && |
| 8445 | SrcI->getOpcode() == Instruction::Xor && |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 8446 | Op1 == Context->getTrue() && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8447 | (!Op0->hasOneUse() || !isa<CmpInst>(Op0))) { |
| Eli Friedman | 722b479 | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 8448 | Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8449 | return BinaryOperator::CreateXor(New, |
| 8450 | Context->getConstantInt(CI.getType(), 1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8451 | } |
| 8452 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8453 | |
| Eli Friedman | 1cfc6b4 | 2009-07-13 21:45:57 +0000 | [diff] [blame] | 8454 | case Instruction::Shl: { |
| 8455 | // Canonicalize trunc inside shl, if we can. |
| 8456 | ConstantInt *CI = dyn_cast<ConstantInt>(Op1); |
| 8457 | if (CI && DestBitSize < SrcBitSize && |
| 8458 | CI->getLimitedValue(DestBitSize) < DestBitSize) { |
| 8459 | Value *Op0c = InsertCastBefore(Instruction::Trunc, Op0, DestTy, *SrcI); |
| 8460 | Value *Op1c = InsertCastBefore(Instruction::Trunc, Op1, DestTy, *SrcI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8461 | return BinaryOperator::CreateShl(Op0c, Op1c); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8462 | } |
| 8463 | break; |
| Eli Friedman | 1cfc6b4 | 2009-07-13 21:45:57 +0000 | [diff] [blame] | 8464 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8465 | } |
| 8466 | return 0; |
| 8467 | } |
| 8468 | |
| 8469 | Instruction *InstCombiner::visitTrunc(TruncInst &CI) { |
| 8470 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 8471 | return Result; |
| 8472 | |
| 8473 | Value *Src = CI.getOperand(0); |
| 8474 | const Type *Ty = CI.getType(); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8475 | uint32_t DestBitWidth = Ty->getScalarSizeInBits(); |
| 8476 | uint32_t SrcBitWidth = Src->getType()->getScalarSizeInBits(); |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8477 | |
| 8478 | // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0) |
| Eli Friedman | 37a5d41 | 2009-07-18 09:21:25 +0000 | [diff] [blame] | 8479 | if (DestBitWidth == 1) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8480 | Constant *One = Context->getConstantInt(Src->getType(), 1); |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8481 | Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8482 | Value *Zero = Context->getNullValue(Src->getType()); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 8483 | return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero); |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8484 | } |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8485 | |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8486 | // Optimize trunc(lshr(), c) to pull the shift through the truncate. |
| 8487 | ConstantInt *ShAmtV = 0; |
| 8488 | Value *ShiftOp = 0; |
| 8489 | if (Src->hasOneUse() && |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 8490 | match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)), *Context)) { |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8491 | uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); |
| 8492 | |
| 8493 | // Get a mask for the bits shifting in. |
| 8494 | APInt Mask(APInt::getLowBitsSet(SrcBitWidth, ShAmt).shl(DestBitWidth)); |
| 8495 | if (MaskedValueIsZero(ShiftOp, Mask)) { |
| 8496 | if (ShAmt >= DestBitWidth) // All zeros. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8497 | return ReplaceInstUsesWith(CI, Context->getNullValue(Ty)); |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8498 | |
| 8499 | // Okay, we can shrink this. Truncate the input, then return a new |
| 8500 | // shift. |
| 8501 | Value *V1 = InsertCastBefore(Instruction::Trunc, ShiftOp, Ty, CI); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8502 | Value *V2 = Context->getConstantExprTrunc(ShAmtV, Ty); |
| Chris Lattner | 32177f8 | 2009-03-24 18:15:30 +0000 | [diff] [blame] | 8503 | return BinaryOperator::CreateLShr(V1, V2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8504 | } |
| 8505 | } |
| 8506 | |
| 8507 | return 0; |
| 8508 | } |
| 8509 | |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8510 | /// transformZExtICmp - Transform (zext icmp) to bitwise / integer operations |
| 8511 | /// in order to eliminate the icmp. |
| 8512 | Instruction *InstCombiner::transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 8513 | bool DoXform) { |
| 8514 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 8515 | // to an integer, then shift the bit to the appropriate place and then |
| 8516 | // cast to integer to avoid the comparison. |
| 8517 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 8518 | const APInt &Op1CV = Op1C->getValue(); |
| 8519 | |
| 8520 | // zext (x <s 0) to i32 --> x>>u31 true if signbit set. |
| 8521 | // zext (x >s -1) to i32 --> (x>>u31)^1 true if signbit clear. |
| 8522 | if ((ICI->getPredicate() == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| 8523 | (ICI->getPredicate() == ICmpInst::ICMP_SGT &&Op1CV.isAllOnesValue())) { |
| 8524 | if (!DoXform) return ICI; |
| 8525 | |
| 8526 | Value *In = ICI->getOperand(0); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8527 | Value *Sh = Context->getConstantInt(In->getType(), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8528 | In->getType()->getScalarSizeInBits()-1); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8529 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh, |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8530 | In->getName()+".lobit"), |
| 8531 | CI); |
| 8532 | if (In->getType() != CI.getType()) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8533 | In = CastInst::CreateIntegerCast(In, CI.getType(), |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8534 | false/*ZExt*/, "tmp", &CI); |
| 8535 | |
| 8536 | if (ICI->getPredicate() == ICmpInst::ICMP_SGT) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8537 | Constant *One = Context->getConstantInt(In->getType(), 1); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8538 | In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One, |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8539 | In->getName()+".not"), |
| 8540 | CI); |
| 8541 | } |
| 8542 | |
| 8543 | return ReplaceInstUsesWith(CI, In); |
| 8544 | } |
| 8545 | |
| 8546 | |
| 8547 | |
| 8548 | // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. |
| 8549 | // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 8550 | // zext (X == 1) to i32 --> X iff X has only the low bit set. |
| 8551 | // zext (X == 2) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 8552 | // zext (X != 0) to i32 --> X iff X has only the low bit set. |
| 8553 | // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. |
| 8554 | // zext (X != 1) to i32 --> X^1 iff X has only the low bit set. |
| 8555 | // zext (X != 2) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. |
| 8556 | if ((Op1CV == 0 || Op1CV.isPowerOf2()) && |
| 8557 | // This only works for EQ and NE |
| 8558 | ICI->isEquality()) { |
| 8559 | // If Op1C some other power of two, convert: |
| 8560 | uint32_t BitWidth = Op1C->getType()->getBitWidth(); |
| 8561 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 8562 | APInt TypeMask(APInt::getAllOnesValue(BitWidth)); |
| 8563 | ComputeMaskedBits(ICI->getOperand(0), TypeMask, KnownZero, KnownOne); |
| 8564 | |
| 8565 | APInt KnownZeroMask(~KnownZero); |
| 8566 | if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? |
| 8567 | if (!DoXform) return ICI; |
| 8568 | |
| 8569 | bool isNE = ICI->getPredicate() == ICmpInst::ICMP_NE; |
| 8570 | if (Op1CV != 0 && (Op1CV != KnownZeroMask)) { |
| 8571 | // (X&4) == 2 --> false |
| 8572 | // (X&4) != 2 --> true |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8573 | Constant *Res = Context->getConstantInt(Type::Int1Ty, isNE); |
| 8574 | Res = Context->getConstantExprZExt(Res, CI.getType()); |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8575 | return ReplaceInstUsesWith(CI, Res); |
| 8576 | } |
| 8577 | |
| 8578 | uint32_t ShiftAmt = KnownZeroMask.logBase2(); |
| 8579 | Value *In = ICI->getOperand(0); |
| 8580 | if (ShiftAmt) { |
| 8581 | // Perform a logical shr by shiftamt. |
| 8582 | // Insert the shift to put the result in the low bit. |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8583 | In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8584 | Context->getConstantInt(In->getType(), ShiftAmt), |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8585 | In->getName()+".lobit"), CI); |
| 8586 | } |
| 8587 | |
| 8588 | if ((Op1CV != 0) == isNE) { // Toggle the low bit. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8589 | Constant *One = Context->getConstantInt(In->getType(), 1); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8590 | In = BinaryOperator::CreateXor(In, One, "tmp"); |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8591 | InsertNewInstBefore(cast<Instruction>(In), CI); |
| 8592 | } |
| 8593 | |
| 8594 | if (CI.getType() == In->getType()) |
| 8595 | return ReplaceInstUsesWith(CI, In); |
| 8596 | else |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8597 | return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/); |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8598 | } |
| 8599 | } |
| 8600 | } |
| 8601 | |
| 8602 | return 0; |
| 8603 | } |
| 8604 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8605 | Instruction *InstCombiner::visitZExt(ZExtInst &CI) { |
| 8606 | // If one of the common conversion will work .. |
| 8607 | if (Instruction *Result = commonIntCastTransforms(CI)) |
| 8608 | return Result; |
| 8609 | |
| 8610 | Value *Src = CI.getOperand(0); |
| 8611 | |
| Chris Lattner | 215d56e | 2009-02-17 20:47:23 +0000 | [diff] [blame] | 8612 | // If this is a TRUNC followed by a ZEXT then we are dealing with integral |
| 8613 | // types and if the sizes are just right we can convert this into a logical |
| 8614 | // 'and' which will be much cheaper than the pair of casts. |
| 8615 | if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast |
| 8616 | // Get the sizes of the types involved. We know that the intermediate type |
| 8617 | // will be smaller than A or C, but don't know the relation between A and C. |
| 8618 | Value *A = CSrc->getOperand(0); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8619 | unsigned SrcSize = A->getType()->getScalarSizeInBits(); |
| 8620 | unsigned MidSize = CSrc->getType()->getScalarSizeInBits(); |
| 8621 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| Chris Lattner | 215d56e | 2009-02-17 20:47:23 +0000 | [diff] [blame] | 8622 | // If we're actually extending zero bits, then if |
| 8623 | // SrcSize < DstSize: zext(a & mask) |
| 8624 | // SrcSize == DstSize: a & mask |
| 8625 | // SrcSize > DstSize: trunc(a) & mask |
| 8626 | if (SrcSize < DstSize) { |
| 8627 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8628 | Constant *AndConst = Context->getConstantInt(A->getType(), AndValue); |
| Chris Lattner | 215d56e | 2009-02-17 20:47:23 +0000 | [diff] [blame] | 8629 | Instruction *And = |
| 8630 | BinaryOperator::CreateAnd(A, AndConst, CSrc->getName()+".mask"); |
| 8631 | InsertNewInstBefore(And, CI); |
| 8632 | return new ZExtInst(And, CI.getType()); |
| 8633 | } else if (SrcSize == DstSize) { |
| 8634 | APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8635 | return BinaryOperator::CreateAnd(A, Context->getConstantInt(A->getType(), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8636 | AndValue)); |
| Chris Lattner | 215d56e | 2009-02-17 20:47:23 +0000 | [diff] [blame] | 8637 | } else if (SrcSize > DstSize) { |
| 8638 | Instruction *Trunc = new TruncInst(A, CI.getType(), "tmp"); |
| 8639 | InsertNewInstBefore(Trunc, CI); |
| 8640 | APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8641 | return BinaryOperator::CreateAnd(Trunc, |
| 8642 | Context->getConstantInt(Trunc->getType(), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8643 | AndValue)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8644 | } |
| 8645 | } |
| 8646 | |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8647 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) |
| 8648 | return transformZExtICmp(ICI, CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8649 | |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8650 | BinaryOperator *SrcI = dyn_cast<BinaryOperator>(Src); |
| 8651 | if (SrcI && SrcI->getOpcode() == Instruction::Or) { |
| 8652 | // zext (or icmp, icmp) --> or (zext icmp), (zext icmp) if at least one |
| 8653 | // of the (zext icmp) will be transformed. |
| 8654 | ICmpInst *LHS = dyn_cast<ICmpInst>(SrcI->getOperand(0)); |
| 8655 | ICmpInst *RHS = dyn_cast<ICmpInst>(SrcI->getOperand(1)); |
| 8656 | if (LHS && RHS && LHS->hasOneUse() && RHS->hasOneUse() && |
| 8657 | (transformZExtICmp(LHS, CI, false) || |
| 8658 | transformZExtICmp(RHS, CI, false))) { |
| 8659 | Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI); |
| 8660 | Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8661 | return BinaryOperator::Create(Instruction::Or, LCast, RCast); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8662 | } |
| Evan Cheng | e3779cf | 2008-03-24 00:21:34 +0000 | [diff] [blame] | 8663 | } |
| 8664 | |
| Dan Gohman | 7ac1e4a | 2009-06-18 16:30:21 +0000 | [diff] [blame] | 8665 | // zext(trunc(t) & C) -> (t & zext(C)). |
| Dan Gohman | ead83a5 | 2009-06-17 23:17:05 +0000 | [diff] [blame] | 8666 | if (SrcI && SrcI->getOpcode() == Instruction::And && SrcI->hasOneUse()) |
| 8667 | if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) |
| 8668 | if (TruncInst *TI = dyn_cast<TruncInst>(SrcI->getOperand(0))) { |
| 8669 | Value *TI0 = TI->getOperand(0); |
| Dan Gohman | 7ac1e4a | 2009-06-18 16:30:21 +0000 | [diff] [blame] | 8670 | if (TI0->getType() == CI.getType()) |
| 8671 | return |
| 8672 | BinaryOperator::CreateAnd(TI0, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8673 | Context->getConstantExprZExt(C, CI.getType())); |
| Dan Gohman | ead83a5 | 2009-06-17 23:17:05 +0000 | [diff] [blame] | 8674 | } |
| 8675 | |
| Dan Gohman | 7ac1e4a | 2009-06-18 16:30:21 +0000 | [diff] [blame] | 8676 | // zext((trunc(t) & C) ^ C) -> ((t & zext(C)) ^ zext(C)). |
| 8677 | if (SrcI && SrcI->getOpcode() == Instruction::Xor && SrcI->hasOneUse()) |
| 8678 | if (ConstantInt *C = dyn_cast<ConstantInt>(SrcI->getOperand(1))) |
| 8679 | if (BinaryOperator *And = dyn_cast<BinaryOperator>(SrcI->getOperand(0))) |
| 8680 | if (And->getOpcode() == Instruction::And && And->hasOneUse() && |
| 8681 | And->getOperand(1) == C) |
| 8682 | if (TruncInst *TI = dyn_cast<TruncInst>(And->getOperand(0))) { |
| 8683 | Value *TI0 = TI->getOperand(0); |
| 8684 | if (TI0->getType() == CI.getType()) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8685 | Constant *ZC = Context->getConstantExprZExt(C, CI.getType()); |
| Dan Gohman | 7ac1e4a | 2009-06-18 16:30:21 +0000 | [diff] [blame] | 8686 | Instruction *NewAnd = BinaryOperator::CreateAnd(TI0, ZC, "tmp"); |
| 8687 | InsertNewInstBefore(NewAnd, *And); |
| 8688 | return BinaryOperator::CreateXor(NewAnd, ZC); |
| 8689 | } |
| 8690 | } |
| 8691 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8692 | return 0; |
| 8693 | } |
| 8694 | |
| 8695 | Instruction *InstCombiner::visitSExt(SExtInst &CI) { |
| 8696 | if (Instruction *I = commonIntCastTransforms(CI)) |
| 8697 | return I; |
| 8698 | |
| 8699 | Value *Src = CI.getOperand(0); |
| 8700 | |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 8701 | // Canonicalize sign-extend from i1 to a select. |
| 8702 | if (Src->getType() == Type::Int1Ty) |
| 8703 | return SelectInst::Create(Src, |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 8704 | Context->getAllOnesValue(CI.getType()), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8705 | Context->getNullValue(CI.getType())); |
| Dan Gohman | f0f1202 | 2008-05-20 21:01:12 +0000 | [diff] [blame] | 8706 | |
| 8707 | // See if the value being truncated is already sign extended. If so, just |
| 8708 | // eliminate the trunc/sext pair. |
| Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 8709 | if (Operator::getOpcode(Src) == Instruction::Trunc) { |
| Dan Gohman | f0f1202 | 2008-05-20 21:01:12 +0000 | [diff] [blame] | 8710 | Value *Op = cast<User>(Src)->getOperand(0); |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8711 | unsigned OpBits = Op->getType()->getScalarSizeInBits(); |
| 8712 | unsigned MidBits = Src->getType()->getScalarSizeInBits(); |
| 8713 | unsigned DestBits = CI.getType()->getScalarSizeInBits(); |
| Dan Gohman | f0f1202 | 2008-05-20 21:01:12 +0000 | [diff] [blame] | 8714 | unsigned NumSignBits = ComputeNumSignBits(Op); |
| 8715 | |
| 8716 | if (OpBits == DestBits) { |
| 8717 | // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign |
| 8718 | // bits, it is already ready. |
| 8719 | if (NumSignBits > DestBits-MidBits) |
| 8720 | return ReplaceInstUsesWith(CI, Op); |
| 8721 | } else if (OpBits < DestBits) { |
| 8722 | // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign |
| 8723 | // bits, just sext from i32. |
| 8724 | if (NumSignBits > OpBits-MidBits) |
| 8725 | return new SExtInst(Op, CI.getType(), "tmp"); |
| 8726 | } else { |
| 8727 | // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign |
| 8728 | // bits, just truncate to i32. |
| 8729 | if (NumSignBits > OpBits-MidBits) |
| 8730 | return new TruncInst(Op, CI.getType(), "tmp"); |
| 8731 | } |
| 8732 | } |
| Chris Lattner | 8a2d059 | 2008-08-06 07:35:52 +0000 | [diff] [blame] | 8733 | |
| 8734 | // If the input is a shl/ashr pair of a same constant, then this is a sign |
| 8735 | // extension from a smaller value. If we could trust arbitrary bitwidth |
| 8736 | // integers, we could turn this into a truncate to the smaller bit and then |
| 8737 | // use a sext for the whole extension. Since we don't, look deeper and check |
| 8738 | // for a truncate. If the source and dest are the same type, eliminate the |
| 8739 | // trunc and extend and just do shifts. For example, turn: |
| 8740 | // %a = trunc i32 %i to i8 |
| 8741 | // %b = shl i8 %a, 6 |
| 8742 | // %c = ashr i8 %b, 6 |
| 8743 | // %d = sext i8 %c to i32 |
| 8744 | // into: |
| 8745 | // %a = shl i32 %i, 30 |
| 8746 | // %d = ashr i32 %a, 30 |
| 8747 | Value *A = 0; |
| 8748 | ConstantInt *BA = 0, *CA = 0; |
| 8749 | if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)), |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 8750 | m_ConstantInt(CA)), *Context) && |
| Chris Lattner | 8a2d059 | 2008-08-06 07:35:52 +0000 | [diff] [blame] | 8751 | BA == CA && isa<TruncInst>(A)) { |
| 8752 | Value *I = cast<TruncInst>(A)->getOperand(0); |
| 8753 | if (I->getType() == CI.getType()) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8754 | unsigned MidSize = Src->getType()->getScalarSizeInBits(); |
| 8755 | unsigned SrcDstSize = CI.getType()->getScalarSizeInBits(); |
| Chris Lattner | 8a2d059 | 2008-08-06 07:35:52 +0000 | [diff] [blame] | 8756 | unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8757 | Constant *ShAmtV = Context->getConstantInt(CI.getType(), ShAmt); |
| Chris Lattner | 8a2d059 | 2008-08-06 07:35:52 +0000 | [diff] [blame] | 8758 | I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV, |
| 8759 | CI.getName()), CI); |
| 8760 | return BinaryOperator::CreateAShr(I, ShAmtV); |
| 8761 | } |
| 8762 | } |
| 8763 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8764 | return 0; |
| 8765 | } |
| 8766 | |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8767 | /// FitsInFPType - Return a Constant* for the specified FP constant if it fits |
| 8768 | /// in the specified FP type without changing its value. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8769 | static Constant *FitsInFPType(ConstantFP *CFP, const fltSemantics &Sem, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 8770 | LLVMContext *Context) { |
| Dale Johannesen | 6e547b4 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 8771 | bool losesInfo; |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8772 | APFloat F = CFP->getValueAPF(); |
| Dale Johannesen | 6e547b4 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 8773 | (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); |
| 8774 | if (!losesInfo) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8775 | return Context->getConstantFP(F); |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8776 | return 0; |
| 8777 | } |
| 8778 | |
| 8779 | /// LookThroughFPExtensions - If this is an fp extension instruction, look |
| 8780 | /// through it until we get the source value. |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 8781 | static Value *LookThroughFPExtensions(Value *V, LLVMContext *Context) { |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8782 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 8783 | if (I->getOpcode() == Instruction::FPExt) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8784 | return LookThroughFPExtensions(I->getOperand(0), Context); |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8785 | |
| 8786 | // If this value is a constant, return the constant in the smallest FP type |
| 8787 | // that can accurately represent it. This allows us to turn |
| 8788 | // (float)((double)X+2.0) into x+2.0f. |
| 8789 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 8790 | if (CFP->getType() == Type::PPC_FP128Ty) |
| 8791 | return V; // No constant folding of this. |
| 8792 | // See if the value can be truncated to float and then reextended. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8793 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEsingle, Context)) |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8794 | return V; |
| 8795 | if (CFP->getType() == Type::DoubleTy) |
| 8796 | return V; // Won't shrink. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8797 | if (Value *V = FitsInFPType(CFP, APFloat::IEEEdouble, Context)) |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8798 | return V; |
| 8799 | // Don't try to shrink to various long double types. |
| 8800 | } |
| 8801 | |
| 8802 | return V; |
| 8803 | } |
| 8804 | |
| 8805 | Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) { |
| 8806 | if (Instruction *I = commonCastTransforms(CI)) |
| 8807 | return I; |
| 8808 | |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 8809 | // If we have fptrunc(fadd (fpextend x), (fpextend y)), where x and y are |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8810 | // smaller than the destination type, we can eliminate the truncate by doing |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 8811 | // the add as the smaller type. This applies to fadd/fsub/fmul/fdiv as well as |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8812 | // many builtins (sqrt, etc). |
| 8813 | BinaryOperator *OpI = dyn_cast<BinaryOperator>(CI.getOperand(0)); |
| 8814 | if (OpI && OpI->hasOneUse()) { |
| 8815 | switch (OpI->getOpcode()) { |
| 8816 | default: break; |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 8817 | case Instruction::FAdd: |
| 8818 | case Instruction::FSub: |
| 8819 | case Instruction::FMul: |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8820 | case Instruction::FDiv: |
| 8821 | case Instruction::FRem: |
| 8822 | const Type *SrcTy = OpI->getType(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8823 | Value *LHSTrunc = LookThroughFPExtensions(OpI->getOperand(0), Context); |
| 8824 | Value *RHSTrunc = LookThroughFPExtensions(OpI->getOperand(1), Context); |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8825 | if (LHSTrunc->getType() != SrcTy && |
| 8826 | RHSTrunc->getType() != SrcTy) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8827 | unsigned DstSize = CI.getType()->getScalarSizeInBits(); |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8828 | // If the source types were both smaller than the destination type of |
| 8829 | // the cast, do this xform. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8830 | if (LHSTrunc->getType()->getScalarSizeInBits() <= DstSize && |
| 8831 | RHSTrunc->getType()->getScalarSizeInBits() <= DstSize) { |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8832 | LHSTrunc = InsertCastBefore(Instruction::FPExt, LHSTrunc, |
| 8833 | CI.getType(), CI); |
| 8834 | RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc, |
| 8835 | CI.getType(), CI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 8836 | return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc); |
| Chris Lattner | df7e840 | 2008-01-27 05:29:54 +0000 | [diff] [blame] | 8837 | } |
| 8838 | } |
| 8839 | break; |
| 8840 | } |
| 8841 | } |
| 8842 | return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
| 8845 | Instruction *InstCombiner::visitFPExt(CastInst &CI) { |
| 8846 | return commonCastTransforms(CI); |
| 8847 | } |
| 8848 | |
| Chris Lattner | deef1a7 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8849 | Instruction *InstCombiner::visitFPToUI(FPToUIInst &FI) { |
| Chris Lattner | 5f4d691 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 8850 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 8851 | if (OpI == 0) |
| 8852 | return commonCastTransforms(FI); |
| 8853 | |
| 8854 | // fptoui(uitofp(X)) --> X |
| 8855 | // fptoui(sitofp(X)) --> X |
| 8856 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 8857 | // accurately represent all values of X. For example, do not do this with |
| 8858 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 8859 | // 'X' value would cause an undefined result for the fptoui. |
| 8860 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 8861 | OpI->getOperand(0)->getType() == FI.getType() && |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8862 | (int)FI.getType()->getScalarSizeInBits() < /*extra bit for sign */ |
| Chris Lattner | 5f4d691 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 8863 | OpI->getType()->getFPMantissaWidth()) |
| 8864 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
| Chris Lattner | deef1a7 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8865 | |
| 8866 | return commonCastTransforms(FI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8867 | } |
| 8868 | |
| Chris Lattner | deef1a7 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8869 | Instruction *InstCombiner::visitFPToSI(FPToSIInst &FI) { |
| Chris Lattner | 5f4d691 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 8870 | Instruction *OpI = dyn_cast<Instruction>(FI.getOperand(0)); |
| 8871 | if (OpI == 0) |
| 8872 | return commonCastTransforms(FI); |
| 8873 | |
| 8874 | // fptosi(sitofp(X)) --> X |
| 8875 | // fptosi(uitofp(X)) --> X |
| 8876 | // This is safe if the intermediate type has enough bits in its mantissa to |
| 8877 | // accurately represent all values of X. For example, do not do this with |
| 8878 | // i64->float->i64. This is also safe for sitofp case, because any negative |
| 8879 | // 'X' value would cause an undefined result for the fptoui. |
| 8880 | if ((isa<UIToFPInst>(OpI) || isa<SIToFPInst>(OpI)) && |
| 8881 | OpI->getOperand(0)->getType() == FI.getType() && |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 8882 | (int)FI.getType()->getScalarSizeInBits() <= |
| Chris Lattner | 5f4d691 | 2008-08-06 05:13:06 +0000 | [diff] [blame] | 8883 | OpI->getType()->getFPMantissaWidth()) |
| 8884 | return ReplaceInstUsesWith(FI, OpI->getOperand(0)); |
| Chris Lattner | deef1a7 | 2008-05-19 20:25:04 +0000 | [diff] [blame] | 8885 | |
| 8886 | return commonCastTransforms(FI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8887 | } |
| 8888 | |
| 8889 | Instruction *InstCombiner::visitUIToFP(CastInst &CI) { |
| 8890 | return commonCastTransforms(CI); |
| 8891 | } |
| 8892 | |
| 8893 | Instruction *InstCombiner::visitSIToFP(CastInst &CI) { |
| 8894 | return commonCastTransforms(CI); |
| 8895 | } |
| 8896 | |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 8897 | Instruction *InstCombiner::visitPtrToInt(PtrToIntInst &CI) { |
| 8898 | // If the destination integer type is smaller than the intptr_t type for |
| 8899 | // this target, do a ptrtoint to intptr_t then do a trunc. This allows the |
| 8900 | // trunc to be exposed to other transforms. Don't do this for extending |
| 8901 | // ptrtoint's, because we don't know if the target sign or zero extends its |
| 8902 | // pointers. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 8903 | if (TD && |
| 8904 | CI.getType()->getScalarSizeInBits() < TD->getPointerSizeInBits()) { |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 8905 | Value *P = InsertNewInstBefore(new PtrToIntInst(CI.getOperand(0), |
| 8906 | TD->getIntPtrType(), |
| 8907 | "tmp"), CI); |
| 8908 | return new TruncInst(P, CI.getType()); |
| 8909 | } |
| 8910 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8911 | return commonPointerCastTransforms(CI); |
| 8912 | } |
| 8913 | |
| Chris Lattner | 7c162648 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8914 | Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 8915 | // If the source integer type is larger than the intptr_t type for |
| 8916 | // this target, do a trunc to the intptr_t type, then inttoptr of it. This |
| 8917 | // allows the trunc to be exposed to other transforms. Don't do this for |
| 8918 | // extending inttoptr's, because we don't know if the target sign or zero |
| 8919 | // extends to pointers. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 8920 | if (TD && |
| 8921 | CI.getOperand(0)->getType()->getScalarSizeInBits() > |
| Chris Lattner | 3e10f8d | 2009-03-24 18:35:40 +0000 | [diff] [blame] | 8922 | TD->getPointerSizeInBits()) { |
| 8923 | Value *P = InsertNewInstBefore(new TruncInst(CI.getOperand(0), |
| 8924 | TD->getIntPtrType(), |
| 8925 | "tmp"), CI); |
| 8926 | return new IntToPtrInst(P, CI.getType()); |
| 8927 | } |
| 8928 | |
| Chris Lattner | 7c162648 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8929 | if (Instruction *I = commonCastTransforms(CI)) |
| 8930 | return I; |
| Chris Lattner | 7c162648 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8931 | |
| Chris Lattner | 7c162648 | 2008-01-08 07:23:51 +0000 | [diff] [blame] | 8932 | return 0; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8933 | } |
| 8934 | |
| 8935 | Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { |
| 8936 | // If the operands are integer typed then apply the integer transforms, |
| 8937 | // otherwise just apply the common ones. |
| 8938 | Value *Src = CI.getOperand(0); |
| 8939 | const Type *SrcTy = Src->getType(); |
| 8940 | const Type *DestTy = CI.getType(); |
| 8941 | |
| Eli Friedman | 5013d3f | 2009-07-13 20:53:00 +0000 | [diff] [blame] | 8942 | if (isa<PointerType>(SrcTy)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8943 | if (Instruction *I = commonPointerCastTransforms(CI)) |
| 8944 | return I; |
| 8945 | } else { |
| 8946 | if (Instruction *Result = commonCastTransforms(CI)) |
| 8947 | return Result; |
| 8948 | } |
| 8949 | |
| 8950 | |
| 8951 | // Get rid of casts from one type to the same type. These are useless and can |
| 8952 | // be replaced by the operand. |
| 8953 | if (DestTy == Src->getType()) |
| 8954 | return ReplaceInstUsesWith(CI, Src); |
| 8955 | |
| 8956 | if (const PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) { |
| 8957 | const PointerType *SrcPTy = cast<PointerType>(SrcTy); |
| 8958 | const Type *DstElTy = DstPTy->getElementType(); |
| 8959 | const Type *SrcElTy = SrcPTy->getElementType(); |
| 8960 | |
| Nate Begeman | df5b361 | 2008-03-31 00:22:16 +0000 | [diff] [blame] | 8961 | // If the address spaces don't match, don't eliminate the bitcast, which is |
| 8962 | // required for changing types. |
| 8963 | if (SrcPTy->getAddressSpace() != DstPTy->getAddressSpace()) |
| 8964 | return 0; |
| 8965 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8966 | // If we are casting a malloc or alloca to a pointer to a type of the same |
| 8967 | // size, rewrite the allocation instruction to allocate the "right" type. |
| 8968 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Src)) |
| 8969 | if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) |
| 8970 | return V; |
| 8971 | |
| 8972 | // If the source and destination are pointers, and this cast is equivalent |
| 8973 | // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. |
| 8974 | // This can enhance SROA and other transforms that want type-safe pointers. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 8975 | Constant *ZeroUInt = Context->getNullValue(Type::Int32Ty); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8976 | unsigned NumZeros = 0; |
| 8977 | while (SrcElTy != DstElTy && |
| 8978 | isa<CompositeType>(SrcElTy) && !isa<PointerType>(SrcElTy) && |
| 8979 | SrcElTy->getNumContainedTypes() /* not "{}" */) { |
| 8980 | SrcElTy = cast<CompositeType>(SrcElTy)->getTypeAtIndex(ZeroUInt); |
| 8981 | ++NumZeros; |
| 8982 | } |
| 8983 | |
| 8984 | // If we found a path from the src to dest, create the getelementptr now. |
| 8985 | if (SrcElTy == DstElTy) { |
| 8986 | SmallVector<Value*, 8> Idxs(NumZeros+1, ZeroUInt); |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 8987 | return GetElementPtrInst::Create(Src, Idxs.begin(), Idxs.end(), "", |
| 8988 | ((Instruction*) NULL)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 8989 | } |
| 8990 | } |
| 8991 | |
| Eli Friedman | 1d31dee | 2009-07-18 23:06:53 +0000 | [diff] [blame] | 8992 | if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { |
| 8993 | if (DestVTy->getNumElements() == 1) { |
| 8994 | if (!isa<VectorType>(SrcTy)) { |
| 8995 | Value *Elem = InsertCastBefore(Instruction::BitCast, Src, |
| 8996 | DestVTy->getElementType(), CI); |
| 8997 | return InsertElementInst::Create(Context->getUndef(DestTy), Elem, |
| 8998 | Context->getNullValue(Type::Int32Ty)); |
| 8999 | } |
| 9000 | // FIXME: Canonicalize bitcast(insertelement) -> insertelement(bitcast) |
| 9001 | } |
| 9002 | } |
| 9003 | |
| 9004 | if (const VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy)) { |
| 9005 | if (SrcVTy->getNumElements() == 1) { |
| 9006 | if (!isa<VectorType>(DestTy)) { |
| 9007 | Instruction *Elem = |
| 9008 | new ExtractElementInst(Src, Context->getNullValue(Type::Int32Ty)); |
| 9009 | InsertNewInstBefore(Elem, CI); |
| 9010 | return CastInst::Create(Instruction::BitCast, Elem, DestTy); |
| 9011 | } |
| 9012 | } |
| 9013 | } |
| 9014 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9015 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(Src)) { |
| 9016 | if (SVI->hasOneUse()) { |
| 9017 | // Okay, we have (bitconvert (shuffle ..)). Check to see if this is |
| 9018 | // a bitconvert to a vector with the same # elts. |
| 9019 | if (isa<VectorType>(DestTy) && |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 9020 | cast<VectorType>(DestTy)->getNumElements() == |
| 9021 | SVI->getType()->getNumElements() && |
| 9022 | SVI->getType()->getNumElements() == |
| 9023 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9024 | CastInst *Tmp; |
| 9025 | // If either of the operands is a cast from CI.getType(), then |
| 9026 | // evaluating the shuffle in the casted destination's type will allow |
| 9027 | // us to eliminate at least one cast. |
| 9028 | if (((Tmp = dyn_cast<CastInst>(SVI->getOperand(0))) && |
| 9029 | Tmp->getOperand(0)->getType() == DestTy) || |
| 9030 | ((Tmp = dyn_cast<CastInst>(SVI->getOperand(1))) && |
| 9031 | Tmp->getOperand(0)->getType() == DestTy)) { |
| Eli Friedman | 722b479 | 2008-11-30 21:09:11 +0000 | [diff] [blame] | 9032 | Value *LHS = InsertCastBefore(Instruction::BitCast, |
| 9033 | SVI->getOperand(0), DestTy, CI); |
| 9034 | Value *RHS = InsertCastBefore(Instruction::BitCast, |
| 9035 | SVI->getOperand(1), DestTy, CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9036 | // Return a new shuffle vector. Use the same element ID's, as we |
| 9037 | // know the vector types match #elts. |
| 9038 | return new ShuffleVectorInst(LHS, RHS, SVI->getOperand(2)); |
| 9039 | } |
| 9040 | } |
| 9041 | } |
| 9042 | } |
| 9043 | return 0; |
| 9044 | } |
| 9045 | |
| 9046 | /// GetSelectFoldableOperands - We want to turn code that looks like this: |
| 9047 | /// %C = or %A, %B |
| 9048 | /// %D = select %cond, %C, %A |
| 9049 | /// into: |
| 9050 | /// %C = select %cond, %B, 0 |
| 9051 | /// %D = or %A, %C |
| 9052 | /// |
| 9053 | /// Assuming that the specified instruction is an operand to the select, return |
| 9054 | /// a bitmask indicating which operands of this instruction are foldable if they |
| 9055 | /// equal the other incoming value of the select. |
| 9056 | /// |
| 9057 | static unsigned GetSelectFoldableOperands(Instruction *I) { |
| 9058 | switch (I->getOpcode()) { |
| 9059 | case Instruction::Add: |
| 9060 | case Instruction::Mul: |
| 9061 | case Instruction::And: |
| 9062 | case Instruction::Or: |
| 9063 | case Instruction::Xor: |
| 9064 | return 3; // Can fold through either operand. |
| 9065 | case Instruction::Sub: // Can only fold on the amount subtracted. |
| 9066 | case Instruction::Shl: // Can only fold on the shift amount. |
| 9067 | case Instruction::LShr: |
| 9068 | case Instruction::AShr: |
| 9069 | return 1; |
| 9070 | default: |
| 9071 | return 0; // Cannot fold |
| 9072 | } |
| 9073 | } |
| 9074 | |
| 9075 | /// GetSelectFoldableConstant - For the same transformation as the previous |
| 9076 | /// function, return the identity constant that goes into the select. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9077 | static Constant *GetSelectFoldableConstant(Instruction *I, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 9078 | LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9079 | switch (I->getOpcode()) { |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9080 | default: llvm_unreachable("This cannot happen!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9081 | case Instruction::Add: |
| 9082 | case Instruction::Sub: |
| 9083 | case Instruction::Or: |
| 9084 | case Instruction::Xor: |
| 9085 | case Instruction::Shl: |
| 9086 | case Instruction::LShr: |
| 9087 | case Instruction::AShr: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9088 | return Context->getNullValue(I->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9089 | case Instruction::And: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9090 | return Context->getAllOnesValue(I->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9091 | case Instruction::Mul: |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9092 | return Context->getConstantInt(I->getType(), 1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9093 | } |
| 9094 | } |
| 9095 | |
| 9096 | /// FoldSelectOpOp - Here we have (select c, TI, FI), and we know that TI and FI |
| 9097 | /// have the same opcode and only one use each. Try to simplify this. |
| 9098 | Instruction *InstCombiner::FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 9099 | Instruction *FI) { |
| 9100 | if (TI->getNumOperands() == 1) { |
| 9101 | // If this is a non-volatile load or a cast from the same type, |
| 9102 | // merge. |
| 9103 | if (TI->isCast()) { |
| 9104 | if (TI->getOperand(0)->getType() != FI->getOperand(0)->getType()) |
| 9105 | return 0; |
| 9106 | } else { |
| 9107 | return 0; // unknown unary op. |
| 9108 | } |
| 9109 | |
| 9110 | // Fold this by inserting a select from the input values. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9111 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0), |
| 9112 | FI->getOperand(0), SI.getName()+".v"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9113 | InsertNewInstBefore(NewSI, SI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9114 | return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9115 | TI->getType()); |
| 9116 | } |
| 9117 | |
| 9118 | // Only handle binary operators here. |
| 9119 | if (!isa<BinaryOperator>(TI)) |
| 9120 | return 0; |
| 9121 | |
| 9122 | // Figure out if the operations have any operands in common. |
| 9123 | Value *MatchOp, *OtherOpT, *OtherOpF; |
| 9124 | bool MatchIsOpZero; |
| 9125 | if (TI->getOperand(0) == FI->getOperand(0)) { |
| 9126 | MatchOp = TI->getOperand(0); |
| 9127 | OtherOpT = TI->getOperand(1); |
| 9128 | OtherOpF = FI->getOperand(1); |
| 9129 | MatchIsOpZero = true; |
| 9130 | } else if (TI->getOperand(1) == FI->getOperand(1)) { |
| 9131 | MatchOp = TI->getOperand(1); |
| 9132 | OtherOpT = TI->getOperand(0); |
| 9133 | OtherOpF = FI->getOperand(0); |
| 9134 | MatchIsOpZero = false; |
| 9135 | } else if (!TI->isCommutative()) { |
| 9136 | return 0; |
| 9137 | } else if (TI->getOperand(0) == FI->getOperand(1)) { |
| 9138 | MatchOp = TI->getOperand(0); |
| 9139 | OtherOpT = TI->getOperand(1); |
| 9140 | OtherOpF = FI->getOperand(0); |
| 9141 | MatchIsOpZero = true; |
| 9142 | } else if (TI->getOperand(1) == FI->getOperand(0)) { |
| 9143 | MatchOp = TI->getOperand(1); |
| 9144 | OtherOpT = TI->getOperand(0); |
| 9145 | OtherOpF = FI->getOperand(1); |
| 9146 | MatchIsOpZero = true; |
| 9147 | } else { |
| 9148 | return 0; |
| 9149 | } |
| 9150 | |
| 9151 | // If we reach here, they do have operations in common. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 9152 | SelectInst *NewSI = SelectInst::Create(SI.getCondition(), OtherOpT, |
| 9153 | OtherOpF, SI.getName()+".v"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9154 | InsertNewInstBefore(NewSI, SI); |
| 9155 | |
| 9156 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TI)) { |
| 9157 | if (MatchIsOpZero) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9158 | return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9159 | else |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9160 | return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9161 | } |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9162 | llvm_unreachable("Shouldn't get here"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9163 | return 0; |
| 9164 | } |
| 9165 | |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9166 | static bool isSelect01(Constant *C1, Constant *C2) { |
| 9167 | ConstantInt *C1I = dyn_cast<ConstantInt>(C1); |
| 9168 | if (!C1I) |
| 9169 | return false; |
| 9170 | ConstantInt *C2I = dyn_cast<ConstantInt>(C2); |
| 9171 | if (!C2I) |
| 9172 | return false; |
| 9173 | return (C1I->isZero() || C1I->isOne()) && (C2I->isZero() || C2I->isOne()); |
| 9174 | } |
| 9175 | |
| 9176 | /// FoldSelectIntoOp - Try fold the select into one of the operands to |
| 9177 | /// facilitate further optimization. |
| 9178 | Instruction *InstCombiner::FoldSelectIntoOp(SelectInst &SI, Value *TrueVal, |
| 9179 | Value *FalseVal) { |
| 9180 | // See the comment above GetSelectFoldableOperands for a description of the |
| 9181 | // transformation we are doing here. |
| 9182 | if (Instruction *TVI = dyn_cast<Instruction>(TrueVal)) { |
| 9183 | if (TVI->hasOneUse() && TVI->getNumOperands() == 2 && |
| 9184 | !isa<Constant>(FalseVal)) { |
| 9185 | if (unsigned SFO = GetSelectFoldableOperands(TVI)) { |
| 9186 | unsigned OpToFold = 0; |
| 9187 | if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { |
| 9188 | OpToFold = 1; |
| 9189 | } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { |
| 9190 | OpToFold = 2; |
| 9191 | } |
| 9192 | |
| 9193 | if (OpToFold) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9194 | Constant *C = GetSelectFoldableConstant(TVI, Context); |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9195 | Value *OOp = TVI->getOperand(2-OpToFold); |
| 9196 | // Avoid creating select between 2 constants unless it's selecting |
| 9197 | // between 0 and 1. |
| 9198 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
| 9199 | Instruction *NewSel = SelectInst::Create(SI.getCondition(), OOp, C); |
| 9200 | InsertNewInstBefore(NewSel, SI); |
| 9201 | NewSel->takeName(TVI); |
| 9202 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(TVI)) |
| 9203 | return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel); |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9204 | llvm_unreachable("Unknown instruction!!"); |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9205 | } |
| 9206 | } |
| 9207 | } |
| 9208 | } |
| 9209 | } |
| 9210 | |
| 9211 | if (Instruction *FVI = dyn_cast<Instruction>(FalseVal)) { |
| 9212 | if (FVI->hasOneUse() && FVI->getNumOperands() == 2 && |
| 9213 | !isa<Constant>(TrueVal)) { |
| 9214 | if (unsigned SFO = GetSelectFoldableOperands(FVI)) { |
| 9215 | unsigned OpToFold = 0; |
| 9216 | if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { |
| 9217 | OpToFold = 1; |
| 9218 | } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { |
| 9219 | OpToFold = 2; |
| 9220 | } |
| 9221 | |
| 9222 | if (OpToFold) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9223 | Constant *C = GetSelectFoldableConstant(FVI, Context); |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9224 | Value *OOp = FVI->getOperand(2-OpToFold); |
| 9225 | // Avoid creating select between 2 constants unless it's selecting |
| 9226 | // between 0 and 1. |
| 9227 | if (!isa<Constant>(OOp) || isSelect01(C, cast<Constant>(OOp))) { |
| 9228 | Instruction *NewSel = SelectInst::Create(SI.getCondition(), C, OOp); |
| 9229 | InsertNewInstBefore(NewSel, SI); |
| 9230 | NewSel->takeName(FVI); |
| 9231 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FVI)) |
| 9232 | return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel); |
| Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 9233 | llvm_unreachable("Unknown instruction!!"); |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9234 | } |
| 9235 | } |
| 9236 | } |
| 9237 | } |
| 9238 | } |
| 9239 | |
| 9240 | return 0; |
| 9241 | } |
| 9242 | |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9243 | /// visitSelectInstWithICmp - Visit a SelectInst that has an |
| 9244 | /// ICmpInst as its first operand. |
| 9245 | /// |
| 9246 | Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, |
| 9247 | ICmpInst *ICI) { |
| 9248 | bool Changed = false; |
| 9249 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 9250 | Value *CmpLHS = ICI->getOperand(0); |
| 9251 | Value *CmpRHS = ICI->getOperand(1); |
| 9252 | Value *TrueVal = SI.getTrueValue(); |
| 9253 | Value *FalseVal = SI.getFalseValue(); |
| 9254 | |
| 9255 | // Check cases where the comparison is with a constant that |
| 9256 | // can be adjusted to fit the min/max idiom. We may edit ICI in |
| 9257 | // place here, so make sure the select is the only user. |
| 9258 | if (ICI->hasOneUse()) |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9259 | if (ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) { |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9260 | switch (Pred) { |
| 9261 | default: break; |
| 9262 | case ICmpInst::ICMP_ULT: |
| 9263 | case ICmpInst::ICMP_SLT: { |
| 9264 | // X < MIN ? T : F --> F |
| 9265 | if (CI->isMinValue(Pred == ICmpInst::ICMP_SLT)) |
| 9266 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9267 | // X < C ? X : C-1 --> X > C-1 ? C-1 : X |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9268 | Constant *AdjustedRHS = SubOne(CI, Context); |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9269 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 9270 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 9271 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 9272 | CmpRHS = AdjustedRHS; |
| 9273 | std::swap(FalseVal, TrueVal); |
| 9274 | ICI->setPredicate(Pred); |
| 9275 | ICI->setOperand(1, CmpRHS); |
| 9276 | SI.setOperand(1, TrueVal); |
| 9277 | SI.setOperand(2, FalseVal); |
| 9278 | Changed = true; |
| 9279 | } |
| 9280 | break; |
| 9281 | } |
| 9282 | case ICmpInst::ICMP_UGT: |
| 9283 | case ICmpInst::ICMP_SGT: { |
| 9284 | // X > MAX ? T : F --> F |
| 9285 | if (CI->isMaxValue(Pred == ICmpInst::ICMP_SGT)) |
| 9286 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9287 | // X > C ? X : C+1 --> X < C+1 ? C+1 : X |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9288 | Constant *AdjustedRHS = AddOne(CI, Context); |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9289 | if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || |
| 9290 | (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { |
| 9291 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 9292 | CmpRHS = AdjustedRHS; |
| 9293 | std::swap(FalseVal, TrueVal); |
| 9294 | ICI->setPredicate(Pred); |
| 9295 | ICI->setOperand(1, CmpRHS); |
| 9296 | SI.setOperand(1, TrueVal); |
| 9297 | SI.setOperand(2, FalseVal); |
| 9298 | Changed = true; |
| 9299 | } |
| 9300 | break; |
| 9301 | } |
| 9302 | } |
| 9303 | |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9304 | // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if signed |
| 9305 | // (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 9306 | CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 9307 | if (match(TrueVal, m_ConstantInt<-1>(), *Context) && |
| 9308 | match(FalseVal, m_ConstantInt<0>(), *Context)) |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 9309 | Pred = ICI->getPredicate(); |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 9310 | else if (match(TrueVal, m_ConstantInt<0>(), *Context) && |
| 9311 | match(FalseVal, m_ConstantInt<-1>(), *Context)) |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 9312 | Pred = CmpInst::getInversePredicate(ICI->getPredicate()); |
| 9313 | |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9314 | if (Pred != CmpInst::BAD_ICMP_PREDICATE) { |
| 9315 | // If we are just checking for a icmp eq of a single bit and zext'ing it |
| 9316 | // to an integer, then shift the bit to the appropriate place and then |
| 9317 | // cast to integer to avoid the comparison. |
| 9318 | const APInt &Op1CV = CI->getValue(); |
| 9319 | |
| 9320 | // sext (x <s 0) to i32 --> x>>s31 true if signbit set. |
| 9321 | // sext (x >s -1) to i32 --> (x>>s31)^-1 true if signbit clear. |
| 9322 | if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) || |
| Chris Lattner | 3b87408 | 2008-11-16 05:38:51 +0000 | [diff] [blame] | 9323 | (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) { |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9324 | Value *In = ICI->getOperand(0); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9325 | Value *Sh = Context->getConstantInt(In->getType(), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 9326 | In->getType()->getScalarSizeInBits()-1); |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9327 | In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh, |
| 9328 | In->getName()+".lobit"), |
| 9329 | *ICI); |
| Dan Gohman | 47a6077 | 2008-11-02 00:17:33 +0000 | [diff] [blame] | 9330 | if (In->getType() != SI.getType()) |
| 9331 | In = CastInst::CreateIntegerCast(In, SI.getType(), |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9332 | true/*SExt*/, "tmp", ICI); |
| 9333 | |
| 9334 | if (Pred == ICmpInst::ICMP_SGT) |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 9335 | In = InsertNewInstBefore(BinaryOperator::CreateNot(*Context, In, |
| Dan Gohman | 35b7616 | 2008-10-30 20:40:10 +0000 | [diff] [blame] | 9336 | In->getName()+".not"), *ICI); |
| 9337 | |
| 9338 | return ReplaceInstUsesWith(SI, In); |
| 9339 | } |
| 9340 | } |
| 9341 | } |
| 9342 | |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9343 | if (CmpLHS == TrueVal && CmpRHS == FalseVal) { |
| 9344 | // Transform (X == Y) ? X : Y -> Y |
| 9345 | if (Pred == ICmpInst::ICMP_EQ) |
| 9346 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9347 | // Transform (X != Y) ? X : Y -> X |
| 9348 | if (Pred == ICmpInst::ICMP_NE) |
| 9349 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9350 | /// NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 9351 | |
| 9352 | } else if (CmpLHS == FalseVal && CmpRHS == TrueVal) { |
| 9353 | // Transform (X == Y) ? Y : X -> X |
| 9354 | if (Pred == ICmpInst::ICMP_EQ) |
| 9355 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9356 | // Transform (X != Y) ? Y : X -> Y |
| 9357 | if (Pred == ICmpInst::ICMP_NE) |
| 9358 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9359 | /// NOTE: if we wanted to, this is where to detect integer MIN/MAX |
| 9360 | } |
| 9361 | |
| 9362 | /// NOTE: if we wanted to, this is where to detect integer ABS |
| 9363 | |
| 9364 | return Changed ? &SI : 0; |
| 9365 | } |
| 9366 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9367 | Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { |
| 9368 | Value *CondVal = SI.getCondition(); |
| 9369 | Value *TrueVal = SI.getTrueValue(); |
| 9370 | Value *FalseVal = SI.getFalseValue(); |
| 9371 | |
| 9372 | // select true, X, Y -> X |
| 9373 | // select false, X, Y -> Y |
| 9374 | if (ConstantInt *C = dyn_cast<ConstantInt>(CondVal)) |
| 9375 | return ReplaceInstUsesWith(SI, C->getZExtValue() ? TrueVal : FalseVal); |
| 9376 | |
| 9377 | // select C, X, X -> X |
| 9378 | if (TrueVal == FalseVal) |
| 9379 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9380 | |
| 9381 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 9382 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9383 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 9384 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9385 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 9386 | if (isa<Constant>(TrueVal)) |
| 9387 | return ReplaceInstUsesWith(SI, TrueVal); |
| 9388 | else |
| 9389 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9390 | } |
| 9391 | |
| 9392 | if (SI.getType() == Type::Int1Ty) { |
| 9393 | if (ConstantInt *C = dyn_cast<ConstantInt>(TrueVal)) { |
| 9394 | if (C->getZExtValue()) { |
| 9395 | // Change: A = select B, true, C --> A = or B, C |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9396 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9397 | } else { |
| 9398 | // Change: A = select B, false, C --> A = and !B, C |
| 9399 | Value *NotCond = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 9400 | InsertNewInstBefore(BinaryOperator::CreateNot(*Context, CondVal, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9401 | "not."+CondVal->getName()), SI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9402 | return BinaryOperator::CreateAnd(NotCond, FalseVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9403 | } |
| 9404 | } else if (ConstantInt *C = dyn_cast<ConstantInt>(FalseVal)) { |
| 9405 | if (C->getZExtValue() == false) { |
| 9406 | // Change: A = select B, C, false --> A = and B, C |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9407 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9408 | } else { |
| 9409 | // Change: A = select B, C, true --> A = or !B, C |
| 9410 | Value *NotCond = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 9411 | InsertNewInstBefore(BinaryOperator::CreateNot(*Context, CondVal, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9412 | "not."+CondVal->getName()), SI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9413 | return BinaryOperator::CreateOr(NotCond, TrueVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9414 | } |
| 9415 | } |
| Chris Lattner | 53f85a7 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 9416 | |
| 9417 | // select a, b, a -> a&b |
| 9418 | // select a, a, b -> a|b |
| 9419 | if (CondVal == TrueVal) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9420 | return BinaryOperator::CreateOr(CondVal, FalseVal); |
| Chris Lattner | 53f85a7 | 2007-11-25 21:27:53 +0000 | [diff] [blame] | 9421 | else if (CondVal == FalseVal) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9422 | return BinaryOperator::CreateAnd(CondVal, TrueVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9423 | } |
| 9424 | |
| 9425 | // Selecting between two integer constants? |
| 9426 | if (ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal)) |
| 9427 | if (ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) { |
| 9428 | // select C, 1, 0 -> zext C to int |
| 9429 | if (FalseValC->isZero() && TrueValC->getValue() == 1) { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9430 | return CastInst::Create(Instruction::ZExt, CondVal, SI.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9431 | } else if (TrueValC->isZero() && FalseValC->getValue() == 1) { |
| 9432 | // select C, 0, 1 -> zext !C to int |
| 9433 | Value *NotCond = |
| Owen Anderson | 035d41d | 2009-07-13 20:58:05 +0000 | [diff] [blame] | 9434 | InsertNewInstBefore(BinaryOperator::CreateNot(*Context, CondVal, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9435 | "not."+CondVal->getName()), SI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9436 | return CastInst::Create(Instruction::ZExt, NotCond, SI.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9437 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9438 | |
| 9439 | if (ICmpInst *IC = dyn_cast<ICmpInst>(SI.getCondition())) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9440 | // If one of the constants is zero (we know they can't both be) and we |
| 9441 | // have an icmp instruction with zero, and we have an 'and' with the |
| 9442 | // non-constant value, eliminate this whole mess. This corresponds to |
| 9443 | // cases like this: ((X & 27) ? 27 : 0) |
| 9444 | if (TrueValC->isZero() || FalseValC->isZero()) |
| 9445 | if (IC->isEquality() && isa<ConstantInt>(IC->getOperand(1)) && |
| 9446 | cast<Constant>(IC->getOperand(1))->isNullValue()) |
| 9447 | if (Instruction *ICA = dyn_cast<Instruction>(IC->getOperand(0))) |
| 9448 | if (ICA->getOpcode() == Instruction::And && |
| 9449 | isa<ConstantInt>(ICA->getOperand(1)) && |
| 9450 | (ICA->getOperand(1) == TrueValC || |
| 9451 | ICA->getOperand(1) == FalseValC) && |
| 9452 | isOneBitSet(cast<ConstantInt>(ICA->getOperand(1)))) { |
| 9453 | // Okay, now we know that everything is set up, we just don't |
| 9454 | // know whether we have a icmp_ne or icmp_eq and whether the |
| 9455 | // true or false val is the zero. |
| 9456 | bool ShouldNotVal = !TrueValC->isZero(); |
| 9457 | ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE; |
| 9458 | Value *V = ICA; |
| 9459 | if (ShouldNotVal) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9460 | V = InsertNewInstBefore(BinaryOperator::Create( |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9461 | Instruction::Xor, V, ICA->getOperand(1)), SI); |
| 9462 | return ReplaceInstUsesWith(SI, V); |
| 9463 | } |
| 9464 | } |
| 9465 | } |
| 9466 | |
| 9467 | // See if we are selecting two values based on a comparison of the two values. |
| 9468 | if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { |
| 9469 | if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { |
| 9470 | // Transform (X == Y) ? X : Y -> Y |
| Dale Johannesen | 2e1b769 | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 9471 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 9472 | // This is not safe in general for floating point: |
| 9473 | // consider X== -0, Y== +0. |
| 9474 | // It becomes safe if either operand is a nonzero constant. |
| 9475 | ConstantFP *CFPt, *CFPf; |
| 9476 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 9477 | !CFPt->getValueAPF().isZero()) || |
| 9478 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 9479 | !CFPf->getValueAPF().isZero())) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9480 | return ReplaceInstUsesWith(SI, FalseVal); |
| Dale Johannesen | 2e1b769 | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 9481 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9482 | // Transform (X != Y) ? X : Y -> X |
| 9483 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 9484 | return ReplaceInstUsesWith(SI, TrueVal); |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9485 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9486 | |
| 9487 | } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ |
| 9488 | // Transform (X == Y) ? Y : X -> X |
| Dale Johannesen | 2e1b769 | 2007-10-03 17:45:27 +0000 | [diff] [blame] | 9489 | if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { |
| 9490 | // This is not safe in general for floating point: |
| 9491 | // consider X== -0, Y== +0. |
| 9492 | // It becomes safe if either operand is a nonzero constant. |
| 9493 | ConstantFP *CFPt, *CFPf; |
| 9494 | if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && |
| 9495 | !CFPt->getValueAPF().isZero()) || |
| 9496 | ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && |
| 9497 | !CFPf->getValueAPF().isZero())) |
| 9498 | return ReplaceInstUsesWith(SI, FalseVal); |
| 9499 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9500 | // Transform (X != Y) ? Y : X -> Y |
| 9501 | if (FCI->getPredicate() == FCmpInst::FCMP_ONE) |
| 9502 | return ReplaceInstUsesWith(SI, TrueVal); |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9503 | // NOTE: if we wanted to, this is where to detect MIN/MAX |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9504 | } |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9505 | // NOTE: if we wanted to, this is where to detect ABS |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9506 | } |
| 9507 | |
| 9508 | // See if we are selecting two values based on a comparison of the two values. |
| Dan Gohman | 58c0963 | 2008-09-16 18:46:06 +0000 | [diff] [blame] | 9509 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) |
| 9510 | if (Instruction *Result = visitSelectInstWithICmp(SI, ICI)) |
| 9511 | return Result; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9512 | |
| 9513 | if (Instruction *TI = dyn_cast<Instruction>(TrueVal)) |
| 9514 | if (Instruction *FI = dyn_cast<Instruction>(FalseVal)) |
| 9515 | if (TI->hasOneUse() && FI->hasOneUse()) { |
| 9516 | Instruction *AddOp = 0, *SubOp = 0; |
| 9517 | |
| 9518 | // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) |
| 9519 | if (TI->getOpcode() == FI->getOpcode()) |
| 9520 | if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) |
| 9521 | return IV; |
| 9522 | |
| 9523 | // Turn select C, (X+Y), (X-Y) --> (X+(select C, Y, (-Y))). This is |
| 9524 | // even legal for FP. |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 9525 | if ((TI->getOpcode() == Instruction::Sub && |
| 9526 | FI->getOpcode() == Instruction::Add) || |
| 9527 | (TI->getOpcode() == Instruction::FSub && |
| 9528 | FI->getOpcode() == Instruction::FAdd)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9529 | AddOp = FI; SubOp = TI; |
| Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 9530 | } else if ((FI->getOpcode() == Instruction::Sub && |
| 9531 | TI->getOpcode() == Instruction::Add) || |
| 9532 | (FI->getOpcode() == Instruction::FSub && |
| 9533 | TI->getOpcode() == Instruction::FAdd)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9534 | AddOp = TI; SubOp = FI; |
| 9535 | } |
| 9536 | |
| 9537 | if (AddOp) { |
| 9538 | Value *OtherAddOp = 0; |
| 9539 | if (SubOp->getOperand(0) == AddOp->getOperand(0)) { |
| 9540 | OtherAddOp = AddOp->getOperand(1); |
| 9541 | } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { |
| 9542 | OtherAddOp = AddOp->getOperand(0); |
| 9543 | } |
| 9544 | |
| 9545 | if (OtherAddOp) { |
| 9546 | // So at this point we know we have (Y -> OtherAddOp): |
| 9547 | // select C, (add X, Y), (sub X, Z) |
| 9548 | Value *NegVal; // Compute -Z |
| 9549 | if (Constant *C = dyn_cast<Constant>(SubOp->getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9550 | NegVal = Context->getConstantExprNeg(C); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9551 | } else { |
| 9552 | NegVal = InsertNewInstBefore( |
| Owen Anderson | 15b3932 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 9553 | BinaryOperator::CreateNeg(*Context, SubOp->getOperand(1), |
| 9554 | "tmp"), SI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9555 | } |
| 9556 | |
| 9557 | Value *NewTrueOp = OtherAddOp; |
| 9558 | Value *NewFalseOp = NegVal; |
| 9559 | if (AddOp != TI) |
| 9560 | std::swap(NewTrueOp, NewFalseOp); |
| 9561 | Instruction *NewSel = |
| Gabor Greif | b91ea9d | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 9562 | SelectInst::Create(CondVal, NewTrueOp, |
| 9563 | NewFalseOp, SI.getName() + ".p"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9564 | |
| 9565 | NewSel = InsertNewInstBefore(NewSel, SI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 9566 | return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9567 | } |
| 9568 | } |
| 9569 | } |
| 9570 | |
| 9571 | // See if we can fold the select into one of our operands. |
| 9572 | if (SI.getType()->isInteger()) { |
| Evan Cheng | 9f8ee8f | 2009-03-31 20:42:45 +0000 | [diff] [blame] | 9573 | Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal); |
| 9574 | if (FoldI) |
| 9575 | return FoldI; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9576 | } |
| 9577 | |
| 9578 | if (BinaryOperator::isNot(CondVal)) { |
| 9579 | SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); |
| 9580 | SI.setOperand(1, FalseVal); |
| 9581 | SI.setOperand(2, TrueVal); |
| 9582 | return &SI; |
| 9583 | } |
| 9584 | |
| 9585 | return 0; |
| 9586 | } |
| 9587 | |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9588 | /// EnforceKnownAlignment - If the specified pointer points to an object that |
| 9589 | /// we control, modify the object's alignment to PrefAlign. This isn't |
| 9590 | /// often possible though. If alignment is important, a more reliable approach |
| 9591 | /// is to simply align all global variables and allocation instructions to |
| 9592 | /// their preferred alignment from the beginning. |
| 9593 | /// |
| 9594 | static unsigned EnforceKnownAlignment(Value *V, |
| 9595 | unsigned Align, unsigned PrefAlign) { |
| Chris Lattner | 47cf345 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9596 | |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9597 | User *U = dyn_cast<User>(V); |
| 9598 | if (!U) return Align; |
| 9599 | |
| Dan Gohman | 9545fb0 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 9600 | switch (Operator::getOpcode(U)) { |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9601 | default: break; |
| 9602 | case Instruction::BitCast: |
| 9603 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| 9604 | case Instruction::GetElementPtr: { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9605 | // If all indexes are zero, it is just the alignment of the base pointer. |
| 9606 | bool AllZeroOperands = true; |
| Gabor Greif | e92fbe2 | 2008-06-12 21:51:29 +0000 | [diff] [blame] | 9607 | for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 9608 | if (!isa<Constant>(*i) || |
| 9609 | !cast<Constant>(*i)->isNullValue()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9610 | AllZeroOperands = false; |
| 9611 | break; |
| 9612 | } |
| Chris Lattner | 47cf345 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9613 | |
| 9614 | if (AllZeroOperands) { |
| 9615 | // Treat this like a bitcast. |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9616 | return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); |
| Chris Lattner | 47cf345 | 2007-08-09 19:05:49 +0000 | [diff] [blame] | 9617 | } |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9618 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9619 | } |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9620 | } |
| 9621 | |
| 9622 | if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 9623 | // If there is a large requested alignment and we can, bump up the alignment |
| 9624 | // of the global. |
| 9625 | if (!GV->isDeclaration()) { |
| Dan Gohman | f6fe71e | 2009-02-16 23:02:21 +0000 | [diff] [blame] | 9626 | if (GV->getAlignment() >= PrefAlign) |
| 9627 | Align = GV->getAlignment(); |
| 9628 | else { |
| 9629 | GV->setAlignment(PrefAlign); |
| 9630 | Align = PrefAlign; |
| 9631 | } |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9632 | } |
| 9633 | } else if (AllocationInst *AI = dyn_cast<AllocationInst>(V)) { |
| 9634 | // If there is a requested alignment and if this is an alloca, round up. We |
| 9635 | // don't do this for malloc, because some systems can't respect the request. |
| 9636 | if (isa<AllocaInst>(AI)) { |
| Dan Gohman | f6fe71e | 2009-02-16 23:02:21 +0000 | [diff] [blame] | 9637 | if (AI->getAlignment() >= PrefAlign) |
| 9638 | Align = AI->getAlignment(); |
| 9639 | else { |
| 9640 | AI->setAlignment(PrefAlign); |
| 9641 | Align = PrefAlign; |
| 9642 | } |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9643 | } |
| 9644 | } |
| 9645 | |
| 9646 | return Align; |
| 9647 | } |
| 9648 | |
| 9649 | /// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that |
| 9650 | /// we can determine, return it, otherwise return 0. If PrefAlign is specified, |
| 9651 | /// and it is more than the alignment of the ultimate object, see if we can |
| 9652 | /// increase the alignment of the ultimate object, making this check succeed. |
| 9653 | unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, |
| 9654 | unsigned PrefAlign) { |
| 9655 | unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : |
| 9656 | sizeof(PrefAlign) * CHAR_BIT; |
| 9657 | APInt Mask = APInt::getAllOnesValue(BitWidth); |
| 9658 | APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); |
| 9659 | ComputeMaskedBits(V, Mask, KnownZero, KnownOne); |
| 9660 | unsigned TrailZ = KnownZero.countTrailingOnes(); |
| 9661 | unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); |
| 9662 | |
| 9663 | if (PrefAlign > Align) |
| 9664 | Align = EnforceKnownAlignment(V, Align, PrefAlign); |
| 9665 | |
| 9666 | // We don't need to make any adjustment. |
| 9667 | return Align; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9668 | } |
| 9669 | |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9670 | Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { |
| Dan Gohman | 2d648bb | 2008-04-10 18:43:06 +0000 | [diff] [blame] | 9671 | unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); |
| Dan Gohman | eb25491 | 2009-02-22 18:06:32 +0000 | [diff] [blame] | 9672 | unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9673 | unsigned MinAlign = std::min(DstAlign, SrcAlign); |
| Chris Lattner | 3947da7 | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9674 | unsigned CopyAlign = MI->getAlignment(); |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9675 | |
| 9676 | if (CopyAlign < MinAlign) { |
| Owen Anderson | f9f9936 | 2009-07-09 18:36:20 +0000 | [diff] [blame] | 9677 | MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(), |
| 9678 | MinAlign, false)); |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9679 | return MI; |
| 9680 | } |
| 9681 | |
| 9682 | // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with |
| 9683 | // load/store. |
| 9684 | ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); |
| 9685 | if (MemOpLength == 0) return 0; |
| 9686 | |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9687 | // Source and destination pointer types are always "i8*" for intrinsic. See |
| 9688 | // if the size is something we can handle with a single primitive load/store. |
| 9689 | // A single load+store correctly handles overlapping memory in the memmove |
| 9690 | // case. |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9691 | unsigned Size = MemOpLength->getZExtValue(); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9692 | if (Size == 0) return MI; // Delete this mem transfer. |
| 9693 | |
| 9694 | if (Size > 8 || (Size&(Size-1))) |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9695 | return 0; // If not 1/2/4/8 bytes, exit. |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9696 | |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9697 | // Use an integer load+store unless we can find something better. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9698 | Type *NewPtrTy = |
| 9699 | Context->getPointerTypeUnqual(Context->getIntegerType(Size<<3)); |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9700 | |
| 9701 | // Memcpy forces the use of i8* for the source and destination. That means |
| 9702 | // that if you're using memcpy to move one double around, you'll get a cast |
| 9703 | // from double* to i8*. We'd much rather use a double load+store rather than |
| 9704 | // an i64 load+store, here because this improves the odds that the source or |
| 9705 | // dest address will be promotable. See if we can find a better type than the |
| 9706 | // integer datatype. |
| 9707 | if (Value *Op = getBitCastOperand(MI->getOperand(1))) { |
| 9708 | const Type *SrcETy = cast<PointerType>(Op->getType())->getElementType(); |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 9709 | if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9710 | // The SrcETy might be something like {{{double}}} or [1 x double]. Rip |
| 9711 | // down through these levels if so. |
| Dan Gohman | b8e94f6 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 9712 | while (!SrcETy->isSingleValueType()) { |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9713 | if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { |
| 9714 | if (STy->getNumElements() == 1) |
| 9715 | SrcETy = STy->getElementType(0); |
| 9716 | else |
| 9717 | break; |
| 9718 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { |
| 9719 | if (ATy->getNumElements() == 1) |
| 9720 | SrcETy = ATy->getElementType(); |
| 9721 | else |
| 9722 | break; |
| 9723 | } else |
| 9724 | break; |
| 9725 | } |
| 9726 | |
| Dan Gohman | b8e94f6 | 2008-05-23 01:52:21 +0000 | [diff] [blame] | 9727 | if (SrcETy->isSingleValueType()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9728 | NewPtrTy = Context->getPointerTypeUnqual(SrcETy); |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9729 | } |
| 9730 | } |
| 9731 | |
| 9732 | |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9733 | // If the memcpy/memmove provides better alignment info than we can |
| 9734 | // infer, use it. |
| 9735 | SrcAlign = std::max(SrcAlign, CopyAlign); |
| 9736 | DstAlign = std::max(DstAlign, CopyAlign); |
| 9737 | |
| 9738 | Value *Src = InsertBitCastBefore(MI->getOperand(2), NewPtrTy, *MI); |
| 9739 | Value *Dest = InsertBitCastBefore(MI->getOperand(1), NewPtrTy, *MI); |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9740 | Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); |
| 9741 | InsertNewInstBefore(L, *MI); |
| 9742 | InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); |
| 9743 | |
| 9744 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9745 | MI->setOperand(3, Context->getNullValue(MemOpLength->getType())); |
| Chris Lattner | c669fb6 | 2008-01-14 00:28:35 +0000 | [diff] [blame] | 9746 | return MI; |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9747 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9748 | |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9749 | Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { |
| 9750 | unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); |
| Chris Lattner | 3947da7 | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9751 | if (MI->getAlignment() < Alignment) { |
| Owen Anderson | f9f9936 | 2009-07-09 18:36:20 +0000 | [diff] [blame] | 9752 | MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(), |
| 9753 | Alignment, false)); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9754 | return MI; |
| 9755 | } |
| 9756 | |
| 9757 | // Extract the length and alignment and fill if they are constant. |
| 9758 | ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); |
| 9759 | ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); |
| 9760 | if (!LenC || !FillC || FillC->getType() != Type::Int8Ty) |
| 9761 | return 0; |
| 9762 | uint64_t Len = LenC->getZExtValue(); |
| Chris Lattner | 3947da7 | 2009-03-08 03:59:00 +0000 | [diff] [blame] | 9763 | Alignment = MI->getAlignment(); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9764 | |
| 9765 | // If the length is zero, this is a no-op |
| 9766 | if (Len == 0) return MI; // memset(d,c,0,a) -> noop |
| 9767 | |
| 9768 | // memset(s,c,n) -> store s, c (for n=1,2,4,8) |
| 9769 | if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9770 | const Type *ITy = Context->getIntegerType(Len*8); // n=1 -> i8. |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9771 | |
| 9772 | Value *Dest = MI->getDest(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9773 | Dest = InsertBitCastBefore(Dest, Context->getPointerTypeUnqual(ITy), *MI); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9774 | |
| 9775 | // Alignment 0 is identity for alignment 1 for memset, but not store. |
| 9776 | if (Alignment == 0) Alignment = 1; |
| 9777 | |
| 9778 | // Extract the fill value and store. |
| 9779 | uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9780 | InsertNewInstBefore(new StoreInst(Context->getConstantInt(ITy, Fill), |
| 9781 | Dest, false, Alignment), *MI); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9782 | |
| 9783 | // Set the size of the copy to 0, it will be deleted on the next iteration. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9784 | MI->setLength(Context->getNullValue(LenC->getType())); |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9785 | return MI; |
| 9786 | } |
| 9787 | |
| 9788 | return 0; |
| 9789 | } |
| 9790 | |
| 9791 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9792 | /// visitCallInst - CallInst simplification. This mostly only handles folding |
| 9793 | /// of intrinsic instructions. For normal calls, it allows visitCallSite to do |
| 9794 | /// the heavy lifting. |
| 9795 | /// |
| 9796 | Instruction *InstCombiner::visitCallInst(CallInst &CI) { |
| Chris Lattner | aa295aa | 2009-05-13 17:39:14 +0000 | [diff] [blame] | 9797 | // If the caller function is nounwind, mark the call as nounwind, even if the |
| 9798 | // callee isn't. |
| 9799 | if (CI.getParent()->getParent()->doesNotThrow() && |
| 9800 | !CI.doesNotThrow()) { |
| 9801 | CI.setDoesNotThrow(); |
| 9802 | return &CI; |
| 9803 | } |
| 9804 | |
| 9805 | |
| 9806 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9807 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); |
| 9808 | if (!II) return visitCallSite(&CI); |
| 9809 | |
| 9810 | // Intrinsics cannot occur in an invoke, so handle them here instead of in |
| 9811 | // visitCallSite. |
| 9812 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { |
| 9813 | bool Changed = false; |
| 9814 | |
| 9815 | // memmove/cpy/set of zero bytes is a noop. |
| 9816 | if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { |
| 9817 | if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); |
| 9818 | |
| 9819 | if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) |
| 9820 | if (CI->getZExtValue() == 1) { |
| 9821 | // Replace the instruction with just byte operations. We would |
| 9822 | // transform other cases to loads/stores, but we don't know if |
| 9823 | // alignment is sufficient. |
| 9824 | } |
| 9825 | } |
| 9826 | |
| 9827 | // If we have a memmove and the source operation is a constant global, |
| 9828 | // then the source and dest pointers can't alias, so we can change this |
| 9829 | // into a call to memcpy. |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9830 | if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9831 | if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) |
| 9832 | if (GVSrc->isConstant()) { |
| 9833 | Module *M = CI.getParent()->getParent()->getParent(); |
| Chris Lattner | 82c2e43 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 9834 | Intrinsic::ID MemCpyID = Intrinsic::memcpy; |
| 9835 | const Type *Tys[1]; |
| 9836 | Tys[0] = CI.getOperand(3)->getType(); |
| 9837 | CI.setOperand(0, |
| 9838 | Intrinsic::getDeclaration(M, MemCpyID, Tys, 1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9839 | Changed = true; |
| 9840 | } |
| Chris Lattner | 59b27d9 | 2008-05-28 05:30:41 +0000 | [diff] [blame] | 9841 | |
| 9842 | // memmove(x,x,size) -> noop. |
| 9843 | if (MMI->getSource() == MMI->getDest()) |
| 9844 | return EraseInstFromFunction(CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
| 9847 | // If we can determine a pointer alignment that is bigger than currently |
| 9848 | // set, update the alignment. |
| Chris Lattner | a86628a | 2009-03-08 03:37:16 +0000 | [diff] [blame] | 9849 | if (isa<MemTransferInst>(MI)) { |
| Chris Lattner | 00ae513 | 2008-01-13 23:50:23 +0000 | [diff] [blame] | 9850 | if (Instruction *I = SimplifyMemTransfer(MI)) |
| 9851 | return I; |
| Chris Lattner | 5af8a91 | 2008-04-30 06:39:11 +0000 | [diff] [blame] | 9852 | } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { |
| 9853 | if (Instruction *I = SimplifyMemSet(MSI)) |
| 9854 | return I; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9855 | } |
| 9856 | |
| 9857 | if (Changed) return II; |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9858 | } |
| 9859 | |
| 9860 | switch (II->getIntrinsicID()) { |
| 9861 | default: break; |
| 9862 | case Intrinsic::bswap: |
| 9863 | // bswap(bswap(x)) -> x |
| 9864 | if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) |
| 9865 | if (Operand->getIntrinsicID() == Intrinsic::bswap) |
| 9866 | return ReplaceInstUsesWith(CI, Operand->getOperand(1)); |
| 9867 | break; |
| 9868 | case Intrinsic::ppc_altivec_lvx: |
| 9869 | case Intrinsic::ppc_altivec_lvxl: |
| 9870 | case Intrinsic::x86_sse_loadu_ps: |
| 9871 | case Intrinsic::x86_sse2_loadu_pd: |
| 9872 | case Intrinsic::x86_sse2_loadu_dq: |
| 9873 | // Turn PPC lvx -> load if the pointer is known aligned. |
| 9874 | // Turn X86 loadups -> load if the pointer is known aligned. |
| 9875 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 9876 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9877 | Context->getPointerTypeUnqual(II->getType()), |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9878 | CI); |
| 9879 | return new LoadInst(Ptr); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9880 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9881 | break; |
| 9882 | case Intrinsic::ppc_altivec_stvx: |
| 9883 | case Intrinsic::ppc_altivec_stvxl: |
| 9884 | // Turn stvx -> store if the pointer is known aligned. |
| 9885 | if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { |
| 9886 | const Type *OpPtrTy = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9887 | Context->getPointerTypeUnqual(II->getOperand(1)->getType()); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9888 | Value *Ptr = InsertBitCastBefore(II->getOperand(2), OpPtrTy, CI); |
| 9889 | return new StoreInst(II->getOperand(1), Ptr); |
| 9890 | } |
| 9891 | break; |
| 9892 | case Intrinsic::x86_sse_storeu_ps: |
| 9893 | case Intrinsic::x86_sse2_storeu_pd: |
| 9894 | case Intrinsic::x86_sse2_storeu_dq: |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9895 | // Turn X86 storeu -> store if the pointer is known aligned. |
| 9896 | if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { |
| 9897 | const Type *OpPtrTy = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9898 | Context->getPointerTypeUnqual(II->getOperand(2)->getType()); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9899 | Value *Ptr = InsertBitCastBefore(II->getOperand(1), OpPtrTy, CI); |
| 9900 | return new StoreInst(II->getOperand(2), Ptr); |
| 9901 | } |
| 9902 | break; |
| 9903 | |
| 9904 | case Intrinsic::x86_sse_cvttss2si: { |
| 9905 | // These intrinsics only demands the 0th element of its input vector. If |
| 9906 | // we can simplify the input based on that, do so now. |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 9907 | unsigned VWidth = |
| 9908 | cast<VectorType>(II->getOperand(1)->getType())->getNumElements(); |
| 9909 | APInt DemandedElts(VWidth, 1); |
| 9910 | APInt UndefElts(VWidth, 0); |
| 9911 | if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9912 | UndefElts)) { |
| 9913 | II->setOperand(1, V); |
| 9914 | return II; |
| 9915 | } |
| 9916 | break; |
| 9917 | } |
| 9918 | |
| 9919 | case Intrinsic::ppc_altivec_vperm: |
| 9920 | // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. |
| 9921 | if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { |
| 9922 | assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9923 | |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9924 | // Check that all of the elements are integer constants or undefs. |
| 9925 | bool AllEltsOk = true; |
| 9926 | for (unsigned i = 0; i != 16; ++i) { |
| 9927 | if (!isa<ConstantInt>(Mask->getOperand(i)) && |
| 9928 | !isa<UndefValue>(Mask->getOperand(i))) { |
| 9929 | AllEltsOk = false; |
| 9930 | break; |
| 9931 | } |
| 9932 | } |
| 9933 | |
| 9934 | if (AllEltsOk) { |
| 9935 | // Cast the input vectors to byte vectors. |
| 9936 | Value *Op0 =InsertBitCastBefore(II->getOperand(1),Mask->getType(),CI); |
| 9937 | Value *Op1 =InsertBitCastBefore(II->getOperand(2),Mask->getType(),CI); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 9938 | Value *Result = Context->getUndef(Op0->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9939 | |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9940 | // Only extract each element once. |
| 9941 | Value *ExtractedElts[32]; |
| 9942 | memset(ExtractedElts, 0, sizeof(ExtractedElts)); |
| 9943 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9944 | for (unsigned i = 0; i != 16; ++i) { |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9945 | if (isa<UndefValue>(Mask->getOperand(i))) |
| 9946 | continue; |
| 9947 | unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); |
| 9948 | Idx &= 31; // Match the hardware behavior. |
| 9949 | |
| 9950 | if (ExtractedElts[Idx] == 0) { |
| 9951 | Instruction *Elt = |
| Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 9952 | new ExtractElementInst(Idx < 16 ? Op0 : Op1, |
| 9953 | Context->getConstantInt(Type::Int32Ty, Idx&15, false), "tmp"); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9954 | InsertNewInstBefore(Elt, CI); |
| 9955 | ExtractedElts[Idx] = Elt; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9956 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9957 | |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9958 | // Insert this value into the result vector. |
| 9959 | Result = InsertElementInst::Create(Result, ExtractedElts[Idx], |
| Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 9960 | Context->getConstantInt(Type::Int32Ty, i, false), |
| 9961 | "tmp"); |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9962 | InsertNewInstBefore(cast<Instruction>(Result), CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9963 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9964 | return CastInst::Create(Instruction::BitCast, Result, CI.getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9965 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9966 | } |
| 9967 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9968 | |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9969 | case Intrinsic::stackrestore: { |
| 9970 | // If the save is right next to the restore, remove the restore. This can |
| 9971 | // happen when variable allocas are DCE'd. |
| 9972 | if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { |
| 9973 | if (SS->getIntrinsicID() == Intrinsic::stacksave) { |
| 9974 | BasicBlock::iterator BI = SS; |
| 9975 | if (&*++BI == II) |
| 9976 | return EraseInstFromFunction(CI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 9977 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 9978 | } |
| 9979 | |
| 9980 | // Scan down this block to see if there is another stack restore in the |
| 9981 | // same block without an intervening call/alloca. |
| 9982 | BasicBlock::iterator BI = II; |
| 9983 | TerminatorInst *TI = II->getParent()->getTerminator(); |
| 9984 | bool CannotRemove = false; |
| 9985 | for (++BI; &*BI != TI; ++BI) { |
| 9986 | if (isa<AllocaInst>(BI)) { |
| 9987 | CannotRemove = true; |
| 9988 | break; |
| 9989 | } |
| Chris Lattner | a6b477c | 2008-06-25 05:59:28 +0000 | [diff] [blame] | 9990 | if (CallInst *BCI = dyn_cast<CallInst>(BI)) { |
| 9991 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { |
| 9992 | // If there is a stackrestore below this one, remove this one. |
| 9993 | if (II->getIntrinsicID() == Intrinsic::stackrestore) |
| 9994 | return EraseInstFromFunction(CI); |
| 9995 | // Otherwise, ignore the intrinsic. |
| 9996 | } else { |
| 9997 | // If we found a non-intrinsic call, we can't remove the stack |
| 9998 | // restore. |
| Chris Lattner | 416d91c | 2008-02-18 06:12:38 +0000 | [diff] [blame] | 9999 | CannotRemove = true; |
| 10000 | break; |
| 10001 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 10002 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10003 | } |
| Chris Lattner | 989ba31 | 2008-06-18 04:33:20 +0000 | [diff] [blame] | 10004 | |
| 10005 | // If the stack restore is in a return/unwind block and if there are no |
| 10006 | // allocas or calls between the restore and the return, nuke the restore. |
| 10007 | if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) |
| 10008 | return EraseInstFromFunction(CI); |
| 10009 | break; |
| 10010 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10011 | } |
| 10012 | |
| 10013 | return visitCallSite(II); |
| 10014 | } |
| 10015 | |
| 10016 | // InvokeInst simplification |
| 10017 | // |
| 10018 | Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { |
| 10019 | return visitCallSite(&II); |
| 10020 | } |
| 10021 | |
| Dale Johannesen | 9602183 | 2008-04-25 21:16:07 +0000 | [diff] [blame] | 10022 | /// isSafeToEliminateVarargsCast - If this cast does not affect the value |
| 10023 | /// passed through the varargs area, we can eliminate the use of the cast. |
| Dale Johannesen | 3561546 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 10024 | static bool isSafeToEliminateVarargsCast(const CallSite CS, |
| 10025 | const CastInst * const CI, |
| 10026 | const TargetData * const TD, |
| 10027 | const int ix) { |
| 10028 | if (!CI->isLosslessCast()) |
| 10029 | return false; |
| 10030 | |
| 10031 | // The size of ByVal arguments is derived from the type, so we |
| 10032 | // can't change to a type with a different size. If the size were |
| 10033 | // passed explicitly we could avoid this check. |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10034 | if (!CS.paramHasAttr(ix, Attribute::ByVal)) |
| Dale Johannesen | 3561546 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 10035 | return true; |
| 10036 | |
| 10037 | const Type* SrcTy = |
| 10038 | cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); |
| 10039 | const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); |
| 10040 | if (!SrcTy->isSized() || !DstTy->isSized()) |
| 10041 | return false; |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 10042 | if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) |
| Dale Johannesen | 3561546 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 10043 | return false; |
| 10044 | return true; |
| 10045 | } |
| 10046 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10047 | // visitCallSite - Improvements for call and invoke instructions. |
| 10048 | // |
| 10049 | Instruction *InstCombiner::visitCallSite(CallSite CS) { |
| 10050 | bool Changed = false; |
| 10051 | |
| 10052 | // If the callee is a constexpr cast of a function, attempt to move the cast |
| 10053 | // to the arguments of the call/invoke. |
| 10054 | if (transformConstExprCastCall(CS)) return 0; |
| 10055 | |
| 10056 | Value *Callee = CS.getCalledValue(); |
| 10057 | |
| 10058 | if (Function *CalleeF = dyn_cast<Function>(Callee)) |
| 10059 | if (CalleeF->getCallingConv() != CS.getCallingConv()) { |
| 10060 | Instruction *OldCall = CS.getInstruction(); |
| 10061 | // If the call and callee calling conventions don't match, this call must |
| 10062 | // be unreachable, as the call is undefined. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 10063 | new StoreInst(Context->getTrue(), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10064 | Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), |
| 10065 | OldCall); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10066 | if (!OldCall->use_empty()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10067 | OldCall->replaceAllUsesWith(Context->getUndef(OldCall->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10068 | if (isa<CallInst>(OldCall)) // Not worth removing an invoke here. |
| 10069 | return EraseInstFromFunction(*OldCall); |
| 10070 | return 0; |
| 10071 | } |
| 10072 | |
| 10073 | if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { |
| 10074 | // This instruction is not reachable, just remove it. We insert a store to |
| 10075 | // undef so that we know that this code is not reachable, despite the fact |
| 10076 | // that we can't modify the CFG here. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 10077 | new StoreInst(Context->getTrue(), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10078 | Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10079 | CS.getInstruction()); |
| 10080 | |
| 10081 | if (!CS.getInstruction()->use_empty()) |
| 10082 | CS.getInstruction()-> |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10083 | replaceAllUsesWith(Context->getUndef(CS.getInstruction()->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10084 | |
| 10085 | if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { |
| 10086 | // Don't break the CFG, insert a dummy cond branch. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10087 | BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 10088 | Context->getTrue(), II); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10089 | } |
| 10090 | return EraseInstFromFunction(*CS.getInstruction()); |
| 10091 | } |
| 10092 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10093 | if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) |
| 10094 | if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) |
| 10095 | if (In->getIntrinsicID() == Intrinsic::init_trampoline) |
| 10096 | return transformCallThroughTrampoline(CS); |
| 10097 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10098 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 10099 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| 10100 | if (FTy->isVarArg()) { |
| Dale Johannesen | 502336c | 2008-04-23 01:03:05 +0000 | [diff] [blame] | 10101 | int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10102 | // See if we can optimize any arguments passed through the varargs area of |
| 10103 | // the call. |
| 10104 | for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), |
| Dale Johannesen | 3561546 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 10105 | E = CS.arg_end(); I != E; ++I, ++ix) { |
| 10106 | CastInst *CI = dyn_cast<CastInst>(*I); |
| 10107 | if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { |
| 10108 | *I = CI->getOperand(0); |
| 10109 | Changed = true; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10110 | } |
| Dale Johannesen | 3561546 | 2008-04-23 18:34:37 +0000 | [diff] [blame] | 10111 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10112 | } |
| 10113 | |
| Duncan Sands | 2937e35 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 10114 | if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { |
| Duncan Sands | 7868f3c | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 10115 | // Inline asm calls cannot throw - mark them 'nounwind'. |
| Duncan Sands | 2937e35 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 10116 | CS.setDoesNotThrow(); |
| Duncan Sands | 7868f3c | 2007-12-16 15:51:49 +0000 | [diff] [blame] | 10117 | Changed = true; |
| 10118 | } |
| 10119 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10120 | return Changed ? CS.getInstruction() : 0; |
| 10121 | } |
| 10122 | |
| 10123 | // transformConstExprCastCall - If the callee is a constexpr cast of a function, |
| 10124 | // attempt to move the cast to the arguments of the call/invoke. |
| 10125 | // |
| 10126 | bool InstCombiner::transformConstExprCastCall(CallSite CS) { |
| 10127 | if (!isa<ConstantExpr>(CS.getCalledValue())) return false; |
| 10128 | ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); |
| 10129 | if (CE->getOpcode() != Instruction::BitCast || |
| 10130 | !isa<Function>(CE->getOperand(0))) |
| 10131 | return false; |
| 10132 | Function *Callee = cast<Function>(CE->getOperand(0)); |
| 10133 | Instruction *Caller = CS.getInstruction(); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10134 | const AttrListPtr &CallerPAL = CS.getAttributes(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10135 | |
| 10136 | // Okay, this is a cast from a function to a different type. Unless doing so |
| 10137 | // would cause a type conversion of one of our arguments, change this call to |
| 10138 | // be a direct call with arguments casted to the appropriate types. |
| 10139 | // |
| 10140 | const FunctionType *FT = Callee->getFunctionType(); |
| 10141 | const Type *OldRetTy = Caller->getType(); |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10142 | const Type *NewRetTy = FT->getReturnType(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10143 | |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10144 | if (isa<StructType>(NewRetTy)) |
| Devang Patel | d091d32 | 2008-03-11 18:04:06 +0000 | [diff] [blame] | 10145 | return false; // TODO: Handle multiple return values. |
| 10146 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10147 | // Check to see if we are changing the return type... |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10148 | if (OldRetTy != NewRetTy) { |
| Bill Wendling | d9644a4 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 10149 | if (Callee->isDeclaration() && |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10150 | // Conversion is ok if changing from one pointer type to another or from |
| 10151 | // a pointer to an integer of the same size. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 10152 | !((isa<PointerType>(OldRetTy) || !TD || |
| 10153 | OldRetTy == TD->getIntPtrType()) && |
| 10154 | (isa<PointerType>(NewRetTy) || !TD || |
| 10155 | NewRetTy == TD->getIntPtrType()))) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10156 | return false; // Cannot transform this return value. |
| 10157 | |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10158 | if (!Caller->use_empty() && |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10159 | // void -> non-void is handled specially |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10160 | NewRetTy != Type::VoidTy && !CastInst::isCastable(NewRetTy, OldRetTy)) |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10161 | return false; // Cannot transform this return value. |
| 10162 | |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10163 | if (!CallerPAL.isEmpty() && !Caller->use_empty()) { |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10164 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10165 | if (RAttrs & Attribute::typeIncompatible(NewRetTy)) |
| Duncan Sands | dbe97dc | 2008-01-07 17:16:06 +0000 | [diff] [blame] | 10166 | return false; // Attribute not compatible with transformed value. |
| 10167 | } |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10168 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10169 | // If the callsite is an invoke instruction, and the return value is used by |
| 10170 | // a PHI node in a successor, we cannot change the return type of the call |
| 10171 | // because there is no place to put the cast instruction (without breaking |
| 10172 | // the critical edge). Bail out in this case. |
| 10173 | if (!Caller->use_empty()) |
| 10174 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) |
| 10175 | for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); |
| 10176 | UI != E; ++UI) |
| 10177 | if (PHINode *PN = dyn_cast<PHINode>(*UI)) |
| 10178 | if (PN->getParent() == II->getNormalDest() || |
| 10179 | PN->getParent() == II->getUnwindDest()) |
| 10180 | return false; |
| 10181 | } |
| 10182 | |
| 10183 | unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); |
| 10184 | unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); |
| 10185 | |
| 10186 | CallSite::arg_iterator AI = CS.arg_begin(); |
| 10187 | for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { |
| 10188 | const Type *ParamTy = FT->getParamType(i); |
| 10189 | const Type *ActTy = (*AI)->getType(); |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10190 | |
| 10191 | if (!CastInst::isCastable(ActTy, ParamTy)) |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10192 | return false; // Cannot transform this parameter value. |
| 10193 | |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10194 | if (CallerPAL.getParamAttributes(i + 1) |
| 10195 | & Attribute::typeIncompatible(ParamTy)) |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10196 | return false; // Attribute not compatible with transformed value. |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10197 | |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10198 | // Converting from one pointer type to another or between a pointer and an |
| 10199 | // integer of the same size is safe even if we do not have a body. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10200 | bool isConvertible = ActTy == ParamTy || |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 10201 | (TD && ((isa<PointerType>(ParamTy) || ParamTy == TD->getIntPtrType()) && |
| 10202 | (isa<PointerType>(ActTy) || ActTy == TD->getIntPtrType()))); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10203 | if (Callee->isDeclaration() && !isConvertible) return false; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10204 | } |
| 10205 | |
| 10206 | if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && |
| 10207 | Callee->isDeclaration()) |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10208 | return false; // Do not delete arguments unless we have a function body. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10209 | |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10210 | if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && |
| 10211 | !CallerPAL.isEmpty()) |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10212 | // In this case we have more arguments than the new function type, but we |
| Duncan Sands | 4ced1f8 | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 10213 | // won't be dropping them. Check that these extra arguments have attributes |
| 10214 | // that are compatible with being a vararg call argument. |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10215 | for (unsigned i = CallerPAL.getNumSlots(); i; --i) { |
| 10216 | if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) |
| Duncan Sands | 4ced1f8 | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 10217 | break; |
| Devang Patel | e480dfa | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 10218 | Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10219 | if (PAttrs & Attribute::VarArgsIncompatible) |
| Duncan Sands | 4ced1f8 | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 10220 | return false; |
| 10221 | } |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10222 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10223 | // Okay, we decided that this is a safe thing to do: go ahead and start |
| 10224 | // inserting cast instructions as necessary... |
| 10225 | std::vector<Value*> Args; |
| 10226 | Args.reserve(NumActualArgs); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10227 | SmallVector<AttributeWithIndex, 8> attrVec; |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10228 | attrVec.reserve(NumCommonArgs); |
| 10229 | |
| 10230 | // Get any return attributes. |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10231 | Attributes RAttrs = CallerPAL.getRetAttributes(); |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10232 | |
| 10233 | // If the return value is not being used, the type may not be compatible |
| 10234 | // with the existing attributes. Wipe out any problematic attributes. |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10235 | RAttrs &= ~Attribute::typeIncompatible(NewRetTy); |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10236 | |
| 10237 | // Add the new return attributes. |
| 10238 | if (RAttrs) |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10239 | attrVec.push_back(AttributeWithIndex::get(0, RAttrs)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10240 | |
| 10241 | AI = CS.arg_begin(); |
| 10242 | for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { |
| 10243 | const Type *ParamTy = FT->getParamType(i); |
| 10244 | if ((*AI)->getType() == ParamTy) { |
| 10245 | Args.push_back(*AI); |
| 10246 | } else { |
| 10247 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, |
| 10248 | false, ParamTy, false); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10249 | CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10250 | Args.push_back(InsertNewInstBefore(NewCast, *Caller)); |
| 10251 | } |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10252 | |
| 10253 | // Add any parameter attributes. |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10254 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10255 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10256 | } |
| 10257 | |
| 10258 | // If the function takes more arguments than the call was taking, add them |
| 10259 | // now... |
| 10260 | for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10261 | Args.push_back(Context->getNullValue(FT->getParamType(i))); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10262 | |
| 10263 | // If we are removing arguments to the function, emit an obnoxious warning... |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10264 | if (FT->getNumParams() < NumActualArgs) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10265 | if (!FT->isVarArg()) { |
| 10266 | cerr << "WARNING: While resolving call to function '" |
| 10267 | << Callee->getName() << "' arguments were dropped!\n"; |
| 10268 | } else { |
| 10269 | // Add all of the arguments in their promoted form to the arg list... |
| 10270 | for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { |
| 10271 | const Type *PTy = getPromotedType((*AI)->getType()); |
| 10272 | if (PTy != (*AI)->getType()) { |
| 10273 | // Must promote to pass through va_arg area! |
| 10274 | Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false, |
| 10275 | PTy, false); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10276 | Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10277 | InsertNewInstBefore(Cast, *Caller); |
| 10278 | Args.push_back(Cast); |
| 10279 | } else { |
| 10280 | Args.push_back(*AI); |
| 10281 | } |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10282 | |
| Duncan Sands | 4ced1f8 | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 10283 | // Add any parameter attributes. |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10284 | if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10285 | attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); |
| Duncan Sands | 4ced1f8 | 2008-01-13 08:02:44 +0000 | [diff] [blame] | 10286 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10287 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 10288 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10289 | |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10290 | if (Attributes FnAttrs = CallerPAL.getFnAttributes()) |
| 10291 | attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); |
| 10292 | |
| Duncan Sands | 7901ce1 | 2008-06-01 07:38:42 +0000 | [diff] [blame] | 10293 | if (NewRetTy == Type::VoidTy) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10294 | Caller->setName(""); // Void type should not have a name. |
| 10295 | |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10296 | const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),attrVec.end()); |
| Duncan Sands | c849e66 | 2008-01-06 18:27:01 +0000 | [diff] [blame] | 10297 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10298 | Instruction *NC; |
| 10299 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10300 | NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), |
| Gabor Greif | b91ea9d | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10301 | Args.begin(), Args.end(), |
| 10302 | Caller->getName(), Caller); |
| Reid Spencer | 6b0b09a | 2007-07-30 19:53:57 +0000 | [diff] [blame] | 10303 | cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10304 | cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10305 | } else { |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10306 | NC = CallInst::Create(Callee, Args.begin(), Args.end(), |
| 10307 | Caller->getName(), Caller); |
| Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 10308 | CallInst *CI = cast<CallInst>(Caller); |
| 10309 | if (CI->isTailCall()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10310 | cast<CallInst>(NC)->setTailCall(); |
| Duncan Sands | f5588dc | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 10311 | cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10312 | cast<CallInst>(NC)->setAttributes(NewCallerPAL); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10313 | } |
| 10314 | |
| 10315 | // Insert a cast of the return type as necessary. |
| 10316 | Value *NV = NC; |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10317 | if (OldRetTy != NV->getType() && !Caller->use_empty()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10318 | if (NV->getType() != Type::VoidTy) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10319 | Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, |
| Duncan Sands | 5c48958 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 10320 | OldRetTy, false); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10321 | NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10322 | |
| 10323 | // If this is an invoke instruction, we should insert it after the first |
| 10324 | // non-phi, instruction in the normal successor block. |
| 10325 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| Dan Gohman | 514277c | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 10326 | BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10327 | InsertNewInstBefore(NC, *I); |
| 10328 | } else { |
| 10329 | // Otherwise, it's a call, just insert cast right after the call instr |
| 10330 | InsertNewInstBefore(NC, *Caller); |
| 10331 | } |
| 10332 | AddUsersToWorkList(*Caller); |
| 10333 | } else { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10334 | NV = Context->getUndef(Caller->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10335 | } |
| 10336 | } |
| 10337 | |
| 10338 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 10339 | Caller->replaceAllUsesWith(NV); |
| 10340 | Caller->eraseFromParent(); |
| 10341 | RemoveFromWorkList(Caller); |
| 10342 | return true; |
| 10343 | } |
| 10344 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10345 | // transformCallThroughTrampoline - Turn a call to a function created by the |
| 10346 | // init_trampoline intrinsic into a direct call to the underlying function. |
| 10347 | // |
| 10348 | Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { |
| 10349 | Value *Callee = CS.getCalledValue(); |
| 10350 | const PointerType *PTy = cast<PointerType>(Callee->getType()); |
| 10351 | const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10352 | const AttrListPtr &Attrs = CS.getAttributes(); |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10353 | |
| 10354 | // If the call already has the 'nest' attribute somewhere then give up - |
| 10355 | // otherwise 'nest' would occur twice after splicing in the chain. |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10356 | if (Attrs.hasAttrSomewhere(Attribute::Nest)) |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10357 | return 0; |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10358 | |
| 10359 | IntrinsicInst *Tramp = |
| 10360 | cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); |
| 10361 | |
| Anton Korobeynikov | 48fc88f | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 10362 | Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10363 | const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); |
| 10364 | const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); |
| 10365 | |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10366 | const AttrListPtr &NestAttrs = NestF->getAttributes(); |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10367 | if (!NestAttrs.isEmpty()) { |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10368 | unsigned NestIdx = 1; |
| 10369 | const Type *NestTy = 0; |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10370 | Attributes NestAttr = Attribute::None; |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10371 | |
| 10372 | // Look for a parameter marked with the 'nest' attribute. |
| 10373 | for (FunctionType::param_iterator I = NestFTy->param_begin(), |
| 10374 | E = NestFTy->param_end(); I != E; ++NestIdx, ++I) |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10375 | if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) { |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10376 | // Record the parameter type and any other attributes. |
| 10377 | NestTy = *I; |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10378 | NestAttr = NestAttrs.getParamAttributes(NestIdx); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10379 | break; |
| 10380 | } |
| 10381 | |
| 10382 | if (NestTy) { |
| 10383 | Instruction *Caller = CS.getInstruction(); |
| 10384 | std::vector<Value*> NewArgs; |
| 10385 | NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); |
| 10386 | |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10387 | SmallVector<AttributeWithIndex, 8> NewAttrs; |
| Chris Lattner | 1c8733e | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 10388 | NewAttrs.reserve(Attrs.getNumSlots() + 1); |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10389 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10390 | // Insert the nest argument into the call argument list, which may |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10391 | // mean appending it. Likewise for attributes. |
| 10392 | |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10393 | // Add any result attributes. |
| 10394 | if (Attributes Attr = Attrs.getRetAttributes()) |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10395 | NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10396 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10397 | { |
| 10398 | unsigned Idx = 1; |
| 10399 | CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 10400 | do { |
| 10401 | if (Idx == NestIdx) { |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10402 | // Add the chain argument and attributes. |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10403 | Value *NestVal = Tramp->getOperand(3); |
| 10404 | if (NestVal->getType() != NestTy) |
| 10405 | NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); |
| 10406 | NewArgs.push_back(NestVal); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10407 | NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10408 | } |
| 10409 | |
| 10410 | if (I == E) |
| 10411 | break; |
| 10412 | |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10413 | // Add the original argument and attributes. |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10414 | NewArgs.push_back(*I); |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10415 | if (Attributes Attr = Attrs.getParamAttributes(Idx)) |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10416 | NewAttrs.push_back |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10417 | (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10418 | |
| 10419 | ++Idx, ++I; |
| 10420 | } while (1); |
| 10421 | } |
| 10422 | |
| Devang Patel | f2a4a92 | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 10423 | // Add any function attributes. |
| 10424 | if (Attributes Attr = Attrs.getFnAttributes()) |
| 10425 | NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); |
| 10426 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10427 | // The trampoline may have been bitcast to a bogus type (FTy). |
| 10428 | // Handle this by synthesizing a new function type, equal to FTy |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10429 | // with the chain parameter inserted. |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10430 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10431 | std::vector<const Type*> NewTypes; |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10432 | NewTypes.reserve(FTy->getNumParams()+1); |
| 10433 | |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10434 | // Insert the chain's type into the list of parameter types, which may |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10435 | // mean appending it. |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10436 | { |
| 10437 | unsigned Idx = 1; |
| 10438 | FunctionType::param_iterator I = FTy->param_begin(), |
| 10439 | E = FTy->param_end(); |
| 10440 | |
| 10441 | do { |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10442 | if (Idx == NestIdx) |
| 10443 | // Add the chain's type. |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10444 | NewTypes.push_back(NestTy); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10445 | |
| 10446 | if (I == E) |
| 10447 | break; |
| 10448 | |
| Duncan Sands | 48b8111 | 2008-01-14 19:52:09 +0000 | [diff] [blame] | 10449 | // Add the original type. |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10450 | NewTypes.push_back(*I); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10451 | |
| 10452 | ++Idx, ++I; |
| 10453 | } while (1); |
| 10454 | } |
| 10455 | |
| 10456 | // Replace the trampoline call with a direct call. Let the generic |
| 10457 | // code sort out any function type mismatches. |
| 10458 | FunctionType *NewFTy = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10459 | Context->getFunctionType(FTy->getReturnType(), NewTypes, |
| 10460 | FTy->isVarArg()); |
| 10461 | Constant *NewCallee = |
| 10462 | NestF->getType() == Context->getPointerTypeUnqual(NewFTy) ? |
| 10463 | NestF : Context->getConstantExprBitCast(NestF, |
| 10464 | Context->getPointerTypeUnqual(NewFTy)); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10465 | const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),NewAttrs.end()); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10466 | |
| 10467 | Instruction *NewCaller; |
| 10468 | if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10469 | NewCaller = InvokeInst::Create(NewCallee, |
| 10470 | II->getNormalDest(), II->getUnwindDest(), |
| 10471 | NewArgs.begin(), NewArgs.end(), |
| 10472 | Caller->getName(), Caller); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10473 | cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10474 | cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10475 | } else { |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10476 | NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), |
| 10477 | Caller->getName(), Caller); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10478 | if (cast<CallInst>(Caller)->isTailCall()) |
| 10479 | cast<CallInst>(NewCaller)->setTailCall(); |
| 10480 | cast<CallInst>(NewCaller)-> |
| 10481 | setCallingConv(cast<CallInst>(Caller)->getCallingConv()); |
| Devang Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 10482 | cast<CallInst>(NewCaller)->setAttributes(NewPAL); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10483 | } |
| 10484 | if (Caller->getType() != Type::VoidTy && !Caller->use_empty()) |
| 10485 | Caller->replaceAllUsesWith(NewCaller); |
| 10486 | Caller->eraseFromParent(); |
| 10487 | RemoveFromWorkList(Caller); |
| 10488 | return 0; |
| 10489 | } |
| 10490 | } |
| 10491 | |
| 10492 | // Replace the trampoline call with a direct call. Since there is no 'nest' |
| 10493 | // parameter, there is no need to adjust the argument list. Let the generic |
| 10494 | // code sort out any function type mismatches. |
| 10495 | Constant *NewCallee = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10496 | NestF->getType() == PTy ? NestF : |
| 10497 | Context->getConstantExprBitCast(NestF, PTy); |
| Duncan Sands | 74833f2 | 2007-09-17 10:26:40 +0000 | [diff] [blame] | 10498 | CS.setCalledFunction(NewCallee); |
| 10499 | return CS.getInstruction(); |
| 10500 | } |
| 10501 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10502 | /// FoldPHIArgBinOpIntoPHI - If we have something like phi [add (a,b), add(c,d)] |
| 10503 | /// and if a/b/c/d and the add's all have a single use, turn this into two phi's |
| 10504 | /// and a single binop. |
| 10505 | Instruction *InstCombiner::FoldPHIArgBinOpIntoPHI(PHINode &PN) { |
| 10506 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| Chris Lattner | 3007801 | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 10507 | assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10508 | unsigned Opc = FirstInst->getOpcode(); |
| 10509 | Value *LHSVal = FirstInst->getOperand(0); |
| 10510 | Value *RHSVal = FirstInst->getOperand(1); |
| 10511 | |
| 10512 | const Type *LHSType = LHSVal->getType(); |
| 10513 | const Type *RHSType = RHSVal->getType(); |
| 10514 | |
| 10515 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 10516 | // kill their operands (i.e. the operands have one use). |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10517 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10518 | Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i)); |
| 10519 | if (!I || I->getOpcode() != Opc || !I->hasOneUse() || |
| 10520 | // Verify type of the LHS matches so we don't fold cmp's of different |
| 10521 | // types or GEP's with different index types. |
| 10522 | I->getOperand(0)->getType() != LHSType || |
| 10523 | I->getOperand(1)->getType() != RHSType) |
| 10524 | return 0; |
| 10525 | |
| 10526 | // If they are CmpInst instructions, check their predicates |
| 10527 | if (Opc == Instruction::ICmp || Opc == Instruction::FCmp) |
| 10528 | if (cast<CmpInst>(I)->getPredicate() != |
| 10529 | cast<CmpInst>(FirstInst)->getPredicate()) |
| 10530 | return 0; |
| 10531 | |
| 10532 | // Keep track of which operand needs a phi node. |
| 10533 | if (I->getOperand(0) != LHSVal) LHSVal = 0; |
| 10534 | if (I->getOperand(1) != RHSVal) RHSVal = 0; |
| 10535 | } |
| 10536 | |
| Chris Lattner | 3007801 | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 10537 | // Otherwise, this is safe to transform! |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10538 | |
| 10539 | Value *InLHS = FirstInst->getOperand(0); |
| 10540 | Value *InRHS = FirstInst->getOperand(1); |
| 10541 | PHINode *NewLHS = 0, *NewRHS = 0; |
| 10542 | if (LHSVal == 0) { |
| Gabor Greif | b91ea9d | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10543 | NewLHS = PHINode::Create(LHSType, |
| 10544 | FirstInst->getOperand(0)->getName() + ".pn"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10545 | NewLHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 10546 | NewLHS->addIncoming(InLHS, PN.getIncomingBlock(0)); |
| 10547 | InsertNewInstBefore(NewLHS, PN); |
| 10548 | LHSVal = NewLHS; |
| 10549 | } |
| 10550 | |
| 10551 | if (RHSVal == 0) { |
| Gabor Greif | b91ea9d | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 10552 | NewRHS = PHINode::Create(RHSType, |
| 10553 | FirstInst->getOperand(1)->getName() + ".pn"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10554 | NewRHS->reserveOperandSpace(PN.getNumOperands()/2); |
| 10555 | NewRHS->addIncoming(InRHS, PN.getIncomingBlock(0)); |
| 10556 | InsertNewInstBefore(NewRHS, PN); |
| 10557 | RHSVal = NewRHS; |
| 10558 | } |
| 10559 | |
| 10560 | // Add all operands to the new PHIs. |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10561 | if (NewLHS || NewRHS) { |
| 10562 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10563 | Instruction *InInst = cast<Instruction>(PN.getIncomingValue(i)); |
| 10564 | if (NewLHS) { |
| 10565 | Value *NewInLHS = InInst->getOperand(0); |
| 10566 | NewLHS->addIncoming(NewInLHS, PN.getIncomingBlock(i)); |
| 10567 | } |
| 10568 | if (NewRHS) { |
| 10569 | Value *NewInRHS = InInst->getOperand(1); |
| 10570 | NewRHS->addIncoming(NewInRHS, PN.getIncomingBlock(i)); |
| 10571 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10572 | } |
| 10573 | } |
| 10574 | |
| 10575 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10576 | return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal); |
| Chris Lattner | 3007801 | 2008-12-01 03:42:51 +0000 | [diff] [blame] | 10577 | CmpInst *CIOp = cast<CmpInst>(FirstInst); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 10578 | return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(), |
| 10579 | LHSVal, RHSVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10580 | } |
| 10581 | |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10582 | Instruction *InstCombiner::FoldPHIArgGEPIntoPHI(PHINode &PN) { |
| 10583 | GetElementPtrInst *FirstInst =cast<GetElementPtrInst>(PN.getIncomingValue(0)); |
| 10584 | |
| 10585 | SmallVector<Value*, 16> FixedOperands(FirstInst->op_begin(), |
| 10586 | FirstInst->op_end()); |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10587 | // This is true if all GEP bases are allocas and if all indices into them are |
| 10588 | // constants. |
| 10589 | bool AllBasePointersAreAllocas = true; |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10590 | |
| 10591 | // Scan to see if all operands are the same opcode, all have one use, and all |
| 10592 | // kill their operands (i.e. the operands have one use). |
| 10593 | for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) { |
| 10594 | GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 10595 | if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() || |
| 10596 | GEP->getNumOperands() != FirstInst->getNumOperands()) |
| 10597 | return 0; |
| 10598 | |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10599 | // Keep track of whether or not all GEPs are of alloca pointers. |
| 10600 | if (AllBasePointersAreAllocas && |
| 10601 | (!isa<AllocaInst>(GEP->getOperand(0)) || |
| 10602 | !GEP->hasAllConstantIndices())) |
| 10603 | AllBasePointersAreAllocas = false; |
| 10604 | |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10605 | // Compare the operand lists. |
| 10606 | for (unsigned op = 0, e = FirstInst->getNumOperands(); op != e; ++op) { |
| 10607 | if (FirstInst->getOperand(op) == GEP->getOperand(op)) |
| 10608 | continue; |
| 10609 | |
| 10610 | // Don't merge two GEPs when two operands differ (introducing phi nodes) |
| 10611 | // if one of the PHIs has a constant for the index. The index may be |
| 10612 | // substantially cheaper to compute for the constants, so making it a |
| 10613 | // variable index could pessimize the path. This also handles the case |
| 10614 | // for struct indices, which must always be constant. |
| 10615 | if (isa<ConstantInt>(FirstInst->getOperand(op)) || |
| 10616 | isa<ConstantInt>(GEP->getOperand(op))) |
| 10617 | return 0; |
| 10618 | |
| 10619 | if (FirstInst->getOperand(op)->getType() !=GEP->getOperand(op)->getType()) |
| 10620 | return 0; |
| 10621 | FixedOperands[op] = 0; // Needs a PHI. |
| 10622 | } |
| 10623 | } |
| 10624 | |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10625 | // If all of the base pointers of the PHI'd GEPs are from allocas, don't |
| Chris Lattner | f1e30c8 | 2009-02-23 05:56:17 +0000 | [diff] [blame] | 10626 | // bother doing this transformation. At best, this will just save a bit of |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10627 | // offset calculation, but all the predecessors will have to materialize the |
| 10628 | // stack address into a register anyway. We'd actually rather *clone* the |
| 10629 | // load up into the predecessors so that we have a load of a gep of an alloca, |
| 10630 | // which can usually all be folded into the load. |
| 10631 | if (AllBasePointersAreAllocas) |
| 10632 | return 0; |
| 10633 | |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10634 | // Otherwise, this is safe to transform. Insert PHI nodes for each operand |
| 10635 | // that is variable. |
| 10636 | SmallVector<PHINode*, 16> OperandPhis(FixedOperands.size()); |
| 10637 | |
| 10638 | bool HasAnyPHIs = false; |
| 10639 | for (unsigned i = 0, e = FixedOperands.size(); i != e; ++i) { |
| 10640 | if (FixedOperands[i]) continue; // operand doesn't need a phi. |
| 10641 | Value *FirstOp = FirstInst->getOperand(i); |
| 10642 | PHINode *NewPN = PHINode::Create(FirstOp->getType(), |
| 10643 | FirstOp->getName()+".pn"); |
| 10644 | InsertNewInstBefore(NewPN, PN); |
| 10645 | |
| 10646 | NewPN->reserveOperandSpace(e); |
| 10647 | NewPN->addIncoming(FirstOp, PN.getIncomingBlock(0)); |
| 10648 | OperandPhis[i] = NewPN; |
| 10649 | FixedOperands[i] = NewPN; |
| 10650 | HasAnyPHIs = true; |
| 10651 | } |
| 10652 | |
| 10653 | |
| 10654 | // Add all operands to the new PHIs. |
| 10655 | if (HasAnyPHIs) { |
| 10656 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10657 | GetElementPtrInst *InGEP =cast<GetElementPtrInst>(PN.getIncomingValue(i)); |
| 10658 | BasicBlock *InBB = PN.getIncomingBlock(i); |
| 10659 | |
| 10660 | for (unsigned op = 0, e = OperandPhis.size(); op != e; ++op) |
| 10661 | if (PHINode *OpPhi = OperandPhis[op]) |
| 10662 | OpPhi->addIncoming(InGEP->getOperand(op), InBB); |
| 10663 | } |
| 10664 | } |
| 10665 | |
| 10666 | Value *Base = FixedOperands[0]; |
| 10667 | return GetElementPtrInst::Create(Base, FixedOperands.begin()+1, |
| 10668 | FixedOperands.end()); |
| 10669 | } |
| 10670 | |
| 10671 | |
| Chris Lattner | f1e30c8 | 2009-02-23 05:56:17 +0000 | [diff] [blame] | 10672 | /// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to |
| 10673 | /// sink the load out of the block that defines it. This means that it must be |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10674 | /// obvious the value of the load is not changed from the point of the load to |
| 10675 | /// the end of the block it is in. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10676 | /// |
| 10677 | /// Finally, it is safe, but not profitable, to sink a load targetting a |
| 10678 | /// non-address-taken alloca. Doing so will cause us to not promote the alloca |
| 10679 | /// to a register. |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10680 | static bool isSafeAndProfitableToSinkLoad(LoadInst *L) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10681 | BasicBlock::iterator BBI = L, E = L->getParent()->end(); |
| 10682 | |
| 10683 | for (++BBI; BBI != E; ++BBI) |
| 10684 | if (BBI->mayWriteToMemory()) |
| 10685 | return false; |
| 10686 | |
| 10687 | // Check for non-address taken alloca. If not address-taken already, it isn't |
| 10688 | // profitable to do this xform. |
| 10689 | if (AllocaInst *AI = dyn_cast<AllocaInst>(L->getOperand(0))) { |
| 10690 | bool isAddressTaken = false; |
| 10691 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 10692 | UI != E; ++UI) { |
| 10693 | if (isa<LoadInst>(UI)) continue; |
| 10694 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 10695 | // If storing TO the alloca, then the address isn't taken. |
| 10696 | if (SI->getOperand(1) == AI) continue; |
| 10697 | } |
| 10698 | isAddressTaken = true; |
| 10699 | break; |
| 10700 | } |
| 10701 | |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10702 | if (!isAddressTaken && AI->isStaticAlloca()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10703 | return false; |
| 10704 | } |
| 10705 | |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10706 | // If this load is a load from a GEP with a constant offset from an alloca, |
| 10707 | // then we don't want to sink it. In its present form, it will be |
| 10708 | // load [constant stack offset]. Sinking it will cause us to have to |
| 10709 | // materialize the stack addresses in each predecessor in a register only to |
| 10710 | // do a shared load from register in the successor. |
| 10711 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(L->getOperand(0))) |
| 10712 | if (AllocaInst *AI = dyn_cast<AllocaInst>(GEP->getOperand(0))) |
| 10713 | if (AI->isStaticAlloca() && GEP->hasAllConstantIndices()) |
| 10714 | return false; |
| 10715 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10716 | return true; |
| 10717 | } |
| 10718 | |
| 10719 | |
| 10720 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 10721 | // operator and they all are only used by the PHI, PHI together their |
| 10722 | // inputs, and do the operation once, to the result of the PHI. |
| 10723 | Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { |
| 10724 | Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0)); |
| 10725 | |
| 10726 | // Scan the instruction, looking for input operations that can be folded away. |
| 10727 | // If all input operands to the phi are the same instruction (e.g. a cast from |
| 10728 | // the same type or "+42") we can pull the operation through the PHI, reducing |
| 10729 | // code size and simplifying code. |
| 10730 | Constant *ConstantOp = 0; |
| 10731 | const Type *CastSrcTy = 0; |
| 10732 | bool isVolatile = false; |
| 10733 | if (isa<CastInst>(FirstInst)) { |
| 10734 | CastSrcTy = FirstInst->getOperand(0)->getType(); |
| 10735 | } else if (isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst)) { |
| 10736 | // Can fold binop, compare or shift here if the RHS is a constant, |
| 10737 | // otherwise call FoldPHIArgBinOpIntoPHI. |
| 10738 | ConstantOp = dyn_cast<Constant>(FirstInst->getOperand(1)); |
| 10739 | if (ConstantOp == 0) |
| 10740 | return FoldPHIArgBinOpIntoPHI(PN); |
| 10741 | } else if (LoadInst *LI = dyn_cast<LoadInst>(FirstInst)) { |
| 10742 | isVolatile = LI->isVolatile(); |
| 10743 | // We can't sink the load if the loaded value could be modified between the |
| 10744 | // load and the PHI. |
| 10745 | if (LI->getParent() != PN.getIncomingBlock(0) || |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10746 | !isSafeAndProfitableToSinkLoad(LI)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10747 | return 0; |
| Chris Lattner | 2d9fdd8 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 10748 | |
| 10749 | // If the PHI is of volatile loads and the load block has multiple |
| 10750 | // successors, sinking it would remove a load of the volatile value from |
| 10751 | // the path through the other successor. |
| 10752 | if (isVolatile && |
| 10753 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 10754 | return 0; |
| 10755 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10756 | } else if (isa<GetElementPtrInst>(FirstInst)) { |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10757 | return FoldPHIArgGEPIntoPHI(PN); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10758 | } else { |
| 10759 | return 0; // Cannot fold this operation. |
| 10760 | } |
| 10761 | |
| 10762 | // Check to see if all arguments are the same operation. |
| 10763 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10764 | if (!isa<Instruction>(PN.getIncomingValue(i))) return 0; |
| 10765 | Instruction *I = cast<Instruction>(PN.getIncomingValue(i)); |
| 10766 | if (!I->hasOneUse() || !I->isSameOperationAs(FirstInst)) |
| 10767 | return 0; |
| 10768 | if (CastSrcTy) { |
| 10769 | if (I->getOperand(0)->getType() != CastSrcTy) |
| 10770 | return 0; // Cast operation must match. |
| 10771 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 10772 | // We can't sink the load if the loaded value could be modified between |
| 10773 | // the load and the PHI. |
| 10774 | if (LI->isVolatile() != isVolatile || |
| 10775 | LI->getParent() != PN.getIncomingBlock(i) || |
| Chris Lattner | adf354b | 2009-02-21 00:46:50 +0000 | [diff] [blame] | 10776 | !isSafeAndProfitableToSinkLoad(LI)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10777 | return 0; |
| Chris Lattner | f786701 | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 10778 | |
| Chris Lattner | 2d9fdd8 | 2008-07-08 17:18:32 +0000 | [diff] [blame] | 10779 | // If the PHI is of volatile loads and the load block has multiple |
| 10780 | // successors, sinking it would remove a load of the volatile value from |
| 10781 | // the path through the other successor. |
| Chris Lattner | f786701 | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 10782 | if (isVolatile && |
| 10783 | LI->getParent()->getTerminator()->getNumSuccessors() != 1) |
| 10784 | return 0; |
| Chris Lattner | f786701 | 2008-04-29 17:28:22 +0000 | [diff] [blame] | 10785 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10786 | } else if (I->getOperand(1) != ConstantOp) { |
| 10787 | return 0; |
| 10788 | } |
| 10789 | } |
| 10790 | |
| 10791 | // Okay, they are all the same operation. Create a new PHI node of the |
| 10792 | // correct type, and PHI together all of the LHS's of the instructions. |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 10793 | PHINode *NewPN = PHINode::Create(FirstInst->getOperand(0)->getType(), |
| 10794 | PN.getName()+".in"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10795 | NewPN->reserveOperandSpace(PN.getNumOperands()/2); |
| 10796 | |
| 10797 | Value *InVal = FirstInst->getOperand(0); |
| 10798 | NewPN->addIncoming(InVal, PN.getIncomingBlock(0)); |
| 10799 | |
| 10800 | // Add all operands to the new PHI. |
| 10801 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) { |
| 10802 | Value *NewInVal = cast<Instruction>(PN.getIncomingValue(i))->getOperand(0); |
| 10803 | if (NewInVal != InVal) |
| 10804 | InVal = 0; |
| 10805 | NewPN->addIncoming(NewInVal, PN.getIncomingBlock(i)); |
| 10806 | } |
| 10807 | |
| 10808 | Value *PhiVal; |
| 10809 | if (InVal) { |
| 10810 | // The new PHI unions all of the same values together. This is really |
| 10811 | // common, so we handle it intelligently here for compile-time speed. |
| 10812 | PhiVal = InVal; |
| 10813 | delete NewPN; |
| 10814 | } else { |
| 10815 | InsertNewInstBefore(NewPN, PN); |
| 10816 | PhiVal = NewPN; |
| 10817 | } |
| 10818 | |
| 10819 | // Insert and return the new operation. |
| 10820 | if (CastInst* FirstCI = dyn_cast<CastInst>(FirstInst)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10821 | return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType()); |
| Chris Lattner | fc984e9 | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 10822 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(FirstInst)) |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 10823 | return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp); |
| Chris Lattner | fc984e9 | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 10824 | if (CmpInst *CIOp = dyn_cast<CmpInst>(FirstInst)) |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 10825 | return CmpInst::Create(*Context, CIOp->getOpcode(), CIOp->getPredicate(), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10826 | PhiVal, ConstantOp); |
| Chris Lattner | fc984e9 | 2008-04-29 17:13:43 +0000 | [diff] [blame] | 10827 | assert(isa<LoadInst>(FirstInst) && "Unknown operation"); |
| 10828 | |
| 10829 | // If this was a volatile load that we are merging, make sure to loop through |
| 10830 | // and mark all the input loads as non-volatile. If we don't do this, we will |
| 10831 | // insert a new volatile load and the old ones will not be deletable. |
| 10832 | if (isVolatile) |
| 10833 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 10834 | cast<LoadInst>(PN.getIncomingValue(i))->setVolatile(false); |
| 10835 | |
| 10836 | return new LoadInst(PhiVal, "", isVolatile); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10837 | } |
| 10838 | |
| 10839 | /// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle |
| 10840 | /// that is dead. |
| 10841 | static bool DeadPHICycle(PHINode *PN, |
| 10842 | SmallPtrSet<PHINode*, 16> &PotentiallyDeadPHIs) { |
| 10843 | if (PN->use_empty()) return true; |
| 10844 | if (!PN->hasOneUse()) return false; |
| 10845 | |
| 10846 | // Remember this node, and if we find the cycle, return. |
| 10847 | if (!PotentiallyDeadPHIs.insert(PN)) |
| 10848 | return true; |
| Chris Lattner | adf2e34 | 2007-08-28 04:23:55 +0000 | [diff] [blame] | 10849 | |
| 10850 | // Don't scan crazily complex things. |
| 10851 | if (PotentiallyDeadPHIs.size() == 16) |
| 10852 | return false; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10853 | |
| 10854 | if (PHINode *PU = dyn_cast<PHINode>(PN->use_back())) |
| 10855 | return DeadPHICycle(PU, PotentiallyDeadPHIs); |
| 10856 | |
| 10857 | return false; |
| 10858 | } |
| 10859 | |
| Chris Lattner | 27b695d | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 10860 | /// PHIsEqualValue - Return true if this phi node is always equal to |
| 10861 | /// NonPhiInVal. This happens with mutually cyclic phi nodes like: |
| 10862 | /// z = some value; x = phi (y, z); y = phi (x, z) |
| 10863 | static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal, |
| 10864 | SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) { |
| 10865 | // See if we already saw this PHI node. |
| 10866 | if (!ValueEqualPHIs.insert(PN)) |
| 10867 | return true; |
| 10868 | |
| 10869 | // Don't scan crazily complex things. |
| 10870 | if (ValueEqualPHIs.size() == 16) |
| 10871 | return false; |
| 10872 | |
| 10873 | // Scan the operands to see if they are either phi nodes or are equal to |
| 10874 | // the value. |
| 10875 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 10876 | Value *Op = PN->getIncomingValue(i); |
| 10877 | if (PHINode *OpPN = dyn_cast<PHINode>(Op)) { |
| 10878 | if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs)) |
| 10879 | return false; |
| 10880 | } else if (Op != NonPhiInVal) |
| 10881 | return false; |
| 10882 | } |
| 10883 | |
| 10884 | return true; |
| 10885 | } |
| 10886 | |
| 10887 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10888 | // PHINode simplification |
| 10889 | // |
| 10890 | Instruction *InstCombiner::visitPHINode(PHINode &PN) { |
| 10891 | // If LCSSA is around, don't mess with Phi nodes |
| 10892 | if (MustPreserveLCSSA) return 0; |
| 10893 | |
| 10894 | if (Value *V = PN.hasConstantValue()) |
| 10895 | return ReplaceInstUsesWith(PN, V); |
| 10896 | |
| 10897 | // If all PHI operands are the same operation, pull them through the PHI, |
| 10898 | // reducing code size. |
| 10899 | if (isa<Instruction>(PN.getIncomingValue(0)) && |
| Chris Lattner | 9e1916e | 2008-12-01 02:34:36 +0000 | [diff] [blame] | 10900 | isa<Instruction>(PN.getIncomingValue(1)) && |
| 10901 | cast<Instruction>(PN.getIncomingValue(0))->getOpcode() == |
| 10902 | cast<Instruction>(PN.getIncomingValue(1))->getOpcode() && |
| 10903 | // FIXME: The hasOneUse check will fail for PHIs that use the value more |
| 10904 | // than themselves more than once. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10905 | PN.getIncomingValue(0)->hasOneUse()) |
| 10906 | if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) |
| 10907 | return Result; |
| 10908 | |
| 10909 | // If this is a trivial cycle in the PHI node graph, remove it. Basically, if |
| 10910 | // this PHI only has a single use (a PHI), and if that PHI only has one use (a |
| 10911 | // PHI)... break the cycle. |
| 10912 | if (PN.hasOneUse()) { |
| 10913 | Instruction *PHIUser = cast<Instruction>(PN.use_back()); |
| 10914 | if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) { |
| 10915 | SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs; |
| 10916 | PotentiallyDeadPHIs.insert(&PN); |
| 10917 | if (DeadPHICycle(PU, PotentiallyDeadPHIs)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10918 | return ReplaceInstUsesWith(PN, Context->getUndef(PN.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10919 | } |
| 10920 | |
| 10921 | // If this phi has a single use, and if that use just computes a value for |
| 10922 | // the next iteration of a loop, delete the phi. This occurs with unused |
| 10923 | // induction variables, e.g. "for (int j = 0; ; ++j);". Detecting this |
| 10924 | // common case here is good because the only other things that catch this |
| 10925 | // are induction variable analysis (sometimes) and ADCE, which is only run |
| 10926 | // late. |
| 10927 | if (PHIUser->hasOneUse() && |
| 10928 | (isa<BinaryOperator>(PHIUser) || isa<GetElementPtrInst>(PHIUser)) && |
| 10929 | PHIUser->use_back() == &PN) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10930 | return ReplaceInstUsesWith(PN, Context->getUndef(PN.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10931 | } |
| 10932 | } |
| 10933 | |
| Chris Lattner | 27b695d | 2007-11-06 21:52:06 +0000 | [diff] [blame] | 10934 | // We sometimes end up with phi cycles that non-obviously end up being the |
| 10935 | // same value, for example: |
| 10936 | // z = some value; x = phi (y, z); y = phi (x, z) |
| 10937 | // where the phi nodes don't necessarily need to be in the same block. Do a |
| 10938 | // quick check to see if the PHI node only contains a single non-phi value, if |
| 10939 | // so, scan to see if the phi cycle is actually equal to that value. |
| 10940 | { |
| 10941 | unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues(); |
| 10942 | // Scan for the first non-phi operand. |
| 10943 | while (InValNo != NumOperandVals && |
| 10944 | isa<PHINode>(PN.getIncomingValue(InValNo))) |
| 10945 | ++InValNo; |
| 10946 | |
| 10947 | if (InValNo != NumOperandVals) { |
| 10948 | Value *NonPhiInVal = PN.getOperand(InValNo); |
| 10949 | |
| 10950 | // Scan the rest of the operands to see if there are any conflicts, if so |
| 10951 | // there is no need to recursively scan other phis. |
| 10952 | for (++InValNo; InValNo != NumOperandVals; ++InValNo) { |
| 10953 | Value *OpVal = PN.getIncomingValue(InValNo); |
| 10954 | if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal)) |
| 10955 | break; |
| 10956 | } |
| 10957 | |
| 10958 | // If we scanned over all operands, then we have one unique value plus |
| 10959 | // phi values. Scan PHI nodes to see if they all merge in each other or |
| 10960 | // the value. |
| 10961 | if (InValNo == NumOperandVals) { |
| 10962 | SmallPtrSet<PHINode*, 16> ValueEqualPHIs; |
| 10963 | if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs)) |
| 10964 | return ReplaceInstUsesWith(PN, NonPhiInVal); |
| 10965 | } |
| 10966 | } |
| 10967 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10968 | return 0; |
| 10969 | } |
| 10970 | |
| 10971 | static Value *InsertCastToIntPtrTy(Value *V, const Type *DTy, |
| 10972 | Instruction *InsertPoint, |
| 10973 | InstCombiner *IC) { |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 10974 | unsigned PtrSize = DTy->getScalarSizeInBits(); |
| 10975 | unsigned VTySize = V->getType()->getScalarSizeInBits(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10976 | // We must cast correctly to the pointer type. Ensure that we |
| 10977 | // sign extend the integer value if it is smaller as this is |
| 10978 | // used for address computation. |
| 10979 | Instruction::CastOps opcode = |
| 10980 | (VTySize < PtrSize ? Instruction::SExt : |
| 10981 | (VTySize == PtrSize ? Instruction::BitCast : Instruction::Trunc)); |
| 10982 | return IC->InsertCastBefore(opcode, V, DTy, *InsertPoint); |
| 10983 | } |
| 10984 | |
| 10985 | |
| 10986 | Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
| 10987 | Value *PtrOp = GEP.getOperand(0); |
| 10988 | // Is it 'getelementptr %P, i32 0' or 'getelementptr %P' |
| 10989 | // If so, eliminate the noop. |
| 10990 | if (GEP.getNumOperands() == 1) |
| 10991 | return ReplaceInstUsesWith(GEP, PtrOp); |
| 10992 | |
| 10993 | if (isa<UndefValue>(GEP.getOperand(0))) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 10994 | return ReplaceInstUsesWith(GEP, Context->getUndef(GEP.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 10995 | |
| 10996 | bool HasZeroPointerIndex = false; |
| 10997 | if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1))) |
| 10998 | HasZeroPointerIndex = C->isNullValue(); |
| 10999 | |
| 11000 | if (GEP.getNumOperands() == 2 && HasZeroPointerIndex) |
| 11001 | return ReplaceInstUsesWith(GEP, PtrOp); |
| 11002 | |
| 11003 | // Eliminate unneeded casts for indices. |
| 11004 | bool MadeChange = false; |
| 11005 | |
| 11006 | gep_type_iterator GTI = gep_type_begin(GEP); |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11007 | for (User::op_iterator i = GEP.op_begin() + 1, e = GEP.op_end(); |
| 11008 | i != e; ++i, ++GTI) { |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11009 | if (TD && isa<SequentialType>(*GTI)) { |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11010 | if (CastInst *CI = dyn_cast<CastInst>(*i)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11011 | if (CI->getOpcode() == Instruction::ZExt || |
| 11012 | CI->getOpcode() == Instruction::SExt) { |
| 11013 | const Type *SrcTy = CI->getOperand(0)->getType(); |
| 11014 | // We can eliminate a cast from i32 to i64 iff the target |
| 11015 | // is a 32-bit pointer target. |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 11016 | if (SrcTy->getScalarSizeInBits() >= TD->getPointerSizeInBits()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11017 | MadeChange = true; |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11018 | *i = CI->getOperand(0); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11019 | } |
| 11020 | } |
| 11021 | } |
| 11022 | // If we are using a wider index than needed for this platform, shrink it |
| Dan Gohman | 5d639ed | 2008-09-11 23:06:38 +0000 | [diff] [blame] | 11023 | // to what we need. If narrower, sign-extend it to what we need. |
| 11024 | // If the incoming value needs a cast instruction, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11025 | // insert it. This explicit cast can make subsequent optimizations more |
| 11026 | // obvious. |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11027 | Value *Op = *i; |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11028 | if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11029 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11030 | *i = Context->getConstantExprTrunc(C, TD->getIntPtrType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11031 | MadeChange = true; |
| 11032 | } else { |
| 11033 | Op = InsertCastBefore(Instruction::Trunc, Op, TD->getIntPtrType(), |
| 11034 | GEP); |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 11035 | *i = Op; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11036 | MadeChange = true; |
| 11037 | } |
| Dan Gohman | 5d639ed | 2008-09-11 23:06:38 +0000 | [diff] [blame] | 11038 | } else if (TD->getTypeSizeInBits(Op->getType()) < TD->getPointerSizeInBits()) { |
| 11039 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11040 | *i = Context->getConstantExprSExt(C, TD->getIntPtrType()); |
| Dan Gohman | 5d639ed | 2008-09-11 23:06:38 +0000 | [diff] [blame] | 11041 | MadeChange = true; |
| 11042 | } else { |
| 11043 | Op = InsertCastBefore(Instruction::SExt, Op, TD->getIntPtrType(), |
| 11044 | GEP); |
| 11045 | *i = Op; |
| 11046 | MadeChange = true; |
| 11047 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11048 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11049 | } |
| 11050 | } |
| 11051 | if (MadeChange) return &GEP; |
| 11052 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11053 | // Combine Indices - If the source pointer to this getelementptr instruction |
| 11054 | // is a getelementptr instruction, combine the indices of the two |
| 11055 | // getelementptr instructions into a single instruction. |
| 11056 | // |
| 11057 | SmallVector<Value*, 8> SrcGEPOperands; |
| 11058 | if (User *Src = dyn_castGetElementPtr(PtrOp)) |
| 11059 | SrcGEPOperands.append(Src->op_begin(), Src->op_end()); |
| 11060 | |
| 11061 | if (!SrcGEPOperands.empty()) { |
| 11062 | // Note that if our source is a gep chain itself that we wait for that |
| 11063 | // chain to be resolved before we perform this transformation. This |
| 11064 | // avoids us creating a TON of code in some cases. |
| 11065 | // |
| 11066 | if (isa<GetElementPtrInst>(SrcGEPOperands[0]) && |
| 11067 | cast<Instruction>(SrcGEPOperands[0])->getNumOperands() == 2) |
| 11068 | return 0; // Wait until our source is folded to completion. |
| 11069 | |
| 11070 | SmallVector<Value*, 8> Indices; |
| 11071 | |
| 11072 | // Find out whether the last index in the source GEP is a sequential idx. |
| 11073 | bool EndsWithSequential = false; |
| 11074 | for (gep_type_iterator I = gep_type_begin(*cast<User>(PtrOp)), |
| 11075 | E = gep_type_end(*cast<User>(PtrOp)); I != E; ++I) |
| 11076 | EndsWithSequential = !isa<StructType>(*I); |
| 11077 | |
| 11078 | // Can we combine the two pointer arithmetics offsets? |
| 11079 | if (EndsWithSequential) { |
| 11080 | // Replace: gep (gep %P, long B), long A, ... |
| 11081 | // With: T = long A+B; gep %P, T, ... |
| 11082 | // |
| 11083 | Value *Sum, *SO1 = SrcGEPOperands.back(), *GO1 = GEP.getOperand(1); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11084 | if (SO1 == Context->getNullValue(SO1->getType())) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11085 | Sum = GO1; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11086 | } else if (GO1 == Context->getNullValue(GO1->getType())) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11087 | Sum = SO1; |
| 11088 | } else { |
| 11089 | // If they aren't the same type, convert both to an integer of the |
| 11090 | // target's pointer size. |
| 11091 | if (SO1->getType() != GO1->getType()) { |
| 11092 | if (Constant *SO1C = dyn_cast<Constant>(SO1)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11093 | SO1 = |
| 11094 | Context->getConstantExprIntegerCast(SO1C, GO1->getType(), true); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11095 | } else if (Constant *GO1C = dyn_cast<Constant>(GO1)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11096 | GO1 = |
| 11097 | Context->getConstantExprIntegerCast(GO1C, SO1->getType(), true); |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11098 | } else if (TD) { |
| Duncan Sands | f99fdc6 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 11099 | unsigned PS = TD->getPointerSizeInBits(); |
| 11100 | if (TD->getTypeSizeInBits(SO1->getType()) == PS) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11101 | // Convert GO1 to SO1's type. |
| 11102 | GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this); |
| 11103 | |
| Duncan Sands | f99fdc6 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 11104 | } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11105 | // Convert SO1 to GO1's type. |
| 11106 | SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this); |
| 11107 | } else { |
| 11108 | const Type *PT = TD->getIntPtrType(); |
| 11109 | SO1 = InsertCastToIntPtrTy(SO1, PT, &GEP, this); |
| 11110 | GO1 = InsertCastToIntPtrTy(GO1, PT, &GEP, this); |
| 11111 | } |
| 11112 | } |
| 11113 | } |
| 11114 | if (isa<Constant>(SO1) && isa<Constant>(GO1)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11115 | Sum = Context->getConstantExprAdd(cast<Constant>(SO1), |
| 11116 | cast<Constant>(GO1)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11117 | else { |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 11118 | Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11119 | InsertNewInstBefore(cast<Instruction>(Sum), GEP); |
| 11120 | } |
| 11121 | } |
| 11122 | |
| 11123 | // Recycle the GEP we already have if possible. |
| 11124 | if (SrcGEPOperands.size() == 2) { |
| 11125 | GEP.setOperand(0, SrcGEPOperands[0]); |
| 11126 | GEP.setOperand(1, Sum); |
| 11127 | return &GEP; |
| 11128 | } else { |
| 11129 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 11130 | SrcGEPOperands.end()-1); |
| 11131 | Indices.push_back(Sum); |
| 11132 | Indices.insert(Indices.end(), GEP.op_begin()+2, GEP.op_end()); |
| 11133 | } |
| 11134 | } else if (isa<Constant>(*GEP.idx_begin()) && |
| 11135 | cast<Constant>(*GEP.idx_begin())->isNullValue() && |
| 11136 | SrcGEPOperands.size() != 1) { |
| 11137 | // Otherwise we can do the fold if the first index of the GEP is a zero |
| 11138 | Indices.insert(Indices.end(), SrcGEPOperands.begin()+1, |
| 11139 | SrcGEPOperands.end()); |
| 11140 | Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end()); |
| 11141 | } |
| 11142 | |
| 11143 | if (!Indices.empty()) |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11144 | return GetElementPtrInst::Create(SrcGEPOperands[0], Indices.begin(), |
| 11145 | Indices.end(), GEP.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11146 | |
| 11147 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(PtrOp)) { |
| 11148 | // GEP of global variable. If all of the indices for this GEP are |
| 11149 | // constants, we can promote this to a constexpr instead of an instruction. |
| 11150 | |
| 11151 | // Scan for nonconstants... |
| 11152 | SmallVector<Constant*, 8> Indices; |
| 11153 | User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); |
| 11154 | for (; I != E && isa<Constant>(*I); ++I) |
| 11155 | Indices.push_back(cast<Constant>(*I)); |
| 11156 | |
| 11157 | if (I == E) { // If they are all constants... |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11158 | Constant *CE = Context->getConstantExprGetElementPtr(GV, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11159 | &Indices[0],Indices.size()); |
| 11160 | |
| 11161 | // Replace all uses of the GEP with the new constexpr... |
| 11162 | return ReplaceInstUsesWith(GEP, CE); |
| 11163 | } |
| 11164 | } else if (Value *X = getBitCastOperand(PtrOp)) { // Is the operand a cast? |
| 11165 | if (!isa<PointerType>(X->getType())) { |
| 11166 | // Not interesting. Source pointer must be a cast from pointer. |
| 11167 | } else if (HasZeroPointerIndex) { |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11168 | // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... |
| 11169 | // into : GEP [10 x i8]* X, i32 0, ... |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11170 | // |
| Duncan Sands | cf866e6 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 11171 | // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ... |
| 11172 | // into : GEP i8* X, ... |
| 11173 | // |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11174 | // This occurs when the program declares an array extern like "int X[];" |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11175 | const PointerType *CPTy = cast<PointerType>(PtrOp->getType()); |
| 11176 | const PointerType *XTy = cast<PointerType>(X->getType()); |
| Duncan Sands | cf866e6 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 11177 | if (const ArrayType *CATy = |
| 11178 | dyn_cast<ArrayType>(CPTy->getElementType())) { |
| 11179 | // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ? |
| 11180 | if (CATy->getElementType() == XTy->getElementType()) { |
| 11181 | // -> GEP i8* X, ... |
| 11182 | SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end()); |
| 11183 | return GetElementPtrInst::Create(X, Indices.begin(), Indices.end(), |
| 11184 | GEP.getName()); |
| 11185 | } else if (const ArrayType *XATy = |
| 11186 | dyn_cast<ArrayType>(XTy->getElementType())) { |
| 11187 | // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ? |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11188 | if (CATy->getElementType() == XATy->getElementType()) { |
| Duncan Sands | cf866e6 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 11189 | // -> GEP [10 x i8]* X, i32 0, ... |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11190 | // At this point, we know that the cast source type is a pointer |
| 11191 | // to an array of the same type as the destination pointer |
| 11192 | // array. Because the array type is never stepped over (there |
| 11193 | // is a leading zero) we can fold the cast into this GEP. |
| 11194 | GEP.setOperand(0, X); |
| 11195 | return &GEP; |
| 11196 | } |
| Duncan Sands | cf866e6 | 2009-03-02 09:18:21 +0000 | [diff] [blame] | 11197 | } |
| 11198 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11199 | } else if (GEP.getNumOperands() == 2) { |
| 11200 | // Transform things like: |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11201 | // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V |
| 11202 | // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11203 | const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType(); |
| 11204 | const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType(); |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11205 | if (TD && isa<ArrayType>(SrcElTy) && |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 11206 | TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) == |
| 11207 | TD->getTypeAllocSize(ResElTy)) { |
| David Greene | 393be88 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 11208 | Value *Idx[2]; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11209 | Idx[0] = Context->getNullValue(Type::Int32Ty); |
| David Greene | 393be88 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 11210 | Idx[1] = GEP.getOperand(1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11211 | Value *V = InsertNewInstBefore( |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11212 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()), GEP); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11213 | // V and GEP are both pointer types --> BitCast |
| 11214 | return new BitCastInst(V, GEP.getType()); |
| 11215 | } |
| 11216 | |
| 11217 | // Transform things like: |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11218 | // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11219 | // (where tmp = 8*tmp2) into: |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11220 | // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11221 | |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11222 | if (TD && isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11223 | uint64_t ArrayEltSize = |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 11224 | TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11225 | |
| 11226 | // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We |
| 11227 | // allow either a mul, shift, or constant here. |
| 11228 | Value *NewIdx = 0; |
| 11229 | ConstantInt *Scale = 0; |
| 11230 | if (ArrayEltSize == 1) { |
| 11231 | NewIdx = GEP.getOperand(1); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11232 | Scale = |
| 11233 | Context->getConstantInt(cast<IntegerType>(NewIdx->getType()), 1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11234 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11235 | NewIdx = Context->getConstantInt(CI->getType(), 1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11236 | Scale = CI; |
| 11237 | } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){ |
| 11238 | if (Inst->getOpcode() == Instruction::Shl && |
| 11239 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 11240 | ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1)); |
| 11241 | uint32_t ShAmtVal = ShAmt->getLimitedValue(64); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11242 | Scale = Context->getConstantInt(cast<IntegerType>(Inst->getType()), |
| Dan Gohman | 8fd520a | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 11243 | 1ULL << ShAmtVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11244 | NewIdx = Inst->getOperand(0); |
| 11245 | } else if (Inst->getOpcode() == Instruction::Mul && |
| 11246 | isa<ConstantInt>(Inst->getOperand(1))) { |
| 11247 | Scale = cast<ConstantInt>(Inst->getOperand(1)); |
| 11248 | NewIdx = Inst->getOperand(0); |
| 11249 | } |
| 11250 | } |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11251 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11252 | // If the index will be to exactly the right offset with the scale taken |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11253 | // out, perform the transformation. Note, we don't know whether Scale is |
| 11254 | // signed or not. We'll use unsigned version of division/modulo |
| 11255 | // operation after making sure Scale doesn't have the sign bit set. |
| Chris Lattner | 0296271 | 2009-02-25 18:20:01 +0000 | [diff] [blame] | 11256 | if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL && |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11257 | Scale->getZExtValue() % ArrayEltSize == 0) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11258 | Scale = Context->getConstantInt(Scale->getType(), |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11259 | Scale->getZExtValue() / ArrayEltSize); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11260 | if (Scale->getZExtValue() != 1) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11261 | Constant *C = |
| 11262 | Context->getConstantExprIntegerCast(Scale, NewIdx->getType(), |
| Wojciech Matyjewicz | 5b5ab53 | 2007-12-12 15:21:32 +0000 | [diff] [blame] | 11263 | false /*ZExt*/); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 11264 | Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11265 | NewIdx = InsertNewInstBefore(Sc, GEP); |
| 11266 | } |
| 11267 | |
| 11268 | // Insert the new GEP instruction. |
| David Greene | 393be88 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 11269 | Value *Idx[2]; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11270 | Idx[0] = Context->getNullValue(Type::Int32Ty); |
| David Greene | 393be88 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 11271 | Idx[1] = NewIdx; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11272 | Instruction *NewGEP = |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11273 | GetElementPtrInst::Create(X, Idx, Idx + 2, GEP.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11274 | NewGEP = InsertNewInstBefore(NewGEP, GEP); |
| 11275 | // The NewGEP must be pointer typed, so must the old one -> BitCast |
| 11276 | return new BitCastInst(NewGEP, GEP.getType()); |
| 11277 | } |
| 11278 | } |
| 11279 | } |
| 11280 | } |
| Chris Lattner | 111ea77 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 11281 | |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 11282 | /// See if we can simplify: |
| 11283 | /// X = bitcast A to B* |
| 11284 | /// Y = gep X, <...constant indices...> |
| 11285 | /// into a gep of the original struct. This is important for SROA and alias |
| 11286 | /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged. |
| Chris Lattner | 111ea77 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 11287 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) { |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11288 | if (TD && |
| 11289 | !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) { |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 11290 | // Determine how much the GEP moves the pointer. We are guaranteed to get |
| 11291 | // a constant back from EmitGEPOffset. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11292 | ConstantInt *OffsetV = |
| 11293 | cast<ConstantInt>(EmitGEPOffset(&GEP, GEP, *this)); |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 11294 | int64_t Offset = OffsetV->getSExtValue(); |
| 11295 | |
| 11296 | // If this GEP instruction doesn't move the pointer, just replace the GEP |
| 11297 | // with a bitcast of the real input to the dest type. |
| 11298 | if (Offset == 0) { |
| 11299 | // If the bitcast is of an allocation, and the allocation will be |
| 11300 | // converted to match the type of the cast, don't touch this. |
| 11301 | if (isa<AllocationInst>(BCI->getOperand(0))) { |
| 11302 | // See if the bitcast simplifies, if so, don't nuke this GEP yet. |
| 11303 | if (Instruction *I = visitBitCast(*BCI)) { |
| 11304 | if (I != BCI) { |
| 11305 | I->takeName(BCI); |
| 11306 | BCI->getParent()->getInstList().insert(BCI, I); |
| 11307 | ReplaceInstUsesWith(*BCI, I); |
| 11308 | } |
| 11309 | return &GEP; |
| Chris Lattner | 111ea77 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 11310 | } |
| Chris Lattner | 111ea77 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 11311 | } |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 11312 | return new BitCastInst(BCI->getOperand(0), GEP.getType()); |
| Chris Lattner | 111ea77 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 11313 | } |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 11314 | |
| 11315 | // Otherwise, if the offset is non-zero, we need to find out if there is a |
| 11316 | // field at Offset in 'A's type. If so, we can pull the cast through the |
| 11317 | // GEP. |
| 11318 | SmallVector<Value*, 8> NewIndices; |
| 11319 | const Type *InTy = |
| 11320 | cast<PointerType>(BCI->getOperand(0)->getType())->getElementType(); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11321 | if (FindElementAtOffset(InTy, Offset, NewIndices, TD, Context)) { |
| Chris Lattner | 94ccd5f | 2009-01-09 05:44:56 +0000 | [diff] [blame] | 11322 | Instruction *NGEP = |
| 11323 | GetElementPtrInst::Create(BCI->getOperand(0), NewIndices.begin(), |
| 11324 | NewIndices.end()); |
| 11325 | if (NGEP->getType() == GEP.getType()) return NGEP; |
| 11326 | InsertNewInstBefore(NGEP, GEP); |
| 11327 | NGEP->takeName(&GEP); |
| 11328 | return new BitCastInst(NGEP, GEP.getType()); |
| 11329 | } |
| Chris Lattner | 111ea77 | 2009-01-09 04:53:57 +0000 | [diff] [blame] | 11330 | } |
| 11331 | } |
| 11332 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11333 | return 0; |
| 11334 | } |
| 11335 | |
| 11336 | Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) { |
| 11337 | // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1 |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11338 | if (AI.isArrayAllocation()) { // Check C != 1 |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11339 | if (const ConstantInt *C = dyn_cast<ConstantInt>(AI.getArraySize())) { |
| 11340 | const Type *NewTy = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11341 | Context->getArrayType(AI.getAllocatedType(), C->getZExtValue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11342 | AllocationInst *New = 0; |
| 11343 | |
| 11344 | // Create and insert the replacement instruction... |
| 11345 | if (isa<MallocInst>(AI)) |
| Owen Anderson | 140166d | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 11346 | New = new MallocInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11347 | else { |
| 11348 | assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!"); |
| Owen Anderson | 140166d | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 11349 | New = new AllocaInst(NewTy, 0, AI.getAlignment(), AI.getName()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11350 | } |
| 11351 | |
| 11352 | InsertNewInstBefore(New, AI); |
| 11353 | |
| 11354 | // Scan to the end of the allocation instructions, to skip over a block of |
| Dale Johannesen | a499d0d | 2009-03-11 22:19:43 +0000 | [diff] [blame] | 11355 | // allocas if possible...also skip interleaved debug info |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11356 | // |
| 11357 | BasicBlock::iterator It = New; |
| Dale Johannesen | a499d0d | 2009-03-11 22:19:43 +0000 | [diff] [blame] | 11358 | while (isa<AllocationInst>(*It) || isa<DbgInfoIntrinsic>(*It)) ++It; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11359 | |
| 11360 | // Now that I is pointing to the first non-allocation-inst in the block, |
| 11361 | // insert our getelementptr instruction... |
| 11362 | // |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11363 | Value *NullIdx = Context->getNullValue(Type::Int32Ty); |
| David Greene | 393be88 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 11364 | Value *Idx[2]; |
| 11365 | Idx[0] = NullIdx; |
| 11366 | Idx[1] = NullIdx; |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11367 | Value *V = GetElementPtrInst::Create(New, Idx, Idx + 2, |
| 11368 | New->getName()+".sub", It); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11369 | |
| 11370 | // Now make everything use the getelementptr instead of the original |
| 11371 | // allocation. |
| 11372 | return ReplaceInstUsesWith(AI, V); |
| 11373 | } else if (isa<UndefValue>(AI.getArraySize())) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11374 | return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11375 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11376 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11377 | |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11378 | if (TD && isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized()) { |
| Dan Gohman | 28e78f0 | 2009-01-13 20:18:38 +0000 | [diff] [blame] | 11379 | // If alloca'ing a zero byte object, replace the alloca with a null pointer. |
| Chris Lattner | 27cc547 | 2009-03-17 17:55:15 +0000 | [diff] [blame] | 11380 | // Note that we only do this for alloca's, because malloc should allocate |
| 11381 | // and return a unique pointer, even for a zero byte allocation. |
| Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 11382 | if (TD->getTypeAllocSize(AI.getAllocatedType()) == 0) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11383 | return ReplaceInstUsesWith(AI, Context->getNullValue(AI.getType())); |
| Dan Gohman | 28e78f0 | 2009-01-13 20:18:38 +0000 | [diff] [blame] | 11384 | |
| 11385 | // If the alignment is 0 (unspecified), assign it the preferred alignment. |
| 11386 | if (AI.getAlignment() == 0) |
| 11387 | AI.setAlignment(TD->getPrefTypeAlignment(AI.getAllocatedType())); |
| 11388 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11389 | |
| 11390 | return 0; |
| 11391 | } |
| 11392 | |
| 11393 | Instruction *InstCombiner::visitFreeInst(FreeInst &FI) { |
| 11394 | Value *Op = FI.getOperand(0); |
| 11395 | |
| 11396 | // free undef -> unreachable. |
| 11397 | if (isa<UndefValue>(Op)) { |
| 11398 | // Insert a new store to null because we cannot modify the CFG here. |
| Owen Anderson | 71f286c6 | 2009-07-21 18:03:38 +0000 | [diff] [blame] | 11399 | new StoreInst(Context->getTrue(), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11400 | Context->getUndef(Context->getPointerTypeUnqual(Type::Int1Ty)), &FI); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11401 | return EraseInstFromFunction(FI); |
| 11402 | } |
| 11403 | |
| 11404 | // If we have 'free null' delete the instruction. This can happen in stl code |
| 11405 | // when lots of inlining happens. |
| 11406 | if (isa<ConstantPointerNull>(Op)) |
| 11407 | return EraseInstFromFunction(FI); |
| 11408 | |
| 11409 | // Change free <ty>* (cast <ty2>* X to <ty>*) into free <ty2>* X |
| 11410 | if (BitCastInst *CI = dyn_cast<BitCastInst>(Op)) { |
| 11411 | FI.setOperand(0, CI->getOperand(0)); |
| 11412 | return &FI; |
| 11413 | } |
| 11414 | |
| 11415 | // Change free (gep X, 0,0,0,0) into free(X) |
| 11416 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 11417 | if (GEPI->hasAllZeroIndices()) { |
| 11418 | AddToWorkList(GEPI); |
| 11419 | FI.setOperand(0, GEPI->getOperand(0)); |
| 11420 | return &FI; |
| 11421 | } |
| 11422 | } |
| 11423 | |
| 11424 | // Change free(malloc) into nothing, if the malloc has a single use. |
| 11425 | if (MallocInst *MI = dyn_cast<MallocInst>(Op)) |
| 11426 | if (MI->hasOneUse()) { |
| 11427 | EraseInstFromFunction(FI); |
| 11428 | return EraseInstFromFunction(*MI); |
| 11429 | } |
| 11430 | |
| 11431 | return 0; |
| 11432 | } |
| 11433 | |
| 11434 | |
| 11435 | /// InstCombineLoadCast - Fold 'load (cast P)' -> cast (load P)' when possible. |
| Devang Patel | a0f8ea8 | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11436 | static Instruction *InstCombineLoadCast(InstCombiner &IC, LoadInst &LI, |
| Bill Wendling | 44a36ea | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11437 | const TargetData *TD) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11438 | User *CI = cast<User>(LI.getOperand(0)); |
| 11439 | Value *CastOp = CI->getOperand(0); |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 11440 | LLVMContext *Context = IC.getContext(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11441 | |
| Nick Lewycky | 291c594 | 2009-05-08 06:47:37 +0000 | [diff] [blame] | 11442 | if (TD) { |
| 11443 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CI)) { |
| 11444 | // Instead of loading constant c string, use corresponding integer value |
| 11445 | // directly if string length is small enough. |
| 11446 | std::string Str; |
| 11447 | if (GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) { |
| 11448 | unsigned len = Str.length(); |
| 11449 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
| 11450 | unsigned numBits = Ty->getPrimitiveSizeInBits(); |
| 11451 | // Replace LI with immediate integer store. |
| 11452 | if ((numBits >> 3) == len + 1) { |
| 11453 | APInt StrVal(numBits, 0); |
| 11454 | APInt SingleChar(numBits, 0); |
| 11455 | if (TD->isLittleEndian()) { |
| 11456 | for (signed i = len-1; i >= 0; i--) { |
| 11457 | SingleChar = (uint64_t) Str[i] & UCHAR_MAX; |
| 11458 | StrVal = (StrVal << 8) | SingleChar; |
| 11459 | } |
| 11460 | } else { |
| 11461 | for (unsigned i = 0; i < len; i++) { |
| 11462 | SingleChar = (uint64_t) Str[i] & UCHAR_MAX; |
| 11463 | StrVal = (StrVal << 8) | SingleChar; |
| 11464 | } |
| 11465 | // Append NULL at the end. |
| 11466 | SingleChar = 0; |
| Bill Wendling | 44a36ea | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11467 | StrVal = (StrVal << 8) | SingleChar; |
| 11468 | } |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11469 | Value *NL = Context->getConstantInt(StrVal); |
| Nick Lewycky | 291c594 | 2009-05-08 06:47:37 +0000 | [diff] [blame] | 11470 | return IC.ReplaceInstUsesWith(LI, NL); |
| Bill Wendling | 44a36ea | 2008-02-26 10:53:30 +0000 | [diff] [blame] | 11471 | } |
| Devang Patel | a0f8ea8 | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11472 | } |
| 11473 | } |
| 11474 | } |
| 11475 | |
| Mon P Wang | bd05ed8 | 2009-02-07 22:19:29 +0000 | [diff] [blame] | 11476 | const PointerType *DestTy = cast<PointerType>(CI->getType()); |
| 11477 | const Type *DestPTy = DestTy->getElementType(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11478 | if (const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType())) { |
| Mon P Wang | bd05ed8 | 2009-02-07 22:19:29 +0000 | [diff] [blame] | 11479 | |
| 11480 | // If the address spaces don't match, don't eliminate the cast. |
| 11481 | if (DestTy->getAddressSpace() != SrcTy->getAddressSpace()) |
| 11482 | return 0; |
| 11483 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11484 | const Type *SrcPTy = SrcTy->getElementType(); |
| 11485 | |
| 11486 | if (DestPTy->isInteger() || isa<PointerType>(DestPTy) || |
| 11487 | isa<VectorType>(DestPTy)) { |
| 11488 | // If the source is an array, the code below will not succeed. Check to |
| 11489 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 11490 | // constants. |
| 11491 | if (const ArrayType *ASrcTy = dyn_cast<ArrayType>(SrcPTy)) |
| 11492 | if (Constant *CSrc = dyn_cast<Constant>(CastOp)) |
| 11493 | if (ASrcTy->getNumElements() != 0) { |
| 11494 | Value *Idxs[2]; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11495 | Idxs[0] = Idxs[1] = Context->getNullValue(Type::Int32Ty); |
| 11496 | CastOp = Context->getConstantExprGetElementPtr(CSrc, Idxs, 2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11497 | SrcTy = cast<PointerType>(CastOp->getType()); |
| 11498 | SrcPTy = SrcTy->getElementType(); |
| 11499 | } |
| 11500 | |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11501 | if (IC.getTargetData() && |
| 11502 | (SrcPTy->isInteger() || isa<PointerType>(SrcPTy) || |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11503 | isa<VectorType>(SrcPTy)) && |
| 11504 | // Do not allow turning this into a load of an integer, which is then |
| 11505 | // casted to a pointer, this pessimizes pointer analysis a lot. |
| 11506 | (isa<PointerType>(SrcPTy) == isa<PointerType>(LI.getType())) && |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11507 | IC.getTargetData()->getTypeSizeInBits(SrcPTy) == |
| 11508 | IC.getTargetData()->getTypeSizeInBits(DestPTy)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11509 | |
| 11510 | // Okay, we are casting from one integer or pointer type to another of |
| 11511 | // the same size. Instead of casting the pointer before the load, cast |
| 11512 | // the result of the loaded value. |
| 11513 | Value *NewLoad = IC.InsertNewInstBefore(new LoadInst(CastOp, |
| 11514 | CI->getName(), |
| 11515 | LI.isVolatile()),LI); |
| 11516 | // Now cast the result of the load. |
| 11517 | return new BitCastInst(NewLoad, LI.getType()); |
| 11518 | } |
| 11519 | } |
| 11520 | } |
| 11521 | return 0; |
| 11522 | } |
| 11523 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11524 | Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { |
| 11525 | Value *Op = LI.getOperand(0); |
| 11526 | |
| Dan Gohman | 5c4d0e1 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11527 | // Attempt to improve the alignment. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11528 | if (TD) { |
| 11529 | unsigned KnownAlign = |
| 11530 | GetOrEnforceKnownAlignment(Op, TD->getPrefTypeAlignment(LI.getType())); |
| 11531 | if (KnownAlign > |
| 11532 | (LI.getAlignment() == 0 ? TD->getABITypeAlignment(LI.getType()) : |
| 11533 | LI.getAlignment())) |
| 11534 | LI.setAlignment(KnownAlign); |
| 11535 | } |
| Dan Gohman | 5c4d0e1 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11536 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11537 | // load (cast X) --> cast (load X) iff safe |
| 11538 | if (isa<CastInst>(Op)) |
| Devang Patel | a0f8ea8 | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11539 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11540 | return Res; |
| 11541 | |
| 11542 | // None of the following transforms are legal for volatile loads. |
| 11543 | if (LI.isVolatile()) return 0; |
| 11544 | |
| Dan Gohman | 0ff5a1f | 2008-10-15 23:19:35 +0000 | [diff] [blame] | 11545 | // Do really simple store-to-load forwarding and load CSE, to catch cases |
| 11546 | // where there are several consequtive memory accesses to the same location, |
| 11547 | // separated by a few arithmetic operations. |
| 11548 | BasicBlock::iterator BBI = &LI; |
| Chris Lattner | 6fd8c80 | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11549 | if (Value *AvailableVal = FindAvailableLoadedValue(Op, LI.getParent(), BBI,6)) |
| 11550 | return ReplaceInstUsesWith(LI, AvailableVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11551 | |
| Christopher Lamb | 2c17539 | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 11552 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) { |
| 11553 | const Value *GEPI0 = GEPI->getOperand(0); |
| 11554 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 11555 | if (isa<ConstantPointerNull>(GEPI0) && |
| 11556 | cast<PointerType>(GEPI0->getType())->getAddressSpace() == 0) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11557 | // Insert a new store to null instruction before the load to indicate |
| 11558 | // that this code is not reachable. We do this instead of inserting |
| 11559 | // an unreachable instruction directly because we cannot modify the |
| 11560 | // CFG. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11561 | new StoreInst(Context->getUndef(LI.getType()), |
| 11562 | Context->getNullValue(Op->getType()), &LI); |
| 11563 | return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11564 | } |
| Christopher Lamb | 2c17539 | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 11565 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11566 | |
| 11567 | if (Constant *C = dyn_cast<Constant>(Op)) { |
| 11568 | // load null/undef -> undef |
| Christopher Lamb | 2c17539 | 2007-12-29 07:56:53 +0000 | [diff] [blame] | 11569 | // TODO: Consider a target hook for valid address spaces for this xform. |
| 11570 | if (isa<UndefValue>(C) || (C->isNullValue() && |
| 11571 | cast<PointerType>(Op->getType())->getAddressSpace() == 0)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11572 | // Insert a new store to null instruction before the load to indicate that |
| 11573 | // this code is not reachable. We do this instead of inserting an |
| 11574 | // unreachable instruction directly because we cannot modify the CFG. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11575 | new StoreInst(Context->getUndef(LI.getType()), |
| 11576 | Context->getNullValue(Op->getType()), &LI); |
| 11577 | return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11578 | } |
| 11579 | |
| 11580 | // Instcombine load (constant global) into the value loaded. |
| 11581 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op)) |
| Duncan Sands | 54e70f6 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 11582 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11583 | return ReplaceInstUsesWith(LI, GV->getInitializer()); |
| 11584 | |
| 11585 | // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded. |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11586 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11587 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 11588 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| Duncan Sands | 54e70f6 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 11589 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11590 | if (Constant *V = |
| Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 11591 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE, |
| Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 11592 | *Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11593 | return ReplaceInstUsesWith(LI, V); |
| 11594 | if (CE->getOperand(0)->isNullValue()) { |
| 11595 | // Insert a new store to null instruction before the load to indicate |
| 11596 | // that this code is not reachable. We do this instead of inserting |
| 11597 | // an unreachable instruction directly because we cannot modify the |
| 11598 | // CFG. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11599 | new StoreInst(Context->getUndef(LI.getType()), |
| 11600 | Context->getNullValue(Op->getType()), &LI); |
| 11601 | return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11602 | } |
| 11603 | |
| 11604 | } else if (CE->isCast()) { |
| Devang Patel | a0f8ea8 | 2007-10-18 19:52:32 +0000 | [diff] [blame] | 11605 | if (Instruction *Res = InstCombineLoadCast(*this, LI, TD)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11606 | return Res; |
| 11607 | } |
| Anton Korobeynikov | 8522e1c | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 11608 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11609 | } |
| Chris Lattner | 0270a11 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 11610 | |
| 11611 | // If this load comes from anywhere in a constant global, and if the global |
| 11612 | // is all undef or zero, we know what it loads. |
| Duncan Sands | 52fb873 | 2008-10-01 15:25:41 +0000 | [diff] [blame] | 11613 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){ |
| Duncan Sands | 54e70f6 | 2009-03-21 21:27:31 +0000 | [diff] [blame] | 11614 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) { |
| Chris Lattner | 0270a11 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 11615 | if (GV->getInitializer()->isNullValue()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11616 | return ReplaceInstUsesWith(LI, Context->getNullValue(LI.getType())); |
| Chris Lattner | 0270a11 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 11617 | else if (isa<UndefValue>(GV->getInitializer())) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11618 | return ReplaceInstUsesWith(LI, Context->getUndef(LI.getType())); |
| Chris Lattner | 0270a11 | 2007-08-11 18:48:48 +0000 | [diff] [blame] | 11619 | } |
| 11620 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11621 | |
| 11622 | if (Op->hasOneUse()) { |
| 11623 | // Change select and PHI nodes to select values instead of addresses: this |
| 11624 | // helps alias analysis out a lot, allows many others simplifications, and |
| 11625 | // exposes redundancy in the code. |
| 11626 | // |
| 11627 | // Note that we cannot do the transformation unless we know that the |
| 11628 | // introduced loads cannot trap! Something like this is valid as long as |
| 11629 | // the condition is always false: load (select bool %C, int* null, int* %G), |
| 11630 | // but it would not be valid if we transformed it to load from null |
| 11631 | // unconditionally. |
| 11632 | // |
| 11633 | if (SelectInst *SI = dyn_cast<SelectInst>(Op)) { |
| 11634 | // load (select (Cond, &V1, &V2)) --> select(Cond, load &V1, load &V2). |
| 11635 | if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && |
| 11636 | isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { |
| 11637 | Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), |
| 11638 | SI->getOperand(1)->getName()+".val"), LI); |
| 11639 | Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), |
| 11640 | SI->getOperand(2)->getName()+".val"), LI); |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 11641 | return SelectInst::Create(SI->getCondition(), V1, V2); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11642 | } |
| 11643 | |
| 11644 | // load (select (cond, null, P)) -> load P |
| 11645 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(1))) |
| 11646 | if (C->isNullValue()) { |
| 11647 | LI.setOperand(0, SI->getOperand(2)); |
| 11648 | return &LI; |
| 11649 | } |
| 11650 | |
| 11651 | // load (select (cond, P, null)) -> load P |
| 11652 | if (Constant *C = dyn_cast<Constant>(SI->getOperand(2))) |
| 11653 | if (C->isNullValue()) { |
| 11654 | LI.setOperand(0, SI->getOperand(1)); |
| 11655 | return &LI; |
| 11656 | } |
| 11657 | } |
| 11658 | } |
| 11659 | return 0; |
| 11660 | } |
| 11661 | |
| 11662 | /// InstCombineStoreToCast - Fold store V, (cast P) -> store (cast V), P |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11663 | /// when possible. This makes it generally easy to do alias analysis and/or |
| 11664 | /// SROA/mem2reg of the memory object. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11665 | static Instruction *InstCombineStoreToCast(InstCombiner &IC, StoreInst &SI) { |
| 11666 | User *CI = cast<User>(SI.getOperand(1)); |
| 11667 | Value *CastOp = CI->getOperand(0); |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 11668 | LLVMContext *Context = IC.getContext(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11669 | |
| 11670 | const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType(); |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11671 | const PointerType *SrcTy = dyn_cast<PointerType>(CastOp->getType()); |
| 11672 | if (SrcTy == 0) return 0; |
| 11673 | |
| 11674 | const Type *SrcPTy = SrcTy->getElementType(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11675 | |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11676 | if (!DestPTy->isInteger() && !isa<PointerType>(DestPTy)) |
| 11677 | return 0; |
| 11678 | |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11679 | /// NewGEPIndices - If SrcPTy is an aggregate type, we can emit a "noop gep" |
| 11680 | /// to its first element. This allows us to handle things like: |
| 11681 | /// store i32 xxx, (bitcast {foo*, float}* %P to i32*) |
| 11682 | /// on 32-bit hosts. |
| 11683 | SmallVector<Value*, 4> NewGEPIndices; |
| 11684 | |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11685 | // If the source is an array, the code below will not succeed. Check to |
| 11686 | // see if a trivial 'gep P, 0, 0' will help matters. Only do this for |
| 11687 | // constants. |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11688 | if (isa<ArrayType>(SrcPTy) || isa<StructType>(SrcPTy)) { |
| 11689 | // Index through pointer. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11690 | Constant *Zero = Context->getNullValue(Type::Int32Ty); |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11691 | NewGEPIndices.push_back(Zero); |
| 11692 | |
| 11693 | while (1) { |
| 11694 | if (const StructType *STy = dyn_cast<StructType>(SrcPTy)) { |
| edwin | 7dc0aa3 | 2009-01-24 17:16:04 +0000 | [diff] [blame] | 11695 | if (!STy->getNumElements()) /* Struct can be empty {} */ |
| edwin | 07d74e7 | 2009-01-24 11:30:49 +0000 | [diff] [blame] | 11696 | break; |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11697 | NewGEPIndices.push_back(Zero); |
| 11698 | SrcPTy = STy->getElementType(0); |
| 11699 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcPTy)) { |
| 11700 | NewGEPIndices.push_back(Zero); |
| 11701 | SrcPTy = ATy->getElementType(); |
| 11702 | } else { |
| 11703 | break; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11704 | } |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11705 | } |
| 11706 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11707 | SrcTy = Context->getPointerType(SrcPTy, SrcTy->getAddressSpace()); |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11708 | } |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11709 | |
| 11710 | if (!SrcPTy->isInteger() && !isa<PointerType>(SrcPTy)) |
| 11711 | return 0; |
| 11712 | |
| Chris Lattner | c73a0d1 | 2009-01-16 20:12:52 +0000 | [diff] [blame] | 11713 | // If the pointers point into different address spaces or if they point to |
| 11714 | // values with different sizes, we can't do the transformation. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11715 | if (!IC.getTargetData() || |
| 11716 | SrcTy->getAddressSpace() != |
| Chris Lattner | c73a0d1 | 2009-01-16 20:12:52 +0000 | [diff] [blame] | 11717 | cast<PointerType>(CI->getType())->getAddressSpace() || |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11718 | IC.getTargetData()->getTypeSizeInBits(SrcPTy) != |
| 11719 | IC.getTargetData()->getTypeSizeInBits(DestPTy)) |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11720 | return 0; |
| 11721 | |
| 11722 | // Okay, we are casting from one integer or pointer type to another of |
| 11723 | // the same size. Instead of casting the pointer before |
| 11724 | // the store, cast the value to be stored. |
| 11725 | Value *NewCast; |
| 11726 | Value *SIOp0 = SI.getOperand(0); |
| 11727 | Instruction::CastOps opcode = Instruction::BitCast; |
| 11728 | const Type* CastSrcTy = SIOp0->getType(); |
| 11729 | const Type* CastDstTy = SrcPTy; |
| 11730 | if (isa<PointerType>(CastDstTy)) { |
| 11731 | if (CastSrcTy->isInteger()) |
| 11732 | opcode = Instruction::IntToPtr; |
| 11733 | } else if (isa<IntegerType>(CastDstTy)) { |
| 11734 | if (isa<PointerType>(SIOp0->getType())) |
| 11735 | opcode = Instruction::PtrToInt; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11736 | } |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11737 | |
| 11738 | // SIOp0 is a pointer to aggregate and this is a store to the first field, |
| 11739 | // emit a GEP to index into its first field. |
| 11740 | if (!NewGEPIndices.empty()) { |
| 11741 | if (Constant *C = dyn_cast<Constant>(CastOp)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11742 | CastOp = Context->getConstantExprGetElementPtr(C, &NewGEPIndices[0], |
| Chris Lattner | 54dddc7 | 2009-01-24 01:00:13 +0000 | [diff] [blame] | 11743 | NewGEPIndices.size()); |
| 11744 | else |
| 11745 | CastOp = IC.InsertNewInstBefore( |
| 11746 | GetElementPtrInst::Create(CastOp, NewGEPIndices.begin(), |
| 11747 | NewGEPIndices.end()), SI); |
| 11748 | } |
| 11749 | |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11750 | if (Constant *C = dyn_cast<Constant>(SIOp0)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11751 | NewCast = Context->getConstantExprCast(opcode, C, CastDstTy); |
| Chris Lattner | a032c0e | 2009-01-16 20:08:59 +0000 | [diff] [blame] | 11752 | else |
| 11753 | NewCast = IC.InsertNewInstBefore( |
| 11754 | CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"), |
| 11755 | SI); |
| 11756 | return new StoreInst(NewCast, CastOp); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11757 | } |
| 11758 | |
| Chris Lattner | 6fd8c80 | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11759 | /// equivalentAddressValues - Test if A and B will obviously have the same |
| 11760 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 11761 | /// value in code like this: |
| Dan Gohman | 8387bb3 | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 11762 | /// %t0 = getelementptr \@a, 0, 3 |
| Chris Lattner | 6fd8c80 | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11763 | /// store i32 0, i32* %t0 |
| Dan Gohman | 8387bb3 | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 11764 | /// %t1 = getelementptr \@a, 0, 3 |
| Chris Lattner | 6fd8c80 | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11765 | /// %t2 = load i32* %t1 |
| 11766 | /// |
| 11767 | static bool equivalentAddressValues(Value *A, Value *B) { |
| 11768 | // Test if the values are trivially equivalent. |
| 11769 | if (A == B) return true; |
| 11770 | |
| 11771 | // Test if the values come form identical arithmetic instructions. |
| 11772 | if (isa<BinaryOperator>(A) || |
| 11773 | isa<CastInst>(A) || |
| 11774 | isa<PHINode>(A) || |
| 11775 | isa<GetElementPtrInst>(A)) |
| 11776 | if (Instruction *BI = dyn_cast<Instruction>(B)) |
| 11777 | if (cast<Instruction>(A)->isIdenticalTo(BI)) |
| 11778 | return true; |
| 11779 | |
| 11780 | // Otherwise they may not be equivalent. |
| 11781 | return false; |
| 11782 | } |
| 11783 | |
| Dale Johannesen | 2c11fe2 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 11784 | // If this instruction has two uses, one of which is a llvm.dbg.declare, |
| 11785 | // return the llvm.dbg.declare. |
| 11786 | DbgDeclareInst *InstCombiner::hasOneUsePlusDeclare(Value *V) { |
| 11787 | if (!V->hasNUses(2)) |
| 11788 | return 0; |
| 11789 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); |
| 11790 | UI != E; ++UI) { |
| 11791 | if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI)) |
| 11792 | return DI; |
| 11793 | if (isa<BitCastInst>(UI) && UI->hasOneUse()) { |
| 11794 | if (DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(UI->use_begin())) |
| 11795 | return DI; |
| 11796 | } |
| 11797 | } |
| 11798 | return 0; |
| 11799 | } |
| 11800 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11801 | Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { |
| 11802 | Value *Val = SI.getOperand(0); |
| 11803 | Value *Ptr = SI.getOperand(1); |
| 11804 | |
| 11805 | if (isa<UndefValue>(Ptr)) { // store X, undef -> noop (even if volatile) |
| 11806 | EraseInstFromFunction(SI); |
| 11807 | ++NumCombined; |
| 11808 | return 0; |
| 11809 | } |
| 11810 | |
| 11811 | // If the RHS is an alloca with a single use, zapify the store, making the |
| 11812 | // alloca dead. |
| Dale Johannesen | 2c11fe2 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 11813 | // If the RHS is an alloca with a two uses, the other one being a |
| 11814 | // llvm.dbg.declare, zapify the store and the declare, making the |
| 11815 | // alloca dead. We must do this to prevent declare's from affecting |
| 11816 | // codegen. |
| 11817 | if (!SI.isVolatile()) { |
| 11818 | if (Ptr->hasOneUse()) { |
| 11819 | if (isa<AllocaInst>(Ptr)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11820 | EraseInstFromFunction(SI); |
| 11821 | ++NumCombined; |
| 11822 | return 0; |
| 11823 | } |
| Dale Johannesen | 2c11fe2 | 2009-03-03 21:26:39 +0000 | [diff] [blame] | 11824 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { |
| 11825 | if (isa<AllocaInst>(GEP->getOperand(0))) { |
| 11826 | if (GEP->getOperand(0)->hasOneUse()) { |
| 11827 | EraseInstFromFunction(SI); |
| 11828 | ++NumCombined; |
| 11829 | return 0; |
| 11830 | } |
| 11831 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(GEP->getOperand(0))) { |
| 11832 | EraseInstFromFunction(*DI); |
| 11833 | EraseInstFromFunction(SI); |
| 11834 | ++NumCombined; |
| 11835 | return 0; |
| 11836 | } |
| 11837 | } |
| 11838 | } |
| 11839 | } |
| 11840 | if (DbgDeclareInst *DI = hasOneUsePlusDeclare(Ptr)) { |
| 11841 | EraseInstFromFunction(*DI); |
| 11842 | EraseInstFromFunction(SI); |
| 11843 | ++NumCombined; |
| 11844 | return 0; |
| 11845 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11846 | } |
| 11847 | |
| Dan Gohman | 5c4d0e1 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11848 | // Attempt to improve the alignment. |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 11849 | if (TD) { |
| 11850 | unsigned KnownAlign = |
| 11851 | GetOrEnforceKnownAlignment(Ptr, TD->getPrefTypeAlignment(Val->getType())); |
| 11852 | if (KnownAlign > |
| 11853 | (SI.getAlignment() == 0 ? TD->getABITypeAlignment(Val->getType()) : |
| 11854 | SI.getAlignment())) |
| 11855 | SI.setAlignment(KnownAlign); |
| 11856 | } |
| Dan Gohman | 5c4d0e1 | 2007-07-20 16:34:21 +0000 | [diff] [blame] | 11857 | |
| Dale Johannesen | 2bf6a6b | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 11858 | // Do really simple DSE, to catch cases where there are several consecutive |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11859 | // stores to the same location, separated by a few arithmetic operations. This |
| 11860 | // situation often occurs with bitfield accesses. |
| 11861 | BasicBlock::iterator BBI = &SI; |
| 11862 | for (unsigned ScanInsts = 6; BBI != SI.getParent()->begin() && ScanInsts; |
| 11863 | --ScanInsts) { |
| Dale Johannesen | b773a55 | 2009-03-04 01:20:34 +0000 | [diff] [blame] | 11864 | --BBI; |
| Dale Johannesen | c961232 | 2009-03-04 01:53:05 +0000 | [diff] [blame] | 11865 | // Don't count debug info directives, lest they affect codegen, |
| 11866 | // and we skip pointer-to-pointer bitcasts, which are NOPs. |
| 11867 | // It is necessary for correctness to skip those that feed into a |
| 11868 | // llvm.dbg.declare, as these are not present when debugging is off. |
| Dale Johannesen | 605879d | 2009-03-03 22:36:47 +0000 | [diff] [blame] | 11869 | if (isa<DbgInfoIntrinsic>(BBI) || |
| Dale Johannesen | c961232 | 2009-03-04 01:53:05 +0000 | [diff] [blame] | 11870 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) { |
| Dale Johannesen | 2bf6a6b | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 11871 | ScanInsts++; |
| Dale Johannesen | 2bf6a6b | 2009-03-03 01:43:03 +0000 | [diff] [blame] | 11872 | continue; |
| 11873 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11874 | |
| 11875 | if (StoreInst *PrevSI = dyn_cast<StoreInst>(BBI)) { |
| 11876 | // Prev store isn't volatile, and stores to the same location? |
| Chris Lattner | 6fd8c80 | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 11877 | if (!PrevSI->isVolatile() &&equivalentAddressValues(PrevSI->getOperand(1), |
| 11878 | SI.getOperand(1))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11879 | ++NumDeadStore; |
| 11880 | ++BBI; |
| 11881 | EraseInstFromFunction(*PrevSI); |
| 11882 | continue; |
| 11883 | } |
| 11884 | break; |
| 11885 | } |
| 11886 | |
| 11887 | // If this is a load, we have to stop. However, if the loaded value is from |
| 11888 | // the pointer we're loading and is producing the pointer we're storing, |
| 11889 | // then *this* store is dead (X = load P; store X -> P). |
| 11890 | if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) { |
| Dan Gohman | 0ff5a1f | 2008-10-15 23:19:35 +0000 | [diff] [blame] | 11891 | if (LI == Val && equivalentAddressValues(LI->getOperand(0), Ptr) && |
| 11892 | !SI.isVolatile()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11893 | EraseInstFromFunction(SI); |
| 11894 | ++NumCombined; |
| 11895 | return 0; |
| 11896 | } |
| 11897 | // Otherwise, this is a load from some other location. Stores before it |
| 11898 | // may not be dead. |
| 11899 | break; |
| 11900 | } |
| 11901 | |
| 11902 | // Don't skip over loads or things that can modify memory. |
| Chris Lattner | 8450428 | 2008-05-08 17:20:30 +0000 | [diff] [blame] | 11903 | if (BBI->mayWriteToMemory() || BBI->mayReadFromMemory()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11904 | break; |
| 11905 | } |
| 11906 | |
| 11907 | |
| 11908 | if (SI.isVolatile()) return 0; // Don't hack volatile stores. |
| 11909 | |
| 11910 | // store X, null -> turns into 'unreachable' in SimplifyCFG |
| Chris Lattner | 96e0a65 | 2009-06-11 17:54:56 +0000 | [diff] [blame] | 11911 | if (isa<ConstantPointerNull>(Ptr) && |
| 11912 | cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11913 | if (!isa<UndefValue>(Val)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 11914 | SI.setOperand(0, Context->getUndef(Val->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11915 | if (Instruction *U = dyn_cast<Instruction>(Val)) |
| 11916 | AddToWorkList(U); // Dropped a use. |
| 11917 | ++NumCombined; |
| 11918 | } |
| 11919 | return 0; // Do not modify these! |
| 11920 | } |
| 11921 | |
| 11922 | // store undef, Ptr -> noop |
| 11923 | if (isa<UndefValue>(Val)) { |
| 11924 | EraseInstFromFunction(SI); |
| 11925 | ++NumCombined; |
| 11926 | return 0; |
| 11927 | } |
| 11928 | |
| 11929 | // If the pointer destination is a cast, see if we can fold the cast into the |
| 11930 | // source instead. |
| 11931 | if (isa<CastInst>(Ptr)) |
| 11932 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 11933 | return Res; |
| 11934 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 11935 | if (CE->isCast()) |
| 11936 | if (Instruction *Res = InstCombineStoreToCast(*this, SI)) |
| 11937 | return Res; |
| 11938 | |
| 11939 | |
| Dale Johannesen | b7a9e3e | 2009-03-05 02:06:48 +0000 | [diff] [blame] | 11940 | // If this store is the last instruction in the basic block (possibly |
| 11941 | // excepting debug info instructions and the pointer bitcasts that feed |
| 11942 | // into them), and if the block ends with an unconditional branch, try |
| 11943 | // to move it to the successor block. |
| 11944 | BBI = &SI; |
| 11945 | do { |
| 11946 | ++BBI; |
| 11947 | } while (isa<DbgInfoIntrinsic>(BBI) || |
| 11948 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11949 | if (BranchInst *BI = dyn_cast<BranchInst>(BBI)) |
| 11950 | if (BI->isUnconditional()) |
| 11951 | if (SimplifyStoreAtEndOfBlock(SI)) |
| 11952 | return 0; // xform done! |
| 11953 | |
| 11954 | return 0; |
| 11955 | } |
| 11956 | |
| 11957 | /// SimplifyStoreAtEndOfBlock - Turn things like: |
| 11958 | /// if () { *P = v1; } else { *P = v2 } |
| 11959 | /// into a phi node with a store in the successor. |
| 11960 | /// |
| 11961 | /// Simplify things like: |
| 11962 | /// *P = v1; if () { *P = v2; } |
| 11963 | /// into a phi node with a store in the successor. |
| 11964 | /// |
| 11965 | bool InstCombiner::SimplifyStoreAtEndOfBlock(StoreInst &SI) { |
| 11966 | BasicBlock *StoreBB = SI.getParent(); |
| 11967 | |
| 11968 | // Check to see if the successor block has exactly two incoming edges. If |
| 11969 | // so, see if the other predecessor contains a store to the same location. |
| 11970 | // if so, insert a PHI node (if needed) and move the stores down. |
| 11971 | BasicBlock *DestBB = StoreBB->getTerminator()->getSuccessor(0); |
| 11972 | |
| 11973 | // Determine whether Dest has exactly two predecessors and, if so, compute |
| 11974 | // the other predecessor. |
| 11975 | pred_iterator PI = pred_begin(DestBB); |
| 11976 | BasicBlock *OtherBB = 0; |
| 11977 | if (*PI != StoreBB) |
| 11978 | OtherBB = *PI; |
| 11979 | ++PI; |
| 11980 | if (PI == pred_end(DestBB)) |
| 11981 | return false; |
| 11982 | |
| 11983 | if (*PI != StoreBB) { |
| 11984 | if (OtherBB) |
| 11985 | return false; |
| 11986 | OtherBB = *PI; |
| 11987 | } |
| 11988 | if (++PI != pred_end(DestBB)) |
| 11989 | return false; |
| Eli Friedman | ab39f9a | 2008-06-13 21:17:49 +0000 | [diff] [blame] | 11990 | |
| 11991 | // Bail out if all the relevant blocks aren't distinct (this can happen, |
| 11992 | // for example, if SI is in an infinite loop) |
| 11993 | if (StoreBB == DestBB || OtherBB == DestBB) |
| 11994 | return false; |
| 11995 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 11996 | // Verify that the other block ends in a branch and is not otherwise empty. |
| 11997 | BasicBlock::iterator BBI = OtherBB->getTerminator(); |
| 11998 | BranchInst *OtherBr = dyn_cast<BranchInst>(BBI); |
| 11999 | if (!OtherBr || BBI == OtherBB->begin()) |
| 12000 | return false; |
| 12001 | |
| 12002 | // If the other block ends in an unconditional branch, check for the 'if then |
| 12003 | // else' case. there is an instruction before the branch. |
| 12004 | StoreInst *OtherStore = 0; |
| 12005 | if (OtherBr->isUnconditional()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12006 | --BBI; |
| Dale Johannesen | b7a9e3e | 2009-03-05 02:06:48 +0000 | [diff] [blame] | 12007 | // Skip over debugging info. |
| 12008 | while (isa<DbgInfoIntrinsic>(BBI) || |
| 12009 | (isa<BitCastInst>(BBI) && isa<PointerType>(BBI->getType()))) { |
| 12010 | if (BBI==OtherBB->begin()) |
| 12011 | return false; |
| 12012 | --BBI; |
| 12013 | } |
| 12014 | // If this isn't a store, or isn't a store to the same location, bail out. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12015 | OtherStore = dyn_cast<StoreInst>(BBI); |
| 12016 | if (!OtherStore || OtherStore->getOperand(1) != SI.getOperand(1)) |
| 12017 | return false; |
| 12018 | } else { |
| 12019 | // Otherwise, the other block ended with a conditional branch. If one of the |
| 12020 | // destinations is StoreBB, then we have the if/then case. |
| 12021 | if (OtherBr->getSuccessor(0) != StoreBB && |
| 12022 | OtherBr->getSuccessor(1) != StoreBB) |
| 12023 | return false; |
| 12024 | |
| 12025 | // Okay, we know that OtherBr now goes to Dest and StoreBB, so this is an |
| 12026 | // if/then triangle. See if there is a store to the same ptr as SI that |
| 12027 | // lives in OtherBB. |
| 12028 | for (;; --BBI) { |
| 12029 | // Check to see if we find the matching store. |
| 12030 | if ((OtherStore = dyn_cast<StoreInst>(BBI))) { |
| 12031 | if (OtherStore->getOperand(1) != SI.getOperand(1)) |
| 12032 | return false; |
| 12033 | break; |
| 12034 | } |
| Eli Friedman | 3a311d5 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 12035 | // If we find something that may be using or overwriting the stored |
| 12036 | // value, or if we run out of instructions, we can't do the xform. |
| 12037 | if (BBI->mayReadFromMemory() || BBI->mayWriteToMemory() || |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12038 | BBI == OtherBB->begin()) |
| 12039 | return false; |
| 12040 | } |
| 12041 | |
| 12042 | // In order to eliminate the store in OtherBr, we have to |
| Eli Friedman | 3a311d5 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 12043 | // make sure nothing reads or overwrites the stored value in |
| 12044 | // StoreBB. |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12045 | for (BasicBlock::iterator I = StoreBB->begin(); &*I != &SI; ++I) { |
| 12046 | // FIXME: This should really be AA driven. |
| Eli Friedman | 3a311d5 | 2008-06-13 22:02:12 +0000 | [diff] [blame] | 12047 | if (I->mayReadFromMemory() || I->mayWriteToMemory()) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12048 | return false; |
| 12049 | } |
| 12050 | } |
| 12051 | |
| 12052 | // Insert a PHI node now if we need it. |
| 12053 | Value *MergedVal = OtherStore->getOperand(0); |
| 12054 | if (MergedVal != SI.getOperand(0)) { |
| Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 12055 | PHINode *PN = PHINode::Create(MergedVal->getType(), "storemerge"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12056 | PN->reserveOperandSpace(2); |
| 12057 | PN->addIncoming(SI.getOperand(0), SI.getParent()); |
| 12058 | PN->addIncoming(OtherStore->getOperand(0), OtherBB); |
| 12059 | MergedVal = InsertNewInstBefore(PN, DestBB->front()); |
| 12060 | } |
| 12061 | |
| 12062 | // Advance to a place where it is safe to insert the new store and |
| 12063 | // insert it. |
| Dan Gohman | 514277c | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 12064 | BBI = DestBB->getFirstNonPHI(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12065 | InsertNewInstBefore(new StoreInst(MergedVal, SI.getOperand(1), |
| 12066 | OtherStore->isVolatile()), *BBI); |
| 12067 | |
| 12068 | // Nuke the old stores. |
| 12069 | EraseInstFromFunction(SI); |
| 12070 | EraseInstFromFunction(*OtherStore); |
| 12071 | ++NumCombined; |
| 12072 | return true; |
| 12073 | } |
| 12074 | |
| 12075 | |
| 12076 | Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { |
| 12077 | // Change br (not X), label True, label False to: br X, label False, True |
| 12078 | Value *X = 0; |
| 12079 | BasicBlock *TrueDest; |
| 12080 | BasicBlock *FalseDest; |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 12081 | if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest), *Context) && |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12082 | !isa<Constant>(X)) { |
| 12083 | // Swap Destinations and condition... |
| 12084 | BI.setCondition(X); |
| 12085 | BI.setSuccessor(0, FalseDest); |
| 12086 | BI.setSuccessor(1, TrueDest); |
| 12087 | return &BI; |
| 12088 | } |
| 12089 | |
| 12090 | // Cannonicalize fcmp_one -> fcmp_oeq |
| 12091 | FCmpInst::Predicate FPred; Value *Y; |
| 12092 | if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 12093 | TrueDest, FalseDest), *Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12094 | if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || |
| 12095 | FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { |
| 12096 | FCmpInst *I = cast<FCmpInst>(BI.getCondition()); |
| 12097 | FCmpInst::Predicate NewPred = FCmpInst::getInversePredicate(FPred); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 12098 | Instruction *NewSCC = new FCmpInst(I, NewPred, X, Y, ""); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12099 | NewSCC->takeName(I); |
| 12100 | // Swap Destinations and condition... |
| 12101 | BI.setCondition(NewSCC); |
| 12102 | BI.setSuccessor(0, FalseDest); |
| 12103 | BI.setSuccessor(1, TrueDest); |
| 12104 | RemoveFromWorkList(I); |
| 12105 | I->eraseFromParent(); |
| 12106 | AddToWorkList(NewSCC); |
| 12107 | return &BI; |
| 12108 | } |
| 12109 | |
| 12110 | // Cannonicalize icmp_ne -> icmp_eq |
| 12111 | ICmpInst::Predicate IPred; |
| 12112 | if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), |
| Owen Anderson | a21eb58 | 2009-07-10 17:35:01 +0000 | [diff] [blame] | 12113 | TrueDest, FalseDest), *Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12114 | if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || |
| 12115 | IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || |
| 12116 | IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { |
| 12117 | ICmpInst *I = cast<ICmpInst>(BI.getCondition()); |
| 12118 | ICmpInst::Predicate NewPred = ICmpInst::getInversePredicate(IPred); |
| Owen Anderson | 6601fcd | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 12119 | Instruction *NewSCC = new ICmpInst(I, NewPred, X, Y, ""); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12120 | NewSCC->takeName(I); |
| 12121 | // Swap Destinations and condition... |
| 12122 | BI.setCondition(NewSCC); |
| 12123 | BI.setSuccessor(0, FalseDest); |
| 12124 | BI.setSuccessor(1, TrueDest); |
| 12125 | RemoveFromWorkList(I); |
| 12126 | I->eraseFromParent();; |
| 12127 | AddToWorkList(NewSCC); |
| 12128 | return &BI; |
| 12129 | } |
| 12130 | |
| 12131 | return 0; |
| 12132 | } |
| 12133 | |
| 12134 | Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { |
| 12135 | Value *Cond = SI.getCondition(); |
| 12136 | if (Instruction *I = dyn_cast<Instruction>(Cond)) { |
| 12137 | if (I->getOpcode() == Instruction::Add) |
| 12138 | if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 12139 | // change 'switch (X+4) case 1:' into 'switch (X) case -3' |
| 12140 | for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12141 | SI.setOperand(i, |
| 12142 | Context->getConstantExprSub(cast<Constant>(SI.getOperand(i)), |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12143 | AddRHS)); |
| 12144 | SI.setOperand(0, I->getOperand(0)); |
| 12145 | AddToWorkList(I); |
| 12146 | return &SI; |
| 12147 | } |
| 12148 | } |
| 12149 | return 0; |
| 12150 | } |
| 12151 | |
| Matthijs Kooijman | da9ef70 | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 12152 | Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) { |
| Matthijs Kooijman | 45e8eb4 | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 12153 | Value *Agg = EV.getAggregateOperand(); |
| Matthijs Kooijman | da9ef70 | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 12154 | |
| Matthijs Kooijman | 45e8eb4 | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 12155 | if (!EV.hasIndices()) |
| 12156 | return ReplaceInstUsesWith(EV, Agg); |
| 12157 | |
| 12158 | if (Constant *C = dyn_cast<Constant>(Agg)) { |
| 12159 | if (isa<UndefValue>(C)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12160 | return ReplaceInstUsesWith(EV, Context->getUndef(EV.getType())); |
| Matthijs Kooijman | 45e8eb4 | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 12161 | |
| 12162 | if (isa<ConstantAggregateZero>(C)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12163 | return ReplaceInstUsesWith(EV, Context->getNullValue(EV.getType())); |
| Matthijs Kooijman | 45e8eb4 | 2008-07-16 12:55:45 +0000 | [diff] [blame] | 12164 | |
| 12165 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { |
| 12166 | // Extract the element indexed by the first index out of the constant |
| 12167 | Value *V = C->getOperand(*EV.idx_begin()); |
| 12168 | if (EV.getNumIndices() > 1) |
| 12169 | // Extract the remaining indices out of the constant indexed by the |
| 12170 | // first index |
| 12171 | return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end()); |
| 12172 | else |
| 12173 | return ReplaceInstUsesWith(EV, V); |
| 12174 | } |
| 12175 | return 0; // Can't handle other constants |
| 12176 | } |
| 12177 | if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) { |
| 12178 | // We're extracting from an insertvalue instruction, compare the indices |
| 12179 | const unsigned *exti, *exte, *insi, *inse; |
| 12180 | for (exti = EV.idx_begin(), insi = IV->idx_begin(), |
| 12181 | exte = EV.idx_end(), inse = IV->idx_end(); |
| 12182 | exti != exte && insi != inse; |
| 12183 | ++exti, ++insi) { |
| 12184 | if (*insi != *exti) |
| 12185 | // The insert and extract both reference distinctly different elements. |
| 12186 | // This means the extract is not influenced by the insert, and we can |
| 12187 | // replace the aggregate operand of the extract with the aggregate |
| 12188 | // operand of the insert. i.e., replace |
| 12189 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 12190 | // %E = extractvalue { i32, { i32 } } %I, 0 |
| 12191 | // with |
| 12192 | // %E = extractvalue { i32, { i32 } } %A, 0 |
| 12193 | return ExtractValueInst::Create(IV->getAggregateOperand(), |
| 12194 | EV.idx_begin(), EV.idx_end()); |
| 12195 | } |
| 12196 | if (exti == exte && insi == inse) |
| 12197 | // Both iterators are at the end: Index lists are identical. Replace |
| 12198 | // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 12199 | // %C = extractvalue { i32, { i32 } } %B, 1, 0 |
| 12200 | // with "i32 42" |
| 12201 | return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand()); |
| 12202 | if (exti == exte) { |
| 12203 | // The extract list is a prefix of the insert list. i.e. replace |
| 12204 | // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0 |
| 12205 | // %E = extractvalue { i32, { i32 } } %I, 1 |
| 12206 | // with |
| 12207 | // %X = extractvalue { i32, { i32 } } %A, 1 |
| 12208 | // %E = insertvalue { i32 } %X, i32 42, 0 |
| 12209 | // by switching the order of the insert and extract (though the |
| 12210 | // insertvalue should be left in, since it may have other uses). |
| 12211 | Value *NewEV = InsertNewInstBefore( |
| 12212 | ExtractValueInst::Create(IV->getAggregateOperand(), |
| 12213 | EV.idx_begin(), EV.idx_end()), |
| 12214 | EV); |
| 12215 | return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(), |
| 12216 | insi, inse); |
| 12217 | } |
| 12218 | if (insi == inse) |
| 12219 | // The insert list is a prefix of the extract list |
| 12220 | // We can simply remove the common indices from the extract and make it |
| 12221 | // operate on the inserted value instead of the insertvalue result. |
| 12222 | // i.e., replace |
| 12223 | // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1 |
| 12224 | // %E = extractvalue { i32, { i32 } } %I, 1, 0 |
| 12225 | // with |
| 12226 | // %E extractvalue { i32 } { i32 42 }, 0 |
| 12227 | return ExtractValueInst::Create(IV->getInsertedValueOperand(), |
| 12228 | exti, exte); |
| 12229 | } |
| 12230 | // Can't simplify extracts from other values. Note that nested extracts are |
| 12231 | // already simplified implicitely by the above (extract ( extract (insert) ) |
| 12232 | // will be translated into extract ( insert ( extract ) ) first and then just |
| 12233 | // the value inserted, if appropriate). |
| Matthijs Kooijman | da9ef70 | 2008-06-11 14:05:05 +0000 | [diff] [blame] | 12234 | return 0; |
| 12235 | } |
| 12236 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12237 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 12238 | /// is to leave as a vector operation. |
| 12239 | static bool CheapToScalarize(Value *V, bool isConstant) { |
| 12240 | if (isa<ConstantAggregateZero>(V)) |
| 12241 | return true; |
| 12242 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
| 12243 | if (isConstant) return true; |
| 12244 | // If all elts are the same, we can extract. |
| 12245 | Constant *Op0 = C->getOperand(0); |
| 12246 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 12247 | if (C->getOperand(i) != Op0) |
| 12248 | return false; |
| 12249 | return true; |
| 12250 | } |
| 12251 | Instruction *I = dyn_cast<Instruction>(V); |
| 12252 | if (!I) return false; |
| 12253 | |
| 12254 | // Insert element gets simplified to the inserted element or is deleted if |
| 12255 | // this is constant idx extract element and its a constant idx insertelt. |
| 12256 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 12257 | isa<ConstantInt>(I->getOperand(2))) |
| 12258 | return true; |
| 12259 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 12260 | return true; |
| 12261 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 12262 | if (BO->hasOneUse() && |
| 12263 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 12264 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 12265 | return true; |
| 12266 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 12267 | if (CI->hasOneUse() && |
| 12268 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 12269 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 12270 | return true; |
| 12271 | |
| 12272 | return false; |
| 12273 | } |
| 12274 | |
| 12275 | /// Read and decode a shufflevector mask. |
| 12276 | /// |
| 12277 | /// It turns undef elements into values that are larger than the number of |
| 12278 | /// elements in the input. |
| 12279 | static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) { |
| 12280 | unsigned NElts = SVI->getType()->getNumElements(); |
| 12281 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
| 12282 | return std::vector<unsigned>(NElts, 0); |
| 12283 | if (isa<UndefValue>(SVI->getOperand(2))) |
| 12284 | return std::vector<unsigned>(NElts, 2*NElts); |
| 12285 | |
| 12286 | std::vector<unsigned> Result; |
| 12287 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 12288 | for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) |
| 12289 | if (isa<UndefValue>(*i)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12290 | Result.push_back(NElts*2); // undef -> 8 |
| 12291 | else |
| Gabor Greif | 1739600 | 2008-06-12 21:37:33 +0000 | [diff] [blame] | 12292 | Result.push_back(cast<ConstantInt>(*i)->getZExtValue()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12293 | return Result; |
| 12294 | } |
| 12295 | |
| 12296 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 12297 | /// value is already around as a register, for example if it were inserted then |
| 12298 | /// extracted from the vector. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12299 | static Value *FindScalarElement(Value *V, unsigned EltNo, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 12300 | LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12301 | assert(isa<VectorType>(V->getType()) && "Not looking at a vector?"); |
| 12302 | const VectorType *PTy = cast<VectorType>(V->getType()); |
| 12303 | unsigned Width = PTy->getNumElements(); |
| 12304 | if (EltNo >= Width) // Out of range access. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12305 | return Context->getUndef(PTy->getElementType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12306 | |
| 12307 | if (isa<UndefValue>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12308 | return Context->getUndef(PTy->getElementType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12309 | else if (isa<ConstantAggregateZero>(V)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12310 | return Context->getNullValue(PTy->getElementType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12311 | else if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
| 12312 | return CP->getOperand(EltNo); |
| 12313 | else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
| 12314 | // If this is an insert to a variable element, we don't know what it is. |
| 12315 | if (!isa<ConstantInt>(III->getOperand(2))) |
| 12316 | return 0; |
| 12317 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
| 12318 | |
| 12319 | // If this is an insert to the element we are looking for, return the |
| 12320 | // inserted value. |
| 12321 | if (EltNo == IIElt) |
| 12322 | return III->getOperand(1); |
| 12323 | |
| 12324 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 12325 | // vector input. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12326 | return FindScalarElement(III->getOperand(0), EltNo, Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12327 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12328 | unsigned LHSWidth = |
| 12329 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12330 | unsigned InEl = getShuffleMask(SVI)[EltNo]; |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12331 | if (InEl < LHSWidth) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12332 | return FindScalarElement(SVI->getOperand(0), InEl, Context); |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12333 | else if (InEl < LHSWidth*2) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12334 | return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth, Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12335 | else |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12336 | return Context->getUndef(PTy->getElementType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12337 | } |
| 12338 | |
| 12339 | // Otherwise, we don't know. |
| 12340 | return 0; |
| 12341 | } |
| 12342 | |
| 12343 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12344 | // If vector val is undef, replace extract with scalar undef. |
| 12345 | if (isa<UndefValue>(EI.getOperand(0))) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12346 | return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12347 | |
| 12348 | // If vector val is constant 0, replace extract with scalar 0. |
| 12349 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12350 | return ReplaceInstUsesWith(EI, Context->getNullValue(EI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12351 | |
| 12352 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
| Matthijs Kooijman | dd3425f | 2008-06-11 09:00:12 +0000 | [diff] [blame] | 12353 | // If vector val is constant with all elements the same, replace EI with |
| 12354 | // that element. When the elements are not identical, we cannot replace yet |
| 12355 | // (we do that below, but only when the index is constant). |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12356 | Constant *op0 = C->getOperand(0); |
| 12357 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 12358 | if (C->getOperand(i) != op0) { |
| 12359 | op0 = 0; |
| 12360 | break; |
| 12361 | } |
| 12362 | if (op0) |
| 12363 | return ReplaceInstUsesWith(EI, op0); |
| 12364 | } |
| Eli Friedman | f34209b | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 12365 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12366 | // If extracting a specified index from the vector, see if we can recursively |
| 12367 | // find a previously computed scalar that was inserted into the vector. |
| 12368 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 12369 | unsigned IndexVal = IdxC->getZExtValue(); |
| Eli Friedman | f34209b | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 12370 | unsigned VectorWidth = |
| 12371 | cast<VectorType>(EI.getOperand(0)->getType())->getNumElements(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12372 | |
| 12373 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 12374 | // crashing the code below. |
| 12375 | if (IndexVal >= VectorWidth) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12376 | return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12377 | |
| 12378 | // This instruction only demands the single element from the input vector. |
| 12379 | // If the input vector has a single use, simplify it based on this use |
| 12380 | // property. |
| Eli Friedman | f34209b | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 12381 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 12382 | APInt UndefElts(VectorWidth, 0); |
| 12383 | APInt DemandedMask(VectorWidth, 1 << IndexVal); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12384 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 12385 | DemandedMask, UndefElts)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12386 | EI.setOperand(0, V); |
| 12387 | return &EI; |
| 12388 | } |
| 12389 | } |
| 12390 | |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12391 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal, Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12392 | return ReplaceInstUsesWith(EI, Elt); |
| 12393 | |
| 12394 | // If the this extractelement is directly using a bitcast from a vector of |
| 12395 | // the same number of elements, see if we can find the source element from |
| 12396 | // it. In this case, we will end up needing to bitcast the scalars. |
| 12397 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
| 12398 | if (const VectorType *VT = |
| 12399 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 12400 | if (VT->getNumElements() == VectorWidth) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12401 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), |
| 12402 | IndexVal, Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12403 | return new BitCastInst(Elt, EI.getType()); |
| 12404 | } |
| 12405 | } |
| 12406 | |
| 12407 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
| 12408 | if (I->hasOneUse()) { |
| 12409 | // Push extractelement into predecessor operation if legal and |
| 12410 | // profitable to do so |
| 12411 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 12412 | bool isConstantElt = isa<ConstantInt>(EI.getOperand(1)); |
| 12413 | if (CheapToScalarize(BO, isConstantElt)) { |
| 12414 | ExtractElementInst *newEI0 = |
| 12415 | new ExtractElementInst(BO->getOperand(0), EI.getOperand(1), |
| 12416 | EI.getName()+".lhs"); |
| 12417 | ExtractElementInst *newEI1 = |
| 12418 | new ExtractElementInst(BO->getOperand(1), EI.getOperand(1), |
| 12419 | EI.getName()+".rhs"); |
| 12420 | InsertNewInstBefore(newEI0, EI); |
| 12421 | InsertNewInstBefore(newEI1, EI); |
| Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 12422 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12423 | } |
| 12424 | } else if (isa<LoadInst>(I)) { |
| Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 12425 | unsigned AS = |
| 12426 | cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace(); |
| Chris Lattner | 13c2d6e | 2008-01-13 22:23:22 +0000 | [diff] [blame] | 12427 | Value *Ptr = InsertBitCastBefore(I->getOperand(0), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12428 | Context->getPointerType(EI.getType(), AS),EI); |
| Gabor Greif | b91ea9d | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 12429 | GetElementPtrInst *GEP = |
| 12430 | GetElementPtrInst::Create(Ptr, EI.getOperand(1), I->getName()+".gep"); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12431 | InsertNewInstBefore(GEP, EI); |
| 12432 | return new LoadInst(GEP); |
| 12433 | } |
| 12434 | } |
| 12435 | if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 12436 | // Extracting the inserted element? |
| 12437 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 12438 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 12439 | // If the inserted and extracted elements are constants, they must not |
| 12440 | // be the same value, extract from the pre-inserted value instead. |
| 12441 | if (isa<Constant>(IE->getOperand(2)) && |
| 12442 | isa<Constant>(EI.getOperand(1))) { |
| 12443 | AddUsesToWorkList(EI); |
| 12444 | EI.setOperand(0, IE->getOperand(0)); |
| 12445 | return &EI; |
| 12446 | } |
| 12447 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 12448 | // If this is extracting an element from a shufflevector, figure out where |
| 12449 | // it came from and extract from the appropriate input element instead. |
| 12450 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 12451 | unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
| 12452 | Value *Src; |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12453 | unsigned LHSWidth = |
| 12454 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
| 12455 | |
| 12456 | if (SrcIdx < LHSWidth) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12457 | Src = SVI->getOperand(0); |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12458 | else if (SrcIdx < LHSWidth*2) { |
| 12459 | SrcIdx -= LHSWidth; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12460 | Src = SVI->getOperand(1); |
| 12461 | } else { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12462 | return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12463 | } |
| Owen Anderson | 9f5b2aa | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 12464 | return new ExtractElementInst(Src, |
| 12465 | Context->getConstantInt(Type::Int32Ty, SrcIdx, false)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12466 | } |
| 12467 | } |
| Eli Friedman | 1d31dee | 2009-07-18 23:06:53 +0000 | [diff] [blame] | 12468 | // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12469 | } |
| 12470 | return 0; |
| 12471 | } |
| 12472 | |
| 12473 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 12474 | /// elements from either LHS or RHS, return the shuffle mask and true. |
| 12475 | /// Otherwise, return false. |
| 12476 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12477 | std::vector<Constant*> &Mask, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 12478 | LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12479 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 12480 | "Invalid CollectSingleShuffleElements"); |
| 12481 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
| 12482 | |
| 12483 | if (isa<UndefValue>(V)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12484 | Mask.assign(NumElts, Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12485 | return true; |
| 12486 | } else if (V == LHS) { |
| 12487 | for (unsigned i = 0; i != NumElts; ++i) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12488 | Mask.push_back(Context->getConstantInt(Type::Int32Ty, i)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12489 | return true; |
| 12490 | } else if (V == RHS) { |
| 12491 | for (unsigned i = 0; i != NumElts; ++i) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12492 | Mask.push_back(Context->getConstantInt(Type::Int32Ty, i+NumElts)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12493 | return true; |
| 12494 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 12495 | // If this is an insert of an extract from some other vector, include it. |
| 12496 | Value *VecOp = IEI->getOperand(0); |
| 12497 | Value *ScalarOp = IEI->getOperand(1); |
| 12498 | Value *IdxOp = IEI->getOperand(2); |
| 12499 | |
| 12500 | if (!isa<ConstantInt>(IdxOp)) |
| 12501 | return false; |
| 12502 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
| 12503 | |
| 12504 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 12505 | // Okay, we can handle this if the vector we are insertinting into is |
| 12506 | // transitively ok. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12507 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12508 | // If so, update the mask to reflect the inserted undef. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12509 | Mask[InsertedIdx] = Context->getUndef(Type::Int32Ty); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12510 | return true; |
| 12511 | } |
| 12512 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 12513 | if (isa<ConstantInt>(EI->getOperand(1)) && |
| 12514 | EI->getOperand(0)->getType() == V->getType()) { |
| 12515 | unsigned ExtractedIdx = |
| 12516 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 12517 | |
| 12518 | // This must be extracting from either LHS or RHS. |
| 12519 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 12520 | // Okay, we can handle this if the vector we are insertinting into is |
| 12521 | // transitively ok. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12522 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask, Context)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12523 | // If so, update the mask to reflect the inserted value. |
| 12524 | if (EI->getOperand(0) == LHS) { |
| Mon P Wang | 6bf3c59 | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12525 | Mask[InsertedIdx % NumElts] = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12526 | Context->getConstantInt(Type::Int32Ty, ExtractedIdx); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12527 | } else { |
| 12528 | assert(EI->getOperand(0) == RHS); |
| Mon P Wang | 6bf3c59 | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12529 | Mask[InsertedIdx % NumElts] = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12530 | Context->getConstantInt(Type::Int32Ty, ExtractedIdx+NumElts); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12531 | |
| 12532 | } |
| 12533 | return true; |
| 12534 | } |
| 12535 | } |
| 12536 | } |
| 12537 | } |
| 12538 | } |
| 12539 | // TODO: Handle shufflevector here! |
| 12540 | |
| 12541 | return false; |
| 12542 | } |
| 12543 | |
| 12544 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 12545 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 12546 | /// that computes V and the LHS value of the shuffle. |
| 12547 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
| Owen Anderson | 5349f05 | 2009-07-06 23:00:19 +0000 | [diff] [blame] | 12548 | Value *&RHS, LLVMContext *Context) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12549 | assert(isa<VectorType>(V->getType()) && |
| 12550 | (RHS == 0 || V->getType() == RHS->getType()) && |
| 12551 | "Invalid shuffle!"); |
| 12552 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
| 12553 | |
| 12554 | if (isa<UndefValue>(V)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12555 | Mask.assign(NumElts, Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12556 | return V; |
| 12557 | } else if (isa<ConstantAggregateZero>(V)) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12558 | Mask.assign(NumElts, Context->getConstantInt(Type::Int32Ty, 0)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12559 | return V; |
| 12560 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 12561 | // If this is an insert of an extract from some other vector, include it. |
| 12562 | Value *VecOp = IEI->getOperand(0); |
| 12563 | Value *ScalarOp = IEI->getOperand(1); |
| 12564 | Value *IdxOp = IEI->getOperand(2); |
| 12565 | |
| 12566 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 12567 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 12568 | EI->getOperand(0)->getType() == V->getType()) { |
| 12569 | unsigned ExtractedIdx = |
| 12570 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 12571 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
| 12572 | |
| 12573 | // Either the extracted from or inserted into vector must be RHSVec, |
| 12574 | // otherwise we'd end up with a shuffle of three inputs. |
| 12575 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 12576 | RHS = EI->getOperand(0); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12577 | Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context); |
| Mon P Wang | 6bf3c59 | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12578 | Mask[InsertedIdx % NumElts] = |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12579 | Context->getConstantInt(Type::Int32Ty, NumElts+ExtractedIdx); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12580 | return V; |
| 12581 | } |
| 12582 | |
| 12583 | if (VecOp == RHS) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12584 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, |
| 12585 | RHS, Context); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12586 | // Everything but the extracted element is replaced with the RHS. |
| 12587 | for (unsigned i = 0; i != NumElts; ++i) { |
| 12588 | if (i != InsertedIdx) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12589 | Mask[i] = Context->getConstantInt(Type::Int32Ty, NumElts+i); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12590 | } |
| 12591 | return V; |
| 12592 | } |
| 12593 | |
| 12594 | // If this insertelement is a chain that comes from exactly these two |
| 12595 | // vectors, return the vector and the effective shuffle. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12596 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask, |
| 12597 | Context)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12598 | return EI->getOperand(0); |
| 12599 | |
| 12600 | } |
| 12601 | } |
| 12602 | } |
| 12603 | // TODO: Handle shufflevector here! |
| 12604 | |
| 12605 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 12606 | for (unsigned i = 0; i != NumElts; ++i) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12607 | Mask.push_back(Context->getConstantInt(Type::Int32Ty, i)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12608 | return V; |
| 12609 | } |
| 12610 | |
| 12611 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 12612 | Value *VecOp = IE.getOperand(0); |
| 12613 | Value *ScalarOp = IE.getOperand(1); |
| 12614 | Value *IdxOp = IE.getOperand(2); |
| 12615 | |
| 12616 | // Inserting an undef or into an undefined place, remove this. |
| 12617 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 12618 | ReplaceInstUsesWith(IE, VecOp); |
| Eli Friedman | f34209b | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 12619 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12620 | // If the inserted element was extracted from some other vector, and if the |
| 12621 | // indexes are constant, try to turn this into a shufflevector operation. |
| 12622 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 12623 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 12624 | EI->getOperand(0)->getType() == IE.getType()) { |
| Eli Friedman | f34209b | 2009-07-18 19:04:16 +0000 | [diff] [blame] | 12625 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12626 | unsigned ExtractedIdx = |
| 12627 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 12628 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
| 12629 | |
| 12630 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 12631 | return ReplaceInstUsesWith(IE, VecOp); |
| 12632 | |
| 12633 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12634 | return ReplaceInstUsesWith(IE, Context->getUndef(IE.getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12635 | |
| 12636 | // If we are extracting a value from a vector, then inserting it right |
| 12637 | // back into the same place, just use the input vector. |
| 12638 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
| 12639 | return ReplaceInstUsesWith(IE, VecOp); |
| 12640 | |
| 12641 | // We could theoretically do this for ANY input. However, doing so could |
| 12642 | // turn chains of insertelement instructions into a chain of shufflevector |
| 12643 | // instructions, and right now we do not merge shufflevectors. As such, |
| 12644 | // only do this in a situation where it is clear that there is benefit. |
| 12645 | if (isa<UndefValue>(VecOp) || isa<ConstantAggregateZero>(VecOp)) { |
| 12646 | // Turn this into shuffle(EIOp0, VecOp, Mask). The result has all of |
| 12647 | // the values of VecOp, except then one read from EIOp0. |
| 12648 | // Build a new shuffle mask. |
| 12649 | std::vector<Constant*> Mask; |
| 12650 | if (isa<UndefValue>(VecOp)) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12651 | Mask.assign(NumVectorElts, Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12652 | else { |
| 12653 | assert(isa<ConstantAggregateZero>(VecOp) && "Unknown thing"); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12654 | Mask.assign(NumVectorElts, Context->getConstantInt(Type::Int32Ty, |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12655 | NumVectorElts)); |
| 12656 | } |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12657 | Mask[InsertedIdx] = |
| 12658 | Context->getConstantInt(Type::Int32Ty, ExtractedIdx); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12659 | return new ShuffleVectorInst(EI->getOperand(0), VecOp, |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12660 | Context->getConstantVector(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12661 | } |
| 12662 | |
| 12663 | // If this insertelement isn't used by some other insertelement, turn it |
| 12664 | // (and any insertelements it points to), into one big shuffle. |
| 12665 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 12666 | std::vector<Constant*> Mask; |
| 12667 | Value *RHS = 0; |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12668 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS, Context); |
| 12669 | if (RHS == 0) RHS = Context->getUndef(LHS->getType()); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12670 | // We now have a shuffle of LHS, RHS, Mask. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12671 | return new ShuffleVectorInst(LHS, RHS, |
| 12672 | Context->getConstantVector(Mask)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12673 | } |
| 12674 | } |
| 12675 | } |
| 12676 | |
| Eli Friedman | befee26 | 2009-06-06 20:08:03 +0000 | [diff] [blame] | 12677 | unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); |
| 12678 | APInt UndefElts(VWidth, 0); |
| 12679 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 12680 | if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) |
| 12681 | return &IE; |
| 12682 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12683 | return 0; |
| 12684 | } |
| 12685 | |
| 12686 | |
| 12687 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 12688 | Value *LHS = SVI.getOperand(0); |
| 12689 | Value *RHS = SVI.getOperand(1); |
| 12690 | std::vector<unsigned> Mask = getShuffleMask(&SVI); |
| 12691 | |
| 12692 | bool MadeChange = false; |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12693 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12694 | // Undefined shuffle mask -> undefined value. |
| 12695 | if (isa<UndefValue>(SVI.getOperand(2))) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12696 | return ReplaceInstUsesWith(SVI, Context->getUndef(SVI.getType())); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 12697 | |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 12698 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); |
| Mon P Wang | bff5d9c | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 12699 | |
| 12700 | if (VWidth != cast<VectorType>(LHS->getType())->getNumElements()) |
| 12701 | return 0; |
| 12702 | |
| Evan Cheng | 63295ab | 2009-02-03 10:05:09 +0000 | [diff] [blame] | 12703 | APInt UndefElts(VWidth, 0); |
| 12704 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
| 12705 | if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
| Dan Gohman | 83b702d | 2008-09-11 22:47:57 +0000 | [diff] [blame] | 12706 | LHS = SVI.getOperand(0); |
| 12707 | RHS = SVI.getOperand(1); |
| Dan Gohman | da93bbe | 2008-09-09 18:11:14 +0000 | [diff] [blame] | 12708 | MadeChange = true; |
| Dan Gohman | 83b702d | 2008-09-11 22:47:57 +0000 | [diff] [blame] | 12709 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12710 | |
| 12711 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 12712 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 12713 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
| 12714 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
| 12715 | // shuffle(undef,undef,mask) -> undef. |
| 12716 | return ReplaceInstUsesWith(SVI, LHS); |
| 12717 | } |
| 12718 | |
| 12719 | // Remap any references to RHS to use LHS. |
| 12720 | std::vector<Constant*> Elts; |
| 12721 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 12722 | if (Mask[i] >= 2*e) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12723 | Elts.push_back(Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12724 | else { |
| 12725 | if ((Mask[i] >= e && isa<UndefValue>(RHS)) || |
| Dan Gohman | bba96b9 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 12726 | (Mask[i] < e && isa<UndefValue>(LHS))) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12727 | Mask[i] = 2*e; // Turn into undef. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12728 | Elts.push_back(Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | bba96b9 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 12729 | } else { |
| Mon P Wang | 6bf3c59 | 2008-08-20 02:23:25 +0000 | [diff] [blame] | 12730 | Mask[i] = Mask[i] % e; // Force to LHS. |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12731 | Elts.push_back(Context->getConstantInt(Type::Int32Ty, Mask[i])); |
| Dan Gohman | bba96b9 | 2008-08-06 18:17:32 +0000 | [diff] [blame] | 12732 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12733 | } |
| 12734 | } |
| 12735 | SVI.setOperand(0, SVI.getOperand(1)); |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12736 | SVI.setOperand(1, Context->getUndef(RHS->getType())); |
| 12737 | SVI.setOperand(2, Context->getConstantVector(Elts)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12738 | LHS = SVI.getOperand(0); |
| 12739 | RHS = SVI.getOperand(1); |
| 12740 | MadeChange = true; |
| 12741 | } |
| 12742 | |
| 12743 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
| 12744 | bool isLHSID = true, isRHSID = true; |
| 12745 | |
| 12746 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 12747 | if (Mask[i] >= e*2) continue; // Ignore undef values. |
| 12748 | // Is this an identity shuffle of the LHS value? |
| 12749 | isLHSID &= (Mask[i] == i); |
| 12750 | |
| 12751 | // Is this an identity shuffle of the RHS value? |
| 12752 | isRHSID &= (Mask[i]-e == i); |
| 12753 | } |
| 12754 | |
| 12755 | // Eliminate identity shuffles. |
| 12756 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 12757 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
| 12758 | |
| 12759 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 12760 | // one without producing an unusual shuffle. Here we are really conservative: |
| 12761 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 12762 | // program, because the code gen may not be smart enough to turn a merged |
| 12763 | // shuffle into two specific shuffles: it may produce worse code. As such, |
| 12764 | // we only merge two shuffles if the result is one of the two input shuffle |
| 12765 | // masks. In this case, merging the shuffles just removes one instruction, |
| 12766 | // which we know is safe. This is good for things like turning: |
| 12767 | // (splat(splat)) -> splat. |
| 12768 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 12769 | if (isa<UndefValue>(RHS)) { |
| 12770 | std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI); |
| 12771 | |
| 12772 | std::vector<unsigned> NewMask; |
| 12773 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) |
| 12774 | if (Mask[i] >= 2*e) |
| 12775 | NewMask.push_back(2*e); |
| 12776 | else |
| 12777 | NewMask.push_back(LHSMask[Mask[i]]); |
| 12778 | |
| 12779 | // If the result mask is equal to the src shuffle or this shuffle mask, do |
| 12780 | // the replacement. |
| 12781 | if (NewMask == LHSMask || NewMask == Mask) { |
| wangmp | 496a76d | 2009-01-26 04:39:00 +0000 | [diff] [blame] | 12782 | unsigned LHSInNElts = |
| 12783 | cast<VectorType>(LHSSVI->getOperand(0)->getType())->getNumElements(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12784 | std::vector<Constant*> Elts; |
| 12785 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
| wangmp | 496a76d | 2009-01-26 04:39:00 +0000 | [diff] [blame] | 12786 | if (NewMask[i] >= LHSInNElts*2) { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12787 | Elts.push_back(Context->getUndef(Type::Int32Ty)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12788 | } else { |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12789 | Elts.push_back(Context->getConstantInt(Type::Int32Ty, NewMask[i])); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12790 | } |
| 12791 | } |
| 12792 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 12793 | LHSSVI->getOperand(1), |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12794 | Context->getConstantVector(Elts)); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12795 | } |
| 12796 | } |
| 12797 | } |
| 12798 | |
| 12799 | return MadeChange ? &SVI : 0; |
| 12800 | } |
| 12801 | |
| 12802 | |
| 12803 | |
| 12804 | |
| 12805 | /// TryToSinkInstruction - Try to move the specified instruction from its |
| 12806 | /// current block into the beginning of DestBlock, which can only happen if it's |
| 12807 | /// safe to move the instruction past all of the instructions between it and the |
| 12808 | /// end of its block. |
| 12809 | static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { |
| 12810 | assert(I->hasOneUse() && "Invariants didn't hold!"); |
| 12811 | |
| 12812 | // Cannot move control-flow-involving, volatile loads, vaarg, etc. |
| Duncan Sands | 2f50083 | 2009-05-06 06:49:50 +0000 | [diff] [blame] | 12813 | if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I)) |
| Chris Lattner | cb19a1c | 2008-05-09 15:07:33 +0000 | [diff] [blame] | 12814 | return false; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12815 | |
| 12816 | // Do not sink alloca instructions out of the entry block. |
| 12817 | if (isa<AllocaInst>(I) && I->getParent() == |
| 12818 | &DestBlock->getParent()->getEntryBlock()) |
| 12819 | return false; |
| 12820 | |
| 12821 | // We can only sink load instructions if there is nothing between the load and |
| 12822 | // the end of block that could change the value. |
| Chris Lattner | 0db40a6 | 2008-05-08 17:37:37 +0000 | [diff] [blame] | 12823 | if (I->mayReadFromMemory()) { |
| 12824 | for (BasicBlock::iterator Scan = I, E = I->getParent()->end(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12825 | Scan != E; ++Scan) |
| 12826 | if (Scan->mayWriteToMemory()) |
| 12827 | return false; |
| 12828 | } |
| 12829 | |
| Dan Gohman | 514277c | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 12830 | BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12831 | |
| Dale Johannesen | 24339f1 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 12832 | CopyPrecedingStopPoint(I, InsertPos); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12833 | I->moveBefore(InsertPos); |
| 12834 | ++NumSunkInst; |
| 12835 | return true; |
| 12836 | } |
| 12837 | |
| 12838 | |
| 12839 | /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding |
| 12840 | /// all reachable code to the worklist. |
| 12841 | /// |
| 12842 | /// This has a couple of tricks to make the code faster and more powerful. In |
| 12843 | /// particular, we constant fold and DCE instructions as we go, to avoid adding |
| 12844 | /// them to the worklist (this significantly speeds up instcombine on code where |
| 12845 | /// many instructions are dead or constant). Additionally, if we find a branch |
| 12846 | /// whose condition is a known constant, we only visit the reachable successors. |
| 12847 | /// |
| 12848 | static void AddReachableCodeToWorklist(BasicBlock *BB, |
| 12849 | SmallPtrSet<BasicBlock*, 64> &Visited, |
| 12850 | InstCombiner &IC, |
| 12851 | const TargetData *TD) { |
| Chris Lattner | a06291a | 2008-08-15 04:03:01 +0000 | [diff] [blame] | 12852 | SmallVector<BasicBlock*, 256> Worklist; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12853 | Worklist.push_back(BB); |
| 12854 | |
| 12855 | while (!Worklist.empty()) { |
| 12856 | BB = Worklist.back(); |
| 12857 | Worklist.pop_back(); |
| 12858 | |
| 12859 | // We have now visited this block! If we've already been here, ignore it. |
| 12860 | if (!Visited.insert(BB)) continue; |
| Devang Patel | 794140c | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 12861 | |
| 12862 | DbgInfoIntrinsic *DBI_Prev = NULL; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12863 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 12864 | Instruction *Inst = BBI++; |
| 12865 | |
| 12866 | // DCE instruction if trivially dead. |
| 12867 | if (isInstructionTriviallyDead(Inst)) { |
| 12868 | ++NumDeadInst; |
| 12869 | DOUT << "IC: DCE: " << *Inst; |
| 12870 | Inst->eraseFromParent(); |
| 12871 | continue; |
| 12872 | } |
| 12873 | |
| 12874 | // ConstantProp instruction if trivially constant. |
| Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 12875 | if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12876 | DOUT << "IC: ConstFold to: " << *C << " from: " << *Inst; |
| 12877 | Inst->replaceAllUsesWith(C); |
| 12878 | ++NumConstProp; |
| 12879 | Inst->eraseFromParent(); |
| 12880 | continue; |
| 12881 | } |
| Chris Lattner | e0f462d | 2007-07-20 22:06:41 +0000 | [diff] [blame] | 12882 | |
| Devang Patel | 794140c | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 12883 | // If there are two consecutive llvm.dbg.stoppoint calls then |
| 12884 | // it is likely that the optimizer deleted code in between these |
| 12885 | // two intrinsics. |
| 12886 | DbgInfoIntrinsic *DBI_Next = dyn_cast<DbgInfoIntrinsic>(Inst); |
| 12887 | if (DBI_Next) { |
| 12888 | if (DBI_Prev |
| 12889 | && DBI_Prev->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint |
| 12890 | && DBI_Next->getIntrinsicID() == llvm::Intrinsic::dbg_stoppoint) { |
| 12891 | IC.RemoveFromWorkList(DBI_Prev); |
| 12892 | DBI_Prev->eraseFromParent(); |
| 12893 | } |
| 12894 | DBI_Prev = DBI_Next; |
| Zhou Sheng | 77e03b9 | 2009-02-23 10:14:11 +0000 | [diff] [blame] | 12895 | } else { |
| 12896 | DBI_Prev = 0; |
| Devang Patel | 794140c | 2008-11-19 18:56:50 +0000 | [diff] [blame] | 12897 | } |
| 12898 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12899 | IC.AddToWorkList(Inst); |
| 12900 | } |
| 12901 | |
| 12902 | // Recursively visit successors. If this is a branch or switch on a |
| 12903 | // constant, only visit the reachable successor. |
| 12904 | TerminatorInst *TI = BB->getTerminator(); |
| 12905 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 12906 | if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) { |
| 12907 | bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue(); |
| Nick Lewycky | d551cf1 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 12908 | BasicBlock *ReachableBB = BI->getSuccessor(!CondVal); |
| Nick Lewycky | d8aa33a | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 12909 | Worklist.push_back(ReachableBB); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12910 | continue; |
| 12911 | } |
| 12912 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 12913 | if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) { |
| 12914 | // See if this is an explicit destination. |
| 12915 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) |
| 12916 | if (SI->getCaseValue(i) == Cond) { |
| Nick Lewycky | d551cf1 | 2008-03-09 08:50:23 +0000 | [diff] [blame] | 12917 | BasicBlock *ReachableBB = SI->getSuccessor(i); |
| Nick Lewycky | d8aa33a | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 12918 | Worklist.push_back(ReachableBB); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12919 | continue; |
| 12920 | } |
| 12921 | |
| 12922 | // Otherwise it is the default destination. |
| 12923 | Worklist.push_back(SI->getSuccessor(0)); |
| 12924 | continue; |
| 12925 | } |
| 12926 | } |
| 12927 | |
| 12928 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 12929 | Worklist.push_back(TI->getSuccessor(i)); |
| 12930 | } |
| 12931 | } |
| 12932 | |
| 12933 | bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) { |
| 12934 | bool Changed = false; |
| Dan Gohman | a80e271 | 2009-07-21 23:21:54 +0000 | [diff] [blame] | 12935 | TD = getAnalysisIfAvailable<TargetData>(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12936 | |
| 12937 | DEBUG(DOUT << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on " |
| 12938 | << F.getNameStr() << "\n"); |
| 12939 | |
| 12940 | { |
| 12941 | // Do a depth-first traversal of the function, populate the worklist with |
| 12942 | // the reachable instructions. Ignore blocks that are not reachable. Keep |
| 12943 | // track of which blocks we visit. |
| 12944 | SmallPtrSet<BasicBlock*, 64> Visited; |
| 12945 | AddReachableCodeToWorklist(F.begin(), Visited, *this, TD); |
| 12946 | |
| 12947 | // Do a quick scan over the function. If we find any blocks that are |
| 12948 | // unreachable, remove any instructions inside of them. This prevents |
| 12949 | // the instcombine code from having to deal with some bad special cases. |
| 12950 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 12951 | if (!Visited.count(BB)) { |
| 12952 | Instruction *Term = BB->getTerminator(); |
| 12953 | while (Term != BB->begin()) { // Remove instrs bottom-up |
| 12954 | BasicBlock::iterator I = Term; --I; |
| 12955 | |
| 12956 | DOUT << "IC: DCE: " << *I; |
| Dale Johannesen | df356c6 | 2009-03-10 21:19:49 +0000 | [diff] [blame] | 12957 | // A debug intrinsic shouldn't force another iteration if we weren't |
| 12958 | // going to do one without it. |
| 12959 | if (!isa<DbgInfoIntrinsic>(I)) { |
| 12960 | ++NumDeadInst; |
| 12961 | Changed = true; |
| 12962 | } |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12963 | if (!I->use_empty()) |
| Owen Anderson | 24be4c1 | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 12964 | I->replaceAllUsesWith(Context->getUndef(I->getType())); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12965 | I->eraseFromParent(); |
| 12966 | } |
| 12967 | } |
| 12968 | } |
| 12969 | |
| 12970 | while (!Worklist.empty()) { |
| 12971 | Instruction *I = RemoveOneFromWorkList(); |
| 12972 | if (I == 0) continue; // skip null values. |
| 12973 | |
| 12974 | // Check to see if we can DCE the instruction. |
| 12975 | if (isInstructionTriviallyDead(I)) { |
| 12976 | // Add operands to the worklist. |
| 12977 | if (I->getNumOperands() < 4) |
| 12978 | AddUsesToWorkList(*I); |
| 12979 | ++NumDeadInst; |
| 12980 | |
| 12981 | DOUT << "IC: DCE: " << *I; |
| 12982 | |
| 12983 | I->eraseFromParent(); |
| 12984 | RemoveFromWorkList(I); |
| Chris Lattner | f6d5886 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 12985 | Changed = true; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12986 | continue; |
| 12987 | } |
| 12988 | |
| 12989 | // Instruction isn't dead, see if we can constant propagate it. |
| Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 12990 | if (Constant *C = ConstantFoldInstruction(I, F.getContext(), TD)) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 12991 | DOUT << "IC: ConstFold to: " << *C << " from: " << *I; |
| 12992 | |
| 12993 | // Add operands to the worklist. |
| 12994 | AddUsesToWorkList(*I); |
| 12995 | ReplaceInstUsesWith(*I, C); |
| 12996 | |
| 12997 | ++NumConstProp; |
| 12998 | I->eraseFromParent(); |
| 12999 | RemoveFromWorkList(I); |
| Chris Lattner | f6d5886 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 13000 | Changed = true; |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13001 | continue; |
| 13002 | } |
| 13003 | |
| Eli Friedman | 5c61918 | 2009-07-15 22:13:34 +0000 | [diff] [blame] | 13004 | if (TD) { |
| Nick Lewycky | adb6792 | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 13005 | // See if we can constant fold its operands. |
| Chris Lattner | f6d5886 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 13006 | for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) |
| 13007 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(i)) |
| Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 13008 | if (Constant *NewC = ConstantFoldConstantExpression(CE, |
| 13009 | F.getContext(), TD)) |
| Chris Lattner | f6d5886 | 2009-01-31 07:04:22 +0000 | [diff] [blame] | 13010 | if (NewC != CE) { |
| 13011 | i->set(NewC); |
| 13012 | Changed = true; |
| 13013 | } |
| Nick Lewycky | adb6792 | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 13014 | } |
| 13015 | |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13016 | // See if we can trivially sink this instruction to a successor basic block. |
| Dan Gohman | 29474e9 | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 13017 | if (I->hasOneUse()) { |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13018 | BasicBlock *BB = I->getParent(); |
| 13019 | BasicBlock *UserParent = cast<Instruction>(I->use_back())->getParent(); |
| 13020 | if (UserParent != BB) { |
| 13021 | bool UserIsSuccessor = false; |
| 13022 | // See if the user is one of our successors. |
| 13023 | for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) |
| 13024 | if (*SI == UserParent) { |
| 13025 | UserIsSuccessor = true; |
| 13026 | break; |
| 13027 | } |
| 13028 | |
| 13029 | // If the user is one of our immediate successors, and if that successor |
| 13030 | // only has us as a predecessors (we'd have to split the critical edge |
| 13031 | // otherwise), we can keep going. |
| 13032 | if (UserIsSuccessor && !isa<PHINode>(I->use_back()) && |
| 13033 | next(pred_begin(UserParent)) == pred_end(UserParent)) |
| 13034 | // Okay, the CFG is simple enough, try to sink this instruction. |
| 13035 | Changed |= TryToSinkInstruction(I, UserParent); |
| 13036 | } |
| 13037 | } |
| 13038 | |
| 13039 | // Now that we have an instruction, try combining it to simplify it... |
| 13040 | #ifndef NDEBUG |
| 13041 | std::string OrigI; |
| 13042 | #endif |
| 13043 | DEBUG(std::ostringstream SS; I->print(SS); OrigI = SS.str();); |
| 13044 | if (Instruction *Result = visit(*I)) { |
| 13045 | ++NumCombined; |
| 13046 | // Should we replace the old instruction with a new one? |
| 13047 | if (Result != I) { |
| 13048 | DOUT << "IC: Old = " << *I |
| 13049 | << " New = " << *Result; |
| 13050 | |
| 13051 | // Everything uses the new instruction now. |
| 13052 | I->replaceAllUsesWith(Result); |
| 13053 | |
| 13054 | // Push the new instruction and any users onto the worklist. |
| 13055 | AddToWorkList(Result); |
| 13056 | AddUsersToWorkList(*Result); |
| 13057 | |
| 13058 | // Move the name to the new instruction first. |
| 13059 | Result->takeName(I); |
| 13060 | |
| 13061 | // Insert the new instruction into the basic block... |
| 13062 | BasicBlock *InstParent = I->getParent(); |
| 13063 | BasicBlock::iterator InsertPos = I; |
| 13064 | |
| 13065 | if (!isa<PHINode>(Result)) // If combining a PHI, don't insert |
| 13066 | while (isa<PHINode>(InsertPos)) // middle of a block of PHIs. |
| 13067 | ++InsertPos; |
| 13068 | |
| 13069 | InstParent->getInstList().insert(InsertPos, Result); |
| 13070 | |
| 13071 | // Make sure that we reprocess all operands now that we reduced their |
| 13072 | // use counts. |
| 13073 | AddUsesToWorkList(*I); |
| 13074 | |
| 13075 | // Instructions can end up on the worklist more than once. Make sure |
| 13076 | // we do not process an instruction that has been deleted. |
| 13077 | RemoveFromWorkList(I); |
| 13078 | |
| 13079 | // Erase the old instruction. |
| 13080 | InstParent->getInstList().erase(I); |
| 13081 | } else { |
| 13082 | #ifndef NDEBUG |
| 13083 | DOUT << "IC: Mod = " << OrigI |
| 13084 | << " New = " << *I; |
| 13085 | #endif |
| 13086 | |
| 13087 | // If the instruction was modified, it's possible that it is now dead. |
| 13088 | // if so, remove it. |
| 13089 | if (isInstructionTriviallyDead(I)) { |
| 13090 | // Make sure we process all operands now that we are reducing their |
| 13091 | // use counts. |
| 13092 | AddUsesToWorkList(*I); |
| 13093 | |
| 13094 | // Instructions may end up in the worklist more than once. Erase all |
| 13095 | // occurrences of this instruction. |
| 13096 | RemoveFromWorkList(I); |
| 13097 | I->eraseFromParent(); |
| 13098 | } else { |
| 13099 | AddToWorkList(I); |
| 13100 | AddUsersToWorkList(*I); |
| 13101 | } |
| 13102 | } |
| 13103 | Changed = true; |
| 13104 | } |
| 13105 | } |
| 13106 | |
| 13107 | assert(WorklistMap.empty() && "Worklist empty, but map not?"); |
| Chris Lattner | b933ea6 | 2007-08-05 08:47:58 +0000 | [diff] [blame] | 13108 | |
| 13109 | // Do an explicit clear, this shrinks the map if needed. |
| 13110 | WorklistMap.clear(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13111 | return Changed; |
| 13112 | } |
| 13113 | |
| 13114 | |
| 13115 | bool InstCombiner::runOnFunction(Function &F) { |
| 13116 | MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID); |
| Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 13117 | Context = &F.getContext(); |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13118 | |
| 13119 | bool EverMadeChange = false; |
| 13120 | |
| 13121 | // Iterate while there is work to do. |
| 13122 | unsigned Iteration = 0; |
| Bill Wendling | d9644a4 | 2008-05-14 22:45:20 +0000 | [diff] [blame] | 13123 | while (DoOneIteration(F, Iteration++)) |
| Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13124 | EverMadeChange = true; |
| 13125 | return EverMadeChange; |
| 13126 | } |
| 13127 | |
| 13128 | FunctionPass *llvm::createInstructionCombiningPass() { |
| 13129 | return new InstCombiner(); |
| 13130 | } |