| 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 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 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. | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 12 | // | 
| Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 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 |  | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/ArrayRef.h" | 
| Bjorn Steinbrink | 8350534 | 2015-07-10 06:55:49 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InstructionSimplify.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" | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Argument.h" | 
|  | 24 | #include "llvm/IR/BasicBlock.h" | 
|  | 25 | #include "llvm/IR/Constant.h" | 
|  | 26 | #include "llvm/IR/Constants.h" | 
|  | 27 | #include "llvm/IR/DerivedTypes.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/IRBuilder.h" | 
| Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 29 | #include "llvm/IR/InstVisitor.h" | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 30 | #include "llvm/IR/InstrTypes.h" | 
|  | 31 | #include "llvm/IR/Instruction.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/IntrinsicInst.h" | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Intrinsics.h" | 
| David Green | 1e44c3b | 2018-10-02 09:48:34 +0000 | [diff] [blame] | 34 | #include "llvm/IR/PatternMatch.h" | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Use.h" | 
|  | 36 | #include "llvm/IR/Value.h" | 
|  | 37 | #include "llvm/Support/Casting.h" | 
|  | 38 | #include "llvm/Support/Compiler.h" | 
|  | 39 | #include "llvm/Support/Debug.h" | 
| Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 40 | #include "llvm/Support/KnownBits.h" | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" | 
| Chandler Carruth | 83ba269 | 2015-01-24 04:19:17 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" | 
| David Green | 1e44c3b | 2018-10-02 09:48:34 +0000 | [diff] [blame] | 43 | #include "llvm/Transforms/Utils/Local.h" | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 44 | #include <cassert> | 
|  | 45 | #include <cstdint> | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 46 |  | 
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "instcombine" | 
|  | 48 |  | 
| David Green | 1e44c3b | 2018-10-02 09:48:34 +0000 | [diff] [blame] | 49 | using namespace llvm::PatternMatch; | 
|  | 50 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 51 | namespace llvm { | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 52 |  | 
|  | 53 | class APInt; | 
|  | 54 | class AssumptionCache; | 
| Hiroshi Yamauchi | 09e539f | 2019-04-15 16:49:00 +0000 | [diff] [blame] | 55 | class BlockFrequencyInfo; | 
| Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 56 | class DataLayout; | 
| Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 57 | class DominatorTree; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 58 | class GEPOperator; | 
|  | 59 | class GlobalVariable; | 
|  | 60 | class LoopInfo; | 
| Adam Nemet | ea06e6e | 2017-07-26 19:03:18 +0000 | [diff] [blame] | 61 | class OptimizationRemarkEmitter; | 
| Hiroshi Yamauchi | 09e539f | 2019-04-15 16:49:00 +0000 | [diff] [blame] | 62 | class ProfileSummaryInfo; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 63 | class TargetLibraryInfo; | 
|  | 64 | class User; | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 65 |  | 
| Sanjay Patel | 73fc8dd | 2017-02-03 21:43:34 +0000 | [diff] [blame] | 66 | /// Assign a complexity or rank value to LLVM Values. This is used to reduce | 
|  | 67 | /// the amount of pattern matching needed for compares and commutative | 
|  | 68 | /// instructions. For example, if we have: | 
|  | 69 | ///   icmp ugt X, Constant | 
|  | 70 | /// or | 
|  | 71 | ///   xor (add X, Constant), cast Z | 
|  | 72 | /// | 
|  | 73 | /// We do not have to consider the commuted variants of these patterns because | 
|  | 74 | /// canonicalization based on complexity guarantees the above ordering. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 75 | /// | 
|  | 76 | /// This routine maps IR values to various complexity ranks: | 
|  | 77 | ///   0 -> undef | 
|  | 78 | ///   1 -> Constants | 
|  | 79 | ///   2 -> Other non-instructions | 
|  | 80 | ///   3 -> Arguments | 
| Sanjay Patel | 73fc8dd | 2017-02-03 21:43:34 +0000 | [diff] [blame] | 81 | ///   4 -> Cast and (f)neg/not instructions | 
|  | 82 | ///   5 -> Other instructions | 
| Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 83 | static inline unsigned getComplexity(Value *V) { | 
|  | 84 | if (isa<Instruction>(V)) { | 
| Sanjay Patel | 47579b2 | 2018-10-13 16:15:37 +0000 | [diff] [blame] | 85 | if (isa<CastInst>(V) || match(V, m_Neg(m_Value())) || | 
|  | 86 | match(V, m_Not(m_Value())) || match(V, m_FNeg(m_Value()))) | 
| Sanjay Patel | 73fc8dd | 2017-02-03 21:43:34 +0000 | [diff] [blame] | 87 | return 4; | 
|  | 88 | return 5; | 
| Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 89 | } | 
| Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 90 | if (isa<Argument>(V)) | 
|  | 91 | return 3; | 
| Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 92 | return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2; | 
|  | 93 | } | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 94 |  | 
| Sanjay Patel | b2e7003 | 2017-05-17 14:21:19 +0000 | [diff] [blame] | 95 | /// Predicate canonicalization reduces the number of patterns that need to be | 
|  | 96 | /// matched by other transforms. For example, we may swap the operands of a | 
|  | 97 | /// conditional branch or select to create a compare with a canonical (inverted) | 
|  | 98 | /// predicate which is then more likely to be matched with other values. | 
|  | 99 | static inline bool isCanonicalPredicate(CmpInst::Predicate Pred) { | 
|  | 100 | switch (Pred) { | 
|  | 101 | case CmpInst::ICMP_NE: | 
|  | 102 | case CmpInst::ICMP_ULE: | 
|  | 103 | case CmpInst::ICMP_SLE: | 
|  | 104 | case CmpInst::ICMP_UGE: | 
|  | 105 | case CmpInst::ICMP_SGE: | 
|  | 106 | // TODO: There are 16 FCMP predicates. Should others be (not) canonical? | 
|  | 107 | case CmpInst::FCMP_ONE: | 
|  | 108 | case CmpInst::FCMP_OLE: | 
|  | 109 | case CmpInst::FCMP_OGE: | 
|  | 110 | return false; | 
|  | 111 | default: | 
|  | 112 | return true; | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 |  | 
| Roman Lebedev | 0c73be5 | 2019-10-07 20:53:08 +0000 | [diff] [blame] | 116 | /// Given an exploded icmp instruction, return true if the comparison only | 
|  | 117 | /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if the | 
|  | 118 | /// result of the comparison is true when the input value is signed. | 
|  | 119 | inline bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, | 
|  | 120 | bool &TrueIfSigned) { | 
|  | 121 | switch (Pred) { | 
|  | 122 | case ICmpInst::ICMP_SLT: // True if LHS s< 0 | 
|  | 123 | TrueIfSigned = true; | 
|  | 124 | return RHS.isNullValue(); | 
|  | 125 | case ICmpInst::ICMP_SLE: // True if LHS s<= -1 | 
|  | 126 | TrueIfSigned = true; | 
|  | 127 | return RHS.isAllOnesValue(); | 
|  | 128 | case ICmpInst::ICMP_SGT: // True if LHS s> -1 | 
|  | 129 | TrueIfSigned = false; | 
|  | 130 | return RHS.isAllOnesValue(); | 
|  | 131 | case ICmpInst::ICMP_SGE: // True if LHS s>= 0 | 
|  | 132 | TrueIfSigned = false; | 
|  | 133 | return RHS.isNullValue(); | 
|  | 134 | case ICmpInst::ICMP_UGT: | 
|  | 135 | // True if LHS u> RHS and RHS == sign-bit-mask - 1 | 
|  | 136 | TrueIfSigned = true; | 
|  | 137 | return RHS.isMaxSignedValue(); | 
|  | 138 | case ICmpInst::ICMP_UGE: | 
|  | 139 | // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) | 
|  | 140 | TrueIfSigned = true; | 
|  | 141 | return RHS.isMinSignedValue(); | 
|  | 142 | case ICmpInst::ICMP_ULT: | 
|  | 143 | // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) | 
|  | 144 | TrueIfSigned = false; | 
|  | 145 | return RHS.isMinSignedValue(); | 
|  | 146 | case ICmpInst::ICMP_ULE: | 
|  | 147 | // True if LHS u<= RHS and RHS == sign-bit-mask - 1 | 
|  | 148 | TrueIfSigned = false; | 
|  | 149 | return RHS.isMaxSignedValue(); | 
|  | 150 | default: | 
|  | 151 | return false; | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
| Roman Lebedev | 32f1e1a | 2019-08-14 09:57:20 +0000 | [diff] [blame] | 155 | llvm::Optional<std::pair<CmpInst::Predicate, Constant *>> | 
|  | 156 | getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred, Constant *C); | 
|  | 157 |  | 
| Sanjay Patel | e800df8e | 2017-06-22 15:28:01 +0000 | [diff] [blame] | 158 | /// Return the source operand of a potentially bitcasted value while optionally | 
|  | 159 | /// checking if it has one use. If there is no bitcast or the one use check is | 
|  | 160 | /// not met, return the input value itself. | 
|  | 161 | static inline Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) { | 
|  | 162 | if (auto *BitCast = dyn_cast<BitCastInst>(V)) | 
|  | 163 | if (!OneUseOnly || BitCast->hasOneUse()) | 
|  | 164 | return BitCast->getOperand(0); | 
|  | 165 |  | 
|  | 166 | // V is not a bitcast or V has more than one use and OneUseOnly is true. | 
|  | 167 | return V; | 
|  | 168 | } | 
|  | 169 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 170 | /// Add one to a Constant | 
| Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 171 | static inline Constant *AddOne(Constant *C) { | 
|  | 172 | return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1)); | 
|  | 173 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 174 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 175 | /// Subtract one from a Constant | 
| Benjamin Kramer | 970f495 | 2014-01-19 16:56:10 +0000 | [diff] [blame] | 176 | static inline Constant *SubOne(Constant *C) { | 
|  | 177 | return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1)); | 
|  | 178 | } | 
|  | 179 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 180 | /// Return true if the specified value is free to invert (apply ~ to). | 
| Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 181 | /// This happens in cases where the ~ can be eliminated.  If WillInvertAllUses | 
|  | 182 | /// is true, work under the assumption that the caller intends to remove all | 
|  | 183 | /// uses of V and only keep uses of ~V. | 
| Roman Lebedev | 2635c32 | 2019-08-13 12:49:06 +0000 | [diff] [blame] | 184 | /// | 
|  | 185 | /// See also: canFreelyInvertAllUsersOf() | 
| Roman Lebedev | 0410489 | 2019-08-13 12:49:16 +0000 | [diff] [blame] | 186 | static inline bool isFreeToInvert(Value *V, bool WillInvertAllUses) { | 
| Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 187 | // ~(~(X)) -> X. | 
| Sanjay Patel | 95790c5 | 2018-10-23 16:54:28 +0000 | [diff] [blame] | 188 | if (match(V, m_Not(m_Value()))) | 
| Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 189 | return true; | 
|  | 190 |  | 
|  | 191 | // Constants can be considered to be not'ed values. | 
| Roman Lebedev | 3a94765 | 2019-07-22 22:09:24 +0000 | [diff] [blame] | 192 | if (match(V, m_AnyIntegralConstant())) | 
| Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 193 | return true; | 
|  | 194 |  | 
|  | 195 | // Compares can be inverted if all of their uses are being modified to use the | 
|  | 196 | // ~V. | 
|  | 197 | if (isa<CmpInst>(V)) | 
|  | 198 | return WillInvertAllUses; | 
|  | 199 |  | 
|  | 200 | // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1 | 
|  | 201 | // - Constant) - A` if we are willing to invert all of the uses. | 
|  | 202 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V)) | 
|  | 203 | if (BO->getOpcode() == Instruction::Add || | 
|  | 204 | BO->getOpcode() == Instruction::Sub) | 
|  | 205 | if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1))) | 
|  | 206 | return WillInvertAllUses; | 
|  | 207 |  | 
| David Green | 1e44c3b | 2018-10-02 09:48:34 +0000 | [diff] [blame] | 208 | // Selects with invertible operands are freely invertible | 
|  | 209 | if (match(V, m_Select(m_Value(), m_Not(m_Value()), m_Not(m_Value())))) | 
|  | 210 | return WillInvertAllUses; | 
|  | 211 |  | 
| Sanjoy Das | 82ea3d4 | 2015-02-24 00:08:41 +0000 | [diff] [blame] | 212 | return false; | 
|  | 213 | } | 
|  | 214 |  | 
| Roman Lebedev | 2635c32 | 2019-08-13 12:49:06 +0000 | [diff] [blame] | 215 | /// Given i1 V, can every user of V be freely adapted if V is changed to !V ? | 
|  | 216 | /// | 
| Roman Lebedev | 0410489 | 2019-08-13 12:49:16 +0000 | [diff] [blame] | 217 | /// See also: isFreeToInvert() | 
| Roman Lebedev | 2635c32 | 2019-08-13 12:49:06 +0000 | [diff] [blame] | 218 | static inline bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) { | 
|  | 219 | // Look at every user of V. | 
|  | 220 | for (User *U : V->users()) { | 
|  | 221 | if (U == IgnoredUser) | 
|  | 222 | continue; // Don't consider this user. | 
|  | 223 |  | 
|  | 224 | auto *I = cast<Instruction>(U); | 
|  | 225 | switch (I->getOpcode()) { | 
|  | 226 | case Instruction::Select: | 
|  | 227 | case Instruction::Br: | 
|  | 228 | break; // Free to invert by swapping true/false values/destinations. | 
|  | 229 | case Instruction::Xor: // Can invert 'xor' if it's a 'not', by ignoring it. | 
|  | 230 | if (!match(I, m_Not(m_Value()))) | 
|  | 231 | return false; // Not a 'not'. | 
|  | 232 | break; | 
|  | 233 | default: | 
|  | 234 | return false; // Don't know, likely not freely invertible. | 
|  | 235 | } | 
|  | 236 | // So far all users were free to invert... | 
|  | 237 | } | 
|  | 238 | return true; // Can freely invert all users! | 
|  | 239 | } | 
|  | 240 |  | 
| Sanjay Patel | a627253 | 2018-07-09 16:16:51 +0000 | [diff] [blame] | 241 | /// Some binary operators require special handling to avoid poison and undefined | 
| Sanjay Patel | 3e5c051 | 2018-06-21 14:59:35 +0000 | [diff] [blame] | 242 | /// behavior. If a constant vector has undef elements, replace those undefs with | 
| Sanjay Patel | 69faf464 | 2018-07-09 23:22:47 +0000 | [diff] [blame] | 243 | /// identity constants if possible because those are always safe to execute. | 
|  | 244 | /// If no identity constant exists, replace undef with some other safe constant. | 
| Sanjay Patel | a627253 | 2018-07-09 16:16:51 +0000 | [diff] [blame] | 245 | static inline Constant *getSafeVectorConstantForBinop( | 
| Sanjay Patel | 69faf464 | 2018-07-09 23:22:47 +0000 | [diff] [blame] | 246 | BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant) { | 
|  | 247 | assert(In->getType()->isVectorTy() && "Not expecting scalars here"); | 
| Sanjay Patel | 3e5c051 | 2018-06-21 14:59:35 +0000 | [diff] [blame] | 248 |  | 
| Sanjay Patel | 69faf464 | 2018-07-09 23:22:47 +0000 | [diff] [blame] | 249 | Type *EltTy = In->getType()->getVectorElementType(); | 
|  | 250 | auto *SafeC = ConstantExpr::getBinOpIdentity(Opcode, EltTy, IsRHSConstant); | 
|  | 251 | if (!SafeC) { | 
|  | 252 | // TODO: Should this be available as a constant utility function? It is | 
|  | 253 | // similar to getBinOpAbsorber(). | 
|  | 254 | if (IsRHSConstant) { | 
|  | 255 | switch (Opcode) { | 
|  | 256 | case Instruction::SRem: // X % 1 = 0 | 
|  | 257 | case Instruction::URem: // X %u 1 = 0 | 
|  | 258 | SafeC = ConstantInt::get(EltTy, 1); | 
|  | 259 | break; | 
|  | 260 | case Instruction::FRem: // X % 1.0 (doesn't simplify, but it is safe) | 
|  | 261 | SafeC = ConstantFP::get(EltTy, 1.0); | 
|  | 262 | break; | 
|  | 263 | default: | 
|  | 264 | llvm_unreachable("Only rem opcodes have no identity constant for RHS"); | 
|  | 265 | } | 
|  | 266 | } else { | 
|  | 267 | switch (Opcode) { | 
|  | 268 | case Instruction::Shl:  // 0 << X = 0 | 
|  | 269 | case Instruction::LShr: // 0 >>u X = 0 | 
|  | 270 | case Instruction::AShr: // 0 >> X = 0 | 
|  | 271 | case Instruction::SDiv: // 0 / X = 0 | 
|  | 272 | case Instruction::UDiv: // 0 /u X = 0 | 
|  | 273 | case Instruction::SRem: // 0 % X = 0 | 
|  | 274 | case Instruction::URem: // 0 %u X = 0 | 
|  | 275 | case Instruction::Sub:  // 0 - X (doesn't simplify, but it is safe) | 
|  | 276 | case Instruction::FSub: // 0.0 - X (doesn't simplify, but it is safe) | 
|  | 277 | case Instruction::FDiv: // 0.0 / X (doesn't simplify, but it is safe) | 
|  | 278 | case Instruction::FRem: // 0.0 % X = 0 | 
|  | 279 | SafeC = Constant::getNullValue(EltTy); | 
|  | 280 | break; | 
|  | 281 | default: | 
|  | 282 | llvm_unreachable("Expected to find identity constant for opcode"); | 
|  | 283 | } | 
|  | 284 | } | 
|  | 285 | } | 
|  | 286 | assert(SafeC && "Must have safe constant for binop"); | 
|  | 287 | unsigned NumElts = In->getType()->getVectorNumElements(); | 
| Sanjay Patel | 3e5c051 | 2018-06-21 14:59:35 +0000 | [diff] [blame] | 288 | SmallVector<Constant *, 16> Out(NumElts); | 
|  | 289 | for (unsigned i = 0; i != NumElts; ++i) { | 
|  | 290 | Constant *C = In->getAggregateElement(i); | 
| Sanjay Patel | 69faf464 | 2018-07-09 23:22:47 +0000 | [diff] [blame] | 291 | Out[i] = isa<UndefValue>(C) ? SafeC : C; | 
| Sanjay Patel | 3e5c051 | 2018-06-21 14:59:35 +0000 | [diff] [blame] | 292 | } | 
|  | 293 | return ConstantVector::get(Out); | 
|  | 294 | } | 
|  | 295 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 296 | /// The core instruction combiner logic. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 297 | /// | 
|  | 298 | /// This class provides both the logic to recursively visit instructions and | 
| Craig Topper | 28ec346 | 2016-12-28 03:12:42 +0000 | [diff] [blame] | 299 | /// combine them. | 
| Duncan Sands | 6c5e435 | 2010-05-11 20:16:09 +0000 | [diff] [blame] | 300 | class LLVM_LIBRARY_VISIBILITY InstCombiner | 
| Chandler Carruth | 1edb9d6 | 2015-01-20 22:44:35 +0000 | [diff] [blame] | 301 | : public InstVisitor<InstCombiner, Instruction *> { | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 302 | // FIXME: These members shouldn't be public. | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 303 | public: | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 304 | /// A worklist of the instructions that need to be simplified. | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 305 | InstCombineWorklist &Worklist; | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 306 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 307 | /// An IRBuilder that automatically inserts new instructions into the | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 308 | /// worklist. | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 309 | using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>; | 
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 310 | BuilderTy &Builder; | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 311 |  | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 312 | private: | 
|  | 313 | // Mode in which we are running the combiner. | 
|  | 314 | const bool MinimizeSize; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 315 |  | 
| Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 316 | /// Enable combines that trigger rarely but are costly in compiletime. | 
|  | 317 | const bool ExpensiveCombines; | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 318 |  | 
| Bjorn Steinbrink | 8350534 | 2015-07-10 06:55:49 +0000 | [diff] [blame] | 319 | AliasAnalysis *AA; | 
|  | 320 |  | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 321 | // Required analyses. | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 322 | AssumptionCache &AC; | 
| Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 323 | TargetLibraryInfo &TLI; | 
|  | 324 | DominatorTree &DT; | 
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 325 | const DataLayout &DL; | 
| Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 326 | const SimplifyQuery SQ; | 
| Adam Nemet | ea06e6e | 2017-07-26 19:03:18 +0000 | [diff] [blame] | 327 | OptimizationRemarkEmitter &ORE; | 
| Hiroshi Yamauchi | 09e539f | 2019-04-15 16:49:00 +0000 | [diff] [blame] | 328 | BlockFrequencyInfo *BFI; | 
|  | 329 | ProfileSummaryInfo *PSI; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 330 |  | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 331 | // Optional analyses. When non-null, these can both be used to do better | 
|  | 332 | // combining and will be updated to reflect any changes. | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 333 | LoopInfo *LI; | 
|  | 334 |  | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 335 | bool MadeIRChange = false; | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 336 |  | 
|  | 337 | public: | 
| Craig Topper | bb4069e | 2017-07-07 23:16:26 +0000 | [diff] [blame] | 338 | InstCombiner(InstCombineWorklist &Worklist, BuilderTy &Builder, | 
| Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 339 | bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA, | 
| Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 340 | AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT, | 
| Hiroshi Yamauchi | 09e539f | 2019-04-15 16:49:00 +0000 | [diff] [blame] | 341 | OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, | 
|  | 342 | ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI) | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 343 | : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize), | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 344 | ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT), | 
| Hiroshi Yamauchi | 09e539f | 2019-04-15 16:49:00 +0000 | [diff] [blame] | 345 | DL(DL), SQ(DL, &TLI, &DT, &AC), ORE(ORE), BFI(BFI), PSI(PSI), LI(LI) {} | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 346 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 347 | /// Run the combiner over the entire worklist until it is empty. | 
| Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 348 | /// | 
|  | 349 | /// \returns true if the IR is changed. | 
|  | 350 | bool run(); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 351 |  | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 352 | AssumptionCache &getAssumptionCache() const { return AC; } | 
|  | 353 |  | 
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 354 | const DataLayout &getDataLayout() const { return DL; } | 
| Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 355 |  | 
| Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 356 | DominatorTree &getDominatorTree() const { return DT; } | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 357 |  | 
| Chandler Carruth | 5175b9a | 2015-01-20 08:35:24 +0000 | [diff] [blame] | 358 | LoopInfo *getLoopInfo() const { return LI; } | 
|  | 359 |  | 
| Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 360 | TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; } | 
| Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 361 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 362 | // Visitation implementation - Implement instruction combining for different | 
|  | 363 | // instruction types.  The semantics are as follows: | 
|  | 364 | // Return Value: | 
|  | 365 | //    null        - No change was made | 
|  | 366 | //     I          - Change was made, I is still valid, I may be dead though | 
|  | 367 | //   otherwise    - Change was made, replace I with returned instruction | 
|  | 368 | // | 
| Cameron McInally | e75412a | 2019-05-10 20:01:04 +0000 | [diff] [blame] | 369 | Instruction *visitFNeg(UnaryOperator &I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 370 | Instruction *visitAdd(BinaryOperator &I); | 
|  | 371 | Instruction *visitFAdd(BinaryOperator &I); | 
| Nikita Popov | 0e322c8 | 2020-01-01 11:11:05 +0100 | [diff] [blame] | 372 | Value *OptimizePointerDifference( | 
|  | 373 | Value *LHS, Value *RHS, Type *Ty, bool isNUW); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 374 | Instruction *visitSub(BinaryOperator &I); | 
|  | 375 | Instruction *visitFSub(BinaryOperator &I); | 
|  | 376 | Instruction *visitMul(BinaryOperator &I); | 
|  | 377 | Instruction *visitFMul(BinaryOperator &I); | 
|  | 378 | Instruction *visitURem(BinaryOperator &I); | 
|  | 379 | Instruction *visitSRem(BinaryOperator &I); | 
|  | 380 | Instruction *visitFRem(BinaryOperator &I); | 
| Sanjay Patel | ae2e3a4 | 2017-10-06 23:20:16 +0000 | [diff] [blame] | 381 | bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 382 | Instruction *commonRemTransforms(BinaryOperator &I); | 
|  | 383 | Instruction *commonIRemTransforms(BinaryOperator &I); | 
|  | 384 | Instruction *commonDivTransforms(BinaryOperator &I); | 
|  | 385 | Instruction *commonIDivTransforms(BinaryOperator &I); | 
|  | 386 | Instruction *visitUDiv(BinaryOperator &I); | 
|  | 387 | Instruction *visitSDiv(BinaryOperator &I); | 
| Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 388 | Instruction *visitFDiv(BinaryOperator &I); | 
| Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 389 | Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 390 | Instruction *visitAnd(BinaryOperator &I); | 
| Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 391 | Instruction *visitOr(BinaryOperator &I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 392 | Instruction *visitXor(BinaryOperator &I); | 
|  | 393 | Instruction *visitShl(BinaryOperator &I); | 
| Roman Lebedev | 49483a3 | 2019-10-20 19:38:50 +0000 | [diff] [blame] | 394 | Value *reassociateShiftAmtsOfTwoSameDirectionShifts( | 
|  | 395 | BinaryOperator *Sh0, const SimplifyQuery &SQ, | 
|  | 396 | bool AnalyzeForSignBitExtraction = false); | 
| Roman Lebedev | 7015a5c | 2019-10-20 20:52:06 +0000 | [diff] [blame] | 397 | Instruction *canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract( | 
|  | 398 | BinaryOperator &I); | 
| Roman Lebedev | ae3315a | 2019-10-02 23:02:12 +0000 | [diff] [blame] | 399 | Instruction *foldVariableSignZeroExtensionOfVariableHighBitExtract( | 
|  | 400 | BinaryOperator &OldAShr); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 401 | Instruction *visitAShr(BinaryOperator &I); | 
|  | 402 | Instruction *visitLShr(BinaryOperator &I); | 
|  | 403 | Instruction *commonShiftTransforms(BinaryOperator &I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 404 | Instruction *visitFCmpInst(FCmpInst &I); | 
|  | 405 | Instruction *visitICmpInst(ICmpInst &I); | 
| Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 406 | Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1, | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 407 | BinaryOperator &I); | 
|  | 408 | Instruction *commonCastTransforms(CastInst &CI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 409 | Instruction *commonPointerCastTransforms(CastInst &CI); | 
|  | 410 | Instruction *visitTrunc(TruncInst &CI); | 
|  | 411 | Instruction *visitZExt(ZExtInst &CI); | 
|  | 412 | Instruction *visitSExt(SExtInst &CI); | 
|  | 413 | Instruction *visitFPTrunc(FPTruncInst &CI); | 
|  | 414 | Instruction *visitFPExt(CastInst &CI); | 
|  | 415 | Instruction *visitFPToUI(FPToUIInst &FI); | 
|  | 416 | Instruction *visitFPToSI(FPToSIInst &FI); | 
|  | 417 | Instruction *visitUIToFP(CastInst &CI); | 
|  | 418 | Instruction *visitSIToFP(CastInst &CI); | 
|  | 419 | Instruction *visitPtrToInt(PtrToIntInst &CI); | 
|  | 420 | Instruction *visitIntToPtr(IntToPtrInst &CI); | 
|  | 421 | Instruction *visitBitCast(BitCastInst &CI); | 
| Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 422 | Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI); | 
| Mehdi Amini | b9a0fa4 | 2015-02-16 21:47:54 +0000 | [diff] [blame] | 423 | Instruction *FoldItoFPtoI(Instruction &FI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 424 | Instruction *visitSelectInst(SelectInst &SI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 425 | Instruction *visitCallInst(CallInst &CI); | 
|  | 426 | Instruction *visitInvokeInst(InvokeInst &II); | 
| Craig Topper | 784929d | 2019-02-08 20:48:56 +0000 | [diff] [blame] | 427 | Instruction *visitCallBrInst(CallBrInst &CBI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 428 |  | 
|  | 429 | Instruction *SliceUpIllegalIntegerPHI(PHINode &PN); | 
|  | 430 | Instruction *visitPHINode(PHINode &PN); | 
|  | 431 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); | 
|  | 432 | Instruction *visitAllocaInst(AllocaInst &AI); | 
| Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 433 | Instruction *visitAllocSite(Instruction &FI); | 
| Gabor Greif | 75f6943 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 434 | Instruction *visitFree(CallInst &FI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 435 | Instruction *visitLoadInst(LoadInst &LI); | 
|  | 436 | Instruction *visitStoreInst(StoreInst &SI); | 
| Quentin Colombet | 96f54de | 2019-02-07 21:27:23 +0000 | [diff] [blame] | 437 | Instruction *visitAtomicRMWInst(AtomicRMWInst &SI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 438 | Instruction *visitBranchInst(BranchInst &BI); | 
| Davide Italiano | aec4617 | 2017-01-31 18:09:05 +0000 | [diff] [blame] | 439 | Instruction *visitFenceInst(FenceInst &FI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 440 | Instruction *visitSwitchInst(SwitchInst &SI); | 
| Hal Finkel | 93873cc1 | 2014-09-07 21:28:34 +0000 | [diff] [blame] | 441 | Instruction *visitReturnInst(ReturnInst &RI); | 
| Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 442 | Instruction *visitInsertValueInst(InsertValueInst &IV); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 443 | Instruction *visitInsertElementInst(InsertElementInst &IE); | 
|  | 444 | Instruction *visitExtractElementInst(ExtractElementInst &EI); | 
|  | 445 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); | 
|  | 446 | Instruction *visitExtractValueInst(ExtractValueInst &EV); | 
| Duncan Sands | 5c05579 | 2011-09-30 13:12:16 +0000 | [diff] [blame] | 447 | Instruction *visitLandingPadInst(LandingPadInst &LI); | 
| Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 448 | Instruction *visitVAStartInst(VAStartInst &I); | 
|  | 449 | Instruction *visitVACopyInst(VACopyInst &I); | 
| aqjune | 4187cb1 | 2019-11-07 01:12:17 +0900 | [diff] [blame] | 450 | Instruction *visitFreeze(FreezeInst &I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 451 |  | 
| Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 452 | /// Specify what to return for unhandled instructions. | 
| Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 453 | Instruction *visitInstruction(Instruction &I) { return nullptr; } | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 454 |  | 
| Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 455 | /// True when DB dominates all uses of DI except UI. | 
|  | 456 | /// UI must be in the same block as DI. | 
|  | 457 | /// The routine checks that the DI parent and DB are different. | 
| Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 458 | bool dominatesAllUses(const Instruction *DI, const Instruction *UI, | 
|  | 459 | const BasicBlock *DB) const; | 
|  | 460 |  | 
| Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 461 | /// Try to replace select with select operand SIOpd in SI-ICmp sequence. | 
| Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 462 | bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, | 
|  | 463 | const unsigned SIOpd); | 
|  | 464 |  | 
| Yaxun Liu | ba01ed0 | 2017-02-10 21:46:07 +0000 | [diff] [blame] | 465 | /// Try to replace instruction \p I with value \p V which are pointers | 
|  | 466 | /// in different address space. | 
|  | 467 | /// \return true if successful. | 
|  | 468 | bool replacePointer(Instruction &I, Value *V); | 
|  | 469 |  | 
| Nikita Popov | b4dd928 | 2020-01-13 18:56:28 +0100 | [diff] [blame] | 470 | LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy, | 
|  | 471 | const Twine &Suffix = ""); | 
|  | 472 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 473 | private: | 
| Sanjay Patel | 2217f75 | 2017-01-31 17:25:42 +0000 | [diff] [blame] | 474 | bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const; | 
|  | 475 | bool shouldChangeType(Type *From, Type *To) const; | 
| Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 476 | Value *dyn_castNegVal(Value *V) const; | 
| Nikita Popov | 0b83c5a | 2020-01-18 16:54:43 +0100 | [diff] [blame] | 477 | Value *freelyNegateValue(Value *V); | 
| David Blaikie | 87ca1b6 | 2015-03-27 20:56:11 +0000 | [diff] [blame] | 478 | Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset, | 
| Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 479 | SmallVectorImpl<Value *> &NewIndices); | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 480 |  | 
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 481 | /// Classify whether a cast is worth optimizing. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 482 | /// | 
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 483 | /// This is a helper to decide whether the simplification of | 
|  | 484 | /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed. | 
|  | 485 | /// | 
|  | 486 | /// \param CI The cast we are interested in. | 
|  | 487 | /// | 
|  | 488 | /// \return true if this cast actually results in any code being generated and | 
|  | 489 | /// if it cannot already be eliminated by some other transformation. | 
|  | 490 | bool shouldOptimizeCast(CastInst *CI); | 
| Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 491 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 492 | /// Try to optimize a sequence of instructions checking if an operation | 
| Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 493 | /// on LHS and RHS overflows. | 
|  | 494 | /// | 
| Sanjoy Das | 6f5dca7 | 2015-08-28 19:09:31 +0000 | [diff] [blame] | 495 | /// If this overflow check is done via one of the overflow check intrinsics, | 
|  | 496 | /// then CtxI has to be the call instruction calling that intrinsic.  If this | 
|  | 497 | /// overflow check is done by arithmetic followed by a compare, then CtxI has | 
|  | 498 | /// to be the arithmetic instruction. | 
|  | 499 | /// | 
| Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 500 | /// If a simplification is possible, stores the simplified result of the | 
|  | 501 | /// operation in OperationResult and result of the overflow check in | 
|  | 502 | /// OverflowResult, and return true.  If no simplification is possible, | 
|  | 503 | /// returns false. | 
| Nikita Popov | 352f598 | 2019-05-26 11:43:31 +0000 | [diff] [blame] | 504 | bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned, | 
|  | 505 | Value *LHS, Value *RHS, | 
| Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 506 | Instruction &CtxI, Value *&OperationResult, | 
|  | 507 | Constant *&OverflowResult); | 
|  | 508 |  | 
| Craig Topper | c1892ec | 2019-01-31 17:23:29 +0000 | [diff] [blame] | 509 | Instruction *visitCallBase(CallBase &Call); | 
| Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 510 | Instruction *tryOptimizeCall(CallInst *CI); | 
| Craig Topper | c1892ec | 2019-01-31 17:23:29 +0000 | [diff] [blame] | 511 | bool transformConstExprCastCall(CallBase &Call); | 
|  | 512 | Instruction *transformCallThroughTrampoline(CallBase &Call, | 
|  | 513 | IntrinsicInst &Tramp); | 
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 514 |  | 
| Philip Reames | 7c8647b | 2019-04-25 01:18:56 +0000 | [diff] [blame] | 515 | Value *simplifyMaskedLoad(IntrinsicInst &II); | 
| Philip Reames | e4588bb | 2019-03-20 18:44:58 +0000 | [diff] [blame] | 516 | Instruction *simplifyMaskedStore(IntrinsicInst &II); | 
| Philip Reames | 7c8647b | 2019-04-25 01:18:56 +0000 | [diff] [blame] | 517 | Instruction *simplifyMaskedGather(IntrinsicInst &II); | 
| Philip Reames | e4588bb | 2019-03-20 18:44:58 +0000 | [diff] [blame] | 518 | Instruction *simplifyMaskedScatter(IntrinsicInst &II); | 
|  | 519 |  | 
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 520 | /// Transform (zext icmp) to bitwise / integer operations in order to | 
|  | 521 | /// eliminate it. | 
|  | 522 | /// | 
|  | 523 | /// \param ICI The icmp of the (zext icmp) pair we are interested in. | 
|  | 524 | /// \parem CI The zext of the (zext icmp) pair we are interested in. | 
|  | 525 | /// \param DoTransform Pass false to just test whether the given (zext icmp) | 
|  | 526 | /// would be transformed. Pass true to actually perform the transformation. | 
|  | 527 | /// | 
|  | 528 | /// \return null if the transformation cannot be performed. If the | 
|  | 529 | /// transformation can be performed the new instruction that replaces the | 
|  | 530 | /// (zext icmp) pair will be returned (if \p DoTransform is false the | 
|  | 531 | /// unmodified \p ICI will be returned in this case). | 
|  | 532 | Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI, | 
|  | 533 | bool DoTransform = true); | 
|  | 534 |  | 
| Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 535 | Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI); | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 536 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 537 | bool willNotOverflowSignedAdd(const Value *LHS, const Value *RHS, | 
|  | 538 | const Instruction &CxtI) const { | 
| Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 539 | return computeOverflowForSignedAdd(LHS, RHS, &CxtI) == | 
|  | 540 | OverflowResult::NeverOverflows; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 541 | } | 
|  | 542 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 543 | bool willNotOverflowUnsignedAdd(const Value *LHS, const Value *RHS, | 
|  | 544 | const Instruction &CxtI) const { | 
| Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 545 | return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) == | 
|  | 546 | OverflowResult::NeverOverflows; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 547 | } | 
|  | 548 |  | 
| Sanjay Patel | 46945b9 | 2018-09-14 21:30:07 +0000 | [diff] [blame] | 549 | bool willNotOverflowAdd(const Value *LHS, const Value *RHS, | 
|  | 550 | const Instruction &CxtI, bool IsSigned) const { | 
|  | 551 | return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI) | 
|  | 552 | : willNotOverflowUnsignedAdd(LHS, RHS, CxtI); | 
|  | 553 | } | 
|  | 554 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 555 | bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS, | 
| Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 556 | const Instruction &CxtI) const { | 
|  | 557 | return computeOverflowForSignedSub(LHS, RHS, &CxtI) == | 
|  | 558 | OverflowResult::NeverOverflows; | 
|  | 559 | } | 
|  | 560 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 561 | bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS, | 
| Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 562 | const Instruction &CxtI) const { | 
|  | 563 | return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) == | 
|  | 564 | OverflowResult::NeverOverflows; | 
|  | 565 | } | 
|  | 566 |  | 
| Sanjay Patel | 46945b9 | 2018-09-14 21:30:07 +0000 | [diff] [blame] | 567 | bool willNotOverflowSub(const Value *LHS, const Value *RHS, | 
|  | 568 | const Instruction &CxtI, bool IsSigned) const { | 
|  | 569 | return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI) | 
|  | 570 | : willNotOverflowUnsignedSub(LHS, RHS, CxtI); | 
|  | 571 | } | 
|  | 572 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 573 | bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS, | 
| Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 574 | const Instruction &CxtI) const { | 
|  | 575 | return computeOverflowForSignedMul(LHS, RHS, &CxtI) == | 
|  | 576 | OverflowResult::NeverOverflows; | 
|  | 577 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 578 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 579 | bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS, | 
|  | 580 | const Instruction &CxtI) const { | 
| Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 581 | return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) == | 
|  | 582 | OverflowResult::NeverOverflows; | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 583 | } | 
|  | 584 |  | 
| Sanjay Patel | 46945b9 | 2018-09-14 21:30:07 +0000 | [diff] [blame] | 585 | bool willNotOverflowMul(const Value *LHS, const Value *RHS, | 
|  | 586 | const Instruction &CxtI, bool IsSigned) const { | 
|  | 587 | return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI) | 
|  | 588 | : willNotOverflowUnsignedMul(LHS, RHS, CxtI); | 
|  | 589 | } | 
|  | 590 |  | 
| Sanjay Patel | 90a3634 | 2018-09-14 22:23:35 +0000 | [diff] [blame] | 591 | bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS, | 
|  | 592 | const Value *RHS, const Instruction &CxtI, | 
|  | 593 | bool IsSigned) const { | 
|  | 594 | switch (Opcode) { | 
|  | 595 | case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned); | 
|  | 596 | case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned); | 
|  | 597 | case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned); | 
|  | 598 | default: llvm_unreachable("Unexpected opcode for overflow query"); | 
|  | 599 | } | 
|  | 600 | } | 
|  | 601 |  | 
| Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 602 | Value *EmitGEPOffset(User *GEP); | 
| Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 603 | Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN); | 
| Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 604 | Instruction *foldCastedBitwiseLogic(BinaryOperator &I); | 
| Sanjay Patel | 94da1de | 2017-08-05 15:19:18 +0000 | [diff] [blame] | 605 | Instruction *narrowBinOp(TruncInst &Trunc); | 
| Sanjay Patel | 1d68112 | 2018-01-25 16:34:36 +0000 | [diff] [blame] | 606 | Instruction *narrowMaskedBinOp(BinaryOperator &And); | 
| Sanjay Patel | 90a3634 | 2018-09-14 22:23:35 +0000 | [diff] [blame] | 607 | Instruction *narrowMathIfNoOverflow(BinaryOperator &I); | 
| Sanjay Patel | c50e55d | 2017-08-09 18:37:41 +0000 | [diff] [blame] | 608 | Instruction *narrowRotate(TruncInst &Trunc); | 
| Guozhi Wei | ae541f6 | 2016-10-25 20:43:42 +0000 | [diff] [blame] | 609 | Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN); | 
| David Green | 186155b | 2019-10-22 15:39:47 +0000 | [diff] [blame] | 610 | Instruction *matchSAddSubSat(SelectInst &MinMax1); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 611 |  | 
| Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 612 | /// Determine if a pair of casts can be replaced by a single cast. | 
|  | 613 | /// | 
|  | 614 | /// \param CI1 The first of a pair of casts. | 
|  | 615 | /// \param CI2 The second of a pair of casts. | 
|  | 616 | /// | 
|  | 617 | /// \return 0 if the cast pair cannot be eliminated, otherwise returns an | 
|  | 618 | /// Instruction::CastOps value for a cast that can replace the pair, casting | 
|  | 619 | /// CI1->getSrcTy() to CI2->getDstTy(). | 
|  | 620 | /// | 
|  | 621 | /// \see CastInst::isEliminableCastPair | 
|  | 622 | Instruction::CastOps isEliminableCastPair(const CastInst *CI1, | 
|  | 623 | const CastInst *CI2); | 
|  | 624 |  | 
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 625 | Value *foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI); | 
| Craig Topper | f2d3e6d | 2017-06-15 19:09:51 +0000 | [diff] [blame] | 626 | Value *foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI); | 
| Roman Lebedev | 2635c32 | 2019-08-13 12:49:06 +0000 | [diff] [blame] | 627 | Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &I); | 
| Sanjay Patel | 5e456b9 | 2017-05-18 20:53:16 +0000 | [diff] [blame] | 628 |  | 
| Sanjay Patel | 64fc5da | 2017-09-02 17:53:33 +0000 | [diff] [blame] | 629 | /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp). | 
|  | 630 | /// NOTE: Unlike most of instcombine, this returns a Value which should | 
|  | 631 | /// already be inserted into the function. | 
|  | 632 | Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd); | 
|  | 633 |  | 
| Craig Topper | da6ea0d | 2017-06-16 05:10:37 +0000 | [diff] [blame] | 634 | Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS, | 
|  | 635 | bool JoinedByAnd, Instruction &CxtI); | 
| Sanjay Patel | 3b20630 | 2018-10-24 15:17:56 +0000 | [diff] [blame] | 636 | Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D); | 
|  | 637 | Value *getSelectCondition(Value *A, Value *B); | 
|  | 638 |  | 
| Nikita Popov | 884feb1 | 2019-03-06 18:30:00 +0000 | [diff] [blame] | 639 | Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II); | 
|  | 640 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 641 | public: | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 642 | /// Inserts an instruction \p New before instruction \p Old | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 643 | /// | 
|  | 644 | /// Also adds the new instruction to the worklist and returns \p New so that | 
|  | 645 | /// it is suitable for use as the return from the visitation patterns. | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 646 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { | 
| Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 647 | assert(New && !New->getParent() && | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 648 | "New instruction already inserted into a basic block!"); | 
|  | 649 | BasicBlock *BB = Old.getParent(); | 
| Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 650 | BB->getInstList().insert(Old.getIterator(), New); // Insert inst | 
| Nikita Popov | e6c9ab4 | 2020-01-30 22:32:46 +0100 | [diff] [blame^] | 651 | Worklist.push(New); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 652 | return New; | 
|  | 653 | } | 
| Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 654 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 655 | /// Same as InsertNewInstBefore, but also sets the debug loc. | 
| Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 656 | Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) { | 
|  | 657 | New->setDebugLoc(Old.getDebugLoc()); | 
|  | 658 | return InsertNewInstBefore(New, Old); | 
|  | 659 | } | 
|  | 660 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 661 | /// A combiner-aware RAUW-like routine. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 662 | /// | 
|  | 663 | /// 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] | 664 | /// replaceable with another preexisting expression. Here we add all uses of | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 665 | /// I to the worklist, replace all uses of I with the new value, then return | 
|  | 666 | /// I, so that the inst combiner will know that I was modified. | 
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 667 | Instruction *replaceInstUsesWith(Instruction &I, Value *V) { | 
| Owen Anderson | 51b75b8c | 2015-03-10 05:13:47 +0000 | [diff] [blame] | 668 | // If there are no uses to replace, then we return nullptr to indicate that | 
|  | 669 | // no changes were made to the program. | 
|  | 670 | if (I.use_empty()) return nullptr; | 
|  | 671 |  | 
| Nikita Popov | e6c9ab4 | 2020-01-30 22:32:46 +0100 | [diff] [blame^] | 672 | Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist. | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 673 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 674 | // If we are replacing the instruction with itself, this must be in a | 
|  | 675 | // segment of unreachable code, so just clobber the instruction. | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 676 | if (&I == V) | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 677 | V = UndefValue::get(I.getType()); | 
| Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 678 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 679 | LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n" | 
|  | 680 | << "    with " << *V << '\n'); | 
| Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 681 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 682 | I.replaceAllUsesWith(V); | 
|  | 683 | return &I; | 
|  | 684 | } | 
|  | 685 |  | 
| Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 686 | /// 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] | 687 | /// \p Result and a constant \p Overflow value. | 
| Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 688 | Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result, | 
| Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 689 | Constant *Overflow) { | 
|  | 690 | Constant *V[] = {UndefValue::get(Result->getType()), Overflow}; | 
| Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 691 | StructType *ST = cast<StructType>(II->getType()); | 
|  | 692 | Constant *Struct = ConstantStruct::get(ST, V); | 
|  | 693 | return InsertValueInst::Create(Struct, Result, 0); | 
|  | 694 | } | 
| Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 695 |  | 
| Philip Reames | 8867971 | 2019-04-17 17:37:58 +0000 | [diff] [blame] | 696 | /// Create and insert the idiom we use to indicate a block is unreachable | 
|  | 697 | /// without having to rewrite the CFG from within InstCombine. | 
|  | 698 | void CreateNonTerminatorUnreachable(Instruction *InsertAt) { | 
|  | 699 | auto &Ctx = InsertAt->getContext(); | 
|  | 700 | new StoreInst(ConstantInt::getTrue(Ctx), | 
|  | 701 | UndefValue::get(Type::getInt1PtrTy(Ctx)), | 
|  | 702 | InsertAt); | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 706 | /// Combiner aware instruction erasure. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 707 | /// | 
|  | 708 | /// When dealing with an instruction that has side effects or produces a void | 
|  | 709 | /// value, we can't rely on DCE to delete the instruction. Instead, visit | 
|  | 710 | /// methods should return the value returned by this function. | 
| Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 711 | Instruction *eraseInstFromFunction(Instruction &I) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 712 | LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n'); | 
| Adrian Prantl | fa9e84e | 2017-03-16 20:11:54 +0000 | [diff] [blame] | 713 | assert(I.use_empty() && "Cannot erase instruction that is used!"); | 
| Vedant Kumar | a087b78 | 2019-11-07 11:19:41 -0800 | [diff] [blame] | 714 | salvageDebugInfoOrMarkUndef(I); | 
| Adrian Prantl | 47ea647 | 2017-03-16 21:14:09 +0000 | [diff] [blame] | 715 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 716 | // Make sure that we reprocess all operands now that we reduced their | 
|  | 717 | // use counts. | 
|  | 718 | if (I.getNumOperands() < 8) { | 
| Sanjay Patel | 8af7fbc3 | 2016-01-31 16:34:48 +0000 | [diff] [blame] | 719 | for (Use &Operand : I.operands()) | 
|  | 720 | if (auto *Inst = dyn_cast<Instruction>(Operand)) | 
| Nikita Popov | e6c9ab4 | 2020-01-30 22:32:46 +0100 | [diff] [blame^] | 721 | Worklist.push(Inst); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 722 | } | 
| Nikita Popov | e6c9ab4 | 2020-01-30 22:32:46 +0100 | [diff] [blame^] | 723 | Worklist.remove(&I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 724 | I.eraseFromParent(); | 
|  | 725 | MadeIRChange = true; | 
| Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 726 | return nullptr; // Don't do anything with FI | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 727 | } | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 728 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 729 | void computeKnownBits(const Value *V, KnownBits &Known, | 
|  | 730 | unsigned Depth, const Instruction *CxtI) const { | 
| Craig Topper | 26c4159 | 2017-05-15 02:30:27 +0000 | [diff] [blame] | 731 | llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 732 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 733 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 734 | KnownBits computeKnownBits(const Value *V, unsigned Depth, | 
|  | 735 | const Instruction *CxtI) const { | 
| Craig Topper | 1a36b7d | 2017-05-15 06:39:41 +0000 | [diff] [blame] | 736 | return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT); | 
|  | 737 | } | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 738 |  | 
| Craig Topper | d4039f7 | 2017-05-25 21:51:12 +0000 | [diff] [blame] | 739 | bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false, | 
|  | 740 | unsigned Depth = 0, | 
|  | 741 | const Instruction *CxtI = nullptr) { | 
|  | 742 | return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT); | 
|  | 743 | } | 
|  | 744 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 745 | bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0, | 
|  | 746 | const Instruction *CxtI = nullptr) const { | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 747 | return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 748 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 749 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 750 | unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0, | 
|  | 751 | const Instruction *CxtI = nullptr) const { | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 752 | return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 753 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 754 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 755 | OverflowResult computeOverflowForUnsignedMul(const Value *LHS, | 
|  | 756 | const Value *RHS, | 
|  | 757 | const Instruction *CxtI) const { | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 758 | return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT); | 
| David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 759 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 760 |  | 
| Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 761 | OverflowResult computeOverflowForSignedMul(const Value *LHS, | 
| Nikita Popov | 39f2beb | 2019-05-26 11:43:37 +0000 | [diff] [blame] | 762 | const Value *RHS, | 
| Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 763 | const Instruction *CxtI) const { | 
|  | 764 | return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT); | 
|  | 765 | } | 
|  | 766 |  | 
| Craig Topper | 2b1fc32 | 2017-05-22 06:25:31 +0000 | [diff] [blame] | 767 | OverflowResult computeOverflowForUnsignedAdd(const Value *LHS, | 
|  | 768 | const Value *RHS, | 
|  | 769 | const Instruction *CxtI) const { | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 770 | return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT); | 
| David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 771 | } | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 772 |  | 
| Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 773 | OverflowResult computeOverflowForSignedAdd(const Value *LHS, | 
|  | 774 | const Value *RHS, | 
|  | 775 | const Instruction *CxtI) const { | 
|  | 776 | return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT); | 
|  | 777 | } | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 778 |  | 
| Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 779 | OverflowResult computeOverflowForUnsignedSub(const Value *LHS, | 
|  | 780 | const Value *RHS, | 
|  | 781 | const Instruction *CxtI) const { | 
|  | 782 | return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT); | 
|  | 783 | } | 
|  | 784 |  | 
|  | 785 | OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, | 
|  | 786 | const Instruction *CxtI) const { | 
|  | 787 | return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT); | 
|  | 788 | } | 
|  | 789 |  | 
| Nikita Popov | 39f2beb | 2019-05-26 11:43:37 +0000 | [diff] [blame] | 790 | OverflowResult computeOverflow( | 
|  | 791 | Instruction::BinaryOps BinaryOp, bool IsSigned, | 
|  | 792 | Value *LHS, Value *RHS, Instruction *CxtI) const; | 
|  | 793 |  | 
| Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 794 | /// Maximum size of array considered when transforming. | 
| Simon Pilgrim | b569624 | 2019-08-28 15:19:49 +0000 | [diff] [blame] | 795 | uint64_t MaxArraySizeForCombine = 0; | 
| Davide Italiano | 2133bf5 | 2017-02-07 17:56:50 +0000 | [diff] [blame] | 796 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 797 | private: | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 798 | /// Performs a few simplifications for operators which are associative | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 799 | /// or commutative. | 
| Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 800 | bool SimplifyAssociativeOrCommutative(BinaryOperator &I); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 801 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 802 | /// Tries to simplify binary operations which some other binary | 
| Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 803 | /// operation distributes over. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 804 | /// | 
|  | 805 | /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)" | 
|  | 806 | /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A | 
|  | 807 | /// & (B | C) -> (A&B) | (A&C)" if this is a win).  Returns the simplified | 
|  | 808 | /// value, or null if it didn't simplify. | 
| Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 809 | Value *SimplifyUsingDistributiveLaws(BinaryOperator &I); | 
| Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 810 |  | 
| Sanjoy Das | 6f1937b | 2018-04-26 20:52:28 +0000 | [diff] [blame] | 811 | /// Tries to simplify add operations using the definition of remainder. | 
|  | 812 | /// | 
|  | 813 | /// The definition of remainder is X % C = X - (X / C ) * C. The add | 
|  | 814 | /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to | 
|  | 815 | /// X % (C0 * C1) | 
|  | 816 | Value *SimplifyAddWithRemainder(BinaryOperator &I); | 
|  | 817 |  | 
| Quentin Colombet | aa103b3 | 2017-09-20 17:32:16 +0000 | [diff] [blame] | 818 | // Binary Op helper for select operations where the expression can be | 
|  | 819 | // efficiently reorganized. | 
|  | 820 | Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS, | 
|  | 821 | Value *RHS); | 
|  | 822 |  | 
| Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 823 | /// This tries to simplify binary operations by factorizing out common terms | 
|  | 824 | /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)"). | 
| Craig Topper | 47c8f66 | 2017-07-06 18:35:52 +0000 | [diff] [blame] | 825 | Value *tryFactorization(BinaryOperator &, Instruction::BinaryOps, Value *, | 
|  | 826 | Value *, Value *, Value *); | 
| Daniel Berlin | 2c75c63 | 2017-04-26 20:56:07 +0000 | [diff] [blame] | 827 |  | 
| Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 828 | /// Match a select chain which produces one of three values based on whether | 
|  | 829 | /// the LHS is less than, equal to, or greater than RHS respectively. | 
|  | 830 | /// Return true if we matched a three way compare idiom. The LHS, RHS, Less, | 
|  | 831 | /// Equal and Greater values are saved in the matching process and returned to | 
|  | 832 | /// the caller. | 
|  | 833 | bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS, | 
|  | 834 | ConstantInt *&Less, ConstantInt *&Equal, | 
|  | 835 | ConstantInt *&Greater); | 
|  | 836 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 837 | /// Attempts to replace V with a simpler value based on the demanded | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 838 | /// bits. | 
| Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 839 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known, | 
|  | 840 | unsigned Depth, Instruction *CxtI); | 
| Craig Topper | 47596dd | 2017-03-25 06:52:52 +0000 | [diff] [blame] | 841 | bool SimplifyDemandedBits(Instruction *I, unsigned Op, | 
| Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 842 | const APInt &DemandedMask, KnownBits &Known, | 
|  | 843 | unsigned Depth = 0); | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 844 |  | 
| Craig Topper | b0076fe | 2017-04-12 18:05:21 +0000 | [diff] [blame] | 845 | /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne | 
|  | 846 | /// bits. It also tries to handle simplifications that can be done based on | 
|  | 847 | /// DemandedMask, but without modifying the Instruction. | 
|  | 848 | Value *SimplifyMultipleUseDemandedBits(Instruction *I, | 
|  | 849 | const APInt &DemandedMask, | 
| Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 850 | KnownBits &Known, | 
| Craig Topper | b0076fe | 2017-04-12 18:05:21 +0000 | [diff] [blame] | 851 | unsigned Depth, Instruction *CxtI); | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 852 |  | 
| Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 853 | /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded | 
|  | 854 | /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence. | 
| Sanjay Patel | cc663b8 | 2017-04-20 22:37:01 +0000 | [diff] [blame] | 855 | Value *simplifyShrShlDemandedBits( | 
| Sanjay Patel | c9485ca | 2017-04-20 22:33:54 +0000 | [diff] [blame] | 856 | Instruction *Shr, const APInt &ShrOp1, Instruction *Shl, | 
| Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 857 | const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known); | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 858 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 859 | /// Tries to simplify operands to an integer instruction based on its | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 860 | /// demanded bits. | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 861 | bool SimplifyDemandedInstructionBits(Instruction &Inst); | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 862 |  | 
| Nicolai Haehnle | b29ee70 | 2018-06-21 13:37:31 +0000 | [diff] [blame] | 863 | Value *simplifyAMDGCNMemoryIntrinsicDemanded(IntrinsicInst *II, | 
|  | 864 | APInt DemandedElts, | 
| Nicolai Haehnle | a69146e | 2019-02-04 21:24:19 +0000 | [diff] [blame] | 865 | int DmaskIdx = -1); | 
| Nicolai Haehnle | b29ee70 | 2018-06-21 13:37:31 +0000 | [diff] [blame] | 866 |  | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 867 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, | 
| Piotr Sobczak | a861c9a | 2019-10-21 08:12:47 +0000 | [diff] [blame] | 868 | APInt &UndefElts, unsigned Depth = 0, | 
|  | 869 | bool AllowMultipleUsers = false); | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 870 |  | 
| Sanjay Patel | bbc6d60 | 2018-06-02 16:27:44 +0000 | [diff] [blame] | 871 | /// Canonicalize the position of binops relative to shufflevector. | 
| Sanjay Patel | 79dceb2 | 2018-10-03 15:20:58 +0000 | [diff] [blame] | 872 | Instruction *foldVectorBinop(BinaryOperator &Inst); | 
| Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 873 |  | 
|  | 874 | /// Given a binary operator, cast instruction, or select which has a PHI node | 
|  | 875 | /// as operand #0, see if we can fold the instruction into the PHI (which is | 
|  | 876 | /// only possible if all operands to the PHI are constants). | 
| Craig Topper | fb71b7d | 2017-04-14 19:20:12 +0000 | [diff] [blame] | 877 | Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 878 |  | 
| Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 879 | /// Given an instruction with a select as one operand and a constant as the | 
|  | 880 | /// other operand, try to fold the binary operator into the select arguments. | 
|  | 881 | /// This also works for Cast instructions, which obviously do not have a | 
|  | 882 | /// second operand. | 
|  | 883 | Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); | 
|  | 884 |  | 
|  | 885 | /// This is a convenience wrapper function for the above two functions. | 
| Sanjay Patel | 8fdd87f | 2018-02-28 16:36:24 +0000 | [diff] [blame] | 886 | Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I); | 
| Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 887 |  | 
| Sanjay Patel | 8d810fe | 2017-10-13 16:43:58 +0000 | [diff] [blame] | 888 | Instruction *foldAddWithConstant(BinaryOperator &Add); | 
|  | 889 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 890 | /// Try to rotate an operation below a PHI node, using PHI nodes for | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 891 | /// its operands. | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 892 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); | 
|  | 893 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); | 
|  | 894 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); | 
|  | 895 | Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN); | 
| Sanjay Patel | 9533407 | 2015-09-27 20:34:31 +0000 | [diff] [blame] | 896 | Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN); | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 897 |  | 
| Xinliang David Li | 4cdc9da | 2017-10-10 05:07:54 +0000 | [diff] [blame] | 898 | /// If an integer typed PHI has only one use which is an IntToPtr operation, | 
|  | 899 | /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise | 
|  | 900 | /// insert a new pointer typed PHI and replace the original one. | 
|  | 901 | Instruction *FoldIntegerTypedPHI(PHINode &PN); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 902 |  | 
| Dehao Chen | f464627 | 2017-10-02 18:13:14 +0000 | [diff] [blame] | 903 | /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the | 
| Robert Lougher | 2428a40 | 2016-12-14 17:49:19 +0000 | [diff] [blame] | 904 | /// folded operation. | 
| Dehao Chen | f464627 | 2017-10-02 18:13:14 +0000 | [diff] [blame] | 905 | void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN); | 
| Robert Lougher | 2428a40 | 2016-12-14 17:49:19 +0000 | [diff] [blame] | 906 |  | 
| Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 907 | Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, | 
|  | 908 | ICmpInst::Predicate Cond, Instruction &I); | 
| Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 909 | Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca, | 
|  | 910 | const Value *Other); | 
| Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 911 | Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, | 
|  | 912 | GlobalVariable *GV, CmpInst &ICI, | 
|  | 913 | ConstantInt *AndCst = nullptr); | 
|  | 914 | Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, | 
|  | 915 | Constant *RHSC); | 
| Craig Topper | bee7479 | 2018-08-20 23:04:25 +0000 | [diff] [blame] | 916 | Instruction *foldICmpAddOpConst(Value *X, const APInt &C, | 
| Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 917 | ICmpInst::Predicate Pred); | 
| Sanjay Patel | a90ee0e | 2019-08-20 14:56:44 +0000 | [diff] [blame] | 918 | Instruction *foldICmpWithCastOp(ICmpInst &ICI); | 
| Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 919 |  | 
| Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame] | 920 | Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp); | 
| Sanjay Patel | a40bf9ff | 2018-12-04 15:35:17 +0000 | [diff] [blame] | 921 | Instruction *foldICmpWithDominatingICmp(ICmpInst &Cmp); | 
| Sanjay Patel | 06b127a | 2016-09-15 14:37:50 +0000 | [diff] [blame] | 922 | Instruction *foldICmpWithConstant(ICmpInst &Cmp); | 
| Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 923 | Instruction *foldICmpInstWithConstant(ICmpInst &Cmp); | 
| Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 924 | Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp); | 
| Roman Lebedev | 2364695 | 2019-09-25 19:06:40 +0000 | [diff] [blame] | 925 | Instruction *foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ); | 
| Sanjay Patel | 10494b2 | 2016-09-16 16:10:22 +0000 | [diff] [blame] | 926 | Instruction *foldICmpEquality(ICmpInst &Cmp); | 
| Roman Lebedev | be612ea | 2019-07-30 15:28:22 +0000 | [diff] [blame] | 927 | Instruction *foldIRemByPowerOfTwoToBitTest(ICmpInst &I); | 
| Roman Lebedev | 49483a3 | 2019-10-20 19:38:50 +0000 | [diff] [blame] | 928 | Instruction *foldSignBitTest(ICmpInst &I); | 
| Nikolai Bozhenov | 0e7ebbc | 2017-10-16 09:19:21 +0000 | [diff] [blame] | 929 | Instruction *foldICmpWithZero(ICmpInst &Cmp); | 
| Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 930 |  | 
| Roman Lebedev | 473a063 | 2019-08-29 12:47:20 +0000 | [diff] [blame] | 931 | Value *foldUnsignedMultiplicationOverflowCheck(ICmpInst &Cmp); | 
|  | 932 |  | 
| Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 933 | Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select, | 
| Anna Thomas | d67165c | 2017-06-23 13:41:45 +0000 | [diff] [blame] | 934 | ConstantInt *C); | 
| Craig Topper | 524c44f | 2017-08-23 05:46:07 +0000 | [diff] [blame] | 935 | Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 936 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 937 | Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 938 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 939 | Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 940 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 941 | Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 942 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 943 | Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 944 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 945 | Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 946 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 947 | Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 948 | const APInt &C); | 
| Sanjay Patel | 80bea34 | 2019-09-11 12:04:26 +0000 | [diff] [blame] | 949 | Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv, | 
|  | 950 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 951 | Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 952 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 953 | Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 954 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 955 | Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 956 | const APInt &C); | 
| Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 957 | Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 958 | const APInt &C); | 
| Sanjay Patel | d3c7bb28 | 2016-08-26 16:42:33 +0000 | [diff] [blame] | 959 | Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 960 | const APInt &C1); | 
| Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 961 | Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 962 | const APInt &C1, const APInt &C2); | 
| Sanjay Patel | 8da42cc | 2016-09-15 22:26:31 +0000 | [diff] [blame] | 963 | Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, | 
|  | 964 | const APInt &C2); | 
|  | 965 | Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1, | 
|  | 966 | const APInt &C2); | 
| Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 967 |  | 
| Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 968 | Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, | 
|  | 969 | BinaryOperator *BO, | 
| Craig Topper | 8ed1aa9 | 2017-10-03 05:31:07 +0000 | [diff] [blame] | 970 | const APInt &C); | 
| Nikita Popov | 6515db2 | 2019-01-19 09:56:01 +0000 | [diff] [blame] | 971 | Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, | 
|  | 972 | const APInt &C); | 
|  | 973 | Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II, | 
|  | 974 | const APInt &C); | 
| Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 975 |  | 
| Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 976 | // Helpers of visitSelectInst(). | 
| Sanjay Patel | f7b851f | 2016-09-30 19:49:22 +0000 | [diff] [blame] | 977 | Instruction *foldSelectExtConst(SelectInst &Sel); | 
| Sanjay Patel | 453ceff | 2016-09-29 22:18:30 +0000 | [diff] [blame] | 978 | Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI); | 
|  | 979 | Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *); | 
|  | 980 | Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, | 
|  | 981 | Value *A, Value *B, Instruction &Outer, | 
|  | 982 | SelectPatternFlavor SPF2, Value *C); | 
|  | 983 | Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); | 
|  | 984 |  | 
| Craig Topper | 70e4f43 | 2017-04-02 17:57:30 +0000 | [diff] [blame] | 985 | Instruction *OptAndOp(BinaryOperator *Op, ConstantInt *OpRHS, | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 986 | ConstantInt *AndRHS, BinaryOperator &TheAnd); | 
| Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 987 |  | 
| Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 988 | Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, | 
|  | 989 | bool isSigned, bool Inside); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 990 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI); | 
| Sanjay Patel | 4a12aa9 | 2018-11-10 20:29:25 +0000 | [diff] [blame] | 991 | bool mergeStoreIntoSuccessor(StoreInst &SI); | 
| Igor Laevsky | 900ffa3 | 2017-02-08 14:32:04 +0000 | [diff] [blame] | 992 |  | 
| Sanjay Patel | 6072842 | 2018-11-14 16:03:36 +0000 | [diff] [blame] | 993 | /// Given an 'or' instruction, check to see if it is part of a bswap idiom. | 
|  | 994 | /// If so, return the equivalent bswap intrinsic. | 
|  | 995 | Instruction *matchBSwap(BinaryOperator &Or); | 
|  | 996 |  | 
| Daniel Neilson | 8f30ec6 | 2018-05-11 14:30:02 +0000 | [diff] [blame] | 997 | Instruction *SimplifyAnyMemTransfer(AnyMemTransferInst *MI); | 
| Daniel Neilson | f6651d4 | 2018-05-11 20:04:50 +0000 | [diff] [blame] | 998 | Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 999 |  | 
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1000 | Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned); | 
| Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 1001 |  | 
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1002 | /// Returns a value X such that Val = X * Scale, or null if none. | 
| Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 1003 | /// | 
|  | 1004 | /// If the multiplication is known not to overflow then NoSignedWrap is set. | 
| Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 1005 | Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap); | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 1006 | }; | 
|  | 1007 |  | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1008 | } // end namespace llvm | 
| Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 1009 |  | 
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 1010 | #undef DEBUG_TYPE | 
|  | 1011 |  | 
| Eugene Zelenko | 7f0f9bc | 2017-10-24 21:24:53 +0000 | [diff] [blame] | 1012 | #endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H |