Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1 | //===- ValueTracking.cpp - Walk computations to compute properties --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines that help analyze properties that chains of |
| 11 | // computations have. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/APFloat.h" |
| 17 | #include "llvm/ADT/APInt.h" |
| 18 | #include "llvm/ADT/ArrayRef.h" |
| 19 | #include "llvm/ADT/None.h" |
James Molloy | bf7b3fe | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Optional.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallSet.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
| 26 | #include "llvm/ADT/iterator_range.h" |
| 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/AssumptionCache.h" |
Max Kazantsev | 187d918 | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/GuardUtils.h" |
Dan Gohman | 2437127 | 2010-12-15 20:10:26 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/InstructionSimplify.h" |
Artur Pilipenko | ba9f09c | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/Loads.h" |
Adam Nemet | 50b9e7f | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | 3b8950a | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 35 | #include "llvm/IR/Argument.h" |
| 36 | #include "llvm/IR/Attributes.h" |
| 37 | #include "llvm/IR/BasicBlock.h" |
Nick Lewycky | 4bf804f | 2014-05-20 05:13:21 +0000 | [diff] [blame] | 38 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 19d764f | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 40 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Constants.h" |
| 42 | #include "llvm/IR/DataLayout.h" |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 43 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 44 | #include "llvm/IR/DiagnosticInfo.h" |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Function.h" |
Chandler Carruth | bd7cba0 | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 47 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 48 | #include "llvm/IR/GlobalAlias.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 49 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 50 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Instructions.h" |
| 54 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 55 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 56 | #include "llvm/IR/LLVMContext.h" |
| 57 | #include "llvm/IR/Metadata.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Module.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/Operator.h" |
Chandler Carruth | df3d8e8 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 60 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Type.h" |
| 62 | #include "llvm/IR/User.h" |
| 63 | #include "llvm/IR/Value.h" |
| 64 | #include "llvm/Support/Casting.h" |
| 65 | #include "llvm/Support/CommandLine.h" |
| 66 | #include "llvm/Support/Compiler.h" |
| 67 | #include "llvm/Support/ErrorHandling.h" |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 68 | #include "llvm/Support/KnownBits.h" |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 69 | #include "llvm/Support/MathExtras.h" |
Matthias Braun | f2fb000 | 2016-01-28 06:29:33 +0000 | [diff] [blame] | 70 | #include <algorithm> |
| 71 | #include <array> |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 72 | #include <cassert> |
| 73 | #include <cstdint> |
| 74 | #include <iterator> |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 75 | #include <utility> |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 77 | using namespace llvm; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 78 | using namespace llvm::PatternMatch; |
| 79 | |
| 80 | const unsigned MaxDepth = 6; |
| 81 | |
Philip Reames | 7c7b72a | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 82 | // Controls the number of uses of the value searched for possible |
| 83 | // dominating comparisons. |
| 84 | static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses", |
Igor Laevsky | a9db22a | 2015-09-29 14:57:52 +0000 | [diff] [blame] | 85 | cl::Hidden, cl::init(20)); |
Philip Reames | 7c7b72a | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 86 | |
Craig Topper | f990516 | 2017-05-03 22:25:19 +0000 | [diff] [blame] | 87 | /// Returns the bitwidth of the given scalar or pointer type. For vector types, |
| 88 | /// returns the element type's bitwidth. |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 89 | static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 90 | if (unsigned BitWidth = Ty->getScalarSizeInBits()) |
| 91 | return BitWidth; |
Matt Arsenault | d080fb1 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 92 | |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 93 | return DL.getIndexTypeSizeInBits(Ty); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 95 | |
Benjamin Kramer | 8f3452d | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 96 | namespace { |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 97 | |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 98 | // Simplifying using an assume can only be done in a particular control-flow |
| 99 | // context (the context instruction provides that context). If an assume and |
| 100 | // the context instruction are not in the same block then the DT helps in |
| 101 | // figuring out if we can use it. |
| 102 | struct Query { |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 103 | const DataLayout &DL; |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 104 | AssumptionCache *AC; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 105 | const Instruction *CxtI; |
| 106 | const DominatorTree *DT; |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 107 | |
Sanjay Patel | f6e615c | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 108 | // Unlike the other analyses, this may be a nullptr because not all clients |
| 109 | // provide it currently. |
| 110 | OptimizationRemarkEmitter *ORE; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 111 | |
Matthias Braun | f2fb000 | 2016-01-28 06:29:33 +0000 | [diff] [blame] | 112 | /// Set of assumptions that should be excluded from further queries. |
| 113 | /// This is because of the potential for mutual recursion to cause |
| 114 | /// computeKnownBits to repeatedly visit the same assume intrinsic. The |
| 115 | /// classic case of this is assume(x = y), which will attempt to determine |
| 116 | /// bits in x from bits in y, which will attempt to determine bits in y from |
| 117 | /// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 118 | /// isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo |
| 119 | /// (all of which can call computeKnownBits), and so on. |
Li Huang | edc73e8 | 2016-10-15 19:00:04 +0000 | [diff] [blame] | 120 | std::array<const Value *, MaxDepth> Excluded; |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 121 | |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 122 | /// If true, it is safe to use metadata during simplification. |
| 123 | InstrInfoQuery IIQ; |
| 124 | |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 125 | unsigned NumExcluded = 0; |
Matthias Braun | f2fb000 | 2016-01-28 06:29:33 +0000 | [diff] [blame] | 126 | |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 127 | Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 128 | const DominatorTree *DT, bool UseInstrInfo, |
| 129 | OptimizationRemarkEmitter *ORE = nullptr) |
| 130 | : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {} |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 131 | |
| 132 | Query(const Query &Q, const Value *NewExcl) |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 133 | : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ), |
Sanjay Patel | f6e615c | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 134 | NumExcluded(Q.NumExcluded) { |
Matthias Braun | f2fb000 | 2016-01-28 06:29:33 +0000 | [diff] [blame] | 135 | Excluded = Q.Excluded; |
| 136 | Excluded[NumExcluded++] = NewExcl; |
| 137 | assert(NumExcluded <= Excluded.size()); |
| 138 | } |
| 139 | |
| 140 | bool isExcluded(const Value *Value) const { |
| 141 | if (NumExcluded == 0) |
| 142 | return false; |
| 143 | auto End = Excluded.begin() + NumExcluded; |
| 144 | return std::find(Excluded.begin(), End, Value) != End; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 145 | } |
| 146 | }; |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 147 | |
Benjamin Kramer | 8f3452d | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 148 | } // end anonymous namespace |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 149 | |
Sanjay Patel | f1ab807 | 2014-11-04 16:09:50 +0000 | [diff] [blame] | 150 | // Given the provided Value and, potentially, a context instruction, return |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 151 | // the preferred context instruction (if any). |
| 152 | static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) { |
| 153 | // If we've been provided with a context instruction, then use that (provided |
| 154 | // it has been inserted). |
| 155 | if (CxtI && CxtI->getParent()) |
| 156 | return CxtI; |
| 157 | |
| 158 | // If the value is really an already-inserted instruction, then use that. |
| 159 | CxtI = dyn_cast<Instruction>(V); |
| 160 | if (CxtI && CxtI->getParent()) |
| 161 | return CxtI; |
| 162 | |
| 163 | return nullptr; |
| 164 | } |
| 165 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 166 | static void computeKnownBits(const Value *V, KnownBits &Known, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 167 | unsigned Depth, const Query &Q); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 168 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 169 | void llvm::computeKnownBits(const Value *V, KnownBits &Known, |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 170 | const DataLayout &DL, unsigned Depth, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 171 | AssumptionCache *AC, const Instruction *CxtI, |
Sanjay Patel | f6e615c | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 172 | const DominatorTree *DT, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 173 | OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 174 | ::computeKnownBits(V, Known, Depth, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 175 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 178 | static KnownBits computeKnownBits(const Value *V, unsigned Depth, |
| 179 | const Query &Q); |
| 180 | |
| 181 | KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL, |
| 182 | unsigned Depth, AssumptionCache *AC, |
| 183 | const Instruction *CxtI, |
Craig Topper | 76ae125 | 2017-05-24 16:53:03 +0000 | [diff] [blame] | 184 | const DominatorTree *DT, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 185 | OptimizationRemarkEmitter *ORE, |
| 186 | bool UseInstrInfo) { |
| 187 | return ::computeKnownBits( |
| 188 | V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 191 | bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 192 | const DataLayout &DL, AssumptionCache *AC, |
| 193 | const Instruction *CxtI, const DominatorTree *DT, |
| 194 | bool UseInstrInfo) { |
Jingyue Wu | 19eda53 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 195 | assert(LHS->getType() == RHS->getType() && |
| 196 | "LHS and RHS should have the same type"); |
| 197 | assert(LHS->getType()->isIntOrIntVectorTy() && |
| 198 | "LHS and RHS should be integers"); |
Roman Lebedev | 6c24dd3 | 2018-04-15 18:59:33 +0000 | [diff] [blame] | 199 | // Look for an inverted mask: (X & ~M) op (Y & M). |
| 200 | Value *M; |
| 201 | if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) && |
| 202 | match(RHS, m_c_And(m_Specific(M), m_Value()))) |
| 203 | return true; |
| 204 | if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) && |
| 205 | match(LHS, m_c_And(m_Specific(M), m_Value()))) |
| 206 | return true; |
Jingyue Wu | 19eda53 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 207 | IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType()); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 208 | KnownBits LHSKnown(IT->getBitWidth()); |
| 209 | KnownBits RHSKnown(IT->getBitWidth()); |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 210 | computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); |
| 211 | computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 212 | return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue(); |
Jingyue Wu | 19eda53 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Zaara Syeda | 682f92f | 2017-05-31 17:12:38 +0000 | [diff] [blame] | 215 | bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) { |
| 216 | for (const User *U : CxtI->users()) { |
| 217 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(U)) |
| 218 | if (IC->isEquality()) |
| 219 | if (Constant *C = dyn_cast<Constant>(IC->getOperand(1))) |
| 220 | if (C->isNullValue()) |
| 221 | continue; |
| 222 | return false; |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 227 | static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 228 | const Query &Q); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 229 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 230 | bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 231 | bool OrZero, unsigned Depth, |
| 232 | AssumptionCache *AC, const Instruction *CxtI, |
| 233 | const DominatorTree *DT, bool UseInstrInfo) { |
| 234 | return ::isKnownToBeAPowerOfTwo( |
| 235 | V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 238 | static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 239 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 240 | bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 241 | AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 242 | const DominatorTree *DT, bool UseInstrInfo) { |
| 243 | return ::isKnownNonZero(V, Depth, |
| 244 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 247 | bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 248 | unsigned Depth, AssumptionCache *AC, |
| 249 | const Instruction *CxtI, const DominatorTree *DT, |
| 250 | bool UseInstrInfo) { |
| 251 | KnownBits Known = |
| 252 | computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo); |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 253 | return Known.isNonNegative(); |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 256 | bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 257 | AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 258 | const DominatorTree *DT, bool UseInstrInfo) { |
Philip Reames | 352b004 | 2016-03-09 21:31:47 +0000 | [diff] [blame] | 259 | if (auto *CI = dyn_cast<ConstantInt>(V)) |
| 260 | return CI->getValue().isStrictlyPositive(); |
Sanjoy Das | 36bb502 | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 261 | |
Philip Reames | 352b004 | 2016-03-09 21:31:47 +0000 | [diff] [blame] | 262 | // TODO: We'd doing two recursive queries here. We should factor this such |
| 263 | // that only a single query is needed. |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 264 | return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) && |
| 265 | isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo); |
Philip Reames | 352b004 | 2016-03-09 21:31:47 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 268 | bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 269 | AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 270 | const DominatorTree *DT, bool UseInstrInfo) { |
| 271 | KnownBits Known = |
| 272 | computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo); |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 273 | return Known.isNegative(); |
Nick Lewycky | 32ea396 | 2016-04-21 00:53:14 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 276 | static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q); |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 277 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 278 | bool llvm::isKnownNonEqual(const Value *V1, const Value *V2, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 279 | const DataLayout &DL, AssumptionCache *AC, |
| 280 | const Instruction *CxtI, const DominatorTree *DT, |
| 281 | bool UseInstrInfo) { |
| 282 | return ::isKnownNonEqual(V1, V2, |
| 283 | Query(DL, AC, safeCxtI(V1, safeCxtI(V2, CxtI)), DT, |
| 284 | UseInstrInfo, /*ORE=*/nullptr)); |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 287 | static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 288 | const Query &Q); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 289 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 290 | bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 291 | const DataLayout &DL, unsigned Depth, |
| 292 | AssumptionCache *AC, const Instruction *CxtI, |
| 293 | const DominatorTree *DT, bool UseInstrInfo) { |
| 294 | return ::MaskedValueIsZero( |
| 295 | V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 298 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, |
| 299 | const Query &Q); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 300 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 301 | unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 302 | unsigned Depth, AssumptionCache *AC, |
| 303 | const Instruction *CxtI, |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 304 | const DominatorTree *DT, bool UseInstrInfo) { |
| 305 | return ::ComputeNumSignBits( |
| 306 | V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo)); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Craig Topper | 7ebd179 | 2017-03-24 22:12:10 +0000 | [diff] [blame] | 309 | static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, |
| 310 | bool NSW, |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 311 | KnownBits &KnownOut, KnownBits &Known2, |
Craig Topper | 7ebd179 | 2017-03-24 22:12:10 +0000 | [diff] [blame] | 312 | unsigned Depth, const Query &Q) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 313 | unsigned BitWidth = KnownOut.getBitWidth(); |
Craig Topper | 7ebd179 | 2017-03-24 22:12:10 +0000 | [diff] [blame] | 314 | |
| 315 | // If an initial sequence of bits in the result is not needed, the |
| 316 | // corresponding bits in the operands are not needed. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 317 | KnownBits LHSKnown(BitWidth); |
| 318 | computeKnownBits(Op0, LHSKnown, Depth + 1, Q); |
| 319 | computeKnownBits(Op1, Known2, Depth + 1, Q); |
Craig Topper | 7ebd179 | 2017-03-24 22:12:10 +0000 | [diff] [blame] | 320 | |
Craig Topper | c9b3b5d | 2017-08-08 16:29:35 +0000 | [diff] [blame] | 321 | KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2); |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 324 | static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 325 | KnownBits &Known, KnownBits &Known2, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 326 | unsigned Depth, const Query &Q) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 327 | unsigned BitWidth = Known.getBitWidth(); |
| 328 | computeKnownBits(Op1, Known, Depth + 1, Q); |
| 329 | computeKnownBits(Op0, Known2, Depth + 1, Q); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 330 | |
| 331 | bool isKnownNegative = false; |
| 332 | bool isKnownNonNegative = false; |
| 333 | // If the multiplication is known not to overflow, compute the sign bit. |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 334 | if (NSW) { |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 335 | if (Op0 == Op1) { |
| 336 | // The product of a number with itself is non-negative. |
| 337 | isKnownNonNegative = true; |
| 338 | } else { |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 339 | bool isKnownNonNegativeOp1 = Known.isNonNegative(); |
| 340 | bool isKnownNonNegativeOp0 = Known2.isNonNegative(); |
| 341 | bool isKnownNegativeOp1 = Known.isNegative(); |
| 342 | bool isKnownNegativeOp0 = Known2.isNegative(); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 343 | // The product of two numbers with the same sign is non-negative. |
| 344 | isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) || |
| 345 | (isKnownNonNegativeOp1 && isKnownNonNegativeOp0); |
| 346 | // The product of a negative number and a non-negative number is either |
| 347 | // negative or zero. |
| 348 | if (!isKnownNonNegative) |
| 349 | isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 350 | isKnownNonZero(Op0, Depth, Q)) || |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 351 | (isKnownNegativeOp0 && isKnownNonNegativeOp1 && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 352 | isKnownNonZero(Op1, Depth, Q)); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Simon Dardis | 1134b2a | 2017-12-09 23:25:57 +0000 | [diff] [blame] | 356 | assert(!Known.hasConflict() && !Known2.hasConflict()); |
| 357 | // Compute a conservative estimate for high known-0 bits. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 358 | unsigned LeadZ = std::max(Known.countMinLeadingZeros() + |
| 359 | Known2.countMinLeadingZeros(), |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 360 | BitWidth) - BitWidth; |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 361 | LeadZ = std::min(LeadZ, BitWidth); |
Simon Dardis | 1134b2a | 2017-12-09 23:25:57 +0000 | [diff] [blame] | 362 | |
| 363 | // The result of the bottom bits of an integer multiply can be |
| 364 | // inferred by looking at the bottom bits of both operands and |
| 365 | // multiplying them together. |
| 366 | // We can infer at least the minimum number of known trailing bits |
| 367 | // of both operands. Depending on number of trailing zeros, we can |
| 368 | // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming |
| 369 | // a and b are divisible by m and n respectively. |
| 370 | // We then calculate how many of those bits are inferrable and set |
| 371 | // the output. For example, the i8 mul: |
| 372 | // a = XXXX1100 (12) |
| 373 | // b = XXXX1110 (14) |
| 374 | // We know the bottom 3 bits are zero since the first can be divided by |
| 375 | // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4). |
| 376 | // Applying the multiplication to the trimmed arguments gets: |
| 377 | // XX11 (3) |
| 378 | // X111 (7) |
| 379 | // ------- |
| 380 | // XX11 |
| 381 | // XX11 |
| 382 | // XX11 |
| 383 | // XX11 |
| 384 | // ------- |
| 385 | // XXXXX01 |
| 386 | // Which allows us to infer the 2 LSBs. Since we're multiplying the result |
| 387 | // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits. |
| 388 | // The proof for this can be described as: |
| 389 | // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) && |
| 390 | // (C7 == (1 << (umin(countTrailingZeros(C1), C5) + |
| 391 | // umin(countTrailingZeros(C2), C6) + |
| 392 | // umin(C5 - umin(countTrailingZeros(C1), C5), |
| 393 | // C6 - umin(countTrailingZeros(C2), C6)))) - 1) |
| 394 | // %aa = shl i8 %a, C5 |
| 395 | // %bb = shl i8 %b, C6 |
| 396 | // %aaa = or i8 %aa, C1 |
| 397 | // %bbb = or i8 %bb, C2 |
| 398 | // %mul = mul i8 %aaa, %bbb |
| 399 | // %mask = and i8 %mul, C7 |
| 400 | // => |
| 401 | // %mask = i8 ((C1*C2)&C7) |
| 402 | // Where C5, C6 describe the known bits of %a, %b |
| 403 | // C1, C2 describe the known bottom bits of %a, %b. |
| 404 | // C7 describes the mask of the known bits of the result. |
| 405 | APInt Bottom0 = Known.One; |
| 406 | APInt Bottom1 = Known2.One; |
| 407 | |
| 408 | // How many times we'd be able to divide each argument by 2 (shr by 1). |
| 409 | // This gives us the number of trailing zeros on the multiplication result. |
| 410 | unsigned TrailBitsKnown0 = (Known.Zero | Known.One).countTrailingOnes(); |
| 411 | unsigned TrailBitsKnown1 = (Known2.Zero | Known2.One).countTrailingOnes(); |
| 412 | unsigned TrailZero0 = Known.countMinTrailingZeros(); |
| 413 | unsigned TrailZero1 = Known2.countMinTrailingZeros(); |
| 414 | unsigned TrailZ = TrailZero0 + TrailZero1; |
| 415 | |
| 416 | // Figure out the fewest known-bits operand. |
| 417 | unsigned SmallestOperand = std::min(TrailBitsKnown0 - TrailZero0, |
| 418 | TrailBitsKnown1 - TrailZero1); |
| 419 | unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth); |
| 420 | |
| 421 | APInt BottomKnown = Bottom0.getLoBits(TrailBitsKnown0) * |
| 422 | Bottom1.getLoBits(TrailBitsKnown1); |
| 423 | |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 424 | Known.resetAll(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 425 | Known.Zero.setHighBits(LeadZ); |
Simon Dardis | 1134b2a | 2017-12-09 23:25:57 +0000 | [diff] [blame] | 426 | Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown); |
| 427 | Known.One |= BottomKnown.getLoBits(ResultBitsKnown); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 428 | |
| 429 | // Only make use of no-wrap flags if we failed to compute the sign bit |
| 430 | // directly. This matters if the multiplication always overflows, in |
| 431 | // which case we prefer to follow the result of the direct computation, |
| 432 | // though as the program is invoking undefined behaviour we can choose |
| 433 | // whatever we like here. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 434 | if (isKnownNonNegative && !Known.isNegative()) |
| 435 | Known.makeNonNegative(); |
| 436 | else if (isKnownNegative && !Known.isNonNegative()) |
| 437 | Known.makeNegative(); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Jingyue Wu | e4d0a5e | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 440 | void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, |
Craig Topper | 955683a | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 441 | KnownBits &Known) { |
| 442 | unsigned BitWidth = Known.getBitWidth(); |
Rafael Espindola | 7c7121e | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 443 | unsigned NumRanges = Ranges.getNumOperands() / 2; |
| 444 | assert(NumRanges >= 1); |
| 445 | |
Craig Topper | 955683a | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 446 | Known.Zero.setAllBits(); |
| 447 | Known.One.setAllBits(); |
Sanjoy Das | e06e113 | 2015-10-28 03:20:15 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | 7c7121e | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 449 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 450 | ConstantInt *Lower = |
| 451 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0)); |
| 452 | ConstantInt *Upper = |
| 453 | mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1)); |
Rafael Espindola | 7c7121e | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 454 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
Rafael Espindola | 7c7121e | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 455 | |
Sanjoy Das | e06e113 | 2015-10-28 03:20:15 +0000 | [diff] [blame] | 456 | // The first CommonPrefixBits of all values in Range are equal. |
| 457 | unsigned CommonPrefixBits = |
| 458 | (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros(); |
| 459 | |
| 460 | APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits); |
Craig Topper | 955683a | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 461 | Known.One &= Range.getUnsignedMax() & Mask; |
| 462 | Known.Zero &= ~Range.getUnsignedMax() & Mask; |
Sanjoy Das | e06e113 | 2015-10-28 03:20:15 +0000 | [diff] [blame] | 463 | } |
Rafael Espindola | 7c7121e | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 464 | } |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 465 | |
Pete Cooper | ae48001 | 2016-08-11 22:23:07 +0000 | [diff] [blame] | 466 | static bool isEphemeralValueOf(const Instruction *I, const Value *E) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 467 | SmallVector<const Value *, 16> WorkSet(1, I); |
| 468 | SmallPtrSet<const Value *, 32> Visited; |
| 469 | SmallPtrSet<const Value *, 16> EphValues; |
| 470 | |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 471 | // The instruction defining an assumption's condition itself is always |
| 472 | // considered ephemeral to that assumption (even if it has other |
| 473 | // non-ephemeral users). See r246696's test case for an example. |
David Majnemer | dc9c737 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 474 | if (is_contained(I->operands(), E)) |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 475 | return true; |
| 476 | |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 477 | while (!WorkSet.empty()) { |
| 478 | const Value *V = WorkSet.pop_back_val(); |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 479 | if (!Visited.insert(V).second) |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 480 | continue; |
| 481 | |
| 482 | // If all uses of this value are ephemeral, then so is this value. |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 483 | if (llvm::all_of(V->users(), [&](const User *U) { |
| 484 | return EphValues.count(U); |
| 485 | })) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 486 | if (V == E) |
| 487 | return true; |
| 488 | |
Hal Finkel | 7d99ae5 | 2017-08-14 17:11:43 +0000 | [diff] [blame] | 489 | if (V == I || isSafeToSpeculativelyExecute(V)) { |
| 490 | EphValues.insert(V); |
| 491 | if (const User *U = dyn_cast<User>(V)) |
| 492 | for (User::const_op_iterator J = U->op_begin(), JE = U->op_end(); |
| 493 | J != JE; ++J) |
| 494 | WorkSet.push_back(*J); |
| 495 | } |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | // Is this an intrinsic that cannot be speculated but also cannot trap? |
Haicheng Wu | 147a400 | 2017-12-15 14:34:41 +0000 | [diff] [blame] | 503 | bool llvm::isAssumeLikeIntrinsic(const Instruction *I) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 504 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 505 | if (Function *F = CI->getCalledFunction()) |
| 506 | switch (F->getIntrinsicID()) { |
| 507 | default: break; |
| 508 | // FIXME: This list is repeated from NoTTI::getIntrinsicCost. |
| 509 | case Intrinsic::assume: |
Dan Gohman | b5e0bec | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 510 | case Intrinsic::sideeffect: |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 511 | case Intrinsic::dbg_declare: |
| 512 | case Intrinsic::dbg_value: |
Shiva Chen | a8a13bc | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 513 | case Intrinsic::dbg_label: |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 514 | case Intrinsic::invariant_start: |
| 515 | case Intrinsic::invariant_end: |
| 516 | case Intrinsic::lifetime_start: |
| 517 | case Intrinsic::lifetime_end: |
| 518 | case Intrinsic::objectsize: |
| 519 | case Intrinsic::ptr_annotation: |
| 520 | case Intrinsic::var_annotation: |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | return false; |
| 525 | } |
| 526 | |
Pete Cooper | ae48001 | 2016-08-11 22:23:07 +0000 | [diff] [blame] | 527 | bool llvm::isValidAssumeForContext(const Instruction *Inv, |
| 528 | const Instruction *CxtI, |
| 529 | const DominatorTree *DT) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 530 | // There are two restrictions on the use of an assume: |
| 531 | // 1. The assume must dominate the context (or the control flow must |
| 532 | // reach the assume whenever it reaches the context). |
| 533 | // 2. The context must not be in the assume's set of ephemeral values |
| 534 | // (otherwise we will use the assume to prove that the condition |
| 535 | // feeding the assume is trivially true, thus causing the removal of |
| 536 | // the assume). |
| 537 | |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 538 | if (DT) { |
Pete Cooper | 2d34392 | 2016-08-12 01:00:15 +0000 | [diff] [blame] | 539 | if (DT->dominates(Inv, CxtI)) |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 540 | return true; |
Pete Cooper | 2d34392 | 2016-08-12 01:00:15 +0000 | [diff] [blame] | 541 | } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) { |
| 542 | // We don't have a DT, but this trivially dominates. |
| 543 | return true; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Pete Cooper | 2d34392 | 2016-08-12 01:00:15 +0000 | [diff] [blame] | 546 | // With or without a DT, the only remaining case we will check is if the |
| 547 | // instructions are in the same BB. Give up if that is not the case. |
| 548 | if (Inv->getParent() != CxtI->getParent()) |
| 549 | return false; |
| 550 | |
Vedant Kumar | 06a2673 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 551 | // If we have a dom tree, then we now know that the assume doesn't dominate |
Pete Cooper | 2d34392 | 2016-08-12 01:00:15 +0000 | [diff] [blame] | 552 | // the other instruction. If we don't have a dom tree then we can check if |
| 553 | // the assume is first in the BB. |
| 554 | if (!DT) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 555 | // Search forward from the assume until we reach the context (or the end |
| 556 | // of the block); the common case is that the assume will come first. |
Pete Cooper | ae48001 | 2016-08-11 22:23:07 +0000 | [diff] [blame] | 557 | for (auto I = std::next(BasicBlock::const_iterator(Inv)), |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 558 | IE = Inv->getParent()->end(); I != IE; ++I) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 559 | if (&*I == CxtI) |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 560 | return true; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Pete Cooper | 2d34392 | 2016-08-12 01:00:15 +0000 | [diff] [blame] | 563 | // The context comes first, but they're both in the same block. Make sure |
| 564 | // there is nothing in between that might interrupt the control flow. |
| 565 | for (BasicBlock::const_iterator I = |
| 566 | std::next(BasicBlock::const_iterator(CxtI)), IE(Inv); |
| 567 | I != IE; ++I) |
| 568 | if (!isSafeToSpeculativelyExecute(&*I) && !isAssumeLikeIntrinsic(&*I)) |
| 569 | return false; |
| 570 | |
| 571 | return !isEphemeralValueOf(Inv, CxtI); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 574 | static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known, |
| 575 | unsigned Depth, const Query &Q) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 576 | // Use of assumptions is context-sensitive. If we don't have a context, we |
| 577 | // cannot use them! |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 578 | if (!Q.AC || !Q.CxtI) |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 579 | return; |
| 580 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 581 | unsigned BitWidth = Known.getBitWidth(); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 582 | |
Hal Finkel | a6e44bd | 2017-01-11 13:24:24 +0000 | [diff] [blame] | 583 | // Note that the patterns below need to be kept in sync with the code |
| 584 | // in AssumptionCache::updateAffectedValues. |
| 585 | |
| 586 | for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 587 | if (!AssumeVH) |
Chandler Carruth | 5a9cd4d | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 588 | continue; |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 589 | CallInst *I = cast<CallInst>(AssumeVH); |
| 590 | assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() && |
| 591 | "Got assumption for the wrong function!"); |
| 592 | if (Q.isExcluded(I)) |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 593 | continue; |
| 594 | |
Vedant Kumar | 06a2673 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 595 | // Warning: This loop can end up being somewhat performance sensitive. |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 596 | // We're running this loop for once for each value queried resulting in a |
| 597 | // runtime of ~O(#assumes * #values). |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 598 | |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 599 | assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume && |
| 600 | "must be an assume intrinsic"); |
| 601 | |
| 602 | Value *Arg = I->getArgOperand(0); |
| 603 | |
| 604 | if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 605 | assert(BitWidth == 1 && "assume operand is not i1?"); |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 606 | Known.setAllOnes(); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
Sanjay Patel | 3ecd20b | 2017-01-17 18:15:49 +0000 | [diff] [blame] | 609 | if (match(Arg, m_Not(m_Specific(V))) && |
| 610 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
| 611 | assert(BitWidth == 1 && "assume operand is not i1?"); |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 612 | Known.setAllZero(); |
Sanjay Patel | 3ecd20b | 2017-01-17 18:15:49 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 615 | |
David Majnemer | 3b7e6d2 | 2014-12-12 23:59:29 +0000 | [diff] [blame] | 616 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 617 | if (Depth == MaxDepth) |
| 618 | continue; |
| 619 | |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 620 | Value *A, *B; |
| 621 | auto m_V = m_CombineOr(m_Specific(V), |
| 622 | m_CombineOr(m_PtrToInt(m_Specific(V)), |
| 623 | m_BitCast(m_Specific(V)))); |
| 624 | |
| 625 | CmpInst::Predicate Pred; |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 626 | uint64_t C; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 627 | // assume(v = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 628 | if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 629 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 630 | KnownBits RHSKnown(BitWidth); |
| 631 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 632 | Known.Zero |= RHSKnown.Zero; |
| 633 | Known.One |= RHSKnown.One; |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 634 | // assume(v & b = a) |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 635 | } else if (match(Arg, |
| 636 | m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 637 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 638 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 639 | KnownBits RHSKnown(BitWidth); |
| 640 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 641 | KnownBits MaskKnown(BitWidth); |
| 642 | computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 643 | |
| 644 | // For those bits in the mask that are known to be one, we can propagate |
| 645 | // known bits from the RHS to V. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 646 | Known.Zero |= RHSKnown.Zero & MaskKnown.One; |
| 647 | Known.One |= RHSKnown.One & MaskKnown.One; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 648 | // assume(~(v & b) = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 649 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))), |
| 650 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 651 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 652 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 653 | KnownBits RHSKnown(BitWidth); |
| 654 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 655 | KnownBits MaskKnown(BitWidth); |
| 656 | computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 657 | |
| 658 | // For those bits in the mask that are known to be one, we can propagate |
| 659 | // inverted known bits from the RHS to V. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 660 | Known.Zero |= RHSKnown.One & MaskKnown.One; |
| 661 | Known.One |= RHSKnown.Zero & MaskKnown.One; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 662 | // assume(v | b = a) |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 663 | } else if (match(Arg, |
| 664 | m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 665 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 666 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 667 | KnownBits RHSKnown(BitWidth); |
| 668 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 669 | KnownBits BKnown(BitWidth); |
| 670 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 671 | |
| 672 | // For those bits in B that are known to be zero, we can propagate known |
| 673 | // bits from the RHS to V. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 674 | Known.Zero |= RHSKnown.Zero & BKnown.Zero; |
| 675 | Known.One |= RHSKnown.One & BKnown.Zero; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 676 | // assume(~(v | b) = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 677 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))), |
| 678 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 679 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 680 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 681 | KnownBits RHSKnown(BitWidth); |
| 682 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 683 | KnownBits BKnown(BitWidth); |
| 684 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 685 | |
| 686 | // For those bits in B that are known to be zero, we can propagate |
| 687 | // inverted known bits from the RHS to V. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 688 | Known.Zero |= RHSKnown.One & BKnown.Zero; |
| 689 | Known.One |= RHSKnown.Zero & BKnown.Zero; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 690 | // assume(v ^ b = a) |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 691 | } else if (match(Arg, |
| 692 | m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 693 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 694 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 695 | KnownBits RHSKnown(BitWidth); |
| 696 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 697 | KnownBits BKnown(BitWidth); |
| 698 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 699 | |
| 700 | // For those bits in B that are known to be zero, we can propagate known |
| 701 | // bits from the RHS to V. For those bits in B that are known to be one, |
| 702 | // we can propagate inverted known bits from the RHS to V. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 703 | Known.Zero |= RHSKnown.Zero & BKnown.Zero; |
| 704 | Known.One |= RHSKnown.One & BKnown.Zero; |
| 705 | Known.Zero |= RHSKnown.One & BKnown.One; |
| 706 | Known.One |= RHSKnown.Zero & BKnown.One; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 707 | // assume(~(v ^ b) = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 708 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))), |
| 709 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 710 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 711 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 712 | KnownBits RHSKnown(BitWidth); |
| 713 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
| 714 | KnownBits BKnown(BitWidth); |
| 715 | computeKnownBits(B, BKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 716 | |
| 717 | // For those bits in B that are known to be zero, we can propagate |
| 718 | // inverted known bits from the RHS to V. For those bits in B that are |
| 719 | // known to be one, we can propagate known bits from the RHS to V. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 720 | Known.Zero |= RHSKnown.One & BKnown.Zero; |
| 721 | Known.One |= RHSKnown.Zero & BKnown.Zero; |
| 722 | Known.Zero |= RHSKnown.Zero & BKnown.One; |
| 723 | Known.One |= RHSKnown.One & BKnown.One; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 724 | // assume(v << c = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 725 | } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)), |
| 726 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 727 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 728 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 729 | C < BitWidth) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 730 | KnownBits RHSKnown(BitWidth); |
| 731 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 732 | // For those bits in RHS that are known, we can propagate them to known |
| 733 | // bits in V shifted to the right by C. |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 734 | RHSKnown.Zero.lshrInPlace(C); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 735 | Known.Zero |= RHSKnown.Zero; |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 736 | RHSKnown.One.lshrInPlace(C); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 737 | Known.One |= RHSKnown.One; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 738 | // assume(~(v << c) = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 739 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))), |
| 740 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 741 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 742 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 743 | C < BitWidth) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 744 | KnownBits RHSKnown(BitWidth); |
| 745 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 746 | // For those bits in RHS that are known, we can propagate them inverted |
| 747 | // to known bits in V shifted to the right by C. |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 748 | RHSKnown.One.lshrInPlace(C); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 749 | Known.Zero |= RHSKnown.One; |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 750 | RHSKnown.Zero.lshrInPlace(C); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 751 | Known.One |= RHSKnown.Zero; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 752 | // assume(v >> c = a) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 753 | } else if (match(Arg, |
Craig Topper | 5f53d1d | 2017-06-24 06:24:04 +0000 | [diff] [blame] | 754 | m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)), |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 755 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 756 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 757 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 758 | C < BitWidth) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 759 | KnownBits RHSKnown(BitWidth); |
| 760 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 761 | // For those bits in RHS that are known, we can propagate them to known |
| 762 | // bits in V shifted to the right by C. |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 763 | Known.Zero |= RHSKnown.Zero << C; |
| 764 | Known.One |= RHSKnown.One << C; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 765 | // assume(~(v >> c) = a) |
Craig Topper | 5f53d1d | 2017-06-24 06:24:04 +0000 | [diff] [blame] | 766 | } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))), |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 767 | m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 768 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 769 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 770 | C < BitWidth) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 771 | KnownBits RHSKnown(BitWidth); |
| 772 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 773 | // For those bits in RHS that are known, we can propagate them inverted |
| 774 | // to known bits in V shifted to the right by C. |
Igor Laevsky | b8aa60d | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 775 | Known.Zero |= RHSKnown.One << C; |
| 776 | Known.One |= RHSKnown.Zero << C; |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 777 | // assume(v >=_s c) where c is non-negative |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 778 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 779 | Pred == ICmpInst::ICMP_SGE && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 780 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 781 | KnownBits RHSKnown(BitWidth); |
| 782 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 783 | |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 784 | if (RHSKnown.isNonNegative()) { |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 785 | // We know that the sign bit is zero. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 786 | Known.makeNonNegative(); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 787 | } |
| 788 | // assume(v >_s c) where c is at least -1. |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 789 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 790 | Pred == ICmpInst::ICMP_SGT && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 791 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 792 | KnownBits RHSKnown(BitWidth); |
| 793 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 794 | |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 795 | if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) { |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 796 | // We know that the sign bit is zero. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 797 | Known.makeNonNegative(); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 798 | } |
| 799 | // assume(v <=_s c) where c is negative |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 800 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 801 | Pred == ICmpInst::ICMP_SLE && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 802 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 803 | KnownBits RHSKnown(BitWidth); |
| 804 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 805 | |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 806 | if (RHSKnown.isNegative()) { |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 807 | // We know that the sign bit is one. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 808 | Known.makeNegative(); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 809 | } |
| 810 | // assume(v <_s c) where c is non-positive |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 811 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 812 | Pred == ICmpInst::ICMP_SLT && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 813 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 814 | KnownBits RHSKnown(BitWidth); |
| 815 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 816 | |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 817 | if (RHSKnown.isZero() || RHSKnown.isNegative()) { |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 818 | // We know that the sign bit is one. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 819 | Known.makeNegative(); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 820 | } |
| 821 | // assume(v <=_u c) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 822 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 823 | Pred == ICmpInst::ICMP_ULE && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 824 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 825 | KnownBits RHSKnown(BitWidth); |
| 826 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 827 | |
| 828 | // Whatever high bits in c are zero are known to be zero. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 829 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); |
| 830 | // assume(v <_u c) |
Philip Reames | 521c9dc | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 831 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 832 | Pred == ICmpInst::ICMP_ULT && |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 833 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 834 | KnownBits RHSKnown(BitWidth); |
| 835 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 836 | |
Sanjay Patel | ce8aaf3 | 2018-02-08 14:52:40 +0000 | [diff] [blame] | 837 | // If the RHS is known zero, then this assumption must be wrong (nothing |
| 838 | // is unsigned less than zero). Signal a conflict and get out of here. |
| 839 | if (RHSKnown.isZero()) { |
| 840 | Known.Zero.setAllBits(); |
| 841 | Known.One.setAllBits(); |
| 842 | break; |
| 843 | } |
| 844 | |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 845 | // Whatever high bits in c are zero are known to be zero (if c is a power |
| 846 | // of 2, then one more). |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 847 | if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I))) |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 848 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1); |
Hal Finkel | 83d886d | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 849 | else |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 850 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
Sanjay Patel | ab1df8f | 2017-02-01 15:41:32 +0000 | [diff] [blame] | 853 | |
| 854 | // If assumptions conflict with each other or previous known bits, then we |
Sanjay Patel | f6e615c | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 855 | // have a logical fallacy. It's possible that the assumption is not reachable, |
| 856 | // so this isn't a real bug. On the other hand, the program may have undefined |
| 857 | // behavior, or we might have a bug in the compiler. We can't assert/crash, so |
| 858 | // clear out the known bits, try to warn the user, and hope for the best. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 859 | if (Known.Zero.intersects(Known.One)) { |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 860 | Known.resetAll(); |
Sanjay Patel | f6e615c | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 861 | |
Vivek Pandya | 2540c74 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 862 | if (Q.ORE) |
| 863 | Q.ORE->emit([&]() { |
| 864 | auto *CxtI = const_cast<Instruction *>(Q.CxtI); |
| 865 | return OptimizationRemarkAnalysis("value-tracking", "BadAssumption", |
| 866 | CxtI) |
| 867 | << "Detected conflicting code assumptions. Program may " |
| 868 | "have undefined behavior, or compiler may have " |
| 869 | "internal error."; |
| 870 | }); |
Sanjay Patel | ab1df8f | 2017-02-01 15:41:32 +0000 | [diff] [blame] | 871 | } |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Sanjay Patel | de116cc | 2017-10-16 14:46:37 +0000 | [diff] [blame] | 874 | /// Compute known bits from a shift operator, including those with a |
| 875 | /// non-constant shift amount. Known is the output of this function. Known2 is a |
| 876 | /// pre-allocated temporary with the same bit width as Known. KZF and KOF are |
Vedant Kumar | 06a2673 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 877 | /// operator-specific functions that, given the known-zero or known-one bits |
Sanjay Patel | de116cc | 2017-10-16 14:46:37 +0000 | [diff] [blame] | 878 | /// respectively, and a shift amount, compute the implied known-zero or |
| 879 | /// known-one bits of the shift operator's result respectively for that shift |
| 880 | /// amount. The results from calling KZF and KOF are conservatively combined for |
| 881 | /// all permitted shift amounts. |
David Majnemer | 5896550 | 2016-08-23 20:52:00 +0000 | [diff] [blame] | 882 | static void computeKnownBitsFromShiftOperator( |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 883 | const Operator *I, KnownBits &Known, KnownBits &Known2, |
| 884 | unsigned Depth, const Query &Q, |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 885 | function_ref<APInt(const APInt &, unsigned)> KZF, |
| 886 | function_ref<APInt(const APInt &, unsigned)> KOF) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 887 | unsigned BitWidth = Known.getBitWidth(); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 888 | |
| 889 | if (auto *SA = dyn_cast<ConstantInt>(I->getOperand(1))) { |
| 890 | unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1); |
| 891 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 892 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 893 | Known.Zero = KZF(Known.Zero, ShiftAmt); |
| 894 | Known.One = KOF(Known.One, ShiftAmt); |
Sanjay Patel | dc813cc | 2017-10-12 17:31:46 +0000 | [diff] [blame] | 895 | // If the known bits conflict, this must be an overflowing left shift, so |
| 896 | // the shift result is poison. We can return anything we want. Choose 0 for |
| 897 | // the best folding opportunity. |
| 898 | if (Known.hasConflict()) |
| 899 | Known.setAllZero(); |
Evgeny Stupachenko | 2af8582 | 2016-08-24 23:01:33 +0000 | [diff] [blame] | 900 | |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 901 | return; |
| 902 | } |
| 903 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 904 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 905 | |
Sanjay Patel | dc813cc | 2017-10-12 17:31:46 +0000 | [diff] [blame] | 906 | // If the shift amount could be greater than or equal to the bit-width of the |
| 907 | // LHS, the value could be poison, but bail out because the check below is |
| 908 | // expensive. TODO: Should we just carry on? |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 909 | if ((~Known.Zero).uge(BitWidth)) { |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 910 | Known.resetAll(); |
Oliver Stannard | 4e5ac8a | 2017-03-14 10:13:17 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 914 | // Note: We cannot use Known.Zero.getLimitedValue() here, because if |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 915 | // BitWidth > 64 and any upper bits are known, we'll end up returning the |
| 916 | // limit value (which implies all bits are known). |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 917 | uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue(); |
| 918 | uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue(); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 919 | |
| 920 | // It would be more-clearly correct to use the two temporaries for this |
| 921 | // calculation. Reusing the APInts here to prevent unnecessary allocations. |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 922 | Known.resetAll(); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 923 | |
James Molloy | bf7b3fe | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 924 | // If we know the shifter operand is nonzero, we can sometimes infer more |
| 925 | // known bits. However this is expensive to compute, so be lazy about it and |
| 926 | // only compute it when absolutely necessary. |
| 927 | Optional<bool> ShifterOperandIsNonZero; |
| 928 | |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 929 | // Early exit if we can't constrain any well-defined shift amount. |
Craig Topper | 996d9b5 | 2017-06-14 17:04:59 +0000 | [diff] [blame] | 930 | if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) && |
| 931 | !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) { |
Sanjay Patel | de116cc | 2017-10-16 14:46:37 +0000 | [diff] [blame] | 932 | ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q); |
James Molloy | bf7b3fe | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 933 | if (!*ShifterOperandIsNonZero) |
| 934 | return; |
| 935 | } |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 936 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 937 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 938 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 939 | Known.Zero.setAllBits(); |
| 940 | Known.One.setAllBits(); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 941 | for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) { |
| 942 | // Combine the shifted known input bits only for those shift amounts |
| 943 | // compatible with its known constraints. |
| 944 | if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt) |
| 945 | continue; |
| 946 | if ((ShiftAmt | ShiftAmtKO) != ShiftAmt) |
| 947 | continue; |
James Molloy | bf7b3fe | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 948 | // If we know the shifter is nonzero, we may be able to infer more known |
| 949 | // bits. This check is sunk down as far as possible to avoid the expensive |
| 950 | // call to isKnownNonZero if the cheaper checks above fail. |
| 951 | if (ShiftAmt == 0) { |
| 952 | if (!ShifterOperandIsNonZero.hasValue()) |
| 953 | ShifterOperandIsNonZero = |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 954 | isKnownNonZero(I->getOperand(1), Depth + 1, Q); |
James Molloy | bf7b3fe | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 955 | if (*ShifterOperandIsNonZero) |
| 956 | continue; |
| 957 | } |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 958 | |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 959 | Known.Zero &= KZF(Known2.Zero, ShiftAmt); |
| 960 | Known.One &= KOF(Known2.One, ShiftAmt); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Sanjay Patel | dc813cc | 2017-10-12 17:31:46 +0000 | [diff] [blame] | 963 | // If the known bits conflict, the result is poison. Return a 0 and hope the |
| 964 | // caller can further optimize that. |
| 965 | if (Known.hasConflict()) |
| 966 | Known.setAllZero(); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 969 | static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known, |
| 970 | unsigned Depth, const Query &Q) { |
| 971 | unsigned BitWidth = Known.getBitWidth(); |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 972 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 973 | KnownBits Known2(Known); |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 974 | switch (I->getOpcode()) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 975 | default: break; |
Rafael Espindola | 7c7121e | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 976 | case Instruction::Load: |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 977 | if (MDNode *MD = |
| 978 | Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range)) |
Craig Topper | 955683a | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 979 | computeKnownBitsFromRangeMetadata(*MD, Known); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 980 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 981 | case Instruction::And: { |
| 982 | // If either the LHS or the RHS are Zero, the result is zero. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 983 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); |
| 984 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 986 | // Output known-1 bits are only known if set in both the LHS & RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 987 | Known.One &= Known2.One; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 988 | // Output known-0 are known to be clear if zero in either the LHS | RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 989 | Known.Zero |= Known2.Zero; |
Philip Reames | 6054bad | 2015-11-10 18:46:14 +0000 | [diff] [blame] | 990 | |
| 991 | // and(x, add (x, -1)) is a common idiom that always clears the low bit; |
| 992 | // here we handle the more general case of adding any odd number by |
| 993 | // matching the form add(x, add(x, y)) where y is odd. |
| 994 | // TODO: This could be generalized to clearing any bit set in y where the |
| 995 | // following bit is known to be unset in y. |
Roman Lebedev | a9bc2db | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 996 | Value *X = nullptr, *Y = nullptr; |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 997 | if (!Known.Zero[0] && !Known.One[0] && |
Roman Lebedev | a9bc2db | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 998 | match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) { |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 999 | Known2.resetAll(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1000 | computeKnownBits(Y, Known2, Depth + 1, Q); |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1001 | if (Known2.countMinTrailingOnes() > 0) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1002 | Known.Zero.setBit(0); |
Philip Reames | 6054bad | 2015-11-10 18:46:14 +0000 | [diff] [blame] | 1003 | } |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1004 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1005 | } |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 1006 | case Instruction::Or: |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1007 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); |
| 1008 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1010 | // Output known-0 bits are only known if clear in both the LHS & RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1011 | Known.Zero &= Known2.Zero; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1012 | // Output known-1 are known to be set if set in either the LHS | RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1013 | Known.One |= Known2.One; |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1014 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1015 | case Instruction::Xor: { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1016 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); |
| 1017 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1019 | // Output known-0 bits are known if clear or set in both the LHS & RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1020 | APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1021 | // Output known-1 are known to be set if set in only one of the LHS, RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1022 | Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero); |
| 1023 | Known.Zero = std::move(KnownZeroOut); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1024 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1025 | } |
| 1026 | case Instruction::Mul: { |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1027 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1028 | computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, Known, |
| 1029 | Known2, Depth, Q); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1030 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1031 | } |
| 1032 | case Instruction::UDiv: { |
| 1033 | // For the purposes of computing leading zeros we can conservatively |
| 1034 | // treat a udiv as a logical right shift by the power of 2 known to |
| 1035 | // be less than the denominator. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1036 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1037 | unsigned LeadZ = Known2.countMinLeadingZeros(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1038 | |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1039 | Known2.resetAll(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1040 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1041 | unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros(); |
| 1042 | if (RHSMaxLeadingZeros != BitWidth) |
| 1043 | LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1044 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1045 | Known.Zero.setHighBits(LeadZ); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1046 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1047 | } |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1048 | case Instruction::Select: { |
Craig Topper | a402bd1 | 2017-04-13 20:39:37 +0000 | [diff] [blame] | 1049 | const Value *LHS, *RHS; |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1050 | SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor; |
| 1051 | if (SelectPatternResult::isMinOrMax(SPF)) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1052 | computeKnownBits(RHS, Known, Depth + 1, Q); |
| 1053 | computeKnownBits(LHS, Known2, Depth + 1, Q); |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1054 | } else { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1055 | computeKnownBits(I->getOperand(2), Known, Depth + 1, Q); |
| 1056 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | unsigned MaxHighOnes = 0; |
| 1060 | unsigned MaxHighZeros = 0; |
| 1061 | if (SPF == SPF_SMAX) { |
| 1062 | // If both sides are negative, the result is negative. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1063 | if (Known.isNegative() && Known2.isNegative()) |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1064 | // We can derive a lower bound on the result by taking the max of the |
| 1065 | // leading one bits. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1066 | MaxHighOnes = |
| 1067 | std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes()); |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1068 | // If either side is non-negative, the result is non-negative. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1069 | else if (Known.isNonNegative() || Known2.isNonNegative()) |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1070 | MaxHighZeros = 1; |
| 1071 | } else if (SPF == SPF_SMIN) { |
| 1072 | // If both sides are non-negative, the result is non-negative. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1073 | if (Known.isNonNegative() && Known2.isNonNegative()) |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1074 | // We can derive an upper bound on the result by taking the max of the |
| 1075 | // leading zero bits. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1076 | MaxHighZeros = std::max(Known.countMinLeadingZeros(), |
| 1077 | Known2.countMinLeadingZeros()); |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1078 | // If either side is negative, the result is negative. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1079 | else if (Known.isNegative() || Known2.isNegative()) |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1080 | MaxHighOnes = 1; |
| 1081 | } else if (SPF == SPF_UMAX) { |
| 1082 | // We can derive a lower bound on the result by taking the max of the |
| 1083 | // leading one bits. |
| 1084 | MaxHighOnes = |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1085 | std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes()); |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1086 | } else if (SPF == SPF_UMIN) { |
| 1087 | // We can derive an upper bound on the result by taking the max of the |
| 1088 | // leading zero bits. |
| 1089 | MaxHighZeros = |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1090 | std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros()); |
Craig Topper | 981880e | 2018-05-25 19:18:09 +0000 | [diff] [blame] | 1091 | } else if (SPF == SPF_ABS) { |
| 1092 | // RHS from matchSelectPattern returns the negation part of abs pattern. |
| 1093 | // If the negate has an NSW flag we can assume the sign bit of the result |
| 1094 | // will be 0 because that makes abs(INT_MIN) undefined. |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1095 | if (Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS))) |
Craig Topper | 981880e | 2018-05-25 19:18:09 +0000 | [diff] [blame] | 1096 | MaxHighZeros = 1; |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1099 | // Only known if known in both the LHS and RHS. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1100 | Known.One &= Known2.One; |
| 1101 | Known.Zero &= Known2.Zero; |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1102 | if (MaxHighOnes > 0) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1103 | Known.One.setHighBits(MaxHighOnes); |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1104 | if (MaxHighZeros > 0) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1105 | Known.Zero.setHighBits(MaxHighZeros); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1106 | break; |
David Majnemer | 2982845 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1107 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1108 | case Instruction::FPTrunc: |
| 1109 | case Instruction::FPExt: |
| 1110 | case Instruction::FPToUI: |
| 1111 | case Instruction::FPToSI: |
| 1112 | case Instruction::SIToFP: |
| 1113 | case Instruction::UIToFP: |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1114 | break; // Can't work with floating point. |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1115 | case Instruction::PtrToInt: |
| 1116 | case Instruction::IntToPtr: |
Justin Bogner | 7d7a23e | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 1117 | // Fall through and handle them the same as zext/trunc. |
| 1118 | LLVM_FALLTHROUGH; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1119 | case Instruction::ZExt: |
| 1120 | case Instruction::Trunc: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1121 | Type *SrcTy = I->getOperand(0)->getType(); |
Nadav Rotem | 12145f0 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | b9a4ddb | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1123 | unsigned SrcBitWidth; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1124 | // Note that we handle pointer operands here because of inttoptr/ptrtoint |
| 1125 | // which fall through here. |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 1126 | Type *ScalarTy = SrcTy->getScalarType(); |
| 1127 | SrcBitWidth = ScalarTy->isPointerTy() ? |
| 1128 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : |
| 1129 | Q.DL.getTypeSizeInBits(ScalarTy); |
Nadav Rotem | 12145f0 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1130 | |
| 1131 | assert(SrcBitWidth && "SrcBitWidth can't be zero"); |
Craig Topper | f782218 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1132 | Known = Known.zextOrTrunc(SrcBitWidth); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1133 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Craig Topper | f782218 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1134 | Known = Known.zextOrTrunc(BitWidth); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1135 | // Any top bits are known to be zero. |
| 1136 | if (BitWidth > SrcBitWidth) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1137 | Known.Zero.setBitsFrom(SrcBitWidth); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1138 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1139 | } |
| 1140 | case Instruction::BitCast: { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1141 | Type *SrcTy = I->getOperand(0)->getType(); |
Vedant Kumar | c2801b1 | 2018-07-06 20:17:42 +0000 | [diff] [blame] | 1142 | if (SrcTy->isIntOrPtrTy() && |
Chris Lattner | 0dabb0b | 2009-07-02 16:04:08 +0000 | [diff] [blame] | 1143 | // TODO: For now, not handling conversions like: |
| 1144 | // (bitcast i64 %x to <2 x i32>) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1145 | !I->getType()->isVectorTy()) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1146 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1147 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1148 | } |
| 1149 | break; |
| 1150 | } |
| 1151 | case Instruction::SExt: { |
| 1152 | // Compute the bits in the result that are not present in the input. |
Chris Lattner | b9a4ddb | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1153 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1154 | |
Craig Topper | f782218 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1155 | Known = Known.trunc(SrcBitWidth); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1156 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1157 | // If the sign bit of the input is known set or clear, then we know the |
| 1158 | // top bits of the result. |
Craig Topper | f782218 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1159 | Known = Known.sext(BitWidth); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1160 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1161 | } |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1162 | case Instruction::Shl: { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1163 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1164 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1165 | auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) { |
| 1166 | APInt KZResult = KnownZero << ShiftAmt; |
| 1167 | KZResult.setLowBits(ShiftAmt); // Low bits known 0. |
Evgeny Stupachenko | 2af8582 | 2016-08-24 23:01:33 +0000 | [diff] [blame] | 1168 | // If this shift has "nsw" keyword, then the result is either a poison |
| 1169 | // value or has the same sign bit as the first operand. |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1170 | if (NSW && KnownZero.isSignBitSet()) |
| 1171 | KZResult.setSignBit(); |
| 1172 | return KZResult; |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1173 | }; |
| 1174 | |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1175 | auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) { |
| 1176 | APInt KOResult = KnownOne << ShiftAmt; |
| 1177 | if (NSW && KnownOne.isSignBitSet()) |
| 1178 | KOResult.setSignBit(); |
| 1179 | return KOResult; |
| 1180 | }; |
| 1181 | |
| 1182 | computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1183 | break; |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1184 | } |
| 1185 | case Instruction::LShr: { |
Sanjay Patel | de116cc | 2017-10-16 14:46:37 +0000 | [diff] [blame] | 1186 | // (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1187 | auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) { |
| 1188 | APInt KZResult = KnownZero.lshr(ShiftAmt); |
| 1189 | // High bits known zero. |
| 1190 | KZResult.setHighBits(ShiftAmt); |
| 1191 | return KZResult; |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1192 | }; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1193 | |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1194 | auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) { |
| 1195 | return KnownOne.lshr(ShiftAmt); |
| 1196 | }; |
| 1197 | |
| 1198 | computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1199 | break; |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1200 | } |
| 1201 | case Instruction::AShr: { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1202 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1203 | auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) { |
| 1204 | return KnownZero.ashr(ShiftAmt); |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1205 | }; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1206 | |
Sam McCall | c7ae88e | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1207 | auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) { |
| 1208 | return KnownOne.ashr(ShiftAmt); |
| 1209 | }; |
| 1210 | |
| 1211 | computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1212 | break; |
Hal Finkel | 71ccd4f | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1213 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1214 | case Instruction::Sub: { |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1215 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Jay Foad | 6b54371 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1216 | computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1217 | Known, Known2, Depth, Q); |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1218 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1219 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1220 | case Instruction::Add: { |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1221 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Jay Foad | 6b54371 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1222 | computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1223 | Known, Known2, Depth, Q); |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1224 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1225 | } |
| 1226 | case Instruction::SRem: |
| 1227 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Duncan Sands | cfd54181 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1228 | APInt RA = Rem->getValue().abs(); |
| 1229 | if (RA.isPowerOf2()) { |
| 1230 | APInt LowBits = RA - 1; |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1231 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1232 | |
Duncan Sands | cfd54181 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1233 | // The low bits of the first operand are unchanged by the srem. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1234 | Known.Zero = Known2.Zero & LowBits; |
| 1235 | Known.One = Known2.One & LowBits; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1236 | |
Duncan Sands | cfd54181 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1237 | // If the first operand is non-negative or has all low bits zero, then |
| 1238 | // the upper bits are all zero. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1239 | if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero)) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1240 | Known.Zero |= ~LowBits; |
Duncan Sands | cfd54181 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1241 | |
| 1242 | // If the first operand is negative and not all low bits are zero, then |
| 1243 | // the upper bits are all one. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1244 | if (Known2.isNegative() && LowBits.intersects(Known2.One)) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1245 | Known.One |= ~LowBits; |
Duncan Sands | cfd54181 | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1246 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1247 | assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?"); |
Craig Topper | 9563b95 | 2017-04-16 21:46:12 +0000 | [diff] [blame] | 1248 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
Nick Lewycky | c14bc77 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1251 | |
| 1252 | // The sign bit is the LHS's sign bit, except when the result of the |
| 1253 | // remainder is zero. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1254 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | 9563b95 | 2017-04-16 21:46:12 +0000 | [diff] [blame] | 1255 | // If it's known zero, our sign bit is also zero. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1256 | if (Known2.isNonNegative()) |
| 1257 | Known.makeNonNegative(); |
Nick Lewycky | c14bc77 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1259 | break; |
| 1260 | case Instruction::URem: { |
| 1261 | if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) { |
Benjamin Kramer | 04a303b | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 1262 | const APInt &RA = Rem->getValue(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1263 | if (RA.isPowerOf2()) { |
| 1264 | APInt LowBits = (RA - 1); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1265 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1266 | Known.Zero |= ~LowBits; |
| 1267 | Known.One &= LowBits; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | // Since the result is less than or equal to either operand, any leading |
| 1273 | // zero bits in either operand must also exist in the result. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1274 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
| 1275 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1276 | |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1277 | unsigned Leaders = |
| 1278 | std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros()); |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1279 | Known.resetAll(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1280 | Known.Zero.setHighBits(Leaders); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | |
Victor Hernandez | a276c60 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 1284 | case Instruction::Alloca: { |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1285 | const AllocaInst *AI = cast<AllocaInst>(I); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1286 | unsigned Align = AI->getAlignment(); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1287 | if (Align == 0) |
Eduard Burtescu | be478ba | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 1288 | Align = Q.DL.getABITypeAlignment(AI->getAllocatedType()); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1290 | if (Align > 0) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1291 | Known.Zero.setLowBits(countTrailingZeros(Align)); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | } |
| 1294 | case Instruction::GetElementPtr: { |
| 1295 | // Analyze all of the subscripts of this getelementptr instruction |
| 1296 | // to determine if we can prove known low zero bits. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1297 | KnownBits LocalKnown(BitWidth); |
| 1298 | computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q); |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1299 | unsigned TrailZ = LocalKnown.countMinTrailingZeros(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1300 | |
| 1301 | gep_type_iterator GTI = gep_type_begin(I); |
| 1302 | for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) { |
| 1303 | Value *Index = I->getOperand(i); |
Peter Collingbourne | 0611580 | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 1304 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1305 | // Handle struct member offset arithmetic. |
Matt Arsenault | c4d070a | 2013-08-19 21:43:16 +0000 | [diff] [blame] | 1306 | |
| 1307 | // Handle case when index is vector zeroinitializer |
| 1308 | Constant *CIndex = cast<Constant>(Index); |
| 1309 | if (CIndex->isZeroValue()) |
| 1310 | continue; |
| 1311 | |
| 1312 | if (CIndex->getType()->isVectorTy()) |
| 1313 | Index = CIndex->getSplatValue(); |
| 1314 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1315 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1316 | const StructLayout *SL = Q.DL.getStructLayout(STy); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1317 | uint64_t Offset = SL->getElementOffset(Idx); |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1318 | TrailZ = std::min<unsigned>(TrailZ, |
| 1319 | countTrailingZeros(Offset)); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1320 | } else { |
| 1321 | // Handle array index arithmetic. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1322 | Type *IndexedTy = GTI.getIndexedType(); |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1323 | if (!IndexedTy->isSized()) { |
| 1324 | TrailZ = 0; |
| 1325 | break; |
| 1326 | } |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1327 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1328 | uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1329 | LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0); |
| 1330 | computeKnownBits(Index, LocalKnown, Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1331 | TrailZ = std::min(TrailZ, |
Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1332 | unsigned(countTrailingZeros(TypeSize) + |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1333 | LocalKnown.countMinTrailingZeros())); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1336 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1337 | Known.Zero.setLowBits(TrailZ); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | case Instruction::PHI: { |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1341 | const PHINode *P = cast<PHINode>(I); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1342 | // Handle the case of a simple two-predecessor recurrence PHI. |
| 1343 | // There's a lot more that could theoretically be done here, but |
| 1344 | // this is sufficient to catch some interesting cases. |
| 1345 | if (P->getNumIncomingValues() == 2) { |
| 1346 | for (unsigned i = 0; i != 2; ++i) { |
| 1347 | Value *L = P->getIncomingValue(i); |
| 1348 | Value *R = P->getIncomingValue(!i); |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1349 | Operator *LU = dyn_cast<Operator>(L); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1350 | if (!LU) |
| 1351 | continue; |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1352 | unsigned Opcode = LU->getOpcode(); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1353 | // Check for operations that have the property that if |
| 1354 | // both their operands have low zero bits, the result |
Artur Pilipenko | a267310 | 2016-08-22 13:14:07 +0000 | [diff] [blame] | 1355 | // will have low zero bits. |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1356 | if (Opcode == Instruction::Add || |
| 1357 | Opcode == Instruction::Sub || |
| 1358 | Opcode == Instruction::And || |
| 1359 | Opcode == Instruction::Or || |
| 1360 | Opcode == Instruction::Mul) { |
| 1361 | Value *LL = LU->getOperand(0); |
| 1362 | Value *LR = LU->getOperand(1); |
| 1363 | // Find a recurrence. |
| 1364 | if (LL == I) |
| 1365 | L = LR; |
| 1366 | else if (LR == I) |
| 1367 | L = LL; |
| 1368 | else |
| 1369 | break; |
| 1370 | // Ok, we have a PHI of the form L op= R. Check for low |
| 1371 | // zero bits. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1372 | computeKnownBits(R, Known2, Depth + 1, Q); |
David Greene | c714f13 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1373 | |
| 1374 | // We need to take the minimum number of known bits |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1375 | KnownBits Known3(Known); |
| 1376 | computeKnownBits(L, Known3, Depth + 1, Q); |
David Greene | c714f13 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1377 | |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1378 | Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(), |
| 1379 | Known3.countMinTrailingZeros())); |
Artur Pilipenko | db1717e | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1380 | |
Artur Pilipenko | db1717e | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1381 | auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU); |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1382 | if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) { |
Artur Pilipenko | db1717e | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1383 | // If initial value of recurrence is nonnegative, and we are adding |
| 1384 | // a nonnegative number with nsw, the result can only be nonnegative |
| 1385 | // or poison value regardless of the number of times we execute the |
| 1386 | // add in phi recurrence. If initial value is negative and we are |
| 1387 | // adding a negative number with nsw, the result can only be |
| 1388 | // negative or poison value. Similar arguments apply to sub and mul. |
| 1389 | // |
| 1390 | // (add non-negative, non-negative) --> non-negative |
| 1391 | // (add negative, negative) --> negative |
| 1392 | if (Opcode == Instruction::Add) { |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1393 | if (Known2.isNonNegative() && Known3.isNonNegative()) |
| 1394 | Known.makeNonNegative(); |
| 1395 | else if (Known2.isNegative() && Known3.isNegative()) |
| 1396 | Known.makeNegative(); |
Artur Pilipenko | db1717e | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | // (sub nsw non-negative, negative) --> non-negative |
| 1400 | // (sub nsw negative, non-negative) --> negative |
| 1401 | else if (Opcode == Instruction::Sub && LL == I) { |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1402 | if (Known2.isNonNegative() && Known3.isNegative()) |
| 1403 | Known.makeNonNegative(); |
| 1404 | else if (Known2.isNegative() && Known3.isNonNegative()) |
| 1405 | Known.makeNegative(); |
Artur Pilipenko | db1717e | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | // (mul nsw non-negative, non-negative) --> non-negative |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1409 | else if (Opcode == Instruction::Mul && Known2.isNonNegative() && |
| 1410 | Known3.isNonNegative()) |
| 1411 | Known.makeNonNegative(); |
Artur Pilipenko | db1717e | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1418 | |
Nick Lewycky | 3b739d2 | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1419 | // Unreachable blocks may have zero-operand PHI nodes. |
| 1420 | if (P->getNumIncomingValues() == 0) |
Jay Foad | b7ba5c2 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1421 | break; |
Nick Lewycky | 3b739d2 | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1422 | |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1423 | // Otherwise take the unions of the known bit sets of the operands, |
| 1424 | // taking conservative care to avoid excessive recursion. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1425 | if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) { |
Duncan Sands | 606199f | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1426 | // Skip if every incoming value references to ourself. |
Nuno Lopes | 0fd518b | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 1427 | if (dyn_cast_or_null<UndefValue>(P->hasConstantValue())) |
Duncan Sands | 606199f | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1430 | Known.Zero.setAllBits(); |
| 1431 | Known.One.setAllBits(); |
Pete Cooper | f23c6af | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1432 | for (Value *IncValue : P->incoming_values()) { |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1433 | // Skip direct self references. |
Pete Cooper | f23c6af | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1434 | if (IncValue == P) continue; |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1435 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1436 | Known2 = KnownBits(BitWidth); |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1437 | // Recurse, but cap the recursion to one level, because we don't |
| 1438 | // want to waste time spinning around in loops. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1439 | computeKnownBits(IncValue, Known2, MaxDepth - 1, Q); |
| 1440 | Known.Zero &= Known2.Zero; |
| 1441 | Known.One &= Known2.One; |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1442 | // If all bits have been ruled out, there's no need to check |
| 1443 | // more operands. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1444 | if (!Known.Zero && !Known.One) |
Dan Gohman | 9004c8a | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | case Instruction::Call: |
Jingyue Wu | e4d0a5e | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1451 | case Instruction::Invoke: |
Hal Finkel | d90d613 | 2016-07-11 02:25:14 +0000 | [diff] [blame] | 1452 | // If range metadata is attached to this call, set known bits from that, |
| 1453 | // and then intersect with known bits based on other properties of the |
| 1454 | // function. |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1455 | if (MDNode *MD = |
| 1456 | Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range)) |
Craig Topper | 955683a | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 1457 | computeKnownBitsFromRangeMetadata(*MD, Known); |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1458 | if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1459 | computeKnownBits(RV, Known2, Depth + 1, Q); |
| 1460 | Known.Zero |= Known2.Zero; |
| 1461 | Known.One |= Known2.One; |
Hal Finkel | d90d613 | 2016-07-11 02:25:14 +0000 | [diff] [blame] | 1462 | } |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1463 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1464 | switch (II->getIntrinsicID()) { |
| 1465 | default: break; |
Chad Rosier | 7eb6751 | 2017-01-17 17:23:51 +0000 | [diff] [blame] | 1466 | case Intrinsic::bitreverse: |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1467 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1468 | Known.Zero |= Known2.Zero.reverseBits(); |
| 1469 | Known.One |= Known2.One.reverseBits(); |
Chad Rosier | 7eb6751 | 2017-01-17 17:23:51 +0000 | [diff] [blame] | 1470 | break; |
Philip Reames | 8b2dfae | 2015-10-06 20:20:45 +0000 | [diff] [blame] | 1471 | case Intrinsic::bswap: |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1472 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1473 | Known.Zero |= Known2.Zero.byteSwap(); |
| 1474 | Known.One |= Known2.One.byteSwap(); |
Philip Reames | 8b2dfae | 2015-10-06 20:20:45 +0000 | [diff] [blame] | 1475 | break; |
Craig Topper | d995493 | 2017-05-08 17:22:34 +0000 | [diff] [blame] | 1476 | case Intrinsic::ctlz: { |
| 1477 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1478 | // If we have a known 1, its position is our upper bound. |
| 1479 | unsigned PossibleLZ = Known2.One.countLeadingZeros(); |
Benjamin Kramer | 009da05 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1480 | // If this call is undefined for 0, the result will be less than 2^n. |
| 1481 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
Craig Topper | d995493 | 2017-05-08 17:22:34 +0000 | [diff] [blame] | 1482 | PossibleLZ = std::min(PossibleLZ, BitWidth - 1); |
| 1483 | unsigned LowBits = Log2_32(PossibleLZ)+1; |
| 1484 | Known.Zero.setBitsFrom(LowBits); |
| 1485 | break; |
| 1486 | } |
| 1487 | case Intrinsic::cttz: { |
| 1488 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1489 | // If we have a known 1, its position is our upper bound. |
| 1490 | unsigned PossibleTZ = Known2.One.countTrailingZeros(); |
| 1491 | // If this call is undefined for 0, the result will be less than 2^n. |
| 1492 | if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext())) |
| 1493 | PossibleTZ = std::min(PossibleTZ, BitWidth - 1); |
| 1494 | unsigned LowBits = Log2_32(PossibleTZ)+1; |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1495 | Known.Zero.setBitsFrom(LowBits); |
Benjamin Kramer | 009da05 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | case Intrinsic::ctpop: { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1499 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Philip Reames | c52d6d5 | 2015-10-14 22:42:12 +0000 | [diff] [blame] | 1500 | // We can bound the space the count needs. Also, bits known to be zero |
| 1501 | // can't contribute to the population. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1502 | unsigned BitsPossiblySet = Known2.countMaxPopulation(); |
Craig Topper | 3557926 | 2017-04-14 06:43:34 +0000 | [diff] [blame] | 1503 | unsigned LowBits = Log2_32(BitsPossiblySet)+1; |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1504 | Known.Zero.setBitsFrom(LowBits); |
Philip Reames | c52d6d5 | 2015-10-14 22:42:12 +0000 | [diff] [blame] | 1505 | // TODO: we could bound KnownOne using the lower bound on the number |
| 1506 | // of bits which might be set provided by popcnt KnownOne2. |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1507 | break; |
| 1508 | } |
Nikita Popov | 9054832 | 2018-12-02 14:14:11 +0000 | [diff] [blame] | 1509 | case Intrinsic::fshr: |
| 1510 | case Intrinsic::fshl: { |
| 1511 | const APInt *SA; |
| 1512 | if (!match(I->getOperand(2), m_APInt(SA))) |
| 1513 | break; |
| 1514 | |
| 1515 | // Normalize to funnel shift left. |
| 1516 | uint64_t ShiftAmt = SA->urem(BitWidth); |
| 1517 | if (II->getIntrinsicID() == Intrinsic::fshr) |
| 1518 | ShiftAmt = BitWidth - ShiftAmt; |
| 1519 | |
| 1520 | KnownBits Known3(Known); |
| 1521 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
| 1522 | computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q); |
| 1523 | |
| 1524 | Known.Zero = |
| 1525 | Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt); |
| 1526 | Known.One = |
| 1527 | Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt); |
| 1528 | break; |
| 1529 | } |
Chad Rosier | 6266031 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 1530 | case Intrinsic::x86_sse42_crc32_64_64: |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1531 | Known.Zero.setBitsFrom(32); |
Evan Cheng | cb559c1 | 2011-05-22 18:25:30 +0000 | [diff] [blame] | 1532 | break; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | break; |
Bjorn Pettersson | c4e191a | 2016-10-06 09:56:21 +0000 | [diff] [blame] | 1536 | case Instruction::ExtractElement: |
| 1537 | // Look through extract element. At the moment we keep this simple and skip |
| 1538 | // tracking the specific element. But at least we might find information |
| 1539 | // valid for all elements of the vector (for example if vector is sign |
| 1540 | // extended, shifted, etc). |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1541 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Bjorn Pettersson | c4e191a | 2016-10-06 09:56:21 +0000 | [diff] [blame] | 1542 | break; |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1543 | case Instruction::ExtractValue: |
| 1544 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) { |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1545 | const ExtractValueInst *EVI = cast<ExtractValueInst>(I); |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1546 | if (EVI->getNumIndices() != 1) break; |
| 1547 | if (EVI->getIndices()[0] == 0) { |
| 1548 | switch (II->getIntrinsicID()) { |
| 1549 | default: break; |
| 1550 | case Intrinsic::uadd_with_overflow: |
| 1551 | case Intrinsic::sadd_with_overflow: |
Jay Foad | 6b54371 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1552 | computeKnownBitsAddSub(true, II->getArgOperand(0), |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1553 | II->getArgOperand(1), false, Known, Known2, |
| 1554 | Depth, Q); |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1555 | break; |
| 1556 | case Intrinsic::usub_with_overflow: |
| 1557 | case Intrinsic::ssub_with_overflow: |
Jay Foad | 6b54371 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1558 | computeKnownBitsAddSub(false, II->getArgOperand(0), |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1559 | II->getArgOperand(1), false, Known, Known2, |
| 1560 | Depth, Q); |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1561 | break; |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1562 | case Intrinsic::umul_with_overflow: |
| 1563 | case Intrinsic::smul_with_overflow: |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1564 | computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1565 | Known, Known2, Depth, Q); |
Nick Lewycky | f201a06 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1566 | break; |
Nick Lewycky | 00cbccc | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1570 | } |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /// Determine which bits of V are known to be either zero or one and return |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 1574 | /// them. |
| 1575 | KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) { |
| 1576 | KnownBits Known(getBitWidth(V->getType(), Q.DL)); |
| 1577 | computeKnownBits(V, Known, Depth, Q); |
| 1578 | return Known; |
| 1579 | } |
| 1580 | |
| 1581 | /// Determine which bits of V are known to be either zero or one and return |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1582 | /// them in the Known bit set. |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1583 | /// |
| 1584 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 1585 | /// we cannot optimize based on the assumption that it is zero without changing |
| 1586 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 1587 | /// optimized based on the contradictory assumption that it is non-zero. |
| 1588 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 1589 | /// this won't lose us code quality. |
| 1590 | /// |
| 1591 | /// This function is defined on values with integer type, values with pointer |
| 1592 | /// type, and vectors of integers. In the case |
| 1593 | /// where V is a vector, known zero, and known one values are the |
| 1594 | /// same width as the vector element, and the bit is set only if it is true |
| 1595 | /// for all of the elements in the vector. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1596 | void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, |
| 1597 | const Query &Q) { |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1598 | assert(V && "No Value?"); |
| 1599 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1600 | unsigned BitWidth = Known.getBitWidth(); |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1601 | |
Craig Topper | eb41f6a | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 1602 | assert((V->getType()->isIntOrIntVectorTy(BitWidth) || |
Craig Topper | 1060082 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1603 | V->getType()->isPtrOrPtrVectorTy()) && |
Sanjay Patel | a96468b | 2016-06-02 20:01:37 +0000 | [diff] [blame] | 1604 | "Not integer or pointer type!"); |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 1605 | |
| 1606 | Type *ScalarTy = V->getType()->getScalarType(); |
| 1607 | unsigned ExpectedWidth = ScalarTy->isPointerTy() ? |
| 1608 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy); |
| 1609 | assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth"); |
Craig Topper | fe5b80b | 2017-03-23 07:06:39 +0000 | [diff] [blame] | 1610 | (void)BitWidth; |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 1611 | (void)ExpectedWidth; |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1612 | |
Sanjay Patel | 59183b9 | 2016-09-16 21:20:36 +0000 | [diff] [blame] | 1613 | const APInt *C; |
| 1614 | if (match(V, m_APInt(C))) { |
| 1615 | // We know all of the bits for a scalar constant or a splat vector constant! |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1616 | Known.One = *C; |
| 1617 | Known.Zero = ~Known.One; |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1618 | return; |
| 1619 | } |
| 1620 | // Null and aggregate-zero are all-zeros. |
Sanjay Patel | 645115f | 2016-05-23 17:57:54 +0000 | [diff] [blame] | 1621 | if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) { |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1622 | Known.setAllZero(); |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1623 | return; |
| 1624 | } |
| 1625 | // Handle a constant vector by taking the intersection of the known bits of |
David Majnemer | c454f3e | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1626 | // each element. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1627 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) { |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1628 | // We know that CDS must be a vector of integers. Take the intersection of |
| 1629 | // each element. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1630 | Known.Zero.setAllBits(); Known.One.setAllBits(); |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1631 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Craig Topper | 3afd582 | 2017-10-21 16:35:39 +0000 | [diff] [blame] | 1632 | APInt Elt = CDS->getElementAsAPInt(i); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1633 | Known.Zero &= ~Elt; |
| 1634 | Known.One &= Elt; |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1635 | } |
| 1636 | return; |
| 1637 | } |
| 1638 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1639 | if (const auto *CV = dyn_cast<ConstantVector>(V)) { |
David Majnemer | c454f3e | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1640 | // We know that CV must be a vector of integers. Take the intersection of |
| 1641 | // each element. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1642 | Known.Zero.setAllBits(); Known.One.setAllBits(); |
David Majnemer | c454f3e | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1643 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { |
| 1644 | Constant *Element = CV->getAggregateElement(i); |
| 1645 | auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); |
| 1646 | if (!ElementCI) { |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1647 | Known.resetAll(); |
David Majnemer | c454f3e | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
Craig Topper | 3afd582 | 2017-10-21 16:35:39 +0000 | [diff] [blame] | 1650 | const APInt &Elt = ElementCI->getValue(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1651 | Known.Zero &= ~Elt; |
| 1652 | Known.One &= Elt; |
David Majnemer | c454f3e | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1653 | } |
| 1654 | return; |
| 1655 | } |
| 1656 | |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1657 | // Start out not knowing anything. |
Craig Topper | ace8b39 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1658 | Known.resetAll(); |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1659 | |
Duncan P. N. Exon Smith | 5cb2954 | 2016-09-24 20:42:02 +0000 | [diff] [blame] | 1660 | // We can't imply anything about undefs. |
| 1661 | if (isa<UndefValue>(V)) |
| 1662 | return; |
| 1663 | |
| 1664 | // There's no point in looking through other users of ConstantData for |
| 1665 | // assumptions. Confirm that we've handled them all. |
| 1666 | assert(!isa<ConstantData>(V) && "Unhandled constant data!"); |
| 1667 | |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1668 | // Limit search depth. |
| 1669 | // All recursive calls that increase depth must come after this. |
| 1670 | if (Depth == MaxDepth) |
| 1671 | return; |
| 1672 | |
| 1673 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has |
| 1674 | // the bits of its aliasee. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1675 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | c9e3e3c | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 1676 | if (!GA->isInterposable()) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1677 | computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q); |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1678 | return; |
| 1679 | } |
| 1680 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1681 | if (const Operator *I = dyn_cast<Operator>(V)) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1682 | computeKnownBitsFromOperator(I, Known, Depth, Q); |
Sanjay Patel | d8f2872 | 2015-09-25 20:12:43 +0000 | [diff] [blame] | 1683 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1684 | // Aligned pointers have trailing zeros - refine Known.Zero set |
Artur Pilipenko | ae7fa1c | 2015-09-30 11:55:45 +0000 | [diff] [blame] | 1685 | if (V->getType()->isPointerTy()) { |
Artur Pilipenko | f5b2749 | 2016-02-24 12:25:10 +0000 | [diff] [blame] | 1686 | unsigned Align = V->getPointerAlignment(Q.DL); |
Artur Pilipenko | ae7fa1c | 2015-09-30 11:55:45 +0000 | [diff] [blame] | 1687 | if (Align) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1688 | Known.Zero.setLowBits(countTrailingZeros(Align)); |
Artur Pilipenko | ae7fa1c | 2015-09-30 11:55:45 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1691 | // computeKnownBitsFromAssume strictly refines Known. |
| 1692 | // Therefore, we run them after computeKnownBitsFromOperator. |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1693 | |
| 1694 | // Check whether a nearby assume intrinsic can determine some known bits. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1695 | computeKnownBitsFromAssume(V, Known, Depth, Q); |
Jingyue Wu | 7a1e934 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1696 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1697 | assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1700 | /// Return true if the given value is known to have exactly one |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1701 | /// bit set when defined. For vectors return true if every element is known to |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1702 | /// be a power of two when defined. Supports values with integer or pointer |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1703 | /// types and vectors of integers. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1704 | bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1705 | const Query &Q) { |
Craig Topper | 16e7603 | 2017-08-21 22:56:12 +0000 | [diff] [blame] | 1706 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
| 1707 | |
Simon Pilgrim | 82c0b7b | 2018-02-06 18:39:23 +0000 | [diff] [blame] | 1708 | // Attempt to match against constants. |
| 1709 | if (OrZero && match(V, m_Power2OrZero())) |
| 1710 | return true; |
| 1711 | if (match(V, m_Power2())) |
| 1712 | return true; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1713 | |
| 1714 | // 1 << X is clearly a power of two if the one is not shifted off the end. If |
| 1715 | // it is shifted off the end then the result is undefined. |
| 1716 | if (match(V, m_Shl(m_One(), m_Value()))) |
| 1717 | return true; |
| 1718 | |
Craig Topper | 54f0462 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1719 | // (signmask) >>l X is clearly a power of two if the one is not shifted off |
| 1720 | // the bottom. If it is shifted off the bottom then the result is undefined. |
| 1721 | if (match(V, m_LShr(m_SignMask(), m_Value()))) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1722 | return true; |
| 1723 | |
| 1724 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 1725 | if (Depth++ == MaxDepth) |
| 1726 | return false; |
| 1727 | |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1728 | Value *X = nullptr, *Y = nullptr; |
Sanjay Patel | 08e3bf9 | 2015-12-30 22:40:52 +0000 | [diff] [blame] | 1729 | // A shift left or a logical shift right of a power of two is a power of two |
| 1730 | // or zero. |
Duncan Sands | 4604fc7 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1731 | if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || |
Sanjay Patel | 08e3bf9 | 2015-12-30 22:40:52 +0000 | [diff] [blame] | 1732 | match(V, m_LShr(m_Value(X), m_Value())))) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1733 | return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q); |
Duncan Sands | 4604fc7 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1734 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1735 | if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V)) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1736 | return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1737 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1738 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1739 | return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) && |
| 1740 | isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q); |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1741 | |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1742 | if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { |
| 1743 | // A power of two and'd with anything is a power of two or zero. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1744 | if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) || |
| 1745 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q)) |
Duncan Sands | dd3149d | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1746 | return true; |
| 1747 | // X & (-X) is always a power of two or zero. |
| 1748 | if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) |
| 1749 | return true; |
| 1750 | return false; |
| 1751 | } |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1752 | |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1753 | // Adding a power-of-two or zero to the same power-of-two or zero yields |
| 1754 | // either the original power-of-two, a larger power-of-two or zero. |
| 1755 | if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1756 | const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V); |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1757 | if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) || |
| 1758 | Q.IIQ.hasNoSignedWrap(VOBO)) { |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1759 | if (match(X, m_And(m_Specific(Y), m_Value())) || |
| 1760 | match(X, m_And(m_Value(), m_Specific(Y)))) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1761 | if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q)) |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1762 | return true; |
| 1763 | if (match(Y, m_And(m_Specific(X), m_Value())) || |
| 1764 | match(Y, m_And(m_Value(), m_Specific(X)))) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1765 | if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q)) |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1766 | return true; |
| 1767 | |
| 1768 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1769 | KnownBits LHSBits(BitWidth); |
| 1770 | computeKnownBits(X, LHSBits, Depth, Q); |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1771 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1772 | KnownBits RHSBits(BitWidth); |
| 1773 | computeKnownBits(Y, RHSBits, Depth, Q); |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1774 | // If i8 V is a power of two or zero: |
| 1775 | // ZeroBits: 1 1 1 0 1 1 1 1 |
| 1776 | // ~ZeroBits: 0 0 0 1 0 0 0 0 |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1777 | if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2()) |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1778 | // If OrZero isn't set, we cannot give back a zero result. |
| 1779 | // Make sure either the LHS or RHS has a bit set. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1780 | if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue()) |
David Majnemer | 36850ad | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1781 | return true; |
| 1782 | } |
| 1783 | } |
David Majnemer | cb9d466 | 2013-05-18 19:30:37 +0000 | [diff] [blame] | 1784 | |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1785 | // An exact divide or right shift can only shift off zero bits, so the result |
Nick Lewycky | 1f7bc70 | 2011-03-21 21:40:32 +0000 | [diff] [blame] | 1786 | // is a power of two only if the first operand is a power of two and not |
| 1787 | // copying a sign bit (sdiv int_min, 2). |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1788 | if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || |
| 1789 | match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { |
Hal Finkel | 851b04c | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1790 | return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1791 | Depth, Q); |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1794 | return false; |
| 1795 | } |
| 1796 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1797 | /// Test whether a GEP's result is known to be non-null. |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1798 | /// |
| 1799 | /// Uses properties inherent in a GEP to try to determine whether it is known |
| 1800 | /// to be non-null. |
| 1801 | /// |
| 1802 | /// Currently this routine does not support vector GEPs. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1803 | static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1804 | const Query &Q) { |
Manoj Gupta | c6da686 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 1805 | const Function *F = nullptr; |
| 1806 | if (const Instruction *I = dyn_cast<Instruction>(GEP)) |
| 1807 | F = I->getFunction(); |
| 1808 | |
| 1809 | if (!GEP->isInBounds() || |
| 1810 | NullPointerIsDefined(F, GEP->getPointerAddressSpace())) |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1811 | return false; |
| 1812 | |
| 1813 | // FIXME: Support vector-GEPs. |
| 1814 | assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP"); |
| 1815 | |
| 1816 | // If the base pointer is non-null, we cannot walk to a null address with an |
| 1817 | // inbounds GEP in address space zero. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1818 | if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q)) |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1819 | return true; |
| 1820 | |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1821 | // Walk the GEP operands and see if any operand introduces a non-zero offset. |
| 1822 | // If so, then the GEP cannot produce a null pointer, as doing so would |
| 1823 | // inherently violate the inbounds contract within address space zero. |
| 1824 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
| 1825 | GTI != GTE; ++GTI) { |
| 1826 | // Struct types are easy -- they must always be indexed by a constant. |
Peter Collingbourne | 0611580 | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 1827 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1828 | ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand()); |
| 1829 | unsigned ElementIdx = OpC->getZExtValue(); |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1830 | const StructLayout *SL = Q.DL.getStructLayout(STy); |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1831 | uint64_t ElementOffset = SL->getElementOffset(ElementIdx); |
| 1832 | if (ElementOffset > 0) |
| 1833 | return true; |
| 1834 | continue; |
| 1835 | } |
| 1836 | |
| 1837 | // If we have a zero-sized type, the index doesn't matter. Keep looping. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1838 | if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0) |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1839 | continue; |
| 1840 | |
| 1841 | // Fast path the constant operand case both for efficiency and so we don't |
| 1842 | // increment Depth when just zipping down an all-constant GEP. |
| 1843 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) { |
| 1844 | if (!OpC->isZero()) |
| 1845 | return true; |
| 1846 | continue; |
| 1847 | } |
| 1848 | |
| 1849 | // We post-increment Depth here because while isKnownNonZero increments it |
| 1850 | // as well, when we pop back up that increment won't persist. We don't want |
| 1851 | // to recurse 10k times just because we have 10k GEP operands. We don't |
| 1852 | // bail completely out because we want to handle constant GEPs regardless |
| 1853 | // of depth. |
| 1854 | if (Depth++ >= MaxDepth) |
| 1855 | continue; |
| 1856 | |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1857 | if (isKnownNonZero(GTI.getOperand(), Depth, Q)) |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1858 | return true; |
| 1859 | } |
| 1860 | |
| 1861 | return false; |
| 1862 | } |
| 1863 | |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1864 | static bool isKnownNonNullFromDominatingCondition(const Value *V, |
| 1865 | const Instruction *CtxI, |
| 1866 | const DominatorTree *DT) { |
| 1867 | assert(V->getType()->isPointerTy() && "V must be pointer type"); |
| 1868 | assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull"); |
| 1869 | |
| 1870 | if (!CtxI || !DT) |
| 1871 | return false; |
| 1872 | |
| 1873 | unsigned NumUsesExplored = 0; |
| 1874 | for (auto *U : V->users()) { |
| 1875 | // Avoid massive lists |
| 1876 | if (NumUsesExplored >= DomConditionsMaxUses) |
| 1877 | break; |
| 1878 | NumUsesExplored++; |
| 1879 | |
| 1880 | // If the value is used as an argument to a call or invoke, then argument |
| 1881 | // attributes may provide an answer about null-ness. |
| 1882 | if (auto CS = ImmutableCallSite(U)) |
| 1883 | if (auto *CalledFunc = CS.getCalledFunction()) |
| 1884 | for (const Argument &Arg : CalledFunc->args()) |
| 1885 | if (CS.getArgOperand(Arg.getArgNo()) == V && |
| 1886 | Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI)) |
| 1887 | return true; |
| 1888 | |
| 1889 | // Consider only compare instructions uniquely controlling a branch |
| 1890 | CmpInst::Predicate Pred; |
| 1891 | if (!match(const_cast<User *>(U), |
| 1892 | m_c_ICmp(Pred, m_Specific(V), m_Zero())) || |
| 1893 | (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)) |
| 1894 | continue; |
| 1895 | |
Max Kazantsev | 1aa1d5c | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1896 | SmallVector<const User *, 4> WorkList; |
| 1897 | SmallPtrSet<const User *, 4> Visited; |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1898 | for (auto *CmpU : U->users()) { |
Max Kazantsev | 1aa1d5c | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1899 | assert(WorkList.empty() && "Should be!"); |
| 1900 | if (Visited.insert(CmpU).second) |
| 1901 | WorkList.push_back(CmpU); |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1902 | |
Max Kazantsev | 1aa1d5c | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1903 | while (!WorkList.empty()) { |
| 1904 | auto *Curr = WorkList.pop_back_val(); |
| 1905 | |
| 1906 | // If a user is an AND, add all its users to the work list. We only |
| 1907 | // propagate "pred != null" condition through AND because it is only |
| 1908 | // correct to assume that all conditions of AND are met in true branch. |
| 1909 | // TODO: Support similar logic of OR and EQ predicate? |
| 1910 | if (Pred == ICmpInst::ICMP_NE) |
| 1911 | if (auto *BO = dyn_cast<BinaryOperator>(Curr)) |
| 1912 | if (BO->getOpcode() == Instruction::And) { |
| 1913 | for (auto *BOU : BO->users()) |
| 1914 | if (Visited.insert(BOU).second) |
| 1915 | WorkList.push_back(BOU); |
| 1916 | continue; |
| 1917 | } |
| 1918 | |
| 1919 | if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) { |
| 1920 | assert(BI->isConditional() && "uses a comparison!"); |
| 1921 | |
| 1922 | BasicBlock *NonNullSuccessor = |
| 1923 | BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0); |
| 1924 | BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); |
| 1925 | if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) |
| 1926 | return true; |
Max Kazantsev | 187d918 | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 1927 | } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) && |
Max Kazantsev | 1aa1d5c | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1928 | DT->dominates(cast<Instruction>(Curr), CtxI)) { |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1929 | return true; |
Max Kazantsev | 1aa1d5c | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1930 | } |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | return false; |
| 1936 | } |
| 1937 | |
Philip Reames | aad4ea4 | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1938 | /// Does the 'Range' metadata (which must be a valid MD_range operand list) |
| 1939 | /// ensure that the value it's attached to is never Value? 'RangeType' is |
| 1940 | /// is the type of the value described by the range. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1941 | static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) { |
Philip Reames | aad4ea4 | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1942 | const unsigned NumRanges = Ranges->getNumOperands() / 2; |
| 1943 | assert(NumRanges >= 1); |
| 1944 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1945 | ConstantInt *Lower = |
| 1946 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0)); |
| 1947 | ConstantInt *Upper = |
| 1948 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1)); |
Philip Reames | aad4ea4 | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1949 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 1950 | if (Range.contains(Value)) |
| 1951 | return false; |
| 1952 | } |
| 1953 | return true; |
| 1954 | } |
| 1955 | |
Sanjay Patel | 38d869a | 2017-02-12 15:35:34 +0000 | [diff] [blame] | 1956 | /// Return true if the given value is known to be non-zero when defined. For |
| 1957 | /// vectors, return true if every element is known to be non-zero when |
| 1958 | /// defined. For pointers, if the context instruction and dominator tree are |
| 1959 | /// specified, perform context-sensitive analysis and return true if the |
| 1960 | /// pointer couldn't possibly be null at the specified instruction. |
| 1961 | /// Supports values with integer or pointer type and vectors of integers. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1962 | bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) { |
Sanjay Patel | 5afdcbe | 2016-05-22 16:07:20 +0000 | [diff] [blame] | 1963 | if (auto *C = dyn_cast<Constant>(V)) { |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1964 | if (C->isNullValue()) |
| 1965 | return false; |
| 1966 | if (isa<ConstantInt>(C)) |
| 1967 | // Must be non-zero due to null test above. |
| 1968 | return true; |
Sanjay Patel | e571983 | 2016-05-24 14:18:49 +0000 | [diff] [blame] | 1969 | |
| 1970 | // For constant vectors, check that all elements are undefined or known |
| 1971 | // non-zero to determine that the whole vector is known non-zero. |
| 1972 | if (auto *VecTy = dyn_cast<VectorType>(C->getType())) { |
| 1973 | for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { |
| 1974 | Constant *Elt = C->getAggregateElement(i); |
| 1975 | if (!Elt || Elt->isNullValue()) |
| 1976 | return false; |
| 1977 | if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt)) |
| 1978 | return false; |
| 1979 | } |
| 1980 | return true; |
| 1981 | } |
| 1982 | |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1983 | // A global variable in address space 0 is non null unless extern weak |
| 1984 | // or an absolute symbol reference. Other address spaces may have null as a |
| 1985 | // valid address for a global, so we can't assume anything. |
| 1986 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1987 | if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && |
| 1988 | GV->getType()->getAddressSpace() == 0) |
| 1989 | return true; |
| 1990 | } else |
| 1991 | return false; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Sanjay Patel | 5afdcbe | 2016-05-22 16:07:20 +0000 | [diff] [blame] | 1994 | if (auto *I = dyn_cast<Instruction>(V)) { |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1995 | if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) { |
Philip Reames | aad4ea4 | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1996 | // If the possible ranges don't contain zero, then the value is |
| 1997 | // definitely non-zero. |
Sanjay Patel | 5afdcbe | 2016-05-22 16:07:20 +0000 | [diff] [blame] | 1998 | if (auto *Ty = dyn_cast<IntegerType>(V->getType())) { |
Philip Reames | aad4ea4 | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1999 | const APInt ZeroValue(Ty->getBitWidth(), 0); |
| 2000 | if (rangeMetadataExcludesValue(Ranges, ZeroValue)) |
| 2001 | return true; |
| 2002 | } |
| 2003 | } |
| 2004 | } |
| 2005 | |
Karl-Johan Karlsson | a85653e | 2018-05-30 15:56:46 +0000 | [diff] [blame] | 2006 | // Some of the tests below are recursive, so bail out if we hit the limit. |
| 2007 | if (Depth++ >= MaxDepth) |
| 2008 | return false; |
| 2009 | |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2010 | // Check for pointer simplifications. |
| 2011 | if (V->getType()->isPointerTy()) { |
| 2012 | // Alloca never returns null, malloc might. |
| 2013 | if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0) |
| 2014 | return true; |
| 2015 | |
| 2016 | // A byval, inalloca, or nonnull argument is never null. |
| 2017 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 2018 | if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr()) |
| 2019 | return true; |
| 2020 | |
| 2021 | // A Load tagged with nonnull metadata is never null. |
| 2022 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2023 | if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull)) |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2024 | return true; |
| 2025 | |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 2026 | if (const auto *Call = dyn_cast<CallBase>(V)) { |
| 2027 | if (Call->isReturnNonNull()) |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2028 | return true; |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 2029 | if (const auto *RP = getArgumentAliasingToReturnedPointer(Call)) |
Karl-Johan Karlsson | a85653e | 2018-05-30 15:56:46 +0000 | [diff] [blame] | 2030 | return isKnownNonZero(RP, Depth, Q); |
Piotr Padlewski | 461e372 | 2018-05-18 23:54:33 +0000 | [diff] [blame] | 2031 | } |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2034 | |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2035 | // Check for recursive pointer simplifications. |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 2036 | if (V->getType()->isPointerTy()) { |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2037 | if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT)) |
Sanjoy Das | 36bb502 | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 2038 | return true; |
Nuno Lopes | fe353a0 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2039 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2040 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2041 | if (isGEPKnownNonNull(GEP, Depth, Q)) |
Chandler Carruth | 70d3beb | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 2042 | return true; |
| 2043 | } |
| 2044 | |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2045 | unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2046 | |
| 2047 | // X | Y != 0 if X != 0 or Y != 0. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2048 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2049 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2050 | return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2051 | |
| 2052 | // ext X != 0 if X != 0. |
| 2053 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2054 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2055 | |
Duncan Sands | 9136782 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 2056 | // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2057 | // if the lowest bit is shifted off the end. |
Craig Topper | f990516 | 2017-05-03 22:25:19 +0000 | [diff] [blame] | 2058 | if (match(V, m_Shl(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2059 | // shl nuw can't remove any non-zero bits. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2060 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2061 | if (Q.IIQ.hasNoUnsignedWrap(BO)) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2062 | return isKnownNonZero(X, Depth, Q); |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2063 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2064 | KnownBits Known(BitWidth); |
| 2065 | computeKnownBits(X, Known, Depth, Q); |
| 2066 | if (Known.One[0]) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2067 | return true; |
| 2068 | } |
Duncan Sands | 9136782 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 2069 | // shr X, Y != 0 if X is negative. Note that the value of the shift is not |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2070 | // defined if the sign bit is shifted off the end. |
| 2071 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2072 | // shr exact can only shift out zero bits. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2073 | const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V); |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2074 | if (BO->isExact()) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2075 | return isKnownNonZero(X, Depth, Q); |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2076 | |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2077 | KnownBits Known = computeKnownBits(X, Depth, Q); |
| 2078 | if (Known.isNegative()) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2079 | return true; |
James Molloy | d44ba4a | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2080 | |
| 2081 | // If the shifter operand is a constant, and all of the bits shifted |
| 2082 | // out are known to be zero, and X is known non-zero then at least one |
| 2083 | // non-zero bit must remain. |
| 2084 | if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) { |
James Molloy | d44ba4a | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2085 | auto ShiftVal = Shift->getLimitedValue(BitWidth - 1); |
| 2086 | // Is there a known one in the portion not shifted out? |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 2087 | if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal) |
James Molloy | d44ba4a | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2088 | return true; |
| 2089 | // Are all the bits to be shifted out known zero? |
NAKAMURA Takumi | e582c6f | 2017-07-11 02:31:51 +0000 | [diff] [blame] | 2090 | if (Known.countMinTrailingZeros() >= ShiftVal) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2091 | return isKnownNonZero(X, Depth, Q); |
James Molloy | d44ba4a | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2092 | } |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2093 | } |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2094 | // div exact can only produce a zero if the dividend is zero. |
Benjamin Kramer | 55c6d57 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 2095 | else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) { |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2096 | return isKnownNonZero(X, Depth, Q); |
Nick Lewycky | 3dfd987 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2097 | } |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2098 | // X + Y. |
| 2099 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2100 | KnownBits XKnown = computeKnownBits(X, Depth, Q); |
| 2101 | KnownBits YKnown = computeKnownBits(Y, Depth, Q); |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2102 | |
| 2103 | // If X and Y are both non-negative (as signed values) then their sum is not |
Duncan Sands | 227fba1 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 2104 | // zero unless both X and Y are zero. |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2105 | if (XKnown.isNonNegative() && YKnown.isNonNegative()) |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2106 | if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q)) |
Duncan Sands | 227fba1 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 2107 | return true; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2108 | |
| 2109 | // If X and Y are both negative (as signed values) then their sum is not |
| 2110 | // zero unless both X and Y equal INT_MIN. |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2111 | if (XKnown.isNegative() && YKnown.isNegative()) { |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2112 | APInt Mask = APInt::getSignedMaxValue(BitWidth); |
| 2113 | // The sign bit of X is set. If some other bit is set then X is not equal |
| 2114 | // to INT_MIN. |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2115 | if (XKnown.One.intersects(Mask)) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2116 | return true; |
| 2117 | // The sign bit of Y is set. If some other bit is set then Y is not equal |
| 2118 | // to INT_MIN. |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2119 | if (YKnown.One.intersects(Mask)) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2120 | return true; |
| 2121 | } |
| 2122 | |
| 2123 | // The sum of a non-negative number and a power of two is not zero. |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2124 | if (XKnown.isNonNegative() && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2125 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q)) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2126 | return true; |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2127 | if (YKnown.isNonNegative() && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2128 | isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q)) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2129 | return true; |
| 2130 | } |
Duncan Sands | 32a43cc | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 2131 | // X * Y. |
| 2132 | else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) { |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2133 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
Duncan Sands | 32a43cc | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 2134 | // If X and Y are non-zero then so is X * Y as long as the multiplication |
| 2135 | // does not overflow. |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2136 | if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) && |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2137 | isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q)) |
Duncan Sands | 32a43cc | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 2138 | return true; |
| 2139 | } |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2140 | // (C ? X : Y) != 0 if X != 0 and Y != 0. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2141 | else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2142 | if (isKnownNonZero(SI->getTrueValue(), Depth, Q) && |
| 2143 | isKnownNonZero(SI->getFalseValue(), Depth, Q)) |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2144 | return true; |
| 2145 | } |
James Molloy | 79c45a3 | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2146 | // PHI |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2147 | else if (const PHINode *PN = dyn_cast<PHINode>(V)) { |
James Molloy | 79c45a3 | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2148 | // Try and detect a recurrence that monotonically increases from a |
| 2149 | // starting value, as these are common as induction variables. |
| 2150 | if (PN->getNumIncomingValues() == 2) { |
| 2151 | Value *Start = PN->getIncomingValue(0); |
| 2152 | Value *Induction = PN->getIncomingValue(1); |
| 2153 | if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start)) |
| 2154 | std::swap(Start, Induction); |
| 2155 | if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) { |
| 2156 | if (!C->isZero() && !C->isNegative()) { |
| 2157 | ConstantInt *X; |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2158 | if (Q.IIQ.UseInstrInfo && |
| 2159 | (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) || |
James Molloy | 79c45a3 | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2160 | match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) && |
| 2161 | !X->isNegative()) |
| 2162 | return true; |
| 2163 | } |
| 2164 | } |
| 2165 | } |
Jun Bum Lim | d9e5500 | 2016-02-01 17:03:07 +0000 | [diff] [blame] | 2166 | // Check if all incoming values are non-zero constant. |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2167 | bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) { |
Craig Topper | 6dbd34d | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 2168 | return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero(); |
Jun Bum Lim | d9e5500 | 2016-02-01 17:03:07 +0000 | [diff] [blame] | 2169 | }); |
| 2170 | if (AllNonZeroConstants) |
| 2171 | return true; |
James Molloy | 79c45a3 | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2172 | } |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2173 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2174 | KnownBits Known(BitWidth); |
| 2175 | computeKnownBits(V, Known, Depth, Q); |
| 2176 | return Known.One != 0; |
Duncan Sands | d70d1a5 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2179 | /// Return true if V2 == V1 + X, where X is known non-zero. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2180 | static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) { |
| 2181 | const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1); |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2182 | if (!BO || BO->getOpcode() != Instruction::Add) |
| 2183 | return false; |
| 2184 | Value *Op = nullptr; |
| 2185 | if (V2 == BO->getOperand(0)) |
| 2186 | Op = BO->getOperand(1); |
| 2187 | else if (V2 == BO->getOperand(1)) |
| 2188 | Op = BO->getOperand(0); |
| 2189 | else |
| 2190 | return false; |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2191 | return isKnownNonZero(Op, 0, Q); |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | /// Return true if it is known that V1 != V2. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2195 | static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) { |
Craig Topper | 275adc9 | 2017-06-06 07:13:15 +0000 | [diff] [blame] | 2196 | if (V1 == V2) |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2197 | return false; |
| 2198 | if (V1->getType() != V2->getType()) |
| 2199 | // We can't look through casts yet. |
| 2200 | return false; |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2201 | if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q)) |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2202 | return true; |
| 2203 | |
Craig Topper | 275adc9 | 2017-06-06 07:13:15 +0000 | [diff] [blame] | 2204 | if (V1->getType()->isIntOrIntVectorTy()) { |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2205 | // Are any known bits in V1 contradictory to known bits in V2? If V1 |
| 2206 | // has a known zero where V2 has a known one, they must not be equal. |
Craig Topper | 43a5f59 | 2017-06-06 07:13:11 +0000 | [diff] [blame] | 2207 | KnownBits Known1 = computeKnownBits(V1, 0, Q); |
| 2208 | KnownBits Known2 = computeKnownBits(V2, 0, Q); |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2209 | |
Craig Topper | 04e2a49 | 2017-06-06 07:13:09 +0000 | [diff] [blame] | 2210 | if (Known1.Zero.intersects(Known2.One) || |
| 2211 | Known2.Zero.intersects(Known1.One)) |
James Molloy | af1480a | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2212 | return true; |
| 2213 | } |
| 2214 | return false; |
| 2215 | } |
| 2216 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2217 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to |
| 2218 | /// simplify operations downstream. Mask is known to be zero for bits that V |
| 2219 | /// cannot have. |
Chris Lattner | cf5128e | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 2220 | /// |
| 2221 | /// This function is defined on values with integer type, values with pointer |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2222 | /// type, and vectors of integers. In the case |
Chris Lattner | cf5128e | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 2223 | /// where V is a vector, the mask, known zero, and known one values are the |
| 2224 | /// same width as the vector element, and the bit is set only if it is true |
| 2225 | /// for all of the elements in the vector. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2226 | bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2227 | const Query &Q) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2228 | KnownBits Known(Mask.getBitWidth()); |
| 2229 | computeKnownBits(V, Known, Depth, Q); |
| 2230 | return Mask.isSubsetOf(Known.Zero); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Craig Topper | 8781944 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2233 | // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). |
| 2234 | // Returns the input and lower/upper bounds. |
| 2235 | static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, |
| 2236 | const APInt *&CLow, const APInt *&CHigh) { |
Craig Topper | b076be5 | 2018-08-23 17:15:02 +0000 | [diff] [blame] | 2237 | assert(isa<Operator>(Select) && |
| 2238 | cast<Operator>(Select)->getOpcode() == Instruction::Select && |
Craig Topper | 647d93d | 2018-08-23 17:45:53 +0000 | [diff] [blame] | 2239 | "Input should be a Select!"); |
Craig Topper | 8781944 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2240 | |
| 2241 | const Value *LHS, *RHS, *LHS2, *RHS2; |
| 2242 | SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor; |
| 2243 | if (SPF != SPF_SMAX && SPF != SPF_SMIN) |
| 2244 | return false; |
| 2245 | |
| 2246 | if (!match(RHS, m_APInt(CLow))) |
| 2247 | return false; |
| 2248 | |
| 2249 | SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor; |
| 2250 | if (getInverseMinMaxFlavor(SPF) != SPF2) |
| 2251 | return false; |
| 2252 | |
| 2253 | if (!match(RHS2, m_APInt(CHigh))) |
| 2254 | return false; |
| 2255 | |
| 2256 | if (SPF == SPF_SMIN) |
| 2257 | std::swap(CLow, CHigh); |
| 2258 | |
| 2259 | In = LHS2; |
| 2260 | return CLow->sle(*CHigh); |
| 2261 | } |
| 2262 | |
Sanjay Patel | efa1f7f | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2263 | /// For vector constants, loop over the elements and find the constant with the |
| 2264 | /// minimum number of sign bits. Return 0 if the value is not a vector constant |
| 2265 | /// or if any element was not analyzed; otherwise, return the count for the |
| 2266 | /// element with the minimum number of sign bits. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2267 | static unsigned computeNumSignBitsVectorConstant(const Value *V, |
| 2268 | unsigned TyBits) { |
| 2269 | const auto *CV = dyn_cast<Constant>(V); |
Sanjay Patel | efa1f7f | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2270 | if (!CV || !CV->getType()->isVectorTy()) |
| 2271 | return 0; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2272 | |
Sanjay Patel | efa1f7f | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2273 | unsigned MinSignBits = TyBits; |
| 2274 | unsigned NumElts = CV->getType()->getVectorNumElements(); |
| 2275 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2276 | // If we find a non-ConstantInt, bail out. |
| 2277 | auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i)); |
| 2278 | if (!Elt) |
| 2279 | return 0; |
| 2280 | |
Craig Topper | 698b76c | 2017-10-21 16:35:41 +0000 | [diff] [blame] | 2281 | MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits()); |
Sanjay Patel | efa1f7f | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | return MinSignBits; |
| 2285 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2286 | |
Sanjoy Das | 9b2526d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2287 | static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth, |
| 2288 | const Query &Q); |
| 2289 | |
| 2290 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, |
| 2291 | const Query &Q) { |
| 2292 | unsigned Result = ComputeNumSignBitsImpl(V, Depth, Q); |
| 2293 | assert(Result > 0 && "At least one sign bit needs to be present!"); |
| 2294 | return Result; |
| 2295 | } |
| 2296 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2297 | /// Return the number of times the sign bit of the register is replicated into |
| 2298 | /// the other bits. We know that at least 1 bit is always equal to the sign bit |
| 2299 | /// (itself), but other cases can give us information. For example, immediately |
| 2300 | /// after an "ashr X, 2", we know that the top 3 bits are all equal to each |
Sanjay Patel | efa1f7f | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2301 | /// other, so we return 3. For vectors, return the number of sign bits for the |
Vedant Kumar | 06a2673 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 2302 | /// vector element with the minimum number of known sign bits. |
Sanjoy Das | 9b2526d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2303 | static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth, |
| 2304 | const Query &Q) { |
Craig Topper | 16e7603 | 2017-08-21 22:56:12 +0000 | [diff] [blame] | 2305 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Sanjoy Das | 9b2526d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2306 | |
| 2307 | // We return the minimum number of sign bits that are guaranteed to be present |
| 2308 | // in V, so for undef we have to conservatively return 1. We don't have the |
| 2309 | // same behavior for poison though -- that's a FIXME today. |
| 2310 | |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 2311 | Type *ScalarTy = V->getType()->getScalarType(); |
| 2312 | unsigned TyBits = ScalarTy->isPointerTy() ? |
| 2313 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : |
| 2314 | Q.DL.getTypeSizeInBits(ScalarTy); |
| 2315 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2316 | unsigned Tmp, Tmp2; |
| 2317 | unsigned FirstAnswer = 1; |
| 2318 | |
Jay Foad | 6b54371 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2319 | // Note that ConstantInt is handled by the general computeKnownBits case |
Chris Lattner | d82e511 | 2008-06-02 18:39:07 +0000 | [diff] [blame] | 2320 | // below. |
| 2321 | |
Matt Arsenault | 96a2ab4 | 2016-12-20 19:06:15 +0000 | [diff] [blame] | 2322 | if (Depth == MaxDepth) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2323 | return 1; // Limit search depth. |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2324 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2325 | const Operator *U = dyn_cast<Operator>(V); |
Dan Gohman | ca17890 | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2326 | switch (Operator::getOpcode(V)) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2327 | default: break; |
| 2328 | case Instruction::SExt: |
Mon P Wang | 69a0080 | 2009-12-02 04:59:58 +0000 | [diff] [blame] | 2329 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2330 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2331 | |
Nadav Rotem | 368d2e9 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2332 | case Instruction::SDiv: { |
Nadav Rotem | 10faa1b | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 2333 | const APInt *Denominator; |
| 2334 | // sdiv X, C -> adds log(C) sign bits. |
| 2335 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 2336 | |
| 2337 | // Ignore non-positive denominator. |
| 2338 | if (!Denominator->isStrictlyPositive()) |
| 2339 | break; |
| 2340 | |
| 2341 | // Calculate the incoming numerator bits. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2342 | unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Nadav Rotem | 10faa1b | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 2343 | |
| 2344 | // Add floor(log(C)) bits to the numerator bits. |
| 2345 | return std::min(TyBits, NumBits + Denominator->logBase2()); |
| 2346 | } |
| 2347 | break; |
Nadav Rotem | 368d2e9 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | case Instruction::SRem: { |
| 2351 | const APInt *Denominator; |
Sanjoy Das | 93d8819 | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 2352 | // srem X, C -> we know that the result is within [-C+1,C) when C is a |
| 2353 | // positive constant. This let us put a lower bound on the number of sign |
| 2354 | // bits. |
Nadav Rotem | 368d2e9 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2355 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 2356 | |
| 2357 | // Ignore non-positive denominator. |
| 2358 | if (!Denominator->isStrictlyPositive()) |
| 2359 | break; |
| 2360 | |
| 2361 | // Calculate the incoming numerator bits. SRem by a positive constant |
| 2362 | // can't lower the number of sign bits. |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2363 | unsigned NumrBits = |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2364 | ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Nadav Rotem | 368d2e9 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Calculate the leading sign bit constraints by examining the |
Sanjoy Das | 93d8819 | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 2367 | // denominator. Given that the denominator is positive, there are two |
| 2368 | // cases: |
| 2369 | // |
| 2370 | // 1. the numerator is positive. The result range is [0,C) and [0,C) u< |
| 2371 | // (1 << ceilLogBase2(C)). |
| 2372 | // |
| 2373 | // 2. the numerator is negative. Then the result range is (-C,0] and |
| 2374 | // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). |
| 2375 | // |
| 2376 | // Thus a lower bound on the number of sign bits is `TyBits - |
| 2377 | // ceilLogBase2(C)`. |
Nadav Rotem | 368d2e9 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2378 | |
Sanjoy Das | 93d8819 | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 2379 | unsigned ResBits = TyBits - Denominator->ceilLogBase2(); |
Nadav Rotem | 368d2e9 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2380 | return std::max(NumrBits, ResBits); |
| 2381 | } |
| 2382 | break; |
| 2383 | } |
Nadav Rotem | 10faa1b | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 2384 | |
Chris Lattner | 6b0dc92 | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2385 | case Instruction::AShr: { |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2386 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Chris Lattner | 6b0dc92 | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2387 | // ashr X, C -> adds C sign bits. Vectors too. |
| 2388 | const APInt *ShAmt; |
| 2389 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
Simon Pilgrim | 7640666 | 2018-01-01 22:44:59 +0000 | [diff] [blame] | 2390 | if (ShAmt->uge(TyBits)) |
Sanjoy Das | 9b2526d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2391 | break; // Bad shift. |
Simon Pilgrim | 7640666 | 2018-01-01 22:44:59 +0000 | [diff] [blame] | 2392 | unsigned ShAmtLimited = ShAmt->getZExtValue(); |
Sanjoy Das | 9b2526d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2393 | Tmp += ShAmtLimited; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2394 | if (Tmp > TyBits) Tmp = TyBits; |
| 2395 | } |
| 2396 | return Tmp; |
Chris Lattner | 6b0dc92 | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2397 | } |
| 2398 | case Instruction::Shl: { |
| 2399 | const APInt *ShAmt; |
| 2400 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2401 | // shl destroys sign bits. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2402 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Simon Pilgrim | 7640666 | 2018-01-01 22:44:59 +0000 | [diff] [blame] | 2403 | if (ShAmt->uge(TyBits) || // Bad shift. |
| 2404 | ShAmt->uge(Tmp)) break; // Shifted all sign bits out. |
Chris Lattner | 6b0dc92 | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2405 | Tmp2 = ShAmt->getZExtValue(); |
Chris Lattner | 6b0dc92 | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2406 | return Tmp - Tmp2; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2407 | } |
| 2408 | break; |
Chris Lattner | 6b0dc92 | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2409 | } |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2410 | case Instruction::And: |
| 2411 | case Instruction::Or: |
| 2412 | case Instruction::Xor: // NOT is handled here. |
| 2413 | // Logical binary ops preserve the number of sign bits at the worst. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2414 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2415 | if (Tmp != 1) { |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2416 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2417 | FirstAnswer = std::min(Tmp, Tmp2); |
| 2418 | // We computed what we know about the sign bits as our first |
| 2419 | // answer. Now proceed to the generic code that uses |
Jay Foad | 6b54371 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2420 | // computeKnownBits, and pick whichever answer is better. |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2421 | } |
| 2422 | break; |
| 2423 | |
Craig Topper | 8781944 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2424 | case Instruction::Select: { |
| 2425 | // If we have a clamp pattern, we know that the number of sign bits will be |
| 2426 | // the minimum of the clamp min/max range. |
| 2427 | const Value *X; |
| 2428 | const APInt *CLow, *CHigh; |
| 2429 | if (isSignedMinMaxClamp(U, X, CLow, CHigh)) |
| 2430 | return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); |
| 2431 | |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2432 | Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2433 | if (Tmp == 1) break; |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2434 | Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2435 | return std::min(Tmp, Tmp2); |
Craig Topper | 8781944 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2436 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2438 | case Instruction::Add: |
| 2439 | // Add can have at most one carry bit. Thus we know that the output |
| 2440 | // is, at worst, one more bit than the inputs. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2441 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2442 | if (Tmp == 1) break; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2443 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2444 | // Special case decrementing a value (ADD X, -1): |
David Majnemer | 597e1ab | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2445 | if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1))) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2446 | if (CRHS->isAllOnesValue()) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2447 | KnownBits Known(TyBits); |
| 2448 | computeKnownBits(U->getOperand(0), Known, Depth + 1, Q); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2449 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2450 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2451 | // sign bits set. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2452 | if ((Known.Zero | 1).isAllOnesValue()) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2453 | return TyBits; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2455 | // If we are subtracting one from a positive number, there is no carry |
| 2456 | // out of the result. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 2457 | if (Known.isNonNegative()) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2458 | return Tmp; |
| 2459 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2460 | |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2461 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2462 | if (Tmp2 == 1) break; |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2463 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2465 | case Instruction::Sub: |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2466 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2467 | if (Tmp2 == 1) break; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2468 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2469 | // Handle NEG. |
David Majnemer | 597e1ab | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2470 | if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0))) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2471 | if (CLHS->isNullValue()) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2472 | KnownBits Known(TyBits); |
| 2473 | computeKnownBits(U->getOperand(1), Known, Depth + 1, Q); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2474 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2475 | // sign bits set. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2476 | if ((Known.Zero | 1).isAllOnesValue()) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2477 | return TyBits; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2478 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2479 | // If the input is known to be positive (the sign bit is known clear), |
| 2480 | // the output of the NEG has the same number of sign bits as the input. |
Craig Topper | 8b69610 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 2481 | if (Known.isNonNegative()) |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2482 | return Tmp2; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2483 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2484 | // Otherwise, we treat this like a SUB. |
| 2485 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2486 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2487 | // Sub can have at most one carry bit. Thus we know that the output |
| 2488 | // is, at worst, one more bit than the inputs. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2489 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2490 | if (Tmp == 1) break; |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2491 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2492 | |
Amjad Aboud | 066b24c | 2017-08-18 22:56:55 +0000 | [diff] [blame] | 2493 | case Instruction::Mul: { |
| 2494 | // The output of the Mul can be at most twice the valid bits in the inputs. |
| 2495 | unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2496 | if (SignBitsOp0 == 1) break; |
Amjad Aboud | 066b24c | 2017-08-18 22:56:55 +0000 | [diff] [blame] | 2497 | unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | 07e41a6 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2498 | if (SignBitsOp1 == 1) break; |
Amjad Aboud | 066b24c | 2017-08-18 22:56:55 +0000 | [diff] [blame] | 2499 | unsigned OutValidBits = |
| 2500 | (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1); |
| 2501 | return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1; |
| 2502 | } |
| 2503 | |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2504 | case Instruction::PHI: { |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2505 | const PHINode *PN = cast<PHINode>(U); |
David Majnemer | 77e22b7 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2506 | unsigned NumIncomingValues = PN->getNumIncomingValues(); |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2507 | // Don't analyze large in-degree PHIs. |
David Majnemer | 77e22b7 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2508 | if (NumIncomingValues > 4) break; |
| 2509 | // Unreachable blocks may have zero-operand PHI nodes. |
| 2510 | if (NumIncomingValues == 0) break; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2511 | |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2512 | // Take the minimum of all incoming values. This can't infinitely loop |
| 2513 | // because of our depth threshold. |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2514 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q); |
David Majnemer | 77e22b7 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2515 | for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) { |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2516 | if (Tmp == 1) return Tmp; |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2517 | Tmp = std::min( |
Matthias Braun | 5e5e5aa | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2518 | Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q)); |
Chris Lattner | 8d10f9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2519 | } |
| 2520 | return Tmp; |
| 2521 | } |
| 2522 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2523 | case Instruction::Trunc: |
| 2524 | // FIXME: it's tricky to do anything useful for this, but it is an important |
| 2525 | // case for targets like X86. |
| 2526 | break; |
Bjorn Pettersson | c4e191a | 2016-10-06 09:56:21 +0000 | [diff] [blame] | 2527 | |
| 2528 | case Instruction::ExtractElement: |
| 2529 | // Look through extract element. At the moment we keep this simple and skip |
| 2530 | // tracking the specific element. But at least we might find information |
| 2531 | // valid for all elements of the vector (for example if vector is sign |
| 2532 | // extended, shifted, etc). |
| 2533 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Sanjay Patel | 52931b4 | 2018-10-26 21:05:14 +0000 | [diff] [blame] | 2534 | |
Sanjay Patel | 6709348 | 2018-11-02 15:51:47 +0000 | [diff] [blame] | 2535 | case Instruction::ShuffleVector: { |
Sanjay Patel | 7fad5fb | 2018-11-03 13:18:55 +0000 | [diff] [blame] | 2536 | // TODO: This is copied almost directly from the SelectionDAG version of |
| 2537 | // ComputeNumSignBits. It would be better if we could share common |
| 2538 | // code. If not, make sure that changes are translated to the DAG. |
| 2539 | |
| 2540 | // Collect the minimum number of sign bits that are shared by every vector |
| 2541 | // element referenced by the shuffle. |
| 2542 | auto *Shuf = cast<ShuffleVectorInst>(U); |
| 2543 | int NumElts = Shuf->getOperand(0)->getType()->getVectorNumElements(); |
| 2544 | int NumMaskElts = Shuf->getMask()->getType()->getVectorNumElements(); |
| 2545 | APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0); |
| 2546 | for (int i = 0; i != NumMaskElts; ++i) { |
| 2547 | int M = Shuf->getMaskValue(i); |
| 2548 | assert(M < NumElts * 2 && "Invalid shuffle mask constant"); |
| 2549 | // For undef elements, we don't know anything about the common state of |
| 2550 | // the shuffle result. |
| 2551 | if (M == -1) |
| 2552 | return 1; |
| 2553 | if (M < NumElts) |
| 2554 | DemandedLHS.setBit(M % NumElts); |
| 2555 | else |
| 2556 | DemandedRHS.setBit(M % NumElts); |
| 2557 | } |
| 2558 | Tmp = std::numeric_limits<unsigned>::max(); |
| 2559 | if (!!DemandedLHS) |
| 2560 | Tmp = ComputeNumSignBits(Shuf->getOperand(0), Depth + 1, Q); |
| 2561 | if (!!DemandedRHS) { |
| 2562 | Tmp2 = ComputeNumSignBits(Shuf->getOperand(1), Depth + 1, Q); |
| 2563 | Tmp = std::min(Tmp, Tmp2); |
| 2564 | } |
| 2565 | // If we don't know anything, early out and try computeKnownBits fall-back. |
| 2566 | if (Tmp == 1) |
Sanjay Patel | 52931b4 | 2018-10-26 21:05:14 +0000 | [diff] [blame] | 2567 | break; |
Sanjay Patel | 7fad5fb | 2018-11-03 13:18:55 +0000 | [diff] [blame] | 2568 | assert(Tmp <= V->getType()->getScalarSizeInBits() && |
| 2569 | "Failed to determine minimum sign bits"); |
| 2570 | return Tmp; |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2571 | } |
Sanjay Patel | 6709348 | 2018-11-02 15:51:47 +0000 | [diff] [blame] | 2572 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2573 | |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2574 | // Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 2575 | // use this information. |
Sanjay Patel | efa1f7f | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2576 | |
| 2577 | // If we can examine all elements of a vector constant successfully, we're |
| 2578 | // done (we can't do any better than that). If not, keep trying. |
| 2579 | if (unsigned VecSignBits = computeNumSignBitsVectorConstant(V, TyBits)) |
| 2580 | return VecSignBits; |
| 2581 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2582 | KnownBits Known(TyBits); |
| 2583 | computeKnownBits(V, Known, Depth, Q); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2584 | |
Sanjay Patel | ba911c4 | 2016-06-23 17:41:59 +0000 | [diff] [blame] | 2585 | // If we know that the sign bit is either zero or one, determine the number of |
| 2586 | // identical bits in the top of the input value. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 2587 | return std::max(FirstAnswer, Known.countMinSignBits()); |
Chris Lattner | 173234a | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2588 | } |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2589 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2590 | /// This function computes the integer multiple of Base that equals V. |
| 2591 | /// If successful, it returns true and returns the multiple in |
| 2592 | /// Multiple. If unsuccessful, it returns false. It looks |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2593 | /// through SExt instructions only if LookThroughSExt is true. |
| 2594 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2595 | bool LookThroughSExt, unsigned Depth) { |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2596 | const unsigned MaxDepth = 6; |
| 2597 | |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2598 | assert(V && "No Value?"); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2599 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2600 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!"); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2601 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2602 | Type *T = V->getType(); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2603 | |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2604 | ConstantInt *CI = dyn_cast<ConstantInt>(V); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2605 | |
| 2606 | if (Base == 0) |
| 2607 | return false; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2608 | |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2609 | if (Base == 1) { |
| 2610 | Multiple = V; |
| 2611 | return true; |
| 2612 | } |
| 2613 | |
| 2614 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); |
| 2615 | Constant *BaseVal = ConstantInt::get(T, Base); |
| 2616 | if (CO && CO == BaseVal) { |
| 2617 | // Multiple is 1. |
| 2618 | Multiple = ConstantInt::get(T, 1); |
| 2619 | return true; |
| 2620 | } |
| 2621 | |
| 2622 | if (CI && CI->getZExtValue() % Base == 0) { |
| 2623 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2624 | return true; |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2625 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2626 | |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2627 | if (Depth == MaxDepth) return false; // Limit search depth. |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2628 | |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2629 | Operator *I = dyn_cast<Operator>(V); |
| 2630 | if (!I) return false; |
| 2631 | |
| 2632 | switch (I->getOpcode()) { |
| 2633 | default: break; |
Chris Lattner | 11fe726 | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2634 | case Instruction::SExt: |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2635 | if (!LookThroughSExt) return false; |
| 2636 | // otherwise fall through to ZExt |
Galina Kistanova | 1c5a50c | 2017-05-31 22:16:24 +0000 | [diff] [blame] | 2637 | LLVM_FALLTHROUGH; |
Chris Lattner | 11fe726 | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2638 | case Instruction::ZExt: |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2639 | return ComputeMultiple(I->getOperand(0), Base, Multiple, |
| 2640 | LookThroughSExt, Depth+1); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2641 | case Instruction::Shl: |
| 2642 | case Instruction::Mul: { |
| 2643 | Value *Op0 = I->getOperand(0); |
| 2644 | Value *Op1 = I->getOperand(1); |
| 2645 | |
| 2646 | if (I->getOpcode() == Instruction::Shl) { |
| 2647 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
| 2648 | if (!Op1CI) return false; |
| 2649 | // Turn Op0 << Op1 into Op0 * 2^Op1 |
| 2650 | APInt Op1Int = Op1CI->getValue(); |
| 2651 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
Jay Foad | a99793c | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2652 | APInt API(Op1Int.getBitWidth(), 0); |
Jay Foad | 7a874dd | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2653 | API.setBit(BitToSet); |
Jay Foad | a99793c | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2654 | Op1 = ConstantInt::get(V->getContext(), API); |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2657 | Value *Mul0 = nullptr; |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2658 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { |
| 2659 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) |
| 2660 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2661 | if (Op1C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2662 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2663 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2664 | if (Op1C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2665 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2666 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2667 | |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2668 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) |
| 2669 | Multiple = ConstantExpr::getMul(MulC, Op1C); |
| 2670 | return true; |
| 2671 | } |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2672 | |
| 2673 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) |
| 2674 | if (Mul0CI->getValue() == 1) { |
| 2675 | // V == Base * Op1, so return Op1 |
| 2676 | Multiple = Op1; |
| 2677 | return true; |
| 2678 | } |
| 2679 | } |
| 2680 | |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2681 | Value *Mul1 = nullptr; |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2682 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { |
| 2683 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) |
| 2684 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2685 | if (Op0C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2686 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2687 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2688 | if (Op0C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2689 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2690 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2691 | |
Chris Lattner | e971131 | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2692 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) |
| 2693 | Multiple = ConstantExpr::getMul(MulC, Op0C); |
| 2694 | return true; |
| 2695 | } |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) |
| 2698 | if (Mul1CI->getValue() == 1) { |
| 2699 | // V == Base * Op0, so return Op0 |
| 2700 | Multiple = Op0; |
| 2701 | return true; |
| 2702 | } |
| 2703 | } |
Victor Hernandez | 2b6705f | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2704 | } |
| 2705 | } |
| 2706 | |
| 2707 | // We could not determine if V is a multiple of Base. |
| 2708 | return false; |
| 2709 | } |
| 2710 | |
David Majnemer | 7f0d15f | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2711 | Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS, |
| 2712 | const TargetLibraryInfo *TLI) { |
| 2713 | const Function *F = ICS.getCalledFunction(); |
| 2714 | if (!F) |
| 2715 | return Intrinsic::not_intrinsic; |
| 2716 | |
| 2717 | if (F->isIntrinsic()) |
| 2718 | return F->getIntrinsicID(); |
| 2719 | |
| 2720 | if (!TLI) |
| 2721 | return Intrinsic::not_intrinsic; |
| 2722 | |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2723 | LibFunc Func; |
David Majnemer | 7f0d15f | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2724 | // We're going to make assumptions on the semantics of the functions, check |
| 2725 | // that the target knows that it's available in this environment and it does |
| 2726 | // not have local linkage. |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2727 | if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func)) |
| 2728 | return Intrinsic::not_intrinsic; |
| 2729 | |
| 2730 | if (!ICS.onlyReadsMemory()) |
David Majnemer | 7f0d15f | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2731 | return Intrinsic::not_intrinsic; |
| 2732 | |
| 2733 | // Otherwise check if we have a call to a function that can be turned into a |
| 2734 | // vector intrinsic. |
| 2735 | switch (Func) { |
| 2736 | default: |
| 2737 | break; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2738 | case LibFunc_sin: |
| 2739 | case LibFunc_sinf: |
| 2740 | case LibFunc_sinl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2741 | return Intrinsic::sin; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2742 | case LibFunc_cos: |
| 2743 | case LibFunc_cosf: |
| 2744 | case LibFunc_cosl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2745 | return Intrinsic::cos; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2746 | case LibFunc_exp: |
| 2747 | case LibFunc_expf: |
| 2748 | case LibFunc_expl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2749 | return Intrinsic::exp; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2750 | case LibFunc_exp2: |
| 2751 | case LibFunc_exp2f: |
| 2752 | case LibFunc_exp2l: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2753 | return Intrinsic::exp2; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2754 | case LibFunc_log: |
| 2755 | case LibFunc_logf: |
| 2756 | case LibFunc_logl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2757 | return Intrinsic::log; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2758 | case LibFunc_log10: |
| 2759 | case LibFunc_log10f: |
| 2760 | case LibFunc_log10l: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2761 | return Intrinsic::log10; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2762 | case LibFunc_log2: |
| 2763 | case LibFunc_log2f: |
| 2764 | case LibFunc_log2l: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2765 | return Intrinsic::log2; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2766 | case LibFunc_fabs: |
| 2767 | case LibFunc_fabsf: |
| 2768 | case LibFunc_fabsl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2769 | return Intrinsic::fabs; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2770 | case LibFunc_fmin: |
| 2771 | case LibFunc_fminf: |
| 2772 | case LibFunc_fminl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2773 | return Intrinsic::minnum; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2774 | case LibFunc_fmax: |
| 2775 | case LibFunc_fmaxf: |
| 2776 | case LibFunc_fmaxl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2777 | return Intrinsic::maxnum; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2778 | case LibFunc_copysign: |
| 2779 | case LibFunc_copysignf: |
| 2780 | case LibFunc_copysignl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2781 | return Intrinsic::copysign; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2782 | case LibFunc_floor: |
| 2783 | case LibFunc_floorf: |
| 2784 | case LibFunc_floorl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2785 | return Intrinsic::floor; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2786 | case LibFunc_ceil: |
| 2787 | case LibFunc_ceilf: |
| 2788 | case LibFunc_ceill: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2789 | return Intrinsic::ceil; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2790 | case LibFunc_trunc: |
| 2791 | case LibFunc_truncf: |
| 2792 | case LibFunc_truncl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2793 | return Intrinsic::trunc; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2794 | case LibFunc_rint: |
| 2795 | case LibFunc_rintf: |
| 2796 | case LibFunc_rintl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2797 | return Intrinsic::rint; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2798 | case LibFunc_nearbyint: |
| 2799 | case LibFunc_nearbyintf: |
| 2800 | case LibFunc_nearbyintl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2801 | return Intrinsic::nearbyint; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2802 | case LibFunc_round: |
| 2803 | case LibFunc_roundf: |
| 2804 | case LibFunc_roundl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2805 | return Intrinsic::round; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2806 | case LibFunc_pow: |
| 2807 | case LibFunc_powf: |
| 2808 | case LibFunc_powl: |
Ahmed Bougacha | 8a8efec | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2809 | return Intrinsic::pow; |
David L. Jones | 32028c8 | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2810 | case LibFunc_sqrt: |
| 2811 | case LibFunc_sqrtf: |
| 2812 | case LibFunc_sqrtl: |
Sanjay Patel | 618cf29 | 2017-11-06 22:40:09 +0000 | [diff] [blame] | 2813 | return Intrinsic::sqrt; |
David Majnemer | 7f0d15f | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | return Intrinsic::not_intrinsic; |
| 2817 | } |
| 2818 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2819 | /// Return true if we can prove that the specified FP value is never equal to |
| 2820 | /// -0.0. |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2821 | /// |
| 2822 | /// NOTE: this function will need to be revisited when we support non-default |
| 2823 | /// rounding modes! |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2824 | bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, |
| 2825 | unsigned Depth) { |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2826 | if (auto *CFP = dyn_cast<ConstantFP>(V)) |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2827 | return !CFP->getValueAPF().isNegZero(); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2828 | |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2829 | // Limit search depth. |
Matt Arsenault | 96a2ab4 | 2016-12-20 19:06:15 +0000 | [diff] [blame] | 2830 | if (Depth == MaxDepth) |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2831 | return false; |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2832 | |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2833 | auto *Op = dyn_cast<Operator>(V); |
| 2834 | if (!Op) |
| 2835 | return false; |
Michael Ilseman | 85893f4 | 2012-12-06 00:07:09 +0000 | [diff] [blame] | 2836 | |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2837 | // Check if the nsz fast-math flag is set. |
| 2838 | if (auto *FPO = dyn_cast<FPMathOperator>(Op)) |
Michael Ilseman | 85893f4 | 2012-12-06 00:07:09 +0000 | [diff] [blame] | 2839 | if (FPO->hasNoSignedZeros()) |
| 2840 | return true; |
| 2841 | |
Sanjay Patel | b5fc14e | 2017-11-13 17:40:47 +0000 | [diff] [blame] | 2842 | // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. |
Sanjay Patel | 1017678 | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 2843 | if (match(Op, m_FAdd(m_Value(), m_PosZeroFP()))) |
Sanjay Patel | b5fc14e | 2017-11-13 17:40:47 +0000 | [diff] [blame] | 2844 | return true; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2845 | |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2846 | // sitofp and uitofp turn into +0.0 for zero. |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2847 | if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2848 | return true; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2849 | |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2850 | if (auto *Call = dyn_cast<CallInst>(Op)) { |
| 2851 | Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2852 | switch (IID) { |
| 2853 | default: |
| 2854 | break; |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2855 | // sqrt(-0.0) = -0.0, no other negative results are possible. |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2856 | case Intrinsic::sqrt: |
Matt Arsenault | 4e1ab5c | 2018-08-06 15:16:26 +0000 | [diff] [blame] | 2857 | case Intrinsic::canonicalize: |
Sanjay Patel | 0e1d4ee | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2858 | return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2859 | // fabs(x) != -0.0 |
| 2860 | case Intrinsic::fabs: |
| 2861 | return true; |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2862 | } |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2863 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2864 | |
Chris Lattner | 833f25d | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2865 | return false; |
| 2866 | } |
| 2867 | |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2868 | /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a |
| 2869 | /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign |
| 2870 | /// bit despite comparing equal. |
| 2871 | static bool cannotBeOrderedLessThanZeroImpl(const Value *V, |
| 2872 | const TargetLibraryInfo *TLI, |
| 2873 | bool SignBitOnly, |
| 2874 | unsigned Depth) { |
Justin Lebar | 0c722da | 2017-01-27 00:58:34 +0000 | [diff] [blame] | 2875 | // TODO: This function does not do the right thing when SignBitOnly is true |
| 2876 | // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform |
| 2877 | // which flips the sign bits of NaNs. See |
| 2878 | // https://llvm.org/bugs/show_bug.cgi?id=31702. |
| 2879 | |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2880 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 2881 | return !CFP->getValueAPF().isNegative() || |
| 2882 | (!SignBitOnly && CFP->getValueAPF().isZero()); |
| 2883 | } |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2884 | |
Craig Topper | b3dee20 | 2018-02-26 22:33:17 +0000 | [diff] [blame] | 2885 | // Handle vector of constants. |
| 2886 | if (auto *CV = dyn_cast<Constant>(V)) { |
| 2887 | if (CV->getType()->isVectorTy()) { |
| 2888 | unsigned NumElts = CV->getType()->getVectorNumElements(); |
| 2889 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2890 | auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i)); |
| 2891 | if (!CFP) |
| 2892 | return false; |
| 2893 | if (CFP->getValueAPF().isNegative() && |
| 2894 | (SignBitOnly || !CFP->getValueAPF().isZero())) |
| 2895 | return false; |
| 2896 | } |
| 2897 | |
| 2898 | // All non-negative ConstantFPs. |
| 2899 | return true; |
| 2900 | } |
| 2901 | } |
| 2902 | |
Matt Arsenault | 96a2ab4 | 2016-12-20 19:06:15 +0000 | [diff] [blame] | 2903 | if (Depth == MaxDepth) |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2904 | return false; // Limit search depth. |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2905 | |
| 2906 | const Operator *I = dyn_cast<Operator>(V); |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2907 | if (!I) |
| 2908 | return false; |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2909 | |
| 2910 | switch (I->getOpcode()) { |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2911 | default: |
| 2912 | break; |
Fiona Glaser | 8634c66 | 2016-01-12 23:37:30 +0000 | [diff] [blame] | 2913 | // Unsigned integers are always nonnegative. |
| 2914 | case Instruction::UIToFP: |
| 2915 | return true; |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2916 | case Instruction::FMul: |
| 2917 | // x*x is always non-negative or a NaN. |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2918 | if (I->getOperand(0) == I->getOperand(1) && |
| 2919 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs())) |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2920 | return true; |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2921 | |
Justin Bogner | 7d7a23e | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 2922 | LLVM_FALLTHROUGH; |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2923 | case Instruction::FAdd: |
| 2924 | case Instruction::FDiv: |
| 2925 | case Instruction::FRem: |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2926 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2927 | Depth + 1) && |
| 2928 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2929 | Depth + 1); |
Fiona Glaser | 8634c66 | 2016-01-12 23:37:30 +0000 | [diff] [blame] | 2930 | case Instruction::Select: |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2931 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2932 | Depth + 1) && |
| 2933 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, |
| 2934 | Depth + 1); |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2935 | case Instruction::FPExt: |
| 2936 | case Instruction::FPTrunc: |
| 2937 | // Widening/narrowing never change sign. |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2938 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2939 | Depth + 1); |
Craig Topper | 3c6e034 | 2018-02-27 19:53:45 +0000 | [diff] [blame] | 2940 | case Instruction::ExtractElement: |
| 2941 | // Look through extract element. At the moment we keep this simple and skip |
| 2942 | // tracking the specific element. But at least we might find information |
| 2943 | // valid for all elements of the vector. |
| 2944 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2945 | Depth + 1); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2946 | case Instruction::Call: |
Justin Lebar | 34292bd | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2947 | const auto *CI = cast<CallInst>(I); |
| 2948 | Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2949 | switch (IID) { |
| 2950 | default: |
| 2951 | break; |
| 2952 | case Intrinsic::maxnum: |
Matt Arsenault | 0904319 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 2953 | return (isKnownNeverNaN(I->getOperand(0), TLI) && |
Sanjay Patel | fd08621 | 2018-08-02 13:46:20 +0000 | [diff] [blame] | 2954 | cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, |
| 2955 | SignBitOnly, Depth + 1)) || |
Matt Arsenault | 0904319 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 2956 | (isKnownNeverNaN(I->getOperand(1), TLI) && |
Sanjay Patel | fd08621 | 2018-08-02 13:46:20 +0000 | [diff] [blame] | 2957 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, |
| 2958 | SignBitOnly, Depth + 1)); |
| 2959 | |
Thomas Lively | 9e79a4f | 2018-10-19 19:01:26 +0000 | [diff] [blame] | 2960 | case Intrinsic::maximum: |
| 2961 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2962 | Depth + 1) || |
| 2963 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2964 | Depth + 1); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2965 | case Intrinsic::minnum: |
Thomas Lively | 9e79a4f | 2018-10-19 19:01:26 +0000 | [diff] [blame] | 2966 | case Intrinsic::minimum: |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2967 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2968 | Depth + 1) && |
| 2969 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2970 | Depth + 1); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2971 | case Intrinsic::exp: |
| 2972 | case Intrinsic::exp2: |
| 2973 | case Intrinsic::fabs: |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2974 | return true; |
Justin Lebar | 34292bd | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2975 | |
| 2976 | case Intrinsic::sqrt: |
| 2977 | // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0. |
| 2978 | if (!SignBitOnly) |
| 2979 | return true; |
| 2980 | return CI->hasNoNaNs() && (CI->hasNoSignedZeros() || |
| 2981 | CannotBeNegativeZero(CI->getOperand(0), TLI)); |
| 2982 | |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2983 | case Intrinsic::powi: |
Justin Lebar | 34292bd | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2984 | if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) { |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2985 | // powi(x,n) is non-negative if n is even. |
Justin Lebar | 34292bd | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2986 | if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0) |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2987 | return true; |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2988 | } |
Justin Lebar | 0c722da | 2017-01-27 00:58:34 +0000 | [diff] [blame] | 2989 | // TODO: This is not correct. Given that exp is an integer, here are the |
| 2990 | // ways that pow can return a negative value: |
| 2991 | // |
| 2992 | // pow(x, exp) --> negative if exp is odd and x is negative. |
| 2993 | // pow(-0, exp) --> -inf if exp is negative odd. |
| 2994 | // pow(-0, exp) --> -0 if exp is positive odd. |
| 2995 | // pow(-inf, exp) --> -0 if exp is negative odd. |
| 2996 | // pow(-inf, exp) --> -inf if exp is positive odd. |
| 2997 | // |
| 2998 | // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN, |
| 2999 | // but we must return false if x == -0. Unfortunately we do not currently |
| 3000 | // have a way of expressing this constraint. See details in |
| 3001 | // https://llvm.org/bugs/show_bug.cgi?id=31702. |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 3002 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 3003 | Depth + 1); |
Justin Lebar | 0c722da | 2017-01-27 00:58:34 +0000 | [diff] [blame] | 3004 | |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3005 | case Intrinsic::fma: |
| 3006 | case Intrinsic::fmuladd: |
| 3007 | // x*x+y is non-negative if y is non-negative. |
| 3008 | return I->getOperand(0) == I->getOperand(1) && |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 3009 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) && |
| 3010 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, |
| 3011 | Depth + 1); |
David Majnemer | 1559762 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 3012 | } |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3013 | break; |
| 3014 | } |
Sanjoy Das | 36bb502 | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 3015 | return false; |
Elena Demikhovsky | b5c82c0 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Matt Arsenault | 1f4353f | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 3018 | bool llvm::CannotBeOrderedLessThanZero(const Value *V, |
| 3019 | const TargetLibraryInfo *TLI) { |
| 3020 | return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0); |
| 3021 | } |
| 3022 | |
| 3023 | bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) { |
| 3024 | return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0); |
| 3025 | } |
| 3026 | |
Matt Arsenault | 0904319 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 3027 | bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, |
| 3028 | unsigned Depth) { |
Sanjay Patel | 04894a4 | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 3029 | assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type"); |
| 3030 | |
| 3031 | // If we're told that NaNs won't happen, assume they won't. |
| 3032 | if (auto *FPMathOp = dyn_cast<FPMathOperator>(V)) |
| 3033 | if (FPMathOp->hasNoNaNs()) |
| 3034 | return true; |
| 3035 | |
Sanjay Patel | 04894a4 | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 3036 | // Handle scalar constants. |
| 3037 | if (auto *CFP = dyn_cast<ConstantFP>(V)) |
| 3038 | return !CFP->isNaN(); |
| 3039 | |
Matt Arsenault | 0904319 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 3040 | if (Depth == MaxDepth) |
| 3041 | return false; |
| 3042 | |
Matt Arsenault | defe1e2 | 2018-08-20 16:51:00 +0000 | [diff] [blame] | 3043 | if (auto *Inst = dyn_cast<Instruction>(V)) { |
| 3044 | switch (Inst->getOpcode()) { |
| 3045 | case Instruction::FAdd: |
| 3046 | case Instruction::FMul: |
| 3047 | case Instruction::FSub: |
| 3048 | case Instruction::FDiv: |
| 3049 | case Instruction::FRem: { |
| 3050 | // TODO: Need isKnownNeverInfinity |
| 3051 | return false; |
| 3052 | } |
| 3053 | case Instruction::Select: { |
| 3054 | return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) && |
| 3055 | isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1); |
| 3056 | } |
| 3057 | case Instruction::SIToFP: |
| 3058 | case Instruction::UIToFP: |
| 3059 | return true; |
| 3060 | case Instruction::FPTrunc: |
| 3061 | case Instruction::FPExt: |
| 3062 | return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1); |
| 3063 | default: |
| 3064 | break; |
| 3065 | } |
| 3066 | } |
| 3067 | |
Matt Arsenault | 0904319 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 3068 | if (const auto *II = dyn_cast<IntrinsicInst>(V)) { |
| 3069 | switch (II->getIntrinsicID()) { |
| 3070 | case Intrinsic::canonicalize: |
| 3071 | case Intrinsic::fabs: |
| 3072 | case Intrinsic::copysign: |
Matt Arsenault | defe1e2 | 2018-08-20 16:51:00 +0000 | [diff] [blame] | 3073 | case Intrinsic::exp: |
| 3074 | case Intrinsic::exp2: |
| 3075 | case Intrinsic::floor: |
| 3076 | case Intrinsic::ceil: |
| 3077 | case Intrinsic::trunc: |
| 3078 | case Intrinsic::rint: |
| 3079 | case Intrinsic::nearbyint: |
| 3080 | case Intrinsic::round: |
Matt Arsenault | 0904319 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 3081 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1); |
| 3082 | case Intrinsic::sqrt: |
| 3083 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) && |
| 3084 | CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI); |
| 3085 | default: |
| 3086 | return false; |
| 3087 | } |
| 3088 | } |
| 3089 | |
Sanjay Patel | 04894a4 | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 3090 | // Bail out for constant expressions, but try to handle vector constants. |
| 3091 | if (!V->getType()->isVectorTy() || !isa<Constant>(V)) |
| 3092 | return false; |
| 3093 | |
| 3094 | // For vectors, verify that each element is not NaN. |
| 3095 | unsigned NumElts = V->getType()->getVectorNumElements(); |
| 3096 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3097 | Constant *Elt = cast<Constant>(V)->getAggregateElement(i); |
| 3098 | if (!Elt) |
| 3099 | return false; |
| 3100 | if (isa<UndefValue>(Elt)) |
| 3101 | continue; |
| 3102 | auto *CElt = dyn_cast<ConstantFP>(Elt); |
| 3103 | if (!CElt || CElt->isNaN()) |
| 3104 | return false; |
| 3105 | } |
| 3106 | // All elements were confirmed not-NaN or undefined. |
| 3107 | return true; |
| 3108 | } |
| 3109 | |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3110 | Value *llvm::isBytewiseValue(Value *V) { |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3112 | // All byte-wide stores are splatable, even of arbitrary variables. |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3113 | if (V->getType()->isIntegerTy(8)) |
| 3114 | return V; |
| 3115 | |
| 3116 | LLVMContext &Ctx = V->getContext(); |
| 3117 | |
| 3118 | // Undef don't care. |
| 3119 | auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx)); |
| 3120 | if (isa<UndefValue>(V)) |
| 3121 | return UndefInt8; |
| 3122 | |
| 3123 | Constant *C = dyn_cast<Constant>(V); |
| 3124 | if (!C) { |
| 3125 | // Conceptually, we could handle things like: |
| 3126 | // %a = zext i8 %X to i16 |
| 3127 | // %b = shl i16 %a, 8 |
| 3128 | // %c = or i16 %a, %b |
| 3129 | // but until there is an example that actually needs this, it doesn't seem |
| 3130 | // worth worrying about. |
| 3131 | return nullptr; |
| 3132 | } |
Chris Lattner | 41bfbb0 | 2011-02-19 19:35:49 +0000 | [diff] [blame] | 3133 | |
| 3134 | // Handle 'null' ConstantArrayZero etc. |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3135 | if (C->isNullValue()) |
| 3136 | return Constant::getNullValue(Type::getInt8Ty(Ctx)); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3137 | |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3138 | // Constant floating-point values can be handled as integer values if the |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3139 | // corresponding integer value is "byteable". An important case is 0.0. |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3140 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 3141 | Type *Ty = nullptr; |
| 3142 | if (CFP->getType()->isHalfTy()) |
| 3143 | Ty = Type::getInt16Ty(Ctx); |
| 3144 | else if (CFP->getType()->isFloatTy()) |
| 3145 | Ty = Type::getInt32Ty(Ctx); |
| 3146 | else if (CFP->getType()->isDoubleTy()) |
| 3147 | Ty = Type::getInt64Ty(Ctx); |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3148 | // Don't handle long double formats, which have strange constraints. |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3149 | return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty)) : nullptr; |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3150 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3151 | |
Benjamin Kramer | a54b82a | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 3152 | // We can handle constant integers that are multiple of 8 bits. |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3153 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
Benjamin Kramer | a54b82a | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 3154 | if (CI->getBitWidth() % 8 == 0) { |
| 3155 | assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); |
Benjamin Kramer | eaf3cbd | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 3156 | if (!CI->getValue().isSplat(8)) |
Benjamin Kramer | a54b82a | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 3157 | return nullptr; |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3158 | return ConstantInt::get(Ctx, CI->getValue().trunc(8)); |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3159 | } |
| 3160 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3161 | |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3162 | auto Merge = [&](Value *LHS, Value *RHS) -> Value * { |
| 3163 | if (LHS == RHS) |
| 3164 | return LHS; |
| 3165 | if (!LHS || !RHS) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3166 | return nullptr; |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3167 | if (LHS == UndefInt8) |
| 3168 | return RHS; |
| 3169 | if (RHS == UndefInt8) |
| 3170 | return LHS; |
| 3171 | return nullptr; |
| 3172 | }; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3173 | |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3174 | if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) { |
| 3175 | Value *Val = UndefInt8; |
| 3176 | for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I) |
| 3177 | if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I))))) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3178 | return nullptr; |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3179 | return Val; |
| 3180 | } |
Chad Rosier | dce42b7 | 2011-12-06 00:19:08 +0000 | [diff] [blame] | 3181 | |
JF Bastien | 1269b2a | 2018-09-21 05:17:42 +0000 | [diff] [blame] | 3182 | if (isa<ConstantVector>(C)) { |
| 3183 | Constant *Splat = cast<ConstantVector>(C)->getSplatValue(); |
| 3184 | return Splat ? isBytewiseValue(Splat) : nullptr; |
| 3185 | } |
| 3186 | |
| 3187 | if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) { |
| 3188 | Value *Val = UndefInt8; |
| 3189 | for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I) |
| 3190 | if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I))))) |
| 3191 | return nullptr; |
| 3192 | return Val; |
| 3193 | } |
| 3194 | |
| 3195 | // Don't try to handle the handful of other constants. |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3196 | return nullptr; |
Chris Lattner | bb89710 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3199 | // This is the recursive version of BuildSubAggregate. It takes a few different |
| 3200 | // arguments. Idxs is the index within the nested struct From that we are |
| 3201 | // looking at now (which is of type IndexedType). IdxSkip is the number of |
| 3202 | // indices from Idxs that should be left out when inserting into the resulting |
| 3203 | // struct. To is the result struct built so far, new insertvalue instructions |
| 3204 | // build on that. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3205 | static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, |
Craig Topper | 9e639e8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 3206 | SmallVectorImpl<unsigned> &Idxs, |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 3207 | unsigned IdxSkip, |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 3208 | Instruction *InsertBefore) { |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3209 | StructType *STy = dyn_cast<StructType>(IndexedType); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3210 | if (STy) { |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3211 | // Save the original To argument so we can modify it |
| 3212 | Value *OrigTo = To; |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3213 | // General case, the type indexed by Idxs is a struct |
| 3214 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 3215 | // Process each struct element recursively |
| 3216 | Idxs.push_back(i); |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3217 | Value *PrevTo = To; |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 3218 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3219 | InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3220 | Idxs.pop_back(); |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3221 | if (!To) { |
| 3222 | // Couldn't find any inserted value for this index? Cleanup |
| 3223 | while (PrevTo != OrigTo) { |
| 3224 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); |
| 3225 | PrevTo = Del->getAggregateOperand(); |
| 3226 | Del->eraseFromParent(); |
| 3227 | } |
| 3228 | // Stop processing elements |
| 3229 | break; |
| 3230 | } |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3231 | } |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 3232 | // If we successfully found a value for each of our subaggregates |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3233 | if (To) |
| 3234 | return To; |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3235 | } |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3236 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of |
| 3237 | // the struct's elements had a value that was inserted directly. In the latter |
| 3238 | // case, perhaps we can't determine each of the subelements individually, but |
| 3239 | // we might be able to find the complete struct somewhere. |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3240 | |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3241 | // Find the value that is at that particular spot |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3242 | Value *V = FindInsertedValue(From, Idxs); |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3243 | |
| 3244 | if (!V) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3245 | return nullptr; |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3246 | |
Vedant Kumar | 06a2673 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 3247 | // Insert the value in the new (sub) aggregate |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3248 | return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip), |
| 3249 | "tmp", InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | // This helper takes a nested struct and extracts a part of it (which is again a |
| 3253 | // struct) into a new value. For example, given the struct: |
| 3254 | // { a, { b, { c, d }, e } } |
| 3255 | // and the indices "1, 1" this returns |
| 3256 | // { c, d }. |
| 3257 | // |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3258 | // It does this by inserting an insertvalue for each element in the resulting |
| 3259 | // struct, as opposed to just inserting a single struct. This will only work if |
| 3260 | // each of the elements of the substruct are known (ie, inserted into From by an |
| 3261 | // insertvalue instruction somewhere). |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3262 | // |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3263 | // All inserted insertvalue instructions are inserted before InsertBefore |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3264 | static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range, |
Dan Gohman | 7db949d | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 3265 | Instruction *InsertBefore) { |
Matthijs Kooijman | 9772891 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 3266 | assert(InsertBefore && "Must have someplace to insert!"); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3267 | Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3268 | idx_range); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3269 | Value *To = UndefValue::get(IndexedType); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3270 | SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3271 | unsigned IdxSkip = Idxs.size(); |
| 3272 | |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3273 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
Vedant Kumar | 06a2673 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 3276 | /// Given an aggregate and a sequence of indices, see if the scalar value |
| 3277 | /// indexed is already around as a register, for example if it was inserted |
| 3278 | /// directly into the aggregate. |
Matthijs Kooijman | 0a9aaf4 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3279 | /// |
| 3280 | /// If InsertBefore is not null, this function will duplicate (modified) |
| 3281 | /// insertvalues when a part of a nested struct is extracted. |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3282 | Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, |
| 3283 | Instruction *InsertBefore) { |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3284 | // Nothing to index? Just return V then (this is useful at the end of our |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3285 | // recursion). |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3286 | if (idx_range.empty()) |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3287 | return V; |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3288 | // We have indices, so V should have an indexable type. |
| 3289 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) && |
| 3290 | "Not looking at a struct or array?"); |
| 3291 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && |
| 3292 | "Invalid indices for type?"); |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 3293 | |
Chris Lattner | a1f00f4 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 3294 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 3295 | C = C->getAggregateElement(idx_range[0]); |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3296 | if (!C) return nullptr; |
Chris Lattner | a1f00f4 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 3297 | return FindInsertedValue(C, idx_range.slice(1), InsertBefore); |
| 3298 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3299 | |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3300 | if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3301 | // Loop the indices for the insertvalue instruction in parallel with the |
| 3302 | // requested indices |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3303 | const unsigned *req_idx = idx_range.begin(); |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 3304 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
| 3305 | i != e; ++i, ++req_idx) { |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3306 | if (req_idx == idx_range.end()) { |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3307 | // We can't handle this without inserting insertvalues |
| 3308 | if (!InsertBefore) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3309 | return nullptr; |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3310 | |
| 3311 | // The requested index identifies a part of a nested aggregate. Handle |
| 3312 | // this specially. For example, |
| 3313 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
| 3314 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
| 3315 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 |
| 3316 | // This can be changed into |
| 3317 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 |
| 3318 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 |
| 3319 | // which allows the unused 0,0 element from the nested struct to be |
| 3320 | // removed. |
| 3321 | return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx), |
| 3322 | InsertBefore); |
Duncan Sands | 9954c76 | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 3323 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3324 | |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3325 | // This insert value inserts something else than what we are looking for. |
Benjamin Kramer | d3c712e | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3326 | // See if the (aggregate) value inserted into has the value we are |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3327 | // looking for, then. |
| 3328 | if (*req_idx != *i) |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3329 | return FindInsertedValue(I->getAggregateOperand(), idx_range, |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3330 | InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3331 | } |
| 3332 | // If we end up here, the indices of the insertvalue match with those |
| 3333 | // requested (though possibly only partially). Now we recursively look at |
| 3334 | // the inserted value, passing any remaining indices. |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3335 | return FindInsertedValue(I->getInsertedValueOperand(), |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 3336 | makeArrayRef(req_idx, idx_range.end()), |
Nick Lewycky | ae3d802 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3337 | InsertBefore); |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3338 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3339 | |
Chris Lattner | df39028 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3340 | if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { |
Benjamin Kramer | d3c712e | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3341 | // If we're extracting a value from an aggregate that was extracted from |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3342 | // something else, we can extract from that something else directly instead. |
| 3343 | // However, we will need to chain I's indices with the requested indices. |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3344 | |
| 3345 | // Calculate the number of indices required |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3346 | unsigned size = I->getNumIndices() + idx_range.size(); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3347 | // Allocate some space to put the new indices in |
Matthijs Kooijman | 3faf9df | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 3348 | SmallVector<unsigned, 5> Idxs; |
| 3349 | Idxs.reserve(size); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3350 | // Add indices from the extract value instruction |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3351 | Idxs.append(I->idx_begin(), I->idx_end()); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3352 | |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3353 | // Add requested indices |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3354 | Idxs.append(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3355 | |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3356 | assert(Idxs.size() == size |
Matthijs Kooijman | 710eb23 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 3357 | && "Number of indices added not correct?"); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3358 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3359 | return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3360 | } |
| 3361 | // Otherwise, we don't know (such as, extracting from a function return value |
| 3362 | // or load instruction) |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3363 | return nullptr; |
Matthijs Kooijman | b23d5ad | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3364 | } |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3365 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3366 | /// Analyze the specified pointer to see if it can be expressed as a base |
| 3367 | /// pointer plus a constant offset. Return the base and offset to the caller. |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3368 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3369 | const DataLayout &DL) { |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3370 | unsigned BitWidth = DL.getIndexTypeSizeInBits(Ptr->getType()); |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3371 | APInt ByteOffset(BitWidth, 0); |
Chandler Carruth | ab99ec2 | 2016-01-04 07:23:12 +0000 | [diff] [blame] | 3372 | |
| 3373 | // We walk up the defs but use a visited set to handle unreachable code. In |
| 3374 | // that case, we stop after accumulating the cycle once (not that it |
| 3375 | // matters). |
| 3376 | SmallPtrSet<Value *, 16> Visited; |
| 3377 | while (Visited.insert(Ptr).second) { |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3378 | if (Ptr->getType()->isVectorTy()) |
| 3379 | break; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3380 | |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3381 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
Tom Stellard | 8b2bb5d | 2016-10-07 14:23:29 +0000 | [diff] [blame] | 3382 | // If one of the values we have visited is an addrspacecast, then |
| 3383 | // the pointer type of this GEP may be different from the type |
| 3384 | // of the Ptr parameter which was passed to this function. This |
| 3385 | // means when we construct GEPOffset, we need to use the size |
| 3386 | // of GEP's pointer type rather than the size of the original |
| 3387 | // pointer type. |
Elena Demikhovsky | 8e229ec | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3388 | APInt GEPOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3389 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
| 3390 | break; |
Matt Arsenault | d080fb1 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 3391 | |
Florian Hahn | 5692e20 | 2019-01-04 14:53:22 +0000 | [diff] [blame] | 3392 | APInt OrigByteOffset(ByteOffset); |
| 3393 | ByteOffset += GEPOffset.sextOrTrunc(ByteOffset.getBitWidth()); |
| 3394 | if (ByteOffset.getMinSignedBits() > 64) { |
| 3395 | // Stop traversal if the pointer offset wouldn't fit into int64_t |
| 3396 | // (this should be removed if Offset is updated to an APInt) |
| 3397 | ByteOffset = OrigByteOffset; |
| 3398 | break; |
| 3399 | } |
Matt Arsenault | d080fb1 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 3400 | |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3401 | Ptr = GEP->getPointerOperand(); |
Tom Stellard | 8b2bb5d | 2016-10-07 14:23:29 +0000 | [diff] [blame] | 3402 | } else if (Operator::getOpcode(Ptr) == Instruction::BitCast || |
| 3403 | Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) { |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3404 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 3405 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
Sanjoy Das | c9e3e3c | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 3406 | if (GA->isInterposable()) |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | Ptr = GA->getAliasee(); |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3409 | } else { |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3410 | break; |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3411 | } |
| 3412 | } |
Nuno Lopes | 5cec347 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3413 | Offset = ByteOffset.getSExtValue(); |
| 3414 | return Ptr; |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3417 | bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP, |
| 3418 | unsigned CharSize) { |
David L Kreitzer | 3592381 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3419 | // Make sure the GEP has exactly three arguments. |
| 3420 | if (GEP->getNumOperands() != 3) |
| 3421 | return false; |
| 3422 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3423 | // Make sure the index-ee is a pointer to array of \p CharSize integers. |
| 3424 | // CharSize. |
David L Kreitzer | 3592381 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3425 | ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType()); |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3426 | if (!AT || !AT->getElementType()->isIntegerTy(CharSize)) |
David L Kreitzer | 3592381 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3427 | return false; |
| 3428 | |
| 3429 | // Check to make sure that the first operand of the GEP is an integer and |
| 3430 | // has value 0 so that we are sure we're indexing into the initializer. |
| 3431 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); |
| 3432 | if (!FirstIdx || !FirstIdx->isZero()) |
| 3433 | return false; |
| 3434 | |
| 3435 | return true; |
Sanjoy Das | 36bb502 | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 3436 | } |
Chris Lattner | ed58a6f | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3437 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3438 | bool llvm::getConstantDataArrayInfo(const Value *V, |
| 3439 | ConstantDataArraySlice &Slice, |
| 3440 | unsigned ElementSize, uint64_t Offset) { |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3441 | assert(V); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3442 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3443 | // Look through bitcast instructions and geps. |
| 3444 | V = V->stripPointerCasts(); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3445 | |
Benjamin Kramer | 046c58a | 2015-03-21 15:36:06 +0000 | [diff] [blame] | 3446 | // If the value is a GEP instruction or constant expression, treat it as an |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3447 | // offset. |
| 3448 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
David L Kreitzer | 3592381 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3449 | // The GEP operator should be based on a pointer to string constant, and is |
| 3450 | // indexing into the string constant. |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3451 | if (!isGEPBasedOnPointerToString(GEP, ElementSize)) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3452 | return false; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3453 | |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3454 | // If the second index isn't a ConstantInt, then this is a variable index |
| 3455 | // into the array. If this occurs, we can't say anything meaningful about |
| 3456 | // the string. |
| 3457 | uint64_t StartIdx = 0; |
Dan Gohman | 0a60fa3 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 3458 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3459 | StartIdx = CI->getZExtValue(); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3460 | else |
| 3461 | return false; |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3462 | return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize, |
| 3463 | StartIdx + Offset); |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3464 | } |
Nick Lewycky | 0cd0fee | 2011-10-20 00:34:35 +0000 | [diff] [blame] | 3465 | |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3466 | // The GEP instruction, constant or instruction, must reference a global |
| 3467 | // variable that is a constant and is initialized. The referenced constant |
| 3468 | // initializer is the array that we'll use for optimization. |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3469 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
Dan Gohman | 8255573 | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 3470 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3471 | return false; |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3472 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3473 | const ConstantDataArray *Array; |
| 3474 | ArrayType *ArrayTy; |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3475 | if (GV->getInitializer()->isNullValue()) { |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3476 | Type *GVTy = GV->getValueType(); |
| 3477 | if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) { |
Sanjay Patel | 44863cb | 2017-06-12 22:34:37 +0000 | [diff] [blame] | 3478 | // A zeroinitializer for the array; there is no ConstantDataArray. |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3479 | Array = nullptr; |
| 3480 | } else { |
| 3481 | const DataLayout &DL = GV->getParent()->getDataLayout(); |
| 3482 | uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy); |
| 3483 | uint64_t Length = SizeInBytes / (ElementSize / 8); |
| 3484 | if (Length <= Offset) |
| 3485 | return false; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3486 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3487 | Slice.Array = nullptr; |
| 3488 | Slice.Offset = 0; |
| 3489 | Slice.Length = Length - Offset; |
| 3490 | return true; |
| 3491 | } |
| 3492 | } else { |
| 3493 | // This must be a ConstantDataArray. |
| 3494 | Array = dyn_cast<ConstantDataArray>(GV->getInitializer()); |
| 3495 | if (!Array) |
| 3496 | return false; |
| 3497 | ArrayTy = Array->getType(); |
| 3498 | } |
| 3499 | if (!ArrayTy->getElementType()->isIntegerTy(ElementSize)) |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3500 | return false; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3501 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3502 | uint64_t NumElts = ArrayTy->getArrayNumElements(); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3503 | if (Offset > NumElts) |
| 3504 | return false; |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3505 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3506 | Slice.Array = Array; |
| 3507 | Slice.Offset = Offset; |
| 3508 | Slice.Length = NumElts - Offset; |
| 3509 | return true; |
| 3510 | } |
| 3511 | |
| 3512 | /// This function computes the length of a null-terminated C string pointed to |
| 3513 | /// by V. If successful, it returns true and returns the string in Str. |
| 3514 | /// If unsuccessful, it returns false. |
| 3515 | bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, |
| 3516 | uint64_t Offset, bool TrimAtNul) { |
| 3517 | ConstantDataArraySlice Slice; |
| 3518 | if (!getConstantDataArrayInfo(V, Slice, 8, Offset)) |
| 3519 | return false; |
| 3520 | |
| 3521 | if (Slice.Array == nullptr) { |
| 3522 | if (TrimAtNul) { |
| 3523 | Str = StringRef(); |
| 3524 | return true; |
| 3525 | } |
| 3526 | if (Slice.Length == 1) { |
| 3527 | Str = StringRef("", 1); |
| 3528 | return true; |
| 3529 | } |
Sanjay Patel | c590cff | 2017-06-09 14:21:18 +0000 | [diff] [blame] | 3530 | // We cannot instantiate a StringRef as we do not have an appropriate string |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3531 | // of 0s at hand. |
| 3532 | return false; |
| 3533 | } |
| 3534 | |
| 3535 | // Start out with the entire array in the StringRef. |
| 3536 | Str = Slice.Array->getAsString(); |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3537 | // Skip over 'offset' bytes. |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3538 | Str = Str.substr(Slice.Offset); |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3539 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3540 | if (TrimAtNul) { |
| 3541 | // Trim off the \0 and anything after it. If the array is not nul |
| 3542 | // terminated, we just return the whole end of string. The client may know |
| 3543 | // some other way that the string is length-bound. |
| 3544 | Str = Str.substr(0, Str.find('\0')); |
| 3545 | } |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3546 | return true; |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3547 | } |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3548 | |
| 3549 | // These next two are very similar to the above, but also look through PHI |
| 3550 | // nodes. |
| 3551 | // TODO: See if we can integrate these two together. |
| 3552 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3553 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3554 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3555 | static uint64_t GetStringLengthH(const Value *V, |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3556 | SmallPtrSetImpl<const PHINode*> &PHIs, |
| 3557 | unsigned CharSize) { |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3558 | // Look through noop bitcast instructions. |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3559 | V = V->stripPointerCasts(); |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3560 | |
| 3561 | // If this is a PHI node, there are two cases: either we have already seen it |
| 3562 | // or we haven't. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3563 | if (const PHINode *PN = dyn_cast<PHINode>(V)) { |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3564 | if (!PHIs.insert(PN).second) |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3565 | return ~0ULL; // already in the set. |
| 3566 | |
| 3567 | // If it was new, see if all the input strings are the same length. |
| 3568 | uint64_t LenSoFar = ~0ULL; |
Pete Cooper | f23c6af | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3569 | for (Value *IncValue : PN->incoming_values()) { |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3570 | uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize); |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3571 | if (Len == 0) return 0; // Unknown length -> unknown. |
| 3572 | |
| 3573 | if (Len == ~0ULL) continue; |
| 3574 | |
| 3575 | if (Len != LenSoFar && LenSoFar != ~0ULL) |
| 3576 | return 0; // Disagree -> unknown. |
| 3577 | LenSoFar = Len; |
| 3578 | } |
| 3579 | |
| 3580 | // Success, all agree. |
| 3581 | return LenSoFar; |
| 3582 | } |
| 3583 | |
| 3584 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3585 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3586 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize); |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3587 | if (Len1 == 0) return 0; |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3588 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize); |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3589 | if (Len2 == 0) return 0; |
| 3590 | if (Len1 == ~0ULL) return Len2; |
| 3591 | if (Len2 == ~0ULL) return Len1; |
| 3592 | if (Len1 != Len2) return 0; |
| 3593 | return Len1; |
| 3594 | } |
Craig Topper | c4265e1 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3595 | |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3596 | // Otherwise, see if we can read the string. |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3597 | ConstantDataArraySlice Slice; |
| 3598 | if (!getConstantDataArrayInfo(V, Slice, CharSize)) |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3599 | return 0; |
| 3600 | |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3601 | if (Slice.Array == nullptr) |
| 3602 | return 1; |
| 3603 | |
| 3604 | // Search for nul characters |
| 3605 | unsigned NullIndex = 0; |
| 3606 | for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) { |
| 3607 | if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0) |
| 3608 | break; |
| 3609 | } |
| 3610 | |
| 3611 | return NullIndex + 1; |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3614 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3615 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
David Bolvansky | 15b6321 | 2018-05-22 20:27:36 +0000 | [diff] [blame] | 3616 | uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) { |
David Bolvansky | 851bb22 | 2018-05-22 15:41:23 +0000 | [diff] [blame] | 3617 | if (!V->getType()->isPointerTy()) |
| 3618 | return 0; |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3619 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3620 | SmallPtrSet<const PHINode*, 32> PHIs; |
Matthias Braun | 708626d | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3621 | uint64_t Len = GetStringLengthH(V, PHIs, CharSize); |
Eric Christopher | 25ec483 | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3622 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
| 3623 | // an empty string as a length. |
| 3624 | return Len == ~0ULL ? 1 : Len; |
| 3625 | } |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3626 | |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 3627 | const Value *llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call) { |
| 3628 | assert(Call && |
| 3629 | "getArgumentAliasingToReturnedPointer only works on nonnull calls"); |
| 3630 | if (const Value *RV = Call->getReturnedArgOperand()) |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3631 | return RV; |
| 3632 | // This can be used only as a aliasing property. |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 3633 | if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call)) |
| 3634 | return Call->getArgOperand(0); |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3635 | return nullptr; |
| 3636 | } |
| 3637 | |
| 3638 | bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 3639 | const CallBase *Call) { |
| 3640 | return Call->getIntrinsicID() == Intrinsic::launder_invariant_group || |
| 3641 | Call->getIntrinsicID() == Intrinsic::strip_invariant_group; |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3644 | /// \p PN defines a loop-variant pointer to an object. Check if the |
Adam Nemet | 50b9e7f | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3645 | /// previous iteration of the loop was referring to the same object as \p PN. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3646 | static bool isSameUnderlyingObjectInLoop(const PHINode *PN, |
| 3647 | const LoopInfo *LI) { |
Adam Nemet | 50b9e7f | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3648 | // Find the loop-defined value. |
| 3649 | Loop *L = LI->getLoopFor(PN->getParent()); |
| 3650 | if (PN->getNumIncomingValues() != 2) |
| 3651 | return true; |
| 3652 | |
| 3653 | // Find the value from previous iteration. |
| 3654 | auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0)); |
| 3655 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 3656 | PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1)); |
| 3657 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 3658 | return true; |
| 3659 | |
| 3660 | // If a new pointer is loaded in the loop, the pointer references a different |
| 3661 | // object in every iteration. E.g.: |
| 3662 | // for (i) |
| 3663 | // int *p = a[i]; |
| 3664 | // ... |
| 3665 | if (auto *Load = dyn_cast<LoadInst>(PrevValue)) |
| 3666 | if (!L->isLoopInvariant(Load->getPointerOperand())) |
| 3667 | return false; |
| 3668 | return true; |
| 3669 | } |
| 3670 | |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3671 | Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL, |
| 3672 | unsigned MaxLookup) { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3673 | if (!V->getType()->isPointerTy()) |
| 3674 | return V; |
| 3675 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
| 3676 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 3677 | V = GEP->getPointerOperand(); |
Matt Arsenault | 7137eb3 | 2014-07-15 00:56:40 +0000 | [diff] [blame] | 3678 | } else if (Operator::getOpcode(V) == Instruction::BitCast || |
| 3679 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3680 | V = cast<Operator>(V)->getOperand(0); |
| 3681 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | c9e3e3c | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 3682 | if (GA->isInterposable()) |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3683 | return V; |
| 3684 | V = GA->getAliasee(); |
Craig Topper | 53726fa | 2017-04-12 22:29:23 +0000 | [diff] [blame] | 3685 | } else if (isa<AllocaInst>(V)) { |
| 3686 | // An alloca can't be further simplified. |
| 3687 | return V; |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3688 | } else { |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 3689 | if (auto *Call = dyn_cast<CallBase>(V)) { |
Piotr Padlewski | c2f24d9 | 2018-07-02 04:49:30 +0000 | [diff] [blame] | 3690 | // CaptureTracking can know about special capturing properties of some |
| 3691 | // intrinsics like launder.invariant.group, that can't be expressed with |
| 3692 | // the attributes, but have properties like returning aliasing pointer. |
| 3693 | // Because some analysis may assume that nocaptured pointer is not |
| 3694 | // returned from some special intrinsic (because function would have to |
| 3695 | // be marked with returns attribute), it is crucial to use this function |
| 3696 | // because it should be in sync with CaptureTracking. Not using it may |
| 3697 | // cause weird miscompilations where 2 aliasing pointers are assumed to |
| 3698 | // noalias. |
Chandler Carruth | 81aa712 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 3699 | if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) { |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3700 | V = RP; |
Hal Finkel | 4cb3366 | 2016-07-11 01:32:20 +0000 | [diff] [blame] | 3701 | continue; |
| 3702 | } |
Piotr Padlewski | 2e3226a | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3703 | } |
Hal Finkel | 4cb3366 | 2016-07-11 01:32:20 +0000 | [diff] [blame] | 3704 | |
Dan Gohman | c01895c | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 3705 | // See if InstructionSimplify knows any relevant tricks. |
| 3706 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3707 | // TODO: Acquire a DominatorTree and AssumptionCache and use them. |
Daniel Berlin | e2c5126 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 3708 | if (Value *Simplified = SimplifyInstruction(I, {DL, I})) { |
Dan Gohman | c01895c | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 3709 | V = Simplified; |
| 3710 | continue; |
| 3711 | } |
| 3712 | |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3713 | return V; |
| 3714 | } |
| 3715 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 3716 | } |
| 3717 | return V; |
| 3718 | } |
Nick Lewycky | 99e0b2a | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3719 | |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3720 | void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects, |
Adam Nemet | 50b9e7f | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3721 | const DataLayout &DL, LoopInfo *LI, |
| 3722 | unsigned MaxLookup) { |
Dan Gohman | b401e3b | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3723 | SmallPtrSet<Value *, 4> Visited; |
| 3724 | SmallVector<Value *, 4> Worklist; |
| 3725 | Worklist.push_back(V); |
| 3726 | do { |
| 3727 | Value *P = Worklist.pop_back_val(); |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3728 | P = GetUnderlyingObject(P, DL, MaxLookup); |
Dan Gohman | b401e3b | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3729 | |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3730 | if (!Visited.insert(P).second) |
Dan Gohman | b401e3b | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3731 | continue; |
| 3732 | |
| 3733 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 3734 | Worklist.push_back(SI->getTrueValue()); |
| 3735 | Worklist.push_back(SI->getFalseValue()); |
| 3736 | continue; |
| 3737 | } |
| 3738 | |
| 3739 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Adam Nemet | 50b9e7f | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3740 | // If this PHI changes the underlying object in every iteration of the |
| 3741 | // loop, don't look through it. Consider: |
| 3742 | // int **A; |
| 3743 | // for (i) { |
| 3744 | // Prev = Curr; // Prev = PHI (Prev_0, Curr) |
| 3745 | // Curr = A[i]; |
| 3746 | // *Prev, *Curr; |
| 3747 | // |
| 3748 | // Prev is tracking Curr one iteration behind so they refer to different |
| 3749 | // underlying objects. |
| 3750 | if (!LI || !LI->isLoopHeader(PN->getParent()) || |
| 3751 | isSameUnderlyingObjectInLoop(PN, LI)) |
Pete Cooper | f23c6af | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3752 | for (Value *IncValue : PN->incoming_values()) |
| 3753 | Worklist.push_back(IncValue); |
Dan Gohman | b401e3b | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3754 | continue; |
| 3755 | } |
| 3756 | |
| 3757 | Objects.push_back(P); |
| 3758 | } while (!Worklist.empty()); |
| 3759 | } |
| 3760 | |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3761 | /// This is the function that does the work of looking through basic |
| 3762 | /// ptrtoint+arithmetic+inttoptr sequences. |
| 3763 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 3764 | do { |
| 3765 | if (const Operator *U = dyn_cast<Operator>(V)) { |
| 3766 | // If we find a ptrtoint, we can transfer control back to the |
| 3767 | // regular getUnderlyingObjectFromInt. |
| 3768 | if (U->getOpcode() == Instruction::PtrToInt) |
| 3769 | return U->getOperand(0); |
| 3770 | // If we find an add of a constant, a multiplied value, or a phi, it's |
| 3771 | // likely that the other operand will lead us to the base |
| 3772 | // object. We don't have to worry about the case where the |
| 3773 | // object address is somehow being computed by the multiply, |
| 3774 | // because our callers only care when the result is an |
| 3775 | // identifiable object. |
| 3776 | if (U->getOpcode() != Instruction::Add || |
| 3777 | (!isa<ConstantInt>(U->getOperand(1)) && |
| 3778 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && |
| 3779 | !isa<PHINode>(U->getOperand(1)))) |
| 3780 | return V; |
| 3781 | V = U->getOperand(0); |
| 3782 | } else { |
| 3783 | return V; |
| 3784 | } |
| 3785 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
| 3786 | } while (true); |
| 3787 | } |
| 3788 | |
| 3789 | /// This is a wrapper around GetUnderlyingObjects and adds support for basic |
| 3790 | /// ptrtoint+arithmetic+inttoptr sequences. |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 3791 | /// It returns false if unidentified object is found in GetUnderlyingObjects. |
| 3792 | bool llvm::getUnderlyingObjectsForCodeGen(const Value *V, |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3793 | SmallVectorImpl<Value *> &Objects, |
| 3794 | const DataLayout &DL) { |
| 3795 | SmallPtrSet<const Value *, 16> Visited; |
| 3796 | SmallVector<const Value *, 4> Working(1, V); |
| 3797 | do { |
| 3798 | V = Working.pop_back_val(); |
| 3799 | |
| 3800 | SmallVector<Value *, 4> Objs; |
| 3801 | GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL); |
| 3802 | |
| 3803 | for (Value *V : Objs) { |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3804 | if (!Visited.insert(V).second) |
| 3805 | continue; |
| 3806 | if (Operator::getOpcode(V) == Instruction::IntToPtr) { |
| 3807 | const Value *O = |
| 3808 | getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 3809 | if (O->getType()->isPointerTy()) { |
| 3810 | Working.push_back(O); |
| 3811 | continue; |
| 3812 | } |
| 3813 | } |
Hiroshi Inoue | f49a981 | 2017-08-02 18:16:32 +0000 | [diff] [blame] | 3814 | // If GetUnderlyingObjects fails to find an identifiable object, |
| 3815 | // getUnderlyingObjectsForCodeGen also fails for safety. |
| 3816 | if (!isIdentifiedObject(V)) { |
| 3817 | Objects.clear(); |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 3818 | return false; |
Hiroshi Inoue | f49a981 | 2017-08-02 18:16:32 +0000 | [diff] [blame] | 3819 | } |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3820 | Objects.push_back(const_cast<Value *>(V)); |
| 3821 | } |
| 3822 | } while (!Working.empty()); |
Hiroshi Inoue | 0f48afc | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 3823 | return true; |
Hiroshi Inoue | eeca49d | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
Sanjay Patel | 0bbad01 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3826 | /// Return true if the only users of this pointer are lifetime markers. |
Nick Lewycky | 99e0b2a | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3827 | bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { |
Chandler Carruth | 36b699f | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3828 | for (const User *U : V->users()) { |
| 3829 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); |
Nick Lewycky | 99e0b2a | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3830 | if (!II) return false; |
| 3831 | |
Vedant Kumar | eeb1971 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 3832 | if (!II->isLifetimeStartOrEnd()) |
Nick Lewycky | 99e0b2a | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3833 | return false; |
| 3834 | } |
| 3835 | return true; |
| 3836 | } |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3837 | |
Sanjoy Das | 0799eb1 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3838 | bool llvm::isSafeToSpeculativelyExecute(const Value *V, |
| 3839 | const Instruction *CtxI, |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 3840 | const DominatorTree *DT) { |
Dan Gohman | febaf84 | 2012-01-04 23:01:09 +0000 | [diff] [blame] | 3841 | const Operator *Inst = dyn_cast<Operator>(V); |
| 3842 | if (!Inst) |
| 3843 | return false; |
| 3844 | |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3845 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 3846 | if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i))) |
| 3847 | if (C->canTrap()) |
| 3848 | return false; |
| 3849 | |
| 3850 | switch (Inst->getOpcode()) { |
| 3851 | default: |
| 3852 | return true; |
| 3853 | case Instruction::UDiv: |
David Majnemer | 3839fd1 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3854 | case Instruction::URem: { |
| 3855 | // x / y is undefined if y == 0. |
| 3856 | const APInt *V; |
| 3857 | if (match(Inst->getOperand(1), m_APInt(V))) |
| 3858 | return *V != 0; |
| 3859 | return false; |
| 3860 | } |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3861 | case Instruction::SDiv: |
| 3862 | case Instruction::SRem: { |
David Majnemer | 3839fd1 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3863 | // x / y is undefined if y == 0 or x == INT_MIN and y == -1 |
David Majnemer | 9e8b721 | 2015-02-01 19:10:19 +0000 | [diff] [blame] | 3864 | const APInt *Numerator, *Denominator; |
| 3865 | if (!match(Inst->getOperand(1), m_APInt(Denominator))) |
| 3866 | return false; |
| 3867 | // We cannot hoist this division if the denominator is 0. |
| 3868 | if (*Denominator == 0) |
| 3869 | return false; |
| 3870 | // It's safe to hoist if the denominator is not 0 or -1. |
| 3871 | if (*Denominator != -1) |
| 3872 | return true; |
| 3873 | // At this point we know that the denominator is -1. It is safe to hoist as |
| 3874 | // long we know that the numerator is not INT_MIN. |
| 3875 | if (match(Inst->getOperand(0), m_APInt(Numerator))) |
| 3876 | return !Numerator->isMinSignedValue(); |
| 3877 | // The numerator *might* be MinSignedValue. |
David Majnemer | 3839fd1 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3878 | return false; |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3879 | } |
| 3880 | case Instruction::Load: { |
| 3881 | const LoadInst *LI = cast<LoadInst>(Inst); |
Kostya Serebryany | 42a9da3 | 2013-11-21 07:29:28 +0000 | [diff] [blame] | 3882 | if (!LI->isUnordered() || |
| 3883 | // Speculative load may create a race that did not exist in the source. |
Sanjoy Das | f5f726b | 2016-07-14 20:19:01 +0000 | [diff] [blame] | 3884 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeThread) || |
Kostya Serebryany | 9253eff | 2015-10-14 00:21:05 +0000 | [diff] [blame] | 3885 | // Speculative load may load data from dirty regions. |
Evgeniy Stepanov | d47b5b3 | 2017-12-09 00:21:41 +0000 | [diff] [blame] | 3886 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) || |
| 3887 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress)) |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3888 | return false; |
Mehdi Amini | 529919f | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3889 | const DataLayout &DL = LI->getModule()->getDataLayout(); |
Sean Silva | e82e4dd | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 3890 | return isDereferenceableAndAlignedPointer(LI->getPointerOperand(), |
| 3891 | LI->getAlignment(), DL, CtxI, DT); |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3892 | } |
Nick Lewycky | 8369687 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3893 | case Instruction::Call: { |
Matt Arsenault | ba65706 | 2017-04-28 21:13:09 +0000 | [diff] [blame] | 3894 | auto *CI = cast<const CallInst>(Inst); |
| 3895 | const Function *Callee = CI->getCalledFunction(); |
David Majnemer | c13175f | 2015-08-28 21:13:39 +0000 | [diff] [blame] | 3896 | |
Matt Arsenault | 921f454 | 2017-05-03 02:26:10 +0000 | [diff] [blame] | 3897 | // The called function could have undefined behavior or side-effects, even |
| 3898 | // if marked readnone nounwind. |
| 3899 | return Callee && Callee->isSpeculatable(); |
Nick Lewycky | 8369687 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3900 | } |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3901 | case Instruction::VAArg: |
| 3902 | case Instruction::Alloca: |
| 3903 | case Instruction::Invoke: |
| 3904 | case Instruction::PHI: |
| 3905 | case Instruction::Store: |
| 3906 | case Instruction::Ret: |
| 3907 | case Instruction::Br: |
| 3908 | case Instruction::IndirectBr: |
| 3909 | case Instruction::Switch: |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3910 | case Instruction::Unreachable: |
| 3911 | case Instruction::Fence: |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3912 | case Instruction::AtomicRMW: |
| 3913 | case Instruction::AtomicCmpXchg: |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3914 | case Instruction::LandingPad: |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3915 | case Instruction::Resume: |
David Majnemer | 8cec2f2 | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3916 | case Instruction::CatchSwitch: |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3917 | case Instruction::CatchPad: |
David Majnemer | 4a45f08 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3918 | case Instruction::CatchRet: |
| 3919 | case Instruction::CleanupPad: |
| 3920 | case Instruction::CleanupRet: |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3921 | return false; // Misc instructions which have effects |
| 3922 | } |
| 3923 | } |
Dan Gohman | de0eb19 | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3924 | |
Quentin Colombet | f5232c2 | 2015-08-06 18:44:34 +0000 | [diff] [blame] | 3925 | bool llvm::mayBeMemoryDependent(const Instruction &I) { |
| 3926 | return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I); |
| 3927 | } |
| 3928 | |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3929 | OverflowResult llvm::computeOverflowForUnsignedMul( |
| 3930 | const Value *LHS, const Value *RHS, const DataLayout &DL, |
| 3931 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 3932 | bool UseInstrInfo) { |
David Majnemer | 25e8e79 | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3933 | // Multiplying n * m significant bits yields a result of n + m significant |
| 3934 | // bits. If the total number of significant bits does not exceed the |
| 3935 | // result bit width (minus 1), there is no overflow. |
| 3936 | // This means if we have enough leading zero bits in the operands |
| 3937 | // we can guarantee that the result does not overflow. |
| 3938 | // Ref: "Hacker's Delight" by Henry Warren |
| 3939 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 3940 | KnownBits LHSKnown(BitWidth); |
| 3941 | KnownBits RHSKnown(BitWidth); |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3942 | computeKnownBits(LHS, LHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr, |
| 3943 | UseInstrInfo); |
| 3944 | computeKnownBits(RHS, RHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr, |
| 3945 | UseInstrInfo); |
David Majnemer | 25e8e79 | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3946 | // Note that underestimating the number of zero bits gives a more |
| 3947 | // conservative answer. |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 3948 | unsigned ZeroBits = LHSKnown.countMinLeadingZeros() + |
| 3949 | RHSKnown.countMinLeadingZeros(); |
David Majnemer | 25e8e79 | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3950 | // First handle the easy case: if we have enough zero bits there's |
| 3951 | // definitely no overflow. |
| 3952 | if (ZeroBits >= BitWidth) |
| 3953 | return OverflowResult::NeverOverflows; |
| 3954 | |
| 3955 | // Get the largest possible values for each operand. |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 3956 | APInt LHSMax = ~LHSKnown.Zero; |
| 3957 | APInt RHSMax = ~RHSKnown.Zero; |
David Majnemer | 25e8e79 | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3958 | |
| 3959 | // We know the multiply operation doesn't overflow if the maximum values for |
| 3960 | // each operand will not overflow after we multiply them together. |
David Majnemer | 5e9c621 | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3961 | bool MaxOverflow; |
Craig Topper | 3bebc1e | 2017-04-19 21:09:45 +0000 | [diff] [blame] | 3962 | (void)LHSMax.umul_ov(RHSMax, MaxOverflow); |
David Majnemer | 5e9c621 | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3963 | if (!MaxOverflow) |
| 3964 | return OverflowResult::NeverOverflows; |
David Majnemer | 25e8e79 | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3965 | |
David Majnemer | 5e9c621 | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3966 | // We know it always overflows if multiplying the smallest possible values for |
| 3967 | // the operands also results in overflow. |
| 3968 | bool MinOverflow; |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 3969 | (void)LHSKnown.One.umul_ov(RHSKnown.One, MinOverflow); |
David Majnemer | 5e9c621 | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3970 | if (MinOverflow) |
| 3971 | return OverflowResult::AlwaysOverflows; |
| 3972 | |
| 3973 | return OverflowResult::MayOverflow; |
David Majnemer | 25e8e79 | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3974 | } |
David Majnemer | 72a9513 | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3975 | |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3976 | OverflowResult |
| 3977 | llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS, |
| 3978 | const DataLayout &DL, AssumptionCache *AC, |
| 3979 | const Instruction *CxtI, |
| 3980 | const DominatorTree *DT, bool UseInstrInfo) { |
Omer Paparo Bivas | 55a6636 | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 3981 | // Multiplying n * m significant bits yields a result of n + m significant |
| 3982 | // bits. If the total number of significant bits does not exceed the |
| 3983 | // result bit width (minus 1), there is no overflow. |
| 3984 | // This means if we have enough leading sign bits in the operands |
| 3985 | // we can guarantee that the result does not overflow. |
| 3986 | // Ref: "Hacker's Delight" by Henry Warren |
| 3987 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
| 3988 | |
| 3989 | // Note that underestimating the number of sign bits gives a more |
| 3990 | // conservative answer. |
| 3991 | unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) + |
| 3992 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT); |
| 3993 | |
| 3994 | // First handle the easy case: if we have enough sign bits there's |
| 3995 | // definitely no overflow. |
| 3996 | if (SignBits > BitWidth + 1) |
| 3997 | return OverflowResult::NeverOverflows; |
| 3998 | |
| 3999 | // There are two ambiguous cases where there can be no overflow: |
| 4000 | // SignBits == BitWidth + 1 and |
| 4001 | // SignBits == BitWidth |
| 4002 | // The second case is difficult to check, therefore we only handle the |
| 4003 | // first case. |
| 4004 | if (SignBits == BitWidth + 1) { |
| 4005 | // It overflows only when both arguments are negative and the true |
| 4006 | // product is exactly the minimum negative number. |
| 4007 | // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 |
| 4008 | // For simplicity we just check if at least one side is not negative. |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 4009 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 4010 | nullptr, UseInstrInfo); |
| 4011 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 4012 | nullptr, UseInstrInfo); |
Omer Paparo Bivas | 55a6636 | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 4013 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) |
| 4014 | return OverflowResult::NeverOverflows; |
| 4015 | } |
| 4016 | return OverflowResult::MayOverflow; |
| 4017 | } |
| 4018 | |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 4019 | OverflowResult llvm::computeOverflowForUnsignedAdd( |
| 4020 | const Value *LHS, const Value *RHS, const DataLayout &DL, |
| 4021 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 4022 | bool UseInstrInfo) { |
| 4023 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 4024 | nullptr, UseInstrInfo); |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4025 | if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) { |
Florian Hahn | 1bf7951 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 4026 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 4027 | nullptr, UseInstrInfo); |
David Majnemer | 72a9513 | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 4028 | |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4029 | if (LHSKnown.isNegative() && RHSKnown.isNegative()) { |
David Majnemer | 72a9513 | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 4030 | // The sign bit is set in both cases: this MUST overflow. |
David Majnemer | 72a9513 | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 4031 | return OverflowResult::AlwaysOverflows; |
| 4032 | } |
| 4033 | |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4034 | if (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()) { |
David Majnemer | 72a9513 | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 4035 | // The sign bit is clear in both cases: this CANNOT overflow. |
David Majnemer | 72a9513 | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 4036 | return OverflowResult::NeverOverflows; |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | return OverflowResult::MayOverflow; |
| 4041 | } |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4042 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4043 | /// Return true if we can prove that adding the two values of the |
Craig Topper | b2bc907 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4044 | /// knownbits will not overflow. |
| 4045 | /// Otherwise return false. |
| 4046 | static bool checkRippleForSignedAdd(const KnownBits &LHSKnown, |
| 4047 | const KnownBits &RHSKnown) { |
| 4048 | // Addition of two 2's complement numbers having opposite signs will never |
| 4049 | // overflow. |
| 4050 | if ((LHSKnown.isNegative() && RHSKnown.isNonNegative()) || |
| 4051 | (LHSKnown.isNonNegative() && RHSKnown.isNegative())) |
| 4052 | return true; |
| 4053 | |
| 4054 | // If either of the values is known to be non-negative, adding them can only |
| 4055 | // overflow if the second is also non-negative, so we can assume that. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4056 | // Two non-negative numbers will only overflow if there is a carry to the |
Craig Topper | b2bc907 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4057 | // sign bit, so we can check if even when the values are as big as possible |
| 4058 | // there is no overflow to the sign bit. |
| 4059 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) { |
| 4060 | APInt MaxLHS = ~LHSKnown.Zero; |
| 4061 | MaxLHS.clearSignBit(); |
| 4062 | APInt MaxRHS = ~RHSKnown.Zero; |
| 4063 | MaxRHS.clearSignBit(); |
| 4064 | APInt Result = std::move(MaxLHS) + std::move(MaxRHS); |
| 4065 | return Result.isSignBitClear(); |
| 4066 | } |
| 4067 | |
| 4068 | // If either of the values is known to be negative, adding them can only |
| 4069 | // overflow if the second is also negative, so we can assume that. |
| 4070 | // Two negative number will only overflow if there is no carry to the sign |
| 4071 | // bit, so we can check if even when the values are as small as possible |
| 4072 | // there is overflow to the sign bit. |
| 4073 | if (LHSKnown.isNegative() || RHSKnown.isNegative()) { |
| 4074 | APInt MinLHS = LHSKnown.One; |
| 4075 | MinLHS.clearSignBit(); |
| 4076 | APInt MinRHS = RHSKnown.One; |
| 4077 | MinRHS.clearSignBit(); |
| 4078 | APInt Result = std::move(MinLHS) + std::move(MinRHS); |
| 4079 | return Result.isSignBitSet(); |
| 4080 | } |
| 4081 | |
| 4082 | // If we reached here it means that we know nothing about the sign bits. |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4083 | // In this case we can't know if there will be an overflow, since by |
Craig Topper | b2bc907 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4084 | // changing the sign bits any two values can be made to overflow. |
| 4085 | return false; |
| 4086 | } |
| 4087 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4088 | static OverflowResult computeOverflowForSignedAdd(const Value *LHS, |
| 4089 | const Value *RHS, |
| 4090 | const AddOperator *Add, |
| 4091 | const DataLayout &DL, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4092 | AssumptionCache *AC, |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4093 | const Instruction *CxtI, |
| 4094 | const DominatorTree *DT) { |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4095 | if (Add && Add->hasNoSignedWrap()) { |
| 4096 | return OverflowResult::NeverOverflows; |
| 4097 | } |
| 4098 | |
Craig Topper | b2bc907 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4099 | // If LHS and RHS each have at least two sign bits, the addition will look |
| 4100 | // like |
| 4101 | // |
| 4102 | // XX..... + |
| 4103 | // YY..... |
| 4104 | // |
| 4105 | // If the carry into the most significant position is 0, X and Y can't both |
| 4106 | // be 1 and therefore the carry out of the addition is also 0. |
| 4107 | // |
| 4108 | // If the carry into the most significant position is 1, X and Y can't both |
| 4109 | // be 0 and therefore the carry out of the addition is also 1. |
| 4110 | // |
| 4111 | // Since the carry into the most significant position is always equal to |
| 4112 | // the carry out of the addition, there is no signed overflow. |
| 4113 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
| 4114 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
| 4115 | return OverflowResult::NeverOverflows; |
| 4116 | |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4117 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4118 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT); |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4119 | |
Craig Topper | b2bc907 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4120 | if (checkRippleForSignedAdd(LHSKnown, RHSKnown)) |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4121 | return OverflowResult::NeverOverflows; |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4122 | |
| 4123 | // The remaining code needs Add to be available. Early returns if not so. |
| 4124 | if (!Add) |
| 4125 | return OverflowResult::MayOverflow; |
| 4126 | |
| 4127 | // If the sign of Add is the same as at least one of the operands, this add |
| 4128 | // CANNOT overflow. This is particularly useful when the sum is |
| 4129 | // @llvm.assume'ed non-negative rather than proved so from analyzing its |
| 4130 | // operands. |
| 4131 | bool LHSOrRHSKnownNonNegative = |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4132 | (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()); |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4133 | bool LHSOrRHSKnownNegative = |
Craig Topper | b2bc907 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4134 | (LHSKnown.isNegative() || RHSKnown.isNegative()); |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4135 | if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) { |
Craig Topper | 3f38291 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4136 | KnownBits AddKnown = computeKnownBits(Add, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4137 | if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) || |
| 4138 | (AddKnown.isNegative() && LHSOrRHSKnownNegative)) { |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4139 | return OverflowResult::NeverOverflows; |
| 4140 | } |
| 4141 | } |
| 4142 | |
| 4143 | return OverflowResult::MayOverflow; |
| 4144 | } |
| 4145 | |
Omer Paparo Bivas | 55a6636 | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 4146 | OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS, |
| 4147 | const Value *RHS, |
| 4148 | const DataLayout &DL, |
| 4149 | AssumptionCache *AC, |
| 4150 | const Instruction *CxtI, |
| 4151 | const DominatorTree *DT) { |
Omer Paparo Bivas | 55a6636 | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 4152 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT); |
Nikita Popov | 72d3673 | 2018-11-28 16:37:04 +0000 | [diff] [blame] | 4153 | if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) { |
| 4154 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4155 | |
| 4156 | // If the LHS is negative and the RHS is non-negative, no unsigned wrap. |
| 4157 | if (LHSKnown.isNegative() && RHSKnown.isNonNegative()) |
| 4158 | return OverflowResult::NeverOverflows; |
| 4159 | |
| 4160 | // If the LHS is non-negative and the RHS negative, we always wrap. |
| 4161 | if (LHSKnown.isNonNegative() && RHSKnown.isNegative()) |
| 4162 | return OverflowResult::AlwaysOverflows; |
| 4163 | } |
Omer Paparo Bivas | 55a6636 | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 4164 | |
| 4165 | return OverflowResult::MayOverflow; |
| 4166 | } |
| 4167 | |
| 4168 | OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS, |
| 4169 | const Value *RHS, |
| 4170 | const DataLayout &DL, |
| 4171 | AssumptionCache *AC, |
| 4172 | const Instruction *CxtI, |
| 4173 | const DominatorTree *DT) { |
| 4174 | // If LHS and RHS each have at least two sign bits, the subtraction |
| 4175 | // cannot overflow. |
| 4176 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
| 4177 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
| 4178 | return OverflowResult::NeverOverflows; |
| 4179 | |
| 4180 | KnownBits LHSKnown = computeKnownBits(LHS, DL, 0, AC, CxtI, DT); |
| 4181 | |
| 4182 | KnownBits RHSKnown = computeKnownBits(RHS, DL, 0, AC, CxtI, DT); |
| 4183 | |
| 4184 | // Subtraction of two 2's complement numbers having identical signs will |
| 4185 | // never overflow. |
| 4186 | if ((LHSKnown.isNegative() && RHSKnown.isNegative()) || |
| 4187 | (LHSKnown.isNonNegative() && RHSKnown.isNonNegative())) |
| 4188 | return OverflowResult::NeverOverflows; |
| 4189 | |
| 4190 | // TODO: implement logic similar to checkRippleForAdd |
| 4191 | return OverflowResult::MayOverflow; |
| 4192 | } |
| 4193 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4194 | bool llvm::isOverflowIntrinsicNoWrap(const IntrinsicInst *II, |
| 4195 | const DominatorTree &DT) { |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4196 | #ifndef NDEBUG |
| 4197 | auto IID = II->getIntrinsicID(); |
| 4198 | assert((IID == Intrinsic::sadd_with_overflow || |
| 4199 | IID == Intrinsic::uadd_with_overflow || |
| 4200 | IID == Intrinsic::ssub_with_overflow || |
| 4201 | IID == Intrinsic::usub_with_overflow || |
| 4202 | IID == Intrinsic::smul_with_overflow || |
| 4203 | IID == Intrinsic::umul_with_overflow) && |
| 4204 | "Not an overflow intrinsic!"); |
| 4205 | #endif |
| 4206 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4207 | SmallVector<const BranchInst *, 2> GuardingBranches; |
| 4208 | SmallVector<const ExtractValueInst *, 2> Results; |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4209 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4210 | for (const User *U : II->users()) { |
| 4211 | if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) { |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4212 | assert(EVI->getNumIndices() == 1 && "Obvious from CI's type"); |
| 4213 | |
| 4214 | if (EVI->getIndices()[0] == 0) |
| 4215 | Results.push_back(EVI); |
| 4216 | else { |
| 4217 | assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type"); |
| 4218 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4219 | for (const auto *U : EVI->users()) |
| 4220 | if (const auto *B = dyn_cast<BranchInst>(U)) { |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4221 | assert(B->isConditional() && "How else is it using an i1?"); |
| 4222 | GuardingBranches.push_back(B); |
| 4223 | } |
| 4224 | } |
| 4225 | } else { |
| 4226 | // We are using the aggregate directly in a way we don't want to analyze |
| 4227 | // here (storing it to a global, say). |
| 4228 | return false; |
| 4229 | } |
| 4230 | } |
| 4231 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4232 | auto AllUsesGuardedByBranch = [&](const BranchInst *BI) { |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4233 | BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1)); |
| 4234 | if (!NoWrapEdge.isSingleEdge()) |
| 4235 | return false; |
| 4236 | |
| 4237 | // Check if all users of the add are provably no-wrap. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4238 | for (const auto *Result : Results) { |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4239 | // If the extractvalue itself is not executed on overflow, the we don't |
| 4240 | // need to check each use separately, since domination is transitive. |
| 4241 | if (DT.dominates(NoWrapEdge, Result->getParent())) |
| 4242 | continue; |
| 4243 | |
| 4244 | for (auto &RU : Result->uses()) |
| 4245 | if (!DT.dominates(NoWrapEdge, RU)) |
| 4246 | return false; |
| 4247 | } |
| 4248 | |
| 4249 | return true; |
| 4250 | }; |
| 4251 | |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4252 | return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); |
Sanjoy Das | 09cfc1e | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4256 | OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add, |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4257 | const DataLayout &DL, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4258 | AssumptionCache *AC, |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4259 | const Instruction *CxtI, |
| 4260 | const DominatorTree *DT) { |
| 4261 | return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1), |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4262 | Add, DL, AC, CxtI, DT); |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4265 | OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS, |
| 4266 | const Value *RHS, |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4267 | const DataLayout &DL, |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4268 | AssumptionCache *AC, |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4269 | const Instruction *CxtI, |
| 4270 | const DominatorTree *DT) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4271 | return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT); |
Jingyue Wu | df4b7e6 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4274 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { |
Eli Friedman | df383ca | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4275 | // A memory operation returns normally if it isn't volatile. A volatile |
| 4276 | // operation is allowed to trap. |
| 4277 | // |
| 4278 | // An atomic operation isn't guaranteed to return in a reasonable amount of |
| 4279 | // time because it's possible for another thread to interfere with it for an |
| 4280 | // arbitrary length of time, but programs aren't allowed to rely on that. |
| 4281 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 4282 | return !LI->isVolatile(); |
| 4283 | if (const StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 4284 | return !SI->isVolatile(); |
| 4285 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I)) |
| 4286 | return !CXI->isVolatile(); |
| 4287 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) |
| 4288 | return !RMWI->isVolatile(); |
| 4289 | if (const MemIntrinsic *MII = dyn_cast<MemIntrinsic>(I)) |
| 4290 | return !MII->isVolatile(); |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4291 | |
Eli Friedman | df383ca | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4292 | // If there is no successor, then execution can't transfer to it. |
| 4293 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) |
| 4294 | return !CRI->unwindsToCaller(); |
| 4295 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I)) |
| 4296 | return !CatchSwitch->unwindsToCaller(); |
| 4297 | if (isa<ResumeInst>(I)) |
| 4298 | return false; |
| 4299 | if (isa<ReturnInst>(I)) |
| 4300 | return false; |
Sebastian Pop | 222cc00 | 2017-03-08 01:54:50 +0000 | [diff] [blame] | 4301 | if (isa<UnreachableInst>(I)) |
| 4302 | return false; |
Sanjoy Das | 39ede08 | 2016-06-08 17:48:36 +0000 | [diff] [blame] | 4303 | |
Eli Friedman | df383ca | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4304 | // Calls can throw, or contain an infinite loop, or kill the process. |
Sanjoy Das | 7fa928a | 2016-12-31 22:12:31 +0000 | [diff] [blame] | 4305 | if (auto CS = ImmutableCallSite(I)) { |
Sanjoy Das | f67baf0 | 2016-12-31 22:12:34 +0000 | [diff] [blame] | 4306 | // Call sites that throw have implicit non-local control flow. |
| 4307 | if (!CS.doesNotThrow()) |
| 4308 | return false; |
| 4309 | |
| 4310 | // Non-throwing call sites can loop infinitely, call exit/pthread_exit |
| 4311 | // etc. and thus not return. However, LLVM already assumes that |
| 4312 | // |
| 4313 | // - Thread exiting actions are modeled as writes to memory invisible to |
| 4314 | // the program. |
| 4315 | // |
| 4316 | // - Loops that don't have side effects (side effects are volatile/atomic |
| 4317 | // stores and IO) always terminate (see http://llvm.org/PR965). |
| 4318 | // Furthermore IO itself is also modeled as writes to memory invisible to |
| 4319 | // the program. |
| 4320 | // |
| 4321 | // We rely on those assumptions here, and use the memory effects of the call |
| 4322 | // target as a proxy for checking that it always returns. |
| 4323 | |
| 4324 | // FIXME: This isn't aggressive enough; a call which only writes to a global |
| 4325 | // is guaranteed to return. |
Sanjoy Das | 84225d2 | 2016-06-14 20:23:16 +0000 | [diff] [blame] | 4326 | return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory() || |
Dan Gohman | b5e0bec | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 4327 | match(I, m_Intrinsic<Intrinsic::assume>()) || |
| 4328 | match(I, m_Intrinsic<Intrinsic::sideeffect>()); |
Eli Friedman | df383ca | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | // Other instructions return normally. |
| 4332 | return true; |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Philip Reames | 2082160 | 2018-03-08 21:25:30 +0000 | [diff] [blame] | 4335 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) { |
| 4336 | // TODO: This is slightly consdervative for invoke instruction since exiting |
| 4337 | // via an exception *is* normal control for them. |
| 4338 | for (auto I = BB->begin(), E = BB->end(); I != E; ++I) |
| 4339 | if (!isGuaranteedToTransferExecutionToSuccessor(&*I)) |
| 4340 | return false; |
| 4341 | return true; |
| 4342 | } |
| 4343 | |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4344 | bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, |
| 4345 | const Loop *L) { |
| 4346 | // The loop header is guaranteed to be executed for every iteration. |
| 4347 | // |
| 4348 | // FIXME: Relax this constraint to cover all basic blocks that are |
| 4349 | // guaranteed to be executed at every iteration. |
| 4350 | if (I->getParent() != L->getHeader()) return false; |
| 4351 | |
| 4352 | for (const Instruction &LI : *L->getHeader()) { |
| 4353 | if (&LI == I) return true; |
| 4354 | if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; |
| 4355 | } |
| 4356 | llvm_unreachable("Instruction not contained in its own parent basic block."); |
| 4357 | } |
| 4358 | |
| 4359 | bool llvm::propagatesFullPoison(const Instruction *I) { |
| 4360 | switch (I->getOpcode()) { |
Sanjoy Das | 475a9ff | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4361 | case Instruction::Add: |
| 4362 | case Instruction::Sub: |
| 4363 | case Instruction::Xor: |
| 4364 | case Instruction::Trunc: |
| 4365 | case Instruction::BitCast: |
| 4366 | case Instruction::AddrSpaceCast: |
Sanjoy Das | 7e76879 | 2017-02-22 06:52:32 +0000 | [diff] [blame] | 4367 | case Instruction::Mul: |
| 4368 | case Instruction::Shl: |
| 4369 | case Instruction::GetElementPtr: |
Sanjoy Das | 475a9ff | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4370 | // These operations all propagate poison unconditionally. Note that poison |
| 4371 | // is not any particular value, so xor or subtraction of poison with |
| 4372 | // itself still yields poison, not zero. |
| 4373 | return true; |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4374 | |
Sanjoy Das | 475a9ff | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4375 | case Instruction::AShr: |
| 4376 | case Instruction::SExt: |
| 4377 | // For these operations, one bit of the input is replicated across |
| 4378 | // multiple output bits. A replicated poison bit is still poison. |
| 4379 | return true; |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4380 | |
Sanjoy Das | 475a9ff | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4381 | case Instruction::ICmp: |
| 4382 | // Comparing poison with any value yields poison. This is why, for |
| 4383 | // instance, x s< (x +nsw 1) can be folded to true. |
| 4384 | return true; |
Sanjoy Das | 2f6c3f7 | 2016-05-29 00:31:18 +0000 | [diff] [blame] | 4385 | |
Sanjoy Das | 475a9ff | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4386 | default: |
| 4387 | return false; |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) { |
| 4392 | switch (I->getOpcode()) { |
| 4393 | case Instruction::Store: |
| 4394 | return cast<StoreInst>(I)->getPointerOperand(); |
| 4395 | |
| 4396 | case Instruction::Load: |
| 4397 | return cast<LoadInst>(I)->getPointerOperand(); |
| 4398 | |
| 4399 | case Instruction::AtomicCmpXchg: |
| 4400 | return cast<AtomicCmpXchgInst>(I)->getPointerOperand(); |
| 4401 | |
| 4402 | case Instruction::AtomicRMW: |
| 4403 | return cast<AtomicRMWInst>(I)->getPointerOperand(); |
| 4404 | |
| 4405 | case Instruction::UDiv: |
| 4406 | case Instruction::SDiv: |
| 4407 | case Instruction::URem: |
| 4408 | case Instruction::SRem: |
| 4409 | return I->getOperand(1); |
| 4410 | |
| 4411 | default: |
| 4412 | return nullptr; |
| 4413 | } |
| 4414 | } |
| 4415 | |
Sanjoy Das | 21ade9b | 2017-04-30 19:41:19 +0000 | [diff] [blame] | 4416 | bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) { |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4417 | // We currently only look for uses of poison values within the same basic |
| 4418 | // block, as that makes it easier to guarantee that the uses will be |
| 4419 | // executed given that PoisonI is executed. |
| 4420 | // |
| 4421 | // FIXME: Expand this to consider uses beyond the same basic block. To do |
| 4422 | // this, look out for the distinction between post-dominance and strong |
| 4423 | // post-dominance. |
| 4424 | const BasicBlock *BB = PoisonI->getParent(); |
| 4425 | |
| 4426 | // Set of instructions that we have proved will yield poison if PoisonI |
| 4427 | // does. |
| 4428 | SmallSet<const Value *, 16> YieldsPoison; |
Sanjoy Das | c8ce43d | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4429 | SmallSet<const BasicBlock *, 4> Visited; |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4430 | YieldsPoison.insert(PoisonI); |
Sanjoy Das | c8ce43d | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4431 | Visited.insert(PoisonI->getParent()); |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4432 | |
Sanjoy Das | c8ce43d | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4433 | BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end(); |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4434 | |
Sanjoy Das | c8ce43d | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4435 | unsigned Iter = 0; |
| 4436 | while (Iter++ < MaxDepth) { |
| 4437 | for (auto &I : make_range(Begin, End)) { |
| 4438 | if (&I != PoisonI) { |
| 4439 | const Value *NotPoison = getGuaranteedNonFullPoisonOp(&I); |
| 4440 | if (NotPoison != nullptr && YieldsPoison.count(NotPoison)) |
| 4441 | return true; |
| 4442 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 4443 | return false; |
| 4444 | } |
| 4445 | |
| 4446 | // Mark poison that propagates from I through uses of I. |
| 4447 | if (YieldsPoison.count(&I)) { |
| 4448 | for (const User *User : I.users()) { |
| 4449 | const Instruction *UserI = cast<Instruction>(User); |
| 4450 | if (propagatesFullPoison(UserI)) |
| 4451 | YieldsPoison.insert(User); |
| 4452 | } |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4453 | } |
| 4454 | } |
Sanjoy Das | c8ce43d | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4455 | |
| 4456 | if (auto *NextBB = BB->getSingleSuccessor()) { |
| 4457 | if (Visited.insert(NextBB).second) { |
| 4458 | BB = NextBB; |
| 4459 | Begin = BB->getFirstNonPHI()->getIterator(); |
| 4460 | End = BB->end(); |
| 4461 | continue; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | break; |
Eugene Zelenko | cecd8f1 | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4466 | } |
Jingyue Wu | 7d4d116 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4467 | return false; |
| 4468 | } |
| 4469 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4470 | static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) { |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4471 | if (FMF.noNaNs()) |
| 4472 | return true; |
| 4473 | |
| 4474 | if (auto *C = dyn_cast<ConstantFP>(V)) |
| 4475 | return !C->isNaN(); |
Thomas Lively | 30635bc | 2018-09-28 21:36:43 +0000 | [diff] [blame] | 4476 | |
| 4477 | if (auto *C = dyn_cast<ConstantDataVector>(V)) { |
| 4478 | if (!C->getElementType()->isFloatingPointTy()) |
| 4479 | return false; |
| 4480 | for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { |
| 4481 | if (C->getElementAsAPFloat(I).isNaN()) |
| 4482 | return false; |
| 4483 | } |
| 4484 | return true; |
| 4485 | } |
| 4486 | |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4487 | return false; |
| 4488 | } |
| 4489 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4490 | static bool isKnownNonZero(const Value *V) { |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4491 | if (auto *C = dyn_cast<ConstantFP>(V)) |
| 4492 | return !C->isZero(); |
Thomas Lively | 30635bc | 2018-09-28 21:36:43 +0000 | [diff] [blame] | 4493 | |
| 4494 | if (auto *C = dyn_cast<ConstantDataVector>(V)) { |
| 4495 | if (!C->getElementType()->isFloatingPointTy()) |
| 4496 | return false; |
| 4497 | for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) { |
| 4498 | if (C->getElementAsAPFloat(I).isZero()) |
| 4499 | return false; |
| 4500 | } |
| 4501 | return true; |
| 4502 | } |
| 4503 | |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4504 | return false; |
| 4505 | } |
| 4506 | |
Nikolai Bozhenov | 406ae06 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 4507 | /// Match clamp pattern for float types without care about NaNs or signed zeros. |
| 4508 | /// Given non-min/max outer cmp/select from the clamp pattern this |
| 4509 | /// function recognizes if it can be substitued by a "canonical" min/max |
| 4510 | /// pattern. |
| 4511 | static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, |
| 4512 | Value *CmpLHS, Value *CmpRHS, |
| 4513 | Value *TrueVal, Value *FalseVal, |
| 4514 | Value *&LHS, Value *&RHS) { |
| 4515 | // Try to match |
| 4516 | // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) |
| 4517 | // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) |
| 4518 | // and return description of the outer Max/Min. |
| 4519 | |
| 4520 | // First, check if select has inverse order: |
| 4521 | if (CmpRHS == FalseVal) { |
| 4522 | std::swap(TrueVal, FalseVal); |
| 4523 | Pred = CmpInst::getInversePredicate(Pred); |
| 4524 | } |
| 4525 | |
| 4526 | // Assume success now. If there's no match, callers should not use these anyway. |
| 4527 | LHS = TrueVal; |
| 4528 | RHS = FalseVal; |
| 4529 | |
| 4530 | const APFloat *FC1; |
| 4531 | if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite()) |
| 4532 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4533 | |
| 4534 | const APFloat *FC2; |
| 4535 | switch (Pred) { |
| 4536 | case CmpInst::FCMP_OLT: |
| 4537 | case CmpInst::FCMP_OLE: |
| 4538 | case CmpInst::FCMP_ULT: |
| 4539 | case CmpInst::FCMP_ULE: |
| 4540 | if (match(FalseVal, |
| 4541 | m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)), |
| 4542 | m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
| 4543 | FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan) |
| 4544 | return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false}; |
| 4545 | break; |
| 4546 | case CmpInst::FCMP_OGT: |
| 4547 | case CmpInst::FCMP_OGE: |
| 4548 | case CmpInst::FCMP_UGT: |
| 4549 | case CmpInst::FCMP_UGE: |
| 4550 | if (match(FalseVal, |
| 4551 | m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)), |
| 4552 | m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
| 4553 | FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan) |
| 4554 | return {SPF_FMINNUM, SPNB_RETURNS_ANY, false}; |
| 4555 | break; |
| 4556 | default: |
| 4557 | break; |
| 4558 | } |
| 4559 | |
| 4560 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4561 | } |
| 4562 | |
Artur Gainullin | 359c2dc | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4563 | /// Recognize variations of: |
| 4564 | /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) |
| 4565 | static SelectPatternResult matchClamp(CmpInst::Predicate Pred, |
| 4566 | Value *CmpLHS, Value *CmpRHS, |
| 4567 | Value *TrueVal, Value *FalseVal) { |
| 4568 | // Swap the select operands and predicate to match the patterns below. |
| 4569 | if (CmpRHS != TrueVal) { |
| 4570 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4571 | std::swap(TrueVal, FalseVal); |
| 4572 | } |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4573 | const APInt *C1; |
| 4574 | if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) { |
| 4575 | const APInt *C2; |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4576 | // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) |
| 4577 | if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4578 | C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4579 | return {SPF_SMAX, SPNB_NA, false}; |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4580 | |
| 4581 | // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) |
| 4582 | if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4583 | C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4584 | return {SPF_SMIN, SPNB_NA, false}; |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4585 | |
| 4586 | // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) |
| 4587 | if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4588 | C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4589 | return {SPF_UMAX, SPNB_NA, false}; |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4590 | |
| 4591 | // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) |
| 4592 | if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4593 | C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4594 | return {SPF_UMIN, SPNB_NA, false}; |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4595 | } |
Artur Gainullin | 359c2dc | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4596 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4597 | } |
| 4598 | |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4599 | /// Recognize variations of: |
| 4600 | /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) |
| 4601 | static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, |
| 4602 | Value *CmpLHS, Value *CmpRHS, |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4603 | Value *TVal, Value *FVal, |
| 4604 | unsigned Depth) { |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4605 | // TODO: Allow FP min/max with nnan/nsz. |
| 4606 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison"); |
| 4607 | |
| 4608 | Value *A, *B; |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4609 | SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1); |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4610 | if (!SelectPatternResult::isMinOrMax(L.Flavor)) |
| 4611 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4612 | |
| 4613 | Value *C, *D; |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4614 | SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1); |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4615 | if (L.Flavor != R.Flavor) |
| 4616 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4617 | |
Sanjay Patel | d080705 | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4618 | // We have something like: x Pred y ? min(a, b) : min(c, d). |
| 4619 | // Try to match the compare to the min/max operations of the select operands. |
| 4620 | // First, make sure we have the right compare predicate. |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4621 | switch (L.Flavor) { |
| 4622 | case SPF_SMIN: |
| 4623 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) { |
| 4624 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4625 | std::swap(CmpLHS, CmpRHS); |
| 4626 | } |
| 4627 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
| 4628 | break; |
| 4629 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4630 | case SPF_SMAX: |
| 4631 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) { |
| 4632 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4633 | std::swap(CmpLHS, CmpRHS); |
| 4634 | } |
| 4635 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) |
| 4636 | break; |
| 4637 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4638 | case SPF_UMIN: |
| 4639 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) { |
| 4640 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4641 | std::swap(CmpLHS, CmpRHS); |
| 4642 | } |
| 4643 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) |
| 4644 | break; |
| 4645 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4646 | case SPF_UMAX: |
| 4647 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
| 4648 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4649 | std::swap(CmpLHS, CmpRHS); |
| 4650 | } |
| 4651 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
| 4652 | break; |
| 4653 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4654 | default: |
Sanjay Patel | a8b0169 | 2018-01-08 18:31:13 +0000 | [diff] [blame] | 4655 | return {SPF_UNKNOWN, SPNB_NA, false}; |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4656 | } |
| 4657 | |
Sanjay Patel | d080705 | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4658 | // If there is a common operand in the already matched min/max and the other |
| 4659 | // min/max operands match the compare operands (either directly or inverted), |
| 4660 | // then this is min/max of the same flavor. |
| 4661 | |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4662 | // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
Sanjay Patel | d080705 | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4663 | // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
| 4664 | if (D == B) { |
| 4665 | if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
| 4666 | match(A, m_Not(m_Specific(CmpRHS))))) |
| 4667 | return {L.Flavor, SPNB_NA, false}; |
| 4668 | } |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4669 | // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
Sanjay Patel | d080705 | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4670 | // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
| 4671 | if (C == B) { |
| 4672 | if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
| 4673 | match(A, m_Not(m_Specific(CmpRHS))))) |
| 4674 | return {L.Flavor, SPNB_NA, false}; |
| 4675 | } |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4676 | // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
Sanjay Patel | d080705 | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4677 | // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
| 4678 | if (D == A) { |
| 4679 | if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
| 4680 | match(B, m_Not(m_Specific(CmpRHS))))) |
| 4681 | return {L.Flavor, SPNB_NA, false}; |
| 4682 | } |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4683 | // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
Sanjay Patel | d080705 | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4684 | // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
| 4685 | if (C == A) { |
| 4686 | if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
| 4687 | match(B, m_Not(m_Specific(CmpRHS))))) |
| 4688 | return {L.Flavor, SPNB_NA, false}; |
| 4689 | } |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4690 | |
| 4691 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4692 | } |
| 4693 | |
Artur Gainullin | 359c2dc | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4694 | /// Match non-obvious integer minimum and maximum sequences. |
| 4695 | static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, |
| 4696 | Value *CmpLHS, Value *CmpRHS, |
| 4697 | Value *TrueVal, Value *FalseVal, |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4698 | Value *&LHS, Value *&RHS, |
| 4699 | unsigned Depth) { |
Artur Gainullin | 359c2dc | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4700 | // Assume success. If there's no match, callers should not use these anyway. |
| 4701 | LHS = TrueVal; |
| 4702 | RHS = FalseVal; |
| 4703 | |
| 4704 | SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal); |
| 4705 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
| 4706 | return SPR; |
Sanjay Patel | 92a30c5 | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4707 | |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4708 | SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth); |
Sanjay Patel | db850cf | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4709 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
| 4710 | return SPR; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4711 | |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4712 | if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT) |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4713 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4714 | |
Sanjay Patel | 38feb36 | 2016-11-13 20:04:52 +0000 | [diff] [blame] | 4715 | // Z = X -nsw Y |
| 4716 | // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0) |
| 4717 | // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0) |
| 4718 | if (match(TrueVal, m_Zero()) && |
Sanjay Patel | 2480670 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4719 | match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4720 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; |
Sanjay Patel | 38feb36 | 2016-11-13 20:04:52 +0000 | [diff] [blame] | 4721 | |
| 4722 | // Z = X -nsw Y |
| 4723 | // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0) |
| 4724 | // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0) |
| 4725 | if (match(FalseVal, m_Zero()) && |
Sanjay Patel | 2480670 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4726 | match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4727 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; |
Sanjay Patel | 38feb36 | 2016-11-13 20:04:52 +0000 | [diff] [blame] | 4728 | |
Artur Gainullin | 359c2dc | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4729 | const APInt *C1; |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4730 | if (!match(CmpRHS, m_APInt(C1))) |
| 4731 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4732 | |
| 4733 | // An unsigned min/max can be written with a signed compare. |
| 4734 | const APInt *C2; |
| 4735 | if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) || |
| 4736 | (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) { |
| 4737 | // Is the sign bit set? |
| 4738 | // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX |
| 4739 | // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN |
Craig Topper | ca3cf87 | 2017-11-08 19:38:45 +0000 | [diff] [blame] | 4740 | if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() && |
| 4741 | C2->isMaxSignedValue()) |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4742 | return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4743 | |
| 4744 | // Is the sign bit clear? |
| 4745 | // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX |
| 4746 | // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4747 | if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() && |
| 4748 | C2->isMinSignedValue()) |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4749 | return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | // Look through 'not' ops to find disguised signed min/max. |
| 4753 | // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C) |
| 4754 | // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C) |
| 4755 | if (match(TrueVal, m_Not(m_Specific(CmpLHS))) && |
Sanjay Patel | 2480670 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4756 | match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2) |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4757 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4758 | |
| 4759 | // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X) |
| 4760 | // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X) |
| 4761 | if (match(FalseVal, m_Not(m_Specific(CmpLHS))) && |
Sanjay Patel | 2480670 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4762 | match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2) |
Nikolai Bozhenov | 2942c78 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4763 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; |
Sanjay Patel | 7c0b299 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4764 | |
| 4765 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4766 | } |
| 4767 | |
Chen Zheng | ebc7653 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4768 | bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { |
Chen Zheng | 7d5acd8 | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4769 | assert(X && Y && "Invalid operand"); |
| 4770 | |
Chen Zheng | ebc7653 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4771 | // X = sub (0, Y) || X = sub nsw (0, Y) |
| 4772 | if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || |
| 4773 | (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) |
Chen Zheng | 7d5acd8 | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4774 | return true; |
| 4775 | |
Chen Zheng | ebc7653 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4776 | // Y = sub (0, X) || Y = sub nsw (0, X) |
| 4777 | if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || |
| 4778 | (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) |
Chen Zheng | 7d5acd8 | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4779 | return true; |
| 4780 | |
Chen Zheng | ebc7653 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4781 | // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) |
Chen Zheng | 7d5acd8 | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4782 | Value *A, *B; |
Chen Zheng | ebc7653 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4783 | return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && |
| 4784 | match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || |
| 4785 | (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && |
| 4786 | match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); |
Chen Zheng | 7d5acd8 | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4789 | static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, |
| 4790 | FastMathFlags FMF, |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4791 | Value *CmpLHS, Value *CmpRHS, |
| 4792 | Value *TrueVal, Value *FalseVal, |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4793 | Value *&LHS, Value *&RHS, |
| 4794 | unsigned Depth) { |
Sanjay Patel | fd7c7dd | 2018-11-04 14:28:48 +0000 | [diff] [blame] | 4795 | if (CmpInst::isFPPredicate(Pred)) { |
| 4796 | // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one |
| 4797 | // 0.0 operand, set the compare's 0.0 operands to that same value for the |
| 4798 | // purpose of identifying min/max. Disregard vector constants with undefined |
| 4799 | // elements because those can not be back-propagated for analysis. |
| 4800 | Value *OutputZeroVal = nullptr; |
| 4801 | if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) && |
| 4802 | !cast<Constant>(TrueVal)->containsUndefElement()) |
| 4803 | OutputZeroVal = TrueVal; |
| 4804 | else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) && |
| 4805 | !cast<Constant>(FalseVal)->containsUndefElement()) |
| 4806 | OutputZeroVal = FalseVal; |
| 4807 | |
| 4808 | if (OutputZeroVal) { |
| 4809 | if (match(CmpLHS, m_AnyZeroFP())) |
| 4810 | CmpLHS = OutputZeroVal; |
| 4811 | if (match(CmpRHS, m_AnyZeroFP())) |
| 4812 | CmpRHS = OutputZeroVal; |
| 4813 | } |
| 4814 | } |
| 4815 | |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4816 | LHS = CmpLHS; |
| 4817 | RHS = CmpRHS; |
| 4818 | |
Sanjay Patel | ba5e1fb | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4819 | // Signed zero may return inconsistent results between implementations. |
| 4820 | // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 |
| 4821 | // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) |
| 4822 | // Therefore, we behave conservatively and only proceed if at least one of the |
| 4823 | // operands is known to not be zero or if we don't care about signed zero. |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4824 | switch (Pred) { |
| 4825 | default: break; |
Sanjay Patel | ba5e1fb | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4826 | // FIXME: Include OGT/OLT/UGT/ULT. |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4827 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: |
| 4828 | case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: |
| 4829 | if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 4830 | !isKnownNonZero(CmpRHS)) |
| 4831 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4832 | } |
| 4833 | |
| 4834 | SelectPatternNaNBehavior NaNBehavior = SPNB_NA; |
| 4835 | bool Ordered = false; |
| 4836 | |
| 4837 | // When given one NaN and one non-NaN input: |
| 4838 | // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. |
| 4839 | // - A simple C99 (a < b ? a : b) construction will return 'b' (as the |
| 4840 | // ordered comparison fails), which could be NaN or non-NaN. |
| 4841 | // so here we discover exactly what NaN behavior is required/accepted. |
| 4842 | if (CmpInst::isFPPredicate(Pred)) { |
| 4843 | bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); |
| 4844 | bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); |
| 4845 | |
| 4846 | if (LHSSafe && RHSSafe) { |
| 4847 | // Both operands are known non-NaN. |
| 4848 | NaNBehavior = SPNB_RETURNS_ANY; |
| 4849 | } else if (CmpInst::isOrdered(Pred)) { |
| 4850 | // An ordered comparison will return false when given a NaN, so it |
| 4851 | // returns the RHS. |
| 4852 | Ordered = true; |
| 4853 | if (LHSSafe) |
James Molloy | 078921e | 2015-08-12 15:11:43 +0000 | [diff] [blame] | 4854 | // LHS is non-NaN, so if RHS is NaN then NaN will be returned. |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4855 | NaNBehavior = SPNB_RETURNS_NAN; |
| 4856 | else if (RHSSafe) |
| 4857 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 4858 | else |
| 4859 | // Completely unsafe. |
| 4860 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4861 | } else { |
| 4862 | Ordered = false; |
| 4863 | // An unordered comparison will return true when given a NaN, so it |
| 4864 | // returns the LHS. |
| 4865 | if (LHSSafe) |
James Molloy | 078921e | 2015-08-12 15:11:43 +0000 | [diff] [blame] | 4866 | // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4867 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 4868 | else if (RHSSafe) |
| 4869 | NaNBehavior = SPNB_RETURNS_NAN; |
| 4870 | else |
| 4871 | // Completely unsafe. |
| 4872 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4873 | } |
| 4874 | } |
| 4875 | |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4876 | if (TrueVal == CmpRHS && FalseVal == CmpLHS) { |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4877 | std::swap(CmpLHS, CmpRHS); |
| 4878 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 4879 | if (NaNBehavior == SPNB_RETURNS_NAN) |
| 4880 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 4881 | else if (NaNBehavior == SPNB_RETURNS_OTHER) |
| 4882 | NaNBehavior = SPNB_RETURNS_NAN; |
| 4883 | Ordered = !Ordered; |
| 4884 | } |
| 4885 | |
| 4886 | // ([if]cmp X, Y) ? X : Y |
| 4887 | if (TrueVal == CmpLHS && FalseVal == CmpRHS) { |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4888 | switch (Pred) { |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4889 | default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4890 | case ICmpInst::ICMP_UGT: |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4891 | case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4892 | case ICmpInst::ICMP_SGT: |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4893 | case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4894 | case ICmpInst::ICMP_ULT: |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4895 | case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4896 | case ICmpInst::ICMP_SLT: |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4897 | case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; |
| 4898 | case FCmpInst::FCMP_UGT: |
| 4899 | case FCmpInst::FCMP_UGE: |
| 4900 | case FCmpInst::FCMP_OGT: |
| 4901 | case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; |
| 4902 | case FCmpInst::FCMP_ULT: |
| 4903 | case FCmpInst::FCMP_ULE: |
| 4904 | case FCmpInst::FCMP_OLT: |
| 4905 | case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4906 | } |
| 4907 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4908 | |
Chen Zheng | d27cef1 | 2018-07-16 02:23:00 +0000 | [diff] [blame] | 4909 | if (isKnownNegation(TrueVal, FalseVal)) { |
| 4910 | // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can |
| 4911 | // match against either LHS or sext(LHS). |
| 4912 | auto MaybeSExtCmpLHS = |
| 4913 | m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS))); |
| 4914 | auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes()); |
| 4915 | auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One()); |
| 4916 | if (match(TrueVal, MaybeSExtCmpLHS)) { |
| 4917 | // Set the return values. If the compare uses the negated value (-X >s 0), |
| 4918 | // swap the return values because the negated value is always 'RHS'. |
Sanjay Patel | cdbffdd | 2018-07-02 14:43:40 +0000 | [diff] [blame] | 4919 | LHS = TrueVal; |
| 4920 | RHS = FalseVal; |
Chen Zheng | d27cef1 | 2018-07-16 02:23:00 +0000 | [diff] [blame] | 4921 | if (match(CmpLHS, m_Neg(m_Specific(FalseVal)))) |
| 4922 | std::swap(LHS, RHS); |
| 4923 | |
| 4924 | // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) |
| 4925 | // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) |
| 4926 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
| 4927 | return {SPF_ABS, SPNB_NA, false}; |
| 4928 | |
| 4929 | // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X) |
| 4930 | // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X) |
| 4931 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
| 4932 | return {SPF_NABS, SPNB_NA, false}; |
| 4933 | } |
| 4934 | else if (match(FalseVal, MaybeSExtCmpLHS)) { |
| 4935 | // Set the return values. If the compare uses the negated value (-X >s 0), |
| 4936 | // swap the return values because the negated value is always 'RHS'. |
Sanjay Patel | cdbffdd | 2018-07-02 14:43:40 +0000 | [diff] [blame] | 4937 | LHS = FalseVal; |
| 4938 | RHS = TrueVal; |
Chen Zheng | d27cef1 | 2018-07-16 02:23:00 +0000 | [diff] [blame] | 4939 | if (match(CmpLHS, m_Neg(m_Specific(TrueVal)))) |
| 4940 | std::swap(LHS, RHS); |
| 4941 | |
| 4942 | // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) |
| 4943 | // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) |
| 4944 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
| 4945 | return {SPF_NABS, SPNB_NA, false}; |
| 4946 | |
| 4947 | // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X) |
| 4948 | // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X) |
| 4949 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
| 4950 | return {SPF_ABS, SPNB_NA, false}; |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4951 | } |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Nikolai Bozhenov | 406ae06 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 4954 | if (CmpInst::isIntPredicate(Pred)) |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4955 | return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth); |
Nikolai Bozhenov | 406ae06 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 4956 | |
| 4957 | // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar |
| 4958 | // may return either -0.0 or 0.0, so fcmp/select pair has stricter |
| 4959 | // semantics than minNum. Be conservative in such case. |
| 4960 | if (NaNBehavior != SPNB_RETURNS_ANY || |
| 4961 | (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 4962 | !isKnownNonZero(CmpRHS))) |
| 4963 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4964 | |
| 4965 | return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); |
James Molloy | 2605531 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4966 | } |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4967 | |
Nikolai Bozhenov | 42759c2 | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 4968 | /// Helps to match a select pattern in case of a type mismatch. |
| 4969 | /// |
| 4970 | /// The function processes the case when type of true and false values of a |
| 4971 | /// select instruction differs from type of the cmp instruction operands because |
Vedant Kumar | d924442 | 2018-03-02 18:57:02 +0000 | [diff] [blame] | 4972 | /// of a cast instruction. The function checks if it is legal to move the cast |
Nikolai Bozhenov | 42759c2 | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 4973 | /// operation after "select". If yes, it returns the new second value of |
| 4974 | /// "select" (with the assumption that cast is moved): |
| 4975 | /// 1. As operand of cast instruction when both values of "select" are same cast |
| 4976 | /// instructions. |
| 4977 | /// 2. As restored constant (by applying reverse cast operation) when the first |
| 4978 | /// value of the "select" is a cast operation and the second value is a |
| 4979 | /// constant. |
| 4980 | /// NOTE: We return only the new second value because the first value could be |
| 4981 | /// accessed as operand of cast instruction. |
James Molloy | 983cfca | 2015-09-02 17:25:25 +0000 | [diff] [blame] | 4982 | static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, |
| 4983 | Instruction::CastOps *CastOp) { |
Sanjay Patel | 7ce2638 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4984 | auto *Cast1 = dyn_cast<CastInst>(V1); |
| 4985 | if (!Cast1) |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4986 | return nullptr; |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4987 | |
Sanjay Patel | 7ce2638 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4988 | *CastOp = Cast1->getOpcode(); |
| 4989 | Type *SrcTy = Cast1->getSrcTy(); |
| 4990 | if (auto *Cast2 = dyn_cast<CastInst>(V2)) { |
| 4991 | // If V1 and V2 are both the same cast from the same type, look through V1. |
| 4992 | if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy()) |
| 4993 | return Cast2->getOperand(0); |
James Molloy | 983cfca | 2015-09-02 17:25:25 +0000 | [diff] [blame] | 4994 | return nullptr; |
| 4995 | } |
| 4996 | |
Sanjay Patel | 7ce2638 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4997 | auto *C = dyn_cast<Constant>(V2); |
| 4998 | if (!C) |
| 4999 | return nullptr; |
| 5000 | |
David Majnemer | 359298e | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 5001 | Constant *CastedTo = nullptr; |
Sanjay Patel | 7ce2638 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 5002 | switch (*CastOp) { |
| 5003 | case Instruction::ZExt: |
| 5004 | if (CmpI->isUnsigned()) |
| 5005 | CastedTo = ConstantExpr::getTrunc(C, SrcTy); |
| 5006 | break; |
| 5007 | case Instruction::SExt: |
| 5008 | if (CmpI->isSigned()) |
| 5009 | CastedTo = ConstantExpr::getTrunc(C, SrcTy, true); |
| 5010 | break; |
| 5011 | case Instruction::Trunc: |
Nikolai Bozhenov | 42759c2 | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 5012 | Constant *CmpConst; |
Nikolai Bozhenov | 429594f | 2017-10-18 14:24:50 +0000 | [diff] [blame] | 5013 | if (match(CmpI->getOperand(1), m_Constant(CmpConst)) && |
| 5014 | CmpConst->getType() == SrcTy) { |
Nikolai Bozhenov | 42759c2 | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 5015 | // Here we have the following case: |
| 5016 | // |
| 5017 | // %cond = cmp iN %x, CmpConst |
| 5018 | // %tr = trunc iN %x to iK |
| 5019 | // %narrowsel = select i1 %cond, iK %t, iK C |
| 5020 | // |
| 5021 | // We can always move trunc after select operation: |
| 5022 | // |
| 5023 | // %cond = cmp iN %x, CmpConst |
| 5024 | // %widesel = select i1 %cond, iN %x, iN CmpConst |
| 5025 | // %tr = trunc iN %widesel to iK |
| 5026 | // |
| 5027 | // Note that C could be extended in any way because we don't care about |
| 5028 | // upper bits after truncation. It can't be abs pattern, because it would |
| 5029 | // look like: |
| 5030 | // |
| 5031 | // select i1 %cond, x, -x. |
| 5032 | // |
| 5033 | // So only min/max pattern could be matched. Such match requires widened C |
| 5034 | // == CmpConst. That is why set widened C = CmpConst, condition trunc |
| 5035 | // CmpConst == C is checked below. |
| 5036 | CastedTo = CmpConst; |
| 5037 | } else { |
| 5038 | CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned()); |
| 5039 | } |
Sanjay Patel | 7ce2638 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 5040 | break; |
| 5041 | case Instruction::FPTrunc: |
| 5042 | CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true); |
| 5043 | break; |
| 5044 | case Instruction::FPExt: |
| 5045 | CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true); |
| 5046 | break; |
| 5047 | case Instruction::FPToUI: |
| 5048 | CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true); |
| 5049 | break; |
| 5050 | case Instruction::FPToSI: |
| 5051 | CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true); |
| 5052 | break; |
| 5053 | case Instruction::UIToFP: |
| 5054 | CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true); |
| 5055 | break; |
| 5056 | case Instruction::SIToFP: |
| 5057 | CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true); |
| 5058 | break; |
| 5059 | default: |
| 5060 | break; |
| 5061 | } |
David Majnemer | 359298e | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 5062 | |
| 5063 | if (!CastedTo) |
| 5064 | return nullptr; |
| 5065 | |
David Majnemer | 359298e | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 5066 | // Make sure the cast doesn't lose any information. |
Sanjay Patel | 7ce2638 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 5067 | Constant *CastedBack = |
| 5068 | ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true); |
David Majnemer | 359298e | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 5069 | if (CastedBack != C) |
| 5070 | return nullptr; |
| 5071 | |
| 5072 | return CastedTo; |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5073 | } |
| 5074 | |
Sanjay Patel | 645115f | 2016-05-23 17:57:54 +0000 | [diff] [blame] | 5075 | SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 5076 | Instruction::CastOps *CastOp, |
| 5077 | unsigned Depth) { |
| 5078 | if (Depth >= MaxDepth) |
| 5079 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 5080 | |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5081 | SelectInst *SI = dyn_cast<SelectInst>(V); |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5082 | if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5083 | |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5084 | CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition()); |
| 5085 | if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5086 | |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5087 | CmpInst::Predicate Pred = CmpI->getPredicate(); |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5088 | Value *CmpLHS = CmpI->getOperand(0); |
| 5089 | Value *CmpRHS = CmpI->getOperand(1); |
| 5090 | Value *TrueVal = SI->getTrueValue(); |
| 5091 | Value *FalseVal = SI->getFalseValue(); |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5092 | FastMathFlags FMF; |
| 5093 | if (isa<FPMathOperator>(CmpI)) |
| 5094 | FMF = CmpI->getFastMathFlags(); |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5095 | |
| 5096 | // Bail out early. |
| 5097 | if (CmpI->isEquality()) |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5098 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5099 | |
| 5100 | // Deal with type mismatches. |
| 5101 | if (CastOp && CmpLHS->getType() != TrueVal->getType()) { |
Sanjay Patel | ba5e1fb | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 5102 | if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) { |
| 5103 | // If this is a potential fmin/fmax with a cast to integer, then ignore |
| 5104 | // -0.0 because there is no corresponding integer value. |
| 5105 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
| 5106 | FMF.setNoSignedZeros(); |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5107 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5108 | cast<CastInst>(TrueVal)->getOperand(0), C, |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 5109 | LHS, RHS, Depth); |
Sanjay Patel | ba5e1fb | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 5110 | } |
| 5111 | if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) { |
| 5112 | // If this is a potential fmin/fmax with a cast to integer, then ignore |
| 5113 | // -0.0 because there is no corresponding integer value. |
| 5114 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
| 5115 | FMF.setNoSignedZeros(); |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5116 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5117 | C, cast<CastInst>(FalseVal)->getOperand(0), |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 5118 | LHS, RHS, Depth); |
Sanjay Patel | ba5e1fb | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 5119 | } |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5120 | } |
James Molloy | cd45f4f | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 5121 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, |
Sanjay Patel | 207c75b | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 5122 | LHS, RHS, Depth); |
James Molloy | eb9033d | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 5123 | } |
Sanjoy Das | 5dacfa4 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 5124 | |
Sanjay Patel | 3c992c6 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 5125 | CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) { |
| 5126 | if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT; |
| 5127 | if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT; |
| 5128 | if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT; |
| 5129 | if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT; |
| 5130 | if (SPF == SPF_FMINNUM) |
| 5131 | return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; |
| 5132 | if (SPF == SPF_FMAXNUM) |
| 5133 | return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; |
| 5134 | llvm_unreachable("unhandled!"); |
| 5135 | } |
| 5136 | |
| 5137 | SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) { |
| 5138 | if (SPF == SPF_SMIN) return SPF_SMAX; |
| 5139 | if (SPF == SPF_UMIN) return SPF_UMAX; |
| 5140 | if (SPF == SPF_SMAX) return SPF_SMIN; |
| 5141 | if (SPF == SPF_UMAX) return SPF_UMIN; |
| 5142 | llvm_unreachable("unhandled!"); |
| 5143 | } |
| 5144 | |
| 5145 | CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) { |
| 5146 | return getMinMaxPred(getInverseMinMaxFlavor(SPF)); |
| 5147 | } |
| 5148 | |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5149 | /// Return true if "icmp Pred LHS RHS" is always true. |
Chad Rosier | c252763 | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5150 | static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, |
| 5151 | const Value *RHS, const DataLayout &DL, |
| 5152 | unsigned Depth) { |
Sanjoy Das | ee8bc9a | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5153 | assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!"); |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5154 | if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS) |
| 5155 | return true; |
| 5156 | |
| 5157 | switch (Pred) { |
| 5158 | default: |
| 5159 | return false; |
| 5160 | |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5161 | case CmpInst::ICMP_SLE: { |
Sanjoy Das | ee8bc9a | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5162 | const APInt *C; |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5163 | |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5164 | // LHS s<= LHS +_{nsw} C if C >= 0 |
Sanjoy Das | 0e67289 | 2015-11-11 00:16:41 +0000 | [diff] [blame] | 5165 | if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C)))) |
Sanjoy Das | ee8bc9a | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5166 | return !C->isNegative(); |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5167 | return false; |
| 5168 | } |
| 5169 | |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5170 | case CmpInst::ICMP_ULE: { |
Sanjoy Das | ee8bc9a | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5171 | const APInt *C; |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5172 | |
Sanjoy Das | 0e67289 | 2015-11-11 00:16:41 +0000 | [diff] [blame] | 5173 | // LHS u<= LHS +_{nuw} C for any C |
| 5174 | if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C)))) |
Sanjoy Das | 18e2900 | 2015-11-06 19:01:03 +0000 | [diff] [blame] | 5175 | return true; |
Sanjoy Das | 662c69d | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5176 | |
| 5177 | // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5178 | auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B, |
| 5179 | const Value *&X, |
Sanjoy Das | 662c69d | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5180 | const APInt *&CA, const APInt *&CB) { |
| 5181 | if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) && |
| 5182 | match(B, m_NUWAdd(m_Specific(X), m_APInt(CB)))) |
| 5183 | return true; |
| 5184 | |
| 5185 | // If X & C == 0 then (X | C) == X +_{nuw} C |
| 5186 | if (match(A, m_Or(m_Value(X), m_APInt(CA))) && |
| 5187 | match(B, m_Or(m_Specific(X), m_APInt(CB)))) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 5188 | KnownBits Known(CA->getBitWidth()); |
Chad Rosier | c252763 | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5189 | computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr, |
| 5190 | /*CxtI*/ nullptr, /*DT*/ nullptr); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 5191 | if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero)) |
Sanjoy Das | 662c69d | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5192 | return true; |
| 5193 | } |
| 5194 | |
| 5195 | return false; |
| 5196 | }; |
| 5197 | |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5198 | const Value *X; |
Sanjoy Das | 662c69d | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5199 | const APInt *CLHS, *CRHS; |
Sanjoy Das | 0e67289 | 2015-11-11 00:16:41 +0000 | [diff] [blame] | 5200 | if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS)) |
| 5201 | return CLHS->ule(*CRHS); |
Sanjoy Das | 662c69d | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5202 | |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5203 | return false; |
| 5204 | } |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5209 | /// ALHS ARHS" is true. Otherwise, return None. |
| 5210 | static Optional<bool> |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5211 | isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, |
Chad Rosier | c252763 | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5212 | const Value *ARHS, const Value *BLHS, const Value *BRHS, |
| 5213 | const DataLayout &DL, unsigned Depth) { |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5214 | switch (Pred) { |
| 5215 | default: |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5216 | return None; |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5217 | |
| 5218 | case CmpInst::ICMP_SLT: |
| 5219 | case CmpInst::ICMP_SLE: |
Chad Rosier | c252763 | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5220 | if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) && |
| 5221 | isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth)) |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5222 | return true; |
| 5223 | return None; |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5224 | |
| 5225 | case CmpInst::ICMP_ULT: |
| 5226 | case CmpInst::ICMP_ULE: |
Chad Rosier | c252763 | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5227 | if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) && |
| 5228 | isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth)) |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5229 | return true; |
| 5230 | return None; |
Sanjoy Das | 772fb1e | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5231 | } |
| 5232 | } |
| 5233 | |
Chad Rosier | 668a46a | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5234 | /// Return true if the operands of the two compares match. IsSwappedOps is true |
| 5235 | /// when the operands match, but are swapped. |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5236 | static bool isMatchingOps(const Value *ALHS, const Value *ARHS, |
| 5237 | const Value *BLHS, const Value *BRHS, |
Chad Rosier | 668a46a | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5238 | bool &IsSwappedOps) { |
| 5239 | |
| 5240 | bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS); |
| 5241 | IsSwappedOps = (ALHS == BRHS && ARHS == BLHS); |
| 5242 | return IsMatchingOps || IsSwappedOps; |
| 5243 | } |
| 5244 | |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5245 | /// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true. |
| 5246 | /// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false. |
| 5247 | /// Otherwise, return None if we can't infer anything. |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5248 | static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred, |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5249 | CmpInst::Predicate BPred, |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5250 | bool AreSwappedOps) { |
| 5251 | // Canonicalize the predicate as if the operands were not commuted. |
| 5252 | if (AreSwappedOps) |
Chad Rosier | 2974be9 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5253 | BPred = ICmpInst::getSwappedPredicate(BPred); |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5254 | |
Chad Rosier | 615a6cf | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 5255 | if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred)) |
Chad Rosier | 2974be9 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5256 | return true; |
Chad Rosier | 615a6cf | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 5257 | if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred)) |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5258 | return false; |
Chad Rosier | 2974be9 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5259 | |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5260 | return None; |
Chad Rosier | 2974be9 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5261 | } |
| 5262 | |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5263 | /// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true. |
| 5264 | /// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false. |
| 5265 | /// Otherwise, return None if we can't infer anything. |
Chad Rosier | a2d385e | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5266 | static Optional<bool> |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5267 | isImpliedCondMatchingImmOperands(CmpInst::Predicate APred, |
Pete Cooper | 1231164 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5268 | const ConstantInt *C1, |
| 5269 | CmpInst::Predicate BPred, |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5270 | const ConstantInt *C2) { |
Chad Rosier | a2d385e | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5271 | ConstantRange DomCR = |
| 5272 | ConstantRange::makeExactICmpRegion(APred, C1->getValue()); |
| 5273 | ConstantRange CR = |
| 5274 | ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue()); |
| 5275 | ConstantRange Intersection = DomCR.intersectWith(CR); |
| 5276 | ConstantRange Difference = DomCR.difference(CR); |
| 5277 | if (Intersection.isEmptySet()) |
| 5278 | return false; |
| 5279 | if (Difference.isEmptySet()) |
| 5280 | return true; |
| 5281 | return None; |
| 5282 | } |
| 5283 | |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5284 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is |
| 5285 | /// false. Otherwise, return None if we can't infer anything. |
| 5286 | static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS, |
| 5287 | const ICmpInst *RHS, |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5288 | const DataLayout &DL, bool LHSIsTrue, |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5289 | unsigned Depth) { |
| 5290 | Value *ALHS = LHS->getOperand(0); |
| 5291 | Value *ARHS = LHS->getOperand(1); |
Chad Rosier | 91054cc | 2017-07-06 20:00:25 +0000 | [diff] [blame] | 5292 | // The rest of the logic assumes the LHS condition is true. If that's not the |
| 5293 | // case, invert the predicate to make it so. |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5294 | ICmpInst::Predicate APred = |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5295 | LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5296 | |
| 5297 | Value *BLHS = RHS->getOperand(0); |
| 5298 | Value *BRHS = RHS->getOperand(1); |
| 5299 | ICmpInst::Predicate BPred = RHS->getPredicate(); |
Chad Rosier | 3ac68db | 2016-04-25 17:23:36 +0000 | [diff] [blame] | 5300 | |
Chad Rosier | 668a46a | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5301 | // Can we infer anything when the two compares have matching operands? |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5302 | bool AreSwappedOps; |
| 5303 | if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) { |
Chad Rosier | 668a46a | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5304 | if (Optional<bool> Implication = isImpliedCondMatchingOperands( |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5305 | APred, BPred, AreSwappedOps)) |
Chad Rosier | a2d385e | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5306 | return Implication; |
Chad Rosier | 668a46a | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5307 | // No amount of additional analysis will infer the second condition, so |
| 5308 | // early exit. |
| 5309 | return None; |
| 5310 | } |
| 5311 | |
| 5312 | // Can we infer anything when the LHS operands match and the RHS operands are |
| 5313 | // constants (not necessarily matching)? |
| 5314 | if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) { |
| 5315 | if (Optional<bool> Implication = isImpliedCondMatchingImmOperands( |
Sanjay Patel | 50eb45a | 2018-12-19 16:49:18 +0000 | [diff] [blame] | 5316 | APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS))) |
Chad Rosier | 668a46a | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5317 | return Implication; |
| 5318 | // No amount of additional analysis will infer the second condition, so |
| 5319 | // early exit. |
| 5320 | return None; |
Chad Rosier | a2d385e | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5321 | } |
| 5322 | |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5323 | if (APred == BPred) |
Chad Rosier | c252763 | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5324 | return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth); |
Chad Rosier | dd01bef | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5325 | return None; |
Sanjoy Das | 3386f43 | 2015-10-28 03:20:19 +0000 | [diff] [blame] | 5326 | } |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5327 | |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5328 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is |
| 5329 | /// false. Otherwise, return None if we can't infer anything. We expect the |
| 5330 | /// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction. |
| 5331 | static Optional<bool> isImpliedCondAndOr(const BinaryOperator *LHS, |
| 5332 | const ICmpInst *RHS, |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5333 | const DataLayout &DL, bool LHSIsTrue, |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5334 | unsigned Depth) { |
| 5335 | // The LHS must be an 'or' or an 'and' instruction. |
| 5336 | assert((LHS->getOpcode() == Instruction::And || |
| 5337 | LHS->getOpcode() == Instruction::Or) && |
| 5338 | "Expected LHS to be 'and' or 'or'."); |
| 5339 | |
Davide Italiano | 085d544 | 2017-08-09 16:06:54 +0000 | [diff] [blame] | 5340 | assert(Depth <= MaxDepth && "Hit recursion limit"); |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5341 | |
| 5342 | // If the result of an 'or' is false, then we know both legs of the 'or' are |
| 5343 | // false. Similarly, if the result of an 'and' is true, then we know both |
| 5344 | // legs of the 'and' are true. |
| 5345 | Value *ALHS, *ARHS; |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5346 | if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) || |
| 5347 | (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) { |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5348 | // FIXME: Make this non-recursion. |
| 5349 | if (Optional<bool> Implication = |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5350 | isImpliedCondition(ALHS, RHS, DL, LHSIsTrue, Depth + 1)) |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5351 | return Implication; |
| 5352 | if (Optional<bool> Implication = |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5353 | isImpliedCondition(ARHS, RHS, DL, LHSIsTrue, Depth + 1)) |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5354 | return Implication; |
| 5355 | return None; |
| 5356 | } |
| 5357 | return None; |
| 5358 | } |
| 5359 | |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5360 | Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS, |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5361 | const DataLayout &DL, bool LHSIsTrue, |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5362 | unsigned Depth) { |
Davide Italiano | 7805791 | 2017-08-09 15:13:50 +0000 | [diff] [blame] | 5363 | // Bail out when we hit the limit. |
| 5364 | if (Depth == MaxDepth) |
| 5365 | return None; |
| 5366 | |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5367 | // A mismatch occurs when we compare a scalar cmp to a vector cmp, for |
| 5368 | // example. |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5369 | if (LHS->getType() != RHS->getType()) |
| 5370 | return None; |
| 5371 | |
| 5372 | Type *OpTy = LHS->getType(); |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5373 | assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!"); |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5374 | |
| 5375 | // LHS ==> RHS by definition |
| 5376 | if (LHS == RHS) |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5377 | return LHSIsTrue; |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5378 | |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5379 | // FIXME: Extending the code below to handle vectors. |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5380 | if (OpTy->isVectorTy()) |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5381 | return None; |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5382 | |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5383 | assert(OpTy->isIntegerTy(1) && "implied by above"); |
| 5384 | |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5385 | // Both LHS and RHS are icmps. |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5386 | const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS); |
| 5387 | const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS); |
| 5388 | if (LHSCmp && RHSCmp) |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5389 | return isImpliedCondICmps(LHSCmp, RHSCmp, DL, LHSIsTrue, Depth); |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5390 | |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5391 | // The LHS should be an 'or' or an 'and' instruction. We expect the RHS to be |
| 5392 | // an icmp. FIXME: Add support for and/or on the RHS. |
| 5393 | const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS); |
| 5394 | if (LHSBO && RHSCmp) { |
| 5395 | if ((LHSBO->getOpcode() == Instruction::And || |
| 5396 | LHSBO->getOpcode() == Instruction::Or)) |
Chad Rosier | 0d8a777 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5397 | return isImpliedCondAndOr(LHSBO, RHSCmp, DL, LHSIsTrue, Depth); |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5398 | } |
Chad Rosier | c5f425b | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5399 | return None; |
Chad Rosier | c1d5445 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5400 | } |
Sanjay Patel | 7e0e2c0 | 2018-12-02 13:26:03 +0000 | [diff] [blame] | 5401 | |
| 5402 | Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond, |
| 5403 | const Instruction *ContextI, |
| 5404 | const DataLayout &DL) { |
| 5405 | assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool"); |
| 5406 | if (!ContextI || !ContextI->getParent()) |
| 5407 | return None; |
| 5408 | |
| 5409 | // TODO: This is a poor/cheap way to determine dominance. Should we use a |
| 5410 | // dominator tree (eg, from a SimplifyQuery) instead? |
| 5411 | const BasicBlock *ContextBB = ContextI->getParent(); |
| 5412 | const BasicBlock *PredBB = ContextBB->getSinglePredecessor(); |
| 5413 | if (!PredBB) |
| 5414 | return None; |
| 5415 | |
| 5416 | // We need a conditional branch in the predecessor. |
| 5417 | Value *PredCond; |
| 5418 | BasicBlock *TrueBB, *FalseBB; |
| 5419 | if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB))) |
| 5420 | return None; |
| 5421 | |
| 5422 | // The branch should get simplified. Don't bother simplifying this condition. |
| 5423 | if (TrueBB == FalseBB) |
| 5424 | return None; |
| 5425 | |
| 5426 | assert((TrueBB == ContextBB || FalseBB == ContextBB) && |
| 5427 | "Predecessor block does not point to successor?"); |
| 5428 | |
| 5429 | // Is this condition implied by the predecessor condition? |
| 5430 | bool CondIsTrue = TrueBB == ContextBB; |
| 5431 | return isImpliedCondition(PredCond, Cond, DL, CondIsTrue); |
| 5432 | } |