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 | |
Bjorn Steinbrink | 8350534 | 2015-07-10 06:55:49 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 5175b9a | 2015-01-20 08:35:24 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 452a007 | 2014-03-04 11:59:06 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetFolder.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 25 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/IntrinsicInst.h" |
| 27 | #include "llvm/IR/Operator.h" |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 28 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 29 | #include "llvm/Pass.h" |
Chandler Carruth | 83ba269 | 2015-01-24 04:19:17 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" |
Adrian Prantl | 47ea647 | 2017-03-16 21:14:09 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.h" |
| 32 | #include "llvm/Support/Dwarf.h" |
| 33 | #include "llvm/IR/DIBuilder.h" |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 34 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 35 | #define DEBUG_TYPE "instcombine" |
| 36 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 37 | namespace llvm { |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 38 | class CallSite; |
| 39 | class DataLayout; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 40 | class DominatorTree; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 41 | class TargetLibraryInfo; |
| 42 | class DbgDeclareInst; |
| 43 | class MemIntrinsic; |
| 44 | class MemSetInst; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 45 | |
Sanjay Patel | 73fc8dd | 2017-02-03 21:43:34 +0000 | [diff] [blame] | 46 | /// Assign a complexity or rank value to LLVM Values. This is used to reduce |
| 47 | /// the amount of pattern matching needed for compares and commutative |
| 48 | /// instructions. For example, if we have: |
| 49 | /// icmp ugt X, Constant |
| 50 | /// or |
| 51 | /// xor (add X, Constant), cast Z |
| 52 | /// |
| 53 | /// We do not have to consider the commuted variants of these patterns because |
| 54 | /// canonicalization based on complexity guarantees the above ordering. |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 55 | /// |
| 56 | /// This routine maps IR values to various complexity ranks: |
| 57 | /// 0 -> undef |
| 58 | /// 1 -> Constants |
| 59 | /// 2 -> Other non-instructions |
| 60 | /// 3 -> Arguments |
Sanjay Patel | 73fc8dd | 2017-02-03 21:43:34 +0000 | [diff] [blame] | 61 | /// 4 -> Cast and (f)neg/not instructions |
| 62 | /// 5 -> Other instructions |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 63 | static inline unsigned getComplexity(Value *V) { |
| 64 | if (isa<Instruction>(V)) { |
Sanjay Patel | 73fc8dd | 2017-02-03 21:43:34 +0000 | [diff] [blame] | 65 | if (isa<CastInst>(V) || BinaryOperator::isNeg(V) || |
| 66 | BinaryOperator::isFNeg(V) || BinaryOperator::isNot(V)) |
| 67 | return 4; |
| 68 | return 5; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 69 | } |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 70 | if (isa<Argument>(V)) |
| 71 | return 3; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 72 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; |
| 73 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 74 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 75 | /// \brief Add one to a Constant |
Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 76 | static inline Constant *AddOne(Constant *C) { |
| 77 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); |
| 78 | } |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 79 | /// \brief Subtract one from a Constant |
Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 80 | static inline Constant *SubOne(Constant *C) { |
| 81 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); |
| 82 | } |
| 83 | |
Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 84 | /// \brief Return true if the specified value is free to invert (apply ~ to). |
| 85 | /// This happens in cases where the ~ can be eliminated. If WillInvertAllUses |
| 86 | /// is true, work under the assumption that the caller intends to remove all |
| 87 | /// uses of V and only keep uses of ~V. |
| 88 | /// |
| 89 | static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) { |
| 90 | // ~(~(X)) -> X. |
| 91 | if (BinaryOperator::isNot(V)) |
| 92 | return true; |
| 93 | |
| 94 | // Constants can be considered to be not'ed values. |
| 95 | if (isa<ConstantInt>(V)) |
| 96 | return true; |
| 97 | |
Sanjay Patel | 611f9f9 | 2016-10-27 17:30:50 +0000 | [diff] [blame] | 98 | // A vector of constant integers can be inverted easily. |
| 99 | Constant *CV; |
| 100 | if (V->getType()->isVectorTy() && match(V, PatternMatch::m_Constant(CV))) { |
| 101 | unsigned NumElts = V->getType()->getVectorNumElements(); |
| 102 | for (unsigned i = 0; i != NumElts; ++i) { |
| 103 | Constant *Elt = CV->getAggregateElement(i); |
| 104 | if (!Elt) |
| 105 | return false; |
| 106 | |
| 107 | if (isa<UndefValue>(Elt)) |
| 108 | continue; |
| 109 | |
| 110 | if (!isa<ConstantInt>(Elt)) |
| 111 | return false; |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 116 | // Compares can be inverted if all of their uses are being modified to use the |
| 117 | // ~V. |
| 118 | if (isa<CmpInst>(V)) |
| 119 | return WillInvertAllUses; |
| 120 | |
| 121 | // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1 |
| 122 | // - Constant) - A` if we are willing to invert all of the uses. |
| 123 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V)) |
| 124 | if (BO->getOpcode() == Instruction::Add || |
| 125 | BO->getOpcode() == Instruction::Sub) |
| 126 | if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1))) |
| 127 | return WillInvertAllUses; |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 132 | |
| 133 | /// \brief Specific patterns of overflow check idioms that we match. |
| 134 | enum OverflowCheckFlavor { |
| 135 | OCF_UNSIGNED_ADD, |
| 136 | OCF_SIGNED_ADD, |
| 137 | OCF_UNSIGNED_SUB, |
| 138 | OCF_SIGNED_SUB, |
| 139 | OCF_UNSIGNED_MUL, |
| 140 | OCF_SIGNED_MUL, |
| 141 | |
| 142 | OCF_INVALID |
| 143 | }; |
| 144 | |
| 145 | /// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op |
| 146 | /// intrinsic. |
| 147 | static inline OverflowCheckFlavor |
| 148 | IntrinsicIDToOverflowCheckFlavor(unsigned ID) { |
| 149 | switch (ID) { |
| 150 | default: |
| 151 | return OCF_INVALID; |
| 152 | case Intrinsic::uadd_with_overflow: |
| 153 | return OCF_UNSIGNED_ADD; |
| 154 | case Intrinsic::sadd_with_overflow: |
| 155 | return OCF_SIGNED_ADD; |
| 156 | case Intrinsic::usub_with_overflow: |
| 157 | return OCF_UNSIGNED_SUB; |
| 158 | case Intrinsic::ssub_with_overflow: |
| 159 | return OCF_SIGNED_SUB; |
| 160 | case Intrinsic::umul_with_overflow: |
| 161 | return OCF_UNSIGNED_MUL; |
| 162 | case Intrinsic::smul_with_overflow: |
| 163 | return OCF_SIGNED_MUL; |
| 164 | } |
| 165 | } |
| 166 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 167 | /// \brief The core instruction combiner logic. |
| 168 | /// |
| 169 | /// This class provides both the logic to recursively visit instructions and |
Craig Topper | 28ec346 | 2016-12-28 03:12:42 +0000 | [diff] [blame] | 170 | /// combine them. |
Duncan Sands | 6c5e435 | 2010-05-11 20:16:09 +0000 | [diff] [blame] | 171 | class LLVM_LIBRARY_VISIBILITY InstCombiner |
Chandler Carruth | 1edb9d6 | 2015-01-20 22:44:35 +0000 | [diff] [blame] | 172 | : public InstVisitor<InstCombiner, Instruction *> { |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 173 | // FIXME: These members shouldn't be public. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 174 | public: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 175 | /// \brief A worklist of the instructions that need to be simplified. |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 176 | InstCombineWorklist &Worklist; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 177 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 178 | /// \brief An IRBuilder that automatically inserts new instructions into the |
| 179 | /// worklist. |
Justin Bogner | 19dd0da | 2016-08-04 23:41:01 +0000 | [diff] [blame] | 180 | typedef IRBuilder<TargetFolder, IRBuilderCallbackInserter> BuilderTy; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 181 | BuilderTy *Builder; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 182 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 183 | private: |
| 184 | // Mode in which we are running the combiner. |
| 185 | const bool MinimizeSize; |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 186 | /// Enable combines that trigger rarely but are costly in compiletime. |
| 187 | const bool ExpensiveCombines; |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 188 | |
Bjorn Steinbrink | 8350534 | 2015-07-10 06:55:49 +0000 | [diff] [blame] | 189 | AliasAnalysis *AA; |
| 190 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 191 | // Required analyses. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 192 | AssumptionCache &AC; |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 193 | TargetLibraryInfo &TLI; |
| 194 | DominatorTree &DT; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 195 | const DataLayout &DL; |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 196 | |
| 197 | // Optional analyses. When non-null, these can both be used to do better |
| 198 | // combining and will be updated to reflect any changes. |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 199 | LoopInfo *LI; |
| 200 | |
| 201 | bool MadeIRChange; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 202 | |
| 203 | public: |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 204 | InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder, |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 205 | bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 206 | AssumptionCache &AC, TargetLibraryInfo &TLI, |
| 207 | DominatorTree &DT, const DataLayout &DL, LoopInfo *LI) |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 208 | : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 209 | ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT), |
| 210 | DL(DL), LI(LI), MadeIRChange(false) {} |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 211 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 212 | /// \brief Run the combiner over the entire worklist until it is empty. |
| 213 | /// |
| 214 | /// \returns true if the IR is changed. |
| 215 | bool run(); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 216 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 217 | AssumptionCache &getAssumptionCache() const { return AC; } |
| 218 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 219 | const DataLayout &getDataLayout() const { return DL; } |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 220 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 221 | DominatorTree &getDominatorTree() const { return DT; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 222 | |
Chandler Carruth | 5175b9a | 2015-01-20 08:35:24 +0000 | [diff] [blame] | 223 | LoopInfo *getLoopInfo() const { return LI; } |
| 224 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 225 | TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; } |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 227 | // Visitation implementation - Implement instruction combining for different |
| 228 | // instruction types. The semantics are as follows: |
| 229 | // Return Value: |
| 230 | // null - No change was made |
| 231 | // I - Change was made, I is still valid, I may be dead though |
| 232 | // otherwise - Change was made, replace I with returned instruction |
| 233 | // |
| 234 | Instruction *visitAdd(BinaryOperator &I); |
| 235 | Instruction *visitFAdd(BinaryOperator &I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 236 | Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 237 | Instruction *visitSub(BinaryOperator &I); |
| 238 | Instruction *visitFSub(BinaryOperator &I); |
| 239 | Instruction *visitMul(BinaryOperator &I); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 240 | Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C, |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 241 | Instruction *InsertBefore); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 242 | Instruction *visitFMul(BinaryOperator &I); |
| 243 | Instruction *visitURem(BinaryOperator &I); |
| 244 | Instruction *visitSRem(BinaryOperator &I); |
| 245 | Instruction *visitFRem(BinaryOperator &I); |
| 246 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
| 247 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 248 | Instruction *commonIRemTransforms(BinaryOperator &I); |
| 249 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 250 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 251 | Instruction *visitUDiv(BinaryOperator &I); |
| 252 | Instruction *visitSDiv(BinaryOperator &I); |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 253 | Instruction *visitFDiv(BinaryOperator &I); |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 254 | Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 255 | Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS); |
| 256 | Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 257 | Instruction *visitAnd(BinaryOperator &I); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 258 | Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 259 | Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 260 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, |
| 261 | Value *B, Value *C); |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 262 | Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A, |
| 263 | Value *B, Value *C); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 264 | Instruction *visitOr(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 265 | Instruction *visitXor(BinaryOperator &I); |
| 266 | Instruction *visitShl(BinaryOperator &I); |
| 267 | Instruction *visitAShr(BinaryOperator &I); |
| 268 | Instruction *visitLShr(BinaryOperator &I); |
| 269 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 270 | Instruction *visitFCmpInst(FCmpInst &I); |
| 271 | Instruction *visitICmpInst(ICmpInst &I); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 272 | Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 273 | BinaryOperator &I); |
| 274 | Instruction *commonCastTransforms(CastInst &CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 275 | Instruction *commonPointerCastTransforms(CastInst &CI); |
| 276 | Instruction *visitTrunc(TruncInst &CI); |
| 277 | Instruction *visitZExt(ZExtInst &CI); |
| 278 | Instruction *visitSExt(SExtInst &CI); |
| 279 | Instruction *visitFPTrunc(FPTruncInst &CI); |
| 280 | Instruction *visitFPExt(CastInst &CI); |
| 281 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 282 | Instruction *visitFPToSI(FPToSIInst &FI); |
| 283 | Instruction *visitUIToFP(CastInst &CI); |
| 284 | Instruction *visitSIToFP(CastInst &CI); |
| 285 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
| 286 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
| 287 | Instruction *visitBitCast(BitCastInst &CI); |
Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 288 | Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI); |
Mehdi Amini | b9a0fa4 | 2015-02-16 21:47:54 +0000 | [diff] [blame] | 289 | Instruction *FoldItoFPtoI(Instruction &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 290 | Instruction *visitSelectInst(SelectInst &SI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 291 | Instruction *visitCallInst(CallInst &CI); |
| 292 | Instruction *visitInvokeInst(InvokeInst &II); |
| 293 | |
| 294 | Instruction *SliceUpIllegalIntegerPHI(PHINode &PN); |
| 295 | Instruction *visitPHINode(PHINode &PN); |
| 296 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 297 | Instruction *visitAllocaInst(AllocaInst &AI); |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 298 | Instruction *visitAllocSite(Instruction &FI); |
Gabor Greif | 75f6943 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 299 | Instruction *visitFree(CallInst &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 300 | Instruction *visitLoadInst(LoadInst &LI); |
| 301 | Instruction *visitStoreInst(StoreInst &SI); |
| 302 | Instruction *visitBranchInst(BranchInst &BI); |
Davide Italiano | aec4617 | 2017-01-31 18:09:05 +0000 | [diff] [blame] | 303 | Instruction *visitFenceInst(FenceInst &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 304 | Instruction *visitSwitchInst(SwitchInst &SI); |
Hal Finkel | 93873cc1 | 2014-09-07 21:28:34 +0000 | [diff] [blame] | 305 | Instruction *visitReturnInst(ReturnInst &RI); |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 306 | Instruction *visitInsertValueInst(InsertValueInst &IV); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 307 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
| 308 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
| 309 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
| 310 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Duncan Sands | 5c05579 | 2011-09-30 13:12:16 +0000 | [diff] [blame] | 311 | Instruction *visitLandingPadInst(LandingPadInst &LI); |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 312 | Instruction *visitVAStartInst(VAStartInst &I); |
| 313 | Instruction *visitVACopyInst(VACopyInst &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 314 | |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 315 | /// Specify what to return for unhandled instructions. |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 316 | Instruction *visitInstruction(Instruction &I) { return nullptr; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 317 | |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 318 | /// True when DB dominates all uses of DI except UI. |
| 319 | /// UI must be in the same block as DI. |
| 320 | /// The routine checks that the DI parent and DB are different. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 321 | bool dominatesAllUses(const Instruction *DI, const Instruction *UI, |
| 322 | const BasicBlock *DB) const; |
| 323 | |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 324 | /// Try to replace select with select operand SIOpd in SI-ICmp sequence. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 325 | bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, |
| 326 | const unsigned SIOpd); |
| 327 | |
Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 328 | /// Try to replace instruction \p I with value \p V which are pointers |
| 329 | /// in different address space. |
| 330 | /// \return true if successful. |
| 331 | bool replacePointer(Instruction &I, Value *V); |
| 332 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 333 | private: |
Sanjay Patel | 2217f75 | 2017-01-31 17:25:42 +0000 | [diff] [blame] | 334 | bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const; |
| 335 | bool shouldChangeType(Type *From, Type *To) const; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 336 | Value *dyn_castNegVal(Value *V) const; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 337 | Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const; |
David Blaikie | 87ca1b6 | 2015-03-27 20:56:11 +0000 | [diff] [blame] | 338 | Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 339 | SmallVectorImpl<Value *> &NewIndices); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 340 | |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 341 | /// Classify whether a cast is worth optimizing. |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 342 | /// |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 343 | /// This is a helper to decide whether the simplification of |
| 344 | /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed. |
| 345 | /// |
| 346 | /// \param CI The cast we are interested in. |
| 347 | /// |
| 348 | /// \return true if this cast actually results in any code being generated and |
| 349 | /// if it cannot already be eliminated by some other transformation. |
| 350 | bool shouldOptimizeCast(CastInst *CI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 351 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 352 | /// \brief Try to optimize a sequence of instructions checking if an operation |
| 353 | /// on LHS and RHS overflows. |
| 354 | /// |
Sanjoy Das | 6f5dca7 | 2015-08-28 19:09:31 +0000 | [diff] [blame] | 355 | /// If this overflow check is done via one of the overflow check intrinsics, |
| 356 | /// then CtxI has to be the call instruction calling that intrinsic. If this |
| 357 | /// overflow check is done by arithmetic followed by a compare, then CtxI has |
| 358 | /// to be the arithmetic instruction. |
| 359 | /// |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 360 | /// If a simplification is possible, stores the simplified result of the |
| 361 | /// operation in OperationResult and result of the overflow check in |
| 362 | /// OverflowResult, and return true. If no simplification is possible, |
| 363 | /// returns false. |
| 364 | bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS, |
| 365 | Instruction &CtxI, Value *&OperationResult, |
| 366 | Constant *&OverflowResult); |
| 367 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 368 | Instruction *visitCallSite(CallSite CS); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 369 | Instruction *tryOptimizeCall(CallInst *CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 370 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 371 | Instruction *transformCallThroughTrampoline(CallSite CS, |
| 372 | IntrinsicInst *Tramp); |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 373 | |
| 374 | /// Transform (zext icmp) to bitwise / integer operations in order to |
| 375 | /// eliminate it. |
| 376 | /// |
| 377 | /// \param ICI The icmp of the (zext icmp) pair we are interested in. |
| 378 | /// \parem CI The zext of the (zext icmp) pair we are interested in. |
| 379 | /// \param DoTransform Pass false to just test whether the given (zext icmp) |
| 380 | /// would be transformed. Pass true to actually perform the transformation. |
| 381 | /// |
| 382 | /// \return null if the transformation cannot be performed. If the |
| 383 | /// transformation can be performed the new instruction that replaces the |
| 384 | /// (zext icmp) pair will be returned (if \p DoTransform is false the |
| 385 | /// unmodified \p ICI will be returned in this case). |
| 386 | Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI, |
| 387 | bool DoTransform = true); |
| 388 | |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 389 | Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 390 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI); |
| 391 | bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI); |
| 392 | bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI); |
| 393 | bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 394 | Value *EmitGEPOffset(User *GEP); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 395 | Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 396 | Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask); |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 397 | Instruction *foldCastedBitwiseLogic(BinaryOperator &I); |
Sanjay Patel | aa8b28e | 2016-11-30 20:48:54 +0000 | [diff] [blame] | 398 | Instruction *shrinkBitwiseLogic(TruncInst &Trunc); |
Guozhi Wei | ae541f6 | 2016-10-25 20:43:42 +0000 | [diff] [blame] | 399 | Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 400 | |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 401 | /// Determine if a pair of casts can be replaced by a single cast. |
| 402 | /// |
| 403 | /// \param CI1 The first of a pair of casts. |
| 404 | /// \param CI2 The second of a pair of casts. |
| 405 | /// |
| 406 | /// \return 0 if the cast pair cannot be eliminated, otherwise returns an |
| 407 | /// Instruction::CastOps value for a cast that can replace the pair, casting |
| 408 | /// CI1->getSrcTy() to CI2->getDstTy(). |
| 409 | /// |
| 410 | /// \see CastInst::isEliminableCastPair |
| 411 | Instruction::CastOps isEliminableCastPair(const CastInst *CI1, |
| 412 | const CastInst *CI2); |
| 413 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 414 | public: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 415 | /// \brief Inserts an instruction \p New before instruction \p Old |
| 416 | /// |
| 417 | /// Also adds the new instruction to the worklist and returns \p New so that |
| 418 | /// it is suitable for use as the return from the visitation patterns. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 419 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 420 | assert(New && !New->getParent() && |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 421 | "New instruction already inserted into a basic block!"); |
| 422 | BasicBlock *BB = Old.getParent(); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 423 | BB->getInstList().insert(Old.getIterator(), New); // Insert inst |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 424 | Worklist.Add(New); |
| 425 | return New; |
| 426 | } |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 427 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 428 | /// \brief Same as InsertNewInstBefore, but also sets the debug loc. |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 429 | Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) { |
| 430 | New->setDebugLoc(Old.getDebugLoc()); |
| 431 | return InsertNewInstBefore(New, Old); |
| 432 | } |
| 433 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 434 | /// \brief A combiner-aware RAUW-like routine. |
| 435 | /// |
| 436 | /// This method is to be used when an instruction is found to be dead, |
Sanjay Patel | f2ea8a2 | 2016-01-06 00:23:12 +0000 | [diff] [blame] | 437 | /// replaceable with another preexisting expression. Here we add all uses of |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 438 | /// I to the worklist, replace all uses of I with the new value, then return |
| 439 | /// I, so that the inst combiner will know that I was modified. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 440 | Instruction *replaceInstUsesWith(Instruction &I, Value *V) { |
Owen Anderson | 51b75b8c | 2015-03-10 05:13:47 +0000 | [diff] [blame] | 441 | // If there are no uses to replace, then we return nullptr to indicate that |
| 442 | // no changes were made to the program. |
| 443 | if (I.use_empty()) return nullptr; |
| 444 | |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 445 | Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 447 | // If we are replacing the instruction with itself, this must be in a |
| 448 | // segment of unreachable code, so just clobber the instruction. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 449 | if (&I == V) |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 450 | V = UndefValue::get(I.getType()); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 451 | |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 452 | DEBUG(dbgs() << "IC: Replacing " << I << "\n" |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 453 | << " with " << *V << '\n'); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 454 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 455 | I.replaceAllUsesWith(V); |
| 456 | return &I; |
| 457 | } |
| 458 | |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 459 | /// Creates a result tuple for an overflow intrinsic \p II with a given |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 460 | /// \p Result and a constant \p Overflow value. |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 461 | Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result, |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 462 | Constant *Overflow) { |
| 463 | Constant *V[] = {UndefValue::get(Result->getType()), Overflow}; |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 464 | StructType *ST = cast<StructType>(II->getType()); |
| 465 | Constant *Struct = ConstantStruct::get(ST, V); |
| 466 | return InsertValueInst::Create(Struct, Result, 0); |
| 467 | } |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 468 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 469 | /// \brief Combiner aware instruction erasure. |
| 470 | /// |
| 471 | /// When dealing with an instruction that has side effects or produces a void |
| 472 | /// value, we can't rely on DCE to delete the instruction. Instead, visit |
| 473 | /// methods should return the value returned by this function. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 474 | Instruction *eraseInstFromFunction(Instruction &I) { |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 475 | DEBUG(dbgs() << "IC: ERASE " << I << '\n'); |
Adrian Prantl | fa9e84e | 2017-03-16 20:11:54 +0000 | [diff] [blame] | 476 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
Adrian Prantl | 47ea647 | 2017-03-16 21:14:09 +0000 | [diff] [blame] | 477 | salvageDebugInfo(I); |
| 478 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 479 | // Make sure that we reprocess all operands now that we reduced their |
| 480 | // use counts. |
| 481 | if (I.getNumOperands() < 8) { |
Sanjay Patel | 8af7fbc3 | 2016-01-31 16:34:48 +0000 | [diff] [blame] | 482 | for (Use &Operand : I.operands()) |
| 483 | if (auto *Inst = dyn_cast<Instruction>(Operand)) |
| 484 | Worklist.Add(Inst); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 485 | } |
| 486 | Worklist.Remove(&I); |
| 487 | I.eraseFromParent(); |
| 488 | MadeIRChange = true; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 489 | return nullptr; // Don't do anything with FI |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 490 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 491 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 492 | void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 493 | unsigned Depth, Instruction *CxtI) const { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 494 | return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI, |
| 495 | &DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 496 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 497 | |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 498 | bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 499 | Instruction *CxtI = nullptr) const { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 500 | return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 501 | } |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 502 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0, |
| 503 | Instruction *CxtI = nullptr) const { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 504 | return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 505 | } |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 506 | void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
| 507 | unsigned Depth = 0, Instruction *CxtI = nullptr) const { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 508 | return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI, |
| 509 | &DT); |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 510 | } |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 511 | OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, |
| 512 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 513 | return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 514 | } |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 515 | OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, |
| 516 | const Instruction *CxtI) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 517 | return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT); |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 518 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 519 | |
Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 520 | /// Maximum size of array considered when transforming. |
David Blaikie | 4c01af2 | 2017-02-07 18:58:17 +0000 | [diff] [blame] | 521 | uint64_t MaxArraySizeForCombine; |
Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 523 | private: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 524 | /// \brief Performs a few simplifications for operators which are associative |
| 525 | /// or commutative. |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 526 | bool SimplifyAssociativeOrCommutative(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 527 | |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 528 | /// \brief Tries to simplify binary operations which some other binary |
| 529 | /// operation distributes over. |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 530 | /// |
| 531 | /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)" |
| 532 | /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A |
| 533 | /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified |
| 534 | /// value, or null if it didn't simplify. |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 535 | Value *SimplifyUsingDistributiveLaws(BinaryOperator &I); |
Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 536 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 537 | /// \brief Attempts to replace V with a simpler value based on the demanded |
| 538 | /// bits. |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 539 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 540 | APInt &KnownOne, unsigned Depth, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 541 | Instruction *CxtI); |
Craig Topper | 47596dd | 2017-03-25 06:52:52 +0000 | [diff] [blame^] | 542 | bool SimplifyDemandedBits(Instruction *I, unsigned Op, |
| 543 | const APInt &DemandedMask, APInt &KnownZero, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 544 | APInt &KnownOne, unsigned Depth = 0); |
Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 545 | /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded |
| 546 | /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence. |
| 547 | Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl, |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 548 | const APInt &DemandedMask, APInt &KnownZero, |
Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 549 | APInt &KnownOne); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 550 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 551 | /// \brief Tries to simplify operands to an integer instruction based on its |
| 552 | /// demanded bits. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 553 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 555 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 556 | APInt &UndefElts, unsigned Depth = 0); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 557 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 558 | Value *SimplifyVectorOp(BinaryOperator &Inst); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 559 | Value *SimplifyBSwap(BinaryOperator &Inst); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 560 | |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 561 | |
| 562 | /// Given a binary operator, cast instruction, or select which has a PHI node |
| 563 | /// as operand #0, see if we can fold the instruction into the PHI (which is |
| 564 | /// only possible if all operands to the PHI are constants). |
Chris Lattner | ea7131a | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 565 | Instruction *FoldOpIntoPhi(Instruction &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 566 | |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 567 | /// Given an instruction with a select as one operand and a constant as the |
| 568 | /// other operand, try to fold the binary operator into the select arguments. |
| 569 | /// This also works for Cast instructions, which obviously do not have a |
| 570 | /// second operand. |
| 571 | Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); |
| 572 | |
| 573 | /// This is a convenience wrapper function for the above two functions. |
| 574 | Instruction *foldOpWithConstantIntoOperand(Instruction &I); |
| 575 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 576 | /// \brief Try to rotate an operation below a PHI node, using PHI nodes for |
| 577 | /// its operands. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 578 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 579 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 580 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 581 | Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN); |
Sanjay Patel | 9533407 | 2015-09-27 20:34:31 +0000 | [diff] [blame] | 582 | Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 583 | |
Robert Lougher | 2428a40 | 2016-12-14 17:49:19 +0000 | [diff] [blame] | 584 | /// Helper function for FoldPHIArgXIntoPHI() to get debug location for the |
| 585 | /// folded operation. |
| 586 | DebugLoc PHIArgMergedDebugLoc(PHINode &PN); |
| 587 | |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 588 | Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 589 | ICmpInst::Predicate Cond, Instruction &I); |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 590 | Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca, |
| 591 | const Value *Other); |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 592 | Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, |
| 593 | GlobalVariable *GV, CmpInst &ICI, |
| 594 | ConstantInt *AndCst = nullptr); |
| 595 | Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, |
| 596 | Constant *RHSC); |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 597 | Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI, |
| 598 | ICmpInst::Predicate Pred); |
| 599 | Instruction *foldICmpWithCastAndCast(ICmpInst &ICI); |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 600 | |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 601 | Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp); |
Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 602 | Instruction *foldICmpWithConstant(ICmpInst &Cmp); |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 603 | Instruction *foldICmpInstWithConstant(ICmpInst &Cmp); |
Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 604 | Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp); |
| 605 | Instruction *foldICmpBinOp(ICmpInst &Cmp); |
| 606 | Instruction *foldICmpEquality(ICmpInst &Cmp); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 607 | |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 608 | Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc, |
| 609 | const APInt *C); |
| 610 | Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, |
| 611 | const APInt *C); |
| 612 | Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor, |
| 613 | const APInt *C); |
| 614 | Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, |
| 615 | const APInt *C); |
| 616 | Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul, |
| 617 | const APInt *C); |
| 618 | Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl, |
| 619 | const APInt *C); |
| 620 | Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, |
| 621 | const APInt *C); |
| 622 | Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, |
| 623 | const APInt *C); |
| 624 | Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, |
| 625 | const APInt *C); |
| 626 | Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, |
| 627 | const APInt *C); |
| 628 | Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add, |
| 629 | const APInt *C); |
Sanjay Patel | d3c7bb28 | 2016-08-26 16:42:33 +0000 | [diff] [blame] | 630 | Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And, |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 631 | const APInt *C1); |
| 632 | Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 633 | const APInt *C1, const APInt *C2); |
Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 634 | Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, |
| 635 | const APInt &C2); |
| 636 | Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, |
| 637 | const APInt &C2); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 638 | |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 639 | Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, |
| 640 | BinaryOperator *BO, |
| 641 | const APInt *C); |
| 642 | Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, const APInt *C); |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 643 | |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 644 | // Helpers of visitSelectInst(). |
Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 645 | Instruction *foldSelectExtConst(SelectInst &Sel); |
Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 646 | Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI); |
| 647 | Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *); |
| 648 | Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, |
| 649 | Value *A, Value *B, Instruction &Outer, |
| 650 | SelectPatternFlavor SPF2, Value *C); |
| 651 | Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
| 652 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 653 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 654 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 656 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
| 657 | bool isSub, Instruction &I); |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 658 | Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, |
| 659 | bool isSigned, bool Inside); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 660 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI); |
Chad Rosier | a00df49 | 2016-05-25 16:22:14 +0000 | [diff] [blame] | 661 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 662 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Igor Laevsky | 900ffa3 | 2017-02-08 14:32:04 +0000 | [diff] [blame] | 663 | |
| 664 | Instruction *SimplifyElementAtomicMemCpy(ElementAtomicMemCpyInst *AMI); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 665 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 666 | Instruction *SimplifyMemSet(MemSetInst *MI); |
| 667 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 668 | Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned); |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 669 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 670 | /// \brief Returns a value X such that Val = X * Scale, or null if none. |
| 671 | /// |
| 672 | /// If the multiplication is known not to overflow then NoSignedWrap is set. |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 673 | Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 676 | } // end namespace llvm. |
| 677 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 678 | #undef DEBUG_TYPE |
| 679 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 680 | #endif |