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