Chris Lattner | 965c769 | 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 | 75075ef | 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 | 493e57d | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Optional.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | 75075ef | 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 | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/AssumptionCache.h" |
Max Kazantsev | 3c284bd | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/GuardUtils.h" |
Dan Gohman | 949ab78 | 2010-12-15 20:10:26 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/InstructionSimplify.h" |
Artur Pilipenko | 31bcca4 | 2016-02-24 12:49:04 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/Loads.h" |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/LoopInfo.h" |
Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
Eugene Zelenko | 75075ef | 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 | ec37354 | 2014-05-20 05:13:21 +0000 | [diff] [blame] | 38 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Constant.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 40 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Constants.h" |
| 42 | #include "llvm/IR/DataLayout.h" |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 43 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 44 | #include "llvm/IR/DiagnosticInfo.h" |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Dominators.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Function.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 47 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 48 | #include "llvm/IR/GlobalAlias.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 49 | #include "llvm/IR/GlobalValue.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 50 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Instructions.h" |
| 54 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 55 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 56 | #include "llvm/IR/LLVMContext.h" |
| 57 | #include "llvm/IR/Metadata.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 60 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 75075ef | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 68 | #include "llvm/Support/KnownBits.h" |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 69 | #include "llvm/Support/MathExtras.h" |
Matthias Braun | 37e5d79 | 2016-01-28 06:29:33 +0000 | [diff] [blame] | 70 | #include <algorithm> |
| 71 | #include <array> |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 72 | #include <cassert> |
| 73 | #include <cstdint> |
| 74 | #include <iterator> |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 75 | #include <utility> |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 77 | using namespace llvm; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 78 | using namespace llvm::PatternMatch; |
| 79 | |
| 80 | const unsigned MaxDepth = 6; |
| 81 | |
Philip Reames | 1c29227 | 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 | cea9ede | 2015-09-29 14:57:52 +0000 | [diff] [blame] | 85 | cl::Hidden, cl::init(20)); |
Philip Reames | 1c29227 | 2015-03-10 22:43:20 +0000 | [diff] [blame] | 86 | |
Craig Topper | 6b3940a | 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 | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 89 | static unsigned getBitWidth(Type *Ty, const DataLayout &DL) { |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 90 | if (unsigned BitWidth = Ty->getScalarSizeInBits()) |
| 91 | return BitWidth; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 92 | |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 93 | return DL.getIndexTypeSizeInBits(Ty); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 94 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 95 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 96 | namespace { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 97 | |
Hal Finkel | 60db058 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 103 | const DataLayout &DL; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 104 | AssumptionCache *AC; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 105 | const Instruction *CxtI; |
| 106 | const DominatorTree *DT; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 107 | |
Sanjay Patel | 54656ca | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 111 | |
Matthias Braun | 37e5d79 | 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 | 6e11a05 | 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 | 755f75f | 2016-10-15 19:00:04 +0000 | [diff] [blame] | 120 | std::array<const Value *, MaxDepth> Excluded; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 121 | |
Florian Hahn | 19f9e32 | 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 | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 125 | unsigned NumExcluded = 0; |
Matthias Braun | 37e5d79 | 2016-01-28 06:29:33 +0000 | [diff] [blame] | 126 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 127 | Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 19f9e32 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 131 | |
| 132 | Query(const Query &Q, const Value *NewExcl) |
Florian Hahn | 19f9e32 | 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 | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 134 | NumExcluded(Q.NumExcluded) { |
Matthias Braun | 37e5d79 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 145 | } |
| 146 | }; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 147 | |
Benjamin Kramer | cfd8d90 | 2014-09-12 08:56:53 +0000 | [diff] [blame] | 148 | } // end anonymous namespace |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 149 | |
Sanjay Patel | 547e975 | 2014-11-04 16:09:50 +0000 | [diff] [blame] | 150 | // Given the provided Value and, potentially, a context instruction, return |
Hal Finkel | 60db058 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 166 | static void computeKnownBits(const Value *V, KnownBits &Known, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 167 | unsigned Depth, const Query &Q); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 168 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 169 | void llvm::computeKnownBits(const Value *V, KnownBits &Known, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 170 | const DataLayout &DL, unsigned Depth, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 171 | AssumptionCache *AC, const Instruction *CxtI, |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 172 | const DominatorTree *DT, |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 173 | OptimizationRemarkEmitter *ORE, bool UseInstrInfo) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 174 | ::computeKnownBits(V, Known, Depth, |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 175 | Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE)); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Craig Topper | 6e11a05 | 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 | a2025ea | 2017-05-24 16:53:03 +0000 | [diff] [blame] | 184 | const DominatorTree *DT, |
Florian Hahn | 19f9e32 | 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 | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 191 | bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS, |
Florian Hahn | 19f9e32 | 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 | ca32190 | 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 | 620b3da | 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 | ca32190 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 207 | IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType()); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 208 | KnownBits LHSKnown(IT->getBitWidth()); |
| 209 | KnownBits RHSKnown(IT->getBitWidth()); |
Florian Hahn | 19f9e32 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 212 | return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue(); |
Jingyue Wu | ca32190 | 2015-05-14 23:53:19 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Zaara Syeda | 3a7578c | 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 | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 227 | static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 228 | const Query &Q); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 229 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 230 | bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, |
Florian Hahn | 19f9e32 | 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 | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 238 | static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 239 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 240 | bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 241 | AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 19f9e32 | 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 | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 247 | bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL, |
Florian Hahn | 19f9e32 | 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 | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 253 | return Known.isNonNegative(); |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 256 | bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 257 | AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 258 | const DominatorTree *DT, bool UseInstrInfo) { |
Philip Reames | 8f12eba | 2016-03-09 21:31:47 +0000 | [diff] [blame] | 259 | if (auto *CI = dyn_cast<ConstantInt>(V)) |
| 260 | return CI->getValue().isStrictlyPositive(); |
Sanjoy Das | 6082c1a | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 261 | |
Philip Reames | 8f12eba | 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 | 19f9e32 | 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 | 8f12eba | 2016-03-09 21:31:47 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 268 | bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 269 | AssumptionCache *AC, const Instruction *CxtI, |
Florian Hahn | 19f9e32 | 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 | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 273 | return Known.isNegative(); |
Nick Lewycky | 762f8a8 | 2016-04-21 00:53:14 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 276 | static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q); |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 277 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 278 | bool llvm::isKnownNonEqual(const Value *V1, const Value *V2, |
Florian Hahn | 19f9e32 | 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 | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 287 | static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 288 | const Query &Q); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 289 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 290 | bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask, |
Florian Hahn | 19f9e32 | 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 | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 298 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, |
| 299 | const Query &Q); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 300 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 301 | unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 302 | unsigned Depth, AssumptionCache *AC, |
| 303 | const Instruction *CxtI, |
Florian Hahn | 19f9e32 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Craig Topper | 8fbb74b | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 311 | KnownBits &KnownOut, KnownBits &Known2, |
Craig Topper | 8fbb74b | 2017-03-24 22:12:10 +0000 | [diff] [blame] | 312 | unsigned Depth, const Query &Q) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 313 | unsigned BitWidth = KnownOut.getBitWidth(); |
Craig Topper | 8fbb74b | 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 | b45eabc | 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 | 8fbb74b | 2017-03-24 22:12:10 +0000 | [diff] [blame] | 320 | |
Craig Topper | b498a23 | 2017-08-08 16:29:35 +0000 | [diff] [blame] | 321 | KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 324 | static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 325 | KnownBits &Known, KnownBits &Known2, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 326 | unsigned Depth, const Query &Q) { |
Craig Topper | b45eabc | 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 | fa30607 | 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 | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 334 | if (NSW) { |
Nick Lewycky | fa30607 | 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 | ca48af3 | 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 | fa30607 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 350 | isKnownNonZero(Op0, Depth, Q)) || |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 351 | (isKnownNegativeOp0 && isKnownNonNegativeOp1 && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 352 | isKnownNonZero(Op1, Depth, Q)); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
Simon Dardis | 70dbd5f | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 358 | unsigned LeadZ = std::max(Known.countMinLeadingZeros() + |
| 359 | Known2.countMinLeadingZeros(), |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 360 | BitWidth) - BitWidth; |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 361 | LeadZ = std::min(LeadZ, BitWidth); |
Simon Dardis | 70dbd5f | 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 | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 424 | Known.resetAll(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 425 | Known.Zero.setHighBits(LeadZ); |
Simon Dardis | 70dbd5f | 2017-12-09 23:25:57 +0000 | [diff] [blame] | 426 | Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown); |
| 427 | Known.One |= BottomKnown.getLoBits(ResultBitsKnown); |
Nick Lewycky | fa30607 | 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 | ca48af3 | 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 | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 440 | void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges, |
Craig Topper | f42b23f | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 441 | KnownBits &Known) { |
| 442 | unsigned BitWidth = Known.getBitWidth(); |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 443 | unsigned NumRanges = Ranges.getNumOperands() / 2; |
| 444 | assert(NumRanges >= 1); |
| 445 | |
Craig Topper | f42b23f | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 446 | Known.Zero.setAllBits(); |
| 447 | Known.One.setAllBits(); |
Sanjoy Das | 1d1929a | 2015-10-28 03:20:15 +0000 | [diff] [blame] | 448 | |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 449 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 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 | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 454 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 455 | |
Sanjoy Das | 1d1929a | 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 | f42b23f | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 461 | Known.One &= Range.getUnsignedMax() & Mask; |
| 462 | Known.Zero &= ~Range.getUnsignedMax() & Mask; |
Sanjoy Das | 1d1929a | 2015-10-28 03:20:15 +0000 | [diff] [blame] | 463 | } |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 464 | } |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 465 | |
Pete Cooper | fa7ae4f | 2016-08-11 22:23:07 +0000 | [diff] [blame] | 466 | static bool isEphemeralValueOf(const Instruction *I, const Value *E) { |
Hal Finkel | 60db058 | 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 | f2199b2 | 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 | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 474 | if (is_contained(I->operands(), E)) |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 475 | return true; |
| 476 | |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 477 | while (!WorkSet.empty()) { |
| 478 | const Value *V = WorkSet.pop_back_val(); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 479 | if (!Visited.insert(V).second) |
Hal Finkel | 60db058 | 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 | 75075ef | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 486 | if (V == E) |
| 487 | return true; |
| 488 | |
Hal Finkel | b03dd4b | 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 | 60db058 | 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 | a446151 | 2017-12-15 14:34:41 +0000 | [diff] [blame] | 503 | bool llvm::isAssumeLikeIntrinsic(const Instruction *I) { |
Hal Finkel | 60db058 | 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 | 2c74fe9 | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 510 | case Intrinsic::sideeffect: |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 511 | case Intrinsic::dbg_declare: |
| 512 | case Intrinsic::dbg_value: |
Shiva Chen | 2c86455 | 2018-05-09 02:40:45 +0000 | [diff] [blame] | 513 | case Intrinsic::dbg_label: |
Hal Finkel | 60db058 | 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 | fa7ae4f | 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 | 60db058 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 538 | if (DT) { |
Pete Cooper | 54a0255 | 2016-08-12 01:00:15 +0000 | [diff] [blame] | 539 | if (DT->dominates(Inv, CxtI)) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 540 | return true; |
Pete Cooper | 54a0255 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Pete Cooper | 54a0255 | 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 | d319674 | 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 | 54a0255 | 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 | 60db058 | 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 | fa7ae4f | 2016-08-11 22:23:07 +0000 | [diff] [blame] | 557 | for (auto I = std::next(BasicBlock::const_iterator(Inv)), |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 558 | IE = Inv->getParent()->end(); I != IE; ++I) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 559 | if (&*I == CxtI) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 560 | return true; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Pete Cooper | 54a0255 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Craig Topper | b45eabc | 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 | 60db058 | 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 | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 578 | if (!Q.AC || !Q.CxtI) |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 579 | return; |
| 580 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 581 | unsigned BitWidth = Known.getBitWidth(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 582 | |
Hal Finkel | 8a9a783 | 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 | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 587 | if (!AssumeVH) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 588 | continue; |
Daniel Jasper | aec2fa3 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 593 | continue; |
| 594 | |
Vedant Kumar | d319674 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 595 | // Warning: This loop can end up being somewhat performance sensitive. |
Daniel Jasper | aec2fa3 | 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 | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 598 | |
Daniel Jasper | aec2fa3 | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 605 | assert(BitWidth == 1 && "assume operand is not i1?"); |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 606 | Known.setAllOnes(); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
Sanjay Patel | 9666996 | 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 | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 612 | Known.setAllZero(); |
Sanjay Patel | 9666996 | 2017-01-17 18:15:49 +0000 | [diff] [blame] | 613 | return; |
| 614 | } |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 615 | |
David Majnemer | 9b60975 | 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 | 60db058 | 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 | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 626 | uint64_t C; |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 627 | // assume(v = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 628 | if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 629 | Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 634 | // assume(v & b = a) |
Mehdi Amini | a28d91d | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 637 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 638 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 60db058 | 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 | b45eabc | 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 | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 648 | // assume(~(v & b) = a) |
Philip Reames | 00d3b27 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 651 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 652 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 15aeaaf | 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 | b45eabc | 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 | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 662 | // assume(v | b = a) |
Mehdi Amini | a28d91d | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 665 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 666 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 15aeaaf | 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 | b45eabc | 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 | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 676 | // assume(~(v | b) = a) |
Philip Reames | 00d3b27 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 679 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 680 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 15aeaaf | 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 | b45eabc | 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 | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 690 | // assume(v ^ b = a) |
Mehdi Amini | a28d91d | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 693 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 694 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 15aeaaf | 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 | b45eabc | 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 | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 707 | // assume(~(v ^ b) = a) |
Philip Reames | 00d3b27 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 710 | Pred == ICmpInst::ICMP_EQ && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 711 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 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 | 15aeaaf | 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 | b45eabc | 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 | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 724 | // assume(v << c = a) |
Philip Reames | 00d3b27 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 727 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 728 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 729 | C < BitWidth) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 730 | KnownBits RHSKnown(BitWidth); |
| 731 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 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 | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 734 | RHSKnown.Zero.lshrInPlace(C); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 735 | Known.Zero |= RHSKnown.Zero; |
Igor Laevsky | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 736 | RHSKnown.One.lshrInPlace(C); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 737 | Known.One |= RHSKnown.One; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 738 | // assume(~(v << c) = a) |
Philip Reames | 00d3b27 | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 741 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 742 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 743 | C < BitWidth) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 744 | KnownBits RHSKnown(BitWidth); |
| 745 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 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 | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 748 | RHSKnown.One.lshrInPlace(C); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 749 | Known.Zero |= RHSKnown.One; |
Igor Laevsky | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 750 | RHSKnown.Zero.lshrInPlace(C); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 751 | Known.One |= RHSKnown.Zero; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 752 | // assume(v >> c = a) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 753 | } else if (match(Arg, |
Craig Topper | 7b66ffe | 2017-06-24 06:24:04 +0000 | [diff] [blame] | 754 | m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 755 | m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 756 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 757 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 758 | C < BitWidth) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 759 | KnownBits RHSKnown(BitWidth); |
| 760 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 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 | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 763 | Known.Zero |= RHSKnown.Zero << C; |
| 764 | Known.One |= RHSKnown.One << C; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 765 | // assume(~(v >> c) = a) |
Craig Topper | 7b66ffe | 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 | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 767 | m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 768 | Pred == ICmpInst::ICMP_EQ && |
Igor Laevsky | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 769 | isValidAssumeForContext(I, Q.CxtI, Q.DT) && |
| 770 | C < BitWidth) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 771 | KnownBits RHSKnown(BitWidth); |
| 772 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 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 | cec8f47 | 2017-12-05 12:18:15 +0000 | [diff] [blame] | 775 | Known.Zero |= RHSKnown.One << C; |
| 776 | Known.One |= RHSKnown.Zero << C; |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 777 | // assume(v >=_s c) where c is non-negative |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 778 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 779 | Pred == ICmpInst::ICMP_SGE && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 780 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 781 | KnownBits RHSKnown(BitWidth); |
| 782 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 783 | |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 784 | if (RHSKnown.isNonNegative()) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 785 | // We know that the sign bit is zero. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 786 | Known.makeNonNegative(); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 787 | } |
| 788 | // assume(v >_s c) where c is at least -1. |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 789 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 790 | Pred == ICmpInst::ICMP_SGT && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 791 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 792 | KnownBits RHSKnown(BitWidth); |
| 793 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 794 | |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 795 | if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 796 | // We know that the sign bit is zero. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 797 | Known.makeNonNegative(); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 798 | } |
| 799 | // assume(v <=_s c) where c is negative |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 800 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 801 | Pred == ICmpInst::ICMP_SLE && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 802 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 803 | KnownBits RHSKnown(BitWidth); |
| 804 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 805 | |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 806 | if (RHSKnown.isNegative()) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 807 | // We know that the sign bit is one. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 808 | Known.makeNegative(); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 809 | } |
| 810 | // assume(v <_s c) where c is non-positive |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 811 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 812 | Pred == ICmpInst::ICMP_SLT && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 813 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 814 | KnownBits RHSKnown(BitWidth); |
| 815 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 816 | |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 817 | if (RHSKnown.isZero() || RHSKnown.isNegative()) { |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 818 | // We know that the sign bit is one. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 819 | Known.makeNegative(); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 820 | } |
| 821 | // assume(v <=_u c) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 822 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 823 | Pred == ICmpInst::ICMP_ULE && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 824 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 825 | KnownBits RHSKnown(BitWidth); |
| 826 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 829 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); |
| 830 | // assume(v <_u c) |
Philip Reames | 00d3b27 | 2014-11-24 23:44:28 +0000 | [diff] [blame] | 831 | } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 832 | Pred == ICmpInst::ICMP_ULT && |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 833 | isValidAssumeForContext(I, Q.CxtI, Q.DT)) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 834 | KnownBits RHSKnown(BitWidth); |
| 835 | computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I)); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 836 | |
Sanjay Patel | a60aec1 | 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 | 15aeaaf | 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 | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 847 | if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I))) |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 848 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1); |
Hal Finkel | 15aeaaf | 2014-09-07 19:21:07 +0000 | [diff] [blame] | 849 | else |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 850 | Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros()); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 851 | } |
| 852 | } |
Sanjay Patel | 25f6d71 | 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 | 54656ca | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 859 | if (Known.Zero.intersects(Known.One)) { |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 860 | Known.resetAll(); |
Sanjay Patel | 54656ca | 2017-02-06 18:26:06 +0000 | [diff] [blame] | 861 | |
Vivek Pandya | 9590658 | 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 | 25f6d71 | 2017-02-01 15:41:32 +0000 | [diff] [blame] | 871 | } |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Sanjay Patel | b7d1238 | 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 | d319674 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 877 | /// operator-specific functions that, given the known-zero or known-one bits |
Sanjay Patel | b7d1238 | 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 | 54690dc | 2016-08-23 20:52:00 +0000 | [diff] [blame] | 882 | static void computeKnownBitsFromShiftOperator( |
Craig Topper | b45eabc | 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 | d0d43e6 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 887 | unsigned BitWidth = Known.getBitWidth(); |
Hal Finkel | f2199b2 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 892 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Sam McCall | d0d43e6 | 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 | e272be7 | 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 | d7f9c35 | 2016-08-24 23:01:33 +0000 | [diff] [blame] | 900 | |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 901 | return; |
| 902 | } |
| 903 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 904 | computeKnownBits(I->getOperand(1), Known, Depth + 1, Q); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 905 | |
Sanjay Patel | e272be7 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 909 | if ((~Known.Zero).uge(BitWidth)) { |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 910 | Known.resetAll(); |
Oliver Stannard | 0620411 | 2017-03-14 10:13:17 +0000 | [diff] [blame] | 911 | return; |
| 912 | } |
| 913 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 914 | // Note: We cannot use Known.Zero.getLimitedValue() here, because if |
Hal Finkel | f2199b2 | 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 | b45eabc | 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 | f2199b2 | 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 | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 922 | Known.resetAll(); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 923 | |
James Molloy | 493e57d | 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 | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 929 | // Early exit if we can't constrain any well-defined shift amount. |
Craig Topper | f93b7b1 | 2017-06-14 17:04:59 +0000 | [diff] [blame] | 930 | if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) && |
| 931 | !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) { |
Sanjay Patel | b7d1238 | 2017-10-16 14:46:37 +0000 | [diff] [blame] | 932 | ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q); |
James Molloy | 493e57d | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 933 | if (!*ShifterOperandIsNonZero) |
| 934 | return; |
| 935 | } |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 936 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 937 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 938 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 939 | Known.Zero.setAllBits(); |
| 940 | Known.One.setAllBits(); |
Hal Finkel | f2199b2 | 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 | 493e57d | 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 | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 954 | isKnownNonZero(I->getOperand(1), Depth + 1, Q); |
James Molloy | 493e57d | 2015-10-26 14:10:46 +0000 | [diff] [blame] | 955 | if (*ShifterOperandIsNonZero) |
| 956 | continue; |
| 957 | } |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 958 | |
Sam McCall | d0d43e6 | 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 | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Sanjay Patel | e272be7 | 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 | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Craig Topper | b45eabc | 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 | ba0a6ca | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 972 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 973 | KnownBits Known2(Known); |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 974 | switch (I->getOpcode()) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 975 | default: break; |
Rafael Espindola | 5319053 | 2012-03-30 15:52:11 +0000 | [diff] [blame] | 976 | case Instruction::Load: |
Florian Hahn | 19f9e32 | 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 | f42b23f | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 979 | computeKnownBitsFromRangeMetadata(*MD, Known); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 980 | break; |
Chris Lattner | 965c769 | 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 | b45eabc | 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 | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 987 | Known.One &= Known2.One; |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 989 | Known.Zero |= Known2.Zero; |
Philip Reames | 2d85874 | 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 | 6959b8e | 2018-04-27 21:23:20 +0000 | [diff] [blame] | 996 | Value *X = nullptr, *Y = nullptr; |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 997 | if (!Known.Zero[0] && !Known.One[0] && |
Roman Lebedev | 6959b8e | 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 | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 999 | Known2.resetAll(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1000 | computeKnownBits(Y, Known2, Depth + 1, Q); |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1001 | if (Known2.countMinTrailingOnes() > 0) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1002 | Known.Zero.setBit(0); |
Philip Reames | 2d85874 | 2015-11-10 18:46:14 +0000 | [diff] [blame] | 1003 | } |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1004 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1005 | } |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 1006 | case Instruction::Or: |
Craig Topper | b45eabc | 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 | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1011 | Known.Zero &= Known2.Zero; |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1013 | Known.One |= Known2.One; |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1014 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1015 | case Instruction::Xor: { |
Craig Topper | b45eabc | 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 | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1020 | APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One); |
Chris Lattner | 965c769 | 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 | b45eabc | 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 | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1024 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1025 | } |
| 1026 | case Instruction::Mul: { |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1027 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Craig Topper | b45eabc | 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 | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1030 | break; |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1036 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1037 | unsigned LeadZ = Known2.countMinLeadingZeros(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1038 | |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1039 | Known2.resetAll(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1040 | computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q); |
Craig Topper | 8df66c6 | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1044 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1045 | Known.Zero.setHighBits(LeadZ); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1046 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1047 | } |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1048 | case Instruction::Select: { |
Craig Topper | e953dec | 2017-04-13 20:39:37 +0000 | [diff] [blame] | 1049 | const Value *LHS, *RHS; |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1050 | SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor; |
| 1051 | if (SelectPatternResult::isMinOrMax(SPF)) { |
Craig Topper | b45eabc | 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 | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1054 | } else { |
Craig Topper | b45eabc | 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 | a19d0f2 | 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 | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1063 | if (Known.isNegative() && Known2.isNegative()) |
David Majnemer | a19d0f2 | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1066 | MaxHighOnes = |
| 1067 | std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes()); |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1068 | // If either side is non-negative, the result is non-negative. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1069 | else if (Known.isNonNegative() || Known2.isNonNegative()) |
David Majnemer | a19d0f2 | 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 | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1073 | if (Known.isNonNegative() && Known2.isNonNegative()) |
David Majnemer | a19d0f2 | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1076 | MaxHighZeros = std::max(Known.countMinLeadingZeros(), |
| 1077 | Known2.countMinLeadingZeros()); |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1078 | // If either side is negative, the result is negative. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1079 | else if (Known.isNegative() || Known2.isNegative()) |
David Majnemer | a19d0f2 | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1085 | std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes()); |
David Majnemer | a19d0f2 | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1090 | std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros()); |
Craig Topper | 8f77dca | 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 | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1095 | if (Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS))) |
Craig Topper | 8f77dca | 2018-05-25 19:18:09 +0000 | [diff] [blame] | 1096 | MaxHighZeros = 1; |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1099 | // Only known if known in both the LHS and RHS. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1100 | Known.One &= Known2.One; |
| 1101 | Known.Zero &= Known2.Zero; |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1102 | if (MaxHighOnes > 0) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1103 | Known.One.setHighBits(MaxHighOnes); |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1104 | if (MaxHighZeros > 0) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1105 | Known.Zero.setHighBits(MaxHighZeros); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1106 | break; |
David Majnemer | a19d0f2 | 2016-08-06 08:16:00 +0000 | [diff] [blame] | 1107 | } |
Chris Lattner | 965c769 | 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 | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1114 | break; // Can't work with floating point. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1115 | case Instruction::PtrToInt: |
| 1116 | case Instruction::IntToPtr: |
Justin Bogner | cd1d5aa | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1119 | case Instruction::ZExt: |
| 1120 | case Instruction::Trunc: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1121 | Type *SrcTy = I->getOperand(0)->getType(); |
Nadav Rotem | 15198e9 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1123 | unsigned SrcBitWidth; |
Chris Lattner | 965c769 | 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 | 945b7e5 | 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 | 15198e9 | 2012-10-26 17:17:05 +0000 | [diff] [blame] | 1130 | |
| 1131 | assert(SrcBitWidth && "SrcBitWidth can't be zero"); |
Craig Topper | d938fd1 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1132 | Known = Known.zextOrTrunc(SrcBitWidth); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1133 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Craig Topper | d938fd1 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1134 | Known = Known.zextOrTrunc(BitWidth); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1135 | // Any top bits are known to be zero. |
| 1136 | if (BitWidth > SrcBitWidth) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1137 | Known.Zero.setBitsFrom(SrcBitWidth); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1138 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1139 | } |
| 1140 | case Instruction::BitCast: { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1141 | Type *SrcTy = I->getOperand(0)->getType(); |
Vedant Kumar | b3091da | 2018-07-06 20:17:42 +0000 | [diff] [blame] | 1142 | if (SrcTy->isIntOrPtrTy() && |
Chris Lattner | edb8407 | 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 | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1145 | !I->getType()->isVectorTy()) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1146 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1147 | break; |
Chris Lattner | 965c769 | 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 | 0cdbc7a | 2009-09-08 00:13:52 +0000 | [diff] [blame] | 1153 | unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1154 | |
Craig Topper | d938fd1 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1155 | Known = Known.trunc(SrcBitWidth); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1156 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Chris Lattner | 965c769 | 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 | d938fd1 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 1159 | Known = Known.sext(BitWidth); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1160 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1161 | } |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1162 | case Instruction::Shl: { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1163 | // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0 |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1164 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Sam McCall | d0d43e6 | 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 | d7f9c35 | 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 | d0d43e6 | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1170 | if (NSW && KnownZero.isSignBitSet()) |
| 1171 | KZResult.setSignBit(); |
| 1172 | return KZResult; |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1173 | }; |
| 1174 | |
Sam McCall | d0d43e6 | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1183 | break; |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1184 | } |
| 1185 | case Instruction::LShr: { |
Sanjay Patel | b7d1238 | 2017-10-16 14:46:37 +0000 | [diff] [blame] | 1186 | // (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Sam McCall | d0d43e6 | 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 | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1192 | }; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1193 | |
Sam McCall | d0d43e6 | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1199 | break; |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1200 | } |
| 1201 | case Instruction::AShr: { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1202 | // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0 |
Sam McCall | d0d43e6 | 2017-12-04 12:51:49 +0000 | [diff] [blame] | 1203 | auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) { |
| 1204 | return KnownZero.ashr(ShiftAmt); |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1205 | }; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1206 | |
Sam McCall | d0d43e6 | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1212 | break; |
Hal Finkel | f2199b2 | 2015-10-23 20:37:08 +0000 | [diff] [blame] | 1213 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1214 | case Instruction::Sub: { |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1215 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1216 | computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1217 | Known, Known2, Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1218 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1219 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1220 | case Instruction::Add: { |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1221 | bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I)); |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1222 | computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1223 | Known, Known2, Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1224 | break; |
Chris Lattner | 965c769 | 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 | 26cd6bd | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1231 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1232 | |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1233 | // The low bits of the first operand are unchanged by the srem. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1234 | Known.Zero = Known2.Zero & LowBits; |
| 1235 | Known.One = Known2.One & LowBits; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1236 | |
Duncan Sands | 26cd6bd | 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 | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1239 | if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero)) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1240 | Known.Zero |= ~LowBits; |
Duncan Sands | 26cd6bd | 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 | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1244 | if (Known2.isNegative() && LowBits.intersects(Known2.One)) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1245 | Known.One |= ~LowBits; |
Duncan Sands | 26cd6bd | 2010-01-29 06:18:37 +0000 | [diff] [blame] | 1246 | |
Craig Topper | b45eabc | 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 | da886c6 | 2017-04-16 21:46:12 +0000 | [diff] [blame] | 1248 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
Nick Lewycky | e467979 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1254 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Craig Topper | da886c6 | 2017-04-16 21:46:12 +0000 | [diff] [blame] | 1255 | // If it's known zero, our sign bit is also zero. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 1256 | if (Known2.isNonNegative()) |
| 1257 | Known.makeNonNegative(); |
Nick Lewycky | e467979 | 2011-03-07 01:50:10 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 965c769 | 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 | 46e38f3 | 2016-06-08 10:01:20 +0000 | [diff] [blame] | 1262 | const APInt &RA = Rem->getValue(); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1263 | if (RA.isPowerOf2()) { |
| 1264 | APInt LowBits = (RA - 1); |
Craig Topper | b45eabc | 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 | 965c769 | 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 | b45eabc | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1276 | |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1277 | unsigned Leaders = |
| 1278 | std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros()); |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1279 | Known.resetAll(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1280 | Known.Zero.setHighBits(Leaders); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | } |
| 1283 | |
Victor Hernandez | a3aaf85 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 1284 | case Instruction::Alloca: { |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1285 | const AllocaInst *AI = cast<AllocaInst>(I); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1286 | unsigned Align = AI->getAlignment(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1287 | if (Align == 0) |
Eduard Burtescu | 90c4449 | 2016-01-18 00:10:01 +0000 | [diff] [blame] | 1288 | Align = Q.DL.getABITypeAlignment(AI->getAllocatedType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1289 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1290 | if (Align > 0) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1291 | Known.Zero.setLowBits(countTrailingZeros(Align)); |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1297 | KnownBits LocalKnown(BitWidth); |
| 1298 | computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q); |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1299 | unsigned TrailZ = LocalKnown.countMinTrailingZeros(); |
Chris Lattner | 965c769 | 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 | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 1304 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1305 | // Handle struct member offset arithmetic. |
Matt Arsenault | 74742a1 | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1315 | unsigned Idx = cast<ConstantInt>(Index)->getZExtValue(); |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1316 | const StructLayout *SL = Q.DL.getStructLayout(STy); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1317 | uint64_t Offset = SL->getElementOffset(Idx); |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1318 | TrailZ = std::min<unsigned>(TrailZ, |
| 1319 | countTrailingZeros(Offset)); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1320 | } else { |
| 1321 | // Handle array index arithmetic. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1322 | Type *IndexedTy = GTI.getIndexedType(); |
Jay Foad | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1323 | if (!IndexedTy->isSized()) { |
| 1324 | TrailZ = 0; |
| 1325 | break; |
| 1326 | } |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1327 | unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits(); |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1328 | uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy); |
Craig Topper | b45eabc | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1331 | TrailZ = std::min(TrailZ, |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1332 | unsigned(countTrailingZeros(TypeSize) + |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1333 | LocalKnown.countMinTrailingZeros())); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 1336 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1337 | Known.Zero.setLowBits(TrailZ); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | case Instruction::PHI: { |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1341 | const PHINode *P = cast<PHINode>(I); |
Chris Lattner | 965c769 | 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 | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1349 | Operator *LU = dyn_cast<Operator>(L); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1350 | if (!LU) |
| 1351 | continue; |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 1352 | unsigned Opcode = LU->getOpcode(); |
Chris Lattner | 965c769 | 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 | bc76eca | 2016-08-22 13:14:07 +0000 | [diff] [blame] | 1355 | // will have low zero bits. |
Chris Lattner | 965c769 | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1372 | computeKnownBits(R, Known2, Depth + 1, Q); |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1373 | |
| 1374 | // We need to take the minimum number of known bits |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1375 | KnownBits Known3(Known); |
| 1376 | computeKnownBits(L, Known3, Depth + 1, Q); |
David Greene | aebd9e0 | 2008-10-27 23:24:03 +0000 | [diff] [blame] | 1377 | |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1378 | Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(), |
| 1379 | Known3.countMinTrailingZeros())); |
Artur Pilipenko | c6eb6bd | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1380 | |
Artur Pilipenko | c6eb6bd | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1381 | auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU); |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1382 | if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) { |
Artur Pilipenko | c6eb6bd | 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 | ca48af3 | 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 | c6eb6bd | 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 | ca48af3 | 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 | c6eb6bd | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | // (mul nsw non-negative, non-negative) --> non-negative |
Craig Topper | ca48af3 | 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 | c6eb6bd | 2016-10-12 16:18:43 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1418 | |
Nick Lewycky | ac0b62c | 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 | 5a29c36 | 2014-05-15 12:12:55 +0000 | [diff] [blame] | 1421 | break; |
Nick Lewycky | ac0b62c | 2011-02-10 23:54:10 +0000 | [diff] [blame] | 1422 | |
Dan Gohman | bf0002e | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1425 | if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) { |
Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1426 | // Skip if every incoming value references to ourself. |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 1427 | if (dyn_cast_or_null<UndefValue>(P->hasConstantValue())) |
Duncan Sands | 7dc3d47 | 2011-03-08 12:39:03 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1430 | Known.Zero.setAllBits(); |
| 1431 | Known.One.setAllBits(); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1432 | for (Value *IncValue : P->incoming_values()) { |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1433 | // Skip direct self references. |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 1434 | if (IncValue == P) continue; |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1435 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1436 | Known2 = KnownBits(BitWidth); |
Dan Gohman | bf0002e | 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 | b45eabc | 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 | bf0002e | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1444 | if (!Known.Zero && !Known.One) |
Dan Gohman | bf0002e | 2009-05-21 02:28:33 +0000 | [diff] [blame] | 1445 | break; |
| 1446 | } |
| 1447 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1448 | break; |
| 1449 | } |
| 1450 | case Instruction::Call: |
Jingyue Wu | 37fcb59 | 2014-06-19 16:50:16 +0000 | [diff] [blame] | 1451 | case Instruction::Invoke: |
Hal Finkel | 6fd5e1f | 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 | 19f9e32 | 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 | f42b23f | 2017-04-28 06:28:56 +0000 | [diff] [blame] | 1457 | computeKnownBitsFromRangeMetadata(*MD, Known); |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1458 | if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) { |
Craig Topper | b45eabc | 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 | 6fd5e1f | 2016-07-11 02:25:14 +0000 | [diff] [blame] | 1462 | } |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1463 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1464 | switch (II->getIntrinsicID()) { |
| 1465 | default: break; |
Chad Rosier | 8520429 | 2017-01-17 17:23:51 +0000 | [diff] [blame] | 1466 | case Intrinsic::bitreverse: |
Craig Topper | b45eabc | 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 | 8520429 | 2017-01-17 17:23:51 +0000 | [diff] [blame] | 1470 | break; |
Philip Reames | 675418e | 2015-10-06 20:20:45 +0000 | [diff] [blame] | 1471 | case Intrinsic::bswap: |
Craig Topper | b45eabc | 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 | 675418e | 2015-10-06 20:20:45 +0000 | [diff] [blame] | 1475 | break; |
Craig Topper | 868813f | 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 | 4ee5747 | 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 | 868813f | 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 | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1495 | Known.Zero.setBitsFrom(LowBits); |
Benjamin Kramer | 4ee5747 | 2011-12-24 17:31:46 +0000 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | case Intrinsic::ctpop: { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1499 | computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q); |
Philip Reames | ddcf6b3 | 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 | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 1502 | unsigned BitsPossiblySet = Known2.countMaxPopulation(); |
Craig Topper | 66df10f | 2017-04-14 06:43:34 +0000 | [diff] [blame] | 1503 | unsigned LowBits = Log2_32(BitsPossiblySet)+1; |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1504 | Known.Zero.setBitsFrom(LowBits); |
Philip Reames | ddcf6b3 | 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 | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1507 | break; |
| 1508 | } |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 1509 | case Intrinsic::x86_sse42_crc32_64_64: |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1510 | Known.Zero.setBitsFrom(32); |
Evan Cheng | 2a746bf | 2011-05-22 18:25:30 +0000 | [diff] [blame] | 1511 | break; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | break; |
Bjorn Pettersson | 3961603 | 2016-10-06 09:56:21 +0000 | [diff] [blame] | 1515 | case Instruction::ExtractElement: |
| 1516 | // Look through extract element. At the moment we keep this simple and skip |
| 1517 | // tracking the specific element. But at least we might find information |
| 1518 | // valid for all elements of the vector (for example if vector is sign |
| 1519 | // extended, shifted, etc). |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1520 | computeKnownBits(I->getOperand(0), Known, Depth + 1, Q); |
Bjorn Pettersson | 3961603 | 2016-10-06 09:56:21 +0000 | [diff] [blame] | 1521 | break; |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1522 | case Instruction::ExtractValue: |
| 1523 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) { |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1524 | const ExtractValueInst *EVI = cast<ExtractValueInst>(I); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1525 | if (EVI->getNumIndices() != 1) break; |
| 1526 | if (EVI->getIndices()[0] == 0) { |
| 1527 | switch (II->getIntrinsicID()) { |
| 1528 | default: break; |
| 1529 | case Intrinsic::uadd_with_overflow: |
| 1530 | case Intrinsic::sadd_with_overflow: |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1531 | computeKnownBitsAddSub(true, II->getArgOperand(0), |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1532 | II->getArgOperand(1), false, Known, Known2, |
| 1533 | Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1534 | break; |
| 1535 | case Intrinsic::usub_with_overflow: |
| 1536 | case Intrinsic::ssub_with_overflow: |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1537 | computeKnownBitsAddSub(false, II->getArgOperand(0), |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1538 | II->getArgOperand(1), false, Known, Known2, |
| 1539 | Depth, Q); |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1540 | break; |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1541 | case Intrinsic::umul_with_overflow: |
| 1542 | case Intrinsic::smul_with_overflow: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1543 | computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false, |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1544 | Known, Known2, Depth, Q); |
Nick Lewycky | fa30607 | 2012-03-18 23:28:48 +0000 | [diff] [blame] | 1545 | break; |
Nick Lewycky | fea3e00 | 2012-03-09 09:23:50 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1549 | } |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /// Determine which bits of V are known to be either zero or one and return |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 1553 | /// them. |
| 1554 | KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) { |
| 1555 | KnownBits Known(getBitWidth(V->getType(), Q.DL)); |
| 1556 | computeKnownBits(V, Known, Depth, Q); |
| 1557 | return Known; |
| 1558 | } |
| 1559 | |
| 1560 | /// Determine which bits of V are known to be either zero or one and return |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1561 | /// them in the Known bit set. |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1562 | /// |
| 1563 | /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that |
| 1564 | /// we cannot optimize based on the assumption that it is zero without changing |
| 1565 | /// it to be an explicit zero. If we don't change it to zero, other code could |
| 1566 | /// optimized based on the contradictory assumption that it is non-zero. |
| 1567 | /// Because instcombine aggressively folds operations with undef args anyway, |
| 1568 | /// this won't lose us code quality. |
| 1569 | /// |
| 1570 | /// This function is defined on values with integer type, values with pointer |
| 1571 | /// type, and vectors of integers. In the case |
| 1572 | /// where V is a vector, known zero, and known one values are the |
| 1573 | /// same width as the vector element, and the bit is set only if it is true |
| 1574 | /// for all of the elements in the vector. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1575 | void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth, |
| 1576 | const Query &Q) { |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1577 | assert(V && "No Value?"); |
| 1578 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1579 | unsigned BitWidth = Known.getBitWidth(); |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1580 | |
Craig Topper | fde4723 | 2017-07-09 07:04:03 +0000 | [diff] [blame] | 1581 | assert((V->getType()->isIntOrIntVectorTy(BitWidth) || |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1582 | V->getType()->isPtrOrPtrVectorTy()) && |
Sanjay Patel | dba8b4c | 2016-06-02 20:01:37 +0000 | [diff] [blame] | 1583 | "Not integer or pointer type!"); |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 1584 | |
| 1585 | Type *ScalarTy = V->getType()->getScalarType(); |
| 1586 | unsigned ExpectedWidth = ScalarTy->isPointerTy() ? |
| 1587 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy); |
| 1588 | assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth"); |
Craig Topper | d73c6b4 | 2017-03-23 07:06:39 +0000 | [diff] [blame] | 1589 | (void)BitWidth; |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 1590 | (void)ExpectedWidth; |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1591 | |
Sanjay Patel | c96f6db | 2016-09-16 21:20:36 +0000 | [diff] [blame] | 1592 | const APInt *C; |
| 1593 | if (match(V, m_APInt(C))) { |
| 1594 | // We know all of the bits for a scalar constant or a splat vector constant! |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1595 | Known.One = *C; |
| 1596 | Known.Zero = ~Known.One; |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1597 | return; |
| 1598 | } |
| 1599 | // Null and aggregate-zero are all-zeros. |
Sanjay Patel | e8dc090 | 2016-05-23 17:57:54 +0000 | [diff] [blame] | 1600 | if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) { |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1601 | Known.setAllZero(); |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1602 | return; |
| 1603 | } |
| 1604 | // Handle a constant vector by taking the intersection of the known bits of |
David Majnemer | 3918cdd | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1605 | // each element. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1606 | if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) { |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1607 | // We know that CDS must be a vector of integers. Take the intersection of |
| 1608 | // each element. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1609 | Known.Zero.setAllBits(); Known.One.setAllBits(); |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1610 | for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { |
Craig Topper | b98ee58 | 2017-10-21 16:35:39 +0000 | [diff] [blame] | 1611 | APInt Elt = CDS->getElementAsAPInt(i); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1612 | Known.Zero &= ~Elt; |
| 1613 | Known.One &= Elt; |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1614 | } |
| 1615 | return; |
| 1616 | } |
| 1617 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1618 | if (const auto *CV = dyn_cast<ConstantVector>(V)) { |
David Majnemer | 3918cdd | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1619 | // We know that CV must be a vector of integers. Take the intersection of |
| 1620 | // each element. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1621 | Known.Zero.setAllBits(); Known.One.setAllBits(); |
David Majnemer | 3918cdd | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1622 | for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) { |
| 1623 | Constant *Element = CV->getAggregateElement(i); |
| 1624 | auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element); |
| 1625 | if (!ElementCI) { |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1626 | Known.resetAll(); |
David Majnemer | 3918cdd | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1627 | return; |
| 1628 | } |
Craig Topper | b98ee58 | 2017-10-21 16:35:39 +0000 | [diff] [blame] | 1629 | const APInt &Elt = ElementCI->getValue(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1630 | Known.Zero &= ~Elt; |
| 1631 | Known.One &= Elt; |
David Majnemer | 3918cdd | 2016-05-04 06:13:33 +0000 | [diff] [blame] | 1632 | } |
| 1633 | return; |
| 1634 | } |
| 1635 | |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1636 | // Start out not knowing anything. |
Craig Topper | f0aeee0 | 2017-05-05 17:36:09 +0000 | [diff] [blame] | 1637 | Known.resetAll(); |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1638 | |
Duncan P. N. Exon Smith | b1b208a | 2016-09-24 20:42:02 +0000 | [diff] [blame] | 1639 | // We can't imply anything about undefs. |
| 1640 | if (isa<UndefValue>(V)) |
| 1641 | return; |
| 1642 | |
| 1643 | // There's no point in looking through other users of ConstantData for |
| 1644 | // assumptions. Confirm that we've handled them all. |
| 1645 | assert(!isa<ConstantData>(V) && "Unhandled constant data!"); |
| 1646 | |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1647 | // Limit search depth. |
| 1648 | // All recursive calls that increase depth must come after this. |
| 1649 | if (Depth == MaxDepth) |
| 1650 | return; |
| 1651 | |
| 1652 | // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has |
| 1653 | // the bits of its aliasee. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1654 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 1655 | if (!GA->isInterposable()) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1656 | computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q); |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1657 | return; |
| 1658 | } |
| 1659 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1660 | if (const Operator *I = dyn_cast<Operator>(V)) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1661 | computeKnownBitsFromOperator(I, Known, Depth, Q); |
Sanjay Patel | a67559c | 2015-09-25 20:12:43 +0000 | [diff] [blame] | 1662 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1663 | // Aligned pointers have trailing zeros - refine Known.Zero set |
Artur Pilipenko | 029d853 | 2015-09-30 11:55:45 +0000 | [diff] [blame] | 1664 | if (V->getType()->isPointerTy()) { |
Artur Pilipenko | ae51afc | 2016-02-24 12:25:10 +0000 | [diff] [blame] | 1665 | unsigned Align = V->getPointerAlignment(Q.DL); |
Artur Pilipenko | 029d853 | 2015-09-30 11:55:45 +0000 | [diff] [blame] | 1666 | if (Align) |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1667 | Known.Zero.setLowBits(countTrailingZeros(Align)); |
Artur Pilipenko | 029d853 | 2015-09-30 11:55:45 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1670 | // computeKnownBitsFromAssume strictly refines Known. |
| 1671 | // Therefore, we run them after computeKnownBitsFromOperator. |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Check whether a nearby assume intrinsic can determine some known bits. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1674 | computeKnownBitsFromAssume(V, Known, Depth, Q); |
Jingyue Wu | 12b0c28 | 2015-06-15 05:46:29 +0000 | [diff] [blame] | 1675 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1676 | assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?"); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1679 | /// Return true if the given value is known to have exactly one |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1680 | /// bit set when defined. For vectors return true if every element is known to |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 1681 | /// be a power of two when defined. Supports values with integer or pointer |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1682 | /// types and vectors of integers. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1683 | bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1684 | const Query &Q) { |
Craig Topper | 7227eba | 2017-08-21 22:56:12 +0000 | [diff] [blame] | 1685 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
| 1686 | |
Simon Pilgrim | 9f2ae7e | 2018-02-06 18:39:23 +0000 | [diff] [blame] | 1687 | // Attempt to match against constants. |
| 1688 | if (OrZero && match(V, m_Power2OrZero())) |
| 1689 | return true; |
| 1690 | if (match(V, m_Power2())) |
| 1691 | return true; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1692 | |
| 1693 | // 1 << X is clearly a power of two if the one is not shifted off the end. If |
| 1694 | // it is shifted off the end then the result is undefined. |
| 1695 | if (match(V, m_Shl(m_One(), m_Value()))) |
| 1696 | return true; |
| 1697 | |
Craig Topper | bcfd2d1 | 2017-04-20 16:56:25 +0000 | [diff] [blame] | 1698 | // (signmask) >>l X is clearly a power of two if the one is not shifted off |
| 1699 | // the bottom. If it is shifted off the bottom then the result is undefined. |
| 1700 | if (match(V, m_LShr(m_SignMask(), m_Value()))) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1701 | return true; |
| 1702 | |
| 1703 | // The remaining tests are all recursive, so bail out if we hit the limit. |
| 1704 | if (Depth++ == MaxDepth) |
| 1705 | return false; |
| 1706 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1707 | Value *X = nullptr, *Y = nullptr; |
Sanjay Patel | 41160c2 | 2015-12-30 22:40:52 +0000 | [diff] [blame] | 1708 | // A shift left or a logical shift right of a power of two is a power of two |
| 1709 | // or zero. |
Duncan Sands | 985ba63 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1710 | if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) || |
Sanjay Patel | 41160c2 | 2015-12-30 22:40:52 +0000 | [diff] [blame] | 1711 | match(V, m_LShr(m_Value(X), m_Value())))) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1712 | return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q); |
Duncan Sands | 985ba63 | 2011-10-28 18:30:05 +0000 | [diff] [blame] | 1713 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1714 | if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V)) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1715 | return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1716 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1717 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1718 | return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) && |
| 1719 | isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q); |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1720 | |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1721 | if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) { |
| 1722 | // A power of two and'd with anything is a power of two or zero. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1723 | if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) || |
| 1724 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q)) |
Duncan Sands | ba286d7 | 2011-10-26 20:55:21 +0000 | [diff] [blame] | 1725 | return true; |
| 1726 | // X & (-X) is always a power of two or zero. |
| 1727 | if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X)))) |
| 1728 | return true; |
| 1729 | return false; |
| 1730 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1731 | |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1732 | // Adding a power-of-two or zero to the same power-of-two or zero yields |
| 1733 | // either the original power-of-two, a larger power-of-two or zero. |
| 1734 | if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1735 | const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V); |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1736 | if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) || |
| 1737 | Q.IIQ.hasNoSignedWrap(VOBO)) { |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1738 | if (match(X, m_And(m_Specific(Y), m_Value())) || |
| 1739 | match(X, m_And(m_Value(), m_Specific(Y)))) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1740 | if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q)) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1741 | return true; |
| 1742 | if (match(Y, m_And(m_Specific(X), m_Value())) || |
| 1743 | match(Y, m_And(m_Value(), m_Specific(X)))) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1744 | if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q)) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1745 | return true; |
| 1746 | |
| 1747 | unsigned BitWidth = V->getType()->getScalarSizeInBits(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1748 | KnownBits LHSBits(BitWidth); |
| 1749 | computeKnownBits(X, LHSBits, Depth, Q); |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1750 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1751 | KnownBits RHSBits(BitWidth); |
| 1752 | computeKnownBits(Y, RHSBits, Depth, Q); |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1753 | // If i8 V is a power of two or zero: |
| 1754 | // ZeroBits: 1 1 1 0 1 1 1 1 |
| 1755 | // ~ZeroBits: 0 0 0 1 0 0 0 0 |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1756 | if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2()) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1757 | // If OrZero isn't set, we cannot give back a zero result. |
| 1758 | // Make sure either the LHS or RHS has a bit set. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 1759 | if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue()) |
David Majnemer | b7d5409 | 2013-07-30 21:01:36 +0000 | [diff] [blame] | 1760 | return true; |
| 1761 | } |
| 1762 | } |
David Majnemer | beab567 | 2013-05-18 19:30:37 +0000 | [diff] [blame] | 1763 | |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1764 | // An exact divide or right shift can only shift off zero bits, so the result |
Nick Lewycky | f0469af | 2011-03-21 21:40:32 +0000 | [diff] [blame] | 1765 | // is a power of two only if the first operand is a power of two and not |
| 1766 | // copying a sign bit (sdiv int_min, 2). |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 1767 | if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) || |
| 1768 | match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) { |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 1769 | return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1770 | Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1773 | return false; |
| 1774 | } |
| 1775 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1776 | /// Test whether a GEP's result is known to be non-null. |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1777 | /// |
| 1778 | /// Uses properties inherent in a GEP to try to determine whether it is known |
| 1779 | /// to be non-null. |
| 1780 | /// |
| 1781 | /// Currently this routine does not support vector GEPs. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1782 | static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1783 | const Query &Q) { |
Manoj Gupta | 77eeac3 | 2018-07-09 22:27:23 +0000 | [diff] [blame] | 1784 | const Function *F = nullptr; |
| 1785 | if (const Instruction *I = dyn_cast<Instruction>(GEP)) |
| 1786 | F = I->getFunction(); |
| 1787 | |
| 1788 | if (!GEP->isInBounds() || |
| 1789 | NullPointerIsDefined(F, GEP->getPointerAddressSpace())) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1790 | return false; |
| 1791 | |
| 1792 | // FIXME: Support vector-GEPs. |
| 1793 | assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP"); |
| 1794 | |
| 1795 | // If the base pointer is non-null, we cannot walk to a null address with an |
| 1796 | // inbounds GEP in address space zero. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1797 | if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1798 | return true; |
| 1799 | |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1800 | // Walk the GEP operands and see if any operand introduces a non-zero offset. |
| 1801 | // If so, then the GEP cannot produce a null pointer, as doing so would |
| 1802 | // inherently violate the inbounds contract within address space zero. |
| 1803 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
| 1804 | GTI != GTE; ++GTI) { |
| 1805 | // Struct types are easy -- they must always be indexed by a constant. |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 1806 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1807 | ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand()); |
| 1808 | unsigned ElementIdx = OpC->getZExtValue(); |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1809 | const StructLayout *SL = Q.DL.getStructLayout(STy); |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1810 | uint64_t ElementOffset = SL->getElementOffset(ElementIdx); |
| 1811 | if (ElementOffset > 0) |
| 1812 | return true; |
| 1813 | continue; |
| 1814 | } |
| 1815 | |
| 1816 | // If we have a zero-sized type, the index doesn't matter. Keep looping. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1817 | if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1818 | continue; |
| 1819 | |
| 1820 | // Fast path the constant operand case both for efficiency and so we don't |
| 1821 | // increment Depth when just zipping down an all-constant GEP. |
| 1822 | if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) { |
| 1823 | if (!OpC->isZero()) |
| 1824 | return true; |
| 1825 | continue; |
| 1826 | } |
| 1827 | |
| 1828 | // We post-increment Depth here because while isKnownNonZero increments it |
| 1829 | // as well, when we pop back up that increment won't persist. We don't want |
| 1830 | // to recurse 10k times just because we have 10k GEP operands. We don't |
| 1831 | // bail completely out because we want to handle constant GEPs regardless |
| 1832 | // of depth. |
| 1833 | if (Depth++ >= MaxDepth) |
| 1834 | continue; |
| 1835 | |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 1836 | if (isKnownNonZero(GTI.getOperand(), Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 1837 | return true; |
| 1838 | } |
| 1839 | |
| 1840 | return false; |
| 1841 | } |
| 1842 | |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1843 | static bool isKnownNonNullFromDominatingCondition(const Value *V, |
| 1844 | const Instruction *CtxI, |
| 1845 | const DominatorTree *DT) { |
| 1846 | assert(V->getType()->isPointerTy() && "V must be pointer type"); |
| 1847 | assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull"); |
| 1848 | |
| 1849 | if (!CtxI || !DT) |
| 1850 | return false; |
| 1851 | |
| 1852 | unsigned NumUsesExplored = 0; |
| 1853 | for (auto *U : V->users()) { |
| 1854 | // Avoid massive lists |
| 1855 | if (NumUsesExplored >= DomConditionsMaxUses) |
| 1856 | break; |
| 1857 | NumUsesExplored++; |
| 1858 | |
| 1859 | // If the value is used as an argument to a call or invoke, then argument |
| 1860 | // attributes may provide an answer about null-ness. |
| 1861 | if (auto CS = ImmutableCallSite(U)) |
| 1862 | if (auto *CalledFunc = CS.getCalledFunction()) |
| 1863 | for (const Argument &Arg : CalledFunc->args()) |
| 1864 | if (CS.getArgOperand(Arg.getArgNo()) == V && |
| 1865 | Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI)) |
| 1866 | return true; |
| 1867 | |
| 1868 | // Consider only compare instructions uniquely controlling a branch |
| 1869 | CmpInst::Predicate Pred; |
| 1870 | if (!match(const_cast<User *>(U), |
| 1871 | m_c_ICmp(Pred, m_Specific(V), m_Zero())) || |
| 1872 | (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)) |
| 1873 | continue; |
| 1874 | |
Max Kazantsev | 2dbbd64 | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1875 | SmallVector<const User *, 4> WorkList; |
| 1876 | SmallPtrSet<const User *, 4> Visited; |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1877 | for (auto *CmpU : U->users()) { |
Max Kazantsev | 2dbbd64 | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1878 | assert(WorkList.empty() && "Should be!"); |
| 1879 | if (Visited.insert(CmpU).second) |
| 1880 | WorkList.push_back(CmpU); |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1881 | |
Max Kazantsev | 2dbbd64 | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1882 | while (!WorkList.empty()) { |
| 1883 | auto *Curr = WorkList.pop_back_val(); |
| 1884 | |
| 1885 | // If a user is an AND, add all its users to the work list. We only |
| 1886 | // propagate "pred != null" condition through AND because it is only |
| 1887 | // correct to assume that all conditions of AND are met in true branch. |
| 1888 | // TODO: Support similar logic of OR and EQ predicate? |
| 1889 | if (Pred == ICmpInst::ICMP_NE) |
| 1890 | if (auto *BO = dyn_cast<BinaryOperator>(Curr)) |
| 1891 | if (BO->getOpcode() == Instruction::And) { |
| 1892 | for (auto *BOU : BO->users()) |
| 1893 | if (Visited.insert(BOU).second) |
| 1894 | WorkList.push_back(BOU); |
| 1895 | continue; |
| 1896 | } |
| 1897 | |
| 1898 | if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) { |
| 1899 | assert(BI->isConditional() && "uses a comparison!"); |
| 1900 | |
| 1901 | BasicBlock *NonNullSuccessor = |
| 1902 | BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0); |
| 1903 | BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor); |
| 1904 | if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent())) |
| 1905 | return true; |
Max Kazantsev | 3c284bd | 2018-08-30 03:39:16 +0000 | [diff] [blame] | 1906 | } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) && |
Max Kazantsev | 2dbbd64 | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1907 | DT->dominates(cast<Instruction>(Curr), CtxI)) { |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1908 | return true; |
Max Kazantsev | 2dbbd64 | 2018-08-06 11:14:18 +0000 | [diff] [blame] | 1909 | } |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | return false; |
| 1915 | } |
| 1916 | |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1917 | /// Does the 'Range' metadata (which must be a valid MD_range operand list) |
| 1918 | /// ensure that the value it's attached to is never Value? 'RangeType' is |
| 1919 | /// is the type of the value described by the range. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1920 | static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) { |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1921 | const unsigned NumRanges = Ranges->getNumOperands() / 2; |
| 1922 | assert(NumRanges >= 1); |
| 1923 | for (unsigned i = 0; i < NumRanges; ++i) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1924 | ConstantInt *Lower = |
| 1925 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0)); |
| 1926 | ConstantInt *Upper = |
| 1927 | mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1)); |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1928 | ConstantRange Range(Lower->getValue(), Upper->getValue()); |
| 1929 | if (Range.contains(Value)) |
| 1930 | return false; |
| 1931 | } |
| 1932 | return true; |
| 1933 | } |
| 1934 | |
Sanjay Patel | 97e4b987 | 2017-02-12 15:35:34 +0000 | [diff] [blame] | 1935 | /// Return true if the given value is known to be non-zero when defined. For |
| 1936 | /// vectors, return true if every element is known to be non-zero when |
| 1937 | /// defined. For pointers, if the context instruction and dominator tree are |
| 1938 | /// specified, perform context-sensitive analysis and return true if the |
| 1939 | /// pointer couldn't possibly be null at the specified instruction. |
| 1940 | /// Supports values with integer or pointer type and vectors of integers. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 1941 | bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) { |
Sanjay Patel | 8ec7e7c | 2016-05-22 16:07:20 +0000 | [diff] [blame] | 1942 | if (auto *C = dyn_cast<Constant>(V)) { |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1943 | if (C->isNullValue()) |
| 1944 | return false; |
| 1945 | if (isa<ConstantInt>(C)) |
| 1946 | // Must be non-zero due to null test above. |
| 1947 | return true; |
Sanjay Patel | 23019d1 | 2016-05-24 14:18:49 +0000 | [diff] [blame] | 1948 | |
| 1949 | // For constant vectors, check that all elements are undefined or known |
| 1950 | // non-zero to determine that the whole vector is known non-zero. |
| 1951 | if (auto *VecTy = dyn_cast<VectorType>(C->getType())) { |
| 1952 | for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) { |
| 1953 | Constant *Elt = C->getAggregateElement(i); |
| 1954 | if (!Elt || Elt->isNullValue()) |
| 1955 | return false; |
| 1956 | if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt)) |
| 1957 | return false; |
| 1958 | } |
| 1959 | return true; |
| 1960 | } |
| 1961 | |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1962 | // A global variable in address space 0 is non null unless extern weak |
| 1963 | // or an absolute symbol reference. Other address spaces may have null as a |
| 1964 | // valid address for a global, so we can't assume anything. |
| 1965 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 1966 | if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() && |
| 1967 | GV->getType()->getAddressSpace() == 0) |
| 1968 | return true; |
| 1969 | } else |
| 1970 | return false; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Sanjay Patel | 8ec7e7c | 2016-05-22 16:07:20 +0000 | [diff] [blame] | 1973 | if (auto *I = dyn_cast<Instruction>(V)) { |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 1974 | if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) { |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1975 | // If the possible ranges don't contain zero, then the value is |
| 1976 | // definitely non-zero. |
Sanjay Patel | 8ec7e7c | 2016-05-22 16:07:20 +0000 | [diff] [blame] | 1977 | if (auto *Ty = dyn_cast<IntegerType>(V->getType())) { |
Philip Reames | 4cb4d3e | 2014-10-30 20:25:19 +0000 | [diff] [blame] | 1978 | const APInt ZeroValue(Ty->getBitWidth(), 0); |
| 1979 | if (rangeMetadataExcludesValue(Ranges, ZeroValue)) |
| 1980 | return true; |
| 1981 | } |
| 1982 | } |
| 1983 | } |
| 1984 | |
Karl-Johan Karlsson | ebaaa2d | 2018-05-30 15:56:46 +0000 | [diff] [blame] | 1985 | // Some of the tests below are recursive, so bail out if we hit the limit. |
| 1986 | if (Depth++ >= MaxDepth) |
| 1987 | return false; |
| 1988 | |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1989 | // Check for pointer simplifications. |
| 1990 | if (V->getType()->isPointerTy()) { |
| 1991 | // Alloca never returns null, malloc might. |
| 1992 | if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0) |
| 1993 | return true; |
| 1994 | |
| 1995 | // A byval, inalloca, or nonnull argument is never null. |
| 1996 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 1997 | if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr()) |
| 1998 | return true; |
| 1999 | |
| 2000 | // A Load tagged with nonnull metadata is never null. |
| 2001 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2002 | if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull)) |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2003 | return true; |
| 2004 | |
Piotr Padlewski | 5642a42 | 2018-05-18 23:54:33 +0000 | [diff] [blame] | 2005 | if (auto CS = ImmutableCallSite(V)) { |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2006 | if (CS.isReturnNonNull()) |
| 2007 | return true; |
Piotr Padlewski | d6f7346 | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 2008 | if (const auto *RP = getArgumentAliasingToReturnedPointer(CS)) |
Karl-Johan Karlsson | ebaaa2d | 2018-05-30 15:56:46 +0000 | [diff] [blame] | 2009 | return isKnownNonZero(RP, Depth, Q); |
Piotr Padlewski | 5642a42 | 2018-05-18 23:54:33 +0000 | [diff] [blame] | 2010 | } |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2013 | |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2014 | // Check for recursive pointer simplifications. |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 2015 | if (V->getType()->isPointerTy()) { |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2016 | if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT)) |
Sanjoy Das | 6082c1a | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 2017 | return true; |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 2018 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2019 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2020 | if (isGEPKnownNonNull(GEP, Depth, Q)) |
Chandler Carruth | 80d3e56 | 2012-12-07 02:08:58 +0000 | [diff] [blame] | 2021 | return true; |
| 2022 | } |
| 2023 | |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2024 | unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2025 | |
| 2026 | // X | Y != 0 if X != 0 or Y != 0. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2027 | Value *X = nullptr, *Y = nullptr; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2028 | if (match(V, m_Or(m_Value(X), m_Value(Y)))) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2029 | return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2030 | |
| 2031 | // ext X != 0 if X != 0. |
| 2032 | if (isa<SExtInst>(V) || isa<ZExtInst>(V)) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2033 | return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2034 | |
Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 2035 | // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2036 | // if the lowest bit is shifted off the end. |
Craig Topper | 6b3940a | 2017-05-03 22:25:19 +0000 | [diff] [blame] | 2037 | if (match(V, m_Shl(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2038 | // shl nuw can't remove any non-zero bits. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2039 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2040 | if (Q.IIQ.hasNoUnsignedWrap(BO)) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2041 | return isKnownNonZero(X, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2042 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2043 | KnownBits Known(BitWidth); |
| 2044 | computeKnownBits(X, Known, Depth, Q); |
| 2045 | if (Known.One[0]) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2046 | return true; |
| 2047 | } |
Duncan Sands | 2e9e4f1 | 2011-01-29 13:27:00 +0000 | [diff] [blame] | 2048 | // shr X, Y != 0 if X is negative. Note that the value of the shift is not |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2049 | // defined if the sign bit is shifted off the end. |
| 2050 | else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) { |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2051 | // shr exact can only shift out zero bits. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2052 | const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2053 | if (BO->isExact()) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2054 | return isKnownNonZero(X, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2055 | |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2056 | KnownBits Known = computeKnownBits(X, Depth, Q); |
| 2057 | if (Known.isNegative()) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2058 | return true; |
James Molloy | b6be1eb | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2059 | |
| 2060 | // If the shifter operand is a constant, and all of the bits shifted |
| 2061 | // out are known to be zero, and X is known non-zero then at least one |
| 2062 | // non-zero bit must remain. |
| 2063 | if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) { |
James Molloy | b6be1eb | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2064 | auto ShiftVal = Shift->getLimitedValue(BitWidth - 1); |
| 2065 | // Is there a known one in the portion not shifted out? |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 2066 | if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal) |
James Molloy | b6be1eb | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2067 | return true; |
| 2068 | // Are all the bits to be shifted out known zero? |
NAKAMURA Takumi | 76bab1f | 2017-07-11 02:31:51 +0000 | [diff] [blame] | 2069 | if (Known.countMinTrailingZeros() >= ShiftVal) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2070 | return isKnownNonZero(X, Depth, Q); |
James Molloy | b6be1eb | 2015-09-24 16:06:32 +0000 | [diff] [blame] | 2071 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2072 | } |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2073 | // div exact can only produce a zero if the dividend is zero. |
Benjamin Kramer | 9442cd0 | 2012-01-01 17:55:30 +0000 | [diff] [blame] | 2074 | else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) { |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2075 | return isKnownNonZero(X, Depth, Q); |
Nick Lewycky | c9aab85 | 2011-02-28 08:02:21 +0000 | [diff] [blame] | 2076 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2077 | // X + Y. |
| 2078 | else if (match(V, m_Add(m_Value(X), m_Value(Y)))) { |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2079 | KnownBits XKnown = computeKnownBits(X, Depth, Q); |
| 2080 | KnownBits YKnown = computeKnownBits(Y, Depth, Q); |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2081 | |
| 2082 | // If X and Y are both non-negative (as signed values) then their sum is not |
Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 2083 | // zero unless both X and Y are zero. |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2084 | if (XKnown.isNonNegative() && YKnown.isNonNegative()) |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2085 | if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q)) |
Duncan Sands | 9e9d5b2 | 2011-01-25 15:14:15 +0000 | [diff] [blame] | 2086 | return true; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2087 | |
| 2088 | // If X and Y are both negative (as signed values) then their sum is not |
| 2089 | // zero unless both X and Y equal INT_MIN. |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2090 | if (XKnown.isNegative() && YKnown.isNegative()) { |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2091 | APInt Mask = APInt::getSignedMaxValue(BitWidth); |
| 2092 | // The sign bit of X is set. If some other bit is set then X is not equal |
| 2093 | // to INT_MIN. |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2094 | if (XKnown.One.intersects(Mask)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2095 | return true; |
| 2096 | // The sign bit of Y is set. If some other bit is set then Y is not equal |
| 2097 | // to INT_MIN. |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2098 | if (YKnown.One.intersects(Mask)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2099 | return true; |
| 2100 | } |
| 2101 | |
| 2102 | // The sum of a non-negative number and a power of two is not zero. |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2103 | if (XKnown.isNonNegative() && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2104 | isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2105 | return true; |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 2106 | if (YKnown.isNonNegative() && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2107 | isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2108 | return true; |
| 2109 | } |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 2110 | // X * Y. |
| 2111 | else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) { |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2112 | const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V); |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 2113 | // If X and Y are non-zero then so is X * Y as long as the multiplication |
| 2114 | // does not overflow. |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2115 | if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) && |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2116 | isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q)) |
Duncan Sands | 7cb61e5 | 2011-10-27 19:16:21 +0000 | [diff] [blame] | 2117 | return true; |
| 2118 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2119 | // (C ? X : Y) != 0 if X != 0 and Y != 0. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2120 | else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2121 | if (isKnownNonZero(SI->getTrueValue(), Depth, Q) && |
| 2122 | isKnownNonZero(SI->getFalseValue(), Depth, Q)) |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2123 | return true; |
| 2124 | } |
James Molloy | 897048b | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2125 | // PHI |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2126 | else if (const PHINode *PN = dyn_cast<PHINode>(V)) { |
James Molloy | 897048b | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2127 | // Try and detect a recurrence that monotonically increases from a |
| 2128 | // starting value, as these are common as induction variables. |
| 2129 | if (PN->getNumIncomingValues() == 2) { |
| 2130 | Value *Start = PN->getIncomingValue(0); |
| 2131 | Value *Induction = PN->getIncomingValue(1); |
| 2132 | if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start)) |
| 2133 | std::swap(Start, Induction); |
| 2134 | if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) { |
| 2135 | if (!C->isZero() && !C->isNegative()) { |
| 2136 | ConstantInt *X; |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 2137 | if (Q.IIQ.UseInstrInfo && |
| 2138 | (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) || |
James Molloy | 897048b | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2139 | match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) && |
| 2140 | !X->isNegative()) |
| 2141 | return true; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
Jun Bum Lim | ca83266 | 2016-02-01 17:03:07 +0000 | [diff] [blame] | 2145 | // Check if all incoming values are non-zero constant. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2146 | bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) { |
Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 2147 | return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero(); |
Jun Bum Lim | ca83266 | 2016-02-01 17:03:07 +0000 | [diff] [blame] | 2148 | }); |
| 2149 | if (AllNonZeroConstants) |
| 2150 | return true; |
James Molloy | 897048b | 2015-09-29 14:08:45 +0000 | [diff] [blame] | 2151 | } |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2152 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2153 | KnownBits Known(BitWidth); |
| 2154 | computeKnownBits(V, Known, Depth, Q); |
| 2155 | return Known.One != 0; |
Duncan Sands | d395108 | 2011-01-25 09:38:29 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2158 | /// Return true if V2 == V1 + X, where X is known non-zero. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2159 | static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) { |
| 2160 | const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1); |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2161 | if (!BO || BO->getOpcode() != Instruction::Add) |
| 2162 | return false; |
| 2163 | Value *Op = nullptr; |
| 2164 | if (V2 == BO->getOperand(0)) |
| 2165 | Op = BO->getOperand(1); |
| 2166 | else if (V2 == BO->getOperand(1)) |
| 2167 | Op = BO->getOperand(0); |
| 2168 | else |
| 2169 | return false; |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2170 | return isKnownNonZero(Op, 0, Q); |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | /// Return true if it is known that V1 != V2. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2174 | static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) { |
Craig Topper | 3002d5b | 2017-06-06 07:13:15 +0000 | [diff] [blame] | 2175 | if (V1 == V2) |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2176 | return false; |
| 2177 | if (V1->getType() != V2->getType()) |
| 2178 | // We can't look through casts yet. |
| 2179 | return false; |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2180 | if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q)) |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2181 | return true; |
| 2182 | |
Craig Topper | 3002d5b | 2017-06-06 07:13:15 +0000 | [diff] [blame] | 2183 | if (V1->getType()->isIntOrIntVectorTy()) { |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2184 | // Are any known bits in V1 contradictory to known bits in V2? If V1 |
| 2185 | // has a known zero where V2 has a known one, they must not be equal. |
Craig Topper | 8e662f7 | 2017-06-06 07:13:11 +0000 | [diff] [blame] | 2186 | KnownBits Known1 = computeKnownBits(V1, 0, Q); |
| 2187 | KnownBits Known2 = computeKnownBits(V2, 0, Q); |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2188 | |
Craig Topper | 8365df8 | 2017-06-06 07:13:09 +0000 | [diff] [blame] | 2189 | if (Known1.Zero.intersects(Known2.One) || |
| 2190 | Known2.Zero.intersects(Known1.One)) |
James Molloy | 1d88d6f | 2015-10-22 13:18:42 +0000 | [diff] [blame] | 2191 | return true; |
| 2192 | } |
| 2193 | return false; |
| 2194 | } |
| 2195 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2196 | /// Return true if 'V & Mask' is known to be zero. We use this predicate to |
| 2197 | /// simplify operations downstream. Mask is known to be zero for bits that V |
| 2198 | /// cannot have. |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 2199 | /// |
| 2200 | /// This function is defined on values with integer type, values with pointer |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2201 | /// type, and vectors of integers. In the case |
Chris Lattner | 4bc2825 | 2009-09-08 00:06:16 +0000 | [diff] [blame] | 2202 | /// where V is a vector, the mask, known zero, and known one values are the |
| 2203 | /// same width as the vector element, and the bit is set only if it is true |
| 2204 | /// for all of the elements in the vector. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2205 | bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth, |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2206 | const Query &Q) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2207 | KnownBits Known(Mask.getBitWidth()); |
| 2208 | computeKnownBits(V, Known, Depth, Q); |
| 2209 | return Mask.isSubsetOf(Known.Zero); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Craig Topper | bec15b6 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2212 | // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow). |
| 2213 | // Returns the input and lower/upper bounds. |
| 2214 | static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, |
| 2215 | const APInt *&CLow, const APInt *&CHigh) { |
Craig Topper | 15f8692 | 2018-08-23 17:15:02 +0000 | [diff] [blame] | 2216 | assert(isa<Operator>(Select) && |
| 2217 | cast<Operator>(Select)->getOpcode() == Instruction::Select && |
Craig Topper | dfa176e | 2018-08-23 17:45:53 +0000 | [diff] [blame] | 2218 | "Input should be a Select!"); |
Craig Topper | bec15b6 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2219 | |
| 2220 | const Value *LHS, *RHS, *LHS2, *RHS2; |
| 2221 | SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor; |
| 2222 | if (SPF != SPF_SMAX && SPF != SPF_SMIN) |
| 2223 | return false; |
| 2224 | |
| 2225 | if (!match(RHS, m_APInt(CLow))) |
| 2226 | return false; |
| 2227 | |
| 2228 | SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor; |
| 2229 | if (getInverseMinMaxFlavor(SPF) != SPF2) |
| 2230 | return false; |
| 2231 | |
| 2232 | if (!match(RHS2, m_APInt(CHigh))) |
| 2233 | return false; |
| 2234 | |
| 2235 | if (SPF == SPF_SMIN) |
| 2236 | std::swap(CLow, CHigh); |
| 2237 | |
| 2238 | In = LHS2; |
| 2239 | return CLow->sle(*CHigh); |
| 2240 | } |
| 2241 | |
Sanjay Patel | a06d989 | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2242 | /// For vector constants, loop over the elements and find the constant with the |
| 2243 | /// minimum number of sign bits. Return 0 if the value is not a vector constant |
| 2244 | /// or if any element was not analyzed; otherwise, return the count for the |
| 2245 | /// element with the minimum number of sign bits. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2246 | static unsigned computeNumSignBitsVectorConstant(const Value *V, |
| 2247 | unsigned TyBits) { |
| 2248 | const auto *CV = dyn_cast<Constant>(V); |
Sanjay Patel | a06d989 | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2249 | if (!CV || !CV->getType()->isVectorTy()) |
| 2250 | return 0; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2251 | |
Sanjay Patel | a06d989 | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2252 | unsigned MinSignBits = TyBits; |
| 2253 | unsigned NumElts = CV->getType()->getVectorNumElements(); |
| 2254 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2255 | // If we find a non-ConstantInt, bail out. |
| 2256 | auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i)); |
| 2257 | if (!Elt) |
| 2258 | return 0; |
| 2259 | |
Craig Topper | 8e8b6ef | 2017-10-21 16:35:41 +0000 | [diff] [blame] | 2260 | MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits()); |
Sanjay Patel | a06d989 | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | return MinSignBits; |
| 2264 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2265 | |
Sanjoy Das | 39a684d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2266 | static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth, |
| 2267 | const Query &Q); |
| 2268 | |
| 2269 | static unsigned ComputeNumSignBits(const Value *V, unsigned Depth, |
| 2270 | const Query &Q) { |
| 2271 | unsigned Result = ComputeNumSignBitsImpl(V, Depth, Q); |
| 2272 | assert(Result > 0 && "At least one sign bit needs to be present!"); |
| 2273 | return Result; |
| 2274 | } |
| 2275 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2276 | /// Return the number of times the sign bit of the register is replicated into |
| 2277 | /// the other bits. We know that at least 1 bit is always equal to the sign bit |
| 2278 | /// (itself), but other cases can give us information. For example, immediately |
| 2279 | /// after an "ashr X, 2", we know that the top 3 bits are all equal to each |
Sanjay Patel | a06d989 | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2280 | /// other, so we return 3. For vectors, return the number of sign bits for the |
Vedant Kumar | d319674 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 2281 | /// vector element with the minimum number of known sign bits. |
Sanjoy Das | 39a684d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2282 | static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth, |
| 2283 | const Query &Q) { |
Craig Topper | 7227eba | 2017-08-21 22:56:12 +0000 | [diff] [blame] | 2284 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Sanjoy Das | 39a684d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2285 | |
| 2286 | // We return the minimum number of sign bits that are guaranteed to be present |
| 2287 | // in V, so for undef we have to conservatively return 1. We don't have the |
| 2288 | // same behavior for poison though -- that's a FIXME today. |
| 2289 | |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 2290 | Type *ScalarTy = V->getType()->getScalarType(); |
| 2291 | unsigned TyBits = ScalarTy->isPointerTy() ? |
| 2292 | Q.DL.getIndexTypeSizeInBits(ScalarTy) : |
| 2293 | Q.DL.getTypeSizeInBits(ScalarTy); |
| 2294 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2295 | unsigned Tmp, Tmp2; |
| 2296 | unsigned FirstAnswer = 1; |
| 2297 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2298 | // Note that ConstantInt is handled by the general computeKnownBits case |
Chris Lattner | 2e01a69 | 2008-06-02 18:39:07 +0000 | [diff] [blame] | 2299 | // below. |
| 2300 | |
Matt Arsenault | cb2a7eb | 2016-12-20 19:06:15 +0000 | [diff] [blame] | 2301 | if (Depth == MaxDepth) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2302 | return 1; // Limit search depth. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2303 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2304 | const Operator *U = dyn_cast<Operator>(V); |
Dan Gohman | 80ca01c | 2009-07-17 20:47:02 +0000 | [diff] [blame] | 2305 | switch (Operator::getOpcode(V)) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2306 | default: break; |
| 2307 | case Instruction::SExt: |
Mon P Wang | bb3eac9 | 2009-12-02 04:59:58 +0000 | [diff] [blame] | 2308 | Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits(); |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2309 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2310 | |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2311 | case Instruction::SDiv: { |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 2312 | const APInt *Denominator; |
| 2313 | // sdiv X, C -> adds log(C) sign bits. |
| 2314 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 2315 | |
| 2316 | // Ignore non-positive denominator. |
| 2317 | if (!Denominator->isStrictlyPositive()) |
| 2318 | break; |
| 2319 | |
| 2320 | // Calculate the incoming numerator bits. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2321 | unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 2322 | |
| 2323 | // Add floor(log(C)) bits to the numerator bits. |
| 2324 | return std::min(TyBits, NumBits + Denominator->logBase2()); |
| 2325 | } |
| 2326 | break; |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | case Instruction::SRem: { |
| 2330 | const APInt *Denominator; |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 2331 | // srem X, C -> we know that the result is within [-C+1,C) when C is a |
| 2332 | // positive constant. This let us put a lower bound on the number of sign |
| 2333 | // bits. |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2334 | if (match(U->getOperand(1), m_APInt(Denominator))) { |
| 2335 | |
| 2336 | // Ignore non-positive denominator. |
| 2337 | if (!Denominator->isStrictlyPositive()) |
| 2338 | break; |
| 2339 | |
| 2340 | // Calculate the incoming numerator bits. SRem by a positive constant |
| 2341 | // can't lower the number of sign bits. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2342 | unsigned NumrBits = |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2343 | ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Calculate the leading sign bit constraints by examining the |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 2346 | // denominator. Given that the denominator is positive, there are two |
| 2347 | // cases: |
| 2348 | // |
| 2349 | // 1. the numerator is positive. The result range is [0,C) and [0,C) u< |
| 2350 | // (1 << ceilLogBase2(C)). |
| 2351 | // |
| 2352 | // 2. the numerator is negative. Then the result range is (-C,0] and |
| 2353 | // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)). |
| 2354 | // |
| 2355 | // Thus a lower bound on the number of sign bits is `TyBits - |
| 2356 | // ceilLogBase2(C)`. |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2357 | |
Sanjoy Das | e561fee | 2015-03-25 22:33:53 +0000 | [diff] [blame] | 2358 | unsigned ResBits = TyBits - Denominator->ceilLogBase2(); |
Nadav Rotem | c99a387 | 2015-03-06 00:23:58 +0000 | [diff] [blame] | 2359 | return std::max(NumrBits, ResBits); |
| 2360 | } |
| 2361 | break; |
| 2362 | } |
Nadav Rotem | 029c5c7 | 2015-03-03 21:39:02 +0000 | [diff] [blame] | 2363 | |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2364 | case Instruction::AShr: { |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2365 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2366 | // ashr X, C -> adds C sign bits. Vectors too. |
| 2367 | const APInt *ShAmt; |
| 2368 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
Simon Pilgrim | 6720726 | 2018-01-01 22:44:59 +0000 | [diff] [blame] | 2369 | if (ShAmt->uge(TyBits)) |
Sanjoy Das | 39a684d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2370 | break; // Bad shift. |
Simon Pilgrim | 6720726 | 2018-01-01 22:44:59 +0000 | [diff] [blame] | 2371 | unsigned ShAmtLimited = ShAmt->getZExtValue(); |
Sanjoy Das | 39a684d | 2017-02-25 20:30:45 +0000 | [diff] [blame] | 2372 | Tmp += ShAmtLimited; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2373 | if (Tmp > TyBits) Tmp = TyBits; |
| 2374 | } |
| 2375 | return Tmp; |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2376 | } |
| 2377 | case Instruction::Shl: { |
| 2378 | const APInt *ShAmt; |
| 2379 | if (match(U->getOperand(1), m_APInt(ShAmt))) { |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2380 | // shl destroys sign bits. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2381 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Simon Pilgrim | 6720726 | 2018-01-01 22:44:59 +0000 | [diff] [blame] | 2382 | if (ShAmt->uge(TyBits) || // Bad shift. |
| 2383 | ShAmt->uge(Tmp)) break; // Shifted all sign bits out. |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2384 | Tmp2 = ShAmt->getZExtValue(); |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2385 | return Tmp - Tmp2; |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2386 | } |
| 2387 | break; |
Chris Lattner | 61a1d6c | 2012-01-26 21:37:55 +0000 | [diff] [blame] | 2388 | } |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2389 | case Instruction::And: |
| 2390 | case Instruction::Or: |
| 2391 | case Instruction::Xor: // NOT is handled here. |
| 2392 | // Logical binary ops preserve the number of sign bits at the worst. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2393 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2394 | if (Tmp != 1) { |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2395 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2396 | FirstAnswer = std::min(Tmp, Tmp2); |
| 2397 | // We computed what we know about the sign bits as our first |
| 2398 | // answer. Now proceed to the generic code that uses |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2399 | // computeKnownBits, and pick whichever answer is better. |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2400 | } |
| 2401 | break; |
| 2402 | |
Craig Topper | bec15b6 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2403 | case Instruction::Select: { |
| 2404 | // If we have a clamp pattern, we know that the number of sign bits will be |
| 2405 | // the minimum of the clamp min/max range. |
| 2406 | const Value *X; |
| 2407 | const APInt *CLow, *CHigh; |
| 2408 | if (isSignedMinMaxClamp(U, X, CLow, CHigh)) |
| 2409 | return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits()); |
| 2410 | |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2411 | Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2412 | if (Tmp == 1) break; |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2413 | Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2414 | return std::min(Tmp, Tmp2); |
Craig Topper | bec15b6 | 2018-08-22 23:27:50 +0000 | [diff] [blame] | 2415 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2416 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2417 | case Instruction::Add: |
| 2418 | // Add can have at most one carry bit. Thus we know that the output |
| 2419 | // is, at worst, one more bit than the inputs. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2420 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2421 | if (Tmp == 1) break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2422 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2423 | // Special case decrementing a value (ADD X, -1): |
David Majnemer | a55027f | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2424 | if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1))) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2425 | if (CRHS->isAllOnesValue()) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2426 | KnownBits Known(TyBits); |
| 2427 | computeKnownBits(U->getOperand(0), Known, Depth + 1, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2429 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2430 | // sign bits set. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2431 | if ((Known.Zero | 1).isAllOnesValue()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2432 | return TyBits; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2433 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2434 | // If we are subtracting one from a positive number, there is no carry |
| 2435 | // out of the result. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 2436 | if (Known.isNonNegative()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2437 | return Tmp; |
| 2438 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2439 | |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2440 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2441 | if (Tmp2 == 1) break; |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2442 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2443 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2444 | case Instruction::Sub: |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2445 | Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2446 | if (Tmp2 == 1) break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2448 | // Handle NEG. |
David Majnemer | a55027f | 2014-12-26 09:20:17 +0000 | [diff] [blame] | 2449 | if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0))) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2450 | if (CLHS->isNullValue()) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2451 | KnownBits Known(TyBits); |
| 2452 | computeKnownBits(U->getOperand(1), Known, Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2453 | // If the input is known to be 0 or 1, the output is 0/-1, which is all |
| 2454 | // sign bits set. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2455 | if ((Known.Zero | 1).isAllOnesValue()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2456 | return TyBits; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2458 | // If the input is known to be positive (the sign bit is known clear), |
| 2459 | // the output of the NEG has the same number of sign bits as the input. |
Craig Topper | ca48af3 | 2017-04-29 16:43:11 +0000 | [diff] [blame] | 2460 | if (Known.isNonNegative()) |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2461 | return Tmp2; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2462 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2463 | // Otherwise, we treat this like a SUB. |
| 2464 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2465 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2466 | // Sub can have at most one carry bit. Thus we know that the output |
| 2467 | // is, at worst, one more bit than the inputs. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2468 | Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2469 | if (Tmp == 1) break; |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2470 | return std::min(Tmp, Tmp2)-1; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2471 | |
Amjad Aboud | 88ffa3a | 2017-08-18 22:56:55 +0000 | [diff] [blame] | 2472 | case Instruction::Mul: { |
| 2473 | // The output of the Mul can be at most twice the valid bits in the inputs. |
| 2474 | unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2475 | if (SignBitsOp0 == 1) break; |
Amjad Aboud | 88ffa3a | 2017-08-18 22:56:55 +0000 | [diff] [blame] | 2476 | unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q); |
Stanislav Mekhanoshin | b8269a9 | 2018-07-25 16:39:24 +0000 | [diff] [blame] | 2477 | if (SignBitsOp1 == 1) break; |
Amjad Aboud | 88ffa3a | 2017-08-18 22:56:55 +0000 | [diff] [blame] | 2478 | unsigned OutValidBits = |
| 2479 | (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1); |
| 2480 | return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1; |
| 2481 | } |
| 2482 | |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2483 | case Instruction::PHI: { |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 2484 | const PHINode *PN = cast<PHINode>(U); |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2485 | unsigned NumIncomingValues = PN->getNumIncomingValues(); |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2486 | // Don't analyze large in-degree PHIs. |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2487 | if (NumIncomingValues > 4) break; |
| 2488 | // Unreachable blocks may have zero-operand PHI nodes. |
| 2489 | if (NumIncomingValues == 0) break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2490 | |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2491 | // Take the minimum of all incoming values. This can't infinitely loop |
| 2492 | // because of our depth threshold. |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2493 | Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q); |
David Majnemer | 6ee8d17 | 2015-01-04 07:06:53 +0000 | [diff] [blame] | 2494 | for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) { |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2495 | if (Tmp == 1) return Tmp; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2496 | Tmp = std::min( |
Matthias Braun | feb81bc | 2016-01-15 22:22:04 +0000 | [diff] [blame] | 2497 | Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q)); |
Chris Lattner | 35d3b9d | 2010-01-07 23:44:37 +0000 | [diff] [blame] | 2498 | } |
| 2499 | return Tmp; |
| 2500 | } |
| 2501 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2502 | case Instruction::Trunc: |
| 2503 | // FIXME: it's tricky to do anything useful for this, but it is an important |
| 2504 | // case for targets like X86. |
| 2505 | break; |
Bjorn Pettersson | 3961603 | 2016-10-06 09:56:21 +0000 | [diff] [blame] | 2506 | |
| 2507 | case Instruction::ExtractElement: |
| 2508 | // Look through extract element. At the moment we keep this simple and skip |
| 2509 | // tracking the specific element. But at least we might find information |
| 2510 | // valid for all elements of the vector (for example if vector is sign |
| 2511 | // extended, shifted, etc). |
| 2512 | return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2513 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2514 | |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2515 | // Finally, if we can prove that the top bits of the result are 0's or 1's, |
| 2516 | // use this information. |
Sanjay Patel | a06d989 | 2016-06-22 19:20:59 +0000 | [diff] [blame] | 2517 | |
| 2518 | // If we can examine all elements of a vector constant successfully, we're |
| 2519 | // done (we can't do any better than that). If not, keep trying. |
| 2520 | if (unsigned VecSignBits = computeNumSignBitsVectorConstant(V, TyBits)) |
| 2521 | return VecSignBits; |
| 2522 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 2523 | KnownBits Known(TyBits); |
| 2524 | computeKnownBits(V, Known, Depth, Q); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2525 | |
Sanjay Patel | e053621 | 2016-06-23 17:41:59 +0000 | [diff] [blame] | 2526 | // If we know that the sign bit is either zero or one, determine the number of |
| 2527 | // identical bits in the top of the input value. |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 2528 | return std::max(FirstAnswer, Known.countMinSignBits()); |
Chris Lattner | 965c769 | 2008-06-02 01:18:21 +0000 | [diff] [blame] | 2529 | } |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2530 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2531 | /// This function computes the integer multiple of Base that equals V. |
| 2532 | /// If successful, it returns true and returns the multiple in |
| 2533 | /// Multiple. If unsuccessful, it returns false. It looks |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2534 | /// through SExt instructions only if LookThroughSExt is true. |
| 2535 | bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2536 | bool LookThroughSExt, unsigned Depth) { |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2537 | const unsigned MaxDepth = 6; |
| 2538 | |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2539 | assert(V && "No Value?"); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2540 | assert(Depth <= MaxDepth && "Limit Search Depth"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2541 | assert(V->getType()->isIntegerTy() && "Not integer or pointer type!"); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2542 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2543 | Type *T = V->getType(); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2544 | |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2545 | ConstantInt *CI = dyn_cast<ConstantInt>(V); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2546 | |
| 2547 | if (Base == 0) |
| 2548 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2549 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2550 | if (Base == 1) { |
| 2551 | Multiple = V; |
| 2552 | return true; |
| 2553 | } |
| 2554 | |
| 2555 | ConstantExpr *CO = dyn_cast<ConstantExpr>(V); |
| 2556 | Constant *BaseVal = ConstantInt::get(T, Base); |
| 2557 | if (CO && CO == BaseVal) { |
| 2558 | // Multiple is 1. |
| 2559 | Multiple = ConstantInt::get(T, 1); |
| 2560 | return true; |
| 2561 | } |
| 2562 | |
| 2563 | if (CI && CI->getZExtValue() % Base == 0) { |
| 2564 | Multiple = ConstantInt::get(T, CI->getZExtValue() / Base); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2565 | return true; |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2566 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2567 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2568 | if (Depth == MaxDepth) return false; // Limit search depth. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2569 | |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2570 | Operator *I = dyn_cast<Operator>(V); |
| 2571 | if (!I) return false; |
| 2572 | |
| 2573 | switch (I->getOpcode()) { |
| 2574 | default: break; |
Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2575 | case Instruction::SExt: |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2576 | if (!LookThroughSExt) return false; |
| 2577 | // otherwise fall through to ZExt |
Galina Kistanova | 244621f | 2017-05-31 22:16:24 +0000 | [diff] [blame] | 2578 | LLVM_FALLTHROUGH; |
Chris Lattner | 4f0b47d | 2009-11-26 01:50:12 +0000 | [diff] [blame] | 2579 | case Instruction::ZExt: |
Dan Gohman | 6a976bb | 2009-11-18 00:58:27 +0000 | [diff] [blame] | 2580 | return ComputeMultiple(I->getOperand(0), Base, Multiple, |
| 2581 | LookThroughSExt, Depth+1); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2582 | case Instruction::Shl: |
| 2583 | case Instruction::Mul: { |
| 2584 | Value *Op0 = I->getOperand(0); |
| 2585 | Value *Op1 = I->getOperand(1); |
| 2586 | |
| 2587 | if (I->getOpcode() == Instruction::Shl) { |
| 2588 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
| 2589 | if (!Op1CI) return false; |
| 2590 | // Turn Op0 << Op1 into Op0 * 2^Op1 |
| 2591 | APInt Op1Int = Op1CI->getValue(); |
| 2592 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2593 | APInt API(Op1Int.getBitWidth(), 0); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 2594 | API.setBit(BitToSet); |
Jay Foad | 15084f0 | 2010-11-30 09:02:01 +0000 | [diff] [blame] | 2595 | Op1 = ConstantInt::get(V->getContext(), API); |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2598 | Value *Mul0 = nullptr; |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2599 | if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) { |
| 2600 | if (Constant *Op1C = dyn_cast<Constant>(Op1)) |
| 2601 | if (Constant *MulC = dyn_cast<Constant>(Mul0)) { |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2602 | if (Op1C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2603 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2604 | Op1C = ConstantExpr::getZExt(Op1C, MulC->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2605 | if (Op1C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2606 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2607 | MulC = ConstantExpr::getZExt(MulC, Op1C->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2608 | |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2609 | // V == Base * (Mul0 * Op1), so return (Mul0 * Op1) |
| 2610 | Multiple = ConstantExpr::getMul(MulC, Op1C); |
| 2611 | return true; |
| 2612 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2613 | |
| 2614 | if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0)) |
| 2615 | if (Mul0CI->getValue() == 1) { |
| 2616 | // V == Base * Op1, so return Op1 |
| 2617 | Multiple = Op1; |
| 2618 | return true; |
| 2619 | } |
| 2620 | } |
| 2621 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 2622 | Value *Mul1 = nullptr; |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2623 | if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) { |
| 2624 | if (Constant *Op0C = dyn_cast<Constant>(Op0)) |
| 2625 | if (Constant *MulC = dyn_cast<Constant>(Mul1)) { |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2626 | if (Op0C->getType()->getPrimitiveSizeInBits() < |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2627 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2628 | Op0C = ConstantExpr::getZExt(Op0C, MulC->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2629 | if (Op0C->getType()->getPrimitiveSizeInBits() > |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2630 | MulC->getType()->getPrimitiveSizeInBits()) |
| 2631 | MulC = ConstantExpr::getZExt(MulC, Op0C->getType()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2632 | |
Chris Lattner | 72d283c | 2010-09-05 17:20:46 +0000 | [diff] [blame] | 2633 | // V == Base * (Mul1 * Op0), so return (Mul1 * Op0) |
| 2634 | Multiple = ConstantExpr::getMul(MulC, Op0C); |
| 2635 | return true; |
| 2636 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2637 | |
| 2638 | if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1)) |
| 2639 | if (Mul1CI->getValue() == 1) { |
| 2640 | // V == Base * Op0, so return Op0 |
| 2641 | Multiple = Op0; |
| 2642 | return true; |
| 2643 | } |
| 2644 | } |
Victor Hernandez | 4744488 | 2009-11-10 08:28:35 +0000 | [diff] [blame] | 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | // We could not determine if V is a multiple of Base. |
| 2649 | return false; |
| 2650 | } |
| 2651 | |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2652 | Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS, |
| 2653 | const TargetLibraryInfo *TLI) { |
| 2654 | const Function *F = ICS.getCalledFunction(); |
| 2655 | if (!F) |
| 2656 | return Intrinsic::not_intrinsic; |
| 2657 | |
| 2658 | if (F->isIntrinsic()) |
| 2659 | return F->getIntrinsicID(); |
| 2660 | |
| 2661 | if (!TLI) |
| 2662 | return Intrinsic::not_intrinsic; |
| 2663 | |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2664 | LibFunc Func; |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2665 | // We're going to make assumptions on the semantics of the functions, check |
| 2666 | // that the target knows that it's available in this environment and it does |
| 2667 | // not have local linkage. |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2668 | if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func)) |
| 2669 | return Intrinsic::not_intrinsic; |
| 2670 | |
| 2671 | if (!ICS.onlyReadsMemory()) |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2672 | return Intrinsic::not_intrinsic; |
| 2673 | |
| 2674 | // Otherwise check if we have a call to a function that can be turned into a |
| 2675 | // vector intrinsic. |
| 2676 | switch (Func) { |
| 2677 | default: |
| 2678 | break; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2679 | case LibFunc_sin: |
| 2680 | case LibFunc_sinf: |
| 2681 | case LibFunc_sinl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2682 | return Intrinsic::sin; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2683 | case LibFunc_cos: |
| 2684 | case LibFunc_cosf: |
| 2685 | case LibFunc_cosl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2686 | return Intrinsic::cos; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2687 | case LibFunc_exp: |
| 2688 | case LibFunc_expf: |
| 2689 | case LibFunc_expl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2690 | return Intrinsic::exp; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2691 | case LibFunc_exp2: |
| 2692 | case LibFunc_exp2f: |
| 2693 | case LibFunc_exp2l: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2694 | return Intrinsic::exp2; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2695 | case LibFunc_log: |
| 2696 | case LibFunc_logf: |
| 2697 | case LibFunc_logl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2698 | return Intrinsic::log; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2699 | case LibFunc_log10: |
| 2700 | case LibFunc_log10f: |
| 2701 | case LibFunc_log10l: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2702 | return Intrinsic::log10; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2703 | case LibFunc_log2: |
| 2704 | case LibFunc_log2f: |
| 2705 | case LibFunc_log2l: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2706 | return Intrinsic::log2; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2707 | case LibFunc_fabs: |
| 2708 | case LibFunc_fabsf: |
| 2709 | case LibFunc_fabsl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2710 | return Intrinsic::fabs; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2711 | case LibFunc_fmin: |
| 2712 | case LibFunc_fminf: |
| 2713 | case LibFunc_fminl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2714 | return Intrinsic::minnum; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2715 | case LibFunc_fmax: |
| 2716 | case LibFunc_fmaxf: |
| 2717 | case LibFunc_fmaxl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2718 | return Intrinsic::maxnum; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2719 | case LibFunc_copysign: |
| 2720 | case LibFunc_copysignf: |
| 2721 | case LibFunc_copysignl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2722 | return Intrinsic::copysign; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2723 | case LibFunc_floor: |
| 2724 | case LibFunc_floorf: |
| 2725 | case LibFunc_floorl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2726 | return Intrinsic::floor; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2727 | case LibFunc_ceil: |
| 2728 | case LibFunc_ceilf: |
| 2729 | case LibFunc_ceill: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2730 | return Intrinsic::ceil; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2731 | case LibFunc_trunc: |
| 2732 | case LibFunc_truncf: |
| 2733 | case LibFunc_truncl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2734 | return Intrinsic::trunc; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2735 | case LibFunc_rint: |
| 2736 | case LibFunc_rintf: |
| 2737 | case LibFunc_rintl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2738 | return Intrinsic::rint; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2739 | case LibFunc_nearbyint: |
| 2740 | case LibFunc_nearbyintf: |
| 2741 | case LibFunc_nearbyintl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2742 | return Intrinsic::nearbyint; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2743 | case LibFunc_round: |
| 2744 | case LibFunc_roundf: |
| 2745 | case LibFunc_roundl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2746 | return Intrinsic::round; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2747 | case LibFunc_pow: |
| 2748 | case LibFunc_powf: |
| 2749 | case LibFunc_powl: |
Ahmed Bougacha | d765a82 | 2016-04-27 19:04:35 +0000 | [diff] [blame] | 2750 | return Intrinsic::pow; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 2751 | case LibFunc_sqrt: |
| 2752 | case LibFunc_sqrtf: |
| 2753 | case LibFunc_sqrtl: |
Sanjay Patel | 86d24f1 | 2017-11-06 22:40:09 +0000 | [diff] [blame] | 2754 | return Intrinsic::sqrt; |
David Majnemer | b4b2723 | 2016-04-19 19:10:21 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | return Intrinsic::not_intrinsic; |
| 2758 | } |
| 2759 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 2760 | /// Return true if we can prove that the specified FP value is never equal to |
| 2761 | /// -0.0. |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2762 | /// |
| 2763 | /// NOTE: this function will need to be revisited when we support non-default |
| 2764 | /// rounding modes! |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2765 | bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI, |
| 2766 | unsigned Depth) { |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2767 | if (auto *CFP = dyn_cast<ConstantFP>(V)) |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2768 | return !CFP->getValueAPF().isNegZero(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2769 | |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2770 | // Limit search depth. |
Matt Arsenault | cb2a7eb | 2016-12-20 19:06:15 +0000 | [diff] [blame] | 2771 | if (Depth == MaxDepth) |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2772 | return false; |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2773 | |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2774 | auto *Op = dyn_cast<Operator>(V); |
| 2775 | if (!Op) |
| 2776 | return false; |
Michael Ilseman | 0f12837 | 2012-12-06 00:07:09 +0000 | [diff] [blame] | 2777 | |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2778 | // Check if the nsz fast-math flag is set. |
| 2779 | if (auto *FPO = dyn_cast<FPMathOperator>(Op)) |
Michael Ilseman | 0f12837 | 2012-12-06 00:07:09 +0000 | [diff] [blame] | 2780 | if (FPO->hasNoSignedZeros()) |
| 2781 | return true; |
| 2782 | |
Sanjay Patel | 9e3d8f4 | 2017-11-13 17:40:47 +0000 | [diff] [blame] | 2783 | // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0. |
Sanjay Patel | 93e64dd | 2018-03-25 21:16:33 +0000 | [diff] [blame] | 2784 | if (match(Op, m_FAdd(m_Value(), m_PosZeroFP()))) |
Sanjay Patel | 9e3d8f4 | 2017-11-13 17:40:47 +0000 | [diff] [blame] | 2785 | return true; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2787 | // sitofp and uitofp turn into +0.0 for zero. |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2788 | if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op)) |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2789 | return true; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2790 | |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2791 | if (auto *Call = dyn_cast<CallInst>(Op)) { |
| 2792 | Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI); |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2793 | switch (IID) { |
| 2794 | default: |
| 2795 | break; |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2796 | // sqrt(-0.0) = -0.0, no other negative results are possible. |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2797 | case Intrinsic::sqrt: |
Matt Arsenault | 56b31d8 | 2018-08-06 15:16:26 +0000 | [diff] [blame] | 2798 | case Intrinsic::canonicalize: |
Sanjay Patel | 20df88a | 2017-11-13 17:56:23 +0000 | [diff] [blame] | 2799 | return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1); |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2800 | // fabs(x) != -0.0 |
| 2801 | case Intrinsic::fabs: |
| 2802 | return true; |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2803 | } |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2804 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 2805 | |
Chris Lattner | a12a6de | 2008-06-02 01:29:46 +0000 | [diff] [blame] | 2806 | return false; |
| 2807 | } |
| 2808 | |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2809 | /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a |
| 2810 | /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign |
| 2811 | /// bit despite comparing equal. |
| 2812 | static bool cannotBeOrderedLessThanZeroImpl(const Value *V, |
| 2813 | const TargetLibraryInfo *TLI, |
| 2814 | bool SignBitOnly, |
| 2815 | unsigned Depth) { |
Justin Lebar | 322c127 | 2017-01-27 00:58:34 +0000 | [diff] [blame] | 2816 | // TODO: This function does not do the right thing when SignBitOnly is true |
| 2817 | // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform |
| 2818 | // which flips the sign bits of NaNs. See |
| 2819 | // https://llvm.org/bugs/show_bug.cgi?id=31702. |
| 2820 | |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2821 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 2822 | return !CFP->getValueAPF().isNegative() || |
| 2823 | (!SignBitOnly && CFP->getValueAPF().isZero()); |
| 2824 | } |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2825 | |
Craig Topper | 69c8972 | 2018-02-26 22:33:17 +0000 | [diff] [blame] | 2826 | // Handle vector of constants. |
| 2827 | if (auto *CV = dyn_cast<Constant>(V)) { |
| 2828 | if (CV->getType()->isVectorTy()) { |
| 2829 | unsigned NumElts = CV->getType()->getVectorNumElements(); |
| 2830 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2831 | auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i)); |
| 2832 | if (!CFP) |
| 2833 | return false; |
| 2834 | if (CFP->getValueAPF().isNegative() && |
| 2835 | (SignBitOnly || !CFP->getValueAPF().isZero())) |
| 2836 | return false; |
| 2837 | } |
| 2838 | |
| 2839 | // All non-negative ConstantFPs. |
| 2840 | return true; |
| 2841 | } |
| 2842 | } |
| 2843 | |
Matt Arsenault | cb2a7eb | 2016-12-20 19:06:15 +0000 | [diff] [blame] | 2844 | if (Depth == MaxDepth) |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2845 | return false; // Limit search depth. |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2846 | |
| 2847 | const Operator *I = dyn_cast<Operator>(V); |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2848 | if (!I) |
| 2849 | return false; |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2850 | |
| 2851 | switch (I->getOpcode()) { |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2852 | default: |
| 2853 | break; |
Fiona Glaser | db7824f | 2016-01-12 23:37:30 +0000 | [diff] [blame] | 2854 | // Unsigned integers are always nonnegative. |
| 2855 | case Instruction::UIToFP: |
| 2856 | return true; |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2857 | case Instruction::FMul: |
| 2858 | // x*x is always non-negative or a NaN. |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2859 | if (I->getOperand(0) == I->getOperand(1) && |
| 2860 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs())) |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2861 | return true; |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2862 | |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 2863 | LLVM_FALLTHROUGH; |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2864 | case Instruction::FAdd: |
| 2865 | case Instruction::FDiv: |
| 2866 | case Instruction::FRem: |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2867 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2868 | Depth + 1) && |
| 2869 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2870 | Depth + 1); |
Fiona Glaser | db7824f | 2016-01-12 23:37:30 +0000 | [diff] [blame] | 2871 | case Instruction::Select: |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2872 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2873 | Depth + 1) && |
| 2874 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, |
| 2875 | Depth + 1); |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2876 | case Instruction::FPExt: |
| 2877 | case Instruction::FPTrunc: |
| 2878 | // Widening/narrowing never change sign. |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2879 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2880 | Depth + 1); |
Craig Topper | 3019910 | 2018-02-27 19:53:45 +0000 | [diff] [blame] | 2881 | case Instruction::ExtractElement: |
| 2882 | // Look through extract element. At the moment we keep this simple and skip |
| 2883 | // tracking the specific element. But at least we might find information |
| 2884 | // valid for all elements of the vector. |
| 2885 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2886 | Depth + 1); |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2887 | case Instruction::Call: |
Justin Lebar | 7e3184c | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2888 | const auto *CI = cast<CallInst>(I); |
| 2889 | Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI); |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2890 | switch (IID) { |
| 2891 | default: |
| 2892 | break; |
| 2893 | case Intrinsic::maxnum: |
Matt Arsenault | d54b7f0 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 2894 | return (isKnownNeverNaN(I->getOperand(0), TLI) && |
Sanjay Patel | f9a0d59 | 2018-08-02 13:46:20 +0000 | [diff] [blame] | 2895 | cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, |
| 2896 | SignBitOnly, Depth + 1)) || |
Matt Arsenault | d54b7f0 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 2897 | (isKnownNeverNaN(I->getOperand(1), TLI) && |
Sanjay Patel | f9a0d59 | 2018-08-02 13:46:20 +0000 | [diff] [blame] | 2898 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, |
| 2899 | SignBitOnly, Depth + 1)); |
| 2900 | |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2901 | case Intrinsic::minnum: |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2902 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2903 | Depth + 1) && |
| 2904 | cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly, |
| 2905 | Depth + 1); |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2906 | case Intrinsic::exp: |
| 2907 | case Intrinsic::exp2: |
| 2908 | case Intrinsic::fabs: |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2909 | return true; |
Justin Lebar | 7e3184c | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2910 | |
| 2911 | case Intrinsic::sqrt: |
| 2912 | // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0. |
| 2913 | if (!SignBitOnly) |
| 2914 | return true; |
| 2915 | return CI->hasNoNaNs() && (CI->hasNoSignedZeros() || |
| 2916 | CannotBeNegativeZero(CI->getOperand(0), TLI)); |
| 2917 | |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2918 | case Intrinsic::powi: |
Justin Lebar | 7e3184c | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2919 | if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) { |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2920 | // powi(x,n) is non-negative if n is even. |
Justin Lebar | 7e3184c | 2017-01-26 00:10:26 +0000 | [diff] [blame] | 2921 | if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0) |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2922 | return true; |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2923 | } |
Justin Lebar | 322c127 | 2017-01-27 00:58:34 +0000 | [diff] [blame] | 2924 | // TODO: This is not correct. Given that exp is an integer, here are the |
| 2925 | // ways that pow can return a negative value: |
| 2926 | // |
| 2927 | // pow(x, exp) --> negative if exp is odd and x is negative. |
| 2928 | // pow(-0, exp) --> -inf if exp is negative odd. |
| 2929 | // pow(-0, exp) --> -0 if exp is positive odd. |
| 2930 | // pow(-inf, exp) --> -0 if exp is negative odd. |
| 2931 | // pow(-inf, exp) --> -inf if exp is positive odd. |
| 2932 | // |
| 2933 | // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN, |
| 2934 | // but we must return false if x == -0. Unfortunately we do not currently |
| 2935 | // have a way of expressing this constraint. See details in |
| 2936 | // https://llvm.org/bugs/show_bug.cgi?id=31702. |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2937 | return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, |
| 2938 | Depth + 1); |
Justin Lebar | 322c127 | 2017-01-27 00:58:34 +0000 | [diff] [blame] | 2939 | |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2940 | case Intrinsic::fma: |
| 2941 | case Intrinsic::fmuladd: |
| 2942 | // x*x+y is non-negative if y is non-negative. |
| 2943 | return I->getOperand(0) == I->getOperand(1) && |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2944 | (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) && |
| 2945 | cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly, |
| 2946 | Depth + 1); |
David Majnemer | 3ee5f34 | 2016-04-13 06:55:52 +0000 | [diff] [blame] | 2947 | } |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2948 | break; |
| 2949 | } |
Sanjoy Das | 6082c1a | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 2950 | return false; |
Elena Demikhovsky | 45f0448 | 2015-01-28 08:03:58 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
Matt Arsenault | 1e0edbf | 2017-01-11 00:33:24 +0000 | [diff] [blame] | 2953 | bool llvm::CannotBeOrderedLessThanZero(const Value *V, |
| 2954 | const TargetLibraryInfo *TLI) { |
| 2955 | return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0); |
| 2956 | } |
| 2957 | |
| 2958 | bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) { |
| 2959 | return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0); |
| 2960 | } |
| 2961 | |
Matt Arsenault | d54b7f0 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 2962 | bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI, |
| 2963 | unsigned Depth) { |
Sanjay Patel | 6840c5f | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 2964 | assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type"); |
| 2965 | |
| 2966 | // If we're told that NaNs won't happen, assume they won't. |
| 2967 | if (auto *FPMathOp = dyn_cast<FPMathOperator>(V)) |
| 2968 | if (FPMathOp->hasNoNaNs()) |
| 2969 | return true; |
| 2970 | |
Sanjay Patel | 6840c5f | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 2971 | // Handle scalar constants. |
| 2972 | if (auto *CFP = dyn_cast<ConstantFP>(V)) |
| 2973 | return !CFP->isNaN(); |
| 2974 | |
Matt Arsenault | d54b7f0 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 2975 | if (Depth == MaxDepth) |
| 2976 | return false; |
| 2977 | |
Matt Arsenault | 450fcc7 | 2018-08-20 16:51:00 +0000 | [diff] [blame] | 2978 | if (auto *Inst = dyn_cast<Instruction>(V)) { |
| 2979 | switch (Inst->getOpcode()) { |
| 2980 | case Instruction::FAdd: |
| 2981 | case Instruction::FMul: |
| 2982 | case Instruction::FSub: |
| 2983 | case Instruction::FDiv: |
| 2984 | case Instruction::FRem: { |
| 2985 | // TODO: Need isKnownNeverInfinity |
| 2986 | return false; |
| 2987 | } |
| 2988 | case Instruction::Select: { |
| 2989 | return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) && |
| 2990 | isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1); |
| 2991 | } |
| 2992 | case Instruction::SIToFP: |
| 2993 | case Instruction::UIToFP: |
| 2994 | return true; |
| 2995 | case Instruction::FPTrunc: |
| 2996 | case Instruction::FPExt: |
| 2997 | return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1); |
| 2998 | default: |
| 2999 | break; |
| 3000 | } |
| 3001 | } |
| 3002 | |
Matt Arsenault | d54b7f0 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 3003 | if (const auto *II = dyn_cast<IntrinsicInst>(V)) { |
| 3004 | switch (II->getIntrinsicID()) { |
| 3005 | case Intrinsic::canonicalize: |
| 3006 | case Intrinsic::fabs: |
| 3007 | case Intrinsic::copysign: |
Matt Arsenault | 450fcc7 | 2018-08-20 16:51:00 +0000 | [diff] [blame] | 3008 | case Intrinsic::exp: |
| 3009 | case Intrinsic::exp2: |
| 3010 | case Intrinsic::floor: |
| 3011 | case Intrinsic::ceil: |
| 3012 | case Intrinsic::trunc: |
| 3013 | case Intrinsic::rint: |
| 3014 | case Intrinsic::nearbyint: |
| 3015 | case Intrinsic::round: |
Matt Arsenault | d54b7f0 | 2018-08-09 22:40:08 +0000 | [diff] [blame] | 3016 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1); |
| 3017 | case Intrinsic::sqrt: |
| 3018 | return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) && |
| 3019 | CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI); |
| 3020 | default: |
| 3021 | return false; |
| 3022 | } |
| 3023 | } |
| 3024 | |
Sanjay Patel | 6840c5f | 2017-09-05 23:13:13 +0000 | [diff] [blame] | 3025 | // Bail out for constant expressions, but try to handle vector constants. |
| 3026 | if (!V->getType()->isVectorTy() || !isa<Constant>(V)) |
| 3027 | return false; |
| 3028 | |
| 3029 | // For vectors, verify that each element is not NaN. |
| 3030 | unsigned NumElts = V->getType()->getVectorNumElements(); |
| 3031 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3032 | Constant *Elt = cast<Constant>(V)->getAggregateElement(i); |
| 3033 | if (!Elt) |
| 3034 | return false; |
| 3035 | if (isa<UndefValue>(Elt)) |
| 3036 | continue; |
| 3037 | auto *CElt = dyn_cast<ConstantFP>(Elt); |
| 3038 | if (!CElt || CElt->isNaN()) |
| 3039 | return false; |
| 3040 | } |
| 3041 | // All elements were confirmed not-NaN or undefined. |
| 3042 | return true; |
| 3043 | } |
| 3044 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3045 | /// If the specified value can be set by repeating the same byte in memory, |
| 3046 | /// return the i8 value that it is represented with. This is |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3047 | /// true for all i8 values obviously, but is also true for i32 0, i32 -1, |
| 3048 | /// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated |
| 3049 | /// byte store (e.g. i16 0x1234), return null. |
| 3050 | Value *llvm::isBytewiseValue(Value *V) { |
| 3051 | // All byte-wide stores are splatable, even of arbitrary variables. |
| 3052 | if (V->getType()->isIntegerTy(8)) return V; |
Chris Lattner | acf6b07 | 2011-02-19 19:35:49 +0000 | [diff] [blame] | 3053 | |
| 3054 | // Handle 'null' ConstantArrayZero etc. |
| 3055 | if (Constant *C = dyn_cast<Constant>(V)) |
| 3056 | if (C->isNullValue()) |
| 3057 | return Constant::getNullValue(Type::getInt8Ty(V->getContext())); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3058 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3059 | // Constant float and double values can be handled as integer values if the |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3060 | // corresponding integer value is "byteable". An important case is 0.0. |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3061 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) { |
| 3062 | if (CFP->getType()->isFloatTy()) |
| 3063 | V = ConstantExpr::getBitCast(CFP, Type::getInt32Ty(V->getContext())); |
| 3064 | if (CFP->getType()->isDoubleTy()) |
| 3065 | V = ConstantExpr::getBitCast(CFP, Type::getInt64Ty(V->getContext())); |
| 3066 | // Don't handle long double formats, which have strange constraints. |
| 3067 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3068 | |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 3069 | // We can handle constant integers that are multiple of 8 bits. |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3070 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 3071 | if (CI->getBitWidth() % 8 == 0) { |
| 3072 | assert(CI->getBitWidth() > 8 && "8 bits should be handled above!"); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3073 | |
Benjamin Kramer | b4b5150 | 2015-03-25 16:49:59 +0000 | [diff] [blame] | 3074 | if (!CI->getValue().isSplat(8)) |
Benjamin Kramer | 17d9015 | 2015-02-07 19:29:02 +0000 | [diff] [blame] | 3075 | return nullptr; |
| 3076 | return ConstantInt::get(V->getContext(), CI->getValue().trunc(8)); |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3077 | } |
| 3078 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3079 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3080 | // A ConstantDataArray/Vector is splatable if all its members are equal and |
| 3081 | // also splatable. |
| 3082 | if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(V)) { |
| 3083 | Value *Elt = CA->getElementAsConstant(0); |
| 3084 | Value *Val = isBytewiseValue(Elt); |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3085 | if (!Val) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3086 | return nullptr; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3088 | for (unsigned I = 1, E = CA->getNumElements(); I != E; ++I) |
| 3089 | if (CA->getElementAsConstant(I) != Elt) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3090 | return nullptr; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3092 | return Val; |
| 3093 | } |
Chad Rosier | 8abf65a | 2011-12-06 00:19:08 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3095 | // Conceptually, we could handle things like: |
| 3096 | // %a = zext i8 %X to i16 |
| 3097 | // %b = shl i16 %a, 8 |
| 3098 | // %c = or i16 %a, %b |
| 3099 | // but until there is an example that actually needs this, it doesn't seem |
| 3100 | // worth worrying about. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3101 | return nullptr; |
Chris Lattner | 9cb1035 | 2010-12-26 20:15:01 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3104 | // This is the recursive version of BuildSubAggregate. It takes a few different |
| 3105 | // arguments. Idxs is the index within the nested struct From that we are |
| 3106 | // looking at now (which is of type IndexedType). IdxSkip is the number of |
| 3107 | // indices from Idxs that should be left out when inserting into the resulting |
| 3108 | // struct. To is the result struct built so far, new insertvalue instructions |
| 3109 | // build on that. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3110 | static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType, |
Craig Topper | 2cd5ff8 | 2013-07-11 16:22:38 +0000 | [diff] [blame] | 3111 | SmallVectorImpl<unsigned> &Idxs, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 3112 | unsigned IdxSkip, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 3113 | Instruction *InsertBefore) { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3114 | StructType *STy = dyn_cast<StructType>(IndexedType); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3115 | if (STy) { |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3116 | // Save the original To argument so we can modify it |
| 3117 | Value *OrigTo = To; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3118 | // General case, the type indexed by Idxs is a struct |
| 3119 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 3120 | // Process each struct element recursively |
| 3121 | Idxs.push_back(i); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3122 | Value *PrevTo = To; |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 3123 | To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip, |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3124 | InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3125 | Idxs.pop_back(); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3126 | if (!To) { |
| 3127 | // Couldn't find any inserted value for this index? Cleanup |
| 3128 | while (PrevTo != OrigTo) { |
| 3129 | InsertValueInst* Del = cast<InsertValueInst>(PrevTo); |
| 3130 | PrevTo = Del->getAggregateOperand(); |
| 3131 | Del->eraseFromParent(); |
| 3132 | } |
| 3133 | // Stop processing elements |
| 3134 | break; |
| 3135 | } |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3136 | } |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 3137 | // If we successfully found a value for each of our subaggregates |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3138 | if (To) |
| 3139 | return To; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3140 | } |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3141 | // Base case, the type indexed by SourceIdxs is not a struct, or not all of |
| 3142 | // the struct's elements had a value that was inserted directly. In the latter |
| 3143 | // case, perhaps we can't determine each of the subelements individually, but |
| 3144 | // we might be able to find the complete struct somewhere. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3145 | |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3146 | // Find the value that is at that particular spot |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3147 | Value *V = FindInsertedValue(From, Idxs); |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3148 | |
| 3149 | if (!V) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3150 | return nullptr; |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3151 | |
Vedant Kumar | d319674 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 3152 | // Insert the value in the new (sub) aggregate |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3153 | return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip), |
| 3154 | "tmp", InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | // This helper takes a nested struct and extracts a part of it (which is again a |
| 3158 | // struct) into a new value. For example, given the struct: |
| 3159 | // { a, { b, { c, d }, e } } |
| 3160 | // and the indices "1, 1" this returns |
| 3161 | // { c, d }. |
| 3162 | // |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3163 | // It does this by inserting an insertvalue for each element in the resulting |
| 3164 | // struct, as opposed to just inserting a single struct. This will only work if |
| 3165 | // each of the elements of the substruct are known (ie, inserted into From by an |
| 3166 | // insertvalue instruction somewhere). |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3167 | // |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3168 | // All inserted insertvalue instructions are inserted before InsertBefore |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3169 | static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range, |
Dan Gohman | a6d0afc | 2009-08-07 01:32:21 +0000 | [diff] [blame] | 3170 | Instruction *InsertBefore) { |
Matthijs Kooijman | 69801d4 | 2008-06-16 13:28:31 +0000 | [diff] [blame] | 3171 | assert(InsertBefore && "Must have someplace to insert!"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3172 | Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3173 | idx_range); |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 3174 | Value *To = UndefValue::get(IndexedType); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3175 | SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3176 | unsigned IdxSkip = Idxs.size(); |
| 3177 | |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3178 | return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Vedant Kumar | d319674 | 2018-02-28 19:08:52 +0000 | [diff] [blame] | 3181 | /// Given an aggregate and a sequence of indices, see if the scalar value |
| 3182 | /// indexed is already around as a register, for example if it was inserted |
| 3183 | /// directly into the aggregate. |
Matthijs Kooijman | fa4d0b8 | 2008-06-16 14:13:46 +0000 | [diff] [blame] | 3184 | /// |
| 3185 | /// If InsertBefore is not null, this function will duplicate (modified) |
| 3186 | /// insertvalues when a part of a nested struct is extracted. |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3187 | Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range, |
| 3188 | Instruction *InsertBefore) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3189 | // Nothing to index? Just return V then (this is useful at the end of our |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3190 | // recursion). |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3191 | if (idx_range.empty()) |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3192 | return V; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3193 | // We have indices, so V should have an indexable type. |
| 3194 | assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) && |
| 3195 | "Not looking at a struct or array?"); |
| 3196 | assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) && |
| 3197 | "Invalid indices for type?"); |
Owen Anderson | f1f1743 | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 3198 | |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 3199 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 3200 | C = C->getAggregateElement(idx_range[0]); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3201 | if (!C) return nullptr; |
Chris Lattner | 6705883 | 2012-01-25 06:48:06 +0000 | [diff] [blame] | 3202 | return FindInsertedValue(C, idx_range.slice(1), InsertBefore); |
| 3203 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3204 | |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3205 | if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) { |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3206 | // Loop the indices for the insertvalue instruction in parallel with the |
| 3207 | // requested indices |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3208 | const unsigned *req_idx = idx_range.begin(); |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 3209 | for (const unsigned *i = I->idx_begin(), *e = I->idx_end(); |
| 3210 | i != e; ++i, ++req_idx) { |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3211 | if (req_idx == idx_range.end()) { |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3212 | // We can't handle this without inserting insertvalues |
| 3213 | if (!InsertBefore) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3214 | return nullptr; |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3215 | |
| 3216 | // The requested index identifies a part of a nested aggregate. Handle |
| 3217 | // this specially. For example, |
| 3218 | // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0 |
| 3219 | // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1 |
| 3220 | // %C = extractvalue {i32, { i32, i32 } } %B, 1 |
| 3221 | // This can be changed into |
| 3222 | // %A = insertvalue {i32, i32 } undef, i32 10, 0 |
| 3223 | // %C = insertvalue {i32, i32 } %A, i32 11, 1 |
| 3224 | // which allows the unused 0,0 element from the nested struct to be |
| 3225 | // removed. |
| 3226 | return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx), |
| 3227 | InsertBefore); |
Duncan Sands | db356ee | 2008-06-19 08:47:31 +0000 | [diff] [blame] | 3228 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3229 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3230 | // This insert value inserts something else than what we are looking for. |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3231 | // See if the (aggregate) value inserted into has the value we are |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3232 | // looking for, then. |
| 3233 | if (*req_idx != *i) |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3234 | return FindInsertedValue(I->getAggregateOperand(), idx_range, |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3235 | InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3236 | } |
| 3237 | // If we end up here, the indices of the insertvalue match with those |
| 3238 | // requested (though possibly only partially). Now we recursively look at |
| 3239 | // the inserted value, passing any remaining indices. |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3240 | return FindInsertedValue(I->getInsertedValueOperand(), |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 3241 | makeArrayRef(req_idx, idx_range.end()), |
Nick Lewycky | 39dbfd3 | 2009-11-23 03:29:18 +0000 | [diff] [blame] | 3242 | InsertBefore); |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3243 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3244 | |
Chris Lattner | f7eb543 | 2012-01-24 07:54:10 +0000 | [diff] [blame] | 3245 | if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3246 | // If we're extracting a value from an aggregate that was extracted from |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3247 | // something else, we can extract from that something else directly instead. |
| 3248 | // However, we will need to chain I's indices with the requested indices. |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Calculate the number of indices required |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3251 | unsigned size = I->getNumIndices() + idx_range.size(); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3252 | // Allocate some space to put the new indices in |
Matthijs Kooijman | 8369c67 | 2008-06-17 08:24:37 +0000 | [diff] [blame] | 3253 | SmallVector<unsigned, 5> Idxs; |
| 3254 | Idxs.reserve(size); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3255 | // Add indices from the extract value instruction |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3256 | Idxs.append(I->idx_begin(), I->idx_end()); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3257 | |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3258 | // Add requested indices |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3259 | Idxs.append(idx_range.begin(), idx_range.end()); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3260 | |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3261 | assert(Idxs.size() == size |
Matthijs Kooijman | 5cb3877 | 2008-06-16 12:57:37 +0000 | [diff] [blame] | 3262 | && "Number of indices added not correct?"); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3263 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 3264 | return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore); |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3265 | } |
| 3266 | // Otherwise, we don't know (such as, extracting from a function return value |
| 3267 | // or load instruction) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 3268 | return nullptr; |
Matthijs Kooijman | e92e18b | 2008-06-16 12:48:21 +0000 | [diff] [blame] | 3269 | } |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3270 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3271 | /// Analyze the specified pointer to see if it can be expressed as a base |
| 3272 | /// pointer plus a constant offset. Return the base and offset to the caller. |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3273 | Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3274 | const DataLayout &DL) { |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3275 | unsigned BitWidth = DL.getIndexTypeSizeInBits(Ptr->getType()); |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3276 | APInt ByteOffset(BitWidth, 0); |
Chandler Carruth | 7664127 | 2016-01-04 07:23:12 +0000 | [diff] [blame] | 3277 | |
| 3278 | // We walk up the defs but use a visited set to handle unreachable code. In |
| 3279 | // that case, we stop after accumulating the cycle once (not that it |
| 3280 | // matters). |
| 3281 | SmallPtrSet<Value *, 16> Visited; |
| 3282 | while (Visited.insert(Ptr).second) { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3283 | if (Ptr->getType()->isVectorTy()) |
| 3284 | break; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3285 | |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3286 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
Tom Stellard | 17eb341 | 2016-10-07 14:23:29 +0000 | [diff] [blame] | 3287 | // If one of the values we have visited is an addrspacecast, then |
| 3288 | // the pointer type of this GEP may be different from the type |
| 3289 | // of the Ptr parameter which was passed to this function. This |
| 3290 | // means when we construct GEPOffset, we need to use the size |
| 3291 | // of GEP's pointer type rather than the size of the original |
| 3292 | // pointer type. |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3293 | APInt GEPOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3294 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
| 3295 | break; |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 3296 | |
Tom Stellard | 17eb341 | 2016-10-07 14:23:29 +0000 | [diff] [blame] | 3297 | ByteOffset += GEPOffset.getSExtValue(); |
Matt Arsenault | f55e5e7 | 2013-08-10 17:34:08 +0000 | [diff] [blame] | 3298 | |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3299 | Ptr = GEP->getPointerOperand(); |
Tom Stellard | 17eb341 | 2016-10-07 14:23:29 +0000 | [diff] [blame] | 3300 | } else if (Operator::getOpcode(Ptr) == Instruction::BitCast || |
| 3301 | Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3302 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 3303 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 3304 | if (GA->isInterposable()) |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3305 | break; |
| 3306 | Ptr = GA->getAliasee(); |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3307 | } else { |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3308 | break; |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3309 | } |
| 3310 | } |
Nuno Lopes | 368c4d0 | 2012-12-31 20:48:35 +0000 | [diff] [blame] | 3311 | Offset = ByteOffset.getSExtValue(); |
| 3312 | return Ptr; |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3315 | bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP, |
| 3316 | unsigned CharSize) { |
David L Kreitzer | 752c144 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3317 | // Make sure the GEP has exactly three arguments. |
| 3318 | if (GEP->getNumOperands() != 3) |
| 3319 | return false; |
| 3320 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3321 | // Make sure the index-ee is a pointer to array of \p CharSize integers. |
| 3322 | // CharSize. |
David L Kreitzer | 752c144 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3323 | ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType()); |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3324 | if (!AT || !AT->getElementType()->isIntegerTy(CharSize)) |
David L Kreitzer | 752c144 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3325 | return false; |
| 3326 | |
| 3327 | // Check to make sure that the first operand of the GEP is an integer and |
| 3328 | // has value 0 so that we are sure we're indexing into the initializer. |
| 3329 | const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1)); |
| 3330 | if (!FirstIdx || !FirstIdx->isZero()) |
| 3331 | return false; |
| 3332 | |
| 3333 | return true; |
Sanjoy Das | 6082c1a | 2016-05-07 02:08:15 +0000 | [diff] [blame] | 3334 | } |
Chris Lattner | e28618d | 2010-11-30 22:25:26 +0000 | [diff] [blame] | 3335 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3336 | bool llvm::getConstantDataArrayInfo(const Value *V, |
| 3337 | ConstantDataArraySlice &Slice, |
| 3338 | unsigned ElementSize, uint64_t Offset) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3339 | assert(V); |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3341 | // Look through bitcast instructions and geps. |
| 3342 | V = V->stripPointerCasts(); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3343 | |
Benjamin Kramer | 0248a3e | 2015-03-21 15:36:06 +0000 | [diff] [blame] | 3344 | // If the value is a GEP instruction or constant expression, treat it as an |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3345 | // offset. |
| 3346 | if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
David L Kreitzer | 752c144 | 2016-04-13 14:31:06 +0000 | [diff] [blame] | 3347 | // The GEP operator should be based on a pointer to string constant, and is |
| 3348 | // indexing into the string constant. |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3349 | if (!isGEPBasedOnPointerToString(GEP, ElementSize)) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3350 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3351 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3352 | // If the second index isn't a ConstantInt, then this is a variable index |
| 3353 | // into the array. If this occurs, we can't say anything meaningful about |
| 3354 | // the string. |
| 3355 | uint64_t StartIdx = 0; |
Dan Gohman | 0b4df04 | 2010-04-14 22:20:45 +0000 | [diff] [blame] | 3356 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2))) |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3357 | StartIdx = CI->getZExtValue(); |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3358 | else |
| 3359 | return false; |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3360 | return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize, |
| 3361 | StartIdx + Offset); |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3362 | } |
Nick Lewycky | 4620988 | 2011-10-20 00:34:35 +0000 | [diff] [blame] | 3363 | |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3364 | // The GEP instruction, constant or instruction, must reference a global |
| 3365 | // variable that is a constant and is initialized. The referenced constant |
| 3366 | // initializer is the array that we'll use for optimization. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3367 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
Dan Gohman | 5d5bc6d | 2009-08-19 18:20:44 +0000 | [diff] [blame] | 3368 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3369 | return false; |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3370 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3371 | const ConstantDataArray *Array; |
| 3372 | ArrayType *ArrayTy; |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3373 | if (GV->getInitializer()->isNullValue()) { |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3374 | Type *GVTy = GV->getValueType(); |
| 3375 | if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) { |
Sanjay Patel | 2ad88f8 | 2017-06-12 22:34:37 +0000 | [diff] [blame] | 3376 | // A zeroinitializer for the array; there is no ConstantDataArray. |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3377 | Array = nullptr; |
| 3378 | } else { |
| 3379 | const DataLayout &DL = GV->getParent()->getDataLayout(); |
| 3380 | uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy); |
| 3381 | uint64_t Length = SizeInBytes / (ElementSize / 8); |
| 3382 | if (Length <= Offset) |
| 3383 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3384 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3385 | Slice.Array = nullptr; |
| 3386 | Slice.Offset = 0; |
| 3387 | Slice.Length = Length - Offset; |
| 3388 | return true; |
| 3389 | } |
| 3390 | } else { |
| 3391 | // This must be a ConstantDataArray. |
| 3392 | Array = dyn_cast<ConstantDataArray>(GV->getInitializer()); |
| 3393 | if (!Array) |
| 3394 | return false; |
| 3395 | ArrayTy = Array->getType(); |
| 3396 | } |
| 3397 | if (!ArrayTy->getElementType()->isIntegerTy(ElementSize)) |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3398 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3399 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3400 | uint64_t NumElts = ArrayTy->getArrayNumElements(); |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3401 | if (Offset > NumElts) |
| 3402 | return false; |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3403 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3404 | Slice.Array = Array; |
| 3405 | Slice.Offset = Offset; |
| 3406 | Slice.Length = NumElts - Offset; |
| 3407 | return true; |
| 3408 | } |
| 3409 | |
| 3410 | /// This function computes the length of a null-terminated C string pointed to |
| 3411 | /// by V. If successful, it returns true and returns the string in Str. |
| 3412 | /// If unsuccessful, it returns false. |
| 3413 | bool llvm::getConstantStringInfo(const Value *V, StringRef &Str, |
| 3414 | uint64_t Offset, bool TrimAtNul) { |
| 3415 | ConstantDataArraySlice Slice; |
| 3416 | if (!getConstantDataArrayInfo(V, Slice, 8, Offset)) |
| 3417 | return false; |
| 3418 | |
| 3419 | if (Slice.Array == nullptr) { |
| 3420 | if (TrimAtNul) { |
| 3421 | Str = StringRef(); |
| 3422 | return true; |
| 3423 | } |
| 3424 | if (Slice.Length == 1) { |
| 3425 | Str = StringRef("", 1); |
| 3426 | return true; |
| 3427 | } |
Sanjay Patel | fef83e8 | 2017-06-09 14:21:18 +0000 | [diff] [blame] | 3428 | // We cannot instantiate a StringRef as we do not have an appropriate string |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3429 | // of 0s at hand. |
| 3430 | return false; |
| 3431 | } |
| 3432 | |
| 3433 | // Start out with the entire array in the StringRef. |
| 3434 | Str = Slice.Array->getAsString(); |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3435 | // Skip over 'offset' bytes. |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3436 | Str = Str.substr(Slice.Offset); |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3437 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3438 | if (TrimAtNul) { |
| 3439 | // Trim off the \0 and anything after it. If the array is not nul |
| 3440 | // terminated, we just return the whole end of string. The client may know |
| 3441 | // some other way that the string is length-bound. |
| 3442 | Str = Str.substr(0, Str.find('\0')); |
| 3443 | } |
Bill Wendling | fa54bc2 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 3444 | return true; |
Evan Cheng | da3db11 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 3445 | } |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3446 | |
| 3447 | // These next two are very similar to the above, but also look through PHI |
| 3448 | // nodes. |
| 3449 | // TODO: See if we can integrate these two together. |
| 3450 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3451 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3452 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3453 | static uint64_t GetStringLengthH(const Value *V, |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3454 | SmallPtrSetImpl<const PHINode*> &PHIs, |
| 3455 | unsigned CharSize) { |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3456 | // Look through noop bitcast instructions. |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3457 | V = V->stripPointerCasts(); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3458 | |
| 3459 | // If this is a PHI node, there are two cases: either we have already seen it |
| 3460 | // or we haven't. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3461 | if (const PHINode *PN = dyn_cast<PHINode>(V)) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3462 | if (!PHIs.insert(PN).second) |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3463 | return ~0ULL; // already in the set. |
| 3464 | |
| 3465 | // If it was new, see if all the input strings are the same length. |
| 3466 | uint64_t LenSoFar = ~0ULL; |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3467 | for (Value *IncValue : PN->incoming_values()) { |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3468 | uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3469 | if (Len == 0) return 0; // Unknown length -> unknown. |
| 3470 | |
| 3471 | if (Len == ~0ULL) continue; |
| 3472 | |
| 3473 | if (Len != LenSoFar && LenSoFar != ~0ULL) |
| 3474 | return 0; // Disagree -> unknown. |
| 3475 | LenSoFar = Len; |
| 3476 | } |
| 3477 | |
| 3478 | // Success, all agree. |
| 3479 | return LenSoFar; |
| 3480 | } |
| 3481 | |
| 3482 | // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y) |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3483 | if (const SelectInst *SI = dyn_cast<SelectInst>(V)) { |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3484 | uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3485 | if (Len1 == 0) return 0; |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3486 | uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3487 | if (Len2 == 0) return 0; |
| 3488 | if (Len1 == ~0ULL) return Len2; |
| 3489 | if (Len2 == ~0ULL) return Len1; |
| 3490 | if (Len1 != Len2) return 0; |
| 3491 | return Len1; |
| 3492 | } |
Craig Topper | 1bef2c8 | 2012-12-22 19:15:35 +0000 | [diff] [blame] | 3493 | |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 3494 | // Otherwise, see if we can read the string. |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3495 | ConstantDataArraySlice Slice; |
| 3496 | if (!getConstantDataArrayInfo(V, Slice, CharSize)) |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3497 | return 0; |
| 3498 | |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3499 | if (Slice.Array == nullptr) |
| 3500 | return 1; |
| 3501 | |
| 3502 | // Search for nul characters |
| 3503 | unsigned NullIndex = 0; |
| 3504 | for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) { |
| 3505 | if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0) |
| 3506 | break; |
| 3507 | } |
| 3508 | |
| 3509 | return NullIndex + 1; |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3512 | /// If we can compute the length of the string pointed to by |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3513 | /// the specified pointer, return 'len+1'. If we can't, return 0. |
David Bolvansky | 1f343fa | 2018-05-22 20:27:36 +0000 | [diff] [blame] | 3514 | uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) { |
David Bolvansky | 41f4b64 | 2018-05-22 15:41:23 +0000 | [diff] [blame] | 3515 | if (!V->getType()->isPointerTy()) |
| 3516 | return 0; |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3517 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3518 | SmallPtrSet<const PHINode*, 32> PHIs; |
Matthias Braun | 50ec0b5 | 2017-05-19 22:37:09 +0000 | [diff] [blame] | 3519 | uint64_t Len = GetStringLengthH(V, PHIs, CharSize); |
Eric Christopher | 4899cbc | 2010-03-05 06:58:57 +0000 | [diff] [blame] | 3520 | // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return |
| 3521 | // an empty string as a length. |
| 3522 | return Len == ~0ULL ? 1 : Len; |
| 3523 | } |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3524 | |
Piotr Padlewski | d6f7346 | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3525 | const Value *llvm::getArgumentAliasingToReturnedPointer(ImmutableCallSite CS) { |
| 3526 | assert(CS && |
| 3527 | "getArgumentAliasingToReturnedPointer only works on nonnull CallSite"); |
| 3528 | if (const Value *RV = CS.getReturnedArgOperand()) |
| 3529 | return RV; |
| 3530 | // This can be used only as a aliasing property. |
| 3531 | if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(CS)) |
| 3532 | return CS.getArgOperand(0); |
| 3533 | return nullptr; |
| 3534 | } |
| 3535 | |
| 3536 | bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing( |
Piotr Padlewski | 5b3db45 | 2018-07-02 04:49:30 +0000 | [diff] [blame] | 3537 | ImmutableCallSite CS) { |
| 3538 | return CS.getIntrinsicID() == Intrinsic::launder_invariant_group || |
| 3539 | CS.getIntrinsicID() == Intrinsic::strip_invariant_group; |
Piotr Padlewski | d6f7346 | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3542 | /// \p PN defines a loop-variant pointer to an object. Check if the |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3543 | /// previous iteration of the loop was referring to the same object as \p PN. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3544 | static bool isSameUnderlyingObjectInLoop(const PHINode *PN, |
| 3545 | const LoopInfo *LI) { |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3546 | // Find the loop-defined value. |
| 3547 | Loop *L = LI->getLoopFor(PN->getParent()); |
| 3548 | if (PN->getNumIncomingValues() != 2) |
| 3549 | return true; |
| 3550 | |
| 3551 | // Find the value from previous iteration. |
| 3552 | auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0)); |
| 3553 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 3554 | PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1)); |
| 3555 | if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L) |
| 3556 | return true; |
| 3557 | |
| 3558 | // If a new pointer is loaded in the loop, the pointer references a different |
| 3559 | // object in every iteration. E.g.: |
| 3560 | // for (i) |
| 3561 | // int *p = a[i]; |
| 3562 | // ... |
| 3563 | if (auto *Load = dyn_cast<LoadInst>(PrevValue)) |
| 3564 | if (!L->isLoopInvariant(Load->getPointerOperand())) |
| 3565 | return false; |
| 3566 | return true; |
| 3567 | } |
| 3568 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3569 | Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL, |
| 3570 | unsigned MaxLookup) { |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3571 | if (!V->getType()->isPointerTy()) |
| 3572 | return V; |
| 3573 | for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) { |
| 3574 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 3575 | V = GEP->getPointerOperand(); |
Matt Arsenault | 70f4db88 | 2014-07-15 00:56:40 +0000 | [diff] [blame] | 3576 | } else if (Operator::getOpcode(V) == Instruction::BitCast || |
| 3577 | Operator::getOpcode(V) == Instruction::AddrSpaceCast) { |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3578 | V = cast<Operator>(V)->getOperand(0); |
| 3579 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 3580 | if (GA->isInterposable()) |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3581 | return V; |
| 3582 | V = GA->getAliasee(); |
Craig Topper | 8548241 | 2017-04-12 22:29:23 +0000 | [diff] [blame] | 3583 | } else if (isa<AllocaInst>(V)) { |
| 3584 | // An alloca can't be further simplified. |
| 3585 | return V; |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3586 | } else { |
Piotr Padlewski | d6f7346 | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3587 | if (auto CS = CallSite(V)) { |
Piotr Padlewski | 5b3db45 | 2018-07-02 04:49:30 +0000 | [diff] [blame] | 3588 | // CaptureTracking can know about special capturing properties of some |
| 3589 | // intrinsics like launder.invariant.group, that can't be expressed with |
| 3590 | // the attributes, but have properties like returning aliasing pointer. |
| 3591 | // Because some analysis may assume that nocaptured pointer is not |
| 3592 | // returned from some special intrinsic (because function would have to |
| 3593 | // be marked with returns attribute), it is crucial to use this function |
| 3594 | // because it should be in sync with CaptureTracking. Not using it may |
| 3595 | // cause weird miscompilations where 2 aliasing pointers are assumed to |
| 3596 | // noalias. |
Piotr Padlewski | d6f7346 | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3597 | if (auto *RP = getArgumentAliasingToReturnedPointer(CS)) { |
| 3598 | V = RP; |
Hal Finkel | 5c12d8f | 2016-07-11 01:32:20 +0000 | [diff] [blame] | 3599 | continue; |
| 3600 | } |
Piotr Padlewski | d6f7346 | 2018-05-23 09:16:44 +0000 | [diff] [blame] | 3601 | } |
Hal Finkel | 5c12d8f | 2016-07-11 01:32:20 +0000 | [diff] [blame] | 3602 | |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 3603 | // See if InstructionSimplify knows any relevant tricks. |
| 3604 | if (Instruction *I = dyn_cast<Instruction>(V)) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3605 | // TODO: Acquire a DominatorTree and AssumptionCache and use them. |
Daniel Berlin | 4d0fe64 | 2017-04-28 19:55:38 +0000 | [diff] [blame] | 3606 | if (Value *Simplified = SimplifyInstruction(I, {DL, I})) { |
Dan Gohman | 05b18f1 | 2010-12-15 20:49:55 +0000 | [diff] [blame] | 3607 | V = Simplified; |
| 3608 | continue; |
| 3609 | } |
| 3610 | |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 3611 | return V; |
| 3612 | } |
| 3613 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 3614 | } |
| 3615 | return V; |
| 3616 | } |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3617 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3618 | void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects, |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3619 | const DataLayout &DL, LoopInfo *LI, |
| 3620 | unsigned MaxLookup) { |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3621 | SmallPtrSet<Value *, 4> Visited; |
| 3622 | SmallVector<Value *, 4> Worklist; |
| 3623 | Worklist.push_back(V); |
| 3624 | do { |
| 3625 | Value *P = Worklist.pop_back_val(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3626 | P = GetUnderlyingObject(P, DL, MaxLookup); |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3627 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3628 | if (!Visited.insert(P).second) |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3629 | continue; |
| 3630 | |
| 3631 | if (SelectInst *SI = dyn_cast<SelectInst>(P)) { |
| 3632 | Worklist.push_back(SI->getTrueValue()); |
| 3633 | Worklist.push_back(SI->getFalseValue()); |
| 3634 | continue; |
| 3635 | } |
| 3636 | |
| 3637 | if (PHINode *PN = dyn_cast<PHINode>(P)) { |
Adam Nemet | e2b885c | 2015-04-23 20:09:20 +0000 | [diff] [blame] | 3638 | // If this PHI changes the underlying object in every iteration of the |
| 3639 | // loop, don't look through it. Consider: |
| 3640 | // int **A; |
| 3641 | // for (i) { |
| 3642 | // Prev = Curr; // Prev = PHI (Prev_0, Curr) |
| 3643 | // Curr = A[i]; |
| 3644 | // *Prev, *Curr; |
| 3645 | // |
| 3646 | // Prev is tracking Curr one iteration behind so they refer to different |
| 3647 | // underlying objects. |
| 3648 | if (!LI || !LI->isLoopHeader(PN->getParent()) || |
| 3649 | isSameUnderlyingObjectInLoop(PN, LI)) |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3650 | for (Value *IncValue : PN->incoming_values()) |
| 3651 | Worklist.push_back(IncValue); |
Dan Gohman | ed7c24e2 | 2012-05-10 18:57:38 +0000 | [diff] [blame] | 3652 | continue; |
| 3653 | } |
| 3654 | |
| 3655 | Objects.push_back(P); |
| 3656 | } while (!Worklist.empty()); |
| 3657 | } |
| 3658 | |
Hiroshi Inoue | b9417db | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3659 | /// This is the function that does the work of looking through basic |
| 3660 | /// ptrtoint+arithmetic+inttoptr sequences. |
| 3661 | static const Value *getUnderlyingObjectFromInt(const Value *V) { |
| 3662 | do { |
| 3663 | if (const Operator *U = dyn_cast<Operator>(V)) { |
| 3664 | // If we find a ptrtoint, we can transfer control back to the |
| 3665 | // regular getUnderlyingObjectFromInt. |
| 3666 | if (U->getOpcode() == Instruction::PtrToInt) |
| 3667 | return U->getOperand(0); |
| 3668 | // If we find an add of a constant, a multiplied value, or a phi, it's |
| 3669 | // likely that the other operand will lead us to the base |
| 3670 | // object. We don't have to worry about the case where the |
| 3671 | // object address is somehow being computed by the multiply, |
| 3672 | // because our callers only care when the result is an |
| 3673 | // identifiable object. |
| 3674 | if (U->getOpcode() != Instruction::Add || |
| 3675 | (!isa<ConstantInt>(U->getOperand(1)) && |
| 3676 | Operator::getOpcode(U->getOperand(1)) != Instruction::Mul && |
| 3677 | !isa<PHINode>(U->getOperand(1)))) |
| 3678 | return V; |
| 3679 | V = U->getOperand(0); |
| 3680 | } else { |
| 3681 | return V; |
| 3682 | } |
| 3683 | assert(V->getType()->isIntegerTy() && "Unexpected operand type!"); |
| 3684 | } while (true); |
| 3685 | } |
| 3686 | |
| 3687 | /// This is a wrapper around GetUnderlyingObjects and adds support for basic |
| 3688 | /// ptrtoint+arithmetic+inttoptr sequences. |
Hiroshi Inoue | b49b015 | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 3689 | /// It returns false if unidentified object is found in GetUnderlyingObjects. |
| 3690 | bool llvm::getUnderlyingObjectsForCodeGen(const Value *V, |
Hiroshi Inoue | b9417db | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3691 | SmallVectorImpl<Value *> &Objects, |
| 3692 | const DataLayout &DL) { |
| 3693 | SmallPtrSet<const Value *, 16> Visited; |
| 3694 | SmallVector<const Value *, 4> Working(1, V); |
| 3695 | do { |
| 3696 | V = Working.pop_back_val(); |
| 3697 | |
| 3698 | SmallVector<Value *, 4> Objs; |
| 3699 | GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL); |
| 3700 | |
| 3701 | for (Value *V : Objs) { |
Hiroshi Inoue | b9417db | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3702 | if (!Visited.insert(V).second) |
| 3703 | continue; |
| 3704 | if (Operator::getOpcode(V) == Instruction::IntToPtr) { |
| 3705 | const Value *O = |
| 3706 | getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0)); |
| 3707 | if (O->getType()->isPointerTy()) { |
| 3708 | Working.push_back(O); |
| 3709 | continue; |
| 3710 | } |
| 3711 | } |
Hiroshi Inoue | 0bd906e | 2017-08-02 18:16:32 +0000 | [diff] [blame] | 3712 | // If GetUnderlyingObjects fails to find an identifiable object, |
| 3713 | // getUnderlyingObjectsForCodeGen also fails for safety. |
| 3714 | if (!isIdentifiedObject(V)) { |
| 3715 | Objects.clear(); |
Hiroshi Inoue | b49b015 | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 3716 | return false; |
Hiroshi Inoue | 0bd906e | 2017-08-02 18:16:32 +0000 | [diff] [blame] | 3717 | } |
Hiroshi Inoue | b9417db | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3718 | Objects.push_back(const_cast<Value *>(V)); |
| 3719 | } |
| 3720 | } while (!Working.empty()); |
Hiroshi Inoue | b49b015 | 2017-10-12 06:26:04 +0000 | [diff] [blame] | 3721 | return true; |
Hiroshi Inoue | b9417db | 2017-08-01 03:32:15 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Sanjay Patel | aee8421 | 2014-11-04 16:27:42 +0000 | [diff] [blame] | 3724 | /// Return true if the only users of this pointer are lifetime markers. |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3725 | bool llvm::onlyUsedByLifetimeMarkers(const Value *V) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3726 | for (const User *U : V->users()) { |
| 3727 | const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); |
Nick Lewycky | 3e334a4 | 2011-06-27 04:20:45 +0000 | [diff] [blame] | 3728 | if (!II) return false; |
| 3729 | |
| 3730 | if (II->getIntrinsicID() != Intrinsic::lifetime_start && |
| 3731 | II->getIntrinsicID() != Intrinsic::lifetime_end) |
| 3732 | return false; |
| 3733 | } |
| 3734 | return true; |
| 3735 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3736 | |
Sanjoy Das | f8a0db5 | 2015-05-18 18:07:00 +0000 | [diff] [blame] | 3737 | bool llvm::isSafeToSpeculativelyExecute(const Value *V, |
| 3738 | const Instruction *CtxI, |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 3739 | const DominatorTree *DT) { |
Dan Gohman | 7ac046a | 2012-01-04 23:01:09 +0000 | [diff] [blame] | 3740 | const Operator *Inst = dyn_cast<Operator>(V); |
| 3741 | if (!Inst) |
| 3742 | return false; |
| 3743 | |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3744 | for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i) |
| 3745 | if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i))) |
| 3746 | if (C->canTrap()) |
| 3747 | return false; |
| 3748 | |
| 3749 | switch (Inst->getOpcode()) { |
| 3750 | default: |
| 3751 | return true; |
| 3752 | case Instruction::UDiv: |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3753 | case Instruction::URem: { |
| 3754 | // x / y is undefined if y == 0. |
| 3755 | const APInt *V; |
| 3756 | if (match(Inst->getOperand(1), m_APInt(V))) |
| 3757 | return *V != 0; |
| 3758 | return false; |
| 3759 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3760 | case Instruction::SDiv: |
| 3761 | case Instruction::SRem: { |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3762 | // x / y is undefined if y == 0 or x == INT_MIN and y == -1 |
David Majnemer | 8a6578a | 2015-02-01 19:10:19 +0000 | [diff] [blame] | 3763 | const APInt *Numerator, *Denominator; |
| 3764 | if (!match(Inst->getOperand(1), m_APInt(Denominator))) |
| 3765 | return false; |
| 3766 | // We cannot hoist this division if the denominator is 0. |
| 3767 | if (*Denominator == 0) |
| 3768 | return false; |
| 3769 | // It's safe to hoist if the denominator is not 0 or -1. |
| 3770 | if (*Denominator != -1) |
| 3771 | return true; |
| 3772 | // At this point we know that the denominator is -1. It is safe to hoist as |
| 3773 | // long we know that the numerator is not INT_MIN. |
| 3774 | if (match(Inst->getOperand(0), m_APInt(Numerator))) |
| 3775 | return !Numerator->isMinSignedValue(); |
| 3776 | // The numerator *might* be MinSignedValue. |
David Majnemer | f20d7c4 | 2014-11-04 23:49:08 +0000 | [diff] [blame] | 3777 | return false; |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3778 | } |
| 3779 | case Instruction::Load: { |
| 3780 | const LoadInst *LI = cast<LoadInst>(Inst); |
Kostya Serebryany | 0b45828 | 2013-11-21 07:29:28 +0000 | [diff] [blame] | 3781 | if (!LI->isUnordered() || |
| 3782 | // Speculative load may create a race that did not exist in the source. |
Sanjoy Das | b66374c | 2016-07-14 20:19:01 +0000 | [diff] [blame] | 3783 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeThread) || |
Kostya Serebryany | 5cb86d5 | 2015-10-14 00:21:05 +0000 | [diff] [blame] | 3784 | // Speculative load may load data from dirty regions. |
Evgeniy Stepanov | c667c1f | 2017-12-09 00:21:41 +0000 | [diff] [blame] | 3785 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) || |
| 3786 | LI->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress)) |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3787 | return false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3788 | const DataLayout &DL = LI->getModule()->getDataLayout(); |
Sean Silva | 45835e7 | 2016-07-02 23:47:27 +0000 | [diff] [blame] | 3789 | return isDereferenceableAndAlignedPointer(LI->getPointerOperand(), |
| 3790 | LI->getAlignment(), DL, CtxI, DT); |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3791 | } |
Nick Lewycky | b4039f6 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3792 | case Instruction::Call: { |
Matt Arsenault | cf5e7fe | 2017-04-28 21:13:09 +0000 | [diff] [blame] | 3793 | auto *CI = cast<const CallInst>(Inst); |
| 3794 | const Function *Callee = CI->getCalledFunction(); |
David Majnemer | 0a92f86 | 2015-08-28 21:13:39 +0000 | [diff] [blame] | 3795 | |
Matt Arsenault | 6a288c1 | 2017-05-03 02:26:10 +0000 | [diff] [blame] | 3796 | // The called function could have undefined behavior or side-effects, even |
| 3797 | // if marked readnone nounwind. |
| 3798 | return Callee && Callee->isSpeculatable(); |
Nick Lewycky | b4039f6 | 2011-12-21 05:52:02 +0000 | [diff] [blame] | 3799 | } |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3800 | case Instruction::VAArg: |
| 3801 | case Instruction::Alloca: |
| 3802 | case Instruction::Invoke: |
| 3803 | case Instruction::PHI: |
| 3804 | case Instruction::Store: |
| 3805 | case Instruction::Ret: |
| 3806 | case Instruction::Br: |
| 3807 | case Instruction::IndirectBr: |
| 3808 | case Instruction::Switch: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3809 | case Instruction::Unreachable: |
| 3810 | case Instruction::Fence: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3811 | case Instruction::AtomicRMW: |
| 3812 | case Instruction::AtomicCmpXchg: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3813 | case Instruction::LandingPad: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3814 | case Instruction::Resume: |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 3815 | case Instruction::CatchSwitch: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3816 | case Instruction::CatchPad: |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3817 | case Instruction::CatchRet: |
| 3818 | case Instruction::CleanupPad: |
| 3819 | case Instruction::CleanupRet: |
Dan Gohman | 75d7d5e | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 3820 | return false; // Misc instructions which have effects |
| 3821 | } |
| 3822 | } |
Dan Gohman | 1b0f79d | 2013-01-31 02:40:59 +0000 | [diff] [blame] | 3823 | |
Quentin Colombet | 6443cce | 2015-08-06 18:44:34 +0000 | [diff] [blame] | 3824 | bool llvm::mayBeMemoryDependent(const Instruction &I) { |
| 3825 | return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I); |
| 3826 | } |
| 3827 | |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3828 | OverflowResult llvm::computeOverflowForUnsignedMul( |
| 3829 | const Value *LHS, const Value *RHS, const DataLayout &DL, |
| 3830 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 3831 | bool UseInstrInfo) { |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3832 | // Multiplying n * m significant bits yields a result of n + m significant |
| 3833 | // bits. If the total number of significant bits does not exceed the |
| 3834 | // result bit width (minus 1), there is no overflow. |
| 3835 | // This means if we have enough leading zero bits in the operands |
| 3836 | // we can guarantee that the result does not overflow. |
| 3837 | // Ref: "Hacker's Delight" by Henry Warren |
| 3838 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 3839 | KnownBits LHSKnown(BitWidth); |
| 3840 | KnownBits RHSKnown(BitWidth); |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3841 | computeKnownBits(LHS, LHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr, |
| 3842 | UseInstrInfo); |
| 3843 | computeKnownBits(RHS, RHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr, |
| 3844 | UseInstrInfo); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3845 | // Note that underestimating the number of zero bits gives a more |
| 3846 | // conservative answer. |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 3847 | unsigned ZeroBits = LHSKnown.countMinLeadingZeros() + |
| 3848 | RHSKnown.countMinLeadingZeros(); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3849 | // First handle the easy case: if we have enough zero bits there's |
| 3850 | // definitely no overflow. |
| 3851 | if (ZeroBits >= BitWidth) |
| 3852 | return OverflowResult::NeverOverflows; |
| 3853 | |
| 3854 | // Get the largest possible values for each operand. |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 3855 | APInt LHSMax = ~LHSKnown.Zero; |
| 3856 | APInt RHSMax = ~RHSKnown.Zero; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3857 | |
| 3858 | // We know the multiply operation doesn't overflow if the maximum values for |
| 3859 | // each operand will not overflow after we multiply them together. |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3860 | bool MaxOverflow; |
Craig Topper | 9b71a40 | 2017-04-19 21:09:45 +0000 | [diff] [blame] | 3861 | (void)LHSMax.umul_ov(RHSMax, MaxOverflow); |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3862 | if (!MaxOverflow) |
| 3863 | return OverflowResult::NeverOverflows; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3864 | |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3865 | // We know it always overflows if multiplying the smallest possible values for |
| 3866 | // the operands also results in overflow. |
| 3867 | bool MinOverflow; |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 3868 | (void)LHSKnown.One.umul_ov(RHSKnown.One, MinOverflow); |
David Majnemer | c8a576b | 2015-01-02 07:29:47 +0000 | [diff] [blame] | 3869 | if (MinOverflow) |
| 3870 | return OverflowResult::AlwaysOverflows; |
| 3871 | |
| 3872 | return OverflowResult::MayOverflow; |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 3873 | } |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3874 | |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3875 | OverflowResult |
| 3876 | llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS, |
| 3877 | const DataLayout &DL, AssumptionCache *AC, |
| 3878 | const Instruction *CxtI, |
| 3879 | const DominatorTree *DT, bool UseInstrInfo) { |
Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 3880 | // Multiplying n * m significant bits yields a result of n + m significant |
| 3881 | // bits. If the total number of significant bits does not exceed the |
| 3882 | // result bit width (minus 1), there is no overflow. |
| 3883 | // This means if we have enough leading sign bits in the operands |
| 3884 | // we can guarantee that the result does not overflow. |
| 3885 | // Ref: "Hacker's Delight" by Henry Warren |
| 3886 | unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); |
| 3887 | |
| 3888 | // Note that underestimating the number of sign bits gives a more |
| 3889 | // conservative answer. |
| 3890 | unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) + |
| 3891 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT); |
| 3892 | |
| 3893 | // First handle the easy case: if we have enough sign bits there's |
| 3894 | // definitely no overflow. |
| 3895 | if (SignBits > BitWidth + 1) |
| 3896 | return OverflowResult::NeverOverflows; |
| 3897 | |
| 3898 | // There are two ambiguous cases where there can be no overflow: |
| 3899 | // SignBits == BitWidth + 1 and |
| 3900 | // SignBits == BitWidth |
| 3901 | // The second case is difficult to check, therefore we only handle the |
| 3902 | // first case. |
| 3903 | if (SignBits == BitWidth + 1) { |
| 3904 | // It overflows only when both arguments are negative and the true |
| 3905 | // product is exactly the minimum negative number. |
| 3906 | // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000 |
| 3907 | // For simplicity we just check if at least one side is not negative. |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3908 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 3909 | nullptr, UseInstrInfo); |
| 3910 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 3911 | nullptr, UseInstrInfo); |
Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 3912 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) |
| 3913 | return OverflowResult::NeverOverflows; |
| 3914 | } |
| 3915 | return OverflowResult::MayOverflow; |
| 3916 | } |
| 3917 | |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3918 | OverflowResult llvm::computeOverflowForUnsignedAdd( |
| 3919 | const Value *LHS, const Value *RHS, const DataLayout &DL, |
| 3920 | AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT, |
| 3921 | bool UseInstrInfo) { |
| 3922 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 3923 | nullptr, UseInstrInfo); |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 3924 | if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) { |
Florian Hahn | 19f9e32 | 2018-08-17 14:39:04 +0000 | [diff] [blame] | 3925 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT, |
| 3926 | nullptr, UseInstrInfo); |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3927 | |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 3928 | if (LHSKnown.isNegative() && RHSKnown.isNegative()) { |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3929 | // The sign bit is set in both cases: this MUST overflow. |
| 3930 | // Create a simple add instruction, and insert it into the struct. |
| 3931 | return OverflowResult::AlwaysOverflows; |
| 3932 | } |
| 3933 | |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 3934 | if (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()) { |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 3935 | // The sign bit is clear in both cases: this CANNOT overflow. |
| 3936 | // Create a simple add instruction, and insert it into the struct. |
| 3937 | return OverflowResult::NeverOverflows; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | return OverflowResult::MayOverflow; |
| 3942 | } |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 3943 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3944 | /// Return true if we can prove that adding the two values of the |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 3945 | /// knownbits will not overflow. |
| 3946 | /// Otherwise return false. |
| 3947 | static bool checkRippleForSignedAdd(const KnownBits &LHSKnown, |
| 3948 | const KnownBits &RHSKnown) { |
| 3949 | // Addition of two 2's complement numbers having opposite signs will never |
| 3950 | // overflow. |
| 3951 | if ((LHSKnown.isNegative() && RHSKnown.isNonNegative()) || |
| 3952 | (LHSKnown.isNonNegative() && RHSKnown.isNegative())) |
| 3953 | return true; |
| 3954 | |
| 3955 | // If either of the values is known to be non-negative, adding them can only |
| 3956 | // overflow if the second is also non-negative, so we can assume that. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3957 | // Two non-negative numbers will only overflow if there is a carry to the |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 3958 | // sign bit, so we can check if even when the values are as big as possible |
| 3959 | // there is no overflow to the sign bit. |
| 3960 | if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) { |
| 3961 | APInt MaxLHS = ~LHSKnown.Zero; |
| 3962 | MaxLHS.clearSignBit(); |
| 3963 | APInt MaxRHS = ~RHSKnown.Zero; |
| 3964 | MaxRHS.clearSignBit(); |
| 3965 | APInt Result = std::move(MaxLHS) + std::move(MaxRHS); |
| 3966 | return Result.isSignBitClear(); |
| 3967 | } |
| 3968 | |
| 3969 | // If either of the values is known to be negative, adding them can only |
| 3970 | // overflow if the second is also negative, so we can assume that. |
| 3971 | // Two negative number will only overflow if there is no carry to the sign |
| 3972 | // bit, so we can check if even when the values are as small as possible |
| 3973 | // there is overflow to the sign bit. |
| 3974 | if (LHSKnown.isNegative() || RHSKnown.isNegative()) { |
| 3975 | APInt MinLHS = LHSKnown.One; |
| 3976 | MinLHS.clearSignBit(); |
| 3977 | APInt MinRHS = RHSKnown.One; |
| 3978 | MinRHS.clearSignBit(); |
| 3979 | APInt Result = std::move(MinLHS) + std::move(MinRHS); |
| 3980 | return Result.isSignBitSet(); |
| 3981 | } |
| 3982 | |
| 3983 | // If we reached here it means that we know nothing about the sign bits. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 3984 | // In this case we can't know if there will be an overflow, since by |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 3985 | // changing the sign bits any two values can be made to overflow. |
| 3986 | return false; |
| 3987 | } |
| 3988 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3989 | static OverflowResult computeOverflowForSignedAdd(const Value *LHS, |
| 3990 | const Value *RHS, |
| 3991 | const AddOperator *Add, |
| 3992 | const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 3993 | AssumptionCache *AC, |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 3994 | const Instruction *CxtI, |
| 3995 | const DominatorTree *DT) { |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 3996 | if (Add && Add->hasNoSignedWrap()) { |
| 3997 | return OverflowResult::NeverOverflows; |
| 3998 | } |
| 3999 | |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4000 | // If LHS and RHS each have at least two sign bits, the addition will look |
| 4001 | // like |
| 4002 | // |
| 4003 | // XX..... + |
| 4004 | // YY..... |
| 4005 | // |
| 4006 | // If the carry into the most significant position is 0, X and Y can't both |
| 4007 | // be 1 and therefore the carry out of the addition is also 0. |
| 4008 | // |
| 4009 | // If the carry into the most significant position is 1, X and Y can't both |
| 4010 | // be 0 and therefore the carry out of the addition is also 1. |
| 4011 | // |
| 4012 | // Since the carry into the most significant position is always equal to |
| 4013 | // the carry out of the addition, there is no signed overflow. |
| 4014 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
| 4015 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
| 4016 | return OverflowResult::NeverOverflows; |
| 4017 | |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4018 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4019 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT); |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4020 | |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4021 | if (checkRippleForSignedAdd(LHSKnown, RHSKnown)) |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4022 | return OverflowResult::NeverOverflows; |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4023 | |
| 4024 | // The remaining code needs Add to be available. Early returns if not so. |
| 4025 | if (!Add) |
| 4026 | return OverflowResult::MayOverflow; |
| 4027 | |
| 4028 | // If the sign of Add is the same as at least one of the operands, this add |
| 4029 | // CANNOT overflow. This is particularly useful when the sum is |
| 4030 | // @llvm.assume'ed non-negative rather than proved so from analyzing its |
| 4031 | // operands. |
| 4032 | bool LHSOrRHSKnownNonNegative = |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4033 | (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4034 | bool LHSOrRHSKnownNegative = |
Craig Topper | bb97372 | 2017-05-15 02:44:08 +0000 | [diff] [blame] | 4035 | (LHSKnown.isNegative() || RHSKnown.isNegative()); |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4036 | if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) { |
Craig Topper | 6e11a05 | 2017-05-08 16:22:48 +0000 | [diff] [blame] | 4037 | KnownBits AddKnown = computeKnownBits(Add, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4038 | if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) || |
| 4039 | (AddKnown.isNegative() && LHSOrRHSKnownNegative)) { |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4040 | return OverflowResult::NeverOverflows; |
| 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | return OverflowResult::MayOverflow; |
| 4045 | } |
| 4046 | |
Omer Paparo Bivas | fbb83de | 2018-05-10 19:46:19 +0000 | [diff] [blame] | 4047 | OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS, |
| 4048 | const Value *RHS, |
| 4049 | const DataLayout &DL, |
| 4050 | AssumptionCache *AC, |
| 4051 | const Instruction *CxtI, |
| 4052 | const DominatorTree *DT) { |
| 4053 | // If the LHS is negative and the RHS is non-negative, no unsigned wrap. |
| 4054 | KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4055 | KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT); |
| 4056 | if (LHSKnown.isNegative() && RHSKnown.isNonNegative()) |
| 4057 | return OverflowResult::NeverOverflows; |
| 4058 | |
| 4059 | return OverflowResult::MayOverflow; |
| 4060 | } |
| 4061 | |
| 4062 | OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS, |
| 4063 | const Value *RHS, |
| 4064 | const DataLayout &DL, |
| 4065 | AssumptionCache *AC, |
| 4066 | const Instruction *CxtI, |
| 4067 | const DominatorTree *DT) { |
| 4068 | // If LHS and RHS each have at least two sign bits, the subtraction |
| 4069 | // cannot overflow. |
| 4070 | if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 && |
| 4071 | ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1) |
| 4072 | return OverflowResult::NeverOverflows; |
| 4073 | |
| 4074 | KnownBits LHSKnown = computeKnownBits(LHS, DL, 0, AC, CxtI, DT); |
| 4075 | |
| 4076 | KnownBits RHSKnown = computeKnownBits(RHS, DL, 0, AC, CxtI, DT); |
| 4077 | |
| 4078 | // Subtraction of two 2's complement numbers having identical signs will |
| 4079 | // never overflow. |
| 4080 | if ((LHSKnown.isNegative() && RHSKnown.isNegative()) || |
| 4081 | (LHSKnown.isNonNegative() && RHSKnown.isNonNegative())) |
| 4082 | return OverflowResult::NeverOverflows; |
| 4083 | |
| 4084 | // TODO: implement logic similar to checkRippleForAdd |
| 4085 | return OverflowResult::MayOverflow; |
| 4086 | } |
| 4087 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4088 | bool llvm::isOverflowIntrinsicNoWrap(const IntrinsicInst *II, |
| 4089 | const DominatorTree &DT) { |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4090 | #ifndef NDEBUG |
| 4091 | auto IID = II->getIntrinsicID(); |
| 4092 | assert((IID == Intrinsic::sadd_with_overflow || |
| 4093 | IID == Intrinsic::uadd_with_overflow || |
| 4094 | IID == Intrinsic::ssub_with_overflow || |
| 4095 | IID == Intrinsic::usub_with_overflow || |
| 4096 | IID == Intrinsic::smul_with_overflow || |
| 4097 | IID == Intrinsic::umul_with_overflow) && |
| 4098 | "Not an overflow intrinsic!"); |
| 4099 | #endif |
| 4100 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4101 | SmallVector<const BranchInst *, 2> GuardingBranches; |
| 4102 | SmallVector<const ExtractValueInst *, 2> Results; |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4103 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4104 | for (const User *U : II->users()) { |
| 4105 | if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) { |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4106 | assert(EVI->getNumIndices() == 1 && "Obvious from CI's type"); |
| 4107 | |
| 4108 | if (EVI->getIndices()[0] == 0) |
| 4109 | Results.push_back(EVI); |
| 4110 | else { |
| 4111 | assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type"); |
| 4112 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4113 | for (const auto *U : EVI->users()) |
| 4114 | if (const auto *B = dyn_cast<BranchInst>(U)) { |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4115 | assert(B->isConditional() && "How else is it using an i1?"); |
| 4116 | GuardingBranches.push_back(B); |
| 4117 | } |
| 4118 | } |
| 4119 | } else { |
| 4120 | // We are using the aggregate directly in a way we don't want to analyze |
| 4121 | // here (storing it to a global, say). |
| 4122 | return false; |
| 4123 | } |
| 4124 | } |
| 4125 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4126 | auto AllUsesGuardedByBranch = [&](const BranchInst *BI) { |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4127 | BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1)); |
| 4128 | if (!NoWrapEdge.isSingleEdge()) |
| 4129 | return false; |
| 4130 | |
| 4131 | // Check if all users of the add are provably no-wrap. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4132 | for (const auto *Result : Results) { |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4133 | // If the extractvalue itself is not executed on overflow, the we don't |
| 4134 | // need to check each use separately, since domination is transitive. |
| 4135 | if (DT.dominates(NoWrapEdge, Result->getParent())) |
| 4136 | continue; |
| 4137 | |
| 4138 | for (auto &RU : Result->uses()) |
| 4139 | if (!DT.dominates(NoWrapEdge, RU)) |
| 4140 | return false; |
| 4141 | } |
| 4142 | |
| 4143 | return true; |
| 4144 | }; |
| 4145 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4146 | return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); |
Sanjoy Das | f49ca52 | 2016-05-29 00:34:42 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4150 | OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add, |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4151 | const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4152 | AssumptionCache *AC, |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4153 | const Instruction *CxtI, |
| 4154 | const DominatorTree *DT) { |
| 4155 | return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1), |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4156 | Add, DL, AC, CxtI, DT); |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4157 | } |
| 4158 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4159 | OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS, |
| 4160 | const Value *RHS, |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4161 | const DataLayout &DL, |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4162 | AssumptionCache *AC, |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4163 | const Instruction *CxtI, |
| 4164 | const DominatorTree *DT) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4165 | return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT); |
Jingyue Wu | 10fcea5 | 2015-08-20 18:27:04 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4168 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) { |
Eli Friedman | f1da33e | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4169 | // A memory operation returns normally if it isn't volatile. A volatile |
| 4170 | // operation is allowed to trap. |
| 4171 | // |
| 4172 | // An atomic operation isn't guaranteed to return in a reasonable amount of |
| 4173 | // time because it's possible for another thread to interfere with it for an |
| 4174 | // arbitrary length of time, but programs aren't allowed to rely on that. |
| 4175 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 4176 | return !LI->isVolatile(); |
| 4177 | if (const StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 4178 | return !SI->isVolatile(); |
| 4179 | if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I)) |
| 4180 | return !CXI->isVolatile(); |
| 4181 | if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I)) |
| 4182 | return !RMWI->isVolatile(); |
| 4183 | if (const MemIntrinsic *MII = dyn_cast<MemIntrinsic>(I)) |
| 4184 | return !MII->isVolatile(); |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4185 | |
Eli Friedman | f1da33e | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4186 | // If there is no successor, then execution can't transfer to it. |
| 4187 | if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) |
| 4188 | return !CRI->unwindsToCaller(); |
| 4189 | if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I)) |
| 4190 | return !CatchSwitch->unwindsToCaller(); |
| 4191 | if (isa<ResumeInst>(I)) |
| 4192 | return false; |
| 4193 | if (isa<ReturnInst>(I)) |
| 4194 | return false; |
Sebastian Pop | 4a4d245 | 2017-03-08 01:54:50 +0000 | [diff] [blame] | 4195 | if (isa<UnreachableInst>(I)) |
| 4196 | return false; |
Sanjoy Das | 9a65cd2 | 2016-06-08 17:48:36 +0000 | [diff] [blame] | 4197 | |
Eli Friedman | f1da33e | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4198 | // Calls can throw, or contain an infinite loop, or kill the process. |
Sanjoy Das | 0945530 | 2016-12-31 22:12:31 +0000 | [diff] [blame] | 4199 | if (auto CS = ImmutableCallSite(I)) { |
Sanjoy Das | 3bb2dbd | 2016-12-31 22:12:34 +0000 | [diff] [blame] | 4200 | // Call sites that throw have implicit non-local control flow. |
| 4201 | if (!CS.doesNotThrow()) |
| 4202 | return false; |
| 4203 | |
| 4204 | // Non-throwing call sites can loop infinitely, call exit/pthread_exit |
| 4205 | // etc. and thus not return. However, LLVM already assumes that |
| 4206 | // |
| 4207 | // - Thread exiting actions are modeled as writes to memory invisible to |
| 4208 | // the program. |
| 4209 | // |
| 4210 | // - Loops that don't have side effects (side effects are volatile/atomic |
| 4211 | // stores and IO) always terminate (see http://llvm.org/PR965). |
| 4212 | // Furthermore IO itself is also modeled as writes to memory invisible to |
| 4213 | // the program. |
| 4214 | // |
| 4215 | // We rely on those assumptions here, and use the memory effects of the call |
| 4216 | // target as a proxy for checking that it always returns. |
| 4217 | |
| 4218 | // FIXME: This isn't aggressive enough; a call which only writes to a global |
| 4219 | // is guaranteed to return. |
Sanjoy Das | d7e8206 | 2016-06-14 20:23:16 +0000 | [diff] [blame] | 4220 | return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory() || |
Dan Gohman | 2c74fe9 | 2017-11-08 21:59:51 +0000 | [diff] [blame] | 4221 | match(I, m_Intrinsic<Intrinsic::assume>()) || |
| 4222 | match(I, m_Intrinsic<Intrinsic::sideeffect>()); |
Eli Friedman | f1da33e | 2016-06-11 21:48:25 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | // Other instructions return normally. |
| 4226 | return true; |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Philip Reames | fbffd12 | 2018-03-08 21:25:30 +0000 | [diff] [blame] | 4229 | bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) { |
| 4230 | // TODO: This is slightly consdervative for invoke instruction since exiting |
| 4231 | // via an exception *is* normal control for them. |
| 4232 | for (auto I = BB->begin(), E = BB->end(); I != E; ++I) |
| 4233 | if (!isGuaranteedToTransferExecutionToSuccessor(&*I)) |
| 4234 | return false; |
| 4235 | return true; |
| 4236 | } |
| 4237 | |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4238 | bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I, |
| 4239 | const Loop *L) { |
| 4240 | // The loop header is guaranteed to be executed for every iteration. |
| 4241 | // |
| 4242 | // FIXME: Relax this constraint to cover all basic blocks that are |
| 4243 | // guaranteed to be executed at every iteration. |
| 4244 | if (I->getParent() != L->getHeader()) return false; |
| 4245 | |
| 4246 | for (const Instruction &LI : *L->getHeader()) { |
| 4247 | if (&LI == I) return true; |
| 4248 | if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false; |
| 4249 | } |
| 4250 | llvm_unreachable("Instruction not contained in its own parent basic block."); |
| 4251 | } |
| 4252 | |
| 4253 | bool llvm::propagatesFullPoison(const Instruction *I) { |
| 4254 | switch (I->getOpcode()) { |
Sanjoy Das | 7b0b408 | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4255 | case Instruction::Add: |
| 4256 | case Instruction::Sub: |
| 4257 | case Instruction::Xor: |
| 4258 | case Instruction::Trunc: |
| 4259 | case Instruction::BitCast: |
| 4260 | case Instruction::AddrSpaceCast: |
Sanjoy Das | 5cd6c5ca | 2017-02-22 06:52:32 +0000 | [diff] [blame] | 4261 | case Instruction::Mul: |
| 4262 | case Instruction::Shl: |
| 4263 | case Instruction::GetElementPtr: |
Sanjoy Das | 7b0b408 | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4264 | // These operations all propagate poison unconditionally. Note that poison |
| 4265 | // is not any particular value, so xor or subtraction of poison with |
| 4266 | // itself still yields poison, not zero. |
| 4267 | return true; |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4268 | |
Sanjoy Das | 7b0b408 | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4269 | case Instruction::AShr: |
| 4270 | case Instruction::SExt: |
| 4271 | // For these operations, one bit of the input is replicated across |
| 4272 | // multiple output bits. A replicated poison bit is still poison. |
| 4273 | return true; |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4274 | |
Sanjoy Das | 7b0b408 | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4275 | case Instruction::ICmp: |
| 4276 | // Comparing poison with any value yields poison. This is why, for |
| 4277 | // instance, x s< (x +nsw 1) can be folded to true. |
| 4278 | return true; |
Sanjoy Das | 70c2bbd | 2016-05-29 00:31:18 +0000 | [diff] [blame] | 4279 | |
Sanjoy Das | 7b0b408 | 2017-02-21 02:42:42 +0000 | [diff] [blame] | 4280 | default: |
| 4281 | return false; |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4282 | } |
| 4283 | } |
| 4284 | |
| 4285 | const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) { |
| 4286 | switch (I->getOpcode()) { |
| 4287 | case Instruction::Store: |
| 4288 | return cast<StoreInst>(I)->getPointerOperand(); |
| 4289 | |
| 4290 | case Instruction::Load: |
| 4291 | return cast<LoadInst>(I)->getPointerOperand(); |
| 4292 | |
| 4293 | case Instruction::AtomicCmpXchg: |
| 4294 | return cast<AtomicCmpXchgInst>(I)->getPointerOperand(); |
| 4295 | |
| 4296 | case Instruction::AtomicRMW: |
| 4297 | return cast<AtomicRMWInst>(I)->getPointerOperand(); |
| 4298 | |
| 4299 | case Instruction::UDiv: |
| 4300 | case Instruction::SDiv: |
| 4301 | case Instruction::URem: |
| 4302 | case Instruction::SRem: |
| 4303 | return I->getOperand(1); |
| 4304 | |
| 4305 | default: |
| 4306 | return nullptr; |
| 4307 | } |
| 4308 | } |
| 4309 | |
Sanjoy Das | 08989c7 | 2017-04-30 19:41:19 +0000 | [diff] [blame] | 4310 | bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) { |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4311 | // We currently only look for uses of poison values within the same basic |
| 4312 | // block, as that makes it easier to guarantee that the uses will be |
| 4313 | // executed given that PoisonI is executed. |
| 4314 | // |
| 4315 | // FIXME: Expand this to consider uses beyond the same basic block. To do |
| 4316 | // this, look out for the distinction between post-dominance and strong |
| 4317 | // post-dominance. |
| 4318 | const BasicBlock *BB = PoisonI->getParent(); |
| 4319 | |
| 4320 | // Set of instructions that we have proved will yield poison if PoisonI |
| 4321 | // does. |
| 4322 | SmallSet<const Value *, 16> YieldsPoison; |
Sanjoy Das | a6155b6 | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4323 | SmallSet<const BasicBlock *, 4> Visited; |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4324 | YieldsPoison.insert(PoisonI); |
Sanjoy Das | a6155b6 | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4325 | Visited.insert(PoisonI->getParent()); |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4326 | |
Sanjoy Das | a6155b6 | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4327 | BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end(); |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4328 | |
Sanjoy Das | a6155b6 | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4329 | unsigned Iter = 0; |
| 4330 | while (Iter++ < MaxDepth) { |
| 4331 | for (auto &I : make_range(Begin, End)) { |
| 4332 | if (&I != PoisonI) { |
| 4333 | const Value *NotPoison = getGuaranteedNonFullPoisonOp(&I); |
| 4334 | if (NotPoison != nullptr && YieldsPoison.count(NotPoison)) |
| 4335 | return true; |
| 4336 | if (!isGuaranteedToTransferExecutionToSuccessor(&I)) |
| 4337 | return false; |
| 4338 | } |
| 4339 | |
| 4340 | // Mark poison that propagates from I through uses of I. |
| 4341 | if (YieldsPoison.count(&I)) { |
| 4342 | for (const User *User : I.users()) { |
| 4343 | const Instruction *UserI = cast<Instruction>(User); |
| 4344 | if (propagatesFullPoison(UserI)) |
| 4345 | YieldsPoison.insert(User); |
| 4346 | } |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4347 | } |
| 4348 | } |
Sanjoy Das | a6155b6 | 2016-04-22 17:41:06 +0000 | [diff] [blame] | 4349 | |
| 4350 | if (auto *NextBB = BB->getSingleSuccessor()) { |
| 4351 | if (Visited.insert(NextBB).second) { |
| 4352 | BB = NextBB; |
| 4353 | Begin = BB->getFirstNonPHI()->getIterator(); |
| 4354 | End = BB->end(); |
| 4355 | continue; |
| 4356 | } |
| 4357 | } |
| 4358 | |
| 4359 | break; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4360 | } |
Jingyue Wu | 42f1d67 | 2015-07-28 18:22:40 +0000 | [diff] [blame] | 4361 | return false; |
| 4362 | } |
| 4363 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4364 | static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4365 | if (FMF.noNaNs()) |
| 4366 | return true; |
| 4367 | |
| 4368 | if (auto *C = dyn_cast<ConstantFP>(V)) |
| 4369 | return !C->isNaN(); |
| 4370 | return false; |
| 4371 | } |
| 4372 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 4373 | static bool isKnownNonZero(const Value *V) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4374 | if (auto *C = dyn_cast<ConstantFP>(V)) |
| 4375 | return !C->isZero(); |
| 4376 | return false; |
| 4377 | } |
| 4378 | |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 4379 | /// Match clamp pattern for float types without care about NaNs or signed zeros. |
| 4380 | /// Given non-min/max outer cmp/select from the clamp pattern this |
| 4381 | /// function recognizes if it can be substitued by a "canonical" min/max |
| 4382 | /// pattern. |
| 4383 | static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, |
| 4384 | Value *CmpLHS, Value *CmpRHS, |
| 4385 | Value *TrueVal, Value *FalseVal, |
| 4386 | Value *&LHS, Value *&RHS) { |
| 4387 | // Try to match |
| 4388 | // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2)) |
| 4389 | // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2)) |
| 4390 | // and return description of the outer Max/Min. |
| 4391 | |
| 4392 | // First, check if select has inverse order: |
| 4393 | if (CmpRHS == FalseVal) { |
| 4394 | std::swap(TrueVal, FalseVal); |
| 4395 | Pred = CmpInst::getInversePredicate(Pred); |
| 4396 | } |
| 4397 | |
| 4398 | // Assume success now. If there's no match, callers should not use these anyway. |
| 4399 | LHS = TrueVal; |
| 4400 | RHS = FalseVal; |
| 4401 | |
| 4402 | const APFloat *FC1; |
| 4403 | if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite()) |
| 4404 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4405 | |
| 4406 | const APFloat *FC2; |
| 4407 | switch (Pred) { |
| 4408 | case CmpInst::FCMP_OLT: |
| 4409 | case CmpInst::FCMP_OLE: |
| 4410 | case CmpInst::FCMP_ULT: |
| 4411 | case CmpInst::FCMP_ULE: |
| 4412 | if (match(FalseVal, |
| 4413 | m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)), |
| 4414 | m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
| 4415 | FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan) |
| 4416 | return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false}; |
| 4417 | break; |
| 4418 | case CmpInst::FCMP_OGT: |
| 4419 | case CmpInst::FCMP_OGE: |
| 4420 | case CmpInst::FCMP_UGT: |
| 4421 | case CmpInst::FCMP_UGE: |
| 4422 | if (match(FalseVal, |
| 4423 | m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)), |
| 4424 | m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) && |
| 4425 | FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan) |
| 4426 | return {SPF_FMINNUM, SPNB_RETURNS_ANY, false}; |
| 4427 | break; |
| 4428 | default: |
| 4429 | break; |
| 4430 | } |
| 4431 | |
| 4432 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4433 | } |
| 4434 | |
Artur Gainullin | af7ba8f | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4435 | /// Recognize variations of: |
| 4436 | /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v))) |
| 4437 | static SelectPatternResult matchClamp(CmpInst::Predicate Pred, |
| 4438 | Value *CmpLHS, Value *CmpRHS, |
| 4439 | Value *TrueVal, Value *FalseVal) { |
| 4440 | // Swap the select operands and predicate to match the patterns below. |
| 4441 | if (CmpRHS != TrueVal) { |
| 4442 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4443 | std::swap(TrueVal, FalseVal); |
| 4444 | } |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4445 | const APInt *C1; |
| 4446 | if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) { |
| 4447 | const APInt *C2; |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4448 | // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1) |
| 4449 | if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4450 | C1->slt(*C2) && Pred == CmpInst::ICMP_SLT) |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4451 | return {SPF_SMAX, SPNB_NA, false}; |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4452 | |
| 4453 | // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1) |
| 4454 | if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4455 | C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT) |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4456 | return {SPF_SMIN, SPNB_NA, false}; |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4457 | |
| 4458 | // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1) |
| 4459 | if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4460 | C1->ult(*C2) && Pred == CmpInst::ICMP_ULT) |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4461 | return {SPF_UMAX, SPNB_NA, false}; |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4462 | |
| 4463 | // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1) |
| 4464 | if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) && |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4465 | C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT) |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4466 | return {SPF_UMIN, SPNB_NA, false}; |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4467 | } |
Artur Gainullin | af7ba8f | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4468 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4469 | } |
| 4470 | |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4471 | /// Recognize variations of: |
| 4472 | /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c)) |
| 4473 | static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, |
| 4474 | Value *CmpLHS, Value *CmpRHS, |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4475 | Value *TVal, Value *FVal, |
| 4476 | unsigned Depth) { |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4477 | // TODO: Allow FP min/max with nnan/nsz. |
| 4478 | assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison"); |
| 4479 | |
| 4480 | Value *A, *B; |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4481 | SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1); |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4482 | if (!SelectPatternResult::isMinOrMax(L.Flavor)) |
| 4483 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4484 | |
| 4485 | Value *C, *D; |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4486 | SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1); |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4487 | if (L.Flavor != R.Flavor) |
| 4488 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4489 | |
Sanjay Patel | e63d8dd | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4490 | // We have something like: x Pred y ? min(a, b) : min(c, d). |
| 4491 | // Try to match the compare to the min/max operations of the select operands. |
| 4492 | // First, make sure we have the right compare predicate. |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4493 | switch (L.Flavor) { |
| 4494 | case SPF_SMIN: |
| 4495 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) { |
| 4496 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4497 | std::swap(CmpLHS, CmpRHS); |
| 4498 | } |
| 4499 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) |
| 4500 | break; |
| 4501 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4502 | case SPF_SMAX: |
| 4503 | if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) { |
| 4504 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4505 | std::swap(CmpLHS, CmpRHS); |
| 4506 | } |
| 4507 | if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) |
| 4508 | break; |
| 4509 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4510 | case SPF_UMIN: |
| 4511 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) { |
| 4512 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4513 | std::swap(CmpLHS, CmpRHS); |
| 4514 | } |
| 4515 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) |
| 4516 | break; |
| 4517 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4518 | case SPF_UMAX: |
| 4519 | if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) { |
| 4520 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 4521 | std::swap(CmpLHS, CmpRHS); |
| 4522 | } |
| 4523 | if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) |
| 4524 | break; |
| 4525 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4526 | default: |
Sanjay Patel | 7dfe96a | 2018-01-08 18:31:13 +0000 | [diff] [blame] | 4527 | return {SPF_UNKNOWN, SPNB_NA, false}; |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
Sanjay Patel | e63d8dd | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4530 | // If there is a common operand in the already matched min/max and the other |
| 4531 | // min/max operands match the compare operands (either directly or inverted), |
| 4532 | // then this is min/max of the same flavor. |
| 4533 | |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4534 | // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
Sanjay Patel | e63d8dd | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4535 | // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b)) |
| 4536 | if (D == B) { |
| 4537 | if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
| 4538 | match(A, m_Not(m_Specific(CmpRHS))))) |
| 4539 | return {L.Flavor, SPNB_NA, false}; |
| 4540 | } |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4541 | // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
Sanjay Patel | e63d8dd | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4542 | // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d)) |
| 4543 | if (C == B) { |
| 4544 | if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
| 4545 | match(A, m_Not(m_Specific(CmpRHS))))) |
| 4546 | return {L.Flavor, SPNB_NA, false}; |
| 4547 | } |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4548 | // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
Sanjay Patel | e63d8dd | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4549 | // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a)) |
| 4550 | if (D == A) { |
| 4551 | if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) && |
| 4552 | match(B, m_Not(m_Specific(CmpRHS))))) |
| 4553 | return {L.Flavor, SPNB_NA, false}; |
| 4554 | } |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4555 | // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
Sanjay Patel | e63d8dd | 2018-01-11 15:13:47 +0000 | [diff] [blame] | 4556 | // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d)) |
| 4557 | if (C == A) { |
| 4558 | if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) && |
| 4559 | match(B, m_Not(m_Specific(CmpRHS))))) |
| 4560 | return {L.Flavor, SPNB_NA, false}; |
| 4561 | } |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4562 | |
| 4563 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4564 | } |
| 4565 | |
Artur Gainullin | af7ba8f | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4566 | /// Match non-obvious integer minimum and maximum sequences. |
| 4567 | static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, |
| 4568 | Value *CmpLHS, Value *CmpRHS, |
| 4569 | Value *TrueVal, Value *FalseVal, |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4570 | Value *&LHS, Value *&RHS, |
| 4571 | unsigned Depth) { |
Artur Gainullin | af7ba8f | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4572 | // Assume success. If there's no match, callers should not use these anyway. |
| 4573 | LHS = TrueVal; |
| 4574 | RHS = FalseVal; |
| 4575 | |
| 4576 | SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal); |
| 4577 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
| 4578 | return SPR; |
Sanjay Patel | 0c1c70a | 2017-01-20 22:18:47 +0000 | [diff] [blame] | 4579 | |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4580 | SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth); |
Sanjay Patel | 7811430 | 2018-01-02 20:56:45 +0000 | [diff] [blame] | 4581 | if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN) |
| 4582 | return SPR; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4583 | |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4584 | if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT) |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4585 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4586 | |
Sanjay Patel | cfcc42b | 2016-11-13 20:04:52 +0000 | [diff] [blame] | 4587 | // Z = X -nsw Y |
| 4588 | // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0) |
| 4589 | // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0) |
| 4590 | if (match(TrueVal, m_Zero()) && |
Sanjay Patel | 24c6f88 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4591 | match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4592 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; |
Sanjay Patel | cfcc42b | 2016-11-13 20:04:52 +0000 | [diff] [blame] | 4593 | |
| 4594 | // Z = X -nsw Y |
| 4595 | // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0) |
| 4596 | // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0) |
| 4597 | if (match(FalseVal, m_Zero()) && |
Sanjay Patel | 24c6f88 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4598 | match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS)))) |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4599 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; |
Sanjay Patel | cfcc42b | 2016-11-13 20:04:52 +0000 | [diff] [blame] | 4600 | |
Artur Gainullin | af7ba8f | 2017-10-27 20:53:41 +0000 | [diff] [blame] | 4601 | const APInt *C1; |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4602 | if (!match(CmpRHS, m_APInt(C1))) |
| 4603 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4604 | |
| 4605 | // An unsigned min/max can be written with a signed compare. |
| 4606 | const APInt *C2; |
| 4607 | if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) || |
| 4608 | (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) { |
| 4609 | // Is the sign bit set? |
| 4610 | // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX |
| 4611 | // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN |
Craig Topper | 81d772c | 2017-11-08 19:38:45 +0000 | [diff] [blame] | 4612 | if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() && |
| 4613 | C2->isMaxSignedValue()) |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4614 | return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4615 | |
| 4616 | // Is the sign bit clear? |
| 4617 | // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX |
| 4618 | // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4619 | if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() && |
| 4620 | C2->isMinSignedValue()) |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4621 | return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false}; |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | // Look through 'not' ops to find disguised signed min/max. |
| 4625 | // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C) |
| 4626 | // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C) |
| 4627 | if (match(TrueVal, m_Not(m_Specific(CmpLHS))) && |
Sanjay Patel | 24c6f88 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4628 | match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2) |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4629 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false}; |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4630 | |
| 4631 | // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X) |
| 4632 | // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X) |
| 4633 | if (match(FalseVal, m_Not(m_Specific(CmpLHS))) && |
Sanjay Patel | 24c6f88 | 2017-01-21 17:51:25 +0000 | [diff] [blame] | 4634 | match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2) |
Nikolai Bozhenov | 8dcab54 | 2017-10-19 15:36:18 +0000 | [diff] [blame] | 4635 | return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false}; |
Sanjay Patel | 819f096 | 2016-11-13 19:30:19 +0000 | [diff] [blame] | 4636 | |
| 4637 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4638 | } |
| 4639 | |
Chen Zheng | 69bb064 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4640 | bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) { |
Chen Zheng | fdf13ef | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4641 | assert(X && Y && "Invalid operand"); |
| 4642 | |
Chen Zheng | 69bb064 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4643 | // X = sub (0, Y) || X = sub nsw (0, Y) |
| 4644 | if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) || |
| 4645 | (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y))))) |
Chen Zheng | fdf13ef | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4646 | return true; |
| 4647 | |
Chen Zheng | 69bb064 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4648 | // Y = sub (0, X) || Y = sub nsw (0, X) |
| 4649 | if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) || |
| 4650 | (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X))))) |
Chen Zheng | fdf13ef | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4651 | return true; |
| 4652 | |
Chen Zheng | 69bb064 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4653 | // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A) |
Chen Zheng | fdf13ef | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4654 | Value *A, *B; |
Chen Zheng | 69bb064 | 2018-07-21 12:27:54 +0000 | [diff] [blame] | 4655 | return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) && |
| 4656 | match(Y, m_Sub(m_Specific(B), m_Specific(A))))) || |
| 4657 | (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) && |
| 4658 | match(Y, m_NSWSub(m_Specific(B), m_Specific(A))))); |
Chen Zheng | fdf13ef | 2018-07-12 03:06:04 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4661 | static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, |
| 4662 | FastMathFlags FMF, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4663 | Value *CmpLHS, Value *CmpRHS, |
| 4664 | Value *TrueVal, Value *FalseVal, |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4665 | Value *&LHS, Value *&RHS, |
| 4666 | unsigned Depth) { |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4667 | LHS = CmpLHS; |
| 4668 | RHS = CmpRHS; |
| 4669 | |
Sanjay Patel | 9a39979 | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4670 | // Signed zero may return inconsistent results between implementations. |
| 4671 | // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0 |
| 4672 | // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1) |
| 4673 | // Therefore, we behave conservatively and only proceed if at least one of the |
| 4674 | // operands is known to not be zero or if we don't care about signed zero. |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4675 | switch (Pred) { |
| 4676 | default: break; |
Sanjay Patel | 9a39979 | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4677 | // FIXME: Include OGT/OLT/UGT/ULT. |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4678 | case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE: |
| 4679 | case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE: |
| 4680 | if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 4681 | !isKnownNonZero(CmpRHS)) |
| 4682 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4683 | } |
| 4684 | |
| 4685 | SelectPatternNaNBehavior NaNBehavior = SPNB_NA; |
| 4686 | bool Ordered = false; |
| 4687 | |
| 4688 | // When given one NaN and one non-NaN input: |
| 4689 | // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input. |
| 4690 | // - A simple C99 (a < b ? a : b) construction will return 'b' (as the |
| 4691 | // ordered comparison fails), which could be NaN or non-NaN. |
| 4692 | // so here we discover exactly what NaN behavior is required/accepted. |
| 4693 | if (CmpInst::isFPPredicate(Pred)) { |
| 4694 | bool LHSSafe = isKnownNonNaN(CmpLHS, FMF); |
| 4695 | bool RHSSafe = isKnownNonNaN(CmpRHS, FMF); |
| 4696 | |
| 4697 | if (LHSSafe && RHSSafe) { |
| 4698 | // Both operands are known non-NaN. |
| 4699 | NaNBehavior = SPNB_RETURNS_ANY; |
| 4700 | } else if (CmpInst::isOrdered(Pred)) { |
| 4701 | // An ordered comparison will return false when given a NaN, so it |
| 4702 | // returns the RHS. |
| 4703 | Ordered = true; |
| 4704 | if (LHSSafe) |
James Molloy | 8990b06 | 2015-08-12 15:11:43 +0000 | [diff] [blame] | 4705 | // LHS is non-NaN, so if RHS is NaN then NaN will be returned. |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4706 | NaNBehavior = SPNB_RETURNS_NAN; |
| 4707 | else if (RHSSafe) |
| 4708 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 4709 | else |
| 4710 | // Completely unsafe. |
| 4711 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4712 | } else { |
| 4713 | Ordered = false; |
| 4714 | // An unordered comparison will return true when given a NaN, so it |
| 4715 | // returns the LHS. |
| 4716 | if (LHSSafe) |
James Molloy | 8990b06 | 2015-08-12 15:11:43 +0000 | [diff] [blame] | 4717 | // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned. |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4718 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 4719 | else if (RHSSafe) |
| 4720 | NaNBehavior = SPNB_RETURNS_NAN; |
| 4721 | else |
| 4722 | // Completely unsafe. |
| 4723 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4724 | } |
| 4725 | } |
| 4726 | |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4727 | if (TrueVal == CmpRHS && FalseVal == CmpLHS) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4728 | std::swap(CmpLHS, CmpRHS); |
| 4729 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 4730 | if (NaNBehavior == SPNB_RETURNS_NAN) |
| 4731 | NaNBehavior = SPNB_RETURNS_OTHER; |
| 4732 | else if (NaNBehavior == SPNB_RETURNS_OTHER) |
| 4733 | NaNBehavior = SPNB_RETURNS_NAN; |
| 4734 | Ordered = !Ordered; |
| 4735 | } |
| 4736 | |
| 4737 | // ([if]cmp X, Y) ? X : Y |
| 4738 | if (TrueVal == CmpLHS && FalseVal == CmpRHS) { |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4739 | switch (Pred) { |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4740 | default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality. |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4741 | case ICmpInst::ICMP_UGT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4742 | case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4743 | case ICmpInst::ICMP_SGT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4744 | case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4745 | case ICmpInst::ICMP_ULT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4746 | case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4747 | case ICmpInst::ICMP_SLT: |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4748 | case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false}; |
| 4749 | case FCmpInst::FCMP_UGT: |
| 4750 | case FCmpInst::FCMP_UGE: |
| 4751 | case FCmpInst::FCMP_OGT: |
| 4752 | case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered}; |
| 4753 | case FCmpInst::FCMP_ULT: |
| 4754 | case FCmpInst::FCMP_ULE: |
| 4755 | case FCmpInst::FCMP_OLT: |
| 4756 | case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4757 | } |
| 4758 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 4759 | |
Chen Zheng | ccc8422 | 2018-07-16 02:23:00 +0000 | [diff] [blame] | 4760 | if (isKnownNegation(TrueVal, FalseVal)) { |
| 4761 | // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can |
| 4762 | // match against either LHS or sext(LHS). |
| 4763 | auto MaybeSExtCmpLHS = |
| 4764 | m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS))); |
| 4765 | auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes()); |
| 4766 | auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One()); |
| 4767 | if (match(TrueVal, MaybeSExtCmpLHS)) { |
| 4768 | // Set the return values. If the compare uses the negated value (-X >s 0), |
| 4769 | // swap the return values because the negated value is always 'RHS'. |
Sanjay Patel | 284ba0c | 2018-07-02 14:43:40 +0000 | [diff] [blame] | 4770 | LHS = TrueVal; |
| 4771 | RHS = FalseVal; |
Chen Zheng | ccc8422 | 2018-07-16 02:23:00 +0000 | [diff] [blame] | 4772 | if (match(CmpLHS, m_Neg(m_Specific(FalseVal)))) |
| 4773 | std::swap(LHS, RHS); |
| 4774 | |
| 4775 | // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X) |
| 4776 | // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X) |
| 4777 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
| 4778 | return {SPF_ABS, SPNB_NA, false}; |
| 4779 | |
| 4780 | // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X) |
| 4781 | // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X) |
| 4782 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
| 4783 | return {SPF_NABS, SPNB_NA, false}; |
| 4784 | } |
| 4785 | else if (match(FalseVal, MaybeSExtCmpLHS)) { |
| 4786 | // Set the return values. If the compare uses the negated value (-X >s 0), |
| 4787 | // swap the return values because the negated value is always 'RHS'. |
Sanjay Patel | 284ba0c | 2018-07-02 14:43:40 +0000 | [diff] [blame] | 4788 | LHS = FalseVal; |
| 4789 | RHS = TrueVal; |
Chen Zheng | ccc8422 | 2018-07-16 02:23:00 +0000 | [diff] [blame] | 4790 | if (match(CmpLHS, m_Neg(m_Specific(TrueVal)))) |
| 4791 | std::swap(LHS, RHS); |
| 4792 | |
| 4793 | // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X) |
| 4794 | // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X) |
| 4795 | if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes)) |
| 4796 | return {SPF_NABS, SPNB_NA, false}; |
| 4797 | |
| 4798 | // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X) |
| 4799 | // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X) |
| 4800 | if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne)) |
| 4801 | return {SPF_ABS, SPNB_NA, false}; |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4802 | } |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 4805 | if (CmpInst::isIntPredicate(Pred)) |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4806 | return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth); |
Nikolai Bozhenov | 1545eb3 | 2017-08-04 12:22:17 +0000 | [diff] [blame] | 4807 | |
| 4808 | // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar |
| 4809 | // may return either -0.0 or 0.0, so fcmp/select pair has stricter |
| 4810 | // semantics than minNum. Be conservative in such case. |
| 4811 | if (NaNBehavior != SPNB_RETURNS_ANY || |
| 4812 | (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) && |
| 4813 | !isKnownNonZero(CmpRHS))) |
| 4814 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4815 | |
| 4816 | return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS); |
James Molloy | 71b91c2 | 2015-05-11 14:42:20 +0000 | [diff] [blame] | 4817 | } |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4818 | |
Nikolai Bozhenov | 74c047e | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 4819 | /// Helps to match a select pattern in case of a type mismatch. |
| 4820 | /// |
| 4821 | /// The function processes the case when type of true and false values of a |
| 4822 | /// select instruction differs from type of the cmp instruction operands because |
Vedant Kumar | 1a8456d | 2018-03-02 18:57:02 +0000 | [diff] [blame] | 4823 | /// of a cast instruction. The function checks if it is legal to move the cast |
Nikolai Bozhenov | 74c047e | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 4824 | /// operation after "select". If yes, it returns the new second value of |
| 4825 | /// "select" (with the assumption that cast is moved): |
| 4826 | /// 1. As operand of cast instruction when both values of "select" are same cast |
| 4827 | /// instructions. |
| 4828 | /// 2. As restored constant (by applying reverse cast operation) when the first |
| 4829 | /// value of the "select" is a cast operation and the second value is a |
| 4830 | /// constant. |
| 4831 | /// NOTE: We return only the new second value because the first value could be |
| 4832 | /// accessed as operand of cast instruction. |
James Molloy | 569cea6 | 2015-09-02 17:25:25 +0000 | [diff] [blame] | 4833 | static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, |
| 4834 | Instruction::CastOps *CastOp) { |
Sanjay Patel | 14a4b81 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4835 | auto *Cast1 = dyn_cast<CastInst>(V1); |
| 4836 | if (!Cast1) |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4837 | return nullptr; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4838 | |
Sanjay Patel | 14a4b81 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4839 | *CastOp = Cast1->getOpcode(); |
| 4840 | Type *SrcTy = Cast1->getSrcTy(); |
| 4841 | if (auto *Cast2 = dyn_cast<CastInst>(V2)) { |
| 4842 | // If V1 and V2 are both the same cast from the same type, look through V1. |
| 4843 | if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy()) |
| 4844 | return Cast2->getOperand(0); |
James Molloy | 569cea6 | 2015-09-02 17:25:25 +0000 | [diff] [blame] | 4845 | return nullptr; |
| 4846 | } |
| 4847 | |
Sanjay Patel | 14a4b81 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4848 | auto *C = dyn_cast<Constant>(V2); |
| 4849 | if (!C) |
| 4850 | return nullptr; |
| 4851 | |
David Majnemer | d2a074b | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 4852 | Constant *CastedTo = nullptr; |
Sanjay Patel | 14a4b81 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4853 | switch (*CastOp) { |
| 4854 | case Instruction::ZExt: |
| 4855 | if (CmpI->isUnsigned()) |
| 4856 | CastedTo = ConstantExpr::getTrunc(C, SrcTy); |
| 4857 | break; |
| 4858 | case Instruction::SExt: |
| 4859 | if (CmpI->isSigned()) |
| 4860 | CastedTo = ConstantExpr::getTrunc(C, SrcTy, true); |
| 4861 | break; |
| 4862 | case Instruction::Trunc: |
Nikolai Bozhenov | 74c047e | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 4863 | Constant *CmpConst; |
Nikolai Bozhenov | 9723f12 | 2017-10-18 14:24:50 +0000 | [diff] [blame] | 4864 | if (match(CmpI->getOperand(1), m_Constant(CmpConst)) && |
| 4865 | CmpConst->getType() == SrcTy) { |
Nikolai Bozhenov | 74c047e | 2017-10-18 09:28:09 +0000 | [diff] [blame] | 4866 | // Here we have the following case: |
| 4867 | // |
| 4868 | // %cond = cmp iN %x, CmpConst |
| 4869 | // %tr = trunc iN %x to iK |
| 4870 | // %narrowsel = select i1 %cond, iK %t, iK C |
| 4871 | // |
| 4872 | // We can always move trunc after select operation: |
| 4873 | // |
| 4874 | // %cond = cmp iN %x, CmpConst |
| 4875 | // %widesel = select i1 %cond, iN %x, iN CmpConst |
| 4876 | // %tr = trunc iN %widesel to iK |
| 4877 | // |
| 4878 | // Note that C could be extended in any way because we don't care about |
| 4879 | // upper bits after truncation. It can't be abs pattern, because it would |
| 4880 | // look like: |
| 4881 | // |
| 4882 | // select i1 %cond, x, -x. |
| 4883 | // |
| 4884 | // So only min/max pattern could be matched. Such match requires widened C |
| 4885 | // == CmpConst. That is why set widened C = CmpConst, condition trunc |
| 4886 | // CmpConst == C is checked below. |
| 4887 | CastedTo = CmpConst; |
| 4888 | } else { |
| 4889 | CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned()); |
| 4890 | } |
Sanjay Patel | 14a4b81 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4891 | break; |
| 4892 | case Instruction::FPTrunc: |
| 4893 | CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true); |
| 4894 | break; |
| 4895 | case Instruction::FPExt: |
| 4896 | CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true); |
| 4897 | break; |
| 4898 | case Instruction::FPToUI: |
| 4899 | CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true); |
| 4900 | break; |
| 4901 | case Instruction::FPToSI: |
| 4902 | CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true); |
| 4903 | break; |
| 4904 | case Instruction::UIToFP: |
| 4905 | CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true); |
| 4906 | break; |
| 4907 | case Instruction::SIToFP: |
| 4908 | CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true); |
| 4909 | break; |
| 4910 | default: |
| 4911 | break; |
| 4912 | } |
David Majnemer | d2a074b | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 4913 | |
| 4914 | if (!CastedTo) |
| 4915 | return nullptr; |
| 4916 | |
David Majnemer | d2a074b | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 4917 | // Make sure the cast doesn't lose any information. |
Sanjay Patel | 14a4b81 | 2017-01-29 16:34:57 +0000 | [diff] [blame] | 4918 | Constant *CastedBack = |
| 4919 | ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true); |
David Majnemer | d2a074b | 2016-04-29 18:40:34 +0000 | [diff] [blame] | 4920 | if (CastedBack != C) |
| 4921 | return nullptr; |
| 4922 | |
| 4923 | return CastedTo; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Sanjay Patel | e8dc090 | 2016-05-23 17:57:54 +0000 | [diff] [blame] | 4926 | SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4927 | Instruction::CastOps *CastOp, |
| 4928 | unsigned Depth) { |
| 4929 | if (Depth >= MaxDepth) |
| 4930 | return {SPF_UNKNOWN, SPNB_NA, false}; |
| 4931 | |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4932 | SelectInst *SI = dyn_cast<SelectInst>(V); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4933 | if (!SI) return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4934 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4935 | CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition()); |
| 4936 | if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4937 | |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4938 | CmpInst::Predicate Pred = CmpI->getPredicate(); |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4939 | Value *CmpLHS = CmpI->getOperand(0); |
| 4940 | Value *CmpRHS = CmpI->getOperand(1); |
| 4941 | Value *TrueVal = SI->getTrueValue(); |
| 4942 | Value *FalseVal = SI->getFalseValue(); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4943 | FastMathFlags FMF; |
| 4944 | if (isa<FPMathOperator>(CmpI)) |
| 4945 | FMF = CmpI->getFastMathFlags(); |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4946 | |
| 4947 | // Bail out early. |
| 4948 | if (CmpI->isEquality()) |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4949 | return {SPF_UNKNOWN, SPNB_NA, false}; |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4950 | |
| 4951 | // Deal with type mismatches. |
| 4952 | if (CastOp && CmpLHS->getType() != TrueVal->getType()) { |
Sanjay Patel | 9a39979 | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4953 | if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) { |
| 4954 | // If this is a potential fmin/fmax with a cast to integer, then ignore |
| 4955 | // -0.0 because there is no corresponding integer value. |
| 4956 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
| 4957 | FMF.setNoSignedZeros(); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4958 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4959 | cast<CastInst>(TrueVal)->getOperand(0), C, |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4960 | LHS, RHS, Depth); |
Sanjay Patel | 9a39979 | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4961 | } |
| 4962 | if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) { |
| 4963 | // If this is a potential fmin/fmax with a cast to integer, then ignore |
| 4964 | // -0.0 because there is no corresponding integer value. |
| 4965 | if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI) |
| 4966 | FMF.setNoSignedZeros(); |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4967 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4968 | C, cast<CastInst>(FalseVal)->getOperand(0), |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4969 | LHS, RHS, Depth); |
Sanjay Patel | 9a39979 | 2017-12-26 15:09:19 +0000 | [diff] [blame] | 4970 | } |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4971 | } |
James Molloy | 134bec2 | 2015-08-11 09:12:57 +0000 | [diff] [blame] | 4972 | return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal, |
Sanjay Patel | 1d91ec3 | 2018-01-24 15:20:37 +0000 | [diff] [blame] | 4973 | LHS, RHS, Depth); |
James Molloy | 270ef8c | 2015-05-15 16:04:50 +0000 | [diff] [blame] | 4974 | } |
Sanjoy Das | a7e1378 | 2015-10-24 05:37:35 +0000 | [diff] [blame] | 4975 | |
Sanjay Patel | 7ed0bc2 | 2018-03-06 16:57:55 +0000 | [diff] [blame] | 4976 | CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) { |
| 4977 | if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT; |
| 4978 | if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT; |
| 4979 | if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT; |
| 4980 | if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT; |
| 4981 | if (SPF == SPF_FMINNUM) |
| 4982 | return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; |
| 4983 | if (SPF == SPF_FMAXNUM) |
| 4984 | return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; |
| 4985 | llvm_unreachable("unhandled!"); |
| 4986 | } |
| 4987 | |
| 4988 | SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) { |
| 4989 | if (SPF == SPF_SMIN) return SPF_SMAX; |
| 4990 | if (SPF == SPF_UMIN) return SPF_UMAX; |
| 4991 | if (SPF == SPF_SMAX) return SPF_SMIN; |
| 4992 | if (SPF == SPF_UMAX) return SPF_UMIN; |
| 4993 | llvm_unreachable("unhandled!"); |
| 4994 | } |
| 4995 | |
| 4996 | CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) { |
| 4997 | return getMinMaxPred(getInverseMinMaxFlavor(SPF)); |
| 4998 | } |
| 4999 | |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5000 | /// Return true if "icmp Pred LHS RHS" is always true. |
Chad Rosier | e42b44b | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5001 | static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, |
| 5002 | const Value *RHS, const DataLayout &DL, |
| 5003 | unsigned Depth) { |
Sanjoy Das | af1400f | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5004 | assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!"); |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5005 | if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS) |
| 5006 | return true; |
| 5007 | |
| 5008 | switch (Pred) { |
| 5009 | default: |
| 5010 | return false; |
| 5011 | |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5012 | case CmpInst::ICMP_SLE: { |
Sanjoy Das | af1400f | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5013 | const APInt *C; |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5014 | |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5015 | // LHS s<= LHS +_{nsw} C if C >= 0 |
Sanjoy Das | dc26df4 | 2015-11-11 00:16:41 +0000 | [diff] [blame] | 5016 | if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C)))) |
Sanjoy Das | af1400f | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5017 | return !C->isNegative(); |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5018 | return false; |
| 5019 | } |
| 5020 | |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5021 | case CmpInst::ICMP_ULE: { |
Sanjoy Das | af1400f | 2015-11-10 23:56:15 +0000 | [diff] [blame] | 5022 | const APInt *C; |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5023 | |
Sanjoy Das | dc26df4 | 2015-11-11 00:16:41 +0000 | [diff] [blame] | 5024 | // LHS u<= LHS +_{nuw} C for any C |
| 5025 | if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C)))) |
Sanjoy Das | c01b4d2 | 2015-11-06 19:01:03 +0000 | [diff] [blame] | 5026 | return true; |
Sanjoy Das | 9256810 | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5027 | |
| 5028 | // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB) |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5029 | auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B, |
| 5030 | const Value *&X, |
Sanjoy Das | 9256810 | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5031 | const APInt *&CA, const APInt *&CB) { |
| 5032 | if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) && |
| 5033 | match(B, m_NUWAdd(m_Specific(X), m_APInt(CB)))) |
| 5034 | return true; |
| 5035 | |
| 5036 | // If X & C == 0 then (X | C) == X +_{nuw} C |
| 5037 | if (match(A, m_Or(m_Value(X), m_APInt(CA))) && |
| 5038 | match(B, m_Or(m_Specific(X), m_APInt(CB)))) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 5039 | KnownBits Known(CA->getBitWidth()); |
Chad Rosier | e42b44b | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5040 | computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr, |
| 5041 | /*CxtI*/ nullptr, /*DT*/ nullptr); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 5042 | if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero)) |
Sanjoy Das | 9256810 | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5043 | return true; |
| 5044 | } |
| 5045 | |
| 5046 | return false; |
| 5047 | }; |
| 5048 | |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5049 | const Value *X; |
Sanjoy Das | 9256810 | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5050 | const APInt *CLHS, *CRHS; |
Sanjoy Das | dc26df4 | 2015-11-11 00:16:41 +0000 | [diff] [blame] | 5051 | if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS)) |
| 5052 | return CLHS->ule(*CRHS); |
Sanjoy Das | 9256810 | 2015-11-10 23:56:20 +0000 | [diff] [blame] | 5053 | |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5054 | return false; |
| 5055 | } |
| 5056 | } |
| 5057 | } |
| 5058 | |
| 5059 | /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5060 | /// ALHS ARHS" is true. Otherwise, return None. |
| 5061 | static Optional<bool> |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5062 | isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, |
Chad Rosier | e42b44b | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5063 | const Value *ARHS, const Value *BLHS, const Value *BRHS, |
| 5064 | const DataLayout &DL, unsigned Depth) { |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5065 | switch (Pred) { |
| 5066 | default: |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5067 | return None; |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5068 | |
| 5069 | case CmpInst::ICMP_SLT: |
| 5070 | case CmpInst::ICMP_SLE: |
Chad Rosier | e42b44b | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5071 | if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) && |
| 5072 | isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth)) |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5073 | return true; |
| 5074 | return None; |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5075 | |
| 5076 | case CmpInst::ICMP_ULT: |
| 5077 | case CmpInst::ICMP_ULE: |
Chad Rosier | e42b44b | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5078 | if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) && |
| 5079 | isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth)) |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5080 | return true; |
| 5081 | return None; |
Sanjoy Das | 9349dcc | 2015-11-06 19:00:57 +0000 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | |
Chad Rosier | 226a734 | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5085 | /// Return true if the operands of the two compares match. IsSwappedOps is true |
| 5086 | /// when the operands match, but are swapped. |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5087 | static bool isMatchingOps(const Value *ALHS, const Value *ARHS, |
| 5088 | const Value *BLHS, const Value *BRHS, |
Chad Rosier | 226a734 | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5089 | bool &IsSwappedOps) { |
| 5090 | |
| 5091 | bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS); |
| 5092 | IsSwappedOps = (ALHS == BRHS && ARHS == BLHS); |
| 5093 | return IsMatchingOps || IsSwappedOps; |
| 5094 | } |
| 5095 | |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5096 | /// Return true if "icmp1 APred ALHS ARHS" implies "icmp2 BPred BLHS BRHS" is |
| 5097 | /// true. Return false if "icmp1 APred ALHS ARHS" implies "icmp2 BPred BLHS |
| 5098 | /// BRHS" is false. Otherwise, return None if we can't infer anything. |
| 5099 | static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred, |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5100 | const Value *ALHS, |
| 5101 | const Value *ARHS, |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5102 | CmpInst::Predicate BPred, |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5103 | const Value *BLHS, |
| 5104 | const Value *BRHS, |
Chad Rosier | 226a734 | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5105 | bool IsSwappedOps) { |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5106 | // Canonicalize the operands so they're matching. |
| 5107 | if (IsSwappedOps) { |
| 5108 | std::swap(BLHS, BRHS); |
| 5109 | BPred = ICmpInst::getSwappedPredicate(BPred); |
| 5110 | } |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 5111 | if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred)) |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5112 | return true; |
Chad Rosier | 99bc480 | 2016-04-21 16:18:02 +0000 | [diff] [blame] | 5113 | if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred)) |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5114 | return false; |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5115 | |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5116 | return None; |
Chad Rosier | b7dfbb4 | 2016-04-19 17:19:14 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Chad Rosier | 25cfb7d | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5119 | /// Return true if "icmp1 APred ALHS C1" implies "icmp2 BPred BLHS C2" is |
| 5120 | /// true. Return false if "icmp1 APred ALHS C1" implies "icmp2 BPred BLHS |
| 5121 | /// C2" is false. Otherwise, return None if we can't infer anything. |
| 5122 | static Optional<bool> |
Pete Cooper | 35b00d5 | 2016-08-13 01:05:32 +0000 | [diff] [blame] | 5123 | isImpliedCondMatchingImmOperands(CmpInst::Predicate APred, const Value *ALHS, |
| 5124 | const ConstantInt *C1, |
| 5125 | CmpInst::Predicate BPred, |
| 5126 | const Value *BLHS, const ConstantInt *C2) { |
Chad Rosier | 25cfb7d | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5127 | assert(ALHS == BLHS && "LHS operands must match."); |
| 5128 | ConstantRange DomCR = |
| 5129 | ConstantRange::makeExactICmpRegion(APred, C1->getValue()); |
| 5130 | ConstantRange CR = |
| 5131 | ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue()); |
| 5132 | ConstantRange Intersection = DomCR.intersectWith(CR); |
| 5133 | ConstantRange Difference = DomCR.difference(CR); |
| 5134 | if (Intersection.isEmptySet()) |
| 5135 | return false; |
| 5136 | if (Difference.isEmptySet()) |
| 5137 | return true; |
| 5138 | return None; |
| 5139 | } |
| 5140 | |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5141 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is |
| 5142 | /// false. Otherwise, return None if we can't infer anything. |
| 5143 | static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS, |
| 5144 | const ICmpInst *RHS, |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5145 | const DataLayout &DL, bool LHSIsTrue, |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5146 | unsigned Depth) { |
| 5147 | Value *ALHS = LHS->getOperand(0); |
| 5148 | Value *ARHS = LHS->getOperand(1); |
Chad Rosier | a72a9ff | 2017-07-06 20:00:25 +0000 | [diff] [blame] | 5149 | // The rest of the logic assumes the LHS condition is true. If that's not the |
| 5150 | // case, invert the predicate to make it so. |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5151 | ICmpInst::Predicate APred = |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5152 | LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate(); |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5153 | |
| 5154 | Value *BLHS = RHS->getOperand(0); |
| 5155 | Value *BRHS = RHS->getOperand(1); |
| 5156 | ICmpInst::Predicate BPred = RHS->getPredicate(); |
Chad Rosier | e2cbd13 | 2016-04-25 17:23:36 +0000 | [diff] [blame] | 5157 | |
Chad Rosier | 226a734 | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5158 | // Can we infer anything when the two compares have matching operands? |
| 5159 | bool IsSwappedOps; |
| 5160 | if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, IsSwappedOps)) { |
| 5161 | if (Optional<bool> Implication = isImpliedCondMatchingOperands( |
| 5162 | APred, ALHS, ARHS, BPred, BLHS, BRHS, IsSwappedOps)) |
Chad Rosier | 25cfb7d | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5163 | return Implication; |
Chad Rosier | 226a734 | 2016-05-05 17:41:19 +0000 | [diff] [blame] | 5164 | // No amount of additional analysis will infer the second condition, so |
| 5165 | // early exit. |
| 5166 | return None; |
| 5167 | } |
| 5168 | |
| 5169 | // Can we infer anything when the LHS operands match and the RHS operands are |
| 5170 | // constants (not necessarily matching)? |
| 5171 | if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) { |
| 5172 | if (Optional<bool> Implication = isImpliedCondMatchingImmOperands( |
| 5173 | APred, ALHS, cast<ConstantInt>(ARHS), BPred, BLHS, |
| 5174 | cast<ConstantInt>(BRHS))) |
| 5175 | return Implication; |
| 5176 | // No amount of additional analysis will infer the second condition, so |
| 5177 | // early exit. |
| 5178 | return None; |
Chad Rosier | 25cfb7d | 2016-05-05 15:39:18 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5181 | if (APred == BPred) |
Chad Rosier | e42b44b | 2017-07-28 14:39:06 +0000 | [diff] [blame] | 5182 | return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth); |
Chad Rosier | 41dd31f | 2016-04-20 19:15:26 +0000 | [diff] [blame] | 5183 | return None; |
Sanjoy Das | 3ef1e68 | 2015-10-28 03:20:19 +0000 | [diff] [blame] | 5184 | } |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5185 | |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5186 | /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is |
| 5187 | /// false. Otherwise, return None if we can't infer anything. We expect the |
| 5188 | /// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction. |
| 5189 | static Optional<bool> isImpliedCondAndOr(const BinaryOperator *LHS, |
| 5190 | const ICmpInst *RHS, |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5191 | const DataLayout &DL, bool LHSIsTrue, |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5192 | unsigned Depth) { |
| 5193 | // The LHS must be an 'or' or an 'and' instruction. |
| 5194 | assert((LHS->getOpcode() == Instruction::And || |
| 5195 | LHS->getOpcode() == Instruction::Or) && |
| 5196 | "Expected LHS to be 'and' or 'or'."); |
| 5197 | |
Davide Italiano | 1a943a9 | 2017-08-09 16:06:54 +0000 | [diff] [blame] | 5198 | assert(Depth <= MaxDepth && "Hit recursion limit"); |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5199 | |
| 5200 | // If the result of an 'or' is false, then we know both legs of the 'or' are |
| 5201 | // false. Similarly, if the result of an 'and' is true, then we know both |
| 5202 | // legs of the 'and' are true. |
| 5203 | Value *ALHS, *ARHS; |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5204 | if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) || |
| 5205 | (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) { |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5206 | // FIXME: Make this non-recursion. |
| 5207 | if (Optional<bool> Implication = |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5208 | isImpliedCondition(ALHS, RHS, DL, LHSIsTrue, Depth + 1)) |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5209 | return Implication; |
| 5210 | if (Optional<bool> Implication = |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5211 | isImpliedCondition(ARHS, RHS, DL, LHSIsTrue, Depth + 1)) |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5212 | return Implication; |
| 5213 | return None; |
| 5214 | } |
| 5215 | return None; |
| 5216 | } |
| 5217 | |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5218 | Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS, |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5219 | const DataLayout &DL, bool LHSIsTrue, |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5220 | unsigned Depth) { |
Davide Italiano | 30e5194 | 2017-08-09 15:13:50 +0000 | [diff] [blame] | 5221 | // Bail out when we hit the limit. |
| 5222 | if (Depth == MaxDepth) |
| 5223 | return None; |
| 5224 | |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5225 | // A mismatch occurs when we compare a scalar cmp to a vector cmp, for |
| 5226 | // example. |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5227 | if (LHS->getType() != RHS->getType()) |
| 5228 | return None; |
| 5229 | |
| 5230 | Type *OpTy = LHS->getType(); |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5231 | assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!"); |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5232 | |
| 5233 | // LHS ==> RHS by definition |
| 5234 | if (LHS == RHS) |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5235 | return LHSIsTrue; |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5236 | |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5237 | // FIXME: Extending the code below to handle vectors. |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5238 | if (OpTy->isVectorTy()) |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5239 | return None; |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5240 | |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5241 | assert(OpTy->isIntegerTy(1) && "implied by above"); |
| 5242 | |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5243 | // Both LHS and RHS are icmps. |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5244 | const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS); |
| 5245 | const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS); |
| 5246 | if (LHSCmp && RHSCmp) |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5247 | return isImpliedCondICmps(LHSCmp, RHSCmp, DL, LHSIsTrue, Depth); |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5248 | |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5249 | // The LHS should be an 'or' or an 'and' instruction. We expect the RHS to be |
| 5250 | // an icmp. FIXME: Add support for and/or on the RHS. |
| 5251 | const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS); |
| 5252 | if (LHSBO && RHSCmp) { |
| 5253 | if ((LHSBO->getOpcode() == Instruction::And || |
| 5254 | LHSBO->getOpcode() == Instruction::Or)) |
Chad Rosier | dfd1de6 | 2017-08-01 20:18:54 +0000 | [diff] [blame] | 5255 | return isImpliedCondAndOr(LHSBO, RHSCmp, DL, LHSIsTrue, Depth); |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5256 | } |
Chad Rosier | f73a10d | 2017-08-01 19:22:36 +0000 | [diff] [blame] | 5257 | return None; |
Chad Rosier | 2f49803 | 2017-07-28 18:47:43 +0000 | [diff] [blame] | 5258 | } |