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