Matt Arsenault | 52ddb7b | 2013-05-17 21:43:39 +0000 | [diff] [blame] | 1 | //===- InstCombine.h - Main InstCombine pass definition ---------*- C++ -*-===// |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef INSTCOMBINE_INSTCOMBINE_H |
| 11 | #define INSTCOMBINE_INSTCOMBINE_H |
| 12 | |
| 13 | #include "InstCombineWorklist.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/IRBuilder.h" |
| 16 | #include "llvm/IR/IntrinsicInst.h" |
| 17 | #include "llvm/IR/Operator.h" |
Chandler Carruth | dbd6958 | 2012-11-30 03:08:41 +0000 | [diff] [blame] | 18 | #include "llvm/InstVisitor.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 20 | #include "llvm/Support/TargetFolder.h" |
Meador Inge | df796f8 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/SimplifyLibCalls.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | class CallSite; |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 25 | class DataLayout; |
Chad Rosier | e6de63d | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 26 | class TargetLibraryInfo; |
Chris Lattner | 27acfcd | 2010-01-05 05:21:26 +0000 | [diff] [blame] | 27 | class DbgDeclareInst; |
| 28 | class MemIntrinsic; |
| 29 | class MemSetInst; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 31 | /// SelectPatternFlavor - We can match a variety of different patterns for |
| 32 | /// select operations. |
| 33 | enum SelectPatternFlavor { |
| 34 | SPF_UNKNOWN = 0, |
| 35 | SPF_SMIN, SPF_UMIN, |
| 36 | SPF_SMAX, SPF_UMAX |
| 37 | //SPF_ABS - TODO. |
| 38 | }; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 40 | /// getComplexity: Assign a complexity or rank value to LLVM Values... |
| 41 | /// 0 -> undef, 1 -> Const, 2 -> Other, 3 -> Arg, 3 -> Unary, 4 -> OtherInst |
| 42 | static inline unsigned getComplexity(Value *V) { |
| 43 | if (isa<Instruction>(V)) { |
| 44 | if (BinaryOperator::isNeg(V) || |
| 45 | BinaryOperator::isFNeg(V) || |
| 46 | BinaryOperator::isNot(V)) |
| 47 | return 3; |
| 48 | return 4; |
| 49 | } |
| 50 | if (isa<Argument>(V)) return 3; |
| 51 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
| 52 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 53 | |
Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 54 | /// AddOne - Add one to a Constant |
| 55 | static inline Constant *AddOne(Constant *C) { |
| 56 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); |
| 57 | } |
| 58 | /// SubOne - Subtract one from a Constant |
| 59 | static inline Constant *SubOne(Constant *C) { |
| 60 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); |
| 61 | } |
| 62 | |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 64 | /// InstCombineIRInserter - This is an IRBuilder insertion helper that works |
| 65 | /// just like the normal insertion helper, but also adds any new instructions |
| 66 | /// to the instcombine worklist. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 67 | class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 68 | : public IRBuilderDefaultInserter<true> { |
| 69 | InstCombineWorklist &Worklist; |
| 70 | public: |
| 71 | InstCombineIRInserter(InstCombineWorklist &WL) : Worklist(WL) {} |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 73 | void InsertHelper(Instruction *I, const Twine &Name, |
| 74 | BasicBlock *BB, BasicBlock::iterator InsertPt) const { |
| 75 | IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt); |
| 76 | Worklist.Add(I); |
| 77 | } |
| 78 | }; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 80 | /// InstCombiner - The -instcombine pass. |
Duncan Sands | 6c5e435 | 2010-05-11 20:16:09 +0000 | [diff] [blame] | 81 | class LLVM_LIBRARY_VISIBILITY InstCombiner |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 82 | : public FunctionPass, |
| 83 | public InstVisitor<InstCombiner, Instruction*> { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 84 | DataLayout *DL; |
Chad Rosier | e6de63d | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 85 | TargetLibraryInfo *TLI; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 86 | bool MadeIRChange; |
Meador Inge | df796f8 | 2012-10-13 16:45:24 +0000 | [diff] [blame] | 87 | LibCallSimplifier *Simplifier; |
Quentin Colombet | 3b2db0b | 2013-01-07 18:37:41 +0000 | [diff] [blame] | 88 | bool MinimizeSize; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 89 | public: |
| 90 | /// Worklist - All of the instructions that need to be simplified. |
| 91 | InstCombineWorklist Worklist; |
| 92 | |
| 93 | /// Builder - This is an IRBuilder that automatically inserts new |
| 94 | /// instructions into the worklist when they are created. |
| 95 | typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy; |
| 96 | BuilderTy *Builder; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 98 | static char ID; // Pass identification, replacement for typeid |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 99 | InstCombiner() : FunctionPass(ID), DL(0), Builder(0) { |
Quentin Colombet | 3b2db0b | 2013-01-07 18:37:41 +0000 | [diff] [blame] | 100 | MinimizeSize = false; |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 101 | initializeInstCombinerPass(*PassRegistry::getPassRegistry()); |
| 102 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 103 | |
| 104 | public: |
| 105 | virtual bool runOnFunction(Function &F); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 107 | bool DoOneIteration(Function &F, unsigned ItNum); |
| 108 | |
Chris Lattner | 7e04491 | 2010-01-04 07:17:19 +0000 | [diff] [blame] | 109 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Chad Rosier | e6de63d | 2011-12-01 21:29:16 +0000 | [diff] [blame] | 110 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 111 | DataLayout *getDataLayout() const { return DL; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 112 | |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 113 | TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; } |
| 114 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 115 | // Visitation implementation - Implement instruction combining for different |
| 116 | // instruction types. The semantics are as follows: |
| 117 | // Return Value: |
| 118 | // null - No change was made |
| 119 | // I - Change was made, I is still valid, I may be dead though |
| 120 | // otherwise - Change was made, replace I with returned instruction |
| 121 | // |
| 122 | Instruction *visitAdd(BinaryOperator &I); |
| 123 | Instruction *visitFAdd(BinaryOperator &I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 124 | Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 125 | Instruction *visitSub(BinaryOperator &I); |
| 126 | Instruction *visitFSub(BinaryOperator &I); |
| 127 | Instruction *visitMul(BinaryOperator &I); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 128 | Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C, |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 129 | Instruction *InsertBefore); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 130 | Instruction *visitFMul(BinaryOperator &I); |
| 131 | Instruction *visitURem(BinaryOperator &I); |
| 132 | Instruction *visitSRem(BinaryOperator &I); |
| 133 | Instruction *visitFRem(BinaryOperator &I); |
| 134 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
| 135 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 136 | Instruction *commonIRemTransforms(BinaryOperator &I); |
| 137 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 138 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 139 | Instruction *visitUDiv(BinaryOperator &I); |
| 140 | Instruction *visitSDiv(BinaryOperator &I); |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 141 | Instruction *visitFDiv(BinaryOperator &I); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 142 | Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS); |
| 143 | Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 144 | Instruction *visitAnd(BinaryOperator &I); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 145 | Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS); |
| 146 | Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 147 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, |
| 148 | Value *A, Value *B, Value *C); |
| 149 | Instruction *visitOr (BinaryOperator &I); |
| 150 | Instruction *visitXor(BinaryOperator &I); |
| 151 | Instruction *visitShl(BinaryOperator &I); |
| 152 | Instruction *visitAShr(BinaryOperator &I); |
| 153 | Instruction *visitLShr(BinaryOperator &I); |
| 154 | Instruction *commonShiftTransforms(BinaryOperator &I); |
| 155 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
| 156 | Constant *RHSC); |
| 157 | Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, |
| 158 | GlobalVariable *GV, CmpInst &ICI, |
| 159 | ConstantInt *AndCst = 0); |
| 160 | Instruction *visitFCmpInst(FCmpInst &I); |
| 161 | Instruction *visitICmpInst(ICmpInst &I); |
| 162 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
| 163 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 164 | Instruction *LHS, |
| 165 | ConstantInt *RHS); |
| 166 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 167 | ConstantInt *DivRHS); |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 168 | Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 169 | ConstantInt *DivRHS); |
Benjamin Kramer | 0e2d162 | 2013-09-20 22:12:42 +0000 | [diff] [blame] | 170 | Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI, |
| 171 | ICmpInst::Predicate Pred); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 172 | Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 173 | ICmpInst::Predicate Cond, Instruction &I); |
| 174 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
| 175 | BinaryOperator &I); |
| 176 | Instruction *commonCastTransforms(CastInst &CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 177 | Instruction *commonPointerCastTransforms(CastInst &CI); |
| 178 | Instruction *visitTrunc(TruncInst &CI); |
| 179 | Instruction *visitZExt(ZExtInst &CI); |
| 180 | Instruction *visitSExt(SExtInst &CI); |
| 181 | Instruction *visitFPTrunc(FPTruncInst &CI); |
| 182 | Instruction *visitFPExt(CastInst &CI); |
| 183 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 184 | Instruction *visitFPToSI(FPToSIInst &FI); |
| 185 | Instruction *visitUIToFP(CastInst &CI); |
| 186 | Instruction *visitSIToFP(CastInst &CI); |
| 187 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
| 188 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
| 189 | Instruction *visitBitCast(BitCastInst &CI); |
Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 190 | Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 191 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 192 | Instruction *FI); |
| 193 | Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*); |
| 194 | Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, |
| 195 | Value *A, Value *B, Instruction &Outer, |
| 196 | SelectPatternFlavor SPF2, Value *C); |
| 197 | Instruction *visitSelectInst(SelectInst &SI); |
| 198 | Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
| 199 | Instruction *visitCallInst(CallInst &CI); |
| 200 | Instruction *visitInvokeInst(InvokeInst &II); |
| 201 | |
| 202 | Instruction *SliceUpIllegalIntegerPHI(PHINode &PN); |
| 203 | Instruction *visitPHINode(PHINode &PN); |
| 204 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 205 | Instruction *visitAllocaInst(AllocaInst &AI); |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 206 | Instruction *visitAllocSite(Instruction &FI); |
Gabor Greif | 75f6943 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 207 | Instruction *visitFree(CallInst &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 208 | Instruction *visitLoadInst(LoadInst &LI); |
| 209 | Instruction *visitStoreInst(StoreInst &SI); |
| 210 | Instruction *visitBranchInst(BranchInst &BI); |
| 211 | Instruction *visitSwitchInst(SwitchInst &SI); |
| 212 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
| 213 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
| 214 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
| 215 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Duncan Sands | 5c05579 | 2011-09-30 13:12:16 +0000 | [diff] [blame] | 216 | Instruction *visitLandingPadInst(LandingPadInst &LI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 217 | |
| 218 | // visitInstruction - Specify what to return for unhandled instructions... |
| 219 | Instruction *visitInstruction(Instruction &I) { return 0; } |
| 220 | |
| 221 | private: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 222 | bool ShouldChangeType(Type *From, Type *To) const; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 223 | Value *dyn_castNegVal(Value *V) const; |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 224 | Value *dyn_castFNegVal(Value *V, bool NoSignedZero=false) const; |
Matt Arsenault | d79f7d9 | 2013-08-19 22:17:40 +0000 | [diff] [blame] | 225 | Type *FindElementAtOffset(Type *PtrTy, int64_t Offset, |
| 226 | SmallVectorImpl<Value*> &NewIndices); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 227 | Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 4e8137d | 2010-02-11 06:26:33 +0000 | [diff] [blame] | 229 | /// ShouldOptimizeCast - Return true if the cast from "V to Ty" actually |
| 230 | /// results in any code being generated and is interesting to optimize out. If |
| 231 | /// the cast can be eliminated by some other simple transformation, we prefer |
| 232 | /// to do the simplification first. |
| 233 | bool ShouldOptimizeCast(Instruction::CastOps opcode,const Value *V, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 234 | Type *Ty); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 236 | Instruction *visitCallSite(CallSite CS); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 237 | Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 238 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 239 | Instruction *transformCallThroughTrampoline(CallSite CS, |
| 240 | IntrinsicInst *Tramp); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 241 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 242 | bool DoXform = true); |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 243 | Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 244 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 245 | Value *EmitGEPOffset(User *GEP); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 246 | Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 247 | Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 248 | |
| 249 | public: |
| 250 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 251 | // in the program. Add the new instruction to the worklist. |
| 252 | // |
| 253 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
| 254 | assert(New && New->getParent() == 0 && |
| 255 | "New instruction already inserted into a basic block!"); |
| 256 | BasicBlock *BB = Old.getParent(); |
| 257 | BB->getInstList().insert(&Old, New); // Insert inst |
| 258 | Worklist.Add(New); |
| 259 | return New; |
| 260 | } |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 261 | |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 262 | // InsertNewInstWith - same as InsertNewInstBefore, but also sets the |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 263 | // debug loc. |
| 264 | // |
| 265 | Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) { |
| 266 | New->setDebugLoc(Old.getDebugLoc()); |
| 267 | return InsertNewInstBefore(New, Old); |
| 268 | } |
| 269 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 270 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 271 | // found to be dead, replacable with another preexisting expression. Here |
| 272 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 273 | // value, then return I, so that the inst combiner will know that I was |
| 274 | // modified. |
| 275 | // |
| 276 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
| 277 | Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 279 | // If we are replacing the instruction with itself, this must be in a |
| 280 | // segment of unreachable code, so just clobber the instruction. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 281 | if (&I == V) |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 282 | V = UndefValue::get(I.getType()); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 283 | |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 284 | DEBUG(dbgs() << "IC: Replacing " << I << "\n" |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 285 | " with " << *V << '\n'); |
| 286 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 287 | I.replaceAllUsesWith(V); |
| 288 | return &I; |
| 289 | } |
| 290 | |
| 291 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 292 | // effects or produces a void value, we can't rely on DCE to delete the |
| 293 | // instruction. Instead, visit methods should return the value returned by |
| 294 | // this function. |
| 295 | Instruction *EraseInstFromFunction(Instruction &I) { |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 296 | DEBUG(dbgs() << "IC: ERASE " << I << '\n'); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 297 | |
| 298 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 299 | // Make sure that we reprocess all operands now that we reduced their |
| 300 | // use counts. |
| 301 | if (I.getNumOperands() < 8) { |
| 302 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 303 | if (Instruction *Op = dyn_cast<Instruction>(*i)) |
| 304 | Worklist.Add(Op); |
| 305 | } |
| 306 | Worklist.Remove(&I); |
| 307 | I.eraseFromParent(); |
| 308 | MadeIRChange = true; |
| 309 | return 0; // Don't do anything with FI |
| 310 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 311 | |
Rafael Espindola | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 312 | void ComputeMaskedBits(Value *V, APInt &KnownZero, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 313 | APInt &KnownOne, unsigned Depth = 0) const { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 314 | return llvm::ComputeMaskedBits(V, KnownZero, KnownOne, DL, Depth); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 315 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 316 | |
| 317 | bool MaskedValueIsZero(Value *V, const APInt &Mask, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 318 | unsigned Depth = 0) const { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 319 | return llvm::MaskedValueIsZero(V, Mask, DL, Depth); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 320 | } |
| 321 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame^] | 322 | return llvm::ComputeNumSignBits(Op, DL, Depth); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | private: |
| 326 | |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 327 | /// SimplifyAssociativeOrCommutative - This performs a few simplifications for |
| 328 | /// operators which are associative or commutative. |
| 329 | bool SimplifyAssociativeOrCommutative(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 330 | |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 331 | /// SimplifyUsingDistributiveLaws - This tries to simplify binary operations |
| 332 | /// which some other binary operation distributes over either by factorizing |
| 333 | /// out common terms (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this |
| 334 | /// results in simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is |
| 335 | /// a win). Returns the simplified value, or null if it didn't simplify. |
| 336 | Value *SimplifyUsingDistributiveLaws(BinaryOperator &I); |
Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 338 | /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value |
| 339 | /// based on the demanded bits. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 340 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 341 | APInt& KnownZero, APInt& KnownOne, |
| 342 | unsigned Depth); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 343 | bool SimplifyDemandedBits(Use &U, APInt DemandedMask, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 344 | APInt& KnownZero, APInt& KnownOne, |
| 345 | unsigned Depth=0); |
Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 346 | /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded |
| 347 | /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence. |
| 348 | Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl, |
| 349 | APInt DemandedMask, APInt &KnownZero, |
| 350 | APInt &KnownOne); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 352 | /// SimplifyDemandedInstructionBits - Inst is an integer instruction that |
| 353 | /// SimplifyDemandedBits knows about. See if the instruction has any |
| 354 | /// properties that allow us to simplify its operands. |
| 355 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 357 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
| 358 | APInt& UndefElts, unsigned Depth = 0); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 359 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 360 | // FoldOpIntoPhi - Given a binary operator, cast instruction, or select |
| 361 | // which has a PHI node as operand #0, see if we can fold the instruction |
| 362 | // into the PHI (which is only possible if all operands to the PHI are |
| 363 | // constants). |
| 364 | // |
Chris Lattner | ea7131a | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 365 | Instruction *FoldOpIntoPhi(Instruction &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 366 | |
| 367 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 368 | // operator and they all are only used by the PHI, PHI together their |
| 369 | // inputs, and do the operation once, to the result of the PHI. |
| 370 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 371 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 372 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 373 | Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN); |
| 374 | |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 376 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 377 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 379 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
| 380 | bool isSub, Instruction &I); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 381 | Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 382 | bool isSigned, bool Inside); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 383 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI); |
| 384 | Instruction *MatchBSwap(BinaryOperator &I); |
| 385 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
| 386 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| 387 | Instruction *SimplifyMemSet(MemSetInst *MI); |
| 388 | |
| 389 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 390 | Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned); |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 391 | |
| 392 | /// Descale - Return a value X such that Val = X * Scale, or null if none. If |
| 393 | /// the multiplication is known not to overflow then NoSignedWrap is set. |
| 394 | Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 397 | |
| 398 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 399 | } // end namespace llvm. |
| 400 | |
| 401 | #endif |