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