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