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" |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 5175b9a | 2015-01-20 08:35:24 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 452a007 | 2014-03-04 11:59:06 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/TargetFolder.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/ValueTracking.h" |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 20 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/Operator.h" |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 23 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 25 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "instcombine" |
| 27 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 28 | namespace llvm { |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 29 | class CallSite; |
| 30 | class DataLayout; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 31 | class DominatorTree; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 32 | class TargetLibraryInfo; |
| 33 | class DbgDeclareInst; |
| 34 | class MemIntrinsic; |
| 35 | class MemSetInst; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 36 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 37 | /// \brief Specific patterns of select instructions we can match. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 48 | /// \brief Assign a complexity or rank value to LLVM Values. |
| 49 | /// |
| 50 | /// This routine maps IR values to various complexity ranks: |
| 51 | /// 0 -> undef |
| 52 | /// 1 -> Constants |
| 53 | /// 2 -> Other non-instructions |
| 54 | /// 3 -> Arguments |
| 55 | /// 3 -> Unary operations |
| 56 | /// 4 -> Other instructions |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 57 | static inline unsigned getComplexity(Value *V) { |
| 58 | if (isa<Instruction>(V)) { |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 59 | if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) || |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 60 | BinaryOperator::isNot(V)) |
| 61 | return 3; |
| 62 | return 4; |
| 63 | } |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 64 | if (isa<Argument>(V)) |
| 65 | return 3; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 66 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
| 67 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 68 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 69 | /// \brief Add one to a Constant |
Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 70 | static inline Constant *AddOne(Constant *C) { |
| 71 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); |
| 72 | } |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 73 | /// \brief Subtract one from a Constant |
Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 74 | static inline Constant *SubOne(Constant *C) { |
| 75 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); |
| 76 | } |
| 77 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 78 | /// \brief An IRBuilder inserter that adds new instructions to the instcombine |
| 79 | /// worklist. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 80 | class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 81 | : public IRBuilderDefaultInserter<true> { |
| 82 | InstCombineWorklist &Worklist; |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 83 | AssumptionCache *AC; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 85 | public: |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 86 | InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC) |
| 87 | : Worklist(WL), AC(AC) {} |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 88 | |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 89 | void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, |
| 90 | BasicBlock::iterator InsertPt) const { |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 91 | IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt); |
| 92 | Worklist.Add(I); |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 93 | |
| 94 | using namespace llvm::PatternMatch; |
Benjamin Kramer | 63207bc | 2014-10-25 18:09:01 +0000 | [diff] [blame] | 95 | if (match(I, m_Intrinsic<Intrinsic::assume>())) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 96 | AC->registerAssumption(cast<CallInst>(I)); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 97 | } |
| 98 | }; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 99 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 100 | /// \brief The core instruction combiner logic. |
| 101 | /// |
| 102 | /// This class provides both the logic to recursively visit instructions and |
| 103 | /// combine them, as well as the pass infrastructure for running this as part |
| 104 | /// of the LLVM pass pipeline. |
Duncan Sands | 6c5e435 | 2010-05-11 20:16:09 +0000 | [diff] [blame] | 105 | class LLVM_LIBRARY_VISIBILITY InstCombiner |
Chandler Carruth | 1edb9d6 | 2015-01-20 22:44:35 +0000 | [diff] [blame] | 106 | : public InstVisitor<InstCombiner, Instruction *> { |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame^] | 107 | // FIXME: These members shouldn't be public. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 108 | public: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 109 | /// \brief A worklist of the instructions that need to be simplified. |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame^] | 110 | InstCombineWorklist &Worklist; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 111 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 112 | /// \brief An IRBuilder that automatically inserts new instructions into the |
| 113 | /// worklist. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 114 | typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy; |
| 115 | BuilderTy *Builder; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 116 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame^] | 117 | private: |
| 118 | // Mode in which we are running the combiner. |
| 119 | const bool MinimizeSize; |
| 120 | |
| 121 | // Required analyses. |
| 122 | // FIXME: These can never be null and should be references. |
| 123 | AssumptionCache *AC; |
| 124 | TargetLibraryInfo *TLI; |
| 125 | DominatorTree *DT; |
| 126 | |
| 127 | // Optional analyses. When non-null, these can both be used to do better |
| 128 | // combining and will be updated to reflect any changes. |
| 129 | const DataLayout *DL; |
| 130 | LoopInfo *LI; |
| 131 | |
| 132 | bool MadeIRChange; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 133 | |
| 134 | public: |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame^] | 135 | InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder, |
| 136 | bool MinimizeSize, AssumptionCache *AC, TargetLibraryInfo *TLI, |
| 137 | DominatorTree *DT, const DataLayout *DL, LoopInfo *LI) |
| 138 | : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize), |
| 139 | AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {} |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 140 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame^] | 141 | /// \brief Run the combiner over the entire worklist until it is empty. |
| 142 | /// |
| 143 | /// \returns true if the IR is changed. |
| 144 | bool run(); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 145 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 146 | AssumptionCache *getAssumptionCache() const { return AC; } |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 147 | |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 148 | const DataLayout *getDataLayout() const { return DL; } |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 149 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 150 | DominatorTree *getDominatorTree() const { return DT; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 151 | |
Chandler Carruth | 5175b9a | 2015-01-20 08:35:24 +0000 | [diff] [blame] | 152 | LoopInfo *getLoopInfo() const { return LI; } |
| 153 | |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 154 | TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; } |
| 155 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 156 | // Visitation implementation - Implement instruction combining for different |
| 157 | // instruction types. The semantics are as follows: |
| 158 | // Return Value: |
| 159 | // null - No change was made |
| 160 | // I - Change was made, I is still valid, I may be dead though |
| 161 | // otherwise - Change was made, replace I with returned instruction |
| 162 | // |
| 163 | Instruction *visitAdd(BinaryOperator &I); |
| 164 | Instruction *visitFAdd(BinaryOperator &I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 165 | Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 166 | Instruction *visitSub(BinaryOperator &I); |
| 167 | Instruction *visitFSub(BinaryOperator &I); |
| 168 | Instruction *visitMul(BinaryOperator &I); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 169 | Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C, |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 170 | Instruction *InsertBefore); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 171 | Instruction *visitFMul(BinaryOperator &I); |
| 172 | Instruction *visitURem(BinaryOperator &I); |
| 173 | Instruction *visitSRem(BinaryOperator &I); |
| 174 | Instruction *visitFRem(BinaryOperator &I); |
| 175 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
| 176 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 177 | Instruction *commonIRemTransforms(BinaryOperator &I); |
| 178 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 179 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 180 | Instruction *visitUDiv(BinaryOperator &I); |
| 181 | Instruction *visitSDiv(BinaryOperator &I); |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 182 | Instruction *visitFDiv(BinaryOperator &I); |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 183 | Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 184 | Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS); |
| 185 | Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 186 | Instruction *visitAnd(BinaryOperator &I); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 187 | Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 188 | Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 189 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, |
| 190 | Value *B, Value *C); |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 191 | Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A, |
| 192 | Value *B, Value *C); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 193 | Instruction *visitOr(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 194 | Instruction *visitXor(BinaryOperator &I); |
| 195 | Instruction *visitShl(BinaryOperator &I); |
| 196 | Instruction *visitAShr(BinaryOperator &I); |
| 197 | Instruction *visitLShr(BinaryOperator &I); |
| 198 | Instruction *commonShiftTransforms(BinaryOperator &I); |
| 199 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
| 200 | Constant *RHSC); |
| 201 | Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, |
| 202 | GlobalVariable *GV, CmpInst &ICI, |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 203 | ConstantInt *AndCst = nullptr); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 204 | Instruction *visitFCmpInst(FCmpInst &I); |
| 205 | Instruction *visitICmpInst(ICmpInst &I); |
| 206 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 207 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 208 | ConstantInt *RHS); |
| 209 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 210 | ConstantInt *DivRHS); |
Chris Lattner | d369f57 | 2011-02-13 07:43:07 +0000 | [diff] [blame] | 211 | Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 212 | ConstantInt *DivRHS); |
Suyog Sarda | 3a8c2c1 | 2014-07-22 19:19:36 +0000 | [diff] [blame] | 213 | Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A, |
| 214 | ConstantInt *CI1, ConstantInt *CI2); |
David Majnemer | 59939ac | 2014-10-19 08:23:08 +0000 | [diff] [blame] | 215 | Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A, |
| 216 | ConstantInt *CI1, ConstantInt *CI2); |
Benjamin Kramer | 0e2d162 | 2013-09-20 22:12:42 +0000 | [diff] [blame] | 217 | Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI, |
| 218 | ICmpInst::Predicate Pred); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 219 | Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 220 | ICmpInst::Predicate Cond, Instruction &I); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 221 | Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 222 | BinaryOperator &I); |
| 223 | Instruction *commonCastTransforms(CastInst &CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 224 | Instruction *commonPointerCastTransforms(CastInst &CI); |
| 225 | Instruction *visitTrunc(TruncInst &CI); |
| 226 | Instruction *visitZExt(ZExtInst &CI); |
| 227 | Instruction *visitSExt(SExtInst &CI); |
| 228 | Instruction *visitFPTrunc(FPTruncInst &CI); |
| 229 | Instruction *visitFPExt(CastInst &CI); |
| 230 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 231 | Instruction *visitFPToSI(FPToSIInst &FI); |
| 232 | Instruction *visitUIToFP(CastInst &CI); |
| 233 | Instruction *visitSIToFP(CastInst &CI); |
| 234 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
| 235 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
| 236 | Instruction *visitBitCast(BitCastInst &CI); |
Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 237 | Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 238 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI); |
| 239 | Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 240 | Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, |
| 241 | Value *A, Value *B, Instruction &Outer, |
| 242 | SelectPatternFlavor SPF2, Value *C); |
| 243 | Instruction *visitSelectInst(SelectInst &SI); |
| 244 | Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
| 245 | Instruction *visitCallInst(CallInst &CI); |
| 246 | Instruction *visitInvokeInst(InvokeInst &II); |
| 247 | |
| 248 | Instruction *SliceUpIllegalIntegerPHI(PHINode &PN); |
| 249 | Instruction *visitPHINode(PHINode &PN); |
| 250 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 251 | Instruction *visitAllocaInst(AllocaInst &AI); |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 252 | Instruction *visitAllocSite(Instruction &FI); |
Gabor Greif | 75f6943 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 253 | Instruction *visitFree(CallInst &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 254 | Instruction *visitLoadInst(LoadInst &LI); |
| 255 | Instruction *visitStoreInst(StoreInst &SI); |
| 256 | Instruction *visitBranchInst(BranchInst &BI); |
| 257 | Instruction *visitSwitchInst(SwitchInst &SI); |
Hal Finkel | 93873cc1 | 2014-09-07 21:28:34 +0000 | [diff] [blame] | 258 | Instruction *visitReturnInst(ReturnInst &RI); |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 259 | Instruction *visitInsertValueInst(InsertValueInst &IV); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 260 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
| 261 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
| 262 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
| 263 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Duncan Sands | 5c05579 | 2011-09-30 13:12:16 +0000 | [diff] [blame] | 264 | Instruction *visitLandingPadInst(LandingPadInst &LI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 265 | |
| 266 | // visitInstruction - Specify what to return for unhandled instructions... |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 267 | Instruction *visitInstruction(Instruction &I) { return nullptr; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 268 | |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 269 | // True when DB dominates all uses of DI execpt UI. |
| 270 | // UI must be in the same block as DI. |
| 271 | // The routine checks that the DI parent and DB are different. |
| 272 | bool dominatesAllUses(const Instruction *DI, const Instruction *UI, |
| 273 | const BasicBlock *DB) const; |
| 274 | |
| 275 | // Replace select with select operand SIOpd in SI-ICmp sequence when possible |
| 276 | bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, |
| 277 | const unsigned SIOpd); |
| 278 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 279 | private: |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 280 | bool ShouldChangeType(Type *From, Type *To) const; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 281 | Value *dyn_castNegVal(Value *V) const; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 282 | Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const; |
Matt Arsenault | d79f7d9 | 2013-08-19 22:17:40 +0000 | [diff] [blame] | 283 | Type *FindElementAtOffset(Type *PtrTy, int64_t Offset, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 284 | SmallVectorImpl<Value *> &NewIndices); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 285 | Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 286 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 287 | /// \brief Classify whether a cast is worth optimizing. |
| 288 | /// |
| 289 | /// Returns true if the cast from "V to Ty" actually results in any code |
| 290 | /// being generated and is interesting to optimize out. If the cast can be |
| 291 | /// eliminated by some other simple transformation, we prefer to do the |
| 292 | /// simplification first. |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 293 | bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 294 | Type *Ty); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 296 | Instruction *visitCallSite(CallSite CS); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 297 | Instruction *tryOptimizeCall(CallInst *CI, const DataLayout *DL); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 298 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 299 | Instruction *transformCallThroughTrampoline(CallSite CS, |
| 300 | IntrinsicInst *Tramp); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 301 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 302 | bool DoXform = true); |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 303 | Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 304 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction *CxtI); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 305 | bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction *CxtI); |
| 306 | bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction *CxtI); |
Erik Eckstein | a451b9b | 2014-12-17 07:29:19 +0000 | [diff] [blame] | 307 | bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction *CxtI); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 308 | Value *EmitGEPOffset(User *GEP); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 309 | Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 310 | Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 311 | |
| 312 | public: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 313 | /// \brief Inserts an instruction \p New before instruction \p Old |
| 314 | /// |
| 315 | /// Also adds the new instruction to the worklist and returns \p New so that |
| 316 | /// it is suitable for use as the return from the visitation patterns. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 317 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 318 | assert(New && !New->getParent() && |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 319 | "New instruction already inserted into a basic block!"); |
| 320 | BasicBlock *BB = Old.getParent(); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 321 | BB->getInstList().insert(&Old, New); // Insert inst |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 322 | Worklist.Add(New); |
| 323 | return New; |
| 324 | } |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 325 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 326 | /// \brief Same as InsertNewInstBefore, but also sets the debug loc. |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 327 | Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) { |
| 328 | New->setDebugLoc(Old.getDebugLoc()); |
| 329 | return InsertNewInstBefore(New, Old); |
| 330 | } |
| 331 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 332 | /// \brief A combiner-aware RAUW-like routine. |
| 333 | /// |
| 334 | /// This method is to be used when an instruction is found to be dead, |
| 335 | /// replacable with another preexisting expression. Here we add all uses of |
| 336 | /// I to the worklist, replace all uses of I with the new value, then return |
| 337 | /// I, so that the inst combiner will know that I was modified. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 338 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 339 | Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 341 | // If we are replacing the instruction with itself, this must be in a |
| 342 | // segment of unreachable code, so just clobber the instruction. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 343 | if (&I == V) |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 344 | V = UndefValue::get(I.getType()); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 345 | |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 346 | DEBUG(dbgs() << "IC: Replacing " << I << "\n" |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 347 | << " with " << *V << '\n'); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 349 | I.replaceAllUsesWith(V); |
| 350 | return &I; |
| 351 | } |
| 352 | |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 353 | /// Creates a result tuple for an overflow intrinsic \p II with a given |
| 354 | /// \p Result and a constant \p Overflow value. If \p ReUseName is true the |
| 355 | /// \p Result's name is taken from \p II. |
| 356 | Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result, |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 357 | bool Overflow, bool ReUseName = true) { |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 358 | if (ReUseName) |
| 359 | Result->takeName(II); |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 360 | Constant *V[] = {UndefValue::get(Result->getType()), |
| 361 | Overflow ? Builder->getTrue() : Builder->getFalse()}; |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 362 | StructType *ST = cast<StructType>(II->getType()); |
| 363 | Constant *Struct = ConstantStruct::get(ST, V); |
| 364 | return InsertValueInst::Create(Struct, Result, 0); |
| 365 | } |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 366 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 367 | /// \brief Combiner aware instruction erasure. |
| 368 | /// |
| 369 | /// When dealing with an instruction that has side effects or produces a void |
| 370 | /// value, we can't rely on DCE to delete the instruction. Instead, visit |
| 371 | /// methods should return the value returned by this function. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 372 | Instruction *EraseInstFromFunction(Instruction &I) { |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 373 | DEBUG(dbgs() << "IC: ERASE " << I << '\n'); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 374 | |
| 375 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 376 | // Make sure that we reprocess all operands now that we reduced their |
| 377 | // use counts. |
| 378 | if (I.getNumOperands() < 8) { |
| 379 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 380 | if (Instruction *Op = dyn_cast<Instruction>(*i)) |
| 381 | Worklist.Add(Op); |
| 382 | } |
| 383 | Worklist.Remove(&I); |
| 384 | I.eraseFromParent(); |
| 385 | MadeIRChange = true; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 386 | return nullptr; // Don't do anything with FI |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 387 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 388 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 389 | void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 390 | unsigned Depth = 0, Instruction *CxtI = nullptr) const { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 391 | return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI, |
| 392 | DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 393 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 394 | |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 395 | bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 396 | Instruction *CxtI = nullptr) const { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 397 | return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 398 | } |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 399 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0, |
| 400 | Instruction *CxtI = nullptr) const { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 401 | return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 402 | } |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 403 | void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
| 404 | unsigned Depth = 0, Instruction *CxtI = nullptr) const { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 405 | return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI, |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 406 | DT); |
| 407 | } |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 408 | OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, |
| 409 | const Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 410 | return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 411 | } |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 412 | OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, |
| 413 | const Instruction *CxtI) { |
| 414 | return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT); |
| 415 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 416 | |
| 417 | private: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 418 | /// \brief Performs a few simplifications for operators which are associative |
| 419 | /// or commutative. |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 420 | bool SimplifyAssociativeOrCommutative(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 421 | |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 422 | /// \brief Tries to simplify binary operations which some other binary |
| 423 | /// operation distributes over. |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 424 | /// |
| 425 | /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)" |
| 426 | /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A |
| 427 | /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified |
| 428 | /// value, or null if it didn't simplify. |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 429 | Value *SimplifyUsingDistributiveLaws(BinaryOperator &I); |
Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 430 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 431 | /// \brief Attempts to replace V with a simpler value based on the demanded |
| 432 | /// bits. |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 433 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 434 | APInt &KnownOne, unsigned Depth, |
| 435 | Instruction *CxtI = nullptr); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 436 | bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero, |
| 437 | APInt &KnownOne, unsigned Depth = 0); |
Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 438 | /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded |
| 439 | /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence. |
| 440 | Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl, |
| 441 | APInt DemandedMask, APInt &KnownZero, |
| 442 | APInt &KnownOne); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 443 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 444 | /// \brief Tries to simplify operands to an integer instruction based on its |
| 445 | /// demanded bits. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 446 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 448 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 449 | APInt &UndefElts, unsigned Depth = 0); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 450 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 451 | Value *SimplifyVectorOp(BinaryOperator &Inst); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 452 | Value *SimplifyBSwap(BinaryOperator &Inst); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 454 | // FoldOpIntoPhi - Given a binary operator, cast instruction, or select |
| 455 | // which has a PHI node as operand #0, see if we can fold the instruction |
| 456 | // into the PHI (which is only possible if all operands to the PHI are |
| 457 | // constants). |
| 458 | // |
Chris Lattner | ea7131a | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 459 | Instruction *FoldOpIntoPhi(Instruction &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 460 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 461 | /// \brief Try to rotate an operation below a PHI node, using PHI nodes for |
| 462 | /// its operands. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 463 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 464 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 465 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 466 | Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN); |
| 467 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 468 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 469 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 470 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 471 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
| 472 | bool isSub, Instruction &I); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 473 | Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned, |
| 474 | bool Inside); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 475 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI); |
| 476 | Instruction *MatchBSwap(BinaryOperator &I); |
| 477 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
| 478 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| 479 | Instruction *SimplifyMemSet(MemSetInst *MI); |
| 480 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 481 | Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned); |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 482 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 483 | /// \brief Returns a value X such that Val = X * Scale, or null if none. |
| 484 | /// |
| 485 | /// If the multiplication is known not to overflow then NoSignedWrap is set. |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 486 | Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 489 | } // end namespace llvm. |
| 490 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 491 | #undef DEBUG_TYPE |
| 492 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 493 | #endif |