1f22ef01cSRoman Divacky //===- ValueTracking.cpp - Walk computations to compute properties --------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file contains routines that help analyze properties that chains of
11f22ef01cSRoman Divacky // computations have.
12f22ef01cSRoman Divacky //
13f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
14f22ef01cSRoman Divacky
15f22ef01cSRoman Divacky #include "llvm/Analysis/ValueTracking.h"
162cab237bSDimitry Andric #include "llvm/ADT/APFloat.h"
172cab237bSDimitry Andric #include "llvm/ADT/APInt.h"
182cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h"
192cab237bSDimitry Andric #include "llvm/ADT/None.h"
207d523365SDimitry Andric #include "llvm/ADT/Optional.h"
212cab237bSDimitry Andric #include "llvm/ADT/STLExtras.h"
22139f7f9bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
232cab237bSDimitry Andric #include "llvm/ADT/SmallSet.h"
242cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
252cab237bSDimitry Andric #include "llvm/ADT/StringRef.h"
262cab237bSDimitry Andric #include "llvm/ADT/iterator_range.h"
272cab237bSDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
2839d628a0SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
29b5893f02SDimitry Andric #include "llvm/Analysis/GuardUtils.h"
302754fe60SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
313ca95b02SDimitry Andric #include "llvm/Analysis/Loads.h"
32ff0cc061SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
332cab237bSDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
342cab237bSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
352cab237bSDimitry Andric #include "llvm/IR/Argument.h"
362cab237bSDimitry Andric #include "llvm/IR/Attributes.h"
372cab237bSDimitry Andric #include "llvm/IR/BasicBlock.h"
3891bc56edSDimitry Andric #include "llvm/IR/CallSite.h"
392cab237bSDimitry Andric #include "llvm/IR/Constant.h"
4091bc56edSDimitry Andric #include "llvm/IR/ConstantRange.h"
41139f7f9bSDimitry Andric #include "llvm/IR/Constants.h"
42139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h"
43d8866befSDimitry Andric #include "llvm/IR/DerivedTypes.h"
442cab237bSDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
4539d628a0SDimitry Andric #include "llvm/IR/Dominators.h"
462cab237bSDimitry Andric #include "llvm/IR/Function.h"
4791bc56edSDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
48139f7f9bSDimitry Andric #include "llvm/IR/GlobalAlias.h"
492cab237bSDimitry Andric #include "llvm/IR/GlobalValue.h"
50139f7f9bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
512cab237bSDimitry Andric #include "llvm/IR/InstrTypes.h"
522cab237bSDimitry Andric #include "llvm/IR/Instruction.h"
53139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h"
54139f7f9bSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
552cab237bSDimitry Andric #include "llvm/IR/Intrinsics.h"
56139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h"
57139f7f9bSDimitry Andric #include "llvm/IR/Metadata.h"
582cab237bSDimitry Andric #include "llvm/IR/Module.h"
59139f7f9bSDimitry Andric #include "llvm/IR/Operator.h"
6091bc56edSDimitry Andric #include "llvm/IR/PatternMatch.h"
612cab237bSDimitry Andric #include "llvm/IR/Type.h"
622cab237bSDimitry Andric #include "llvm/IR/User.h"
632cab237bSDimitry Andric #include "llvm/IR/Value.h"
642cab237bSDimitry Andric #include "llvm/Support/Casting.h"
652cab237bSDimitry Andric #include "llvm/Support/CommandLine.h"
662cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
672cab237bSDimitry Andric #include "llvm/Support/ErrorHandling.h"
6851690af2SDimitry Andric #include "llvm/Support/KnownBits.h"
69f22ef01cSRoman Divacky #include "llvm/Support/MathExtras.h"
703ca95b02SDimitry Andric #include <algorithm>
713ca95b02SDimitry Andric #include <array>
722cab237bSDimitry Andric #include <cassert>
732cab237bSDimitry Andric #include <cstdint>
742cab237bSDimitry Andric #include <iterator>
752cab237bSDimitry Andric #include <utility>
762cab237bSDimitry Andric
77f22ef01cSRoman Divacky using namespace llvm;
782754fe60SDimitry Andric using namespace llvm::PatternMatch;
792754fe60SDimitry Andric
802754fe60SDimitry Andric const unsigned MaxDepth = 6;
812754fe60SDimitry Andric
82ff0cc061SDimitry Andric // Controls the number of uses of the value searched for possible
83ff0cc061SDimitry Andric // dominating comparisons.
84ff0cc061SDimitry Andric static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
857d523365SDimitry Andric cl::Hidden, cl::init(20));
86ff0cc061SDimitry Andric
870f5676f4SDimitry Andric /// Returns the bitwidth of the given scalar or pointer type. For vector types,
880f5676f4SDimitry Andric /// returns the element type's bitwidth.
getBitWidth(Type * Ty,const DataLayout & DL)89ff0cc061SDimitry Andric static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
902754fe60SDimitry Andric if (unsigned BitWidth = Ty->getScalarSizeInBits())
912754fe60SDimitry Andric return BitWidth;
92f785676fSDimitry Andric
934ba319b5SDimitry Andric return DL.getIndexTypeSizeInBits(Ty);
942754fe60SDimitry Andric }
95f22ef01cSRoman Divacky
9639d628a0SDimitry Andric namespace {
972cab237bSDimitry Andric
9839d628a0SDimitry Andric // Simplifying using an assume can only be done in a particular control-flow
9939d628a0SDimitry Andric // context (the context instruction provides that context). If an assume and
10039d628a0SDimitry Andric // the context instruction are not in the same block then the DT helps in
10139d628a0SDimitry Andric // figuring out if we can use it.
10239d628a0SDimitry Andric struct Query {
1033ca95b02SDimitry Andric const DataLayout &DL;
10439d628a0SDimitry Andric AssumptionCache *AC;
10539d628a0SDimitry Andric const Instruction *CxtI;
10639d628a0SDimitry Andric const DominatorTree *DT;
1072cab237bSDimitry Andric
1087a7e6055SDimitry Andric // Unlike the other analyses, this may be a nullptr because not all clients
1097a7e6055SDimitry Andric // provide it currently.
1107a7e6055SDimitry Andric OptimizationRemarkEmitter *ORE;
11139d628a0SDimitry Andric
1123ca95b02SDimitry Andric /// Set of assumptions that should be excluded from further queries.
1133ca95b02SDimitry Andric /// This is because of the potential for mutual recursion to cause
1143ca95b02SDimitry Andric /// computeKnownBits to repeatedly visit the same assume intrinsic. The
1153ca95b02SDimitry Andric /// classic case of this is assume(x = y), which will attempt to determine
1163ca95b02SDimitry Andric /// bits in x from bits in y, which will attempt to determine bits in y from
1173ca95b02SDimitry Andric /// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
1185517e702SDimitry Andric /// isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo
1195517e702SDimitry Andric /// (all of which can call computeKnownBits), and so on.
1203ca95b02SDimitry Andric std::array<const Value *, MaxDepth> Excluded;
1212cab237bSDimitry Andric
122b5893f02SDimitry Andric /// If true, it is safe to use metadata during simplification.
123b5893f02SDimitry Andric InstrInfoQuery IIQ;
124b5893f02SDimitry Andric
1252cab237bSDimitry Andric unsigned NumExcluded = 0;
1263ca95b02SDimitry Andric
Query__anon8e416d180111::Query1273ca95b02SDimitry Andric Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
128b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo,
129b5893f02SDimitry Andric OptimizationRemarkEmitter *ORE = nullptr)
130b5893f02SDimitry Andric : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
13139d628a0SDimitry Andric
Query__anon8e416d180111::Query13239d628a0SDimitry Andric Query(const Query &Q, const Value *NewExcl)
133b5893f02SDimitry Andric : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ),
1347a7e6055SDimitry Andric NumExcluded(Q.NumExcluded) {
1353ca95b02SDimitry Andric Excluded = Q.Excluded;
1363ca95b02SDimitry Andric Excluded[NumExcluded++] = NewExcl;
1373ca95b02SDimitry Andric assert(NumExcluded <= Excluded.size());
1383ca95b02SDimitry Andric }
1393ca95b02SDimitry Andric
isExcluded__anon8e416d180111::Query1403ca95b02SDimitry Andric bool isExcluded(const Value *Value) const {
1413ca95b02SDimitry Andric if (NumExcluded == 0)
1423ca95b02SDimitry Andric return false;
1433ca95b02SDimitry Andric auto End = Excluded.begin() + NumExcluded;
1443ca95b02SDimitry Andric return std::find(Excluded.begin(), End, Value) != End;
14539d628a0SDimitry Andric }
14639d628a0SDimitry Andric };
1472cab237bSDimitry Andric
14839d628a0SDimitry Andric } // end anonymous namespace
14939d628a0SDimitry Andric
15039d628a0SDimitry Andric // Given the provided Value and, potentially, a context instruction, return
15139d628a0SDimitry Andric // the preferred context instruction (if any).
safeCxtI(const Value * V,const Instruction * CxtI)15239d628a0SDimitry Andric static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
15339d628a0SDimitry Andric // If we've been provided with a context instruction, then use that (provided
15439d628a0SDimitry Andric // it has been inserted).
15539d628a0SDimitry Andric if (CxtI && CxtI->getParent())
15639d628a0SDimitry Andric return CxtI;
15739d628a0SDimitry Andric
15839d628a0SDimitry Andric // If the value is really an already-inserted instruction, then use that.
15939d628a0SDimitry Andric CxtI = dyn_cast<Instruction>(V);
16039d628a0SDimitry Andric if (CxtI && CxtI->getParent())
16139d628a0SDimitry Andric return CxtI;
16239d628a0SDimitry Andric
16339d628a0SDimitry Andric return nullptr;
16439d628a0SDimitry Andric }
16539d628a0SDimitry Andric
16651690af2SDimitry Andric static void computeKnownBits(const Value *V, KnownBits &Known,
1673ca95b02SDimitry Andric unsigned Depth, const Query &Q);
16839d628a0SDimitry Andric
computeKnownBits(const Value * V,KnownBits & Known,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,OptimizationRemarkEmitter * ORE,bool UseInstrInfo)16951690af2SDimitry Andric void llvm::computeKnownBits(const Value *V, KnownBits &Known,
170ff0cc061SDimitry Andric const DataLayout &DL, unsigned Depth,
17139d628a0SDimitry Andric AssumptionCache *AC, const Instruction *CxtI,
1727a7e6055SDimitry Andric const DominatorTree *DT,
173b5893f02SDimitry Andric OptimizationRemarkEmitter *ORE, bool UseInstrInfo) {
17451690af2SDimitry Andric ::computeKnownBits(V, Known, Depth,
175b5893f02SDimitry Andric Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
17639d628a0SDimitry Andric }
17739d628a0SDimitry Andric
1785517e702SDimitry Andric static KnownBits computeKnownBits(const Value *V, unsigned Depth,
1795517e702SDimitry Andric const Query &Q);
1805517e702SDimitry Andric
computeKnownBits(const Value * V,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,OptimizationRemarkEmitter * ORE,bool UseInstrInfo)1815517e702SDimitry Andric KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL,
1825517e702SDimitry Andric unsigned Depth, AssumptionCache *AC,
1835517e702SDimitry Andric const Instruction *CxtI,
184302affcbSDimitry Andric const DominatorTree *DT,
185b5893f02SDimitry Andric OptimizationRemarkEmitter *ORE,
186b5893f02SDimitry Andric bool UseInstrInfo) {
187b5893f02SDimitry Andric return ::computeKnownBits(
188b5893f02SDimitry Andric V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
1895517e702SDimitry Andric }
1905517e702SDimitry Andric
haveNoCommonBitsSet(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)191d88c1a5aSDimitry Andric bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
192b5893f02SDimitry Andric const DataLayout &DL, AssumptionCache *AC,
193b5893f02SDimitry Andric const Instruction *CxtI, const DominatorTree *DT,
194b5893f02SDimitry Andric bool UseInstrInfo) {
195ff0cc061SDimitry Andric assert(LHS->getType() == RHS->getType() &&
196ff0cc061SDimitry Andric "LHS and RHS should have the same type");
197ff0cc061SDimitry Andric assert(LHS->getType()->isIntOrIntVectorTy() &&
198ff0cc061SDimitry Andric "LHS and RHS should be integers");
1994ba319b5SDimitry Andric // Look for an inverted mask: (X & ~M) op (Y & M).
2004ba319b5SDimitry Andric Value *M;
2014ba319b5SDimitry Andric if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
2024ba319b5SDimitry Andric match(RHS, m_c_And(m_Specific(M), m_Value())))
2034ba319b5SDimitry Andric return true;
2044ba319b5SDimitry Andric if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
2054ba319b5SDimitry Andric match(LHS, m_c_And(m_Specific(M), m_Value())))
2064ba319b5SDimitry Andric return true;
207ff0cc061SDimitry Andric IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
20851690af2SDimitry Andric KnownBits LHSKnown(IT->getBitWidth());
20951690af2SDimitry Andric KnownBits RHSKnown(IT->getBitWidth());
210b5893f02SDimitry Andric computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
211b5893f02SDimitry Andric computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
21251690af2SDimitry Andric return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue();
213ff0cc061SDimitry Andric }
214ff0cc061SDimitry Andric
isOnlyUsedInZeroEqualityComparison(const Instruction * CxtI)215f9448bf3SDimitry Andric bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) {
216f9448bf3SDimitry Andric for (const User *U : CxtI->users()) {
217f9448bf3SDimitry Andric if (const ICmpInst *IC = dyn_cast<ICmpInst>(U))
218f9448bf3SDimitry Andric if (IC->isEquality())
219f9448bf3SDimitry Andric if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
220f9448bf3SDimitry Andric if (C->isNullValue())
221f9448bf3SDimitry Andric continue;
222f9448bf3SDimitry Andric return false;
223f9448bf3SDimitry Andric }
224f9448bf3SDimitry Andric return true;
225f9448bf3SDimitry Andric }
226f9448bf3SDimitry Andric
227d88c1a5aSDimitry Andric static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
2283ca95b02SDimitry Andric const Query &Q);
22939d628a0SDimitry Andric
isKnownToBeAPowerOfTwo(const Value * V,const DataLayout & DL,bool OrZero,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)230d88c1a5aSDimitry Andric bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
231b5893f02SDimitry Andric bool OrZero, unsigned Depth,
232b5893f02SDimitry Andric AssumptionCache *AC, const Instruction *CxtI,
233b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
234b5893f02SDimitry Andric return ::isKnownToBeAPowerOfTwo(
235b5893f02SDimitry Andric V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
236ff0cc061SDimitry Andric }
237ff0cc061SDimitry Andric
238d88c1a5aSDimitry Andric static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q);
239ff0cc061SDimitry Andric
isKnownNonZero(const Value * V,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)240d88c1a5aSDimitry Andric bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
241ff0cc061SDimitry Andric AssumptionCache *AC, const Instruction *CxtI,
242b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
243b5893f02SDimitry Andric return ::isKnownNonZero(V, Depth,
244b5893f02SDimitry Andric Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
245ff0cc061SDimitry Andric }
246ff0cc061SDimitry Andric
isKnownNonNegative(const Value * V,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)247d88c1a5aSDimitry Andric bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
248b5893f02SDimitry Andric unsigned Depth, AssumptionCache *AC,
249b5893f02SDimitry Andric const Instruction *CxtI, const DominatorTree *DT,
250b5893f02SDimitry Andric bool UseInstrInfo) {
251b5893f02SDimitry Andric KnownBits Known =
252b5893f02SDimitry Andric computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
2535517e702SDimitry Andric return Known.isNonNegative();
2547d523365SDimitry Andric }
2557d523365SDimitry Andric
isKnownPositive(const Value * V,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)256d88c1a5aSDimitry Andric bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
2573ca95b02SDimitry Andric AssumptionCache *AC, const Instruction *CxtI,
258b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
2593ca95b02SDimitry Andric if (auto *CI = dyn_cast<ConstantInt>(V))
2603ca95b02SDimitry Andric return CI->getValue().isStrictlyPositive();
2613ca95b02SDimitry Andric
2623ca95b02SDimitry Andric // TODO: We'd doing two recursive queries here. We should factor this such
2633ca95b02SDimitry Andric // that only a single query is needed.
264b5893f02SDimitry Andric return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
265b5893f02SDimitry Andric isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
2663ca95b02SDimitry Andric }
2673ca95b02SDimitry Andric
isKnownNegative(const Value * V,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)268d88c1a5aSDimitry Andric bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth,
2693ca95b02SDimitry Andric AssumptionCache *AC, const Instruction *CxtI,
270b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
271b5893f02SDimitry Andric KnownBits Known =
272b5893f02SDimitry Andric computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
2735517e702SDimitry Andric return Known.isNegative();
2743ca95b02SDimitry Andric }
2753ca95b02SDimitry Andric
276d88c1a5aSDimitry Andric static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q);
2777d523365SDimitry Andric
isKnownNonEqual(const Value * V1,const Value * V2,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)278d88c1a5aSDimitry Andric bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
279b5893f02SDimitry Andric const DataLayout &DL, AssumptionCache *AC,
280b5893f02SDimitry Andric const Instruction *CxtI, const DominatorTree *DT,
281b5893f02SDimitry Andric bool UseInstrInfo) {
282b5893f02SDimitry Andric return ::isKnownNonEqual(V1, V2,
283b5893f02SDimitry Andric Query(DL, AC, safeCxtI(V1, safeCxtI(V2, CxtI)), DT,
284b5893f02SDimitry Andric UseInstrInfo, /*ORE=*/nullptr));
2857d523365SDimitry Andric }
2867d523365SDimitry Andric
287d88c1a5aSDimitry Andric static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
2883ca95b02SDimitry Andric const Query &Q);
289ff0cc061SDimitry Andric
MaskedValueIsZero(const Value * V,const APInt & Mask,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)290d88c1a5aSDimitry Andric bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
291b5893f02SDimitry Andric const DataLayout &DL, unsigned Depth,
292b5893f02SDimitry Andric AssumptionCache *AC, const Instruction *CxtI,
293b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
294b5893f02SDimitry Andric return ::MaskedValueIsZero(
295b5893f02SDimitry Andric V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
296ff0cc061SDimitry Andric }
297ff0cc061SDimitry Andric
298d88c1a5aSDimitry Andric static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
299d88c1a5aSDimitry Andric const Query &Q);
300ff0cc061SDimitry Andric
ComputeNumSignBits(const Value * V,const DataLayout & DL,unsigned Depth,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)301d88c1a5aSDimitry Andric unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
302ff0cc061SDimitry Andric unsigned Depth, AssumptionCache *AC,
303ff0cc061SDimitry Andric const Instruction *CxtI,
304b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
305b5893f02SDimitry Andric return ::ComputeNumSignBits(
306b5893f02SDimitry Andric V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
30739d628a0SDimitry Andric }
30839d628a0SDimitry Andric
computeKnownBitsAddSub(bool Add,const Value * Op0,const Value * Op1,bool NSW,KnownBits & KnownOut,KnownBits & Known2,unsigned Depth,const Query & Q)309d88c1a5aSDimitry Andric static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
310d88c1a5aSDimitry Andric bool NSW,
31151690af2SDimitry Andric KnownBits &KnownOut, KnownBits &Known2,
3123ca95b02SDimitry Andric unsigned Depth, const Query &Q) {
31351690af2SDimitry Andric unsigned BitWidth = KnownOut.getBitWidth();
314dff0c46cSDimitry Andric
31539d628a0SDimitry Andric // If an initial sequence of bits in the result is not needed, the
31639d628a0SDimitry Andric // corresponding bits in the operands are not needed.
31751690af2SDimitry Andric KnownBits LHSKnown(BitWidth);
31851690af2SDimitry Andric computeKnownBits(Op0, LHSKnown, Depth + 1, Q);
31951690af2SDimitry Andric computeKnownBits(Op1, Known2, Depth + 1, Q);
320dff0c46cSDimitry Andric
3212cab237bSDimitry Andric KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2);
322bd5abe19SDimitry Andric }
323bd5abe19SDimitry Andric
computeKnownBitsMul(const Value * Op0,const Value * Op1,bool NSW,KnownBits & Known,KnownBits & Known2,unsigned Depth,const Query & Q)324d88c1a5aSDimitry Andric static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
32551690af2SDimitry Andric KnownBits &Known, KnownBits &Known2,
3263ca95b02SDimitry Andric unsigned Depth, const Query &Q) {
32751690af2SDimitry Andric unsigned BitWidth = Known.getBitWidth();
32851690af2SDimitry Andric computeKnownBits(Op1, Known, Depth + 1, Q);
32951690af2SDimitry Andric computeKnownBits(Op0, Known2, Depth + 1, Q);
330f22ef01cSRoman Divacky
331dff0c46cSDimitry Andric bool isKnownNegative = false;
332dff0c46cSDimitry Andric bool isKnownNonNegative = false;
333dff0c46cSDimitry Andric // If the multiplication is known not to overflow, compute the sign bit.
334dff0c46cSDimitry Andric if (NSW) {
335dff0c46cSDimitry Andric if (Op0 == Op1) {
336dff0c46cSDimitry Andric // The product of a number with itself is non-negative.
337dff0c46cSDimitry Andric isKnownNonNegative = true;
338dff0c46cSDimitry Andric } else {
339f37b6182SDimitry Andric bool isKnownNonNegativeOp1 = Known.isNonNegative();
340f37b6182SDimitry Andric bool isKnownNonNegativeOp0 = Known2.isNonNegative();
341f37b6182SDimitry Andric bool isKnownNegativeOp1 = Known.isNegative();
342f37b6182SDimitry Andric bool isKnownNegativeOp0 = Known2.isNegative();
343dff0c46cSDimitry Andric // The product of two numbers with the same sign is non-negative.
344dff0c46cSDimitry Andric isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
345dff0c46cSDimitry Andric (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
346dff0c46cSDimitry Andric // The product of a negative number and a non-negative number is either
347dff0c46cSDimitry Andric // negative or zero.
348dff0c46cSDimitry Andric if (!isKnownNonNegative)
349dff0c46cSDimitry Andric isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
3503ca95b02SDimitry Andric isKnownNonZero(Op0, Depth, Q)) ||
351dff0c46cSDimitry Andric (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
3523ca95b02SDimitry Andric isKnownNonZero(Op1, Depth, Q));
353f22ef01cSRoman Divacky }
354f22ef01cSRoman Divacky }
355f22ef01cSRoman Divacky
3562cab237bSDimitry Andric assert(!Known.hasConflict() && !Known2.hasConflict());
3572cab237bSDimitry Andric // Compute a conservative estimate for high known-0 bits.
3585517e702SDimitry Andric unsigned LeadZ = std::max(Known.countMinLeadingZeros() +
3595517e702SDimitry Andric Known2.countMinLeadingZeros(),
360f22ef01cSRoman Divacky BitWidth) - BitWidth;
361f22ef01cSRoman Divacky LeadZ = std::min(LeadZ, BitWidth);
3622cab237bSDimitry Andric
3632cab237bSDimitry Andric // The result of the bottom bits of an integer multiply can be
3642cab237bSDimitry Andric // inferred by looking at the bottom bits of both operands and
3652cab237bSDimitry Andric // multiplying them together.
3662cab237bSDimitry Andric // We can infer at least the minimum number of known trailing bits
3672cab237bSDimitry Andric // of both operands. Depending on number of trailing zeros, we can
3682cab237bSDimitry Andric // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming
3692cab237bSDimitry Andric // a and b are divisible by m and n respectively.
3702cab237bSDimitry Andric // We then calculate how many of those bits are inferrable and set
3712cab237bSDimitry Andric // the output. For example, the i8 mul:
3722cab237bSDimitry Andric // a = XXXX1100 (12)
3732cab237bSDimitry Andric // b = XXXX1110 (14)
3742cab237bSDimitry Andric // We know the bottom 3 bits are zero since the first can be divided by
3752cab237bSDimitry Andric // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4).
3762cab237bSDimitry Andric // Applying the multiplication to the trimmed arguments gets:
3772cab237bSDimitry Andric // XX11 (3)
3782cab237bSDimitry Andric // X111 (7)
3792cab237bSDimitry Andric // -------
3802cab237bSDimitry Andric // XX11
3812cab237bSDimitry Andric // XX11
3822cab237bSDimitry Andric // XX11
3832cab237bSDimitry Andric // XX11
3842cab237bSDimitry Andric // -------
3852cab237bSDimitry Andric // XXXXX01
3862cab237bSDimitry Andric // Which allows us to infer the 2 LSBs. Since we're multiplying the result
3872cab237bSDimitry Andric // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits.
3882cab237bSDimitry Andric // The proof for this can be described as:
3892cab237bSDimitry Andric // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) &&
3902cab237bSDimitry Andric // (C7 == (1 << (umin(countTrailingZeros(C1), C5) +
3912cab237bSDimitry Andric // umin(countTrailingZeros(C2), C6) +
3922cab237bSDimitry Andric // umin(C5 - umin(countTrailingZeros(C1), C5),
3932cab237bSDimitry Andric // C6 - umin(countTrailingZeros(C2), C6)))) - 1)
3942cab237bSDimitry Andric // %aa = shl i8 %a, C5
3952cab237bSDimitry Andric // %bb = shl i8 %b, C6
3962cab237bSDimitry Andric // %aaa = or i8 %aa, C1
3972cab237bSDimitry Andric // %bbb = or i8 %bb, C2
3982cab237bSDimitry Andric // %mul = mul i8 %aaa, %bbb
3992cab237bSDimitry Andric // %mask = and i8 %mul, C7
4002cab237bSDimitry Andric // =>
4012cab237bSDimitry Andric // %mask = i8 ((C1*C2)&C7)
4022cab237bSDimitry Andric // Where C5, C6 describe the known bits of %a, %b
4032cab237bSDimitry Andric // C1, C2 describe the known bottom bits of %a, %b.
4042cab237bSDimitry Andric // C7 describes the mask of the known bits of the result.
4052cab237bSDimitry Andric APInt Bottom0 = Known.One;
4062cab237bSDimitry Andric APInt Bottom1 = Known2.One;
4072cab237bSDimitry Andric
4082cab237bSDimitry Andric // How many times we'd be able to divide each argument by 2 (shr by 1).
4092cab237bSDimitry Andric // This gives us the number of trailing zeros on the multiplication result.
4102cab237bSDimitry Andric unsigned TrailBitsKnown0 = (Known.Zero | Known.One).countTrailingOnes();
4112cab237bSDimitry Andric unsigned TrailBitsKnown1 = (Known2.Zero | Known2.One).countTrailingOnes();
4122cab237bSDimitry Andric unsigned TrailZero0 = Known.countMinTrailingZeros();
4132cab237bSDimitry Andric unsigned TrailZero1 = Known2.countMinTrailingZeros();
4142cab237bSDimitry Andric unsigned TrailZ = TrailZero0 + TrailZero1;
4152cab237bSDimitry Andric
4162cab237bSDimitry Andric // Figure out the fewest known-bits operand.
4172cab237bSDimitry Andric unsigned SmallestOperand = std::min(TrailBitsKnown0 - TrailZero0,
4182cab237bSDimitry Andric TrailBitsKnown1 - TrailZero1);
4192cab237bSDimitry Andric unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth);
4202cab237bSDimitry Andric
4212cab237bSDimitry Andric APInt BottomKnown = Bottom0.getLoBits(TrailBitsKnown0) *
4222cab237bSDimitry Andric Bottom1.getLoBits(TrailBitsKnown1);
4232cab237bSDimitry Andric
4240f5676f4SDimitry Andric Known.resetAll();
42551690af2SDimitry Andric Known.Zero.setHighBits(LeadZ);
4262cab237bSDimitry Andric Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown);
4272cab237bSDimitry Andric Known.One |= BottomKnown.getLoBits(ResultBitsKnown);
428dff0c46cSDimitry Andric
429dff0c46cSDimitry Andric // Only make use of no-wrap flags if we failed to compute the sign bit
430dff0c46cSDimitry Andric // directly. This matters if the multiplication always overflows, in
431dff0c46cSDimitry Andric // which case we prefer to follow the result of the direct computation,
432dff0c46cSDimitry Andric // though as the program is invoking undefined behaviour we can choose
433dff0c46cSDimitry Andric // whatever we like here.
434f37b6182SDimitry Andric if (isKnownNonNegative && !Known.isNegative())
435f37b6182SDimitry Andric Known.makeNonNegative();
436f37b6182SDimitry Andric else if (isKnownNegative && !Known.isNonNegative())
437f37b6182SDimitry Andric Known.makeNegative();
438dff0c46cSDimitry Andric }
439dff0c46cSDimitry Andric
computeKnownBitsFromRangeMetadata(const MDNode & Ranges,KnownBits & Known)44091bc56edSDimitry Andric void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
441f37b6182SDimitry Andric KnownBits &Known) {
442f37b6182SDimitry Andric unsigned BitWidth = Known.getBitWidth();
443dff0c46cSDimitry Andric unsigned NumRanges = Ranges.getNumOperands() / 2;
444dff0c46cSDimitry Andric assert(NumRanges >= 1);
445dff0c46cSDimitry Andric
446f37b6182SDimitry Andric Known.Zero.setAllBits();
447f37b6182SDimitry Andric Known.One.setAllBits();
4487d523365SDimitry Andric
449dff0c46cSDimitry Andric for (unsigned i = 0; i < NumRanges; ++i) {
45039d628a0SDimitry Andric ConstantInt *Lower =
45139d628a0SDimitry Andric mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
45239d628a0SDimitry Andric ConstantInt *Upper =
45339d628a0SDimitry Andric mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
454dff0c46cSDimitry Andric ConstantRange Range(Lower->getValue(), Upper->getValue());
455dff0c46cSDimitry Andric
4567d523365SDimitry Andric // The first CommonPrefixBits of all values in Range are equal.
4577d523365SDimitry Andric unsigned CommonPrefixBits =
4587d523365SDimitry Andric (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros();
4597d523365SDimitry Andric
4607d523365SDimitry Andric APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
461f37b6182SDimitry Andric Known.One &= Range.getUnsignedMax() & Mask;
462f37b6182SDimitry Andric Known.Zero &= ~Range.getUnsignedMax() & Mask;
4637d523365SDimitry Andric }
464dff0c46cSDimitry Andric }
46591bc56edSDimitry Andric
isEphemeralValueOf(const Instruction * I,const Value * E)466d88c1a5aSDimitry Andric static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
46739d628a0SDimitry Andric SmallVector<const Value *, 16> WorkSet(1, I);
46839d628a0SDimitry Andric SmallPtrSet<const Value *, 32> Visited;
46939d628a0SDimitry Andric SmallPtrSet<const Value *, 16> EphValues;
47039d628a0SDimitry Andric
4717d523365SDimitry Andric // The instruction defining an assumption's condition itself is always
4727d523365SDimitry Andric // considered ephemeral to that assumption (even if it has other
4737d523365SDimitry Andric // non-ephemeral users). See r246696's test case for an example.
474d88c1a5aSDimitry Andric if (is_contained(I->operands(), E))
4757d523365SDimitry Andric return true;
4767d523365SDimitry Andric
47739d628a0SDimitry Andric while (!WorkSet.empty()) {
47839d628a0SDimitry Andric const Value *V = WorkSet.pop_back_val();
47939d628a0SDimitry Andric if (!Visited.insert(V).second)
48039d628a0SDimitry Andric continue;
48139d628a0SDimitry Andric
48239d628a0SDimitry Andric // If all uses of this value are ephemeral, then so is this value.
4832cab237bSDimitry Andric if (llvm::all_of(V->users(), [&](const User *U) {
4842cab237bSDimitry Andric return EphValues.count(U);
4852cab237bSDimitry Andric })) {
48639d628a0SDimitry Andric if (V == E)
48739d628a0SDimitry Andric return true;
48839d628a0SDimitry Andric
4892cab237bSDimitry Andric if (V == I || isSafeToSpeculativelyExecute(V)) {
49039d628a0SDimitry Andric EphValues.insert(V);
49139d628a0SDimitry Andric if (const User *U = dyn_cast<User>(V))
49239d628a0SDimitry Andric for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
4932cab237bSDimitry Andric J != JE; ++J)
49439d628a0SDimitry Andric WorkSet.push_back(*J);
49539d628a0SDimitry Andric }
49639d628a0SDimitry Andric }
49739d628a0SDimitry Andric }
49839d628a0SDimitry Andric
49939d628a0SDimitry Andric return false;
50039d628a0SDimitry Andric }
50139d628a0SDimitry Andric
50239d628a0SDimitry Andric // Is this an intrinsic that cannot be speculated but also cannot trap?
isAssumeLikeIntrinsic(const Instruction * I)5032cab237bSDimitry Andric bool llvm::isAssumeLikeIntrinsic(const Instruction *I) {
50439d628a0SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(I))
50539d628a0SDimitry Andric if (Function *F = CI->getCalledFunction())
50639d628a0SDimitry Andric switch (F->getIntrinsicID()) {
50739d628a0SDimitry Andric default: break;
50839d628a0SDimitry Andric // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
50939d628a0SDimitry Andric case Intrinsic::assume:
5102cab237bSDimitry Andric case Intrinsic::sideeffect:
51139d628a0SDimitry Andric case Intrinsic::dbg_declare:
51239d628a0SDimitry Andric case Intrinsic::dbg_value:
5134ba319b5SDimitry Andric case Intrinsic::dbg_label:
51439d628a0SDimitry Andric case Intrinsic::invariant_start:
51539d628a0SDimitry Andric case Intrinsic::invariant_end:
51639d628a0SDimitry Andric case Intrinsic::lifetime_start:
51739d628a0SDimitry Andric case Intrinsic::lifetime_end:
51839d628a0SDimitry Andric case Intrinsic::objectsize:
51939d628a0SDimitry Andric case Intrinsic::ptr_annotation:
52039d628a0SDimitry Andric case Intrinsic::var_annotation:
52139d628a0SDimitry Andric return true;
52239d628a0SDimitry Andric }
52339d628a0SDimitry Andric
52439d628a0SDimitry Andric return false;
52539d628a0SDimitry Andric }
52639d628a0SDimitry Andric
isValidAssumeForContext(const Instruction * Inv,const Instruction * CxtI,const DominatorTree * DT)527d88c1a5aSDimitry Andric bool llvm::isValidAssumeForContext(const Instruction *Inv,
528d88c1a5aSDimitry Andric const Instruction *CxtI,
5293ca95b02SDimitry Andric const DominatorTree *DT) {
53039d628a0SDimitry Andric // There are two restrictions on the use of an assume:
53139d628a0SDimitry Andric // 1. The assume must dominate the context (or the control flow must
53239d628a0SDimitry Andric // reach the assume whenever it reaches the context).
53339d628a0SDimitry Andric // 2. The context must not be in the assume's set of ephemeral values
53439d628a0SDimitry Andric // (otherwise we will use the assume to prove that the condition
53539d628a0SDimitry Andric // feeding the assume is trivially true, thus causing the removal of
53639d628a0SDimitry Andric // the assume).
53739d628a0SDimitry Andric
5383ca95b02SDimitry Andric if (DT) {
539d88c1a5aSDimitry Andric if (DT->dominates(Inv, CxtI))
54039d628a0SDimitry Andric return true;
541d88c1a5aSDimitry Andric } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
542d88c1a5aSDimitry Andric // We don't have a DT, but this trivially dominates.
543d88c1a5aSDimitry Andric return true;
54439d628a0SDimitry Andric }
54539d628a0SDimitry Andric
546d88c1a5aSDimitry Andric // With or without a DT, the only remaining case we will check is if the
547d88c1a5aSDimitry Andric // instructions are in the same BB. Give up if that is not the case.
548d88c1a5aSDimitry Andric if (Inv->getParent() != CxtI->getParent())
54939d628a0SDimitry Andric return false;
55039d628a0SDimitry Andric
5514ba319b5SDimitry Andric // If we have a dom tree, then we now know that the assume doesn't dominate
552d88c1a5aSDimitry Andric // the other instruction. If we don't have a dom tree then we can check if
553d88c1a5aSDimitry Andric // the assume is first in the BB.
554d88c1a5aSDimitry Andric if (!DT) {
55539d628a0SDimitry Andric // Search forward from the assume until we reach the context (or the end
55639d628a0SDimitry Andric // of the block); the common case is that the assume will come first.
557d88c1a5aSDimitry Andric for (auto I = std::next(BasicBlock::const_iterator(Inv)),
55839d628a0SDimitry Andric IE = Inv->getParent()->end(); I != IE; ++I)
5593ca95b02SDimitry Andric if (&*I == CxtI)
56039d628a0SDimitry Andric return true;
561d88c1a5aSDimitry Andric }
56239d628a0SDimitry Andric
563d88c1a5aSDimitry Andric // The context comes first, but they're both in the same block. Make sure
564d88c1a5aSDimitry Andric // there is nothing in between that might interrupt the control flow.
56539d628a0SDimitry Andric for (BasicBlock::const_iterator I =
566d88c1a5aSDimitry Andric std::next(BasicBlock::const_iterator(CxtI)), IE(Inv);
567d88c1a5aSDimitry Andric I != IE; ++I)
5687d523365SDimitry Andric if (!isSafeToSpeculativelyExecute(&*I) && !isAssumeLikeIntrinsic(&*I))
56939d628a0SDimitry Andric return false;
57039d628a0SDimitry Andric
5713ca95b02SDimitry Andric return !isEphemeralValueOf(Inv, CxtI);
57239d628a0SDimitry Andric }
57339d628a0SDimitry Andric
computeKnownBitsFromAssume(const Value * V,KnownBits & Known,unsigned Depth,const Query & Q)57451690af2SDimitry Andric static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
57551690af2SDimitry Andric unsigned Depth, const Query &Q) {
57639d628a0SDimitry Andric // Use of assumptions is context-sensitive. If we don't have a context, we
57739d628a0SDimitry Andric // cannot use them!
57839d628a0SDimitry Andric if (!Q.AC || !Q.CxtI)
57939d628a0SDimitry Andric return;
58039d628a0SDimitry Andric
58151690af2SDimitry Andric unsigned BitWidth = Known.getBitWidth();
58239d628a0SDimitry Andric
583f1a29dd3SDimitry Andric // Note that the patterns below need to be kept in sync with the code
584f1a29dd3SDimitry Andric // in AssumptionCache::updateAffectedValues.
585f1a29dd3SDimitry Andric
586f1a29dd3SDimitry Andric for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
58739d628a0SDimitry Andric if (!AssumeVH)
58839d628a0SDimitry Andric continue;
58939d628a0SDimitry Andric CallInst *I = cast<CallInst>(AssumeVH);
59039d628a0SDimitry Andric assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
59139d628a0SDimitry Andric "Got assumption for the wrong function!");
5923ca95b02SDimitry Andric if (Q.isExcluded(I))
59339d628a0SDimitry Andric continue;
59439d628a0SDimitry Andric
5954ba319b5SDimitry Andric // Warning: This loop can end up being somewhat performance sensitive.
59639d628a0SDimitry Andric // We're running this loop for once for each value queried resulting in a
59739d628a0SDimitry Andric // runtime of ~O(#assumes * #values).
59839d628a0SDimitry Andric
599ff0cc061SDimitry Andric assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
60039d628a0SDimitry Andric "must be an assume intrinsic");
60139d628a0SDimitry Andric
60239d628a0SDimitry Andric Value *Arg = I->getArgOperand(0);
60339d628a0SDimitry Andric
6043ca95b02SDimitry Andric if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
60539d628a0SDimitry Andric assert(BitWidth == 1 && "assume operand is not i1?");
6060f5676f4SDimitry Andric Known.setAllOnes();
60739d628a0SDimitry Andric return;
60839d628a0SDimitry Andric }
6097a7e6055SDimitry Andric if (match(Arg, m_Not(m_Specific(V))) &&
6107a7e6055SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
6117a7e6055SDimitry Andric assert(BitWidth == 1 && "assume operand is not i1?");
6120f5676f4SDimitry Andric Known.setAllZero();
6137a7e6055SDimitry Andric return;
6147a7e6055SDimitry Andric }
61539d628a0SDimitry Andric
61639d628a0SDimitry Andric // The remaining tests are all recursive, so bail out if we hit the limit.
61739d628a0SDimitry Andric if (Depth == MaxDepth)
61839d628a0SDimitry Andric continue;
61939d628a0SDimitry Andric
62039d628a0SDimitry Andric Value *A, *B;
62139d628a0SDimitry Andric auto m_V = m_CombineOr(m_Specific(V),
62239d628a0SDimitry Andric m_CombineOr(m_PtrToInt(m_Specific(V)),
62339d628a0SDimitry Andric m_BitCast(m_Specific(V))));
62439d628a0SDimitry Andric
62539d628a0SDimitry Andric CmpInst::Predicate Pred;
6262cab237bSDimitry Andric uint64_t C;
62739d628a0SDimitry Andric // assume(v = a)
62839d628a0SDimitry Andric if (match(Arg, m_c_ICmp(Pred, m_V, m_Value(A))) &&
6293ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
63051690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
63151690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
63251690af2SDimitry Andric Known.Zero |= RHSKnown.Zero;
63351690af2SDimitry Andric Known.One |= RHSKnown.One;
63439d628a0SDimitry Andric // assume(v & b = a)
635ff0cc061SDimitry Andric } else if (match(Arg,
636ff0cc061SDimitry Andric m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
6373ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
6383ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
63951690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
64051690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
64151690af2SDimitry Andric KnownBits MaskKnown(BitWidth);
64251690af2SDimitry Andric computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
64339d628a0SDimitry Andric
64439d628a0SDimitry Andric // For those bits in the mask that are known to be one, we can propagate
64539d628a0SDimitry Andric // known bits from the RHS to V.
64651690af2SDimitry Andric Known.Zero |= RHSKnown.Zero & MaskKnown.One;
64751690af2SDimitry Andric Known.One |= RHSKnown.One & MaskKnown.One;
64839d628a0SDimitry Andric // assume(~(v & b) = a)
64939d628a0SDimitry Andric } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
65039d628a0SDimitry Andric m_Value(A))) &&
6513ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
6523ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
65351690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
65451690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
65551690af2SDimitry Andric KnownBits MaskKnown(BitWidth);
65651690af2SDimitry Andric computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
65739d628a0SDimitry Andric
65839d628a0SDimitry Andric // For those bits in the mask that are known to be one, we can propagate
65939d628a0SDimitry Andric // inverted known bits from the RHS to V.
66051690af2SDimitry Andric Known.Zero |= RHSKnown.One & MaskKnown.One;
66151690af2SDimitry Andric Known.One |= RHSKnown.Zero & MaskKnown.One;
66239d628a0SDimitry Andric // assume(v | b = a)
663ff0cc061SDimitry Andric } else if (match(Arg,
664ff0cc061SDimitry Andric m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
6653ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
6663ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
66751690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
66851690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
66951690af2SDimitry Andric KnownBits BKnown(BitWidth);
67051690af2SDimitry Andric computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
67139d628a0SDimitry Andric
67239d628a0SDimitry Andric // For those bits in B that are known to be zero, we can propagate known
67339d628a0SDimitry Andric // bits from the RHS to V.
67451690af2SDimitry Andric Known.Zero |= RHSKnown.Zero & BKnown.Zero;
67551690af2SDimitry Andric Known.One |= RHSKnown.One & BKnown.Zero;
67639d628a0SDimitry Andric // assume(~(v | b) = a)
67739d628a0SDimitry Andric } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
67839d628a0SDimitry Andric m_Value(A))) &&
6793ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
6803ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
68151690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
68251690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
68351690af2SDimitry Andric KnownBits BKnown(BitWidth);
68451690af2SDimitry Andric computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
68539d628a0SDimitry Andric
68639d628a0SDimitry Andric // For those bits in B that are known to be zero, we can propagate
68739d628a0SDimitry Andric // inverted known bits from the RHS to V.
68851690af2SDimitry Andric Known.Zero |= RHSKnown.One & BKnown.Zero;
68951690af2SDimitry Andric Known.One |= RHSKnown.Zero & BKnown.Zero;
69039d628a0SDimitry Andric // assume(v ^ b = a)
691ff0cc061SDimitry Andric } else if (match(Arg,
692ff0cc061SDimitry Andric m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
6933ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
6943ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
69551690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
69651690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
69751690af2SDimitry Andric KnownBits BKnown(BitWidth);
69851690af2SDimitry Andric computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
69939d628a0SDimitry Andric
70039d628a0SDimitry Andric // For those bits in B that are known to be zero, we can propagate known
70139d628a0SDimitry Andric // bits from the RHS to V. For those bits in B that are known to be one,
70239d628a0SDimitry Andric // we can propagate inverted known bits from the RHS to V.
70351690af2SDimitry Andric Known.Zero |= RHSKnown.Zero & BKnown.Zero;
70451690af2SDimitry Andric Known.One |= RHSKnown.One & BKnown.Zero;
70551690af2SDimitry Andric Known.Zero |= RHSKnown.One & BKnown.One;
70651690af2SDimitry Andric Known.One |= RHSKnown.Zero & BKnown.One;
70739d628a0SDimitry Andric // assume(~(v ^ b) = a)
70839d628a0SDimitry Andric } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
70939d628a0SDimitry Andric m_Value(A))) &&
7103ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
7113ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
71251690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
71351690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
71451690af2SDimitry Andric KnownBits BKnown(BitWidth);
71551690af2SDimitry Andric computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
71639d628a0SDimitry Andric
71739d628a0SDimitry Andric // For those bits in B that are known to be zero, we can propagate
71839d628a0SDimitry Andric // inverted known bits from the RHS to V. For those bits in B that are
71939d628a0SDimitry Andric // known to be one, we can propagate known bits from the RHS to V.
72051690af2SDimitry Andric Known.Zero |= RHSKnown.One & BKnown.Zero;
72151690af2SDimitry Andric Known.One |= RHSKnown.Zero & BKnown.Zero;
72251690af2SDimitry Andric Known.Zero |= RHSKnown.Zero & BKnown.One;
72351690af2SDimitry Andric Known.One |= RHSKnown.One & BKnown.One;
72439d628a0SDimitry Andric // assume(v << c = a)
72539d628a0SDimitry Andric } else if (match(Arg, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
72639d628a0SDimitry Andric m_Value(A))) &&
7273ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
7282cab237bSDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
7292cab237bSDimitry Andric C < BitWidth) {
73051690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
73151690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
73239d628a0SDimitry Andric // For those bits in RHS that are known, we can propagate them to known
73339d628a0SDimitry Andric // bits in V shifted to the right by C.
7342cab237bSDimitry Andric RHSKnown.Zero.lshrInPlace(C);
73551690af2SDimitry Andric Known.Zero |= RHSKnown.Zero;
7362cab237bSDimitry Andric RHSKnown.One.lshrInPlace(C);
73751690af2SDimitry Andric Known.One |= RHSKnown.One;
73839d628a0SDimitry Andric // assume(~(v << c) = a)
73939d628a0SDimitry Andric } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
74039d628a0SDimitry Andric m_Value(A))) &&
7413ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
7422cab237bSDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
7432cab237bSDimitry Andric C < BitWidth) {
74451690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
74551690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
74639d628a0SDimitry Andric // For those bits in RHS that are known, we can propagate them inverted
74739d628a0SDimitry Andric // to known bits in V shifted to the right by C.
7482cab237bSDimitry Andric RHSKnown.One.lshrInPlace(C);
74951690af2SDimitry Andric Known.Zero |= RHSKnown.One;
7502cab237bSDimitry Andric RHSKnown.Zero.lshrInPlace(C);
75151690af2SDimitry Andric Known.One |= RHSKnown.Zero;
75239d628a0SDimitry Andric // assume(v >> c = a)
75339d628a0SDimitry Andric } else if (match(Arg,
754edd7eaddSDimitry Andric m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)),
75539d628a0SDimitry Andric m_Value(A))) &&
7563ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
7572cab237bSDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
7582cab237bSDimitry Andric C < BitWidth) {
75951690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
76051690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
76139d628a0SDimitry Andric // For those bits in RHS that are known, we can propagate them to known
76239d628a0SDimitry Andric // bits in V shifted to the right by C.
7632cab237bSDimitry Andric Known.Zero |= RHSKnown.Zero << C;
7642cab237bSDimitry Andric Known.One |= RHSKnown.One << C;
76539d628a0SDimitry Andric // assume(~(v >> c) = a)
766edd7eaddSDimitry Andric } else if (match(Arg, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))),
76739d628a0SDimitry Andric m_Value(A))) &&
7683ca95b02SDimitry Andric Pred == ICmpInst::ICMP_EQ &&
7692cab237bSDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT) &&
7702cab237bSDimitry Andric C < BitWidth) {
77151690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
77251690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
77339d628a0SDimitry Andric // For those bits in RHS that are known, we can propagate them inverted
77439d628a0SDimitry Andric // to known bits in V shifted to the right by C.
7752cab237bSDimitry Andric Known.Zero |= RHSKnown.One << C;
7762cab237bSDimitry Andric Known.One |= RHSKnown.Zero << C;
77739d628a0SDimitry Andric // assume(v >=_s c) where c is non-negative
77839d628a0SDimitry Andric } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
7793ca95b02SDimitry Andric Pred == ICmpInst::ICMP_SGE &&
7803ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
78151690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
78251690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
78339d628a0SDimitry Andric
784f37b6182SDimitry Andric if (RHSKnown.isNonNegative()) {
78539d628a0SDimitry Andric // We know that the sign bit is zero.
786f37b6182SDimitry Andric Known.makeNonNegative();
78739d628a0SDimitry Andric }
78839d628a0SDimitry Andric // assume(v >_s c) where c is at least -1.
78939d628a0SDimitry Andric } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
7903ca95b02SDimitry Andric Pred == ICmpInst::ICMP_SGT &&
7913ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
79251690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
79351690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
79439d628a0SDimitry Andric
7950f5676f4SDimitry Andric if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) {
79639d628a0SDimitry Andric // We know that the sign bit is zero.
797f37b6182SDimitry Andric Known.makeNonNegative();
79839d628a0SDimitry Andric }
79939d628a0SDimitry Andric // assume(v <=_s c) where c is negative
80039d628a0SDimitry Andric } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
8013ca95b02SDimitry Andric Pred == ICmpInst::ICMP_SLE &&
8023ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
80351690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
80451690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
80539d628a0SDimitry Andric
806f37b6182SDimitry Andric if (RHSKnown.isNegative()) {
80739d628a0SDimitry Andric // We know that the sign bit is one.
808f37b6182SDimitry Andric Known.makeNegative();
80939d628a0SDimitry Andric }
81039d628a0SDimitry Andric // assume(v <_s c) where c is non-positive
81139d628a0SDimitry Andric } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
8123ca95b02SDimitry Andric Pred == ICmpInst::ICMP_SLT &&
8133ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
81451690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
81551690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
81639d628a0SDimitry Andric
8170f5676f4SDimitry Andric if (RHSKnown.isZero() || RHSKnown.isNegative()) {
81839d628a0SDimitry Andric // We know that the sign bit is one.
819f37b6182SDimitry Andric Known.makeNegative();
82039d628a0SDimitry Andric }
82139d628a0SDimitry Andric // assume(v <=_u c)
82239d628a0SDimitry Andric } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
8233ca95b02SDimitry Andric Pred == ICmpInst::ICMP_ULE &&
8243ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
82551690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
82651690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
82739d628a0SDimitry Andric
82839d628a0SDimitry Andric // Whatever high bits in c are zero are known to be zero.
8295517e702SDimitry Andric Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
83039d628a0SDimitry Andric // assume(v <_u c)
83139d628a0SDimitry Andric } else if (match(Arg, m_ICmp(Pred, m_V, m_Value(A))) &&
8323ca95b02SDimitry Andric Pred == ICmpInst::ICMP_ULT &&
8333ca95b02SDimitry Andric isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
83451690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
83551690af2SDimitry Andric computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
83639d628a0SDimitry Andric
8374ba319b5SDimitry Andric // If the RHS is known zero, then this assumption must be wrong (nothing
8384ba319b5SDimitry Andric // is unsigned less than zero). Signal a conflict and get out of here.
8394ba319b5SDimitry Andric if (RHSKnown.isZero()) {
8404ba319b5SDimitry Andric Known.Zero.setAllBits();
8414ba319b5SDimitry Andric Known.One.setAllBits();
8424ba319b5SDimitry Andric break;
8434ba319b5SDimitry Andric }
8444ba319b5SDimitry Andric
84539d628a0SDimitry Andric // Whatever high bits in c are zero are known to be zero (if c is a power
84639d628a0SDimitry Andric // of 2, then one more).
8473ca95b02SDimitry Andric if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I)))
8485517e702SDimitry Andric Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1);
84939d628a0SDimitry Andric else
8505517e702SDimitry Andric Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
85139d628a0SDimitry Andric }
85239d628a0SDimitry Andric }
853c819c124SDimitry Andric
854c819c124SDimitry Andric // If assumptions conflict with each other or previous known bits, then we
8557a7e6055SDimitry Andric // have a logical fallacy. It's possible that the assumption is not reachable,
8567a7e6055SDimitry Andric // so this isn't a real bug. On the other hand, the program may have undefined
8577a7e6055SDimitry Andric // behavior, or we might have a bug in the compiler. We can't assert/crash, so
8587a7e6055SDimitry Andric // clear out the known bits, try to warn the user, and hope for the best.
85951690af2SDimitry Andric if (Known.Zero.intersects(Known.One)) {
8600f5676f4SDimitry Andric Known.resetAll();
8617a7e6055SDimitry Andric
8622cab237bSDimitry Andric if (Q.ORE)
8632cab237bSDimitry Andric Q.ORE->emit([&]() {
8647a7e6055SDimitry Andric auto *CxtI = const_cast<Instruction *>(Q.CxtI);
8652cab237bSDimitry Andric return OptimizationRemarkAnalysis("value-tracking", "BadAssumption",
8662cab237bSDimitry Andric CxtI)
8672cab237bSDimitry Andric << "Detected conflicting code assumptions. Program may "
8687a7e6055SDimitry Andric "have undefined behavior, or compiler may have "
8692cab237bSDimitry Andric "internal error.";
8702cab237bSDimitry Andric });
871c819c124SDimitry Andric }
87239d628a0SDimitry Andric }
87339d628a0SDimitry Andric
8742cab237bSDimitry Andric /// Compute known bits from a shift operator, including those with a
8752cab237bSDimitry Andric /// non-constant shift amount. Known is the output of this function. Known2 is a
8762cab237bSDimitry Andric /// pre-allocated temporary with the same bit width as Known. KZF and KOF are
8774ba319b5SDimitry Andric /// operator-specific functions that, given the known-zero or known-one bits
8782cab237bSDimitry Andric /// respectively, and a shift amount, compute the implied known-zero or
8792cab237bSDimitry Andric /// known-one bits of the shift operator's result respectively for that shift
8802cab237bSDimitry Andric /// amount. The results from calling KZF and KOF are conservatively combined for
8812cab237bSDimitry Andric /// all permitted shift amounts.
computeKnownBitsFromShiftOperator(const Operator * I,KnownBits & Known,KnownBits & Known2,unsigned Depth,const Query & Q,function_ref<APInt (const APInt &,unsigned)> KZF,function_ref<APInt (const APInt &,unsigned)> KOF)882d88c1a5aSDimitry Andric static void computeKnownBitsFromShiftOperator(
88351690af2SDimitry Andric const Operator *I, KnownBits &Known, KnownBits &Known2,
88451690af2SDimitry Andric unsigned Depth, const Query &Q,
885d88c1a5aSDimitry Andric function_ref<APInt(const APInt &, unsigned)> KZF,
886d88c1a5aSDimitry Andric function_ref<APInt(const APInt &, unsigned)> KOF) {
88751690af2SDimitry Andric unsigned BitWidth = Known.getBitWidth();
8887d523365SDimitry Andric
8897d523365SDimitry Andric if (auto *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
8907d523365SDimitry Andric unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1);
8917d523365SDimitry Andric
89251690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
89351690af2SDimitry Andric Known.Zero = KZF(Known.Zero, ShiftAmt);
89451690af2SDimitry Andric Known.One = KOF(Known.One, ShiftAmt);
8952cab237bSDimitry Andric // If the known bits conflict, this must be an overflowing left shift, so
8962cab237bSDimitry Andric // the shift result is poison. We can return anything we want. Choose 0 for
8972cab237bSDimitry Andric // the best folding opportunity.
8982cab237bSDimitry Andric if (Known.hasConflict())
8992cab237bSDimitry Andric Known.setAllZero();
900d88c1a5aSDimitry Andric
9017d523365SDimitry Andric return;
9027d523365SDimitry Andric }
9037d523365SDimitry Andric
90451690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
9057d523365SDimitry Andric
9062cab237bSDimitry Andric // If the shift amount could be greater than or equal to the bit-width of the
9072cab237bSDimitry Andric // LHS, the value could be poison, but bail out because the check below is
9082cab237bSDimitry Andric // expensive. TODO: Should we just carry on?
90951690af2SDimitry Andric if ((~Known.Zero).uge(BitWidth)) {
9100f5676f4SDimitry Andric Known.resetAll();
9117a7e6055SDimitry Andric return;
9127a7e6055SDimitry Andric }
9137a7e6055SDimitry Andric
91451690af2SDimitry Andric // Note: We cannot use Known.Zero.getLimitedValue() here, because if
9157d523365SDimitry Andric // BitWidth > 64 and any upper bits are known, we'll end up returning the
9167d523365SDimitry Andric // limit value (which implies all bits are known).
91751690af2SDimitry Andric uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue();
91851690af2SDimitry Andric uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue();
9197d523365SDimitry Andric
9207d523365SDimitry Andric // It would be more-clearly correct to use the two temporaries for this
9217d523365SDimitry Andric // calculation. Reusing the APInts here to prevent unnecessary allocations.
9220f5676f4SDimitry Andric Known.resetAll();
9237d523365SDimitry Andric
9247d523365SDimitry Andric // If we know the shifter operand is nonzero, we can sometimes infer more
9257d523365SDimitry Andric // known bits. However this is expensive to compute, so be lazy about it and
9267d523365SDimitry Andric // only compute it when absolutely necessary.
9277d523365SDimitry Andric Optional<bool> ShifterOperandIsNonZero;
9287d523365SDimitry Andric
9297d523365SDimitry Andric // Early exit if we can't constrain any well-defined shift amount.
93024d58133SDimitry Andric if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) &&
93124d58133SDimitry Andric !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) {
9322cab237bSDimitry Andric ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q);
9337d523365SDimitry Andric if (!*ShifterOperandIsNonZero)
9347d523365SDimitry Andric return;
9357d523365SDimitry Andric }
9367d523365SDimitry Andric
93751690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
9387d523365SDimitry Andric
93951690af2SDimitry Andric Known.Zero.setAllBits();
94051690af2SDimitry Andric Known.One.setAllBits();
9417d523365SDimitry Andric for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) {
9427d523365SDimitry Andric // Combine the shifted known input bits only for those shift amounts
9437d523365SDimitry Andric // compatible with its known constraints.
9447d523365SDimitry Andric if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt)
9457d523365SDimitry Andric continue;
9467d523365SDimitry Andric if ((ShiftAmt | ShiftAmtKO) != ShiftAmt)
9477d523365SDimitry Andric continue;
9487d523365SDimitry Andric // If we know the shifter is nonzero, we may be able to infer more known
9497d523365SDimitry Andric // bits. This check is sunk down as far as possible to avoid the expensive
9507d523365SDimitry Andric // call to isKnownNonZero if the cheaper checks above fail.
9517d523365SDimitry Andric if (ShiftAmt == 0) {
9527d523365SDimitry Andric if (!ShifterOperandIsNonZero.hasValue())
9537d523365SDimitry Andric ShifterOperandIsNonZero =
9543ca95b02SDimitry Andric isKnownNonZero(I->getOperand(1), Depth + 1, Q);
9557d523365SDimitry Andric if (*ShifterOperandIsNonZero)
9567d523365SDimitry Andric continue;
9577d523365SDimitry Andric }
9587d523365SDimitry Andric
95951690af2SDimitry Andric Known.Zero &= KZF(Known2.Zero, ShiftAmt);
96051690af2SDimitry Andric Known.One &= KOF(Known2.One, ShiftAmt);
9617d523365SDimitry Andric }
9627d523365SDimitry Andric
9632cab237bSDimitry Andric // If the known bits conflict, the result is poison. Return a 0 and hope the
9642cab237bSDimitry Andric // caller can further optimize that.
9652cab237bSDimitry Andric if (Known.hasConflict())
9662cab237bSDimitry Andric Known.setAllZero();
9677d523365SDimitry Andric }
9687d523365SDimitry Andric
computeKnownBitsFromOperator(const Operator * I,KnownBits & Known,unsigned Depth,const Query & Q)96951690af2SDimitry Andric static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
97051690af2SDimitry Andric unsigned Depth, const Query &Q) {
97151690af2SDimitry Andric unsigned BitWidth = Known.getBitWidth();
972dff0c46cSDimitry Andric
97351690af2SDimitry Andric KnownBits Known2(Known);
974dff0c46cSDimitry Andric switch (I->getOpcode()) {
975dff0c46cSDimitry Andric default: break;
976dff0c46cSDimitry Andric case Instruction::Load:
977b5893f02SDimitry Andric if (MDNode *MD =
978b5893f02SDimitry Andric Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
979f37b6182SDimitry Andric computeKnownBitsFromRangeMetadata(*MD, Known);
98091bc56edSDimitry Andric break;
981dff0c46cSDimitry Andric case Instruction::And: {
982dff0c46cSDimitry Andric // If either the LHS or the RHS are Zero, the result is zero.
98351690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
98451690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
985dff0c46cSDimitry Andric
986dff0c46cSDimitry Andric // Output known-1 bits are only known if set in both the LHS & RHS.
98751690af2SDimitry Andric Known.One &= Known2.One;
988dff0c46cSDimitry Andric // Output known-0 are known to be clear if zero in either the LHS | RHS.
98951690af2SDimitry Andric Known.Zero |= Known2.Zero;
9907d523365SDimitry Andric
9917d523365SDimitry Andric // and(x, add (x, -1)) is a common idiom that always clears the low bit;
9927d523365SDimitry Andric // here we handle the more general case of adding any odd number by
9937d523365SDimitry Andric // matching the form add(x, add(x, y)) where y is odd.
9947d523365SDimitry Andric // TODO: This could be generalized to clearing any bit set in y where the
9957d523365SDimitry Andric // following bit is known to be unset in y.
9964ba319b5SDimitry Andric Value *X = nullptr, *Y = nullptr;
99751690af2SDimitry Andric if (!Known.Zero[0] && !Known.One[0] &&
9984ba319b5SDimitry Andric match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) {
9990f5676f4SDimitry Andric Known2.resetAll();
100051690af2SDimitry Andric computeKnownBits(Y, Known2, Depth + 1, Q);
10015517e702SDimitry Andric if (Known2.countMinTrailingOnes() > 0)
100251690af2SDimitry Andric Known.Zero.setBit(0);
10037d523365SDimitry Andric }
100491bc56edSDimitry Andric break;
1005dff0c46cSDimitry Andric }
10062cab237bSDimitry Andric case Instruction::Or:
100751690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
100851690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1009dff0c46cSDimitry Andric
1010dff0c46cSDimitry Andric // Output known-0 bits are only known if clear in both the LHS & RHS.
101151690af2SDimitry Andric Known.Zero &= Known2.Zero;
1012dff0c46cSDimitry Andric // Output known-1 are known to be set if set in either the LHS | RHS.
101351690af2SDimitry Andric Known.One |= Known2.One;
101491bc56edSDimitry Andric break;
1015dff0c46cSDimitry Andric case Instruction::Xor: {
101651690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
101751690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1018dff0c46cSDimitry Andric
1019dff0c46cSDimitry Andric // Output known-0 bits are known if clear or set in both the LHS & RHS.
102051690af2SDimitry Andric APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
1021dff0c46cSDimitry Andric // Output known-1 are known to be set if set in only one of the LHS, RHS.
102251690af2SDimitry Andric Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
102351690af2SDimitry Andric Known.Zero = std::move(KnownZeroOut);
102491bc56edSDimitry Andric break;
1025dff0c46cSDimitry Andric }
1026dff0c46cSDimitry Andric case Instruction::Mul: {
1027b5893f02SDimitry Andric bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
102851690af2SDimitry Andric computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, Known,
102951690af2SDimitry Andric Known2, Depth, Q);
1030dff0c46cSDimitry Andric break;
1031dff0c46cSDimitry Andric }
1032f22ef01cSRoman Divacky case Instruction::UDiv: {
1033f22ef01cSRoman Divacky // For the purposes of computing leading zeros we can conservatively
1034f22ef01cSRoman Divacky // treat a udiv as a logical right shift by the power of 2 known to
1035f22ef01cSRoman Divacky // be less than the denominator.
103651690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
10375517e702SDimitry Andric unsigned LeadZ = Known2.countMinLeadingZeros();
1038f22ef01cSRoman Divacky
10390f5676f4SDimitry Andric Known2.resetAll();
104051690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
10415517e702SDimitry Andric unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
10425517e702SDimitry Andric if (RHSMaxLeadingZeros != BitWidth)
10435517e702SDimitry Andric LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
1044f22ef01cSRoman Divacky
104551690af2SDimitry Andric Known.Zero.setHighBits(LeadZ);
104691bc56edSDimitry Andric break;
1047f22ef01cSRoman Divacky }
1048d88c1a5aSDimitry Andric case Instruction::Select: {
10497a7e6055SDimitry Andric const Value *LHS, *RHS;
1050d88c1a5aSDimitry Andric SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
1051d88c1a5aSDimitry Andric if (SelectPatternResult::isMinOrMax(SPF)) {
105251690af2SDimitry Andric computeKnownBits(RHS, Known, Depth + 1, Q);
105351690af2SDimitry Andric computeKnownBits(LHS, Known2, Depth + 1, Q);
1054d88c1a5aSDimitry Andric } else {
105551690af2SDimitry Andric computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
105651690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1057d88c1a5aSDimitry Andric }
1058d88c1a5aSDimitry Andric
1059d88c1a5aSDimitry Andric unsigned MaxHighOnes = 0;
1060d88c1a5aSDimitry Andric unsigned MaxHighZeros = 0;
1061d88c1a5aSDimitry Andric if (SPF == SPF_SMAX) {
1062d88c1a5aSDimitry Andric // If both sides are negative, the result is negative.
1063f37b6182SDimitry Andric if (Known.isNegative() && Known2.isNegative())
1064d88c1a5aSDimitry Andric // We can derive a lower bound on the result by taking the max of the
1065d88c1a5aSDimitry Andric // leading one bits.
10665517e702SDimitry Andric MaxHighOnes =
10675517e702SDimitry Andric std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
1068d88c1a5aSDimitry Andric // If either side is non-negative, the result is non-negative.
1069f37b6182SDimitry Andric else if (Known.isNonNegative() || Known2.isNonNegative())
1070d88c1a5aSDimitry Andric MaxHighZeros = 1;
1071d88c1a5aSDimitry Andric } else if (SPF == SPF_SMIN) {
1072d88c1a5aSDimitry Andric // If both sides are non-negative, the result is non-negative.
1073f37b6182SDimitry Andric if (Known.isNonNegative() && Known2.isNonNegative())
1074d88c1a5aSDimitry Andric // We can derive an upper bound on the result by taking the max of the
1075d88c1a5aSDimitry Andric // leading zero bits.
10765517e702SDimitry Andric MaxHighZeros = std::max(Known.countMinLeadingZeros(),
10775517e702SDimitry Andric Known2.countMinLeadingZeros());
1078d88c1a5aSDimitry Andric // If either side is negative, the result is negative.
1079f37b6182SDimitry Andric else if (Known.isNegative() || Known2.isNegative())
1080d88c1a5aSDimitry Andric MaxHighOnes = 1;
1081d88c1a5aSDimitry Andric } else if (SPF == SPF_UMAX) {
1082d88c1a5aSDimitry Andric // We can derive a lower bound on the result by taking the max of the
1083d88c1a5aSDimitry Andric // leading one bits.
1084d88c1a5aSDimitry Andric MaxHighOnes =
10855517e702SDimitry Andric std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
1086d88c1a5aSDimitry Andric } else if (SPF == SPF_UMIN) {
1087d88c1a5aSDimitry Andric // We can derive an upper bound on the result by taking the max of the
1088d88c1a5aSDimitry Andric // leading zero bits.
1089d88c1a5aSDimitry Andric MaxHighZeros =
10905517e702SDimitry Andric std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
10914ba319b5SDimitry Andric } else if (SPF == SPF_ABS) {
10924ba319b5SDimitry Andric // RHS from matchSelectPattern returns the negation part of abs pattern.
10934ba319b5SDimitry Andric // If the negate has an NSW flag we can assume the sign bit of the result
10944ba319b5SDimitry Andric // will be 0 because that makes abs(INT_MIN) undefined.
1095b5893f02SDimitry Andric if (Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
10964ba319b5SDimitry Andric MaxHighZeros = 1;
1097d88c1a5aSDimitry Andric }
1098d88c1a5aSDimitry Andric
1099f22ef01cSRoman Divacky // Only known if known in both the LHS and RHS.
110051690af2SDimitry Andric Known.One &= Known2.One;
110151690af2SDimitry Andric Known.Zero &= Known2.Zero;
1102d88c1a5aSDimitry Andric if (MaxHighOnes > 0)
110351690af2SDimitry Andric Known.One.setHighBits(MaxHighOnes);
1104d88c1a5aSDimitry Andric if (MaxHighZeros > 0)
110551690af2SDimitry Andric Known.Zero.setHighBits(MaxHighZeros);
110691bc56edSDimitry Andric break;
1107d88c1a5aSDimitry Andric }
1108f22ef01cSRoman Divacky case Instruction::FPTrunc:
1109f22ef01cSRoman Divacky case Instruction::FPExt:
1110f22ef01cSRoman Divacky case Instruction::FPToUI:
1111f22ef01cSRoman Divacky case Instruction::FPToSI:
1112f22ef01cSRoman Divacky case Instruction::SIToFP:
1113f22ef01cSRoman Divacky case Instruction::UIToFP:
111491bc56edSDimitry Andric break; // Can't work with floating point.
1115f22ef01cSRoman Divacky case Instruction::PtrToInt:
1116f22ef01cSRoman Divacky case Instruction::IntToPtr:
1117d88c1a5aSDimitry Andric // Fall through and handle them the same as zext/trunc.
1118d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
1119f22ef01cSRoman Divacky case Instruction::ZExt:
1120f22ef01cSRoman Divacky case Instruction::Trunc: {
11216122f3e6SDimitry Andric Type *SrcTy = I->getOperand(0)->getType();
1122f22ef01cSRoman Divacky
1123f22ef01cSRoman Divacky unsigned SrcBitWidth;
1124f22ef01cSRoman Divacky // Note that we handle pointer operands here because of inttoptr/ptrtoint
1125f22ef01cSRoman Divacky // which fall through here.
11264ba319b5SDimitry Andric Type *ScalarTy = SrcTy->getScalarType();
11274ba319b5SDimitry Andric SrcBitWidth = ScalarTy->isPointerTy() ?
11284ba319b5SDimitry Andric Q.DL.getIndexTypeSizeInBits(ScalarTy) :
11294ba319b5SDimitry Andric Q.DL.getTypeSizeInBits(ScalarTy);
1130f22ef01cSRoman Divacky
11313861d79fSDimitry Andric assert(SrcBitWidth && "SrcBitWidth can't be zero");
11320f5676f4SDimitry Andric Known = Known.zextOrTrunc(SrcBitWidth);
113351690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
11340f5676f4SDimitry Andric Known = Known.zextOrTrunc(BitWidth);
1135f22ef01cSRoman Divacky // Any top bits are known to be zero.
1136f22ef01cSRoman Divacky if (BitWidth > SrcBitWidth)
113751690af2SDimitry Andric Known.Zero.setBitsFrom(SrcBitWidth);
113891bc56edSDimitry Andric break;
1139f22ef01cSRoman Divacky }
1140f22ef01cSRoman Divacky case Instruction::BitCast: {
11416122f3e6SDimitry Andric Type *SrcTy = I->getOperand(0)->getType();
11424ba319b5SDimitry Andric if (SrcTy->isIntOrPtrTy() &&
1143f22ef01cSRoman Divacky // TODO: For now, not handling conversions like:
1144f22ef01cSRoman Divacky // (bitcast i64 %x to <2 x i32>)
1145f22ef01cSRoman Divacky !I->getType()->isVectorTy()) {
114651690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
114791bc56edSDimitry Andric break;
1148f22ef01cSRoman Divacky }
1149f22ef01cSRoman Divacky break;
1150f22ef01cSRoman Divacky }
1151f22ef01cSRoman Divacky case Instruction::SExt: {
1152f22ef01cSRoman Divacky // Compute the bits in the result that are not present in the input.
1153f22ef01cSRoman Divacky unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1154f22ef01cSRoman Divacky
11550f5676f4SDimitry Andric Known = Known.trunc(SrcBitWidth);
115651690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1157f22ef01cSRoman Divacky // If the sign bit of the input is known set or clear, then we know the
1158f22ef01cSRoman Divacky // top bits of the result.
11590f5676f4SDimitry Andric Known = Known.sext(BitWidth);
116091bc56edSDimitry Andric break;
1161f22ef01cSRoman Divacky }
11627d523365SDimitry Andric case Instruction::Shl: {
1163f22ef01cSRoman Divacky // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
1164b5893f02SDimitry Andric bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
11657a7e6055SDimitry Andric auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) {
11667a7e6055SDimitry Andric APInt KZResult = KnownZero << ShiftAmt;
11677a7e6055SDimitry Andric KZResult.setLowBits(ShiftAmt); // Low bits known 0.
1168d88c1a5aSDimitry Andric // If this shift has "nsw" keyword, then the result is either a poison
1169d88c1a5aSDimitry Andric // value or has the same sign bit as the first operand.
11706bc11b14SDimitry Andric if (NSW && KnownZero.isSignBitSet())
11717a7e6055SDimitry Andric KZResult.setSignBit();
1172d88c1a5aSDimitry Andric return KZResult;
11737d523365SDimitry Andric };
11747d523365SDimitry Andric
11757a7e6055SDimitry Andric auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) {
1176d88c1a5aSDimitry Andric APInt KOResult = KnownOne << ShiftAmt;
11776bc11b14SDimitry Andric if (NSW && KnownOne.isSignBitSet())
11787a7e6055SDimitry Andric KOResult.setSignBit();
1179d88c1a5aSDimitry Andric return KOResult;
11807d523365SDimitry Andric };
11817d523365SDimitry Andric
118251690af2SDimitry Andric computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
1183f22ef01cSRoman Divacky break;
11847d523365SDimitry Andric }
11857d523365SDimitry Andric case Instruction::LShr: {
11862cab237bSDimitry Andric // (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
11876bc11b14SDimitry Andric auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
11886bc11b14SDimitry Andric APInt KZResult = KnownZero.lshr(ShiftAmt);
11897d523365SDimitry Andric // High bits known zero.
11906bc11b14SDimitry Andric KZResult.setHighBits(ShiftAmt);
11916bc11b14SDimitry Andric return KZResult;
11927d523365SDimitry Andric };
1193f22ef01cSRoman Divacky
11947a7e6055SDimitry Andric auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
11957a7e6055SDimitry Andric return KnownOne.lshr(ShiftAmt);
11967d523365SDimitry Andric };
11977d523365SDimitry Andric
119851690af2SDimitry Andric computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
1199f22ef01cSRoman Divacky break;
12007d523365SDimitry Andric }
12017d523365SDimitry Andric case Instruction::AShr: {
1202f22ef01cSRoman Divacky // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
12037a7e6055SDimitry Andric auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
12047a7e6055SDimitry Andric return KnownZero.ashr(ShiftAmt);
12057d523365SDimitry Andric };
1206f22ef01cSRoman Divacky
12077a7e6055SDimitry Andric auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
12087a7e6055SDimitry Andric return KnownOne.ashr(ShiftAmt);
12097d523365SDimitry Andric };
1210f22ef01cSRoman Divacky
121151690af2SDimitry Andric computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
1212f22ef01cSRoman Divacky break;
12137d523365SDimitry Andric }
1214f22ef01cSRoman Divacky case Instruction::Sub: {
1215b5893f02SDimitry Andric bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
121691bc56edSDimitry Andric computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
121751690af2SDimitry Andric Known, Known2, Depth, Q);
1218dff0c46cSDimitry Andric break;
1219f22ef01cSRoman Divacky }
1220f22ef01cSRoman Divacky case Instruction::Add: {
1221b5893f02SDimitry Andric bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
122291bc56edSDimitry Andric computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
122351690af2SDimitry Andric Known, Known2, Depth, Q);
1224dff0c46cSDimitry Andric break;
1225f22ef01cSRoman Divacky }
1226f22ef01cSRoman Divacky case Instruction::SRem:
1227f22ef01cSRoman Divacky if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
1228f22ef01cSRoman Divacky APInt RA = Rem->getValue().abs();
1229f22ef01cSRoman Divacky if (RA.isPowerOf2()) {
1230f22ef01cSRoman Divacky APInt LowBits = RA - 1;
123151690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1232f22ef01cSRoman Divacky
1233f22ef01cSRoman Divacky // The low bits of the first operand are unchanged by the srem.
123451690af2SDimitry Andric Known.Zero = Known2.Zero & LowBits;
123551690af2SDimitry Andric Known.One = Known2.One & LowBits;
1236f22ef01cSRoman Divacky
1237f22ef01cSRoman Divacky // If the first operand is non-negative or has all low bits zero, then
1238f22ef01cSRoman Divacky // the upper bits are all zero.
1239f37b6182SDimitry Andric if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero))
124051690af2SDimitry Andric Known.Zero |= ~LowBits;
1241f22ef01cSRoman Divacky
1242f22ef01cSRoman Divacky // If the first operand is negative and not all low bits are zero, then
1243f22ef01cSRoman Divacky // the upper bits are all one.
1244f37b6182SDimitry Andric if (Known2.isNegative() && LowBits.intersects(Known2.One))
124551690af2SDimitry Andric Known.One |= ~LowBits;
1246f22ef01cSRoman Divacky
124751690af2SDimitry Andric assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
12486bc11b14SDimitry Andric break;
1249f22ef01cSRoman Divacky }
1250f22ef01cSRoman Divacky }
12513b0f4066SDimitry Andric
12523b0f4066SDimitry Andric // The sign bit is the LHS's sign bit, except when the result of the
12533b0f4066SDimitry Andric // remainder is zero.
125451690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
12553b0f4066SDimitry Andric // If it's known zero, our sign bit is also zero.
1256f37b6182SDimitry Andric if (Known2.isNonNegative())
1257f37b6182SDimitry Andric Known.makeNonNegative();
12583b0f4066SDimitry Andric
1259f22ef01cSRoman Divacky break;
1260f22ef01cSRoman Divacky case Instruction::URem: {
1261f22ef01cSRoman Divacky if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
12623ca95b02SDimitry Andric const APInt &RA = Rem->getValue();
1263f22ef01cSRoman Divacky if (RA.isPowerOf2()) {
1264f22ef01cSRoman Divacky APInt LowBits = (RA - 1);
126551690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
126651690af2SDimitry Andric Known.Zero |= ~LowBits;
126751690af2SDimitry Andric Known.One &= LowBits;
1268f22ef01cSRoman Divacky break;
1269f22ef01cSRoman Divacky }
1270f22ef01cSRoman Divacky }
1271f22ef01cSRoman Divacky
1272f22ef01cSRoman Divacky // Since the result is less than or equal to either operand, any leading
1273f22ef01cSRoman Divacky // zero bits in either operand must also exist in the result.
127451690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
127551690af2SDimitry Andric computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1276f22ef01cSRoman Divacky
12775517e702SDimitry Andric unsigned Leaders =
12785517e702SDimitry Andric std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
12790f5676f4SDimitry Andric Known.resetAll();
128051690af2SDimitry Andric Known.Zero.setHighBits(Leaders);
1281f22ef01cSRoman Divacky break;
1282f22ef01cSRoman Divacky }
1283f22ef01cSRoman Divacky
1284f22ef01cSRoman Divacky case Instruction::Alloca: {
1285d88c1a5aSDimitry Andric const AllocaInst *AI = cast<AllocaInst>(I);
1286f22ef01cSRoman Divacky unsigned Align = AI->getAlignment();
1287ff0cc061SDimitry Andric if (Align == 0)
12883ca95b02SDimitry Andric Align = Q.DL.getABITypeAlignment(AI->getAllocatedType());
1289f22ef01cSRoman Divacky
1290f22ef01cSRoman Divacky if (Align > 0)
129151690af2SDimitry Andric Known.Zero.setLowBits(countTrailingZeros(Align));
1292f22ef01cSRoman Divacky break;
1293f22ef01cSRoman Divacky }
1294f22ef01cSRoman Divacky case Instruction::GetElementPtr: {
1295f22ef01cSRoman Divacky // Analyze all of the subscripts of this getelementptr instruction
1296f22ef01cSRoman Divacky // to determine if we can prove known low zero bits.
129751690af2SDimitry Andric KnownBits LocalKnown(BitWidth);
129851690af2SDimitry Andric computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q);
12995517e702SDimitry Andric unsigned TrailZ = LocalKnown.countMinTrailingZeros();
1300f22ef01cSRoman Divacky
1301f22ef01cSRoman Divacky gep_type_iterator GTI = gep_type_begin(I);
1302f22ef01cSRoman Divacky for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1303f22ef01cSRoman Divacky Value *Index = I->getOperand(i);
1304d88c1a5aSDimitry Andric if (StructType *STy = GTI.getStructTypeOrNull()) {
1305f22ef01cSRoman Divacky // Handle struct member offset arithmetic.
1306f785676fSDimitry Andric
1307f785676fSDimitry Andric // Handle case when index is vector zeroinitializer
1308f785676fSDimitry Andric Constant *CIndex = cast<Constant>(Index);
1309f785676fSDimitry Andric if (CIndex->isZeroValue())
1310f785676fSDimitry Andric continue;
1311f785676fSDimitry Andric
1312f785676fSDimitry Andric if (CIndex->getType()->isVectorTy())
1313f785676fSDimitry Andric Index = CIndex->getSplatValue();
1314f785676fSDimitry Andric
1315f22ef01cSRoman Divacky unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
13163ca95b02SDimitry Andric const StructLayout *SL = Q.DL.getStructLayout(STy);
1317f22ef01cSRoman Divacky uint64_t Offset = SL->getElementOffset(Idx);
1318f785676fSDimitry Andric TrailZ = std::min<unsigned>(TrailZ,
1319f785676fSDimitry Andric countTrailingZeros(Offset));
1320f22ef01cSRoman Divacky } else {
1321f22ef01cSRoman Divacky // Handle array index arithmetic.
13226122f3e6SDimitry Andric Type *IndexedTy = GTI.getIndexedType();
132391bc56edSDimitry Andric if (!IndexedTy->isSized()) {
132491bc56edSDimitry Andric TrailZ = 0;
132591bc56edSDimitry Andric break;
132691bc56edSDimitry Andric }
1327f22ef01cSRoman Divacky unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
13283ca95b02SDimitry Andric uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy);
132951690af2SDimitry Andric LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0);
133051690af2SDimitry Andric computeKnownBits(Index, LocalKnown, Depth + 1, Q);
1331f22ef01cSRoman Divacky TrailZ = std::min(TrailZ,
1332f785676fSDimitry Andric unsigned(countTrailingZeros(TypeSize) +
13335517e702SDimitry Andric LocalKnown.countMinTrailingZeros()));
1334f22ef01cSRoman Divacky }
1335f22ef01cSRoman Divacky }
1336f22ef01cSRoman Divacky
133751690af2SDimitry Andric Known.Zero.setLowBits(TrailZ);
1338f22ef01cSRoman Divacky break;
1339f22ef01cSRoman Divacky }
1340f22ef01cSRoman Divacky case Instruction::PHI: {
1341d88c1a5aSDimitry Andric const PHINode *P = cast<PHINode>(I);
1342f22ef01cSRoman Divacky // Handle the case of a simple two-predecessor recurrence PHI.
1343f22ef01cSRoman Divacky // There's a lot more that could theoretically be done here, but
1344f22ef01cSRoman Divacky // this is sufficient to catch some interesting cases.
1345f22ef01cSRoman Divacky if (P->getNumIncomingValues() == 2) {
1346f22ef01cSRoman Divacky for (unsigned i = 0; i != 2; ++i) {
1347f22ef01cSRoman Divacky Value *L = P->getIncomingValue(i);
1348f22ef01cSRoman Divacky Value *R = P->getIncomingValue(!i);
1349f22ef01cSRoman Divacky Operator *LU = dyn_cast<Operator>(L);
1350f22ef01cSRoman Divacky if (!LU)
1351f22ef01cSRoman Divacky continue;
1352f22ef01cSRoman Divacky unsigned Opcode = LU->getOpcode();
1353f22ef01cSRoman Divacky // Check for operations that have the property that if
1354f22ef01cSRoman Divacky // both their operands have low zero bits, the result
1355f22ef01cSRoman Divacky // will have low zero bits.
1356f22ef01cSRoman Divacky if (Opcode == Instruction::Add ||
1357f22ef01cSRoman Divacky Opcode == Instruction::Sub ||
1358f22ef01cSRoman Divacky Opcode == Instruction::And ||
1359f22ef01cSRoman Divacky Opcode == Instruction::Or ||
1360f22ef01cSRoman Divacky Opcode == Instruction::Mul) {
1361f22ef01cSRoman Divacky Value *LL = LU->getOperand(0);
1362f22ef01cSRoman Divacky Value *LR = LU->getOperand(1);
1363f22ef01cSRoman Divacky // Find a recurrence.
1364f22ef01cSRoman Divacky if (LL == I)
1365f22ef01cSRoman Divacky L = LR;
1366f22ef01cSRoman Divacky else if (LR == I)
1367f22ef01cSRoman Divacky L = LL;
1368f22ef01cSRoman Divacky else
1369f22ef01cSRoman Divacky break;
1370f22ef01cSRoman Divacky // Ok, we have a PHI of the form L op= R. Check for low
1371f22ef01cSRoman Divacky // zero bits.
137251690af2SDimitry Andric computeKnownBits(R, Known2, Depth + 1, Q);
1373f22ef01cSRoman Divacky
1374f22ef01cSRoman Divacky // We need to take the minimum number of known bits
137551690af2SDimitry Andric KnownBits Known3(Known);
137651690af2SDimitry Andric computeKnownBits(L, Known3, Depth + 1, Q);
1377f22ef01cSRoman Divacky
13785517e702SDimitry Andric Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
13795517e702SDimitry Andric Known3.countMinTrailingZeros()));
1380d88c1a5aSDimitry Andric
1381d88c1a5aSDimitry Andric auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU);
1382b5893f02SDimitry Andric if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1383d88c1a5aSDimitry Andric // If initial value of recurrence is nonnegative, and we are adding
1384d88c1a5aSDimitry Andric // a nonnegative number with nsw, the result can only be nonnegative
1385d88c1a5aSDimitry Andric // or poison value regardless of the number of times we execute the
1386d88c1a5aSDimitry Andric // add in phi recurrence. If initial value is negative and we are
1387d88c1a5aSDimitry Andric // adding a negative number with nsw, the result can only be
1388d88c1a5aSDimitry Andric // negative or poison value. Similar arguments apply to sub and mul.
1389d88c1a5aSDimitry Andric //
1390d88c1a5aSDimitry Andric // (add non-negative, non-negative) --> non-negative
1391d88c1a5aSDimitry Andric // (add negative, negative) --> negative
1392d88c1a5aSDimitry Andric if (Opcode == Instruction::Add) {
1393f37b6182SDimitry Andric if (Known2.isNonNegative() && Known3.isNonNegative())
1394f37b6182SDimitry Andric Known.makeNonNegative();
1395f37b6182SDimitry Andric else if (Known2.isNegative() && Known3.isNegative())
1396f37b6182SDimitry Andric Known.makeNegative();
1397d88c1a5aSDimitry Andric }
1398d88c1a5aSDimitry Andric
1399d88c1a5aSDimitry Andric // (sub nsw non-negative, negative) --> non-negative
1400d88c1a5aSDimitry Andric // (sub nsw negative, non-negative) --> negative
1401d88c1a5aSDimitry Andric else if (Opcode == Instruction::Sub && LL == I) {
1402f37b6182SDimitry Andric if (Known2.isNonNegative() && Known3.isNegative())
1403f37b6182SDimitry Andric Known.makeNonNegative();
1404f37b6182SDimitry Andric else if (Known2.isNegative() && Known3.isNonNegative())
1405f37b6182SDimitry Andric Known.makeNegative();
1406d88c1a5aSDimitry Andric }
1407d88c1a5aSDimitry Andric
1408d88c1a5aSDimitry Andric // (mul nsw non-negative, non-negative) --> non-negative
1409f37b6182SDimitry Andric else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1410f37b6182SDimitry Andric Known3.isNonNegative())
1411f37b6182SDimitry Andric Known.makeNonNegative();
1412d88c1a5aSDimitry Andric }
1413d88c1a5aSDimitry Andric
1414f22ef01cSRoman Divacky break;
1415f22ef01cSRoman Divacky }
1416f22ef01cSRoman Divacky }
1417f22ef01cSRoman Divacky }
1418f22ef01cSRoman Divacky
14192754fe60SDimitry Andric // Unreachable blocks may have zero-operand PHI nodes.
14202754fe60SDimitry Andric if (P->getNumIncomingValues() == 0)
142191bc56edSDimitry Andric break;
14222754fe60SDimitry Andric
1423f22ef01cSRoman Divacky // Otherwise take the unions of the known bit sets of the operands,
1424f22ef01cSRoman Divacky // taking conservative care to avoid excessive recursion.
142551690af2SDimitry Andric if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) {
14263b0f4066SDimitry Andric // Skip if every incoming value references to ourself.
14277ae0e2c9SDimitry Andric if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
14283b0f4066SDimitry Andric break;
14293b0f4066SDimitry Andric
143051690af2SDimitry Andric Known.Zero.setAllBits();
143151690af2SDimitry Andric Known.One.setAllBits();
1432ff0cc061SDimitry Andric for (Value *IncValue : P->incoming_values()) {
1433f22ef01cSRoman Divacky // Skip direct self references.
1434ff0cc061SDimitry Andric if (IncValue == P) continue;
1435f22ef01cSRoman Divacky
143651690af2SDimitry Andric Known2 = KnownBits(BitWidth);
1437f22ef01cSRoman Divacky // Recurse, but cap the recursion to one level, because we don't
1438f22ef01cSRoman Divacky // want to waste time spinning around in loops.
143951690af2SDimitry Andric computeKnownBits(IncValue, Known2, MaxDepth - 1, Q);
144051690af2SDimitry Andric Known.Zero &= Known2.Zero;
144151690af2SDimitry Andric Known.One &= Known2.One;
1442f22ef01cSRoman Divacky // If all bits have been ruled out, there's no need to check
1443f22ef01cSRoman Divacky // more operands.
144451690af2SDimitry Andric if (!Known.Zero && !Known.One)
1445f22ef01cSRoman Divacky break;
1446f22ef01cSRoman Divacky }
1447f22ef01cSRoman Divacky }
1448f22ef01cSRoman Divacky break;
1449f22ef01cSRoman Divacky }
1450f22ef01cSRoman Divacky case Instruction::Call:
145191bc56edSDimitry Andric case Instruction::Invoke:
14523ca95b02SDimitry Andric // If range metadata is attached to this call, set known bits from that,
14533ca95b02SDimitry Andric // and then intersect with known bits based on other properties of the
14543ca95b02SDimitry Andric // function.
1455b5893f02SDimitry Andric if (MDNode *MD =
1456b5893f02SDimitry Andric Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1457f37b6182SDimitry Andric computeKnownBitsFromRangeMetadata(*MD, Known);
1458d88c1a5aSDimitry Andric if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) {
145951690af2SDimitry Andric computeKnownBits(RV, Known2, Depth + 1, Q);
146051690af2SDimitry Andric Known.Zero |= Known2.Zero;
146151690af2SDimitry Andric Known.One |= Known2.One;
14623ca95b02SDimitry Andric }
1463d88c1a5aSDimitry Andric if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1464f22ef01cSRoman Divacky switch (II->getIntrinsicID()) {
1465f22ef01cSRoman Divacky default: break;
14667a7e6055SDimitry Andric case Intrinsic::bitreverse:
146751690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
146851690af2SDimitry Andric Known.Zero |= Known2.Zero.reverseBits();
146951690af2SDimitry Andric Known.One |= Known2.One.reverseBits();
14707a7e6055SDimitry Andric break;
14717d523365SDimitry Andric case Intrinsic::bswap:
147251690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
147351690af2SDimitry Andric Known.Zero |= Known2.Zero.byteSwap();
147451690af2SDimitry Andric Known.One |= Known2.One.byteSwap();
14757d523365SDimitry Andric break;
14765517e702SDimitry Andric case Intrinsic::ctlz: {
14775517e702SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
14785517e702SDimitry Andric // If we have a known 1, its position is our upper bound.
14795517e702SDimitry Andric unsigned PossibleLZ = Known2.One.countLeadingZeros();
1480dff0c46cSDimitry Andric // If this call is undefined for 0, the result will be less than 2^n.
1481dff0c46cSDimitry Andric if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
14825517e702SDimitry Andric PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
14835517e702SDimitry Andric unsigned LowBits = Log2_32(PossibleLZ)+1;
14845517e702SDimitry Andric Known.Zero.setBitsFrom(LowBits);
14855517e702SDimitry Andric break;
14865517e702SDimitry Andric }
14875517e702SDimitry Andric case Intrinsic::cttz: {
14885517e702SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
14895517e702SDimitry Andric // If we have a known 1, its position is our upper bound.
14905517e702SDimitry Andric unsigned PossibleTZ = Known2.One.countTrailingZeros();
14915517e702SDimitry Andric // If this call is undefined for 0, the result will be less than 2^n.
14925517e702SDimitry Andric if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
14935517e702SDimitry Andric PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
14945517e702SDimitry Andric unsigned LowBits = Log2_32(PossibleTZ)+1;
149551690af2SDimitry Andric Known.Zero.setBitsFrom(LowBits);
1496dff0c46cSDimitry Andric break;
1497dff0c46cSDimitry Andric }
1498dff0c46cSDimitry Andric case Intrinsic::ctpop: {
149951690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
15007d523365SDimitry Andric // We can bound the space the count needs. Also, bits known to be zero
15017d523365SDimitry Andric // can't contribute to the population.
15025517e702SDimitry Andric unsigned BitsPossiblySet = Known2.countMaxPopulation();
15037a7e6055SDimitry Andric unsigned LowBits = Log2_32(BitsPossiblySet)+1;
150451690af2SDimitry Andric Known.Zero.setBitsFrom(LowBits);
15057d523365SDimitry Andric // TODO: we could bound KnownOne using the lower bound on the number
15067d523365SDimitry Andric // of bits which might be set provided by popcnt KnownOne2.
15077d523365SDimitry Andric break;
15087d523365SDimitry Andric }
1509b5893f02SDimitry Andric case Intrinsic::fshr:
1510b5893f02SDimitry Andric case Intrinsic::fshl: {
1511b5893f02SDimitry Andric const APInt *SA;
1512b5893f02SDimitry Andric if (!match(I->getOperand(2), m_APInt(SA)))
1513b5893f02SDimitry Andric break;
1514b5893f02SDimitry Andric
1515b5893f02SDimitry Andric // Normalize to funnel shift left.
1516b5893f02SDimitry Andric uint64_t ShiftAmt = SA->urem(BitWidth);
1517b5893f02SDimitry Andric if (II->getIntrinsicID() == Intrinsic::fshr)
1518b5893f02SDimitry Andric ShiftAmt = BitWidth - ShiftAmt;
1519b5893f02SDimitry Andric
1520b5893f02SDimitry Andric KnownBits Known3(Known);
1521b5893f02SDimitry Andric computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1522b5893f02SDimitry Andric computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
1523b5893f02SDimitry Andric
1524b5893f02SDimitry Andric Known.Zero =
1525b5893f02SDimitry Andric Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1526b5893f02SDimitry Andric Known.One =
1527b5893f02SDimitry Andric Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1528b5893f02SDimitry Andric break;
1529b5893f02SDimitry Andric }
1530bd5abe19SDimitry Andric case Intrinsic::x86_sse42_crc32_64_64:
153151690af2SDimitry Andric Known.Zero.setBitsFrom(32);
1532bd5abe19SDimitry Andric break;
1533f22ef01cSRoman Divacky }
1534f22ef01cSRoman Divacky }
1535f22ef01cSRoman Divacky break;
1536d88c1a5aSDimitry Andric case Instruction::ExtractElement:
1537d88c1a5aSDimitry Andric // Look through extract element. At the moment we keep this simple and skip
1538d88c1a5aSDimitry Andric // tracking the specific element. But at least we might find information
1539d88c1a5aSDimitry Andric // valid for all elements of the vector (for example if vector is sign
1540d88c1a5aSDimitry Andric // extended, shifted, etc).
154151690af2SDimitry Andric computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1542d88c1a5aSDimitry Andric break;
1543dff0c46cSDimitry Andric case Instruction::ExtractValue:
1544dff0c46cSDimitry Andric if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1545d88c1a5aSDimitry Andric const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1546dff0c46cSDimitry Andric if (EVI->getNumIndices() != 1) break;
1547dff0c46cSDimitry Andric if (EVI->getIndices()[0] == 0) {
1548dff0c46cSDimitry Andric switch (II->getIntrinsicID()) {
1549dff0c46cSDimitry Andric default: break;
1550dff0c46cSDimitry Andric case Intrinsic::uadd_with_overflow:
1551dff0c46cSDimitry Andric case Intrinsic::sadd_with_overflow:
155291bc56edSDimitry Andric computeKnownBitsAddSub(true, II->getArgOperand(0),
155351690af2SDimitry Andric II->getArgOperand(1), false, Known, Known2,
155451690af2SDimitry Andric Depth, Q);
1555dff0c46cSDimitry Andric break;
1556dff0c46cSDimitry Andric case Intrinsic::usub_with_overflow:
1557dff0c46cSDimitry Andric case Intrinsic::ssub_with_overflow:
155891bc56edSDimitry Andric computeKnownBitsAddSub(false, II->getArgOperand(0),
155951690af2SDimitry Andric II->getArgOperand(1), false, Known, Known2,
156051690af2SDimitry Andric Depth, Q);
1561dff0c46cSDimitry Andric break;
1562dff0c46cSDimitry Andric case Intrinsic::umul_with_overflow:
1563dff0c46cSDimitry Andric case Intrinsic::smul_with_overflow:
1564ff0cc061SDimitry Andric computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
156551690af2SDimitry Andric Known, Known2, Depth, Q);
1566dff0c46cSDimitry Andric break;
1567dff0c46cSDimitry Andric }
1568dff0c46cSDimitry Andric }
1569dff0c46cSDimitry Andric }
1570f22ef01cSRoman Divacky }
15718f0fd8f6SDimitry Andric }
15728f0fd8f6SDimitry Andric
15738f0fd8f6SDimitry Andric /// Determine which bits of V are known to be either zero or one and return
15745517e702SDimitry Andric /// them.
computeKnownBits(const Value * V,unsigned Depth,const Query & Q)15755517e702SDimitry Andric KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) {
15765517e702SDimitry Andric KnownBits Known(getBitWidth(V->getType(), Q.DL));
15775517e702SDimitry Andric computeKnownBits(V, Known, Depth, Q);
15785517e702SDimitry Andric return Known;
15795517e702SDimitry Andric }
15805517e702SDimitry Andric
15815517e702SDimitry Andric /// Determine which bits of V are known to be either zero or one and return
158251690af2SDimitry Andric /// them in the Known bit set.
15838f0fd8f6SDimitry Andric ///
15848f0fd8f6SDimitry Andric /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
15858f0fd8f6SDimitry Andric /// we cannot optimize based on the assumption that it is zero without changing
15868f0fd8f6SDimitry Andric /// it to be an explicit zero. If we don't change it to zero, other code could
15878f0fd8f6SDimitry Andric /// optimized based on the contradictory assumption that it is non-zero.
15888f0fd8f6SDimitry Andric /// Because instcombine aggressively folds operations with undef args anyway,
15898f0fd8f6SDimitry Andric /// this won't lose us code quality.
15908f0fd8f6SDimitry Andric ///
15918f0fd8f6SDimitry Andric /// This function is defined on values with integer type, values with pointer
15928f0fd8f6SDimitry Andric /// type, and vectors of integers. In the case
15938f0fd8f6SDimitry Andric /// where V is a vector, known zero, and known one values are the
15948f0fd8f6SDimitry Andric /// same width as the vector element, and the bit is set only if it is true
15958f0fd8f6SDimitry Andric /// for all of the elements in the vector.
computeKnownBits(const Value * V,KnownBits & Known,unsigned Depth,const Query & Q)159651690af2SDimitry Andric void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
159751690af2SDimitry Andric const Query &Q) {
15988f0fd8f6SDimitry Andric assert(V && "No Value?");
15998f0fd8f6SDimitry Andric assert(Depth <= MaxDepth && "Limit Search Depth");
160051690af2SDimitry Andric unsigned BitWidth = Known.getBitWidth();
16018f0fd8f6SDimitry Andric
1602c4394386SDimitry Andric assert((V->getType()->isIntOrIntVectorTy(BitWidth) ||
1603c4394386SDimitry Andric V->getType()->isPtrOrPtrVectorTy()) &&
16043ca95b02SDimitry Andric "Not integer or pointer type!");
16054ba319b5SDimitry Andric
16064ba319b5SDimitry Andric Type *ScalarTy = V->getType()->getScalarType();
16074ba319b5SDimitry Andric unsigned ExpectedWidth = ScalarTy->isPointerTy() ?
16084ba319b5SDimitry Andric Q.DL.getIndexTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy);
16094ba319b5SDimitry Andric assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth");
16107a7e6055SDimitry Andric (void)BitWidth;
16114ba319b5SDimitry Andric (void)ExpectedWidth;
16128f0fd8f6SDimitry Andric
1613d88c1a5aSDimitry Andric const APInt *C;
1614d88c1a5aSDimitry Andric if (match(V, m_APInt(C))) {
1615d88c1a5aSDimitry Andric // We know all of the bits for a scalar constant or a splat vector constant!
161651690af2SDimitry Andric Known.One = *C;
161751690af2SDimitry Andric Known.Zero = ~Known.One;
16188f0fd8f6SDimitry Andric return;
16198f0fd8f6SDimitry Andric }
16208f0fd8f6SDimitry Andric // Null and aggregate-zero are all-zeros.
16213ca95b02SDimitry Andric if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
16220f5676f4SDimitry Andric Known.setAllZero();
16238f0fd8f6SDimitry Andric return;
16248f0fd8f6SDimitry Andric }
16258f0fd8f6SDimitry Andric // Handle a constant vector by taking the intersection of the known bits of
16263ca95b02SDimitry Andric // each element.
1627d88c1a5aSDimitry Andric if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
16288f0fd8f6SDimitry Andric // We know that CDS must be a vector of integers. Take the intersection of
16298f0fd8f6SDimitry Andric // each element.
163051690af2SDimitry Andric Known.Zero.setAllBits(); Known.One.setAllBits();
16318f0fd8f6SDimitry Andric for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
16322cab237bSDimitry Andric APInt Elt = CDS->getElementAsAPInt(i);
163351690af2SDimitry Andric Known.Zero &= ~Elt;
163451690af2SDimitry Andric Known.One &= Elt;
16358f0fd8f6SDimitry Andric }
16368f0fd8f6SDimitry Andric return;
16378f0fd8f6SDimitry Andric }
16388f0fd8f6SDimitry Andric
1639d88c1a5aSDimitry Andric if (const auto *CV = dyn_cast<ConstantVector>(V)) {
16403ca95b02SDimitry Andric // We know that CV must be a vector of integers. Take the intersection of
16413ca95b02SDimitry Andric // each element.
164251690af2SDimitry Andric Known.Zero.setAllBits(); Known.One.setAllBits();
16433ca95b02SDimitry Andric for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
16443ca95b02SDimitry Andric Constant *Element = CV->getAggregateElement(i);
16453ca95b02SDimitry Andric auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
16463ca95b02SDimitry Andric if (!ElementCI) {
16470f5676f4SDimitry Andric Known.resetAll();
16483ca95b02SDimitry Andric return;
16493ca95b02SDimitry Andric }
16502cab237bSDimitry Andric const APInt &Elt = ElementCI->getValue();
165151690af2SDimitry Andric Known.Zero &= ~Elt;
165251690af2SDimitry Andric Known.One &= Elt;
16533ca95b02SDimitry Andric }
16543ca95b02SDimitry Andric return;
16553ca95b02SDimitry Andric }
16563ca95b02SDimitry Andric
16578f0fd8f6SDimitry Andric // Start out not knowing anything.
16580f5676f4SDimitry Andric Known.resetAll();
16598f0fd8f6SDimitry Andric
1660d88c1a5aSDimitry Andric // We can't imply anything about undefs.
1661d88c1a5aSDimitry Andric if (isa<UndefValue>(V))
1662d88c1a5aSDimitry Andric return;
1663d88c1a5aSDimitry Andric
1664d88c1a5aSDimitry Andric // There's no point in looking through other users of ConstantData for
1665d88c1a5aSDimitry Andric // assumptions. Confirm that we've handled them all.
1666d88c1a5aSDimitry Andric assert(!isa<ConstantData>(V) && "Unhandled constant data!");
1667d88c1a5aSDimitry Andric
16688f0fd8f6SDimitry Andric // Limit search depth.
16698f0fd8f6SDimitry Andric // All recursive calls that increase depth must come after this.
16708f0fd8f6SDimitry Andric if (Depth == MaxDepth)
16718f0fd8f6SDimitry Andric return;
16728f0fd8f6SDimitry Andric
16738f0fd8f6SDimitry Andric // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
16748f0fd8f6SDimitry Andric // the bits of its aliasee.
1675d88c1a5aSDimitry Andric if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
16763ca95b02SDimitry Andric if (!GA->isInterposable())
167751690af2SDimitry Andric computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
16788f0fd8f6SDimitry Andric return;
16798f0fd8f6SDimitry Andric }
16808f0fd8f6SDimitry Andric
1681d88c1a5aSDimitry Andric if (const Operator *I = dyn_cast<Operator>(V))
168251690af2SDimitry Andric computeKnownBitsFromOperator(I, Known, Depth, Q);
16837d523365SDimitry Andric
168451690af2SDimitry Andric // Aligned pointers have trailing zeros - refine Known.Zero set
16857d523365SDimitry Andric if (V->getType()->isPointerTy()) {
16863ca95b02SDimitry Andric unsigned Align = V->getPointerAlignment(Q.DL);
16877d523365SDimitry Andric if (Align)
168851690af2SDimitry Andric Known.Zero.setLowBits(countTrailingZeros(Align));
16897d523365SDimitry Andric }
16907d523365SDimitry Andric
169151690af2SDimitry Andric // computeKnownBitsFromAssume strictly refines Known.
169251690af2SDimitry Andric // Therefore, we run them after computeKnownBitsFromOperator.
16938f0fd8f6SDimitry Andric
16948f0fd8f6SDimitry Andric // Check whether a nearby assume intrinsic can determine some known bits.
169551690af2SDimitry Andric computeKnownBitsFromAssume(V, Known, Depth, Q);
169691bc56edSDimitry Andric
169751690af2SDimitry Andric assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
1698f22ef01cSRoman Divacky }
1699f22ef01cSRoman Divacky
170039d628a0SDimitry Andric /// Return true if the given value is known to have exactly one
17012754fe60SDimitry Andric /// bit set when defined. For vectors return true if every element is known to
17022754fe60SDimitry Andric /// be a power of two when defined. Supports values with integer or pointer
17032754fe60SDimitry Andric /// types and vectors of integers.
isKnownToBeAPowerOfTwo(const Value * V,bool OrZero,unsigned Depth,const Query & Q)1704d88c1a5aSDimitry Andric bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
17053ca95b02SDimitry Andric const Query &Q) {
17062cab237bSDimitry Andric assert(Depth <= MaxDepth && "Limit Search Depth");
17072cab237bSDimitry Andric
17084ba319b5SDimitry Andric // Attempt to match against constants.
17094ba319b5SDimitry Andric if (OrZero && match(V, m_Power2OrZero()))
17104ba319b5SDimitry Andric return true;
17114ba319b5SDimitry Andric if (match(V, m_Power2()))
17124ba319b5SDimitry Andric return true;
17132754fe60SDimitry Andric
17142754fe60SDimitry Andric // 1 << X is clearly a power of two if the one is not shifted off the end. If
17152754fe60SDimitry Andric // it is shifted off the end then the result is undefined.
17162754fe60SDimitry Andric if (match(V, m_Shl(m_One(), m_Value())))
17172754fe60SDimitry Andric return true;
17182754fe60SDimitry Andric
17196bc11b14SDimitry Andric // (signmask) >>l X is clearly a power of two if the one is not shifted off
17206bc11b14SDimitry Andric // the bottom. If it is shifted off the bottom then the result is undefined.
17216bc11b14SDimitry Andric if (match(V, m_LShr(m_SignMask(), m_Value())))
17222754fe60SDimitry Andric return true;
17232754fe60SDimitry Andric
17242754fe60SDimitry Andric // The remaining tests are all recursive, so bail out if we hit the limit.
17252754fe60SDimitry Andric if (Depth++ == MaxDepth)
17262754fe60SDimitry Andric return false;
17272754fe60SDimitry Andric
172891bc56edSDimitry Andric Value *X = nullptr, *Y = nullptr;
17294d0b32cdSDimitry Andric // A shift left or a logical shift right of a power of two is a power of two
17304d0b32cdSDimitry Andric // or zero.
1731dff0c46cSDimitry Andric if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
17324d0b32cdSDimitry Andric match(V, m_LShr(m_Value(X), m_Value()))))
17333ca95b02SDimitry Andric return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q);
1734dff0c46cSDimitry Andric
1735d88c1a5aSDimitry Andric if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V))
17363ca95b02SDimitry Andric return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q);
17372754fe60SDimitry Andric
1738d88c1a5aSDimitry Andric if (const SelectInst *SI = dyn_cast<SelectInst>(V))
17393ca95b02SDimitry Andric return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) &&
17403ca95b02SDimitry Andric isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q);
1741dff0c46cSDimitry Andric
1742dff0c46cSDimitry Andric if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
1743dff0c46cSDimitry Andric // A power of two and'd with anything is a power of two or zero.
17443ca95b02SDimitry Andric if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) ||
17453ca95b02SDimitry Andric isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q))
1746dff0c46cSDimitry Andric return true;
1747dff0c46cSDimitry Andric // X & (-X) is always a power of two or zero.
1748dff0c46cSDimitry Andric if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
1749dff0c46cSDimitry Andric return true;
1750dff0c46cSDimitry Andric return false;
1751dff0c46cSDimitry Andric }
17522754fe60SDimitry Andric
1753f785676fSDimitry Andric // Adding a power-of-two or zero to the same power-of-two or zero yields
1754f785676fSDimitry Andric // either the original power-of-two, a larger power-of-two or zero.
1755f785676fSDimitry Andric if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
1756d88c1a5aSDimitry Andric const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
1757b5893f02SDimitry Andric if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
1758b5893f02SDimitry Andric Q.IIQ.hasNoSignedWrap(VOBO)) {
1759f785676fSDimitry Andric if (match(X, m_And(m_Specific(Y), m_Value())) ||
1760f785676fSDimitry Andric match(X, m_And(m_Value(), m_Specific(Y))))
17613ca95b02SDimitry Andric if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q))
1762f785676fSDimitry Andric return true;
1763f785676fSDimitry Andric if (match(Y, m_And(m_Specific(X), m_Value())) ||
1764f785676fSDimitry Andric match(Y, m_And(m_Value(), m_Specific(X))))
17653ca95b02SDimitry Andric if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q))
1766f785676fSDimitry Andric return true;
1767f785676fSDimitry Andric
1768f785676fSDimitry Andric unsigned BitWidth = V->getType()->getScalarSizeInBits();
176951690af2SDimitry Andric KnownBits LHSBits(BitWidth);
177051690af2SDimitry Andric computeKnownBits(X, LHSBits, Depth, Q);
1771f785676fSDimitry Andric
177251690af2SDimitry Andric KnownBits RHSBits(BitWidth);
177351690af2SDimitry Andric computeKnownBits(Y, RHSBits, Depth, Q);
1774f785676fSDimitry Andric // If i8 V is a power of two or zero:
1775f785676fSDimitry Andric // ZeroBits: 1 1 1 0 1 1 1 1
1776f785676fSDimitry Andric // ~ZeroBits: 0 0 0 1 0 0 0 0
177751690af2SDimitry Andric if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
1778f785676fSDimitry Andric // If OrZero isn't set, we cannot give back a zero result.
1779f785676fSDimitry Andric // Make sure either the LHS or RHS has a bit set.
178051690af2SDimitry Andric if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
1781f785676fSDimitry Andric return true;
1782f785676fSDimitry Andric }
1783f785676fSDimitry Andric }
1784f785676fSDimitry Andric
17853b0f4066SDimitry Andric // An exact divide or right shift can only shift off zero bits, so the result
17863b0f4066SDimitry Andric // is a power of two only if the first operand is a power of two and not
17873b0f4066SDimitry Andric // copying a sign bit (sdiv int_min, 2).
1788dff0c46cSDimitry Andric if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
1789dff0c46cSDimitry Andric match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
179039d628a0SDimitry Andric return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
17913ca95b02SDimitry Andric Depth, Q);
1792139f7f9bSDimitry Andric }
1793139f7f9bSDimitry Andric
1794139f7f9bSDimitry Andric return false;
1795139f7f9bSDimitry Andric }
1796139f7f9bSDimitry Andric
17974ba319b5SDimitry Andric /// Test whether a GEP's result is known to be non-null.
1798139f7f9bSDimitry Andric ///
1799139f7f9bSDimitry Andric /// Uses properties inherent in a GEP to try to determine whether it is known
1800139f7f9bSDimitry Andric /// to be non-null.
1801139f7f9bSDimitry Andric ///
1802139f7f9bSDimitry Andric /// Currently this routine does not support vector GEPs.
isGEPKnownNonNull(const GEPOperator * GEP,unsigned Depth,const Query & Q)1803d88c1a5aSDimitry Andric static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
18043ca95b02SDimitry Andric const Query &Q) {
18054ba319b5SDimitry Andric const Function *F = nullptr;
18064ba319b5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(GEP))
18074ba319b5SDimitry Andric F = I->getFunction();
18084ba319b5SDimitry Andric
18094ba319b5SDimitry Andric if (!GEP->isInBounds() ||
18104ba319b5SDimitry Andric NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
1811139f7f9bSDimitry Andric return false;
1812139f7f9bSDimitry Andric
1813139f7f9bSDimitry Andric // FIXME: Support vector-GEPs.
1814139f7f9bSDimitry Andric assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
1815139f7f9bSDimitry Andric
1816139f7f9bSDimitry Andric // If the base pointer is non-null, we cannot walk to a null address with an
1817139f7f9bSDimitry Andric // inbounds GEP in address space zero.
18183ca95b02SDimitry Andric if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q))
1819139f7f9bSDimitry Andric return true;
1820139f7f9bSDimitry Andric
1821139f7f9bSDimitry Andric // Walk the GEP operands and see if any operand introduces a non-zero offset.
1822139f7f9bSDimitry Andric // If so, then the GEP cannot produce a null pointer, as doing so would
1823139f7f9bSDimitry Andric // inherently violate the inbounds contract within address space zero.
1824139f7f9bSDimitry Andric for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
1825139f7f9bSDimitry Andric GTI != GTE; ++GTI) {
1826139f7f9bSDimitry Andric // Struct types are easy -- they must always be indexed by a constant.
1827d88c1a5aSDimitry Andric if (StructType *STy = GTI.getStructTypeOrNull()) {
1828139f7f9bSDimitry Andric ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
1829139f7f9bSDimitry Andric unsigned ElementIdx = OpC->getZExtValue();
18303ca95b02SDimitry Andric const StructLayout *SL = Q.DL.getStructLayout(STy);
1831139f7f9bSDimitry Andric uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
1832139f7f9bSDimitry Andric if (ElementOffset > 0)
1833139f7f9bSDimitry Andric return true;
1834139f7f9bSDimitry Andric continue;
1835139f7f9bSDimitry Andric }
1836139f7f9bSDimitry Andric
1837139f7f9bSDimitry Andric // If we have a zero-sized type, the index doesn't matter. Keep looping.
18383ca95b02SDimitry Andric if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
1839139f7f9bSDimitry Andric continue;
1840139f7f9bSDimitry Andric
1841139f7f9bSDimitry Andric // Fast path the constant operand case both for efficiency and so we don't
1842139f7f9bSDimitry Andric // increment Depth when just zipping down an all-constant GEP.
1843139f7f9bSDimitry Andric if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
1844139f7f9bSDimitry Andric if (!OpC->isZero())
1845139f7f9bSDimitry Andric return true;
1846139f7f9bSDimitry Andric continue;
1847139f7f9bSDimitry Andric }
1848139f7f9bSDimitry Andric
1849139f7f9bSDimitry Andric // We post-increment Depth here because while isKnownNonZero increments it
1850139f7f9bSDimitry Andric // as well, when we pop back up that increment won't persist. We don't want
1851139f7f9bSDimitry Andric // to recurse 10k times just because we have 10k GEP operands. We don't
1852139f7f9bSDimitry Andric // bail completely out because we want to handle constant GEPs regardless
1853139f7f9bSDimitry Andric // of depth.
1854139f7f9bSDimitry Andric if (Depth++ >= MaxDepth)
1855139f7f9bSDimitry Andric continue;
1856139f7f9bSDimitry Andric
18573ca95b02SDimitry Andric if (isKnownNonZero(GTI.getOperand(), Depth, Q))
1858139f7f9bSDimitry Andric return true;
18593b0f4066SDimitry Andric }
18603b0f4066SDimitry Andric
18612754fe60SDimitry Andric return false;
18622754fe60SDimitry Andric }
18632754fe60SDimitry Andric
isKnownNonNullFromDominatingCondition(const Value * V,const Instruction * CtxI,const DominatorTree * DT)18642cab237bSDimitry Andric static bool isKnownNonNullFromDominatingCondition(const Value *V,
18652cab237bSDimitry Andric const Instruction *CtxI,
18662cab237bSDimitry Andric const DominatorTree *DT) {
18672cab237bSDimitry Andric assert(V->getType()->isPointerTy() && "V must be pointer type");
18682cab237bSDimitry Andric assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull");
18692cab237bSDimitry Andric
18702cab237bSDimitry Andric if (!CtxI || !DT)
18712cab237bSDimitry Andric return false;
18722cab237bSDimitry Andric
18732cab237bSDimitry Andric unsigned NumUsesExplored = 0;
18742cab237bSDimitry Andric for (auto *U : V->users()) {
18752cab237bSDimitry Andric // Avoid massive lists
18762cab237bSDimitry Andric if (NumUsesExplored >= DomConditionsMaxUses)
18772cab237bSDimitry Andric break;
18782cab237bSDimitry Andric NumUsesExplored++;
18792cab237bSDimitry Andric
18802cab237bSDimitry Andric // If the value is used as an argument to a call or invoke, then argument
18812cab237bSDimitry Andric // attributes may provide an answer about null-ness.
18822cab237bSDimitry Andric if (auto CS = ImmutableCallSite(U))
18832cab237bSDimitry Andric if (auto *CalledFunc = CS.getCalledFunction())
18842cab237bSDimitry Andric for (const Argument &Arg : CalledFunc->args())
18852cab237bSDimitry Andric if (CS.getArgOperand(Arg.getArgNo()) == V &&
18862cab237bSDimitry Andric Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
18872cab237bSDimitry Andric return true;
18882cab237bSDimitry Andric
18892cab237bSDimitry Andric // Consider only compare instructions uniquely controlling a branch
18902cab237bSDimitry Andric CmpInst::Predicate Pred;
18912cab237bSDimitry Andric if (!match(const_cast<User *>(U),
18922cab237bSDimitry Andric m_c_ICmp(Pred, m_Specific(V), m_Zero())) ||
18932cab237bSDimitry Andric (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE))
18942cab237bSDimitry Andric continue;
18952cab237bSDimitry Andric
1896b5893f02SDimitry Andric SmallVector<const User *, 4> WorkList;
1897b5893f02SDimitry Andric SmallPtrSet<const User *, 4> Visited;
18982cab237bSDimitry Andric for (auto *CmpU : U->users()) {
1899b5893f02SDimitry Andric assert(WorkList.empty() && "Should be!");
1900b5893f02SDimitry Andric if (Visited.insert(CmpU).second)
1901b5893f02SDimitry Andric WorkList.push_back(CmpU);
1902b5893f02SDimitry Andric
1903b5893f02SDimitry Andric while (!WorkList.empty()) {
1904b5893f02SDimitry Andric auto *Curr = WorkList.pop_back_val();
1905b5893f02SDimitry Andric
1906b5893f02SDimitry Andric // If a user is an AND, add all its users to the work list. We only
1907b5893f02SDimitry Andric // propagate "pred != null" condition through AND because it is only
1908b5893f02SDimitry Andric // correct to assume that all conditions of AND are met in true branch.
1909b5893f02SDimitry Andric // TODO: Support similar logic of OR and EQ predicate?
1910b5893f02SDimitry Andric if (Pred == ICmpInst::ICMP_NE)
1911b5893f02SDimitry Andric if (auto *BO = dyn_cast<BinaryOperator>(Curr))
1912b5893f02SDimitry Andric if (BO->getOpcode() == Instruction::And) {
1913b5893f02SDimitry Andric for (auto *BOU : BO->users())
1914b5893f02SDimitry Andric if (Visited.insert(BOU).second)
1915b5893f02SDimitry Andric WorkList.push_back(BOU);
1916b5893f02SDimitry Andric continue;
1917b5893f02SDimitry Andric }
1918b5893f02SDimitry Andric
1919b5893f02SDimitry Andric if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
19202cab237bSDimitry Andric assert(BI->isConditional() && "uses a comparison!");
19212cab237bSDimitry Andric
19222cab237bSDimitry Andric BasicBlock *NonNullSuccessor =
19232cab237bSDimitry Andric BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0);
19242cab237bSDimitry Andric BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
19252cab237bSDimitry Andric if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
19262cab237bSDimitry Andric return true;
1927b5893f02SDimitry Andric } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) &&
1928b5893f02SDimitry Andric DT->dominates(cast<Instruction>(Curr), CtxI)) {
19292cab237bSDimitry Andric return true;
19302cab237bSDimitry Andric }
19312cab237bSDimitry Andric }
19322cab237bSDimitry Andric }
1933b5893f02SDimitry Andric }
19342cab237bSDimitry Andric
19352cab237bSDimitry Andric return false;
19362cab237bSDimitry Andric }
19372cab237bSDimitry Andric
193839d628a0SDimitry Andric /// Does the 'Range' metadata (which must be a valid MD_range operand list)
193939d628a0SDimitry Andric /// ensure that the value it's attached to is never Value? 'RangeType' is
194039d628a0SDimitry Andric /// is the type of the value described by the range.
rangeMetadataExcludesValue(const MDNode * Ranges,const APInt & Value)1941d88c1a5aSDimitry Andric static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
194239d628a0SDimitry Andric const unsigned NumRanges = Ranges->getNumOperands() / 2;
194339d628a0SDimitry Andric assert(NumRanges >= 1);
194439d628a0SDimitry Andric for (unsigned i = 0; i < NumRanges; ++i) {
194539d628a0SDimitry Andric ConstantInt *Lower =
194639d628a0SDimitry Andric mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
194739d628a0SDimitry Andric ConstantInt *Upper =
194839d628a0SDimitry Andric mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
194939d628a0SDimitry Andric ConstantRange Range(Lower->getValue(), Upper->getValue());
195039d628a0SDimitry Andric if (Range.contains(Value))
195139d628a0SDimitry Andric return false;
195239d628a0SDimitry Andric }
195339d628a0SDimitry Andric return true;
195439d628a0SDimitry Andric }
195539d628a0SDimitry Andric
19567a7e6055SDimitry Andric /// Return true if the given value is known to be non-zero when defined. For
19577a7e6055SDimitry Andric /// vectors, return true if every element is known to be non-zero when
19587a7e6055SDimitry Andric /// defined. For pointers, if the context instruction and dominator tree are
19597a7e6055SDimitry Andric /// specified, perform context-sensitive analysis and return true if the
19607a7e6055SDimitry Andric /// pointer couldn't possibly be null at the specified instruction.
19617a7e6055SDimitry Andric /// Supports values with integer or pointer type and vectors of integers.
isKnownNonZero(const Value * V,unsigned Depth,const Query & Q)1962d88c1a5aSDimitry Andric bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) {
19633ca95b02SDimitry Andric if (auto *C = dyn_cast<Constant>(V)) {
19642754fe60SDimitry Andric if (C->isNullValue())
19652754fe60SDimitry Andric return false;
19662754fe60SDimitry Andric if (isa<ConstantInt>(C))
19672754fe60SDimitry Andric // Must be non-zero due to null test above.
19682754fe60SDimitry Andric return true;
19693ca95b02SDimitry Andric
19703ca95b02SDimitry Andric // For constant vectors, check that all elements are undefined or known
19713ca95b02SDimitry Andric // non-zero to determine that the whole vector is known non-zero.
19723ca95b02SDimitry Andric if (auto *VecTy = dyn_cast<VectorType>(C->getType())) {
19733ca95b02SDimitry Andric for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
19743ca95b02SDimitry Andric Constant *Elt = C->getAggregateElement(i);
19753ca95b02SDimitry Andric if (!Elt || Elt->isNullValue())
19763ca95b02SDimitry Andric return false;
19773ca95b02SDimitry Andric if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
19783ca95b02SDimitry Andric return false;
19793ca95b02SDimitry Andric }
19803ca95b02SDimitry Andric return true;
19813ca95b02SDimitry Andric }
19823ca95b02SDimitry Andric
19832cab237bSDimitry Andric // A global variable in address space 0 is non null unless extern weak
19842cab237bSDimitry Andric // or an absolute symbol reference. Other address spaces may have null as a
19852cab237bSDimitry Andric // valid address for a global, so we can't assume anything.
19862cab237bSDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
19872cab237bSDimitry Andric if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
19882cab237bSDimitry Andric GV->getType()->getAddressSpace() == 0)
19892cab237bSDimitry Andric return true;
19902cab237bSDimitry Andric } else
19912754fe60SDimitry Andric return false;
19922754fe60SDimitry Andric }
19932754fe60SDimitry Andric
19943ca95b02SDimitry Andric if (auto *I = dyn_cast<Instruction>(V)) {
1995b5893f02SDimitry Andric if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) {
199639d628a0SDimitry Andric // If the possible ranges don't contain zero, then the value is
199739d628a0SDimitry Andric // definitely non-zero.
19983ca95b02SDimitry Andric if (auto *Ty = dyn_cast<IntegerType>(V->getType())) {
199939d628a0SDimitry Andric const APInt ZeroValue(Ty->getBitWidth(), 0);
200039d628a0SDimitry Andric if (rangeMetadataExcludesValue(Ranges, ZeroValue))
200139d628a0SDimitry Andric return true;
200239d628a0SDimitry Andric }
200339d628a0SDimitry Andric }
200439d628a0SDimitry Andric }
200539d628a0SDimitry Andric
20064ba319b5SDimitry Andric // Some of the tests below are recursive, so bail out if we hit the limit.
20074ba319b5SDimitry Andric if (Depth++ >= MaxDepth)
20084ba319b5SDimitry Andric return false;
20094ba319b5SDimitry Andric
20102cab237bSDimitry Andric // Check for pointer simplifications.
20112cab237bSDimitry Andric if (V->getType()->isPointerTy()) {
20122cab237bSDimitry Andric // Alloca never returns null, malloc might.
20132cab237bSDimitry Andric if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0)
20142cab237bSDimitry Andric return true;
20152cab237bSDimitry Andric
20162cab237bSDimitry Andric // A byval, inalloca, or nonnull argument is never null.
20172cab237bSDimitry Andric if (const Argument *A = dyn_cast<Argument>(V))
20182cab237bSDimitry Andric if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr())
20192cab237bSDimitry Andric return true;
20202cab237bSDimitry Andric
20212cab237bSDimitry Andric // A Load tagged with nonnull metadata is never null.
20222cab237bSDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(V))
2023b5893f02SDimitry Andric if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
20242cab237bSDimitry Andric return true;
20252cab237bSDimitry Andric
2026b5893f02SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(V)) {
2027b5893f02SDimitry Andric if (Call->isReturnNonNull())
20282cab237bSDimitry Andric return true;
2029b5893f02SDimitry Andric if (const auto *RP = getArgumentAliasingToReturnedPointer(Call))
20304ba319b5SDimitry Andric return isKnownNonZero(RP, Depth, Q);
20314ba319b5SDimitry Andric }
20322cab237bSDimitry Andric }
20332cab237bSDimitry Andric
20342754fe60SDimitry Andric
20352cab237bSDimitry Andric // Check for recursive pointer simplifications.
2036139f7f9bSDimitry Andric if (V->getType()->isPointerTy()) {
20372cab237bSDimitry Andric if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
2038139f7f9bSDimitry Andric return true;
20392cab237bSDimitry Andric
2040d88c1a5aSDimitry Andric if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V))
20413ca95b02SDimitry Andric if (isGEPKnownNonNull(GEP, Depth, Q))
2042139f7f9bSDimitry Andric return true;
2043139f7f9bSDimitry Andric }
2044139f7f9bSDimitry Andric
20453ca95b02SDimitry Andric unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL);
20462754fe60SDimitry Andric
20472754fe60SDimitry Andric // X | Y != 0 if X != 0 or Y != 0.
204891bc56edSDimitry Andric Value *X = nullptr, *Y = nullptr;
20492754fe60SDimitry Andric if (match(V, m_Or(m_Value(X), m_Value(Y))))
20503ca95b02SDimitry Andric return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q);
20512754fe60SDimitry Andric
20522754fe60SDimitry Andric // ext X != 0 if X != 0.
20532754fe60SDimitry Andric if (isa<SExtInst>(V) || isa<ZExtInst>(V))
20543ca95b02SDimitry Andric return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q);
20552754fe60SDimitry Andric
20562754fe60SDimitry Andric // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
20572754fe60SDimitry Andric // if the lowest bit is shifted off the end.
20580f5676f4SDimitry Andric if (match(V, m_Shl(m_Value(X), m_Value(Y)))) {
20593b0f4066SDimitry Andric // shl nuw can't remove any non-zero bits.
2060d88c1a5aSDimitry Andric const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
2061b5893f02SDimitry Andric if (Q.IIQ.hasNoUnsignedWrap(BO))
20623ca95b02SDimitry Andric return isKnownNonZero(X, Depth, Q);
20633b0f4066SDimitry Andric
206451690af2SDimitry Andric KnownBits Known(BitWidth);
206551690af2SDimitry Andric computeKnownBits(X, Known, Depth, Q);
206651690af2SDimitry Andric if (Known.One[0])
20672754fe60SDimitry Andric return true;
20682754fe60SDimitry Andric }
20692754fe60SDimitry Andric // shr X, Y != 0 if X is negative. Note that the value of the shift is not
20702754fe60SDimitry Andric // defined if the sign bit is shifted off the end.
20712754fe60SDimitry Andric else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
20723b0f4066SDimitry Andric // shr exact can only shift out zero bits.
2073d88c1a5aSDimitry Andric const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
20743b0f4066SDimitry Andric if (BO->isExact())
20753ca95b02SDimitry Andric return isKnownNonZero(X, Depth, Q);
20763b0f4066SDimitry Andric
20775517e702SDimitry Andric KnownBits Known = computeKnownBits(X, Depth, Q);
20785517e702SDimitry Andric if (Known.isNegative())
20792754fe60SDimitry Andric return true;
20807d523365SDimitry Andric
20817d523365SDimitry Andric // If the shifter operand is a constant, and all of the bits shifted
20827d523365SDimitry Andric // out are known to be zero, and X is known non-zero then at least one
20837d523365SDimitry Andric // non-zero bit must remain.
20847d523365SDimitry Andric if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) {
20857d523365SDimitry Andric auto ShiftVal = Shift->getLimitedValue(BitWidth - 1);
20867d523365SDimitry Andric // Is there a known one in the portion not shifted out?
20875517e702SDimitry Andric if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal)
20887d523365SDimitry Andric return true;
20897d523365SDimitry Andric // Are all the bits to be shifted out known zero?
20905517e702SDimitry Andric if (Known.countMinTrailingZeros() >= ShiftVal)
20913ca95b02SDimitry Andric return isKnownNonZero(X, Depth, Q);
20927d523365SDimitry Andric }
20932754fe60SDimitry Andric }
20943b0f4066SDimitry Andric // div exact can only produce a zero if the dividend is zero.
2095dff0c46cSDimitry Andric else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
20963ca95b02SDimitry Andric return isKnownNonZero(X, Depth, Q);
20973b0f4066SDimitry Andric }
20982754fe60SDimitry Andric // X + Y.
20992754fe60SDimitry Andric else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
21005517e702SDimitry Andric KnownBits XKnown = computeKnownBits(X, Depth, Q);
21015517e702SDimitry Andric KnownBits YKnown = computeKnownBits(Y, Depth, Q);
21022754fe60SDimitry Andric
21032754fe60SDimitry Andric // If X and Y are both non-negative (as signed values) then their sum is not
21042754fe60SDimitry Andric // zero unless both X and Y are zero.
21055517e702SDimitry Andric if (XKnown.isNonNegative() && YKnown.isNonNegative())
21063ca95b02SDimitry Andric if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q))
21072754fe60SDimitry Andric return true;
21082754fe60SDimitry Andric
21092754fe60SDimitry Andric // If X and Y are both negative (as signed values) then their sum is not
21102754fe60SDimitry Andric // zero unless both X and Y equal INT_MIN.
21115517e702SDimitry Andric if (XKnown.isNegative() && YKnown.isNegative()) {
21122754fe60SDimitry Andric APInt Mask = APInt::getSignedMaxValue(BitWidth);
21132754fe60SDimitry Andric // The sign bit of X is set. If some other bit is set then X is not equal
21142754fe60SDimitry Andric // to INT_MIN.
21155517e702SDimitry Andric if (XKnown.One.intersects(Mask))
21162754fe60SDimitry Andric return true;
21172754fe60SDimitry Andric // The sign bit of Y is set. If some other bit is set then Y is not equal
21182754fe60SDimitry Andric // to INT_MIN.
21195517e702SDimitry Andric if (YKnown.One.intersects(Mask))
21202754fe60SDimitry Andric return true;
21212754fe60SDimitry Andric }
21222754fe60SDimitry Andric
21232754fe60SDimitry Andric // The sum of a non-negative number and a power of two is not zero.
21245517e702SDimitry Andric if (XKnown.isNonNegative() &&
21253ca95b02SDimitry Andric isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
21262754fe60SDimitry Andric return true;
21275517e702SDimitry Andric if (YKnown.isNonNegative() &&
21283ca95b02SDimitry Andric isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2129dff0c46cSDimitry Andric return true;
2130dff0c46cSDimitry Andric }
2131dff0c46cSDimitry Andric // X * Y.
2132dff0c46cSDimitry Andric else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
2133d88c1a5aSDimitry Andric const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
2134dff0c46cSDimitry Andric // If X and Y are non-zero then so is X * Y as long as the multiplication
2135dff0c46cSDimitry Andric // does not overflow.
2136b5893f02SDimitry Andric if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
21373ca95b02SDimitry Andric isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q))
21382754fe60SDimitry Andric return true;
21392754fe60SDimitry Andric }
21402754fe60SDimitry Andric // (C ? X : Y) != 0 if X != 0 and Y != 0.
2141d88c1a5aSDimitry Andric else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
21423ca95b02SDimitry Andric if (isKnownNonZero(SI->getTrueValue(), Depth, Q) &&
21433ca95b02SDimitry Andric isKnownNonZero(SI->getFalseValue(), Depth, Q))
21442754fe60SDimitry Andric return true;
21452754fe60SDimitry Andric }
21467d523365SDimitry Andric // PHI
2147d88c1a5aSDimitry Andric else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
21487d523365SDimitry Andric // Try and detect a recurrence that monotonically increases from a
21497d523365SDimitry Andric // starting value, as these are common as induction variables.
21507d523365SDimitry Andric if (PN->getNumIncomingValues() == 2) {
21517d523365SDimitry Andric Value *Start = PN->getIncomingValue(0);
21527d523365SDimitry Andric Value *Induction = PN->getIncomingValue(1);
21537d523365SDimitry Andric if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start))
21547d523365SDimitry Andric std::swap(Start, Induction);
21557d523365SDimitry Andric if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) {
21567d523365SDimitry Andric if (!C->isZero() && !C->isNegative()) {
21577d523365SDimitry Andric ConstantInt *X;
2158b5893f02SDimitry Andric if (Q.IIQ.UseInstrInfo &&
2159b5893f02SDimitry Andric (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) ||
21607d523365SDimitry Andric match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) &&
21617d523365SDimitry Andric !X->isNegative())
21627d523365SDimitry Andric return true;
21637d523365SDimitry Andric }
21647d523365SDimitry Andric }
21657d523365SDimitry Andric }
21663ca95b02SDimitry Andric // Check if all incoming values are non-zero constant.
21672cab237bSDimitry Andric bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) {
2168c4394386SDimitry Andric return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero();
21693ca95b02SDimitry Andric });
21703ca95b02SDimitry Andric if (AllNonZeroConstants)
21713ca95b02SDimitry Andric return true;
21727d523365SDimitry Andric }
21732754fe60SDimitry Andric
217451690af2SDimitry Andric KnownBits Known(BitWidth);
217551690af2SDimitry Andric computeKnownBits(V, Known, Depth, Q);
217651690af2SDimitry Andric return Known.One != 0;
21772754fe60SDimitry Andric }
21782754fe60SDimitry Andric
21797d523365SDimitry Andric /// Return true if V2 == V1 + X, where X is known non-zero.
isAddOfNonZero(const Value * V1,const Value * V2,const Query & Q)2180d88c1a5aSDimitry Andric static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) {
2181d88c1a5aSDimitry Andric const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
21827d523365SDimitry Andric if (!BO || BO->getOpcode() != Instruction::Add)
21837d523365SDimitry Andric return false;
21847d523365SDimitry Andric Value *Op = nullptr;
21857d523365SDimitry Andric if (V2 == BO->getOperand(0))
21867d523365SDimitry Andric Op = BO->getOperand(1);
21877d523365SDimitry Andric else if (V2 == BO->getOperand(1))
21887d523365SDimitry Andric Op = BO->getOperand(0);
21897d523365SDimitry Andric else
21907d523365SDimitry Andric return false;
21913ca95b02SDimitry Andric return isKnownNonZero(Op, 0, Q);
21927d523365SDimitry Andric }
21937d523365SDimitry Andric
21947d523365SDimitry Andric /// Return true if it is known that V1 != V2.
isKnownNonEqual(const Value * V1,const Value * V2,const Query & Q)2195d88c1a5aSDimitry Andric static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) {
2196db17bf38SDimitry Andric if (V1 == V2)
21977d523365SDimitry Andric return false;
21987d523365SDimitry Andric if (V1->getType() != V2->getType())
21997d523365SDimitry Andric // We can't look through casts yet.
22007d523365SDimitry Andric return false;
22013ca95b02SDimitry Andric if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q))
22027d523365SDimitry Andric return true;
22037d523365SDimitry Andric
2204db17bf38SDimitry Andric if (V1->getType()->isIntOrIntVectorTy()) {
22057d523365SDimitry Andric // Are any known bits in V1 contradictory to known bits in V2? If V1
22067d523365SDimitry Andric // has a known zero where V2 has a known one, they must not be equal.
2207db17bf38SDimitry Andric KnownBits Known1 = computeKnownBits(V1, 0, Q);
2208db17bf38SDimitry Andric KnownBits Known2 = computeKnownBits(V2, 0, Q);
22097d523365SDimitry Andric
2210db17bf38SDimitry Andric if (Known1.Zero.intersects(Known2.One) ||
2211db17bf38SDimitry Andric Known2.Zero.intersects(Known1.One))
22127d523365SDimitry Andric return true;
22137d523365SDimitry Andric }
22147d523365SDimitry Andric return false;
22157d523365SDimitry Andric }
22167d523365SDimitry Andric
221739d628a0SDimitry Andric /// Return true if 'V & Mask' is known to be zero. We use this predicate to
221839d628a0SDimitry Andric /// simplify operations downstream. Mask is known to be zero for bits that V
221939d628a0SDimitry Andric /// cannot have.
2220f22ef01cSRoman Divacky ///
2221f22ef01cSRoman Divacky /// This function is defined on values with integer type, values with pointer
2222ff0cc061SDimitry Andric /// type, and vectors of integers. In the case
2223f22ef01cSRoman Divacky /// where V is a vector, the mask, known zero, and known one values are the
2224f22ef01cSRoman Divacky /// same width as the vector element, and the bit is set only if it is true
2225f22ef01cSRoman Divacky /// for all of the elements in the vector.
MaskedValueIsZero(const Value * V,const APInt & Mask,unsigned Depth,const Query & Q)2226d88c1a5aSDimitry Andric bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
22273ca95b02SDimitry Andric const Query &Q) {
222851690af2SDimitry Andric KnownBits Known(Mask.getBitWidth());
222951690af2SDimitry Andric computeKnownBits(V, Known, Depth, Q);
223051690af2SDimitry Andric return Mask.isSubsetOf(Known.Zero);
2231f22ef01cSRoman Divacky }
2232f22ef01cSRoman Divacky
2233b5893f02SDimitry Andric // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
2234b5893f02SDimitry Andric // Returns the input and lower/upper bounds.
isSignedMinMaxClamp(const Value * Select,const Value * & In,const APInt * & CLow,const APInt * & CHigh)2235b5893f02SDimitry Andric static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
2236b5893f02SDimitry Andric const APInt *&CLow, const APInt *&CHigh) {
2237b5893f02SDimitry Andric assert(isa<Operator>(Select) &&
2238b5893f02SDimitry Andric cast<Operator>(Select)->getOpcode() == Instruction::Select &&
2239b5893f02SDimitry Andric "Input should be a Select!");
2240b5893f02SDimitry Andric
2241b5893f02SDimitry Andric const Value *LHS, *RHS, *LHS2, *RHS2;
2242b5893f02SDimitry Andric SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor;
2243b5893f02SDimitry Andric if (SPF != SPF_SMAX && SPF != SPF_SMIN)
2244b5893f02SDimitry Andric return false;
2245b5893f02SDimitry Andric
2246b5893f02SDimitry Andric if (!match(RHS, m_APInt(CLow)))
2247b5893f02SDimitry Andric return false;
2248b5893f02SDimitry Andric
2249b5893f02SDimitry Andric SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor;
2250b5893f02SDimitry Andric if (getInverseMinMaxFlavor(SPF) != SPF2)
2251b5893f02SDimitry Andric return false;
2252b5893f02SDimitry Andric
2253b5893f02SDimitry Andric if (!match(RHS2, m_APInt(CHigh)))
2254b5893f02SDimitry Andric return false;
2255b5893f02SDimitry Andric
2256b5893f02SDimitry Andric if (SPF == SPF_SMIN)
2257b5893f02SDimitry Andric std::swap(CLow, CHigh);
2258b5893f02SDimitry Andric
2259b5893f02SDimitry Andric In = LHS2;
2260b5893f02SDimitry Andric return CLow->sle(*CHigh);
2261b5893f02SDimitry Andric }
2262b5893f02SDimitry Andric
22633ca95b02SDimitry Andric /// For vector constants, loop over the elements and find the constant with the
22643ca95b02SDimitry Andric /// minimum number of sign bits. Return 0 if the value is not a vector constant
22653ca95b02SDimitry Andric /// or if any element was not analyzed; otherwise, return the count for the
22663ca95b02SDimitry Andric /// element with the minimum number of sign bits.
computeNumSignBitsVectorConstant(const Value * V,unsigned TyBits)2267d88c1a5aSDimitry Andric static unsigned computeNumSignBitsVectorConstant(const Value *V,
2268d88c1a5aSDimitry Andric unsigned TyBits) {
2269d88c1a5aSDimitry Andric const auto *CV = dyn_cast<Constant>(V);
22703ca95b02SDimitry Andric if (!CV || !CV->getType()->isVectorTy())
22713ca95b02SDimitry Andric return 0;
2272f22ef01cSRoman Divacky
22733ca95b02SDimitry Andric unsigned MinSignBits = TyBits;
22743ca95b02SDimitry Andric unsigned NumElts = CV->getType()->getVectorNumElements();
22753ca95b02SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
22763ca95b02SDimitry Andric // If we find a non-ConstantInt, bail out.
22773ca95b02SDimitry Andric auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
22783ca95b02SDimitry Andric if (!Elt)
22793ca95b02SDimitry Andric return 0;
22803ca95b02SDimitry Andric
22812cab237bSDimitry Andric MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
22823ca95b02SDimitry Andric }
22833ca95b02SDimitry Andric
22843ca95b02SDimitry Andric return MinSignBits;
22853ca95b02SDimitry Andric }
2286f22ef01cSRoman Divacky
22877a7e6055SDimitry Andric static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
22887a7e6055SDimitry Andric const Query &Q);
22897a7e6055SDimitry Andric
ComputeNumSignBits(const Value * V,unsigned Depth,const Query & Q)22907a7e6055SDimitry Andric static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
22917a7e6055SDimitry Andric const Query &Q) {
22927a7e6055SDimitry Andric unsigned Result = ComputeNumSignBitsImpl(V, Depth, Q);
22937a7e6055SDimitry Andric assert(Result > 0 && "At least one sign bit needs to be present!");
22947a7e6055SDimitry Andric return Result;
22957a7e6055SDimitry Andric }
22967a7e6055SDimitry Andric
229739d628a0SDimitry Andric /// Return the number of times the sign bit of the register is replicated into
229839d628a0SDimitry Andric /// the other bits. We know that at least 1 bit is always equal to the sign bit
229939d628a0SDimitry Andric /// (itself), but other cases can give us information. For example, immediately
230039d628a0SDimitry Andric /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
23013ca95b02SDimitry Andric /// other, so we return 3. For vectors, return the number of sign bits for the
23024ba319b5SDimitry Andric /// vector element with the minimum number of known sign bits.
ComputeNumSignBitsImpl(const Value * V,unsigned Depth,const Query & Q)23037a7e6055SDimitry Andric static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
23047a7e6055SDimitry Andric const Query &Q) {
23052cab237bSDimitry Andric assert(Depth <= MaxDepth && "Limit Search Depth");
23067a7e6055SDimitry Andric
23077a7e6055SDimitry Andric // We return the minimum number of sign bits that are guaranteed to be present
23087a7e6055SDimitry Andric // in V, so for undef we have to conservatively return 1. We don't have the
23097a7e6055SDimitry Andric // same behavior for poison though -- that's a FIXME today.
23107a7e6055SDimitry Andric
23114ba319b5SDimitry Andric Type *ScalarTy = V->getType()->getScalarType();
23124ba319b5SDimitry Andric unsigned TyBits = ScalarTy->isPointerTy() ?
23134ba319b5SDimitry Andric Q.DL.getIndexTypeSizeInBits(ScalarTy) :
23144ba319b5SDimitry Andric Q.DL.getTypeSizeInBits(ScalarTy);
23154ba319b5SDimitry Andric
2316f22ef01cSRoman Divacky unsigned Tmp, Tmp2;
2317f22ef01cSRoman Divacky unsigned FirstAnswer = 1;
2318f22ef01cSRoman Divacky
231991bc56edSDimitry Andric // Note that ConstantInt is handled by the general computeKnownBits case
2320f22ef01cSRoman Divacky // below.
2321f22ef01cSRoman Divacky
2322d88c1a5aSDimitry Andric if (Depth == MaxDepth)
2323f22ef01cSRoman Divacky return 1; // Limit search depth.
2324f22ef01cSRoman Divacky
2325d88c1a5aSDimitry Andric const Operator *U = dyn_cast<Operator>(V);
2326f22ef01cSRoman Divacky switch (Operator::getOpcode(V)) {
2327f22ef01cSRoman Divacky default: break;
2328f22ef01cSRoman Divacky case Instruction::SExt:
2329f22ef01cSRoman Divacky Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
23303ca95b02SDimitry Andric return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
2331ff0cc061SDimitry Andric
2332ff0cc061SDimitry Andric case Instruction::SDiv: {
2333ff0cc061SDimitry Andric const APInt *Denominator;
2334ff0cc061SDimitry Andric // sdiv X, C -> adds log(C) sign bits.
2335ff0cc061SDimitry Andric if (match(U->getOperand(1), m_APInt(Denominator))) {
2336ff0cc061SDimitry Andric
2337ff0cc061SDimitry Andric // Ignore non-positive denominator.
2338ff0cc061SDimitry Andric if (!Denominator->isStrictlyPositive())
2339ff0cc061SDimitry Andric break;
2340ff0cc061SDimitry Andric
2341ff0cc061SDimitry Andric // Calculate the incoming numerator bits.
23423ca95b02SDimitry Andric unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2343ff0cc061SDimitry Andric
2344ff0cc061SDimitry Andric // Add floor(log(C)) bits to the numerator bits.
2345ff0cc061SDimitry Andric return std::min(TyBits, NumBits + Denominator->logBase2());
2346ff0cc061SDimitry Andric }
2347ff0cc061SDimitry Andric break;
2348ff0cc061SDimitry Andric }
2349ff0cc061SDimitry Andric
2350ff0cc061SDimitry Andric case Instruction::SRem: {
2351ff0cc061SDimitry Andric const APInt *Denominator;
2352ff0cc061SDimitry Andric // srem X, C -> we know that the result is within [-C+1,C) when C is a
2353ff0cc061SDimitry Andric // positive constant. This let us put a lower bound on the number of sign
2354ff0cc061SDimitry Andric // bits.
2355ff0cc061SDimitry Andric if (match(U->getOperand(1), m_APInt(Denominator))) {
2356ff0cc061SDimitry Andric
2357ff0cc061SDimitry Andric // Ignore non-positive denominator.
2358ff0cc061SDimitry Andric if (!Denominator->isStrictlyPositive())
2359ff0cc061SDimitry Andric break;
2360ff0cc061SDimitry Andric
2361ff0cc061SDimitry Andric // Calculate the incoming numerator bits. SRem by a positive constant
2362ff0cc061SDimitry Andric // can't lower the number of sign bits.
2363ff0cc061SDimitry Andric unsigned NumrBits =
23643ca95b02SDimitry Andric ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2365ff0cc061SDimitry Andric
2366ff0cc061SDimitry Andric // Calculate the leading sign bit constraints by examining the
2367ff0cc061SDimitry Andric // denominator. Given that the denominator is positive, there are two
2368ff0cc061SDimitry Andric // cases:
2369ff0cc061SDimitry Andric //
2370ff0cc061SDimitry Andric // 1. the numerator is positive. The result range is [0,C) and [0,C) u<
2371ff0cc061SDimitry Andric // (1 << ceilLogBase2(C)).
2372ff0cc061SDimitry Andric //
2373ff0cc061SDimitry Andric // 2. the numerator is negative. Then the result range is (-C,0] and
2374ff0cc061SDimitry Andric // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
2375ff0cc061SDimitry Andric //
2376ff0cc061SDimitry Andric // Thus a lower bound on the number of sign bits is `TyBits -
2377ff0cc061SDimitry Andric // ceilLogBase2(C)`.
2378ff0cc061SDimitry Andric
2379ff0cc061SDimitry Andric unsigned ResBits = TyBits - Denominator->ceilLogBase2();
2380ff0cc061SDimitry Andric return std::max(NumrBits, ResBits);
2381ff0cc061SDimitry Andric }
2382ff0cc061SDimitry Andric break;
2383ff0cc061SDimitry Andric }
2384f22ef01cSRoman Divacky
2385dff0c46cSDimitry Andric case Instruction::AShr: {
23863ca95b02SDimitry Andric Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2387dff0c46cSDimitry Andric // ashr X, C -> adds C sign bits. Vectors too.
2388dff0c46cSDimitry Andric const APInt *ShAmt;
2389dff0c46cSDimitry Andric if (match(U->getOperand(1), m_APInt(ShAmt))) {
239030785c0eSDimitry Andric if (ShAmt->uge(TyBits))
23917a7e6055SDimitry Andric break; // Bad shift.
239230785c0eSDimitry Andric unsigned ShAmtLimited = ShAmt->getZExtValue();
23937a7e6055SDimitry Andric Tmp += ShAmtLimited;
2394f22ef01cSRoman Divacky if (Tmp > TyBits) Tmp = TyBits;
2395f22ef01cSRoman Divacky }
2396f22ef01cSRoman Divacky return Tmp;
2397dff0c46cSDimitry Andric }
2398dff0c46cSDimitry Andric case Instruction::Shl: {
2399dff0c46cSDimitry Andric const APInt *ShAmt;
2400dff0c46cSDimitry Andric if (match(U->getOperand(1), m_APInt(ShAmt))) {
2401f22ef01cSRoman Divacky // shl destroys sign bits.
24023ca95b02SDimitry Andric Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
240330785c0eSDimitry Andric if (ShAmt->uge(TyBits) || // Bad shift.
240430785c0eSDimitry Andric ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
2405dff0c46cSDimitry Andric Tmp2 = ShAmt->getZExtValue();
2406dff0c46cSDimitry Andric return Tmp - Tmp2;
2407f22ef01cSRoman Divacky }
2408f22ef01cSRoman Divacky break;
2409dff0c46cSDimitry Andric }
2410f22ef01cSRoman Divacky case Instruction::And:
2411f22ef01cSRoman Divacky case Instruction::Or:
2412f22ef01cSRoman Divacky case Instruction::Xor: // NOT is handled here.
2413f22ef01cSRoman Divacky // Logical binary ops preserve the number of sign bits at the worst.
24143ca95b02SDimitry Andric Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2415f22ef01cSRoman Divacky if (Tmp != 1) {
24163ca95b02SDimitry Andric Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
2417f22ef01cSRoman Divacky FirstAnswer = std::min(Tmp, Tmp2);
2418f22ef01cSRoman Divacky // We computed what we know about the sign bits as our first
2419f22ef01cSRoman Divacky // answer. Now proceed to the generic code that uses
242091bc56edSDimitry Andric // computeKnownBits, and pick whichever answer is better.
2421f22ef01cSRoman Divacky }
2422f22ef01cSRoman Divacky break;
2423f22ef01cSRoman Divacky
2424b5893f02SDimitry Andric case Instruction::Select: {
2425b5893f02SDimitry Andric // If we have a clamp pattern, we know that the number of sign bits will be
2426b5893f02SDimitry Andric // the minimum of the clamp min/max range.
2427b5893f02SDimitry Andric const Value *X;
2428b5893f02SDimitry Andric const APInt *CLow, *CHigh;
2429b5893f02SDimitry Andric if (isSignedMinMaxClamp(U, X, CLow, CHigh))
2430b5893f02SDimitry Andric return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
2431b5893f02SDimitry Andric
24323ca95b02SDimitry Andric Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
24334ba319b5SDimitry Andric if (Tmp == 1) break;
24343ca95b02SDimitry Andric Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
2435f22ef01cSRoman Divacky return std::min(Tmp, Tmp2);
2436b5893f02SDimitry Andric }
2437f22ef01cSRoman Divacky
2438f22ef01cSRoman Divacky case Instruction::Add:
2439f22ef01cSRoman Divacky // Add can have at most one carry bit. Thus we know that the output
2440f22ef01cSRoman Divacky // is, at worst, one more bit than the inputs.
24413ca95b02SDimitry Andric Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
24424ba319b5SDimitry Andric if (Tmp == 1) break;
2443f22ef01cSRoman Divacky
2444f22ef01cSRoman Divacky // Special case decrementing a value (ADD X, -1):
244539d628a0SDimitry Andric if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
2446f22ef01cSRoman Divacky if (CRHS->isAllOnesValue()) {
244751690af2SDimitry Andric KnownBits Known(TyBits);
244851690af2SDimitry Andric computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
2449f22ef01cSRoman Divacky
2450f22ef01cSRoman Divacky // If the input is known to be 0 or 1, the output is 0/-1, which is all
2451f22ef01cSRoman Divacky // sign bits set.
245251690af2SDimitry Andric if ((Known.Zero | 1).isAllOnesValue())
2453f22ef01cSRoman Divacky return TyBits;
2454f22ef01cSRoman Divacky
2455f22ef01cSRoman Divacky // If we are subtracting one from a positive number, there is no carry
2456f22ef01cSRoman Divacky // out of the result.
2457f37b6182SDimitry Andric if (Known.isNonNegative())
2458f22ef01cSRoman Divacky return Tmp;
2459f22ef01cSRoman Divacky }
2460f22ef01cSRoman Divacky
24613ca95b02SDimitry Andric Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
24624ba319b5SDimitry Andric if (Tmp2 == 1) break;
2463f22ef01cSRoman Divacky return std::min(Tmp, Tmp2)-1;
2464f22ef01cSRoman Divacky
2465f22ef01cSRoman Divacky case Instruction::Sub:
24663ca95b02SDimitry Andric Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
24674ba319b5SDimitry Andric if (Tmp2 == 1) break;
2468f22ef01cSRoman Divacky
2469f22ef01cSRoman Divacky // Handle NEG.
247039d628a0SDimitry Andric if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
2471f22ef01cSRoman Divacky if (CLHS->isNullValue()) {
247251690af2SDimitry Andric KnownBits Known(TyBits);
247351690af2SDimitry Andric computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
2474f22ef01cSRoman Divacky // If the input is known to be 0 or 1, the output is 0/-1, which is all
2475f22ef01cSRoman Divacky // sign bits set.
247651690af2SDimitry Andric if ((Known.Zero | 1).isAllOnesValue())
2477f22ef01cSRoman Divacky return TyBits;
2478f22ef01cSRoman Divacky
2479f22ef01cSRoman Divacky // If the input is known to be positive (the sign bit is known clear),
2480f22ef01cSRoman Divacky // the output of the NEG has the same number of sign bits as the input.
2481f37b6182SDimitry Andric if (Known.isNonNegative())
2482f22ef01cSRoman Divacky return Tmp2;
2483f22ef01cSRoman Divacky
2484f22ef01cSRoman Divacky // Otherwise, we treat this like a SUB.
2485f22ef01cSRoman Divacky }
2486f22ef01cSRoman Divacky
2487f22ef01cSRoman Divacky // Sub can have at most one carry bit. Thus we know that the output
2488f22ef01cSRoman Divacky // is, at worst, one more bit than the inputs.
24893ca95b02SDimitry Andric Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
24904ba319b5SDimitry Andric if (Tmp == 1) break;
2491f22ef01cSRoman Divacky return std::min(Tmp, Tmp2)-1;
2492f22ef01cSRoman Divacky
24932cab237bSDimitry Andric case Instruction::Mul: {
24942cab237bSDimitry Andric // The output of the Mul can be at most twice the valid bits in the inputs.
24952cab237bSDimitry Andric unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
24964ba319b5SDimitry Andric if (SignBitsOp0 == 1) break;
24972cab237bSDimitry Andric unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
24984ba319b5SDimitry Andric if (SignBitsOp1 == 1) break;
24992cab237bSDimitry Andric unsigned OutValidBits =
25002cab237bSDimitry Andric (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
25012cab237bSDimitry Andric return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
25022cab237bSDimitry Andric }
25032cab237bSDimitry Andric
2504f22ef01cSRoman Divacky case Instruction::PHI: {
2505d88c1a5aSDimitry Andric const PHINode *PN = cast<PHINode>(U);
250639d628a0SDimitry Andric unsigned NumIncomingValues = PN->getNumIncomingValues();
2507f22ef01cSRoman Divacky // Don't analyze large in-degree PHIs.
250839d628a0SDimitry Andric if (NumIncomingValues > 4) break;
250939d628a0SDimitry Andric // Unreachable blocks may have zero-operand PHI nodes.
251039d628a0SDimitry Andric if (NumIncomingValues == 0) break;
2511f22ef01cSRoman Divacky
2512f22ef01cSRoman Divacky // Take the minimum of all incoming values. This can't infinitely loop
2513f22ef01cSRoman Divacky // because of our depth threshold.
25143ca95b02SDimitry Andric Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q);
251539d628a0SDimitry Andric for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
2516f22ef01cSRoman Divacky if (Tmp == 1) return Tmp;
2517ff0cc061SDimitry Andric Tmp = std::min(
25183ca95b02SDimitry Andric Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q));
2519f22ef01cSRoman Divacky }
2520f22ef01cSRoman Divacky return Tmp;
2521f22ef01cSRoman Divacky }
2522f22ef01cSRoman Divacky
2523f22ef01cSRoman Divacky case Instruction::Trunc:
2524f22ef01cSRoman Divacky // FIXME: it's tricky to do anything useful for this, but it is an important
2525f22ef01cSRoman Divacky // case for targets like X86.
2526f22ef01cSRoman Divacky break;
2527d88c1a5aSDimitry Andric
2528d88c1a5aSDimitry Andric case Instruction::ExtractElement:
2529d88c1a5aSDimitry Andric // Look through extract element. At the moment we keep this simple and skip
2530d88c1a5aSDimitry Andric // tracking the specific element. But at least we might find information
2531d88c1a5aSDimitry Andric // valid for all elements of the vector (for example if vector is sign
2532d88c1a5aSDimitry Andric // extended, shifted, etc).
2533d88c1a5aSDimitry Andric return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
2534b5893f02SDimitry Andric
2535b5893f02SDimitry Andric case Instruction::ShuffleVector: {
2536b5893f02SDimitry Andric // TODO: This is copied almost directly from the SelectionDAG version of
2537b5893f02SDimitry Andric // ComputeNumSignBits. It would be better if we could share common
2538b5893f02SDimitry Andric // code. If not, make sure that changes are translated to the DAG.
2539b5893f02SDimitry Andric
2540b5893f02SDimitry Andric // Collect the minimum number of sign bits that are shared by every vector
2541b5893f02SDimitry Andric // element referenced by the shuffle.
2542b5893f02SDimitry Andric auto *Shuf = cast<ShuffleVectorInst>(U);
2543b5893f02SDimitry Andric int NumElts = Shuf->getOperand(0)->getType()->getVectorNumElements();
2544b5893f02SDimitry Andric int NumMaskElts = Shuf->getMask()->getType()->getVectorNumElements();
2545b5893f02SDimitry Andric APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
2546b5893f02SDimitry Andric for (int i = 0; i != NumMaskElts; ++i) {
2547b5893f02SDimitry Andric int M = Shuf->getMaskValue(i);
2548b5893f02SDimitry Andric assert(M < NumElts * 2 && "Invalid shuffle mask constant");
2549b5893f02SDimitry Andric // For undef elements, we don't know anything about the common state of
2550b5893f02SDimitry Andric // the shuffle result.
2551b5893f02SDimitry Andric if (M == -1)
2552b5893f02SDimitry Andric return 1;
2553b5893f02SDimitry Andric if (M < NumElts)
2554b5893f02SDimitry Andric DemandedLHS.setBit(M % NumElts);
2555b5893f02SDimitry Andric else
2556b5893f02SDimitry Andric DemandedRHS.setBit(M % NumElts);
2557b5893f02SDimitry Andric }
2558b5893f02SDimitry Andric Tmp = std::numeric_limits<unsigned>::max();
2559b5893f02SDimitry Andric if (!!DemandedLHS)
2560b5893f02SDimitry Andric Tmp = ComputeNumSignBits(Shuf->getOperand(0), Depth + 1, Q);
2561b5893f02SDimitry Andric if (!!DemandedRHS) {
2562b5893f02SDimitry Andric Tmp2 = ComputeNumSignBits(Shuf->getOperand(1), Depth + 1, Q);
2563b5893f02SDimitry Andric Tmp = std::min(Tmp, Tmp2);
2564b5893f02SDimitry Andric }
2565b5893f02SDimitry Andric // If we don't know anything, early out and try computeKnownBits fall-back.
2566b5893f02SDimitry Andric if (Tmp == 1)
2567b5893f02SDimitry Andric break;
2568b5893f02SDimitry Andric assert(Tmp <= V->getType()->getScalarSizeInBits() &&
2569b5893f02SDimitry Andric "Failed to determine minimum sign bits");
2570b5893f02SDimitry Andric return Tmp;
2571b5893f02SDimitry Andric }
2572f22ef01cSRoman Divacky }
2573f22ef01cSRoman Divacky
2574f22ef01cSRoman Divacky // Finally, if we can prove that the top bits of the result are 0's or 1's,
2575f22ef01cSRoman Divacky // use this information.
25763ca95b02SDimitry Andric
25773ca95b02SDimitry Andric // If we can examine all elements of a vector constant successfully, we're
25783ca95b02SDimitry Andric // done (we can't do any better than that). If not, keep trying.
25793ca95b02SDimitry Andric if (unsigned VecSignBits = computeNumSignBitsVectorConstant(V, TyBits))
25803ca95b02SDimitry Andric return VecSignBits;
25813ca95b02SDimitry Andric
258251690af2SDimitry Andric KnownBits Known(TyBits);
258351690af2SDimitry Andric computeKnownBits(V, Known, Depth, Q);
2584f22ef01cSRoman Divacky
25853ca95b02SDimitry Andric // If we know that the sign bit is either zero or one, determine the number of
25863ca95b02SDimitry Andric // identical bits in the top of the input value.
25875517e702SDimitry Andric return std::max(FirstAnswer, Known.countMinSignBits());
2588f22ef01cSRoman Divacky }
2589f22ef01cSRoman Divacky
259039d628a0SDimitry Andric /// This function computes the integer multiple of Base that equals V.
259139d628a0SDimitry Andric /// If successful, it returns true and returns the multiple in
2592f22ef01cSRoman Divacky /// Multiple. If unsuccessful, it returns false. It looks
2593f22ef01cSRoman Divacky /// through SExt instructions only if LookThroughSExt is true.
ComputeMultiple(Value * V,unsigned Base,Value * & Multiple,bool LookThroughSExt,unsigned Depth)2594f22ef01cSRoman Divacky bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
2595f22ef01cSRoman Divacky bool LookThroughSExt, unsigned Depth) {
2596f22ef01cSRoman Divacky const unsigned MaxDepth = 6;
2597f22ef01cSRoman Divacky
2598f22ef01cSRoman Divacky assert(V && "No Value?");
2599f22ef01cSRoman Divacky assert(Depth <= MaxDepth && "Limit Search Depth");
2600f22ef01cSRoman Divacky assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
2601f22ef01cSRoman Divacky
26026122f3e6SDimitry Andric Type *T = V->getType();
2603f22ef01cSRoman Divacky
2604f22ef01cSRoman Divacky ConstantInt *CI = dyn_cast<ConstantInt>(V);
2605f22ef01cSRoman Divacky
2606f22ef01cSRoman Divacky if (Base == 0)
2607f22ef01cSRoman Divacky return false;
2608f22ef01cSRoman Divacky
2609f22ef01cSRoman Divacky if (Base == 1) {
2610f22ef01cSRoman Divacky Multiple = V;
2611f22ef01cSRoman Divacky return true;
2612f22ef01cSRoman Divacky }
2613f22ef01cSRoman Divacky
2614f22ef01cSRoman Divacky ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
2615f22ef01cSRoman Divacky Constant *BaseVal = ConstantInt::get(T, Base);
2616f22ef01cSRoman Divacky if (CO && CO == BaseVal) {
2617f22ef01cSRoman Divacky // Multiple is 1.
2618f22ef01cSRoman Divacky Multiple = ConstantInt::get(T, 1);
2619f22ef01cSRoman Divacky return true;
2620f22ef01cSRoman Divacky }
2621f22ef01cSRoman Divacky
2622f22ef01cSRoman Divacky if (CI && CI->getZExtValue() % Base == 0) {
2623f22ef01cSRoman Divacky Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
2624f22ef01cSRoman Divacky return true;
2625f22ef01cSRoman Divacky }
2626f22ef01cSRoman Divacky
2627f22ef01cSRoman Divacky if (Depth == MaxDepth) return false; // Limit search depth.
2628f22ef01cSRoman Divacky
2629f22ef01cSRoman Divacky Operator *I = dyn_cast<Operator>(V);
2630f22ef01cSRoman Divacky if (!I) return false;
2631f22ef01cSRoman Divacky
2632f22ef01cSRoman Divacky switch (I->getOpcode()) {
2633f22ef01cSRoman Divacky default: break;
2634f22ef01cSRoman Divacky case Instruction::SExt:
2635f22ef01cSRoman Divacky if (!LookThroughSExt) return false;
2636f22ef01cSRoman Divacky // otherwise fall through to ZExt
2637f9448bf3SDimitry Andric LLVM_FALLTHROUGH;
2638f22ef01cSRoman Divacky case Instruction::ZExt:
2639f22ef01cSRoman Divacky return ComputeMultiple(I->getOperand(0), Base, Multiple,
2640f22ef01cSRoman Divacky LookThroughSExt, Depth+1);
2641f22ef01cSRoman Divacky case Instruction::Shl:
2642f22ef01cSRoman Divacky case Instruction::Mul: {
2643f22ef01cSRoman Divacky Value *Op0 = I->getOperand(0);
2644f22ef01cSRoman Divacky Value *Op1 = I->getOperand(1);
2645f22ef01cSRoman Divacky
2646f22ef01cSRoman Divacky if (I->getOpcode() == Instruction::Shl) {
2647f22ef01cSRoman Divacky ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
2648f22ef01cSRoman Divacky if (!Op1CI) return false;
2649f22ef01cSRoman Divacky // Turn Op0 << Op1 into Op0 * 2^Op1
2650f22ef01cSRoman Divacky APInt Op1Int = Op1CI->getValue();
2651f22ef01cSRoman Divacky uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
26522754fe60SDimitry Andric APInt API(Op1Int.getBitWidth(), 0);
26532754fe60SDimitry Andric API.setBit(BitToSet);
26542754fe60SDimitry Andric Op1 = ConstantInt::get(V->getContext(), API);
2655f22ef01cSRoman Divacky }
2656f22ef01cSRoman Divacky
265791bc56edSDimitry Andric Value *Mul0 = nullptr;
2658e580952dSDimitry Andric if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
2659e580952dSDimitry Andric if (Constant *Op1C = dyn_cast<Constant>(Op1))
2660e580952dSDimitry Andric if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
2661e580952dSDimitry Andric if (Op1C->getType()->getPrimitiveSizeInBits() <
2662e580952dSDimitry Andric MulC->getType()->getPrimitiveSizeInBits())
2663e580952dSDimitry Andric Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
2664e580952dSDimitry Andric if (Op1C->getType()->getPrimitiveSizeInBits() >
2665e580952dSDimitry Andric MulC->getType()->getPrimitiveSizeInBits())
2666e580952dSDimitry Andric MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
2667f22ef01cSRoman Divacky
2668f22ef01cSRoman Divacky // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
2669e580952dSDimitry Andric Multiple = ConstantExpr::getMul(MulC, Op1C);
2670f22ef01cSRoman Divacky return true;
2671f22ef01cSRoman Divacky }
2672f22ef01cSRoman Divacky
2673f22ef01cSRoman Divacky if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
2674f22ef01cSRoman Divacky if (Mul0CI->getValue() == 1) {
2675f22ef01cSRoman Divacky // V == Base * Op1, so return Op1
2676f22ef01cSRoman Divacky Multiple = Op1;
2677f22ef01cSRoman Divacky return true;
2678f22ef01cSRoman Divacky }
2679f22ef01cSRoman Divacky }
2680f22ef01cSRoman Divacky
268191bc56edSDimitry Andric Value *Mul1 = nullptr;
2682e580952dSDimitry Andric if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
2683e580952dSDimitry Andric if (Constant *Op0C = dyn_cast<Constant>(Op0))
2684e580952dSDimitry Andric if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
2685e580952dSDimitry Andric if (Op0C->getType()->getPrimitiveSizeInBits() <
2686e580952dSDimitry Andric MulC->getType()->getPrimitiveSizeInBits())
2687e580952dSDimitry Andric Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
2688e580952dSDimitry Andric if (Op0C->getType()->getPrimitiveSizeInBits() >
2689e580952dSDimitry Andric MulC->getType()->getPrimitiveSizeInBits())
2690e580952dSDimitry Andric MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
2691e580952dSDimitry Andric
2692f22ef01cSRoman Divacky // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
2693e580952dSDimitry Andric Multiple = ConstantExpr::getMul(MulC, Op0C);
2694f22ef01cSRoman Divacky return true;
2695f22ef01cSRoman Divacky }
2696f22ef01cSRoman Divacky
2697f22ef01cSRoman Divacky if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
2698f22ef01cSRoman Divacky if (Mul1CI->getValue() == 1) {
2699f22ef01cSRoman Divacky // V == Base * Op0, so return Op0
2700f22ef01cSRoman Divacky Multiple = Op0;
2701f22ef01cSRoman Divacky return true;
2702f22ef01cSRoman Divacky }
2703f22ef01cSRoman Divacky }
2704f22ef01cSRoman Divacky }
2705f22ef01cSRoman Divacky }
2706f22ef01cSRoman Divacky
2707f22ef01cSRoman Divacky // We could not determine if V is a multiple of Base.
2708f22ef01cSRoman Divacky return false;
2709f22ef01cSRoman Divacky }
2710f22ef01cSRoman Divacky
getIntrinsicForCallSite(ImmutableCallSite ICS,const TargetLibraryInfo * TLI)27113ca95b02SDimitry Andric Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS,
27123ca95b02SDimitry Andric const TargetLibraryInfo *TLI) {
27133ca95b02SDimitry Andric const Function *F = ICS.getCalledFunction();
27143ca95b02SDimitry Andric if (!F)
27153ca95b02SDimitry Andric return Intrinsic::not_intrinsic;
27163ca95b02SDimitry Andric
27173ca95b02SDimitry Andric if (F->isIntrinsic())
27183ca95b02SDimitry Andric return F->getIntrinsicID();
27193ca95b02SDimitry Andric
27203ca95b02SDimitry Andric if (!TLI)
27213ca95b02SDimitry Andric return Intrinsic::not_intrinsic;
27223ca95b02SDimitry Andric
27237a7e6055SDimitry Andric LibFunc Func;
27243ca95b02SDimitry Andric // We're going to make assumptions on the semantics of the functions, check
27253ca95b02SDimitry Andric // that the target knows that it's available in this environment and it does
27263ca95b02SDimitry Andric // not have local linkage.
27273ca95b02SDimitry Andric if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func))
27283ca95b02SDimitry Andric return Intrinsic::not_intrinsic;
27293ca95b02SDimitry Andric
27303ca95b02SDimitry Andric if (!ICS.onlyReadsMemory())
27313ca95b02SDimitry Andric return Intrinsic::not_intrinsic;
27323ca95b02SDimitry Andric
27333ca95b02SDimitry Andric // Otherwise check if we have a call to a function that can be turned into a
27343ca95b02SDimitry Andric // vector intrinsic.
27353ca95b02SDimitry Andric switch (Func) {
27363ca95b02SDimitry Andric default:
27373ca95b02SDimitry Andric break;
27387a7e6055SDimitry Andric case LibFunc_sin:
27397a7e6055SDimitry Andric case LibFunc_sinf:
27407a7e6055SDimitry Andric case LibFunc_sinl:
27413ca95b02SDimitry Andric return Intrinsic::sin;
27427a7e6055SDimitry Andric case LibFunc_cos:
27437a7e6055SDimitry Andric case LibFunc_cosf:
27447a7e6055SDimitry Andric case LibFunc_cosl:
27453ca95b02SDimitry Andric return Intrinsic::cos;
27467a7e6055SDimitry Andric case LibFunc_exp:
27477a7e6055SDimitry Andric case LibFunc_expf:
27487a7e6055SDimitry Andric case LibFunc_expl:
27493ca95b02SDimitry Andric return Intrinsic::exp;
27507a7e6055SDimitry Andric case LibFunc_exp2:
27517a7e6055SDimitry Andric case LibFunc_exp2f:
27527a7e6055SDimitry Andric case LibFunc_exp2l:
27533ca95b02SDimitry Andric return Intrinsic::exp2;
27547a7e6055SDimitry Andric case LibFunc_log:
27557a7e6055SDimitry Andric case LibFunc_logf:
27567a7e6055SDimitry Andric case LibFunc_logl:
27573ca95b02SDimitry Andric return Intrinsic::log;
27587a7e6055SDimitry Andric case LibFunc_log10:
27597a7e6055SDimitry Andric case LibFunc_log10f:
27607a7e6055SDimitry Andric case LibFunc_log10l:
27613ca95b02SDimitry Andric return Intrinsic::log10;
27627a7e6055SDimitry Andric case LibFunc_log2:
27637a7e6055SDimitry Andric case LibFunc_log2f:
27647a7e6055SDimitry Andric case LibFunc_log2l:
27653ca95b02SDimitry Andric return Intrinsic::log2;
27667a7e6055SDimitry Andric case LibFunc_fabs:
27677a7e6055SDimitry Andric case LibFunc_fabsf:
27687a7e6055SDimitry Andric case LibFunc_fabsl:
27693ca95b02SDimitry Andric return Intrinsic::fabs;
27707a7e6055SDimitry Andric case LibFunc_fmin:
27717a7e6055SDimitry Andric case LibFunc_fminf:
27727a7e6055SDimitry Andric case LibFunc_fminl:
27733ca95b02SDimitry Andric return Intrinsic::minnum;
27747a7e6055SDimitry Andric case LibFunc_fmax:
27757a7e6055SDimitry Andric case LibFunc_fmaxf:
27767a7e6055SDimitry Andric case LibFunc_fmaxl:
27773ca95b02SDimitry Andric return Intrinsic::maxnum;
27787a7e6055SDimitry Andric case LibFunc_copysign:
27797a7e6055SDimitry Andric case LibFunc_copysignf:
27807a7e6055SDimitry Andric case LibFunc_copysignl:
27813ca95b02SDimitry Andric return Intrinsic::copysign;
27827a7e6055SDimitry Andric case LibFunc_floor:
27837a7e6055SDimitry Andric case LibFunc_floorf:
27847a7e6055SDimitry Andric case LibFunc_floorl:
27853ca95b02SDimitry Andric return Intrinsic::floor;
27867a7e6055SDimitry Andric case LibFunc_ceil:
27877a7e6055SDimitry Andric case LibFunc_ceilf:
27887a7e6055SDimitry Andric case LibFunc_ceill:
27893ca95b02SDimitry Andric return Intrinsic::ceil;
27907a7e6055SDimitry Andric case LibFunc_trunc:
27917a7e6055SDimitry Andric case LibFunc_truncf:
27927a7e6055SDimitry Andric case LibFunc_truncl:
27933ca95b02SDimitry Andric return Intrinsic::trunc;
27947a7e6055SDimitry Andric case LibFunc_rint:
27957a7e6055SDimitry Andric case LibFunc_rintf:
27967a7e6055SDimitry Andric case LibFunc_rintl:
27973ca95b02SDimitry Andric return Intrinsic::rint;
27987a7e6055SDimitry Andric case LibFunc_nearbyint:
27997a7e6055SDimitry Andric case LibFunc_nearbyintf:
28007a7e6055SDimitry Andric case LibFunc_nearbyintl:
28013ca95b02SDimitry Andric return Intrinsic::nearbyint;
28027a7e6055SDimitry Andric case LibFunc_round:
28037a7e6055SDimitry Andric case LibFunc_roundf:
28047a7e6055SDimitry Andric case LibFunc_roundl:
28053ca95b02SDimitry Andric return Intrinsic::round;
28067a7e6055SDimitry Andric case LibFunc_pow:
28077a7e6055SDimitry Andric case LibFunc_powf:
28087a7e6055SDimitry Andric case LibFunc_powl:
28093ca95b02SDimitry Andric return Intrinsic::pow;
28107a7e6055SDimitry Andric case LibFunc_sqrt:
28117a7e6055SDimitry Andric case LibFunc_sqrtf:
28127a7e6055SDimitry Andric case LibFunc_sqrtl:
28133ca95b02SDimitry Andric return Intrinsic::sqrt;
28143ca95b02SDimitry Andric }
28153ca95b02SDimitry Andric
28163ca95b02SDimitry Andric return Intrinsic::not_intrinsic;
28173ca95b02SDimitry Andric }
28183ca95b02SDimitry Andric
281939d628a0SDimitry Andric /// Return true if we can prove that the specified FP value is never equal to
282039d628a0SDimitry Andric /// -0.0.
2821f22ef01cSRoman Divacky ///
2822f22ef01cSRoman Divacky /// NOTE: this function will need to be revisited when we support non-default
2823f22ef01cSRoman Divacky /// rounding modes!
CannotBeNegativeZero(const Value * V,const TargetLibraryInfo * TLI,unsigned Depth)28243ca95b02SDimitry Andric bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
28253ca95b02SDimitry Andric unsigned Depth) {
28262cab237bSDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(V))
2827f22ef01cSRoman Divacky return !CFP->getValueAPF().isNegZero();
2828f22ef01cSRoman Divacky
28292cab237bSDimitry Andric // Limit search depth.
2830d88c1a5aSDimitry Andric if (Depth == MaxDepth)
28312cab237bSDimitry Andric return false;
2832f22ef01cSRoman Divacky
28332cab237bSDimitry Andric auto *Op = dyn_cast<Operator>(V);
28342cab237bSDimitry Andric if (!Op)
28352cab237bSDimitry Andric return false;
2836f22ef01cSRoman Divacky
28372cab237bSDimitry Andric // Check if the nsz fast-math flag is set.
28382cab237bSDimitry Andric if (auto *FPO = dyn_cast<FPMathOperator>(Op))
2839139f7f9bSDimitry Andric if (FPO->hasNoSignedZeros())
2840139f7f9bSDimitry Andric return true;
2841139f7f9bSDimitry Andric
28422cab237bSDimitry Andric // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
28434ba319b5SDimitry Andric if (match(Op, m_FAdd(m_Value(), m_PosZeroFP())))
2844f22ef01cSRoman Divacky return true;
2845f22ef01cSRoman Divacky
2846f22ef01cSRoman Divacky // sitofp and uitofp turn into +0.0 for zero.
28472cab237bSDimitry Andric if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op))
2848f22ef01cSRoman Divacky return true;
2849f22ef01cSRoman Divacky
28502cab237bSDimitry Andric if (auto *Call = dyn_cast<CallInst>(Op)) {
28512cab237bSDimitry Andric Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI);
28523ca95b02SDimitry Andric switch (IID) {
28533ca95b02SDimitry Andric default:
28543ca95b02SDimitry Andric break;
2855f22ef01cSRoman Divacky // sqrt(-0.0) = -0.0, no other negative results are possible.
28563ca95b02SDimitry Andric case Intrinsic::sqrt:
2857b5893f02SDimitry Andric case Intrinsic::canonicalize:
28582cab237bSDimitry Andric return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1);
28593ca95b02SDimitry Andric // fabs(x) != -0.0
28603ca95b02SDimitry Andric case Intrinsic::fabs:
28613ca95b02SDimitry Andric return true;
2862f22ef01cSRoman Divacky }
2863f22ef01cSRoman Divacky }
2864f22ef01cSRoman Divacky
2865f22ef01cSRoman Divacky return false;
2866f22ef01cSRoman Divacky }
2867f22ef01cSRoman Divacky
2868f1a29dd3SDimitry Andric /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a
2869f1a29dd3SDimitry Andric /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign
2870f1a29dd3SDimitry Andric /// bit despite comparing equal.
cannotBeOrderedLessThanZeroImpl(const Value * V,const TargetLibraryInfo * TLI,bool SignBitOnly,unsigned Depth)2871f1a29dd3SDimitry Andric static bool cannotBeOrderedLessThanZeroImpl(const Value *V,
28723ca95b02SDimitry Andric const TargetLibraryInfo *TLI,
2873f1a29dd3SDimitry Andric bool SignBitOnly,
28743ca95b02SDimitry Andric unsigned Depth) {
28757a7e6055SDimitry Andric // TODO: This function does not do the right thing when SignBitOnly is true
28767a7e6055SDimitry Andric // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform
28777a7e6055SDimitry Andric // which flips the sign bits of NaNs. See
28787a7e6055SDimitry Andric // https://llvm.org/bugs/show_bug.cgi?id=31702.
28797a7e6055SDimitry Andric
2880f1a29dd3SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
2881f1a29dd3SDimitry Andric return !CFP->getValueAPF().isNegative() ||
2882f1a29dd3SDimitry Andric (!SignBitOnly && CFP->getValueAPF().isZero());
2883f1a29dd3SDimitry Andric }
2884ff0cc061SDimitry Andric
28854ba319b5SDimitry Andric // Handle vector of constants.
28864ba319b5SDimitry Andric if (auto *CV = dyn_cast<Constant>(V)) {
28874ba319b5SDimitry Andric if (CV->getType()->isVectorTy()) {
28884ba319b5SDimitry Andric unsigned NumElts = CV->getType()->getVectorNumElements();
28894ba319b5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
28904ba319b5SDimitry Andric auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
28914ba319b5SDimitry Andric if (!CFP)
28924ba319b5SDimitry Andric return false;
28934ba319b5SDimitry Andric if (CFP->getValueAPF().isNegative() &&
28944ba319b5SDimitry Andric (SignBitOnly || !CFP->getValueAPF().isZero()))
28954ba319b5SDimitry Andric return false;
28964ba319b5SDimitry Andric }
28974ba319b5SDimitry Andric
28984ba319b5SDimitry Andric // All non-negative ConstantFPs.
28994ba319b5SDimitry Andric return true;
29004ba319b5SDimitry Andric }
29014ba319b5SDimitry Andric }
29024ba319b5SDimitry Andric
2903d88c1a5aSDimitry Andric if (Depth == MaxDepth)
2904ff0cc061SDimitry Andric return false; // Limit search depth.
2905ff0cc061SDimitry Andric
2906ff0cc061SDimitry Andric const Operator *I = dyn_cast<Operator>(V);
2907f1a29dd3SDimitry Andric if (!I)
2908f1a29dd3SDimitry Andric return false;
2909ff0cc061SDimitry Andric
2910ff0cc061SDimitry Andric switch (I->getOpcode()) {
2911f1a29dd3SDimitry Andric default:
2912f1a29dd3SDimitry Andric break;
2913444ed5c5SDimitry Andric // Unsigned integers are always nonnegative.
2914444ed5c5SDimitry Andric case Instruction::UIToFP:
2915444ed5c5SDimitry Andric return true;
2916ff0cc061SDimitry Andric case Instruction::FMul:
2917ff0cc061SDimitry Andric // x*x is always non-negative or a NaN.
2918f1a29dd3SDimitry Andric if (I->getOperand(0) == I->getOperand(1) &&
2919f1a29dd3SDimitry Andric (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
2920ff0cc061SDimitry Andric return true;
2921f1a29dd3SDimitry Andric
2922d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
2923ff0cc061SDimitry Andric case Instruction::FAdd:
2924ff0cc061SDimitry Andric case Instruction::FDiv:
2925ff0cc061SDimitry Andric case Instruction::FRem:
2926f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2927f1a29dd3SDimitry Andric Depth + 1) &&
2928f1a29dd3SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2929f1a29dd3SDimitry Andric Depth + 1);
2930444ed5c5SDimitry Andric case Instruction::Select:
2931f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2932f1a29dd3SDimitry Andric Depth + 1) &&
2933f1a29dd3SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
2934f1a29dd3SDimitry Andric Depth + 1);
2935ff0cc061SDimitry Andric case Instruction::FPExt:
2936ff0cc061SDimitry Andric case Instruction::FPTrunc:
2937ff0cc061SDimitry Andric // Widening/narrowing never change sign.
2938f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2939f1a29dd3SDimitry Andric Depth + 1);
29404ba319b5SDimitry Andric case Instruction::ExtractElement:
29414ba319b5SDimitry Andric // Look through extract element. At the moment we keep this simple and skip
29424ba319b5SDimitry Andric // tracking the specific element. But at least we might find information
29434ba319b5SDimitry Andric // valid for all elements of the vector.
29444ba319b5SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
29454ba319b5SDimitry Andric Depth + 1);
2946ff0cc061SDimitry Andric case Instruction::Call:
29477a7e6055SDimitry Andric const auto *CI = cast<CallInst>(I);
29487a7e6055SDimitry Andric Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI);
29493ca95b02SDimitry Andric switch (IID) {
29503ca95b02SDimitry Andric default:
29513ca95b02SDimitry Andric break;
2952444ed5c5SDimitry Andric case Intrinsic::maxnum:
2953b5893f02SDimitry Andric return (isKnownNeverNaN(I->getOperand(0), TLI) &&
29544ba319b5SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI,
29554ba319b5SDimitry Andric SignBitOnly, Depth + 1)) ||
2956b5893f02SDimitry Andric (isKnownNeverNaN(I->getOperand(1), TLI) &&
29574ba319b5SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI,
29584ba319b5SDimitry Andric SignBitOnly, Depth + 1));
29594ba319b5SDimitry Andric
2960b5893f02SDimitry Andric case Intrinsic::maximum:
2961b5893f02SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2962b5893f02SDimitry Andric Depth + 1) ||
2963b5893f02SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2964b5893f02SDimitry Andric Depth + 1);
2965444ed5c5SDimitry Andric case Intrinsic::minnum:
2966b5893f02SDimitry Andric case Intrinsic::minimum:
2967f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
2968f1a29dd3SDimitry Andric Depth + 1) &&
2969f1a29dd3SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
2970f1a29dd3SDimitry Andric Depth + 1);
2971ff0cc061SDimitry Andric case Intrinsic::exp:
2972ff0cc061SDimitry Andric case Intrinsic::exp2:
2973ff0cc061SDimitry Andric case Intrinsic::fabs:
2974ff0cc061SDimitry Andric return true;
29757a7e6055SDimitry Andric
29767a7e6055SDimitry Andric case Intrinsic::sqrt:
29777a7e6055SDimitry Andric // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0.
29787a7e6055SDimitry Andric if (!SignBitOnly)
29797a7e6055SDimitry Andric return true;
29807a7e6055SDimitry Andric return CI->hasNoNaNs() && (CI->hasNoSignedZeros() ||
29817a7e6055SDimitry Andric CannotBeNegativeZero(CI->getOperand(0), TLI));
29827a7e6055SDimitry Andric
2983ff0cc061SDimitry Andric case Intrinsic::powi:
29847a7e6055SDimitry Andric if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) {
2985ff0cc061SDimitry Andric // powi(x,n) is non-negative if n is even.
29867a7e6055SDimitry Andric if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0)
2987ff0cc061SDimitry Andric return true;
2988ff0cc061SDimitry Andric }
29897a7e6055SDimitry Andric // TODO: This is not correct. Given that exp is an integer, here are the
29907a7e6055SDimitry Andric // ways that pow can return a negative value:
29917a7e6055SDimitry Andric //
29927a7e6055SDimitry Andric // pow(x, exp) --> negative if exp is odd and x is negative.
29937a7e6055SDimitry Andric // pow(-0, exp) --> -inf if exp is negative odd.
29947a7e6055SDimitry Andric // pow(-0, exp) --> -0 if exp is positive odd.
29957a7e6055SDimitry Andric // pow(-inf, exp) --> -0 if exp is negative odd.
29967a7e6055SDimitry Andric // pow(-inf, exp) --> -inf if exp is positive odd.
29977a7e6055SDimitry Andric //
29987a7e6055SDimitry Andric // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN,
29997a7e6055SDimitry Andric // but we must return false if x == -0. Unfortunately we do not currently
30007a7e6055SDimitry Andric // have a way of expressing this constraint. See details in
30017a7e6055SDimitry Andric // https://llvm.org/bugs/show_bug.cgi?id=31702.
3002f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
3003f1a29dd3SDimitry Andric Depth + 1);
30047a7e6055SDimitry Andric
3005ff0cc061SDimitry Andric case Intrinsic::fma:
3006ff0cc061SDimitry Andric case Intrinsic::fmuladd:
3007ff0cc061SDimitry Andric // x*x+y is non-negative if y is non-negative.
3008ff0cc061SDimitry Andric return I->getOperand(0) == I->getOperand(1) &&
3009f1a29dd3SDimitry Andric (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) &&
3010f1a29dd3SDimitry Andric cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
3011f1a29dd3SDimitry Andric Depth + 1);
3012ff0cc061SDimitry Andric }
3013ff0cc061SDimitry Andric break;
3014ff0cc061SDimitry Andric }
3015ff0cc061SDimitry Andric return false;
3016ff0cc061SDimitry Andric }
3017ff0cc061SDimitry Andric
CannotBeOrderedLessThanZero(const Value * V,const TargetLibraryInfo * TLI)3018f1a29dd3SDimitry Andric bool llvm::CannotBeOrderedLessThanZero(const Value *V,
3019f1a29dd3SDimitry Andric const TargetLibraryInfo *TLI) {
3020f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0);
3021f1a29dd3SDimitry Andric }
3022f1a29dd3SDimitry Andric
SignBitMustBeZero(const Value * V,const TargetLibraryInfo * TLI)3023f1a29dd3SDimitry Andric bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) {
3024f1a29dd3SDimitry Andric return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0);
3025f1a29dd3SDimitry Andric }
3026f1a29dd3SDimitry Andric
isKnownNeverNaN(const Value * V,const TargetLibraryInfo * TLI,unsigned Depth)3027b5893f02SDimitry Andric bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI,
3028b5893f02SDimitry Andric unsigned Depth) {
30292cab237bSDimitry Andric assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type");
30302cab237bSDimitry Andric
30312cab237bSDimitry Andric // If we're told that NaNs won't happen, assume they won't.
30322cab237bSDimitry Andric if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
30332cab237bSDimitry Andric if (FPMathOp->hasNoNaNs())
30342cab237bSDimitry Andric return true;
30352cab237bSDimitry Andric
30362cab237bSDimitry Andric // Handle scalar constants.
30372cab237bSDimitry Andric if (auto *CFP = dyn_cast<ConstantFP>(V))
30382cab237bSDimitry Andric return !CFP->isNaN();
30392cab237bSDimitry Andric
3040b5893f02SDimitry Andric if (Depth == MaxDepth)
3041b5893f02SDimitry Andric return false;
3042b5893f02SDimitry Andric
3043b5893f02SDimitry Andric if (auto *Inst = dyn_cast<Instruction>(V)) {
3044b5893f02SDimitry Andric switch (Inst->getOpcode()) {
3045b5893f02SDimitry Andric case Instruction::FAdd:
3046b5893f02SDimitry Andric case Instruction::FMul:
3047b5893f02SDimitry Andric case Instruction::FSub:
3048b5893f02SDimitry Andric case Instruction::FDiv:
3049b5893f02SDimitry Andric case Instruction::FRem: {
3050b5893f02SDimitry Andric // TODO: Need isKnownNeverInfinity
3051b5893f02SDimitry Andric return false;
3052b5893f02SDimitry Andric }
3053b5893f02SDimitry Andric case Instruction::Select: {
3054b5893f02SDimitry Andric return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
3055b5893f02SDimitry Andric isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1);
3056b5893f02SDimitry Andric }
3057b5893f02SDimitry Andric case Instruction::SIToFP:
3058b5893f02SDimitry Andric case Instruction::UIToFP:
3059b5893f02SDimitry Andric return true;
3060b5893f02SDimitry Andric case Instruction::FPTrunc:
3061b5893f02SDimitry Andric case Instruction::FPExt:
3062b5893f02SDimitry Andric return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1);
3063b5893f02SDimitry Andric default:
3064b5893f02SDimitry Andric break;
3065b5893f02SDimitry Andric }
3066b5893f02SDimitry Andric }
3067b5893f02SDimitry Andric
3068b5893f02SDimitry Andric if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
3069b5893f02SDimitry Andric switch (II->getIntrinsicID()) {
3070b5893f02SDimitry Andric case Intrinsic::canonicalize:
3071b5893f02SDimitry Andric case Intrinsic::fabs:
3072b5893f02SDimitry Andric case Intrinsic::copysign:
3073b5893f02SDimitry Andric case Intrinsic::exp:
3074b5893f02SDimitry Andric case Intrinsic::exp2:
3075b5893f02SDimitry Andric case Intrinsic::floor:
3076b5893f02SDimitry Andric case Intrinsic::ceil:
3077b5893f02SDimitry Andric case Intrinsic::trunc:
3078b5893f02SDimitry Andric case Intrinsic::rint:
3079b5893f02SDimitry Andric case Intrinsic::nearbyint:
3080b5893f02SDimitry Andric case Intrinsic::round:
3081b5893f02SDimitry Andric return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1);
3082b5893f02SDimitry Andric case Intrinsic::sqrt:
3083b5893f02SDimitry Andric return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) &&
3084b5893f02SDimitry Andric CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI);
3085b5893f02SDimitry Andric default:
3086b5893f02SDimitry Andric return false;
3087b5893f02SDimitry Andric }
3088b5893f02SDimitry Andric }
3089b5893f02SDimitry Andric
30902cab237bSDimitry Andric // Bail out for constant expressions, but try to handle vector constants.
30912cab237bSDimitry Andric if (!V->getType()->isVectorTy() || !isa<Constant>(V))
30922cab237bSDimitry Andric return false;
30932cab237bSDimitry Andric
30942cab237bSDimitry Andric // For vectors, verify that each element is not NaN.
30952cab237bSDimitry Andric unsigned NumElts = V->getType()->getVectorNumElements();
30962cab237bSDimitry Andric for (unsigned i = 0; i != NumElts; ++i) {
30972cab237bSDimitry Andric Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
30982cab237bSDimitry Andric if (!Elt)
30992cab237bSDimitry Andric return false;
31002cab237bSDimitry Andric if (isa<UndefValue>(Elt))
31012cab237bSDimitry Andric continue;
31022cab237bSDimitry Andric auto *CElt = dyn_cast<ConstantFP>(Elt);
31032cab237bSDimitry Andric if (!CElt || CElt->isNaN())
31042cab237bSDimitry Andric return false;
31052cab237bSDimitry Andric }
31062cab237bSDimitry Andric // All elements were confirmed not-NaN or undefined.
31072cab237bSDimitry Andric return true;
31082cab237bSDimitry Andric }
31092cab237bSDimitry Andric
isBytewiseValue(Value * V)31102754fe60SDimitry Andric Value *llvm::isBytewiseValue(Value *V) {
3111b5893f02SDimitry Andric
31122754fe60SDimitry Andric // All byte-wide stores are splatable, even of arbitrary variables.
3113b5893f02SDimitry Andric if (V->getType()->isIntegerTy(8))
3114b5893f02SDimitry Andric return V;
31152754fe60SDimitry Andric
3116b5893f02SDimitry Andric LLVMContext &Ctx = V->getContext();
31172754fe60SDimitry Andric
3118b5893f02SDimitry Andric // Undef don't care.
3119b5893f02SDimitry Andric auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
3120b5893f02SDimitry Andric if (isa<UndefValue>(V))
3121b5893f02SDimitry Andric return UndefInt8;
31222754fe60SDimitry Andric
3123b5893f02SDimitry Andric Constant *C = dyn_cast<Constant>(V);
3124b5893f02SDimitry Andric if (!C) {
31252754fe60SDimitry Andric // Conceptually, we could handle things like:
31262754fe60SDimitry Andric // %a = zext i8 %X to i16
31272754fe60SDimitry Andric // %b = shl i16 %a, 8
31282754fe60SDimitry Andric // %c = or i16 %a, %b
31292754fe60SDimitry Andric // but until there is an example that actually needs this, it doesn't seem
31302754fe60SDimitry Andric // worth worrying about.
313191bc56edSDimitry Andric return nullptr;
31322754fe60SDimitry Andric }
31332754fe60SDimitry Andric
3134b5893f02SDimitry Andric // Handle 'null' ConstantArrayZero etc.
3135b5893f02SDimitry Andric if (C->isNullValue())
3136b5893f02SDimitry Andric return Constant::getNullValue(Type::getInt8Ty(Ctx));
3137b5893f02SDimitry Andric
3138b5893f02SDimitry Andric // Constant floating-point values can be handled as integer values if the
3139b5893f02SDimitry Andric // corresponding integer value is "byteable". An important case is 0.0.
3140b5893f02SDimitry Andric if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3141b5893f02SDimitry Andric Type *Ty = nullptr;
3142b5893f02SDimitry Andric if (CFP->getType()->isHalfTy())
3143b5893f02SDimitry Andric Ty = Type::getInt16Ty(Ctx);
3144b5893f02SDimitry Andric else if (CFP->getType()->isFloatTy())
3145b5893f02SDimitry Andric Ty = Type::getInt32Ty(Ctx);
3146b5893f02SDimitry Andric else if (CFP->getType()->isDoubleTy())
3147b5893f02SDimitry Andric Ty = Type::getInt64Ty(Ctx);
3148b5893f02SDimitry Andric // Don't handle long double formats, which have strange constraints.
3149b5893f02SDimitry Andric return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty)) : nullptr;
3150b5893f02SDimitry Andric }
3151b5893f02SDimitry Andric
3152b5893f02SDimitry Andric // We can handle constant integers that are multiple of 8 bits.
3153b5893f02SDimitry Andric if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
3154b5893f02SDimitry Andric if (CI->getBitWidth() % 8 == 0) {
3155b5893f02SDimitry Andric assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
3156b5893f02SDimitry Andric if (!CI->getValue().isSplat(8))
3157b5893f02SDimitry Andric return nullptr;
3158b5893f02SDimitry Andric return ConstantInt::get(Ctx, CI->getValue().trunc(8));
3159b5893f02SDimitry Andric }
3160b5893f02SDimitry Andric }
3161b5893f02SDimitry Andric
3162b5893f02SDimitry Andric auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
3163b5893f02SDimitry Andric if (LHS == RHS)
3164b5893f02SDimitry Andric return LHS;
3165b5893f02SDimitry Andric if (!LHS || !RHS)
3166b5893f02SDimitry Andric return nullptr;
3167b5893f02SDimitry Andric if (LHS == UndefInt8)
3168b5893f02SDimitry Andric return RHS;
3169b5893f02SDimitry Andric if (RHS == UndefInt8)
3170b5893f02SDimitry Andric return LHS;
3171b5893f02SDimitry Andric return nullptr;
3172b5893f02SDimitry Andric };
3173b5893f02SDimitry Andric
3174b5893f02SDimitry Andric if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
3175b5893f02SDimitry Andric Value *Val = UndefInt8;
3176b5893f02SDimitry Andric for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
3177b5893f02SDimitry Andric if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I)))))
3178b5893f02SDimitry Andric return nullptr;
3179b5893f02SDimitry Andric return Val;
3180b5893f02SDimitry Andric }
3181b5893f02SDimitry Andric
3182b5893f02SDimitry Andric if (isa<ConstantVector>(C)) {
3183b5893f02SDimitry Andric Constant *Splat = cast<ConstantVector>(C)->getSplatValue();
3184b5893f02SDimitry Andric return Splat ? isBytewiseValue(Splat) : nullptr;
3185b5893f02SDimitry Andric }
3186b5893f02SDimitry Andric
3187b5893f02SDimitry Andric if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
3188b5893f02SDimitry Andric Value *Val = UndefInt8;
3189b5893f02SDimitry Andric for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
3190b5893f02SDimitry Andric if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I)))))
3191b5893f02SDimitry Andric return nullptr;
3192b5893f02SDimitry Andric return Val;
3193b5893f02SDimitry Andric }
3194b5893f02SDimitry Andric
3195b5893f02SDimitry Andric // Don't try to handle the handful of other constants.
3196b5893f02SDimitry Andric return nullptr;
3197b5893f02SDimitry Andric }
3198b5893f02SDimitry Andric
3199f22ef01cSRoman Divacky // This is the recursive version of BuildSubAggregate. It takes a few different
3200f22ef01cSRoman Divacky // arguments. Idxs is the index within the nested struct From that we are
3201f22ef01cSRoman Divacky // looking at now (which is of type IndexedType). IdxSkip is the number of
3202f22ef01cSRoman Divacky // indices from Idxs that should be left out when inserting into the resulting
3203f22ef01cSRoman Divacky // struct. To is the result struct built so far, new insertvalue instructions
3204f22ef01cSRoman Divacky // build on that.
BuildSubAggregate(Value * From,Value * To,Type * IndexedType,SmallVectorImpl<unsigned> & Idxs,unsigned IdxSkip,Instruction * InsertBefore)32056122f3e6SDimitry Andric static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
3206f785676fSDimitry Andric SmallVectorImpl<unsigned> &Idxs,
3207f22ef01cSRoman Divacky unsigned IdxSkip,
3208f22ef01cSRoman Divacky Instruction *InsertBefore) {
32092cab237bSDimitry Andric StructType *STy = dyn_cast<StructType>(IndexedType);
3210f22ef01cSRoman Divacky if (STy) {
3211f22ef01cSRoman Divacky // Save the original To argument so we can modify it
3212f22ef01cSRoman Divacky Value *OrigTo = To;
3213f22ef01cSRoman Divacky // General case, the type indexed by Idxs is a struct
3214f22ef01cSRoman Divacky for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
3215f22ef01cSRoman Divacky // Process each struct element recursively
3216f22ef01cSRoman Divacky Idxs.push_back(i);
3217f22ef01cSRoman Divacky Value *PrevTo = To;
3218f22ef01cSRoman Divacky To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
3219f22ef01cSRoman Divacky InsertBefore);
3220f22ef01cSRoman Divacky Idxs.pop_back();
3221f22ef01cSRoman Divacky if (!To) {
3222f22ef01cSRoman Divacky // Couldn't find any inserted value for this index? Cleanup
3223f22ef01cSRoman Divacky while (PrevTo != OrigTo) {
3224f22ef01cSRoman Divacky InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
3225f22ef01cSRoman Divacky PrevTo = Del->getAggregateOperand();
3226f22ef01cSRoman Divacky Del->eraseFromParent();
3227f22ef01cSRoman Divacky }
3228f22ef01cSRoman Divacky // Stop processing elements
3229f22ef01cSRoman Divacky break;
3230f22ef01cSRoman Divacky }
3231f22ef01cSRoman Divacky }
32323b0f4066SDimitry Andric // If we successfully found a value for each of our subaggregates
3233f22ef01cSRoman Divacky if (To)
3234f22ef01cSRoman Divacky return To;
3235f22ef01cSRoman Divacky }
3236f22ef01cSRoman Divacky // Base case, the type indexed by SourceIdxs is not a struct, or not all of
3237f22ef01cSRoman Divacky // the struct's elements had a value that was inserted directly. In the latter
3238f22ef01cSRoman Divacky // case, perhaps we can't determine each of the subelements individually, but
3239f22ef01cSRoman Divacky // we might be able to find the complete struct somewhere.
3240f22ef01cSRoman Divacky
3241f22ef01cSRoman Divacky // Find the value that is at that particular spot
324217a519f9SDimitry Andric Value *V = FindInsertedValue(From, Idxs);
3243f22ef01cSRoman Divacky
3244f22ef01cSRoman Divacky if (!V)
324591bc56edSDimitry Andric return nullptr;
3246f22ef01cSRoman Divacky
32474ba319b5SDimitry Andric // Insert the value in the new (sub) aggregate
32482cab237bSDimitry Andric return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
324917a519f9SDimitry Andric "tmp", InsertBefore);
3250f22ef01cSRoman Divacky }
3251f22ef01cSRoman Divacky
3252f22ef01cSRoman Divacky // This helper takes a nested struct and extracts a part of it (which is again a
3253f22ef01cSRoman Divacky // struct) into a new value. For example, given the struct:
3254f22ef01cSRoman Divacky // { a, { b, { c, d }, e } }
3255f22ef01cSRoman Divacky // and the indices "1, 1" this returns
3256f22ef01cSRoman Divacky // { c, d }.
3257f22ef01cSRoman Divacky //
3258f22ef01cSRoman Divacky // It does this by inserting an insertvalue for each element in the resulting
3259f22ef01cSRoman Divacky // struct, as opposed to just inserting a single struct. This will only work if
3260f22ef01cSRoman Divacky // each of the elements of the substruct are known (ie, inserted into From by an
3261f22ef01cSRoman Divacky // insertvalue instruction somewhere).
3262f22ef01cSRoman Divacky //
3263f22ef01cSRoman Divacky // All inserted insertvalue instructions are inserted before InsertBefore
BuildSubAggregate(Value * From,ArrayRef<unsigned> idx_range,Instruction * InsertBefore)326417a519f9SDimitry Andric static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
3265f22ef01cSRoman Divacky Instruction *InsertBefore) {
3266f22ef01cSRoman Divacky assert(InsertBefore && "Must have someplace to insert!");
32676122f3e6SDimitry Andric Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
326817a519f9SDimitry Andric idx_range);
3269f22ef01cSRoman Divacky Value *To = UndefValue::get(IndexedType);
327017a519f9SDimitry Andric SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
3271f22ef01cSRoman Divacky unsigned IdxSkip = Idxs.size();
3272f22ef01cSRoman Divacky
3273f22ef01cSRoman Divacky return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
3274f22ef01cSRoman Divacky }
3275f22ef01cSRoman Divacky
32764ba319b5SDimitry Andric /// Given an aggregate and a sequence of indices, see if the scalar value
32774ba319b5SDimitry Andric /// indexed is already around as a register, for example if it was inserted
32784ba319b5SDimitry Andric /// directly into the aggregate.
3279f22ef01cSRoman Divacky ///
3280f22ef01cSRoman Divacky /// If InsertBefore is not null, this function will duplicate (modified)
3281f22ef01cSRoman Divacky /// insertvalues when a part of a nested struct is extracted.
FindInsertedValue(Value * V,ArrayRef<unsigned> idx_range,Instruction * InsertBefore)328217a519f9SDimitry Andric Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
328317a519f9SDimitry Andric Instruction *InsertBefore) {
3284f22ef01cSRoman Divacky // Nothing to index? Just return V then (this is useful at the end of our
3285dff0c46cSDimitry Andric // recursion).
328617a519f9SDimitry Andric if (idx_range.empty())
3287f22ef01cSRoman Divacky return V;
3288dff0c46cSDimitry Andric // We have indices, so V should have an indexable type.
3289dff0c46cSDimitry Andric assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
3290dff0c46cSDimitry Andric "Not looking at a struct or array?");
3291dff0c46cSDimitry Andric assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
3292dff0c46cSDimitry Andric "Invalid indices for type?");
3293f22ef01cSRoman Divacky
3294dff0c46cSDimitry Andric if (Constant *C = dyn_cast<Constant>(V)) {
3295dff0c46cSDimitry Andric C = C->getAggregateElement(idx_range[0]);
329691bc56edSDimitry Andric if (!C) return nullptr;
3297dff0c46cSDimitry Andric return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
3298dff0c46cSDimitry Andric }
3299dff0c46cSDimitry Andric
3300dff0c46cSDimitry Andric if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
3301f22ef01cSRoman Divacky // Loop the indices for the insertvalue instruction in parallel with the
3302f22ef01cSRoman Divacky // requested indices
330317a519f9SDimitry Andric const unsigned *req_idx = idx_range.begin();
3304f22ef01cSRoman Divacky for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
3305f22ef01cSRoman Divacky i != e; ++i, ++req_idx) {
330617a519f9SDimitry Andric if (req_idx == idx_range.end()) {
3307dff0c46cSDimitry Andric // We can't handle this without inserting insertvalues
3308dff0c46cSDimitry Andric if (!InsertBefore)
330991bc56edSDimitry Andric return nullptr;
3310dff0c46cSDimitry Andric
3311f22ef01cSRoman Divacky // The requested index identifies a part of a nested aggregate. Handle
3312f22ef01cSRoman Divacky // this specially. For example,
3313f22ef01cSRoman Divacky // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
3314f22ef01cSRoman Divacky // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
3315f22ef01cSRoman Divacky // %C = extractvalue {i32, { i32, i32 } } %B, 1
3316f22ef01cSRoman Divacky // This can be changed into
3317f22ef01cSRoman Divacky // %A = insertvalue {i32, i32 } undef, i32 10, 0
3318f22ef01cSRoman Divacky // %C = insertvalue {i32, i32 } %A, i32 11, 1
3319f22ef01cSRoman Divacky // which allows the unused 0,0 element from the nested struct to be
3320f22ef01cSRoman Divacky // removed.
33216122f3e6SDimitry Andric return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
332217a519f9SDimitry Andric InsertBefore);
3323f22ef01cSRoman Divacky }
3324f22ef01cSRoman Divacky
3325f22ef01cSRoman Divacky // This insert value inserts something else than what we are looking for.
33267d523365SDimitry Andric // See if the (aggregate) value inserted into has the value we are
3327f22ef01cSRoman Divacky // looking for, then.
3328f22ef01cSRoman Divacky if (*req_idx != *i)
332917a519f9SDimitry Andric return FindInsertedValue(I->getAggregateOperand(), idx_range,
3330f22ef01cSRoman Divacky InsertBefore);
3331f22ef01cSRoman Divacky }
3332f22ef01cSRoman Divacky // If we end up here, the indices of the insertvalue match with those
3333f22ef01cSRoman Divacky // requested (though possibly only partially). Now we recursively look at
3334f22ef01cSRoman Divacky // the inserted value, passing any remaining indices.
333517a519f9SDimitry Andric return FindInsertedValue(I->getInsertedValueOperand(),
33366122f3e6SDimitry Andric makeArrayRef(req_idx, idx_range.end()),
3337f22ef01cSRoman Divacky InsertBefore);
3338dff0c46cSDimitry Andric }
3339dff0c46cSDimitry Andric
3340dff0c46cSDimitry Andric if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
33417d523365SDimitry Andric // If we're extracting a value from an aggregate that was extracted from
3342f22ef01cSRoman Divacky // something else, we can extract from that something else directly instead.
3343f22ef01cSRoman Divacky // However, we will need to chain I's indices with the requested indices.
3344f22ef01cSRoman Divacky
3345f22ef01cSRoman Divacky // Calculate the number of indices required
334617a519f9SDimitry Andric unsigned size = I->getNumIndices() + idx_range.size();
3347f22ef01cSRoman Divacky // Allocate some space to put the new indices in
3348f22ef01cSRoman Divacky SmallVector<unsigned, 5> Idxs;
3349f22ef01cSRoman Divacky Idxs.reserve(size);
3350f22ef01cSRoman Divacky // Add indices from the extract value instruction
335117a519f9SDimitry Andric Idxs.append(I->idx_begin(), I->idx_end());
3352f22ef01cSRoman Divacky
3353f22ef01cSRoman Divacky // Add requested indices
335417a519f9SDimitry Andric Idxs.append(idx_range.begin(), idx_range.end());
3355f22ef01cSRoman Divacky
3356f22ef01cSRoman Divacky assert(Idxs.size() == size
3357f22ef01cSRoman Divacky && "Number of indices added not correct?");
3358f22ef01cSRoman Divacky
335917a519f9SDimitry Andric return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
3360f22ef01cSRoman Divacky }
3361f22ef01cSRoman Divacky // Otherwise, we don't know (such as, extracting from a function return value
3362f22ef01cSRoman Divacky // or load instruction)
336391bc56edSDimitry Andric return nullptr;
3364f22ef01cSRoman Divacky }
3365f22ef01cSRoman Divacky
336639d628a0SDimitry Andric /// Analyze the specified pointer to see if it can be expressed as a base
336739d628a0SDimitry Andric /// pointer plus a constant offset. Return the base and offset to the caller.
GetPointerBaseWithConstantOffset(Value * Ptr,int64_t & Offset,const DataLayout & DL)33682754fe60SDimitry Andric Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
3369ff0cc061SDimitry Andric const DataLayout &DL) {
33704ba319b5SDimitry Andric unsigned BitWidth = DL.getIndexTypeSizeInBits(Ptr->getType());
3371139f7f9bSDimitry Andric APInt ByteOffset(BitWidth, 0);
33724d0b32cdSDimitry Andric
33734d0b32cdSDimitry Andric // We walk up the defs but use a visited set to handle unreachable code. In
33744d0b32cdSDimitry Andric // that case, we stop after accumulating the cycle once (not that it
33754d0b32cdSDimitry Andric // matters).
33764d0b32cdSDimitry Andric SmallPtrSet<Value *, 16> Visited;
33774d0b32cdSDimitry Andric while (Visited.insert(Ptr).second) {
3378139f7f9bSDimitry Andric if (Ptr->getType()->isVectorTy())
3379139f7f9bSDimitry Andric break;
33802754fe60SDimitry Andric
3381139f7f9bSDimitry Andric if (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
3382d88c1a5aSDimitry Andric // If one of the values we have visited is an addrspacecast, then
3383d88c1a5aSDimitry Andric // the pointer type of this GEP may be different from the type
3384d88c1a5aSDimitry Andric // of the Ptr parameter which was passed to this function. This
3385d88c1a5aSDimitry Andric // means when we construct GEPOffset, we need to use the size
3386d88c1a5aSDimitry Andric // of GEP's pointer type rather than the size of the original
3387d88c1a5aSDimitry Andric // pointer type.
33884ba319b5SDimitry Andric APInt GEPOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
3389ff0cc061SDimitry Andric if (!GEP->accumulateConstantOffset(DL, GEPOffset))
3390139f7f9bSDimitry Andric break;
3391f785676fSDimitry Andric
3392b5893f02SDimitry Andric APInt OrigByteOffset(ByteOffset);
3393b5893f02SDimitry Andric ByteOffset += GEPOffset.sextOrTrunc(ByteOffset.getBitWidth());
3394b5893f02SDimitry Andric if (ByteOffset.getMinSignedBits() > 64) {
3395b5893f02SDimitry Andric // Stop traversal if the pointer offset wouldn't fit into int64_t
3396b5893f02SDimitry Andric // (this should be removed if Offset is updated to an APInt)
3397b5893f02SDimitry Andric ByteOffset = OrigByteOffset;
3398b5893f02SDimitry Andric break;
3399b5893f02SDimitry Andric }
3400f785676fSDimitry Andric
3401139f7f9bSDimitry Andric Ptr = GEP->getPointerOperand();
340291bc56edSDimitry Andric } else if (Operator::getOpcode(Ptr) == Instruction::BitCast ||
340391bc56edSDimitry Andric Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
3404139f7f9bSDimitry Andric Ptr = cast<Operator>(Ptr)->getOperand(0);
3405139f7f9bSDimitry Andric } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
34063ca95b02SDimitry Andric if (GA->isInterposable())
3407139f7f9bSDimitry Andric break;
3408139f7f9bSDimitry Andric Ptr = GA->getAliasee();
34092754fe60SDimitry Andric } else {
3410139f7f9bSDimitry Andric break;
34112754fe60SDimitry Andric }
34122754fe60SDimitry Andric }
3413139f7f9bSDimitry Andric Offset = ByteOffset.getSExtValue();
3414139f7f9bSDimitry Andric return Ptr;
34152754fe60SDimitry Andric }
34162754fe60SDimitry Andric
isGEPBasedOnPointerToString(const GEPOperator * GEP,unsigned CharSize)3417d8866befSDimitry Andric bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
3418d8866befSDimitry Andric unsigned CharSize) {
34193ca95b02SDimitry Andric // Make sure the GEP has exactly three arguments.
34203ca95b02SDimitry Andric if (GEP->getNumOperands() != 3)
34213ca95b02SDimitry Andric return false;
34223ca95b02SDimitry Andric
3423d8866befSDimitry Andric // Make sure the index-ee is a pointer to array of \p CharSize integers.
3424d8866befSDimitry Andric // CharSize.
34253ca95b02SDimitry Andric ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
3426d8866befSDimitry Andric if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
34273ca95b02SDimitry Andric return false;
34283ca95b02SDimitry Andric
34293ca95b02SDimitry Andric // Check to make sure that the first operand of the GEP is an integer and
34303ca95b02SDimitry Andric // has value 0 so that we are sure we're indexing into the initializer.
34313ca95b02SDimitry Andric const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
34323ca95b02SDimitry Andric if (!FirstIdx || !FirstIdx->isZero())
34333ca95b02SDimitry Andric return false;
34343ca95b02SDimitry Andric
34353ca95b02SDimitry Andric return true;
34363ca95b02SDimitry Andric }
34372754fe60SDimitry Andric
getConstantDataArrayInfo(const Value * V,ConstantDataArraySlice & Slice,unsigned ElementSize,uint64_t Offset)3438d8866befSDimitry Andric bool llvm::getConstantDataArrayInfo(const Value *V,
3439d8866befSDimitry Andric ConstantDataArraySlice &Slice,
3440d8866befSDimitry Andric unsigned ElementSize, uint64_t Offset) {
3441dff0c46cSDimitry Andric assert(V);
3442f22ef01cSRoman Divacky
3443dff0c46cSDimitry Andric // Look through bitcast instructions and geps.
3444dff0c46cSDimitry Andric V = V->stripPointerCasts();
3445f22ef01cSRoman Divacky
3446dff0c46cSDimitry Andric // If the value is a GEP instruction or constant expression, treat it as an
3447dff0c46cSDimitry Andric // offset.
3448dff0c46cSDimitry Andric if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
34493ca95b02SDimitry Andric // The GEP operator should be based on a pointer to string constant, and is
34503ca95b02SDimitry Andric // indexing into the string constant.
3451d8866befSDimitry Andric if (!isGEPBasedOnPointerToString(GEP, ElementSize))
3452f22ef01cSRoman Divacky return false;
3453f22ef01cSRoman Divacky
3454f22ef01cSRoman Divacky // If the second index isn't a ConstantInt, then this is a variable index
3455f22ef01cSRoman Divacky // into the array. If this occurs, we can't say anything meaningful about
3456f22ef01cSRoman Divacky // the string.
3457f22ef01cSRoman Divacky uint64_t StartIdx = 0;
3458f22ef01cSRoman Divacky if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
3459f22ef01cSRoman Divacky StartIdx = CI->getZExtValue();
3460f22ef01cSRoman Divacky else
3461f22ef01cSRoman Divacky return false;
3462d8866befSDimitry Andric return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize,
3463d8866befSDimitry Andric StartIdx + Offset);
3464f22ef01cSRoman Divacky }
3465f22ef01cSRoman Divacky
3466f22ef01cSRoman Divacky // The GEP instruction, constant or instruction, must reference a global
3467f22ef01cSRoman Divacky // variable that is a constant and is initialized. The referenced constant
3468f22ef01cSRoman Divacky // initializer is the array that we'll use for optimization.
3469f22ef01cSRoman Divacky const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
3470f22ef01cSRoman Divacky if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
3471f22ef01cSRoman Divacky return false;
3472f22ef01cSRoman Divacky
3473d8866befSDimitry Andric const ConstantDataArray *Array;
3474d8866befSDimitry Andric ArrayType *ArrayTy;
3475dff0c46cSDimitry Andric if (GV->getInitializer()->isNullValue()) {
3476d8866befSDimitry Andric Type *GVTy = GV->getValueType();
3477d8866befSDimitry Andric if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) {
347824d58133SDimitry Andric // A zeroinitializer for the array; there is no ConstantDataArray.
3479d8866befSDimitry Andric Array = nullptr;
3480d8866befSDimitry Andric } else {
3481d8866befSDimitry Andric const DataLayout &DL = GV->getParent()->getDataLayout();
3482d8866befSDimitry Andric uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy);
3483d8866befSDimitry Andric uint64_t Length = SizeInBytes / (ElementSize / 8);
3484d8866befSDimitry Andric if (Length <= Offset)
3485f22ef01cSRoman Divacky return false;
3486f22ef01cSRoman Divacky
3487d8866befSDimitry Andric Slice.Array = nullptr;
3488d8866befSDimitry Andric Slice.Offset = 0;
3489d8866befSDimitry Andric Slice.Length = Length - Offset;
3490d8866befSDimitry Andric return true;
3491d8866befSDimitry Andric }
3492d8866befSDimitry Andric } else {
3493d8866befSDimitry Andric // This must be a ConstantDataArray.
3494d8866befSDimitry Andric Array = dyn_cast<ConstantDataArray>(GV->getInitializer());
3495d8866befSDimitry Andric if (!Array)
3496d8866befSDimitry Andric return false;
3497d8866befSDimitry Andric ArrayTy = Array->getType();
3498d8866befSDimitry Andric }
3499d8866befSDimitry Andric if (!ArrayTy->getElementType()->isIntegerTy(ElementSize))
3500d8866befSDimitry Andric return false;
3501dff0c46cSDimitry Andric
3502d8866befSDimitry Andric uint64_t NumElts = ArrayTy->getArrayNumElements();
3503f22ef01cSRoman Divacky if (Offset > NumElts)
3504f22ef01cSRoman Divacky return false;
3505f22ef01cSRoman Divacky
3506d8866befSDimitry Andric Slice.Array = Array;
3507d8866befSDimitry Andric Slice.Offset = Offset;
3508d8866befSDimitry Andric Slice.Length = NumElts - Offset;
3509d8866befSDimitry Andric return true;
3510d8866befSDimitry Andric }
3511d8866befSDimitry Andric
3512d8866befSDimitry Andric /// This function computes the length of a null-terminated C string pointed to
3513d8866befSDimitry Andric /// by V. If successful, it returns true and returns the string in Str.
3514d8866befSDimitry Andric /// If unsuccessful, it returns false.
getConstantStringInfo(const Value * V,StringRef & Str,uint64_t Offset,bool TrimAtNul)3515d8866befSDimitry Andric bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
3516d8866befSDimitry Andric uint64_t Offset, bool TrimAtNul) {
3517d8866befSDimitry Andric ConstantDataArraySlice Slice;
3518d8866befSDimitry Andric if (!getConstantDataArrayInfo(V, Slice, 8, Offset))
3519d8866befSDimitry Andric return false;
3520d8866befSDimitry Andric
3521d8866befSDimitry Andric if (Slice.Array == nullptr) {
3522d8866befSDimitry Andric if (TrimAtNul) {
3523d8866befSDimitry Andric Str = StringRef();
3524d8866befSDimitry Andric return true;
3525d8866befSDimitry Andric }
3526d8866befSDimitry Andric if (Slice.Length == 1) {
3527d8866befSDimitry Andric Str = StringRef("", 1);
3528d8866befSDimitry Andric return true;
3529d8866befSDimitry Andric }
3530db17bf38SDimitry Andric // We cannot instantiate a StringRef as we do not have an appropriate string
3531d8866befSDimitry Andric // of 0s at hand.
3532d8866befSDimitry Andric return false;
3533d8866befSDimitry Andric }
3534d8866befSDimitry Andric
3535d8866befSDimitry Andric // Start out with the entire array in the StringRef.
3536d8866befSDimitry Andric Str = Slice.Array->getAsString();
3537dff0c46cSDimitry Andric // Skip over 'offset' bytes.
3538d8866befSDimitry Andric Str = Str.substr(Slice.Offset);
3539f22ef01cSRoman Divacky
3540dff0c46cSDimitry Andric if (TrimAtNul) {
3541dff0c46cSDimitry Andric // Trim off the \0 and anything after it. If the array is not nul
3542dff0c46cSDimitry Andric // terminated, we just return the whole end of string. The client may know
3543dff0c46cSDimitry Andric // some other way that the string is length-bound.
3544dff0c46cSDimitry Andric Str = Str.substr(0, Str.find('\0'));
3545dff0c46cSDimitry Andric }
3546f22ef01cSRoman Divacky return true;
3547f22ef01cSRoman Divacky }
3548f22ef01cSRoman Divacky
3549f22ef01cSRoman Divacky // These next two are very similar to the above, but also look through PHI
3550f22ef01cSRoman Divacky // nodes.
3551f22ef01cSRoman Divacky // TODO: See if we can integrate these two together.
3552f22ef01cSRoman Divacky
355339d628a0SDimitry Andric /// If we can compute the length of the string pointed to by
3554f22ef01cSRoman Divacky /// the specified pointer, return 'len+1'. If we can't, return 0.
GetStringLengthH(const Value * V,SmallPtrSetImpl<const PHINode * > & PHIs,unsigned CharSize)3555d88c1a5aSDimitry Andric static uint64_t GetStringLengthH(const Value *V,
3556d8866befSDimitry Andric SmallPtrSetImpl<const PHINode*> &PHIs,
3557d8866befSDimitry Andric unsigned CharSize) {
3558f22ef01cSRoman Divacky // Look through noop bitcast instructions.
3559dff0c46cSDimitry Andric V = V->stripPointerCasts();
3560f22ef01cSRoman Divacky
3561f22ef01cSRoman Divacky // If this is a PHI node, there are two cases: either we have already seen it
3562f22ef01cSRoman Divacky // or we haven't.
3563d88c1a5aSDimitry Andric if (const PHINode *PN = dyn_cast<PHINode>(V)) {
356439d628a0SDimitry Andric if (!PHIs.insert(PN).second)
3565f22ef01cSRoman Divacky return ~0ULL; // already in the set.
3566f22ef01cSRoman Divacky
3567f22ef01cSRoman Divacky // If it was new, see if all the input strings are the same length.
3568f22ef01cSRoman Divacky uint64_t LenSoFar = ~0ULL;
3569ff0cc061SDimitry Andric for (Value *IncValue : PN->incoming_values()) {
3570d8866befSDimitry Andric uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
3571f22ef01cSRoman Divacky if (Len == 0) return 0; // Unknown length -> unknown.
3572f22ef01cSRoman Divacky
3573f22ef01cSRoman Divacky if (Len == ~0ULL) continue;
3574f22ef01cSRoman Divacky
3575f22ef01cSRoman Divacky if (Len != LenSoFar && LenSoFar != ~0ULL)
3576f22ef01cSRoman Divacky return 0; // Disagree -> unknown.
3577f22ef01cSRoman Divacky LenSoFar = Len;
3578f22ef01cSRoman Divacky }
3579f22ef01cSRoman Divacky
3580f22ef01cSRoman Divacky // Success, all agree.
3581f22ef01cSRoman Divacky return LenSoFar;
3582f22ef01cSRoman Divacky }
3583f22ef01cSRoman Divacky
3584f22ef01cSRoman Divacky // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
3585d88c1a5aSDimitry Andric if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
3586d8866befSDimitry Andric uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
3587f22ef01cSRoman Divacky if (Len1 == 0) return 0;
3588d8866befSDimitry Andric uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
3589f22ef01cSRoman Divacky if (Len2 == 0) return 0;
3590f22ef01cSRoman Divacky if (Len1 == ~0ULL) return Len2;
3591f22ef01cSRoman Divacky if (Len2 == ~0ULL) return Len1;
3592f22ef01cSRoman Divacky if (Len1 != Len2) return 0;
3593f22ef01cSRoman Divacky return Len1;
3594f22ef01cSRoman Divacky }
3595f22ef01cSRoman Divacky
3596dff0c46cSDimitry Andric // Otherwise, see if we can read the string.
3597d8866befSDimitry Andric ConstantDataArraySlice Slice;
3598d8866befSDimitry Andric if (!getConstantDataArrayInfo(V, Slice, CharSize))
3599f22ef01cSRoman Divacky return 0;
3600f22ef01cSRoman Divacky
3601d8866befSDimitry Andric if (Slice.Array == nullptr)
3602d8866befSDimitry Andric return 1;
3603d8866befSDimitry Andric
3604d8866befSDimitry Andric // Search for nul characters
3605d8866befSDimitry Andric unsigned NullIndex = 0;
3606d8866befSDimitry Andric for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
3607d8866befSDimitry Andric if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
3608d8866befSDimitry Andric break;
3609d8866befSDimitry Andric }
3610d8866befSDimitry Andric
3611d8866befSDimitry Andric return NullIndex + 1;
3612f22ef01cSRoman Divacky }
3613f22ef01cSRoman Divacky
361439d628a0SDimitry Andric /// If we can compute the length of the string pointed to by
3615f22ef01cSRoman Divacky /// the specified pointer, return 'len+1'. If we can't, return 0.
GetStringLength(const Value * V,unsigned CharSize)3616d8866befSDimitry Andric uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
36174ba319b5SDimitry Andric if (!V->getType()->isPointerTy())
36184ba319b5SDimitry Andric return 0;
3619f22ef01cSRoman Divacky
3620d88c1a5aSDimitry Andric SmallPtrSet<const PHINode*, 32> PHIs;
3621d8866befSDimitry Andric uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
3622f22ef01cSRoman Divacky // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
3623f22ef01cSRoman Divacky // an empty string as a length.
3624f22ef01cSRoman Divacky return Len == ~0ULL ? 1 : Len;
3625f22ef01cSRoman Divacky }
36262754fe60SDimitry Andric
getArgumentAliasingToReturnedPointer(const CallBase * Call)3627b5893f02SDimitry Andric const Value *llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call) {
3628b5893f02SDimitry Andric assert(Call &&
3629b5893f02SDimitry Andric "getArgumentAliasingToReturnedPointer only works on nonnull calls");
3630b5893f02SDimitry Andric if (const Value *RV = Call->getReturnedArgOperand())
36314ba319b5SDimitry Andric return RV;
36324ba319b5SDimitry Andric // This can be used only as a aliasing property.
3633b5893f02SDimitry Andric if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call))
3634b5893f02SDimitry Andric return Call->getArgOperand(0);
36354ba319b5SDimitry Andric return nullptr;
36364ba319b5SDimitry Andric }
36374ba319b5SDimitry Andric
isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase * Call)36384ba319b5SDimitry Andric bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
3639b5893f02SDimitry Andric const CallBase *Call) {
3640b5893f02SDimitry Andric return Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
3641b5893f02SDimitry Andric Call->getIntrinsicID() == Intrinsic::strip_invariant_group;
36424ba319b5SDimitry Andric }
36434ba319b5SDimitry Andric
36444ba319b5SDimitry Andric /// \p PN defines a loop-variant pointer to an object. Check if the
3645ff0cc061SDimitry Andric /// previous iteration of the loop was referring to the same object as \p PN.
isSameUnderlyingObjectInLoop(const PHINode * PN,const LoopInfo * LI)3646d88c1a5aSDimitry Andric static bool isSameUnderlyingObjectInLoop(const PHINode *PN,
3647d88c1a5aSDimitry Andric const LoopInfo *LI) {
3648ff0cc061SDimitry Andric // Find the loop-defined value.
3649ff0cc061SDimitry Andric Loop *L = LI->getLoopFor(PN->getParent());
3650ff0cc061SDimitry Andric if (PN->getNumIncomingValues() != 2)
3651ff0cc061SDimitry Andric return true;
3652ff0cc061SDimitry Andric
3653ff0cc061SDimitry Andric // Find the value from previous iteration.
3654ff0cc061SDimitry Andric auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
3655ff0cc061SDimitry Andric if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
3656ff0cc061SDimitry Andric PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
3657ff0cc061SDimitry Andric if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
3658ff0cc061SDimitry Andric return true;
3659ff0cc061SDimitry Andric
3660ff0cc061SDimitry Andric // If a new pointer is loaded in the loop, the pointer references a different
3661ff0cc061SDimitry Andric // object in every iteration. E.g.:
3662ff0cc061SDimitry Andric // for (i)
3663ff0cc061SDimitry Andric // int *p = a[i];
3664ff0cc061SDimitry Andric // ...
3665ff0cc061SDimitry Andric if (auto *Load = dyn_cast<LoadInst>(PrevValue))
3666ff0cc061SDimitry Andric if (!L->isLoopInvariant(Load->getPointerOperand()))
3667ff0cc061SDimitry Andric return false;
3668ff0cc061SDimitry Andric return true;
3669ff0cc061SDimitry Andric }
3670ff0cc061SDimitry Andric
GetUnderlyingObject(Value * V,const DataLayout & DL,unsigned MaxLookup)3671ff0cc061SDimitry Andric Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
3672ff0cc061SDimitry Andric unsigned MaxLookup) {
36732754fe60SDimitry Andric if (!V->getType()->isPointerTy())
36742754fe60SDimitry Andric return V;
36752754fe60SDimitry Andric for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
36762754fe60SDimitry Andric if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
36772754fe60SDimitry Andric V = GEP->getPointerOperand();
367891bc56edSDimitry Andric } else if (Operator::getOpcode(V) == Instruction::BitCast ||
367991bc56edSDimitry Andric Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
36802754fe60SDimitry Andric V = cast<Operator>(V)->getOperand(0);
36812754fe60SDimitry Andric } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
36823ca95b02SDimitry Andric if (GA->isInterposable())
36832754fe60SDimitry Andric return V;
36842754fe60SDimitry Andric V = GA->getAliasee();
36857a7e6055SDimitry Andric } else if (isa<AllocaInst>(V)) {
36867a7e6055SDimitry Andric // An alloca can't be further simplified.
36877a7e6055SDimitry Andric return V;
36882754fe60SDimitry Andric } else {
3689b5893f02SDimitry Andric if (auto *Call = dyn_cast<CallBase>(V)) {
36904ba319b5SDimitry Andric // CaptureTracking can know about special capturing properties of some
36914ba319b5SDimitry Andric // intrinsics like launder.invariant.group, that can't be expressed with
36924ba319b5SDimitry Andric // the attributes, but have properties like returning aliasing pointer.
36934ba319b5SDimitry Andric // Because some analysis may assume that nocaptured pointer is not
36944ba319b5SDimitry Andric // returned from some special intrinsic (because function would have to
36954ba319b5SDimitry Andric // be marked with returns attribute), it is crucial to use this function
36964ba319b5SDimitry Andric // because it should be in sync with CaptureTracking. Not using it may
36974ba319b5SDimitry Andric // cause weird miscompilations where 2 aliasing pointers are assumed to
36984ba319b5SDimitry Andric // noalias.
3699b5893f02SDimitry Andric if (auto *RP = getArgumentAliasingToReturnedPointer(Call)) {
37004ba319b5SDimitry Andric V = RP;
37013ca95b02SDimitry Andric continue;
37023ca95b02SDimitry Andric }
37034ba319b5SDimitry Andric }
37043ca95b02SDimitry Andric
37052754fe60SDimitry Andric // See if InstructionSimplify knows any relevant tricks.
37062754fe60SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V))
370739d628a0SDimitry Andric // TODO: Acquire a DominatorTree and AssumptionCache and use them.
3708f37b6182SDimitry Andric if (Value *Simplified = SimplifyInstruction(I, {DL, I})) {
37092754fe60SDimitry Andric V = Simplified;
37102754fe60SDimitry Andric continue;
37112754fe60SDimitry Andric }
37122754fe60SDimitry Andric
37132754fe60SDimitry Andric return V;
37142754fe60SDimitry Andric }
37152754fe60SDimitry Andric assert(V->getType()->isPointerTy() && "Unexpected operand type!");
37162754fe60SDimitry Andric }
37172754fe60SDimitry Andric return V;
37182754fe60SDimitry Andric }
371917a519f9SDimitry Andric
GetUnderlyingObjects(Value * V,SmallVectorImpl<Value * > & Objects,const DataLayout & DL,LoopInfo * LI,unsigned MaxLookup)3720ff0cc061SDimitry Andric void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl<Value *> &Objects,
3721ff0cc061SDimitry Andric const DataLayout &DL, LoopInfo *LI,
37227ae0e2c9SDimitry Andric unsigned MaxLookup) {
37237ae0e2c9SDimitry Andric SmallPtrSet<Value *, 4> Visited;
37247ae0e2c9SDimitry Andric SmallVector<Value *, 4> Worklist;
37257ae0e2c9SDimitry Andric Worklist.push_back(V);
37267ae0e2c9SDimitry Andric do {
37277ae0e2c9SDimitry Andric Value *P = Worklist.pop_back_val();
3728ff0cc061SDimitry Andric P = GetUnderlyingObject(P, DL, MaxLookup);
37297ae0e2c9SDimitry Andric
373039d628a0SDimitry Andric if (!Visited.insert(P).second)
37317ae0e2c9SDimitry Andric continue;
37327ae0e2c9SDimitry Andric
37337ae0e2c9SDimitry Andric if (SelectInst *SI = dyn_cast<SelectInst>(P)) {
37347ae0e2c9SDimitry Andric Worklist.push_back(SI->getTrueValue());
37357ae0e2c9SDimitry Andric Worklist.push_back(SI->getFalseValue());
37367ae0e2c9SDimitry Andric continue;
37377ae0e2c9SDimitry Andric }
37387ae0e2c9SDimitry Andric
37397ae0e2c9SDimitry Andric if (PHINode *PN = dyn_cast<PHINode>(P)) {
3740ff0cc061SDimitry Andric // If this PHI changes the underlying object in every iteration of the
3741ff0cc061SDimitry Andric // loop, don't look through it. Consider:
3742ff0cc061SDimitry Andric // int **A;
3743ff0cc061SDimitry Andric // for (i) {
3744ff0cc061SDimitry Andric // Prev = Curr; // Prev = PHI (Prev_0, Curr)
3745ff0cc061SDimitry Andric // Curr = A[i];
3746ff0cc061SDimitry Andric // *Prev, *Curr;
3747ff0cc061SDimitry Andric //
3748ff0cc061SDimitry Andric // Prev is tracking Curr one iteration behind so they refer to different
3749ff0cc061SDimitry Andric // underlying objects.
3750ff0cc061SDimitry Andric if (!LI || !LI->isLoopHeader(PN->getParent()) ||
3751ff0cc061SDimitry Andric isSameUnderlyingObjectInLoop(PN, LI))
3752ff0cc061SDimitry Andric for (Value *IncValue : PN->incoming_values())
3753ff0cc061SDimitry Andric Worklist.push_back(IncValue);
37547ae0e2c9SDimitry Andric continue;
37557ae0e2c9SDimitry Andric }
37567ae0e2c9SDimitry Andric
37577ae0e2c9SDimitry Andric Objects.push_back(P);
37587ae0e2c9SDimitry Andric } while (!Worklist.empty());
37597ae0e2c9SDimitry Andric }
37607ae0e2c9SDimitry Andric
37619dc417c3SDimitry Andric /// This is the function that does the work of looking through basic
37629dc417c3SDimitry Andric /// ptrtoint+arithmetic+inttoptr sequences.
getUnderlyingObjectFromInt(const Value * V)37639dc417c3SDimitry Andric static const Value *getUnderlyingObjectFromInt(const Value *V) {
37649dc417c3SDimitry Andric do {
37659dc417c3SDimitry Andric if (const Operator *U = dyn_cast<Operator>(V)) {
37669dc417c3SDimitry Andric // If we find a ptrtoint, we can transfer control back to the
37679dc417c3SDimitry Andric // regular getUnderlyingObjectFromInt.
37689dc417c3SDimitry Andric if (U->getOpcode() == Instruction::PtrToInt)
37699dc417c3SDimitry Andric return U->getOperand(0);
37709dc417c3SDimitry Andric // If we find an add of a constant, a multiplied value, or a phi, it's
37719dc417c3SDimitry Andric // likely that the other operand will lead us to the base
37729dc417c3SDimitry Andric // object. We don't have to worry about the case where the
37739dc417c3SDimitry Andric // object address is somehow being computed by the multiply,
37749dc417c3SDimitry Andric // because our callers only care when the result is an
37759dc417c3SDimitry Andric // identifiable object.
37769dc417c3SDimitry Andric if (U->getOpcode() != Instruction::Add ||
37779dc417c3SDimitry Andric (!isa<ConstantInt>(U->getOperand(1)) &&
37789dc417c3SDimitry Andric Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
37799dc417c3SDimitry Andric !isa<PHINode>(U->getOperand(1))))
37809dc417c3SDimitry Andric return V;
37819dc417c3SDimitry Andric V = U->getOperand(0);
37829dc417c3SDimitry Andric } else {
37839dc417c3SDimitry Andric return V;
37849dc417c3SDimitry Andric }
37859dc417c3SDimitry Andric assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
37869dc417c3SDimitry Andric } while (true);
37879dc417c3SDimitry Andric }
37889dc417c3SDimitry Andric
37899dc417c3SDimitry Andric /// This is a wrapper around GetUnderlyingObjects and adds support for basic
37909dc417c3SDimitry Andric /// ptrtoint+arithmetic+inttoptr sequences.
37912cab237bSDimitry Andric /// It returns false if unidentified object is found in GetUnderlyingObjects.
getUnderlyingObjectsForCodeGen(const Value * V,SmallVectorImpl<Value * > & Objects,const DataLayout & DL)37922cab237bSDimitry Andric bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,
37939dc417c3SDimitry Andric SmallVectorImpl<Value *> &Objects,
37949dc417c3SDimitry Andric const DataLayout &DL) {
37959dc417c3SDimitry Andric SmallPtrSet<const Value *, 16> Visited;
37969dc417c3SDimitry Andric SmallVector<const Value *, 4> Working(1, V);
37979dc417c3SDimitry Andric do {
37989dc417c3SDimitry Andric V = Working.pop_back_val();
37999dc417c3SDimitry Andric
38009dc417c3SDimitry Andric SmallVector<Value *, 4> Objs;
38019dc417c3SDimitry Andric GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
38029dc417c3SDimitry Andric
38039dc417c3SDimitry Andric for (Value *V : Objs) {
38049dc417c3SDimitry Andric if (!Visited.insert(V).second)
38059dc417c3SDimitry Andric continue;
38069dc417c3SDimitry Andric if (Operator::getOpcode(V) == Instruction::IntToPtr) {
38079dc417c3SDimitry Andric const Value *O =
38089dc417c3SDimitry Andric getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
38099dc417c3SDimitry Andric if (O->getType()->isPointerTy()) {
38109dc417c3SDimitry Andric Working.push_back(O);
38119dc417c3SDimitry Andric continue;
38129dc417c3SDimitry Andric }
38139dc417c3SDimitry Andric }
38149dc417c3SDimitry Andric // If GetUnderlyingObjects fails to find an identifiable object,
38159dc417c3SDimitry Andric // getUnderlyingObjectsForCodeGen also fails for safety.
38169dc417c3SDimitry Andric if (!isIdentifiedObject(V)) {
38179dc417c3SDimitry Andric Objects.clear();
38182cab237bSDimitry Andric return false;
38199dc417c3SDimitry Andric }
38209dc417c3SDimitry Andric Objects.push_back(const_cast<Value *>(V));
38219dc417c3SDimitry Andric }
38229dc417c3SDimitry Andric } while (!Working.empty());
38232cab237bSDimitry Andric return true;
38249dc417c3SDimitry Andric }
38259dc417c3SDimitry Andric
382639d628a0SDimitry Andric /// Return true if the only users of this pointer are lifetime markers.
onlyUsedByLifetimeMarkers(const Value * V)382717a519f9SDimitry Andric bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
382891bc56edSDimitry Andric for (const User *U : V->users()) {
382991bc56edSDimitry Andric const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
383017a519f9SDimitry Andric if (!II) return false;
383117a519f9SDimitry Andric
3832b5893f02SDimitry Andric if (!II->isLifetimeStartOrEnd())
383317a519f9SDimitry Andric return false;
383417a519f9SDimitry Andric }
383517a519f9SDimitry Andric return true;
383617a519f9SDimitry Andric }
3837dff0c46cSDimitry Andric
isSafeToSpeculativelyExecute(const Value * V,const Instruction * CtxI,const DominatorTree * DT)3838dff0c46cSDimitry Andric bool llvm::isSafeToSpeculativelyExecute(const Value *V,
3839ff0cc061SDimitry Andric const Instruction *CtxI,
38403ca95b02SDimitry Andric const DominatorTree *DT) {
3841dff0c46cSDimitry Andric const Operator *Inst = dyn_cast<Operator>(V);
3842dff0c46cSDimitry Andric if (!Inst)
3843dff0c46cSDimitry Andric return false;
3844dff0c46cSDimitry Andric
3845dff0c46cSDimitry Andric for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
3846dff0c46cSDimitry Andric if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
3847dff0c46cSDimitry Andric if (C->canTrap())
3848dff0c46cSDimitry Andric return false;
3849dff0c46cSDimitry Andric
3850dff0c46cSDimitry Andric switch (Inst->getOpcode()) {
3851dff0c46cSDimitry Andric default:
3852dff0c46cSDimitry Andric return true;
3853dff0c46cSDimitry Andric case Instruction::UDiv:
38549cac79b3SDimitry Andric case Instruction::URem: {
38559cac79b3SDimitry Andric // x / y is undefined if y == 0.
38569cac79b3SDimitry Andric const APInt *V;
38579cac79b3SDimitry Andric if (match(Inst->getOperand(1), m_APInt(V)))
38589cac79b3SDimitry Andric return *V != 0;
38599cac79b3SDimitry Andric return false;
38609cac79b3SDimitry Andric }
3861dff0c46cSDimitry Andric case Instruction::SDiv:
3862dff0c46cSDimitry Andric case Instruction::SRem: {
38639cac79b3SDimitry Andric // x / y is undefined if y == 0 or x == INT_MIN and y == -1
3864ff0cc061SDimitry Andric const APInt *Numerator, *Denominator;
3865ff0cc061SDimitry Andric if (!match(Inst->getOperand(1), m_APInt(Denominator)))
3866dff0c46cSDimitry Andric return false;
3867ff0cc061SDimitry Andric // We cannot hoist this division if the denominator is 0.
3868ff0cc061SDimitry Andric if (*Denominator == 0)
3869ff0cc061SDimitry Andric return false;
3870ff0cc061SDimitry Andric // It's safe to hoist if the denominator is not 0 or -1.
3871ff0cc061SDimitry Andric if (*Denominator != -1)
38729cac79b3SDimitry Andric return true;
3873ff0cc061SDimitry Andric // At this point we know that the denominator is -1. It is safe to hoist as
3874ff0cc061SDimitry Andric // long we know that the numerator is not INT_MIN.
3875ff0cc061SDimitry Andric if (match(Inst->getOperand(0), m_APInt(Numerator)))
3876ff0cc061SDimitry Andric return !Numerator->isMinSignedValue();
3877ff0cc061SDimitry Andric // The numerator *might* be MinSignedValue.
3878dff0c46cSDimitry Andric return false;
3879dff0c46cSDimitry Andric }
3880dff0c46cSDimitry Andric case Instruction::Load: {
3881dff0c46cSDimitry Andric const LoadInst *LI = cast<LoadInst>(Inst);
388291bc56edSDimitry Andric if (!LI->isUnordered() ||
388391bc56edSDimitry Andric // Speculative load may create a race that did not exist in the source.
38843ca95b02SDimitry Andric LI->getFunction()->hasFnAttribute(Attribute::SanitizeThread) ||
38857d523365SDimitry Andric // Speculative load may load data from dirty regions.
38862cab237bSDimitry Andric LI->getFunction()->hasFnAttribute(Attribute::SanitizeAddress) ||
38872cab237bSDimitry Andric LI->getFunction()->hasFnAttribute(Attribute::SanitizeHWAddress))
3888dff0c46cSDimitry Andric return false;
3889ff0cc061SDimitry Andric const DataLayout &DL = LI->getModule()->getDataLayout();
38903ca95b02SDimitry Andric return isDereferenceableAndAlignedPointer(LI->getPointerOperand(),
38913ca95b02SDimitry Andric LI->getAlignment(), DL, CtxI, DT);
3892dff0c46cSDimitry Andric }
3893dff0c46cSDimitry Andric case Instruction::Call: {
3894f37b6182SDimitry Andric auto *CI = cast<const CallInst>(Inst);
3895f37b6182SDimitry Andric const Function *Callee = CI->getCalledFunction();
3896dff0c46cSDimitry Andric
3897f37b6182SDimitry Andric // The called function could have undefined behavior or side-effects, even
3898f37b6182SDimitry Andric // if marked readnone nounwind.
3899f37b6182SDimitry Andric return Callee && Callee->isSpeculatable();
3900dff0c46cSDimitry Andric }
3901dff0c46cSDimitry Andric case Instruction::VAArg:
3902dff0c46cSDimitry Andric case Instruction::Alloca:
3903dff0c46cSDimitry Andric case Instruction::Invoke:
3904dff0c46cSDimitry Andric case Instruction::PHI:
3905dff0c46cSDimitry Andric case Instruction::Store:
3906dff0c46cSDimitry Andric case Instruction::Ret:
3907dff0c46cSDimitry Andric case Instruction::Br:
3908dff0c46cSDimitry Andric case Instruction::IndirectBr:
3909dff0c46cSDimitry Andric case Instruction::Switch:
3910dff0c46cSDimitry Andric case Instruction::Unreachable:
3911dff0c46cSDimitry Andric case Instruction::Fence:
3912dff0c46cSDimitry Andric case Instruction::AtomicRMW:
3913dff0c46cSDimitry Andric case Instruction::AtomicCmpXchg:
39147d523365SDimitry Andric case Instruction::LandingPad:
3915dff0c46cSDimitry Andric case Instruction::Resume:
39167d523365SDimitry Andric case Instruction::CatchSwitch:
39177d523365SDimitry Andric case Instruction::CatchPad:
39187d523365SDimitry Andric case Instruction::CatchRet:
39197d523365SDimitry Andric case Instruction::CleanupPad:
39207d523365SDimitry Andric case Instruction::CleanupRet:
3921dff0c46cSDimitry Andric return false; // Misc instructions which have effects
3922dff0c46cSDimitry Andric }
3923dff0c46cSDimitry Andric }
3924139f7f9bSDimitry Andric
mayBeMemoryDependent(const Instruction & I)39257d523365SDimitry Andric bool llvm::mayBeMemoryDependent(const Instruction &I) {
39267d523365SDimitry Andric return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I);
39277d523365SDimitry Andric }
39287d523365SDimitry Andric
computeOverflowForUnsignedMul(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)3929b5893f02SDimitry Andric OverflowResult llvm::computeOverflowForUnsignedMul(
3930b5893f02SDimitry Andric const Value *LHS, const Value *RHS, const DataLayout &DL,
3931b5893f02SDimitry Andric AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
3932b5893f02SDimitry Andric bool UseInstrInfo) {
393339d628a0SDimitry Andric // Multiplying n * m significant bits yields a result of n + m significant
393439d628a0SDimitry Andric // bits. If the total number of significant bits does not exceed the
393539d628a0SDimitry Andric // result bit width (minus 1), there is no overflow.
393639d628a0SDimitry Andric // This means if we have enough leading zero bits in the operands
393739d628a0SDimitry Andric // we can guarantee that the result does not overflow.
393839d628a0SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren
393939d628a0SDimitry Andric unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
394051690af2SDimitry Andric KnownBits LHSKnown(BitWidth);
394151690af2SDimitry Andric KnownBits RHSKnown(BitWidth);
3942b5893f02SDimitry Andric computeKnownBits(LHS, LHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr,
3943b5893f02SDimitry Andric UseInstrInfo);
3944b5893f02SDimitry Andric computeKnownBits(RHS, RHSKnown, DL, /*Depth=*/0, AC, CxtI, DT, nullptr,
3945b5893f02SDimitry Andric UseInstrInfo);
394639d628a0SDimitry Andric // Note that underestimating the number of zero bits gives a more
394739d628a0SDimitry Andric // conservative answer.
39485517e702SDimitry Andric unsigned ZeroBits = LHSKnown.countMinLeadingZeros() +
39495517e702SDimitry Andric RHSKnown.countMinLeadingZeros();
395039d628a0SDimitry Andric // First handle the easy case: if we have enough zero bits there's
395139d628a0SDimitry Andric // definitely no overflow.
395239d628a0SDimitry Andric if (ZeroBits >= BitWidth)
395339d628a0SDimitry Andric return OverflowResult::NeverOverflows;
395439d628a0SDimitry Andric
395539d628a0SDimitry Andric // Get the largest possible values for each operand.
395651690af2SDimitry Andric APInt LHSMax = ~LHSKnown.Zero;
395751690af2SDimitry Andric APInt RHSMax = ~RHSKnown.Zero;
395839d628a0SDimitry Andric
395939d628a0SDimitry Andric // We know the multiply operation doesn't overflow if the maximum values for
396039d628a0SDimitry Andric // each operand will not overflow after we multiply them together.
396139d628a0SDimitry Andric bool MaxOverflow;
39626bc11b14SDimitry Andric (void)LHSMax.umul_ov(RHSMax, MaxOverflow);
396339d628a0SDimitry Andric if (!MaxOverflow)
396439d628a0SDimitry Andric return OverflowResult::NeverOverflows;
396539d628a0SDimitry Andric
396639d628a0SDimitry Andric // We know it always overflows if multiplying the smallest possible values for
396739d628a0SDimitry Andric // the operands also results in overflow.
396839d628a0SDimitry Andric bool MinOverflow;
396951690af2SDimitry Andric (void)LHSKnown.One.umul_ov(RHSKnown.One, MinOverflow);
397039d628a0SDimitry Andric if (MinOverflow)
397139d628a0SDimitry Andric return OverflowResult::AlwaysOverflows;
397239d628a0SDimitry Andric
397339d628a0SDimitry Andric return OverflowResult::MayOverflow;
397439d628a0SDimitry Andric }
397539d628a0SDimitry Andric
3976b5893f02SDimitry Andric OverflowResult
computeOverflowForSignedMul(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)3977b5893f02SDimitry Andric llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
3978b5893f02SDimitry Andric const DataLayout &DL, AssumptionCache *AC,
39794ba319b5SDimitry Andric const Instruction *CxtI,
3980b5893f02SDimitry Andric const DominatorTree *DT, bool UseInstrInfo) {
39814ba319b5SDimitry Andric // Multiplying n * m significant bits yields a result of n + m significant
39824ba319b5SDimitry Andric // bits. If the total number of significant bits does not exceed the
39834ba319b5SDimitry Andric // result bit width (minus 1), there is no overflow.
39844ba319b5SDimitry Andric // This means if we have enough leading sign bits in the operands
39854ba319b5SDimitry Andric // we can guarantee that the result does not overflow.
39864ba319b5SDimitry Andric // Ref: "Hacker's Delight" by Henry Warren
39874ba319b5SDimitry Andric unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
39884ba319b5SDimitry Andric
39894ba319b5SDimitry Andric // Note that underestimating the number of sign bits gives a more
39904ba319b5SDimitry Andric // conservative answer.
39914ba319b5SDimitry Andric unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) +
39924ba319b5SDimitry Andric ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT);
39934ba319b5SDimitry Andric
39944ba319b5SDimitry Andric // First handle the easy case: if we have enough sign bits there's
39954ba319b5SDimitry Andric // definitely no overflow.
39964ba319b5SDimitry Andric if (SignBits > BitWidth + 1)
39974ba319b5SDimitry Andric return OverflowResult::NeverOverflows;
39984ba319b5SDimitry Andric
39994ba319b5SDimitry Andric // There are two ambiguous cases where there can be no overflow:
40004ba319b5SDimitry Andric // SignBits == BitWidth + 1 and
40014ba319b5SDimitry Andric // SignBits == BitWidth
40024ba319b5SDimitry Andric // The second case is difficult to check, therefore we only handle the
40034ba319b5SDimitry Andric // first case.
40044ba319b5SDimitry Andric if (SignBits == BitWidth + 1) {
40054ba319b5SDimitry Andric // It overflows only when both arguments are negative and the true
40064ba319b5SDimitry Andric // product is exactly the minimum negative number.
40074ba319b5SDimitry Andric // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
40084ba319b5SDimitry Andric // For simplicity we just check if at least one side is not negative.
4009b5893f02SDimitry Andric KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
4010b5893f02SDimitry Andric nullptr, UseInstrInfo);
4011b5893f02SDimitry Andric KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
4012b5893f02SDimitry Andric nullptr, UseInstrInfo);
40134ba319b5SDimitry Andric if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
40144ba319b5SDimitry Andric return OverflowResult::NeverOverflows;
40154ba319b5SDimitry Andric }
40164ba319b5SDimitry Andric return OverflowResult::MayOverflow;
40174ba319b5SDimitry Andric }
40184ba319b5SDimitry Andric
computeOverflowForUnsignedAdd(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT,bool UseInstrInfo)4019b5893f02SDimitry Andric OverflowResult llvm::computeOverflowForUnsignedAdd(
4020b5893f02SDimitry Andric const Value *LHS, const Value *RHS, const DataLayout &DL,
4021b5893f02SDimitry Andric AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
4022b5893f02SDimitry Andric bool UseInstrInfo) {
4023b5893f02SDimitry Andric KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
4024b5893f02SDimitry Andric nullptr, UseInstrInfo);
40255517e702SDimitry Andric if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) {
4026b5893f02SDimitry Andric KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
4027b5893f02SDimitry Andric nullptr, UseInstrInfo);
402839d628a0SDimitry Andric
40295517e702SDimitry Andric if (LHSKnown.isNegative() && RHSKnown.isNegative()) {
403039d628a0SDimitry Andric // The sign bit is set in both cases: this MUST overflow.
403139d628a0SDimitry Andric return OverflowResult::AlwaysOverflows;
403239d628a0SDimitry Andric }
403339d628a0SDimitry Andric
40345517e702SDimitry Andric if (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()) {
403539d628a0SDimitry Andric // The sign bit is clear in both cases: this CANNOT overflow.
403639d628a0SDimitry Andric return OverflowResult::NeverOverflows;
403739d628a0SDimitry Andric }
403839d628a0SDimitry Andric }
403939d628a0SDimitry Andric
404039d628a0SDimitry Andric return OverflowResult::MayOverflow;
404139d628a0SDimitry Andric }
4042ff0cc061SDimitry Andric
40434ba319b5SDimitry Andric /// Return true if we can prove that adding the two values of the
40445517e702SDimitry Andric /// knownbits will not overflow.
40455517e702SDimitry Andric /// Otherwise return false.
checkRippleForSignedAdd(const KnownBits & LHSKnown,const KnownBits & RHSKnown)40465517e702SDimitry Andric static bool checkRippleForSignedAdd(const KnownBits &LHSKnown,
40475517e702SDimitry Andric const KnownBits &RHSKnown) {
40485517e702SDimitry Andric // Addition of two 2's complement numbers having opposite signs will never
40495517e702SDimitry Andric // overflow.
40505517e702SDimitry Andric if ((LHSKnown.isNegative() && RHSKnown.isNonNegative()) ||
40515517e702SDimitry Andric (LHSKnown.isNonNegative() && RHSKnown.isNegative()))
40525517e702SDimitry Andric return true;
40535517e702SDimitry Andric
40545517e702SDimitry Andric // If either of the values is known to be non-negative, adding them can only
40555517e702SDimitry Andric // overflow if the second is also non-negative, so we can assume that.
40565517e702SDimitry Andric // Two non-negative numbers will only overflow if there is a carry to the
40575517e702SDimitry Andric // sign bit, so we can check if even when the values are as big as possible
40585517e702SDimitry Andric // there is no overflow to the sign bit.
40595517e702SDimitry Andric if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative()) {
40605517e702SDimitry Andric APInt MaxLHS = ~LHSKnown.Zero;
40615517e702SDimitry Andric MaxLHS.clearSignBit();
40625517e702SDimitry Andric APInt MaxRHS = ~RHSKnown.Zero;
40635517e702SDimitry Andric MaxRHS.clearSignBit();
40645517e702SDimitry Andric APInt Result = std::move(MaxLHS) + std::move(MaxRHS);
40655517e702SDimitry Andric return Result.isSignBitClear();
40665517e702SDimitry Andric }
40675517e702SDimitry Andric
40685517e702SDimitry Andric // If either of the values is known to be negative, adding them can only
40695517e702SDimitry Andric // overflow if the second is also negative, so we can assume that.
40705517e702SDimitry Andric // Two negative number will only overflow if there is no carry to the sign
40715517e702SDimitry Andric // bit, so we can check if even when the values are as small as possible
40725517e702SDimitry Andric // there is overflow to the sign bit.
40735517e702SDimitry Andric if (LHSKnown.isNegative() || RHSKnown.isNegative()) {
40745517e702SDimitry Andric APInt MinLHS = LHSKnown.One;
40755517e702SDimitry Andric MinLHS.clearSignBit();
40765517e702SDimitry Andric APInt MinRHS = RHSKnown.One;
40775517e702SDimitry Andric MinRHS.clearSignBit();
40785517e702SDimitry Andric APInt Result = std::move(MinLHS) + std::move(MinRHS);
40795517e702SDimitry Andric return Result.isSignBitSet();
40805517e702SDimitry Andric }
40815517e702SDimitry Andric
40825517e702SDimitry Andric // If we reached here it means that we know nothing about the sign bits.
40835517e702SDimitry Andric // In this case we can't know if there will be an overflow, since by
40845517e702SDimitry Andric // changing the sign bits any two values can be made to overflow.
40855517e702SDimitry Andric return false;
40865517e702SDimitry Andric }
40875517e702SDimitry Andric
computeOverflowForSignedAdd(const Value * LHS,const Value * RHS,const AddOperator * Add,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT)4088d88c1a5aSDimitry Andric static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
4089d88c1a5aSDimitry Andric const Value *RHS,
4090d88c1a5aSDimitry Andric const AddOperator *Add,
4091d88c1a5aSDimitry Andric const DataLayout &DL,
4092d88c1a5aSDimitry Andric AssumptionCache *AC,
4093d88c1a5aSDimitry Andric const Instruction *CxtI,
4094d88c1a5aSDimitry Andric const DominatorTree *DT) {
40957d523365SDimitry Andric if (Add && Add->hasNoSignedWrap()) {
40967d523365SDimitry Andric return OverflowResult::NeverOverflows;
40977d523365SDimitry Andric }
40987d523365SDimitry Andric
40995517e702SDimitry Andric // If LHS and RHS each have at least two sign bits, the addition will look
41005517e702SDimitry Andric // like
41015517e702SDimitry Andric //
41025517e702SDimitry Andric // XX..... +
41035517e702SDimitry Andric // YY.....
41045517e702SDimitry Andric //
41055517e702SDimitry Andric // If the carry into the most significant position is 0, X and Y can't both
41065517e702SDimitry Andric // be 1 and therefore the carry out of the addition is also 0.
41075517e702SDimitry Andric //
41085517e702SDimitry Andric // If the carry into the most significant position is 1, X and Y can't both
41095517e702SDimitry Andric // be 0 and therefore the carry out of the addition is also 1.
41105517e702SDimitry Andric //
41115517e702SDimitry Andric // Since the carry into the most significant position is always equal to
41125517e702SDimitry Andric // the carry out of the addition, there is no signed overflow.
41135517e702SDimitry Andric if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
41145517e702SDimitry Andric ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
41157d523365SDimitry Andric return OverflowResult::NeverOverflows;
41165517e702SDimitry Andric
41175517e702SDimitry Andric KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
41185517e702SDimitry Andric KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
41195517e702SDimitry Andric
41205517e702SDimitry Andric if (checkRippleForSignedAdd(LHSKnown, RHSKnown))
41215517e702SDimitry Andric return OverflowResult::NeverOverflows;
41227d523365SDimitry Andric
41237d523365SDimitry Andric // The remaining code needs Add to be available. Early returns if not so.
41247d523365SDimitry Andric if (!Add)
41257d523365SDimitry Andric return OverflowResult::MayOverflow;
41267d523365SDimitry Andric
41277d523365SDimitry Andric // If the sign of Add is the same as at least one of the operands, this add
41287d523365SDimitry Andric // CANNOT overflow. This is particularly useful when the sum is
41297d523365SDimitry Andric // @llvm.assume'ed non-negative rather than proved so from analyzing its
41307d523365SDimitry Andric // operands.
41317d523365SDimitry Andric bool LHSOrRHSKnownNonNegative =
41325517e702SDimitry Andric (LHSKnown.isNonNegative() || RHSKnown.isNonNegative());
41335517e702SDimitry Andric bool LHSOrRHSKnownNegative =
41345517e702SDimitry Andric (LHSKnown.isNegative() || RHSKnown.isNegative());
41357d523365SDimitry Andric if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
41365517e702SDimitry Andric KnownBits AddKnown = computeKnownBits(Add, DL, /*Depth=*/0, AC, CxtI, DT);
41375517e702SDimitry Andric if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
41385517e702SDimitry Andric (AddKnown.isNegative() && LHSOrRHSKnownNegative)) {
41397d523365SDimitry Andric return OverflowResult::NeverOverflows;
41407d523365SDimitry Andric }
41417d523365SDimitry Andric }
41427d523365SDimitry Andric
41437d523365SDimitry Andric return OverflowResult::MayOverflow;
41447d523365SDimitry Andric }
41457d523365SDimitry Andric
computeOverflowForUnsignedSub(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT)41464ba319b5SDimitry Andric OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
41474ba319b5SDimitry Andric const Value *RHS,
41484ba319b5SDimitry Andric const DataLayout &DL,
41494ba319b5SDimitry Andric AssumptionCache *AC,
41504ba319b5SDimitry Andric const Instruction *CxtI,
41514ba319b5SDimitry Andric const DominatorTree *DT) {
41524ba319b5SDimitry Andric KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT);
4153b5893f02SDimitry Andric if (LHSKnown.isNonNegative() || LHSKnown.isNegative()) {
41544ba319b5SDimitry Andric KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT);
4155b5893f02SDimitry Andric
4156b5893f02SDimitry Andric // If the LHS is negative and the RHS is non-negative, no unsigned wrap.
41574ba319b5SDimitry Andric if (LHSKnown.isNegative() && RHSKnown.isNonNegative())
41584ba319b5SDimitry Andric return OverflowResult::NeverOverflows;
41594ba319b5SDimitry Andric
4160b5893f02SDimitry Andric // If the LHS is non-negative and the RHS negative, we always wrap.
4161b5893f02SDimitry Andric if (LHSKnown.isNonNegative() && RHSKnown.isNegative())
4162b5893f02SDimitry Andric return OverflowResult::AlwaysOverflows;
4163b5893f02SDimitry Andric }
4164b5893f02SDimitry Andric
41654ba319b5SDimitry Andric return OverflowResult::MayOverflow;
41664ba319b5SDimitry Andric }
41674ba319b5SDimitry Andric
computeOverflowForSignedSub(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT)41684ba319b5SDimitry Andric OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
41694ba319b5SDimitry Andric const Value *RHS,
41704ba319b5SDimitry Andric const DataLayout &DL,
41714ba319b5SDimitry Andric AssumptionCache *AC,
41724ba319b5SDimitry Andric const Instruction *CxtI,
41734ba319b5SDimitry Andric const DominatorTree *DT) {
41744ba319b5SDimitry Andric // If LHS and RHS each have at least two sign bits, the subtraction
41754ba319b5SDimitry Andric // cannot overflow.
41764ba319b5SDimitry Andric if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
41774ba319b5SDimitry Andric ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
41784ba319b5SDimitry Andric return OverflowResult::NeverOverflows;
41794ba319b5SDimitry Andric
41804ba319b5SDimitry Andric KnownBits LHSKnown = computeKnownBits(LHS, DL, 0, AC, CxtI, DT);
41814ba319b5SDimitry Andric
41824ba319b5SDimitry Andric KnownBits RHSKnown = computeKnownBits(RHS, DL, 0, AC, CxtI, DT);
41834ba319b5SDimitry Andric
41844ba319b5SDimitry Andric // Subtraction of two 2's complement numbers having identical signs will
41854ba319b5SDimitry Andric // never overflow.
41864ba319b5SDimitry Andric if ((LHSKnown.isNegative() && RHSKnown.isNegative()) ||
41874ba319b5SDimitry Andric (LHSKnown.isNonNegative() && RHSKnown.isNonNegative()))
41884ba319b5SDimitry Andric return OverflowResult::NeverOverflows;
41894ba319b5SDimitry Andric
41904ba319b5SDimitry Andric // TODO: implement logic similar to checkRippleForAdd
41914ba319b5SDimitry Andric return OverflowResult::MayOverflow;
41924ba319b5SDimitry Andric }
41934ba319b5SDimitry Andric
isOverflowIntrinsicNoWrap(const IntrinsicInst * II,const DominatorTree & DT)4194d88c1a5aSDimitry Andric bool llvm::isOverflowIntrinsicNoWrap(const IntrinsicInst *II,
4195d88c1a5aSDimitry Andric const DominatorTree &DT) {
41963ca95b02SDimitry Andric #ifndef NDEBUG
41973ca95b02SDimitry Andric auto IID = II->getIntrinsicID();
41983ca95b02SDimitry Andric assert((IID == Intrinsic::sadd_with_overflow ||
41993ca95b02SDimitry Andric IID == Intrinsic::uadd_with_overflow ||
42003ca95b02SDimitry Andric IID == Intrinsic::ssub_with_overflow ||
42013ca95b02SDimitry Andric IID == Intrinsic::usub_with_overflow ||
42023ca95b02SDimitry Andric IID == Intrinsic::smul_with_overflow ||
42033ca95b02SDimitry Andric IID == Intrinsic::umul_with_overflow) &&
42043ca95b02SDimitry Andric "Not an overflow intrinsic!");
42053ca95b02SDimitry Andric #endif
42063ca95b02SDimitry Andric
4207d88c1a5aSDimitry Andric SmallVector<const BranchInst *, 2> GuardingBranches;
4208d88c1a5aSDimitry Andric SmallVector<const ExtractValueInst *, 2> Results;
42093ca95b02SDimitry Andric
4210d88c1a5aSDimitry Andric for (const User *U : II->users()) {
4211d88c1a5aSDimitry Andric if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
42123ca95b02SDimitry Andric assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
42133ca95b02SDimitry Andric
42143ca95b02SDimitry Andric if (EVI->getIndices()[0] == 0)
42153ca95b02SDimitry Andric Results.push_back(EVI);
42163ca95b02SDimitry Andric else {
42173ca95b02SDimitry Andric assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
42183ca95b02SDimitry Andric
4219d88c1a5aSDimitry Andric for (const auto *U : EVI->users())
4220d88c1a5aSDimitry Andric if (const auto *B = dyn_cast<BranchInst>(U)) {
42213ca95b02SDimitry Andric assert(B->isConditional() && "How else is it using an i1?");
42223ca95b02SDimitry Andric GuardingBranches.push_back(B);
42233ca95b02SDimitry Andric }
42243ca95b02SDimitry Andric }
42253ca95b02SDimitry Andric } else {
42263ca95b02SDimitry Andric // We are using the aggregate directly in a way we don't want to analyze
42273ca95b02SDimitry Andric // here (storing it to a global, say).
42283ca95b02SDimitry Andric return false;
42293ca95b02SDimitry Andric }
42303ca95b02SDimitry Andric }
42313ca95b02SDimitry Andric
4232d88c1a5aSDimitry Andric auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
42333ca95b02SDimitry Andric BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
42343ca95b02SDimitry Andric if (!NoWrapEdge.isSingleEdge())
42353ca95b02SDimitry Andric return false;
42363ca95b02SDimitry Andric
42373ca95b02SDimitry Andric // Check if all users of the add are provably no-wrap.
4238d88c1a5aSDimitry Andric for (const auto *Result : Results) {
42393ca95b02SDimitry Andric // If the extractvalue itself is not executed on overflow, the we don't
42403ca95b02SDimitry Andric // need to check each use separately, since domination is transitive.
42413ca95b02SDimitry Andric if (DT.dominates(NoWrapEdge, Result->getParent()))
42423ca95b02SDimitry Andric continue;
42433ca95b02SDimitry Andric
42443ca95b02SDimitry Andric for (auto &RU : Result->uses())
42453ca95b02SDimitry Andric if (!DT.dominates(NoWrapEdge, RU))
42463ca95b02SDimitry Andric return false;
42473ca95b02SDimitry Andric }
42483ca95b02SDimitry Andric
42493ca95b02SDimitry Andric return true;
42503ca95b02SDimitry Andric };
42513ca95b02SDimitry Andric
42522cab237bSDimitry Andric return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
42533ca95b02SDimitry Andric }
42543ca95b02SDimitry Andric
42553ca95b02SDimitry Andric
computeOverflowForSignedAdd(const AddOperator * Add,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT)4256d88c1a5aSDimitry Andric OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
42577d523365SDimitry Andric const DataLayout &DL,
42587d523365SDimitry Andric AssumptionCache *AC,
42597d523365SDimitry Andric const Instruction *CxtI,
42607d523365SDimitry Andric const DominatorTree *DT) {
42617d523365SDimitry Andric return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
42627d523365SDimitry Andric Add, DL, AC, CxtI, DT);
42637d523365SDimitry Andric }
42647d523365SDimitry Andric
computeOverflowForSignedAdd(const Value * LHS,const Value * RHS,const DataLayout & DL,AssumptionCache * AC,const Instruction * CxtI,const DominatorTree * DT)4265d88c1a5aSDimitry Andric OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS,
4266d88c1a5aSDimitry Andric const Value *RHS,
42677d523365SDimitry Andric const DataLayout &DL,
42687d523365SDimitry Andric AssumptionCache *AC,
42697d523365SDimitry Andric const Instruction *CxtI,
42707d523365SDimitry Andric const DominatorTree *DT) {
42717d523365SDimitry Andric return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT);
42727d523365SDimitry Andric }
42737d523365SDimitry Andric
isGuaranteedToTransferExecutionToSuccessor(const Instruction * I)42747d523365SDimitry Andric bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
42753ca95b02SDimitry Andric // A memory operation returns normally if it isn't volatile. A volatile
42763ca95b02SDimitry Andric // operation is allowed to trap.
42773ca95b02SDimitry Andric //
42783ca95b02SDimitry Andric // An atomic operation isn't guaranteed to return in a reasonable amount of
42793ca95b02SDimitry Andric // time because it's possible for another thread to interfere with it for an
42803ca95b02SDimitry Andric // arbitrary length of time, but programs aren't allowed to rely on that.
42813ca95b02SDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(I))
42823ca95b02SDimitry Andric return !LI->isVolatile();
42833ca95b02SDimitry Andric if (const StoreInst *SI = dyn_cast<StoreInst>(I))
42843ca95b02SDimitry Andric return !SI->isVolatile();
42853ca95b02SDimitry Andric if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(I))
42863ca95b02SDimitry Andric return !CXI->isVolatile();
42873ca95b02SDimitry Andric if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(I))
42883ca95b02SDimitry Andric return !RMWI->isVolatile();
42893ca95b02SDimitry Andric if (const MemIntrinsic *MII = dyn_cast<MemIntrinsic>(I))
42903ca95b02SDimitry Andric return !MII->isVolatile();
42917d523365SDimitry Andric
42923ca95b02SDimitry Andric // If there is no successor, then execution can't transfer to it.
42933ca95b02SDimitry Andric if (const auto *CRI = dyn_cast<CleanupReturnInst>(I))
42943ca95b02SDimitry Andric return !CRI->unwindsToCaller();
42953ca95b02SDimitry Andric if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I))
42963ca95b02SDimitry Andric return !CatchSwitch->unwindsToCaller();
42973ca95b02SDimitry Andric if (isa<ResumeInst>(I))
42983ca95b02SDimitry Andric return false;
42993ca95b02SDimitry Andric if (isa<ReturnInst>(I))
43003ca95b02SDimitry Andric return false;
43017a7e6055SDimitry Andric if (isa<UnreachableInst>(I))
43027a7e6055SDimitry Andric return false;
43033ca95b02SDimitry Andric
43043ca95b02SDimitry Andric // Calls can throw, or contain an infinite loop, or kill the process.
4305d88c1a5aSDimitry Andric if (auto CS = ImmutableCallSite(I)) {
4306d88c1a5aSDimitry Andric // Call sites that throw have implicit non-local control flow.
4307d88c1a5aSDimitry Andric if (!CS.doesNotThrow())
4308d88c1a5aSDimitry Andric return false;
4309d88c1a5aSDimitry Andric
4310d88c1a5aSDimitry Andric // Non-throwing call sites can loop infinitely, call exit/pthread_exit
4311d88c1a5aSDimitry Andric // etc. and thus not return. However, LLVM already assumes that
4312d88c1a5aSDimitry Andric //
4313d88c1a5aSDimitry Andric // - Thread exiting actions are modeled as writes to memory invisible to
4314d88c1a5aSDimitry Andric // the program.
4315d88c1a5aSDimitry Andric //
4316d88c1a5aSDimitry Andric // - Loops that don't have side effects (side effects are volatile/atomic
4317d88c1a5aSDimitry Andric // stores and IO) always terminate (see http://llvm.org/PR965).
4318d88c1a5aSDimitry Andric // Furthermore IO itself is also modeled as writes to memory invisible to
4319d88c1a5aSDimitry Andric // the program.
4320d88c1a5aSDimitry Andric //
4321d88c1a5aSDimitry Andric // We rely on those assumptions here, and use the memory effects of the call
4322d88c1a5aSDimitry Andric // target as a proxy for checking that it always returns.
4323d88c1a5aSDimitry Andric
4324d88c1a5aSDimitry Andric // FIXME: This isn't aggressive enough; a call which only writes to a global
4325d88c1a5aSDimitry Andric // is guaranteed to return.
43263ca95b02SDimitry Andric return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory() ||
43272cab237bSDimitry Andric match(I, m_Intrinsic<Intrinsic::assume>()) ||
43282cab237bSDimitry Andric match(I, m_Intrinsic<Intrinsic::sideeffect>());
43293ca95b02SDimitry Andric }
43303ca95b02SDimitry Andric
43313ca95b02SDimitry Andric // Other instructions return normally.
43323ca95b02SDimitry Andric return true;
43337d523365SDimitry Andric }
43347d523365SDimitry Andric
isGuaranteedToTransferExecutionToSuccessor(const BasicBlock * BB)43354ba319b5SDimitry Andric bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) {
43364ba319b5SDimitry Andric // TODO: This is slightly consdervative for invoke instruction since exiting
43374ba319b5SDimitry Andric // via an exception *is* normal control for them.
43384ba319b5SDimitry Andric for (auto I = BB->begin(), E = BB->end(); I != E; ++I)
43394ba319b5SDimitry Andric if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
43404ba319b5SDimitry Andric return false;
43414ba319b5SDimitry Andric return true;
43424ba319b5SDimitry Andric }
43434ba319b5SDimitry Andric
isGuaranteedToExecuteForEveryIteration(const Instruction * I,const Loop * L)43447d523365SDimitry Andric bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
43457d523365SDimitry Andric const Loop *L) {
43467d523365SDimitry Andric // The loop header is guaranteed to be executed for every iteration.
43477d523365SDimitry Andric //
43487d523365SDimitry Andric // FIXME: Relax this constraint to cover all basic blocks that are
43497d523365SDimitry Andric // guaranteed to be executed at every iteration.
43507d523365SDimitry Andric if (I->getParent() != L->getHeader()) return false;
43517d523365SDimitry Andric
43527d523365SDimitry Andric for (const Instruction &LI : *L->getHeader()) {
43537d523365SDimitry Andric if (&LI == I) return true;
43547d523365SDimitry Andric if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
43557d523365SDimitry Andric }
43567d523365SDimitry Andric llvm_unreachable("Instruction not contained in its own parent basic block.");
43577d523365SDimitry Andric }
43587d523365SDimitry Andric
propagatesFullPoison(const Instruction * I)43597d523365SDimitry Andric bool llvm::propagatesFullPoison(const Instruction *I) {
43607d523365SDimitry Andric switch (I->getOpcode()) {
43617d523365SDimitry Andric case Instruction::Add:
43627d523365SDimitry Andric case Instruction::Sub:
43637d523365SDimitry Andric case Instruction::Xor:
43647d523365SDimitry Andric case Instruction::Trunc:
43657d523365SDimitry Andric case Instruction::BitCast:
43667d523365SDimitry Andric case Instruction::AddrSpaceCast:
43677a7e6055SDimitry Andric case Instruction::Mul:
43687a7e6055SDimitry Andric case Instruction::Shl:
43697a7e6055SDimitry Andric case Instruction::GetElementPtr:
43707d523365SDimitry Andric // These operations all propagate poison unconditionally. Note that poison
43717d523365SDimitry Andric // is not any particular value, so xor or subtraction of poison with
43727d523365SDimitry Andric // itself still yields poison, not zero.
43737d523365SDimitry Andric return true;
43747d523365SDimitry Andric
43757d523365SDimitry Andric case Instruction::AShr:
43767d523365SDimitry Andric case Instruction::SExt:
43777d523365SDimitry Andric // For these operations, one bit of the input is replicated across
43787d523365SDimitry Andric // multiple output bits. A replicated poison bit is still poison.
43797d523365SDimitry Andric return true;
43807d523365SDimitry Andric
43813ca95b02SDimitry Andric case Instruction::ICmp:
43823ca95b02SDimitry Andric // Comparing poison with any value yields poison. This is why, for
43833ca95b02SDimitry Andric // instance, x s< (x +nsw 1) can be folded to true.
43843ca95b02SDimitry Andric return true;
43853ca95b02SDimitry Andric
43867d523365SDimitry Andric default:
43877d523365SDimitry Andric return false;
43887d523365SDimitry Andric }
43897d523365SDimitry Andric }
43907d523365SDimitry Andric
getGuaranteedNonFullPoisonOp(const Instruction * I)43917d523365SDimitry Andric const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) {
43927d523365SDimitry Andric switch (I->getOpcode()) {
43937d523365SDimitry Andric case Instruction::Store:
43947d523365SDimitry Andric return cast<StoreInst>(I)->getPointerOperand();
43957d523365SDimitry Andric
43967d523365SDimitry Andric case Instruction::Load:
43977d523365SDimitry Andric return cast<LoadInst>(I)->getPointerOperand();
43987d523365SDimitry Andric
43997d523365SDimitry Andric case Instruction::AtomicCmpXchg:
44007d523365SDimitry Andric return cast<AtomicCmpXchgInst>(I)->getPointerOperand();
44017d523365SDimitry Andric
44027d523365SDimitry Andric case Instruction::AtomicRMW:
44037d523365SDimitry Andric return cast<AtomicRMWInst>(I)->getPointerOperand();
44047d523365SDimitry Andric
44057d523365SDimitry Andric case Instruction::UDiv:
44067d523365SDimitry Andric case Instruction::SDiv:
44077d523365SDimitry Andric case Instruction::URem:
44087d523365SDimitry Andric case Instruction::SRem:
44097d523365SDimitry Andric return I->getOperand(1);
44107d523365SDimitry Andric
44117d523365SDimitry Andric default:
44127d523365SDimitry Andric return nullptr;
44137d523365SDimitry Andric }
44147d523365SDimitry Andric }
44157d523365SDimitry Andric
mustTriggerUB(const Instruction * I,const SmallSet<const Value *,16> & KnownPoison)4416*72ccea31SDimitry Andric bool llvm::mustTriggerUB(const Instruction *I,
4417*72ccea31SDimitry Andric const SmallSet<const Value *, 16>& KnownPoison) {
4418*72ccea31SDimitry Andric auto *NotPoison = getGuaranteedNonFullPoisonOp(I);
4419*72ccea31SDimitry Andric return (NotPoison && KnownPoison.count(NotPoison));
4420*72ccea31SDimitry Andric }
4421*72ccea31SDimitry Andric
4422*72ccea31SDimitry Andric
programUndefinedIfFullPoison(const Instruction * PoisonI)4423f37b6182SDimitry Andric bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) {
44247d523365SDimitry Andric // We currently only look for uses of poison values within the same basic
44257d523365SDimitry Andric // block, as that makes it easier to guarantee that the uses will be
44267d523365SDimitry Andric // executed given that PoisonI is executed.
44277d523365SDimitry Andric //
44287d523365SDimitry Andric // FIXME: Expand this to consider uses beyond the same basic block. To do
44297d523365SDimitry Andric // this, look out for the distinction between post-dominance and strong
44307d523365SDimitry Andric // post-dominance.
44317d523365SDimitry Andric const BasicBlock *BB = PoisonI->getParent();
44327d523365SDimitry Andric
44337d523365SDimitry Andric // Set of instructions that we have proved will yield poison if PoisonI
44347d523365SDimitry Andric // does.
44357d523365SDimitry Andric SmallSet<const Value *, 16> YieldsPoison;
44363ca95b02SDimitry Andric SmallSet<const BasicBlock *, 4> Visited;
44377d523365SDimitry Andric YieldsPoison.insert(PoisonI);
44383ca95b02SDimitry Andric Visited.insert(PoisonI->getParent());
44397d523365SDimitry Andric
44403ca95b02SDimitry Andric BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end();
44413ca95b02SDimitry Andric
44423ca95b02SDimitry Andric unsigned Iter = 0;
44433ca95b02SDimitry Andric while (Iter++ < MaxDepth) {
44443ca95b02SDimitry Andric for (auto &I : make_range(Begin, End)) {
44453ca95b02SDimitry Andric if (&I != PoisonI) {
4446*72ccea31SDimitry Andric if (mustTriggerUB(&I, YieldsPoison))
44473ca95b02SDimitry Andric return true;
44483ca95b02SDimitry Andric if (!isGuaranteedToTransferExecutionToSuccessor(&I))
44497d523365SDimitry Andric return false;
44507d523365SDimitry Andric }
44517d523365SDimitry Andric
44527d523365SDimitry Andric // Mark poison that propagates from I through uses of I.
44533ca95b02SDimitry Andric if (YieldsPoison.count(&I)) {
44543ca95b02SDimitry Andric for (const User *User : I.users()) {
44557d523365SDimitry Andric const Instruction *UserI = cast<Instruction>(User);
44563ca95b02SDimitry Andric if (propagatesFullPoison(UserI))
44577d523365SDimitry Andric YieldsPoison.insert(User);
44587d523365SDimitry Andric }
44597d523365SDimitry Andric }
44607d523365SDimitry Andric }
44613ca95b02SDimitry Andric
44623ca95b02SDimitry Andric if (auto *NextBB = BB->getSingleSuccessor()) {
44633ca95b02SDimitry Andric if (Visited.insert(NextBB).second) {
44643ca95b02SDimitry Andric BB = NextBB;
44653ca95b02SDimitry Andric Begin = BB->getFirstNonPHI()->getIterator();
44663ca95b02SDimitry Andric End = BB->end();
44673ca95b02SDimitry Andric continue;
44683ca95b02SDimitry Andric }
44693ca95b02SDimitry Andric }
44703ca95b02SDimitry Andric
44713ca95b02SDimitry Andric break;
44722cab237bSDimitry Andric }
44737d523365SDimitry Andric return false;
44747d523365SDimitry Andric }
44757d523365SDimitry Andric
isKnownNonNaN(const Value * V,FastMathFlags FMF)4476d88c1a5aSDimitry Andric static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
44777d523365SDimitry Andric if (FMF.noNaNs())
44787d523365SDimitry Andric return true;
44797d523365SDimitry Andric
44807d523365SDimitry Andric if (auto *C = dyn_cast<ConstantFP>(V))
44817d523365SDimitry Andric return !C->isNaN();
4482b5893f02SDimitry Andric
4483b5893f02SDimitry Andric if (auto *C = dyn_cast<ConstantDataVector>(V)) {
4484b5893f02SDimitry Andric if (!C->getElementType()->isFloatingPointTy())
4485b5893f02SDimitry Andric return false;
4486b5893f02SDimitry Andric for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
4487b5893f02SDimitry Andric if (C->getElementAsAPFloat(I).isNaN())
4488b5893f02SDimitry Andric return false;
4489b5893f02SDimitry Andric }
4490b5893f02SDimitry Andric return true;
4491b5893f02SDimitry Andric }
4492b5893f02SDimitry Andric
44937d523365SDimitry Andric return false;
44947d523365SDimitry Andric }
44957d523365SDimitry Andric
isKnownNonZero(const Value * V)4496d88c1a5aSDimitry Andric static bool isKnownNonZero(const Value *V) {
44977d523365SDimitry Andric if (auto *C = dyn_cast<ConstantFP>(V))
44987d523365SDimitry Andric return !C->isZero();
4499b5893f02SDimitry Andric
4500b5893f02SDimitry Andric if (auto *C = dyn_cast<ConstantDataVector>(V)) {
4501b5893f02SDimitry Andric if (!C->getElementType()->isFloatingPointTy())
4502b5893f02SDimitry Andric return false;
4503b5893f02SDimitry Andric for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
4504b5893f02SDimitry Andric if (C->getElementAsAPFloat(I).isZero())
4505b5893f02SDimitry Andric return false;
4506b5893f02SDimitry Andric }
4507b5893f02SDimitry Andric return true;
4508b5893f02SDimitry Andric }
4509b5893f02SDimitry Andric
45107d523365SDimitry Andric return false;
45117d523365SDimitry Andric }
45127d523365SDimitry Andric
45132cab237bSDimitry Andric /// Match clamp pattern for float types without care about NaNs or signed zeros.
45142cab237bSDimitry Andric /// Given non-min/max outer cmp/select from the clamp pattern this
45152cab237bSDimitry Andric /// function recognizes if it can be substitued by a "canonical" min/max
45162cab237bSDimitry Andric /// pattern.
matchFastFloatClamp(CmpInst::Predicate Pred,Value * CmpLHS,Value * CmpRHS,Value * TrueVal,Value * FalseVal,Value * & LHS,Value * & RHS)45172cab237bSDimitry Andric static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred,
4518d88c1a5aSDimitry Andric Value *CmpLHS, Value *CmpRHS,
4519d88c1a5aSDimitry Andric Value *TrueVal, Value *FalseVal,
4520d88c1a5aSDimitry Andric Value *&LHS, Value *&RHS) {
45212cab237bSDimitry Andric // Try to match
45222cab237bSDimitry Andric // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
45232cab237bSDimitry Andric // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
45242cab237bSDimitry Andric // and return description of the outer Max/Min.
45252cab237bSDimitry Andric
45262cab237bSDimitry Andric // First, check if select has inverse order:
45272cab237bSDimitry Andric if (CmpRHS == FalseVal) {
45282cab237bSDimitry Andric std::swap(TrueVal, FalseVal);
45292cab237bSDimitry Andric Pred = CmpInst::getInversePredicate(Pred);
45302cab237bSDimitry Andric }
45312cab237bSDimitry Andric
45322cab237bSDimitry Andric // Assume success now. If there's no match, callers should not use these anyway.
45337a7e6055SDimitry Andric LHS = TrueVal;
45347a7e6055SDimitry Andric RHS = FalseVal;
45357a7e6055SDimitry Andric
45362cab237bSDimitry Andric const APFloat *FC1;
45372cab237bSDimitry Andric if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
45382cab237bSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
45392cab237bSDimitry Andric
45402cab237bSDimitry Andric const APFloat *FC2;
45412cab237bSDimitry Andric switch (Pred) {
45422cab237bSDimitry Andric case CmpInst::FCMP_OLT:
45432cab237bSDimitry Andric case CmpInst::FCMP_OLE:
45442cab237bSDimitry Andric case CmpInst::FCMP_ULT:
45452cab237bSDimitry Andric case CmpInst::FCMP_ULE:
45462cab237bSDimitry Andric if (match(FalseVal,
45472cab237bSDimitry Andric m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)),
45482cab237bSDimitry Andric m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
45492cab237bSDimitry Andric FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan)
45502cab237bSDimitry Andric return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
45512cab237bSDimitry Andric break;
45522cab237bSDimitry Andric case CmpInst::FCMP_OGT:
45532cab237bSDimitry Andric case CmpInst::FCMP_OGE:
45542cab237bSDimitry Andric case CmpInst::FCMP_UGT:
45552cab237bSDimitry Andric case CmpInst::FCMP_UGE:
45562cab237bSDimitry Andric if (match(FalseVal,
45572cab237bSDimitry Andric m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)),
45582cab237bSDimitry Andric m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
45592cab237bSDimitry Andric FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan)
45602cab237bSDimitry Andric return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
45612cab237bSDimitry Andric break;
45622cab237bSDimitry Andric default:
45632cab237bSDimitry Andric break;
45642cab237bSDimitry Andric }
45652cab237bSDimitry Andric
45662cab237bSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
45672cab237bSDimitry Andric }
45682cab237bSDimitry Andric
45692cab237bSDimitry Andric /// Recognize variations of:
45702cab237bSDimitry Andric /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
matchClamp(CmpInst::Predicate Pred,Value * CmpLHS,Value * CmpRHS,Value * TrueVal,Value * FalseVal)45712cab237bSDimitry Andric static SelectPatternResult matchClamp(CmpInst::Predicate Pred,
45722cab237bSDimitry Andric Value *CmpLHS, Value *CmpRHS,
45732cab237bSDimitry Andric Value *TrueVal, Value *FalseVal) {
45742cab237bSDimitry Andric // Swap the select operands and predicate to match the patterns below.
45752cab237bSDimitry Andric if (CmpRHS != TrueVal) {
45762cab237bSDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred);
45772cab237bSDimitry Andric std::swap(TrueVal, FalseVal);
45782cab237bSDimitry Andric }
45797a7e6055SDimitry Andric const APInt *C1;
45807a7e6055SDimitry Andric if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
45817a7e6055SDimitry Andric const APInt *C2;
45827a7e6055SDimitry Andric // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
45837a7e6055SDimitry Andric if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
45847a7e6055SDimitry Andric C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
45857a7e6055SDimitry Andric return {SPF_SMAX, SPNB_NA, false};
45867a7e6055SDimitry Andric
45877a7e6055SDimitry Andric // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
45887a7e6055SDimitry Andric if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
45897a7e6055SDimitry Andric C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
45907a7e6055SDimitry Andric return {SPF_SMIN, SPNB_NA, false};
45917a7e6055SDimitry Andric
45927a7e6055SDimitry Andric // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
45937a7e6055SDimitry Andric if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
45947a7e6055SDimitry Andric C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
45957a7e6055SDimitry Andric return {SPF_UMAX, SPNB_NA, false};
45967a7e6055SDimitry Andric
45977a7e6055SDimitry Andric // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
45987a7e6055SDimitry Andric if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
45997a7e6055SDimitry Andric C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
46007a7e6055SDimitry Andric return {SPF_UMIN, SPNB_NA, false};
46017a7e6055SDimitry Andric }
46022cab237bSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
46032cab237bSDimitry Andric }
46042cab237bSDimitry Andric
460530785c0eSDimitry Andric /// Recognize variations of:
460630785c0eSDimitry Andric /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
matchMinMaxOfMinMax(CmpInst::Predicate Pred,Value * CmpLHS,Value * CmpRHS,Value * TVal,Value * FVal,unsigned Depth)460730785c0eSDimitry Andric static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,
460830785c0eSDimitry Andric Value *CmpLHS, Value *CmpRHS,
4609842d113bSDimitry Andric Value *TVal, Value *FVal,
4610842d113bSDimitry Andric unsigned Depth) {
461130785c0eSDimitry Andric // TODO: Allow FP min/max with nnan/nsz.
461230785c0eSDimitry Andric assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
461330785c0eSDimitry Andric
461430785c0eSDimitry Andric Value *A, *B;
4615842d113bSDimitry Andric SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
461630785c0eSDimitry Andric if (!SelectPatternResult::isMinOrMax(L.Flavor))
461730785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
461830785c0eSDimitry Andric
461930785c0eSDimitry Andric Value *C, *D;
4620842d113bSDimitry Andric SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
462130785c0eSDimitry Andric if (L.Flavor != R.Flavor)
462230785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
462330785c0eSDimitry Andric
46244ba319b5SDimitry Andric // We have something like: x Pred y ? min(a, b) : min(c, d).
46254ba319b5SDimitry Andric // Try to match the compare to the min/max operations of the select operands.
46264ba319b5SDimitry Andric // First, make sure we have the right compare predicate.
462730785c0eSDimitry Andric switch (L.Flavor) {
462830785c0eSDimitry Andric case SPF_SMIN:
462930785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
463030785c0eSDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred);
463130785c0eSDimitry Andric std::swap(CmpLHS, CmpRHS);
463230785c0eSDimitry Andric }
463330785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
463430785c0eSDimitry Andric break;
463530785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
463630785c0eSDimitry Andric case SPF_SMAX:
463730785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
463830785c0eSDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred);
463930785c0eSDimitry Andric std::swap(CmpLHS, CmpRHS);
464030785c0eSDimitry Andric }
464130785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
464230785c0eSDimitry Andric break;
464330785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
464430785c0eSDimitry Andric case SPF_UMIN:
464530785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
464630785c0eSDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred);
464730785c0eSDimitry Andric std::swap(CmpLHS, CmpRHS);
464830785c0eSDimitry Andric }
464930785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
465030785c0eSDimitry Andric break;
465130785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
465230785c0eSDimitry Andric case SPF_UMAX:
465330785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
465430785c0eSDimitry Andric Pred = ICmpInst::getSwappedPredicate(Pred);
465530785c0eSDimitry Andric std::swap(CmpLHS, CmpRHS);
465630785c0eSDimitry Andric }
465730785c0eSDimitry Andric if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
465830785c0eSDimitry Andric break;
465930785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
466030785c0eSDimitry Andric default:
46616bee06efSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
466230785c0eSDimitry Andric }
466330785c0eSDimitry Andric
46644ba319b5SDimitry Andric // If there is a common operand in the already matched min/max and the other
46654ba319b5SDimitry Andric // min/max operands match the compare operands (either directly or inverted),
46664ba319b5SDimitry Andric // then this is min/max of the same flavor.
46674ba319b5SDimitry Andric
466830785c0eSDimitry Andric // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
46694ba319b5SDimitry Andric // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
46704ba319b5SDimitry Andric if (D == B) {
46714ba319b5SDimitry Andric if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
46724ba319b5SDimitry Andric match(A, m_Not(m_Specific(CmpRHS)))))
467330785c0eSDimitry Andric return {L.Flavor, SPNB_NA, false};
46744ba319b5SDimitry Andric }
467530785c0eSDimitry Andric // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
46764ba319b5SDimitry Andric // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
46774ba319b5SDimitry Andric if (C == B) {
46784ba319b5SDimitry Andric if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
46794ba319b5SDimitry Andric match(A, m_Not(m_Specific(CmpRHS)))))
468030785c0eSDimitry Andric return {L.Flavor, SPNB_NA, false};
46814ba319b5SDimitry Andric }
468230785c0eSDimitry Andric // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
46834ba319b5SDimitry Andric // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
46844ba319b5SDimitry Andric if (D == A) {
46854ba319b5SDimitry Andric if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
46864ba319b5SDimitry Andric match(B, m_Not(m_Specific(CmpRHS)))))
468730785c0eSDimitry Andric return {L.Flavor, SPNB_NA, false};
46884ba319b5SDimitry Andric }
468930785c0eSDimitry Andric // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
46904ba319b5SDimitry Andric // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
46914ba319b5SDimitry Andric if (C == A) {
46924ba319b5SDimitry Andric if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
46934ba319b5SDimitry Andric match(B, m_Not(m_Specific(CmpRHS)))))
469430785c0eSDimitry Andric return {L.Flavor, SPNB_NA, false};
46954ba319b5SDimitry Andric }
469630785c0eSDimitry Andric
469730785c0eSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
469830785c0eSDimitry Andric }
469930785c0eSDimitry Andric
47002cab237bSDimitry Andric /// Match non-obvious integer minimum and maximum sequences.
matchMinMax(CmpInst::Predicate Pred,Value * CmpLHS,Value * CmpRHS,Value * TrueVal,Value * FalseVal,Value * & LHS,Value * & RHS,unsigned Depth)47012cab237bSDimitry Andric static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
47022cab237bSDimitry Andric Value *CmpLHS, Value *CmpRHS,
47032cab237bSDimitry Andric Value *TrueVal, Value *FalseVal,
4704842d113bSDimitry Andric Value *&LHS, Value *&RHS,
4705842d113bSDimitry Andric unsigned Depth) {
47062cab237bSDimitry Andric // Assume success. If there's no match, callers should not use these anyway.
47072cab237bSDimitry Andric LHS = TrueVal;
47082cab237bSDimitry Andric RHS = FalseVal;
47092cab237bSDimitry Andric
47102cab237bSDimitry Andric SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
47112cab237bSDimitry Andric if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
47122cab237bSDimitry Andric return SPR;
47137a7e6055SDimitry Andric
4714842d113bSDimitry Andric SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
471530785c0eSDimitry Andric if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
471630785c0eSDimitry Andric return SPR;
471730785c0eSDimitry Andric
4718d88c1a5aSDimitry Andric if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
4719d88c1a5aSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
4720d88c1a5aSDimitry Andric
4721d88c1a5aSDimitry Andric // Z = X -nsw Y
4722d88c1a5aSDimitry Andric // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0)
4723d88c1a5aSDimitry Andric // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0)
4724d88c1a5aSDimitry Andric if (match(TrueVal, m_Zero()) &&
47257a7e6055SDimitry Andric match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
4726d88c1a5aSDimitry Andric return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
4727d88c1a5aSDimitry Andric
4728d88c1a5aSDimitry Andric // Z = X -nsw Y
4729d88c1a5aSDimitry Andric // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0)
4730d88c1a5aSDimitry Andric // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0)
4731d88c1a5aSDimitry Andric if (match(FalseVal, m_Zero()) &&
47327a7e6055SDimitry Andric match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
4733d88c1a5aSDimitry Andric return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
4734d88c1a5aSDimitry Andric
47352cab237bSDimitry Andric const APInt *C1;
4736d88c1a5aSDimitry Andric if (!match(CmpRHS, m_APInt(C1)))
4737d88c1a5aSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
4738d88c1a5aSDimitry Andric
4739d88c1a5aSDimitry Andric // An unsigned min/max can be written with a signed compare.
4740d88c1a5aSDimitry Andric const APInt *C2;
4741d88c1a5aSDimitry Andric if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
4742d88c1a5aSDimitry Andric (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
4743d88c1a5aSDimitry Andric // Is the sign bit set?
4744d88c1a5aSDimitry Andric // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
4745d88c1a5aSDimitry Andric // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
47462cab237bSDimitry Andric if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() &&
47472cab237bSDimitry Andric C2->isMaxSignedValue())
4748d88c1a5aSDimitry Andric return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
4749d88c1a5aSDimitry Andric
4750d88c1a5aSDimitry Andric // Is the sign bit clear?
4751d88c1a5aSDimitry Andric // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
4752d88c1a5aSDimitry Andric // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
4753d88c1a5aSDimitry Andric if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() &&
47547a7e6055SDimitry Andric C2->isMinSignedValue())
4755d88c1a5aSDimitry Andric return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
4756d88c1a5aSDimitry Andric }
4757d88c1a5aSDimitry Andric
4758d88c1a5aSDimitry Andric // Look through 'not' ops to find disguised signed min/max.
4759d88c1a5aSDimitry Andric // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C)
4760d88c1a5aSDimitry Andric // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C)
4761d88c1a5aSDimitry Andric if (match(TrueVal, m_Not(m_Specific(CmpLHS))) &&
47627a7e6055SDimitry Andric match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2)
4763d88c1a5aSDimitry Andric return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
4764d88c1a5aSDimitry Andric
4765d88c1a5aSDimitry Andric // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X)
4766d88c1a5aSDimitry Andric // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X)
4767d88c1a5aSDimitry Andric if (match(FalseVal, m_Not(m_Specific(CmpLHS))) &&
47687a7e6055SDimitry Andric match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2)
4769d88c1a5aSDimitry Andric return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
4770d88c1a5aSDimitry Andric
4771d88c1a5aSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
4772d88c1a5aSDimitry Andric }
4773d88c1a5aSDimitry Andric
isKnownNegation(const Value * X,const Value * Y,bool NeedNSW)47744ba319b5SDimitry Andric bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) {
47754ba319b5SDimitry Andric assert(X && Y && "Invalid operand");
47764ba319b5SDimitry Andric
47774ba319b5SDimitry Andric // X = sub (0, Y) || X = sub nsw (0, Y)
47784ba319b5SDimitry Andric if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) ||
47794ba319b5SDimitry Andric (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y)))))
47804ba319b5SDimitry Andric return true;
47814ba319b5SDimitry Andric
47824ba319b5SDimitry Andric // Y = sub (0, X) || Y = sub nsw (0, X)
47834ba319b5SDimitry Andric if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) ||
47844ba319b5SDimitry Andric (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X)))))
47854ba319b5SDimitry Andric return true;
47864ba319b5SDimitry Andric
47874ba319b5SDimitry Andric // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
47884ba319b5SDimitry Andric Value *A, *B;
47894ba319b5SDimitry Andric return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
47904ba319b5SDimitry Andric match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
47914ba319b5SDimitry Andric (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
47924ba319b5SDimitry Andric match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
47934ba319b5SDimitry Andric }
47944ba319b5SDimitry Andric
matchSelectPattern(CmpInst::Predicate Pred,FastMathFlags FMF,Value * CmpLHS,Value * CmpRHS,Value * TrueVal,Value * FalseVal,Value * & LHS,Value * & RHS,unsigned Depth)47957d523365SDimitry Andric static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
47967d523365SDimitry Andric FastMathFlags FMF,
4797ff0cc061SDimitry Andric Value *CmpLHS, Value *CmpRHS,
4798ff0cc061SDimitry Andric Value *TrueVal, Value *FalseVal,
4799842d113bSDimitry Andric Value *&LHS, Value *&RHS,
4800842d113bSDimitry Andric unsigned Depth) {
4801b5893f02SDimitry Andric if (CmpInst::isFPPredicate(Pred)) {
4802b5893f02SDimitry Andric // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
4803b5893f02SDimitry Andric // 0.0 operand, set the compare's 0.0 operands to that same value for the
4804b5893f02SDimitry Andric // purpose of identifying min/max. Disregard vector constants with undefined
4805b5893f02SDimitry Andric // elements because those can not be back-propagated for analysis.
4806b5893f02SDimitry Andric Value *OutputZeroVal = nullptr;
4807b5893f02SDimitry Andric if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
4808b5893f02SDimitry Andric !cast<Constant>(TrueVal)->containsUndefElement())
4809b5893f02SDimitry Andric OutputZeroVal = TrueVal;
4810b5893f02SDimitry Andric else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
4811b5893f02SDimitry Andric !cast<Constant>(FalseVal)->containsUndefElement())
4812b5893f02SDimitry Andric OutputZeroVal = FalseVal;
4813b5893f02SDimitry Andric
4814b5893f02SDimitry Andric if (OutputZeroVal) {
4815b5893f02SDimitry Andric if (match(CmpLHS, m_AnyZeroFP()))
4816b5893f02SDimitry Andric CmpLHS = OutputZeroVal;
4817b5893f02SDimitry Andric if (match(CmpRHS, m_AnyZeroFP()))
4818b5893f02SDimitry Andric CmpRHS = OutputZeroVal;
4819b5893f02SDimitry Andric }
4820b5893f02SDimitry Andric }
4821b5893f02SDimitry Andric
4822ff0cc061SDimitry Andric LHS = CmpLHS;
4823ff0cc061SDimitry Andric RHS = CmpRHS;
4824ff0cc061SDimitry Andric
4825fe4fed2eSDimitry Andric // Signed zero may return inconsistent results between implementations.
48267d523365SDimitry Andric // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
48277d523365SDimitry Andric // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
4828fe4fed2eSDimitry Andric // Therefore, we behave conservatively and only proceed if at least one of the
4829fe4fed2eSDimitry Andric // operands is known to not be zero or if we don't care about signed zero.
4830ff0cc061SDimitry Andric switch (Pred) {
48317d523365SDimitry Andric default: break;
4832fe4fed2eSDimitry Andric // FIXME: Include OGT/OLT/UGT/ULT.
48337d523365SDimitry Andric case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
48347d523365SDimitry Andric case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
48357d523365SDimitry Andric if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
48367d523365SDimitry Andric !isKnownNonZero(CmpRHS))
48377d523365SDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
48387d523365SDimitry Andric }
48397d523365SDimitry Andric
48407d523365SDimitry Andric SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
48417d523365SDimitry Andric bool Ordered = false;
48427d523365SDimitry Andric
48437d523365SDimitry Andric // When given one NaN and one non-NaN input:
48447d523365SDimitry Andric // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
48457d523365SDimitry Andric // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
48467d523365SDimitry Andric // ordered comparison fails), which could be NaN or non-NaN.
48477d523365SDimitry Andric // so here we discover exactly what NaN behavior is required/accepted.
48487d523365SDimitry Andric if (CmpInst::isFPPredicate(Pred)) {
48497d523365SDimitry Andric bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
48507d523365SDimitry Andric bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
48517d523365SDimitry Andric
48527d523365SDimitry Andric if (LHSSafe && RHSSafe) {
48537d523365SDimitry Andric // Both operands are known non-NaN.
48547d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_ANY;
48557d523365SDimitry Andric } else if (CmpInst::isOrdered(Pred)) {
48567d523365SDimitry Andric // An ordered comparison will return false when given a NaN, so it
48577d523365SDimitry Andric // returns the RHS.
48587d523365SDimitry Andric Ordered = true;
48597d523365SDimitry Andric if (LHSSafe)
48607d523365SDimitry Andric // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
48617d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_NAN;
48627d523365SDimitry Andric else if (RHSSafe)
48637d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_OTHER;
48647d523365SDimitry Andric else
48657d523365SDimitry Andric // Completely unsafe.
48667d523365SDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
48677d523365SDimitry Andric } else {
48687d523365SDimitry Andric Ordered = false;
48697d523365SDimitry Andric // An unordered comparison will return true when given a NaN, so it
48707d523365SDimitry Andric // returns the LHS.
48717d523365SDimitry Andric if (LHSSafe)
48727d523365SDimitry Andric // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
48737d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_OTHER;
48747d523365SDimitry Andric else if (RHSSafe)
48757d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_NAN;
48767d523365SDimitry Andric else
48777d523365SDimitry Andric // Completely unsafe.
48787d523365SDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
4879ff0cc061SDimitry Andric }
4880ff0cc061SDimitry Andric }
4881ff0cc061SDimitry Andric
4882ff0cc061SDimitry Andric if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
48837d523365SDimitry Andric std::swap(CmpLHS, CmpRHS);
48847d523365SDimitry Andric Pred = CmpInst::getSwappedPredicate(Pred);
48857d523365SDimitry Andric if (NaNBehavior == SPNB_RETURNS_NAN)
48867d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_OTHER;
48877d523365SDimitry Andric else if (NaNBehavior == SPNB_RETURNS_OTHER)
48887d523365SDimitry Andric NaNBehavior = SPNB_RETURNS_NAN;
48897d523365SDimitry Andric Ordered = !Ordered;
48907d523365SDimitry Andric }
48917d523365SDimitry Andric
48927d523365SDimitry Andric // ([if]cmp X, Y) ? X : Y
48937d523365SDimitry Andric if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
4894ff0cc061SDimitry Andric switch (Pred) {
48957d523365SDimitry Andric default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
4896ff0cc061SDimitry Andric case ICmpInst::ICMP_UGT:
48977d523365SDimitry Andric case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
4898ff0cc061SDimitry Andric case ICmpInst::ICMP_SGT:
48997d523365SDimitry Andric case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
4900ff0cc061SDimitry Andric case ICmpInst::ICMP_ULT:
49017d523365SDimitry Andric case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
4902ff0cc061SDimitry Andric case ICmpInst::ICMP_SLT:
49037d523365SDimitry Andric case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
49047d523365SDimitry Andric case FCmpInst::FCMP_UGT:
49057d523365SDimitry Andric case FCmpInst::FCMP_UGE:
49067d523365SDimitry Andric case FCmpInst::FCMP_OGT:
49077d523365SDimitry Andric case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
49087d523365SDimitry Andric case FCmpInst::FCMP_ULT:
49097d523365SDimitry Andric case FCmpInst::FCMP_ULE:
49107d523365SDimitry Andric case FCmpInst::FCMP_OLT:
49117d523365SDimitry Andric case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
4912ff0cc061SDimitry Andric }
4913ff0cc061SDimitry Andric }
4914ff0cc061SDimitry Andric
49154ba319b5SDimitry Andric if (isKnownNegation(TrueVal, FalseVal)) {
49164ba319b5SDimitry Andric // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
49174ba319b5SDimitry Andric // match against either LHS or sext(LHS).
49184ba319b5SDimitry Andric auto MaybeSExtCmpLHS =
49194ba319b5SDimitry Andric m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
49204ba319b5SDimitry Andric auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
49214ba319b5SDimitry Andric auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
49224ba319b5SDimitry Andric if (match(TrueVal, MaybeSExtCmpLHS)) {
49234ba319b5SDimitry Andric // Set the return values. If the compare uses the negated value (-X >s 0),
49244ba319b5SDimitry Andric // swap the return values because the negated value is always 'RHS'.
49254ba319b5SDimitry Andric LHS = TrueVal;
49264ba319b5SDimitry Andric RHS = FalseVal;
49274ba319b5SDimitry Andric if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
49284ba319b5SDimitry Andric std::swap(LHS, RHS);
4929ff0cc061SDimitry Andric
49304ba319b5SDimitry Andric // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
49314ba319b5SDimitry Andric // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
49324ba319b5SDimitry Andric if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
49334ba319b5SDimitry Andric return {SPF_ABS, SPNB_NA, false};
4934ff0cc061SDimitry Andric
49354ba319b5SDimitry Andric // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
49364ba319b5SDimitry Andric // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
49374ba319b5SDimitry Andric if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
49384ba319b5SDimitry Andric return {SPF_NABS, SPNB_NA, false};
4939ff0cc061SDimitry Andric }
49404ba319b5SDimitry Andric else if (match(FalseVal, MaybeSExtCmpLHS)) {
49414ba319b5SDimitry Andric // Set the return values. If the compare uses the negated value (-X >s 0),
49424ba319b5SDimitry Andric // swap the return values because the negated value is always 'RHS'.
49434ba319b5SDimitry Andric LHS = FalseVal;
49444ba319b5SDimitry Andric RHS = TrueVal;
49454ba319b5SDimitry Andric if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
49464ba319b5SDimitry Andric std::swap(LHS, RHS);
49474ba319b5SDimitry Andric
49484ba319b5SDimitry Andric // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
49494ba319b5SDimitry Andric // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
49504ba319b5SDimitry Andric if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
49514ba319b5SDimitry Andric return {SPF_NABS, SPNB_NA, false};
49524ba319b5SDimitry Andric
49534ba319b5SDimitry Andric // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
49544ba319b5SDimitry Andric // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
49554ba319b5SDimitry Andric if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
49564ba319b5SDimitry Andric return {SPF_ABS, SPNB_NA, false};
4957ff0cc061SDimitry Andric }
4958ff0cc061SDimitry Andric }
4959ff0cc061SDimitry Andric
49602cab237bSDimitry Andric if (CmpInst::isIntPredicate(Pred))
4961842d113bSDimitry Andric return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
49622cab237bSDimitry Andric
49632cab237bSDimitry Andric // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
49642cab237bSDimitry Andric // may return either -0.0 or 0.0, so fcmp/select pair has stricter
49652cab237bSDimitry Andric // semantics than minNum. Be conservative in such case.
49662cab237bSDimitry Andric if (NaNBehavior != SPNB_RETURNS_ANY ||
49672cab237bSDimitry Andric (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
49682cab237bSDimitry Andric !isKnownNonZero(CmpRHS)))
49692cab237bSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
49702cab237bSDimitry Andric
49712cab237bSDimitry Andric return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
4972ff0cc061SDimitry Andric }
4973ff0cc061SDimitry Andric
49742cab237bSDimitry Andric /// Helps to match a select pattern in case of a type mismatch.
49752cab237bSDimitry Andric ///
49762cab237bSDimitry Andric /// The function processes the case when type of true and false values of a
49772cab237bSDimitry Andric /// select instruction differs from type of the cmp instruction operands because
49784ba319b5SDimitry Andric /// of a cast instruction. The function checks if it is legal to move the cast
49792cab237bSDimitry Andric /// operation after "select". If yes, it returns the new second value of
49802cab237bSDimitry Andric /// "select" (with the assumption that cast is moved):
49812cab237bSDimitry Andric /// 1. As operand of cast instruction when both values of "select" are same cast
49822cab237bSDimitry Andric /// instructions.
49832cab237bSDimitry Andric /// 2. As restored constant (by applying reverse cast operation) when the first
49842cab237bSDimitry Andric /// value of the "select" is a cast operation and the second value is a
49852cab237bSDimitry Andric /// constant.
49862cab237bSDimitry Andric /// NOTE: We return only the new second value because the first value could be
49872cab237bSDimitry Andric /// accessed as operand of cast instruction.
lookThroughCast(CmpInst * CmpI,Value * V1,Value * V2,Instruction::CastOps * CastOp)49887d523365SDimitry Andric static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
4989ff0cc061SDimitry Andric Instruction::CastOps *CastOp) {
49907a7e6055SDimitry Andric auto *Cast1 = dyn_cast<CastInst>(V1);
49917a7e6055SDimitry Andric if (!Cast1)
4992ff0cc061SDimitry Andric return nullptr;
4993ff0cc061SDimitry Andric
49947a7e6055SDimitry Andric *CastOp = Cast1->getOpcode();
49957a7e6055SDimitry Andric Type *SrcTy = Cast1->getSrcTy();
49967a7e6055SDimitry Andric if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
49977a7e6055SDimitry Andric // If V1 and V2 are both the same cast from the same type, look through V1.
49987a7e6055SDimitry Andric if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
49997a7e6055SDimitry Andric return Cast2->getOperand(0);
50007d523365SDimitry Andric return nullptr;
50017d523365SDimitry Andric }
50027d523365SDimitry Andric
50037a7e6055SDimitry Andric auto *C = dyn_cast<Constant>(V2);
50047a7e6055SDimitry Andric if (!C)
50057a7e6055SDimitry Andric return nullptr;
50067a7e6055SDimitry Andric
50073ca95b02SDimitry Andric Constant *CastedTo = nullptr;
50087a7e6055SDimitry Andric switch (*CastOp) {
50097a7e6055SDimitry Andric case Instruction::ZExt:
50107a7e6055SDimitry Andric if (CmpI->isUnsigned())
50117a7e6055SDimitry Andric CastedTo = ConstantExpr::getTrunc(C, SrcTy);
50127a7e6055SDimitry Andric break;
50137a7e6055SDimitry Andric case Instruction::SExt:
50147a7e6055SDimitry Andric if (CmpI->isSigned())
50157a7e6055SDimitry Andric CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
50167a7e6055SDimitry Andric break;
50177a7e6055SDimitry Andric case Instruction::Trunc:
50182cab237bSDimitry Andric Constant *CmpConst;
50192cab237bSDimitry Andric if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
50202cab237bSDimitry Andric CmpConst->getType() == SrcTy) {
50212cab237bSDimitry Andric // Here we have the following case:
50222cab237bSDimitry Andric //
50232cab237bSDimitry Andric // %cond = cmp iN %x, CmpConst
50242cab237bSDimitry Andric // %tr = trunc iN %x to iK
50252cab237bSDimitry Andric // %narrowsel = select i1 %cond, iK %t, iK C
50262cab237bSDimitry Andric //
50272cab237bSDimitry Andric // We can always move trunc after select operation:
50282cab237bSDimitry Andric //
50292cab237bSDimitry Andric // %cond = cmp iN %x, CmpConst
50302cab237bSDimitry Andric // %widesel = select i1 %cond, iN %x, iN CmpConst
50312cab237bSDimitry Andric // %tr = trunc iN %widesel to iK
50322cab237bSDimitry Andric //
50332cab237bSDimitry Andric // Note that C could be extended in any way because we don't care about
50342cab237bSDimitry Andric // upper bits after truncation. It can't be abs pattern, because it would
50352cab237bSDimitry Andric // look like:
50362cab237bSDimitry Andric //
50372cab237bSDimitry Andric // select i1 %cond, x, -x.
50382cab237bSDimitry Andric //
50392cab237bSDimitry Andric // So only min/max pattern could be matched. Such match requires widened C
50402cab237bSDimitry Andric // == CmpConst. That is why set widened C = CmpConst, condition trunc
50412cab237bSDimitry Andric // CmpConst == C is checked below.
50422cab237bSDimitry Andric CastedTo = CmpConst;
50432cab237bSDimitry Andric } else {
50447a7e6055SDimitry Andric CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned());
50452cab237bSDimitry Andric }
50467a7e6055SDimitry Andric break;
50477a7e6055SDimitry Andric case Instruction::FPTrunc:
50487a7e6055SDimitry Andric CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true);
50497a7e6055SDimitry Andric break;
50507a7e6055SDimitry Andric case Instruction::FPExt:
50517a7e6055SDimitry Andric CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true);
50527a7e6055SDimitry Andric break;
50537a7e6055SDimitry Andric case Instruction::FPToUI:
50547a7e6055SDimitry Andric CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true);
50557a7e6055SDimitry Andric break;
50567a7e6055SDimitry Andric case Instruction::FPToSI:
50577a7e6055SDimitry Andric CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true);
50587a7e6055SDimitry Andric break;
50597a7e6055SDimitry Andric case Instruction::UIToFP:
50607a7e6055SDimitry Andric CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true);
50617a7e6055SDimitry Andric break;
50627a7e6055SDimitry Andric case Instruction::SIToFP:
50637a7e6055SDimitry Andric CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true);
50647a7e6055SDimitry Andric break;
50657a7e6055SDimitry Andric default:
50667a7e6055SDimitry Andric break;
50677a7e6055SDimitry Andric }
50683ca95b02SDimitry Andric
50693ca95b02SDimitry Andric if (!CastedTo)
5070ff0cc061SDimitry Andric return nullptr;
50713ca95b02SDimitry Andric
50723ca95b02SDimitry Andric // Make sure the cast doesn't lose any information.
50737a7e6055SDimitry Andric Constant *CastedBack =
50747a7e6055SDimitry Andric ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true);
50753ca95b02SDimitry Andric if (CastedBack != C)
50763ca95b02SDimitry Andric return nullptr;
50773ca95b02SDimitry Andric
50783ca95b02SDimitry Andric return CastedTo;
5079ff0cc061SDimitry Andric }
5080ff0cc061SDimitry Andric
matchSelectPattern(Value * V,Value * & LHS,Value * & RHS,Instruction::CastOps * CastOp,unsigned Depth)50813ca95b02SDimitry Andric SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
5082842d113bSDimitry Andric Instruction::CastOps *CastOp,
5083842d113bSDimitry Andric unsigned Depth) {
5084842d113bSDimitry Andric if (Depth >= MaxDepth)
5085842d113bSDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
5086842d113bSDimitry Andric
5087ff0cc061SDimitry Andric SelectInst *SI = dyn_cast<SelectInst>(V);
50887d523365SDimitry Andric if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
5089ff0cc061SDimitry Andric
50907d523365SDimitry Andric CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
50917d523365SDimitry Andric if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
5092ff0cc061SDimitry Andric
50937d523365SDimitry Andric CmpInst::Predicate Pred = CmpI->getPredicate();
5094ff0cc061SDimitry Andric Value *CmpLHS = CmpI->getOperand(0);
5095ff0cc061SDimitry Andric Value *CmpRHS = CmpI->getOperand(1);
5096ff0cc061SDimitry Andric Value *TrueVal = SI->getTrueValue();
5097ff0cc061SDimitry Andric Value *FalseVal = SI->getFalseValue();
50987d523365SDimitry Andric FastMathFlags FMF;
50997d523365SDimitry Andric if (isa<FPMathOperator>(CmpI))
51007d523365SDimitry Andric FMF = CmpI->getFastMathFlags();
5101ff0cc061SDimitry Andric
5102ff0cc061SDimitry Andric // Bail out early.
5103ff0cc061SDimitry Andric if (CmpI->isEquality())
51047d523365SDimitry Andric return {SPF_UNKNOWN, SPNB_NA, false};
5105ff0cc061SDimitry Andric
5106ff0cc061SDimitry Andric // Deal with type mismatches.
5107ff0cc061SDimitry Andric if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
5108fe4fed2eSDimitry Andric if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
5109fe4fed2eSDimitry Andric // If this is a potential fmin/fmax with a cast to integer, then ignore
5110fe4fed2eSDimitry Andric // -0.0 because there is no corresponding integer value.
5111fe4fed2eSDimitry Andric if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
5112fe4fed2eSDimitry Andric FMF.setNoSignedZeros();
51137d523365SDimitry Andric return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
5114ff0cc061SDimitry Andric cast<CastInst>(TrueVal)->getOperand(0), C,
5115842d113bSDimitry Andric LHS, RHS, Depth);
5116fe4fed2eSDimitry Andric }
5117fe4fed2eSDimitry Andric if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
5118fe4fed2eSDimitry Andric // If this is a potential fmin/fmax with a cast to integer, then ignore
5119fe4fed2eSDimitry Andric // -0.0 because there is no corresponding integer value.
5120fe4fed2eSDimitry Andric if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
5121fe4fed2eSDimitry Andric FMF.setNoSignedZeros();
51227d523365SDimitry Andric return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
5123ff0cc061SDimitry Andric C, cast<CastInst>(FalseVal)->getOperand(0),
5124842d113bSDimitry Andric LHS, RHS, Depth);
5125ff0cc061SDimitry Andric }
5126fe4fed2eSDimitry Andric }
51277d523365SDimitry Andric return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
5128842d113bSDimitry Andric LHS, RHS, Depth);
5129ff0cc061SDimitry Andric }
51307d523365SDimitry Andric
getMinMaxPred(SelectPatternFlavor SPF,bool Ordered)51314ba319b5SDimitry Andric CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) {
51324ba319b5SDimitry Andric if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
51334ba319b5SDimitry Andric if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
51344ba319b5SDimitry Andric if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
51354ba319b5SDimitry Andric if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
51364ba319b5SDimitry Andric if (SPF == SPF_FMINNUM)
51374ba319b5SDimitry Andric return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
51384ba319b5SDimitry Andric if (SPF == SPF_FMAXNUM)
51394ba319b5SDimitry Andric return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
51404ba319b5SDimitry Andric llvm_unreachable("unhandled!");
51414ba319b5SDimitry Andric }
51424ba319b5SDimitry Andric
getInverseMinMaxFlavor(SelectPatternFlavor SPF)51434ba319b5SDimitry Andric SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {
51444ba319b5SDimitry Andric if (SPF == SPF_SMIN) return SPF_SMAX;
51454ba319b5SDimitry Andric if (SPF == SPF_UMIN) return SPF_UMAX;
51464ba319b5SDimitry Andric if (SPF == SPF_SMAX) return SPF_SMIN;
51474ba319b5SDimitry Andric if (SPF == SPF_UMAX) return SPF_UMIN;
51484ba319b5SDimitry Andric llvm_unreachable("unhandled!");
51494ba319b5SDimitry Andric }
51504ba319b5SDimitry Andric
getInverseMinMaxPred(SelectPatternFlavor SPF)51514ba319b5SDimitry Andric CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) {
51524ba319b5SDimitry Andric return getMinMaxPred(getInverseMinMaxFlavor(SPF));
51534ba319b5SDimitry Andric }
51544ba319b5SDimitry Andric
51557d523365SDimitry Andric /// Return true if "icmp Pred LHS RHS" is always true.
isTruePredicate(CmpInst::Predicate Pred,const Value * LHS,const Value * RHS,const DataLayout & DL,unsigned Depth)51562cab237bSDimitry Andric static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
51572cab237bSDimitry Andric const Value *RHS, const DataLayout &DL,
51582cab237bSDimitry Andric unsigned Depth) {
51597d523365SDimitry Andric assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!");
51607d523365SDimitry Andric if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
51617d523365SDimitry Andric return true;
51627d523365SDimitry Andric
51637d523365SDimitry Andric switch (Pred) {
51647d523365SDimitry Andric default:
51657d523365SDimitry Andric return false;
51667d523365SDimitry Andric
51677d523365SDimitry Andric case CmpInst::ICMP_SLE: {
51687d523365SDimitry Andric const APInt *C;
51697d523365SDimitry Andric
51707d523365SDimitry Andric // LHS s<= LHS +_{nsw} C if C >= 0
51717d523365SDimitry Andric if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))))
51727d523365SDimitry Andric return !C->isNegative();
51737d523365SDimitry Andric return false;
51747d523365SDimitry Andric }
51757d523365SDimitry Andric
51767d523365SDimitry Andric case CmpInst::ICMP_ULE: {
51777d523365SDimitry Andric const APInt *C;
51787d523365SDimitry Andric
51797d523365SDimitry Andric // LHS u<= LHS +_{nuw} C for any C
51807d523365SDimitry Andric if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
51817d523365SDimitry Andric return true;
51827d523365SDimitry Andric
51837d523365SDimitry Andric // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
5184d88c1a5aSDimitry Andric auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
5185d88c1a5aSDimitry Andric const Value *&X,
51867d523365SDimitry Andric const APInt *&CA, const APInt *&CB) {
51877d523365SDimitry Andric if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) &&
51887d523365SDimitry Andric match(B, m_NUWAdd(m_Specific(X), m_APInt(CB))))
51897d523365SDimitry Andric return true;
51907d523365SDimitry Andric
51917d523365SDimitry Andric // If X & C == 0 then (X | C) == X +_{nuw} C
51927d523365SDimitry Andric if (match(A, m_Or(m_Value(X), m_APInt(CA))) &&
51937d523365SDimitry Andric match(B, m_Or(m_Specific(X), m_APInt(CB)))) {
519451690af2SDimitry Andric KnownBits Known(CA->getBitWidth());
51952cab237bSDimitry Andric computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr,
51962cab237bSDimitry Andric /*CxtI*/ nullptr, /*DT*/ nullptr);
519751690af2SDimitry Andric if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero))
51987d523365SDimitry Andric return true;
51997d523365SDimitry Andric }
52007d523365SDimitry Andric
52017d523365SDimitry Andric return false;
52027d523365SDimitry Andric };
52037d523365SDimitry Andric
5204d88c1a5aSDimitry Andric const Value *X;
52057d523365SDimitry Andric const APInt *CLHS, *CRHS;
52067d523365SDimitry Andric if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS))
52077d523365SDimitry Andric return CLHS->ule(*CRHS);
52087d523365SDimitry Andric
52097d523365SDimitry Andric return false;
52107d523365SDimitry Andric }
52117d523365SDimitry Andric }
52127d523365SDimitry Andric }
52137d523365SDimitry Andric
52147d523365SDimitry Andric /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
52153ca95b02SDimitry Andric /// ALHS ARHS" is true. Otherwise, return None.
52163ca95b02SDimitry Andric static Optional<bool>
isImpliedCondOperands(CmpInst::Predicate Pred,const Value * ALHS,const Value * ARHS,const Value * BLHS,const Value * BRHS,const DataLayout & DL,unsigned Depth)5217d88c1a5aSDimitry Andric isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
52182cab237bSDimitry Andric const Value *ARHS, const Value *BLHS, const Value *BRHS,
52192cab237bSDimitry Andric const DataLayout &DL, unsigned Depth) {
52207d523365SDimitry Andric switch (Pred) {
52217d523365SDimitry Andric default:
52223ca95b02SDimitry Andric return None;
52237d523365SDimitry Andric
52247d523365SDimitry Andric case CmpInst::ICMP_SLT:
52257d523365SDimitry Andric case CmpInst::ICMP_SLE:
52262cab237bSDimitry Andric if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) &&
52272cab237bSDimitry Andric isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth))
52283ca95b02SDimitry Andric return true;
52293ca95b02SDimitry Andric return None;
52307d523365SDimitry Andric
52317d523365SDimitry Andric case CmpInst::ICMP_ULT:
52327d523365SDimitry Andric case CmpInst::ICMP_ULE:
52332cab237bSDimitry Andric if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) &&
52342cab237bSDimitry Andric isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth))
52353ca95b02SDimitry Andric return true;
52363ca95b02SDimitry Andric return None;
52377d523365SDimitry Andric }
52387d523365SDimitry Andric }
52397d523365SDimitry Andric
52403ca95b02SDimitry Andric /// Return true if the operands of the two compares match. IsSwappedOps is true
52413ca95b02SDimitry Andric /// when the operands match, but are swapped.
isMatchingOps(const Value * ALHS,const Value * ARHS,const Value * BLHS,const Value * BRHS,bool & IsSwappedOps)5242d88c1a5aSDimitry Andric static bool isMatchingOps(const Value *ALHS, const Value *ARHS,
5243d88c1a5aSDimitry Andric const Value *BLHS, const Value *BRHS,
52443ca95b02SDimitry Andric bool &IsSwappedOps) {
52453ca95b02SDimitry Andric
52463ca95b02SDimitry Andric bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS);
52473ca95b02SDimitry Andric IsSwappedOps = (ALHS == BRHS && ARHS == BLHS);
52483ca95b02SDimitry Andric return IsMatchingOps || IsSwappedOps;
52493ca95b02SDimitry Andric }
52503ca95b02SDimitry Andric
5251b5893f02SDimitry Andric /// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true.
5252b5893f02SDimitry Andric /// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false.
5253b5893f02SDimitry Andric /// Otherwise, return None if we can't infer anything.
isImpliedCondMatchingOperands(CmpInst::Predicate APred,CmpInst::Predicate BPred,bool AreSwappedOps)52543ca95b02SDimitry Andric static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred,
52553ca95b02SDimitry Andric CmpInst::Predicate BPred,
5256b5893f02SDimitry Andric bool AreSwappedOps) {
5257b5893f02SDimitry Andric // Canonicalize the predicate as if the operands were not commuted.
5258b5893f02SDimitry Andric if (AreSwappedOps)
52593ca95b02SDimitry Andric BPred = ICmpInst::getSwappedPredicate(BPred);
5260b5893f02SDimitry Andric
52613ca95b02SDimitry Andric if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred))
52623ca95b02SDimitry Andric return true;
52633ca95b02SDimitry Andric if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred))
52643ca95b02SDimitry Andric return false;
52653ca95b02SDimitry Andric
52663ca95b02SDimitry Andric return None;
52673ca95b02SDimitry Andric }
52683ca95b02SDimitry Andric
5269b5893f02SDimitry Andric /// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true.
5270b5893f02SDimitry Andric /// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false.
5271b5893f02SDimitry Andric /// Otherwise, return None if we can't infer anything.
52723ca95b02SDimitry Andric static Optional<bool>
isImpliedCondMatchingImmOperands(CmpInst::Predicate APred,const ConstantInt * C1,CmpInst::Predicate BPred,const ConstantInt * C2)5273b5893f02SDimitry Andric isImpliedCondMatchingImmOperands(CmpInst::Predicate APred,
5274d88c1a5aSDimitry Andric const ConstantInt *C1,
5275d88c1a5aSDimitry Andric CmpInst::Predicate BPred,
5276b5893f02SDimitry Andric const ConstantInt *C2) {
52773ca95b02SDimitry Andric ConstantRange DomCR =
52783ca95b02SDimitry Andric ConstantRange::makeExactICmpRegion(APred, C1->getValue());
52793ca95b02SDimitry Andric ConstantRange CR =
52803ca95b02SDimitry Andric ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue());
52813ca95b02SDimitry Andric ConstantRange Intersection = DomCR.intersectWith(CR);
52823ca95b02SDimitry Andric ConstantRange Difference = DomCR.difference(CR);
52833ca95b02SDimitry Andric if (Intersection.isEmptySet())
52843ca95b02SDimitry Andric return false;
52853ca95b02SDimitry Andric if (Difference.isEmptySet())
52863ca95b02SDimitry Andric return true;
52873ca95b02SDimitry Andric return None;
52883ca95b02SDimitry Andric }
52893ca95b02SDimitry Andric
52902cab237bSDimitry Andric /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
52912cab237bSDimitry Andric /// false. Otherwise, return None if we can't infer anything.
isImpliedCondICmps(const ICmpInst * LHS,const ICmpInst * RHS,const DataLayout & DL,bool LHSIsTrue,unsigned Depth)52922cab237bSDimitry Andric static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
52932cab237bSDimitry Andric const ICmpInst *RHS,
52942cab237bSDimitry Andric const DataLayout &DL, bool LHSIsTrue,
52952cab237bSDimitry Andric unsigned Depth) {
52962cab237bSDimitry Andric Value *ALHS = LHS->getOperand(0);
52972cab237bSDimitry Andric Value *ARHS = LHS->getOperand(1);
5298c4394386SDimitry Andric // The rest of the logic assumes the LHS condition is true. If that's not the
5299c4394386SDimitry Andric // case, invert the predicate to make it so.
53002cab237bSDimitry Andric ICmpInst::Predicate APred =
53012cab237bSDimitry Andric LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
53022cab237bSDimitry Andric
53032cab237bSDimitry Andric Value *BLHS = RHS->getOperand(0);
53042cab237bSDimitry Andric Value *BRHS = RHS->getOperand(1);
53052cab237bSDimitry Andric ICmpInst::Predicate BPred = RHS->getPredicate();
53063ca95b02SDimitry Andric
53073ca95b02SDimitry Andric // Can we infer anything when the two compares have matching operands?
5308b5893f02SDimitry Andric bool AreSwappedOps;
5309b5893f02SDimitry Andric if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) {
53103ca95b02SDimitry Andric if (Optional<bool> Implication = isImpliedCondMatchingOperands(
5311b5893f02SDimitry Andric APred, BPred, AreSwappedOps))
53123ca95b02SDimitry Andric return Implication;
53133ca95b02SDimitry Andric // No amount of additional analysis will infer the second condition, so
53143ca95b02SDimitry Andric // early exit.
53153ca95b02SDimitry Andric return None;
53163ca95b02SDimitry Andric }
53173ca95b02SDimitry Andric
53183ca95b02SDimitry Andric // Can we infer anything when the LHS operands match and the RHS operands are
53193ca95b02SDimitry Andric // constants (not necessarily matching)?
53203ca95b02SDimitry Andric if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) {
53213ca95b02SDimitry Andric if (Optional<bool> Implication = isImpliedCondMatchingImmOperands(
5322b5893f02SDimitry Andric APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS)))
53233ca95b02SDimitry Andric return Implication;
53243ca95b02SDimitry Andric // No amount of additional analysis will infer the second condition, so
53253ca95b02SDimitry Andric // early exit.
53263ca95b02SDimitry Andric return None;
53273ca95b02SDimitry Andric }
53287d523365SDimitry Andric
53297d523365SDimitry Andric if (APred == BPred)
53302cab237bSDimitry Andric return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth);
53312cab237bSDimitry Andric return None;
53322cab237bSDimitry Andric }
53337d523365SDimitry Andric
53342cab237bSDimitry Andric /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
53352cab237bSDimitry Andric /// false. Otherwise, return None if we can't infer anything. We expect the
53362cab237bSDimitry Andric /// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction.
isImpliedCondAndOr(const BinaryOperator * LHS,const ICmpInst * RHS,const DataLayout & DL,bool LHSIsTrue,unsigned Depth)53372cab237bSDimitry Andric static Optional<bool> isImpliedCondAndOr(const BinaryOperator *LHS,
53382cab237bSDimitry Andric const ICmpInst *RHS,
53392cab237bSDimitry Andric const DataLayout &DL, bool LHSIsTrue,
53402cab237bSDimitry Andric unsigned Depth) {
53412cab237bSDimitry Andric // The LHS must be an 'or' or an 'and' instruction.
53422cab237bSDimitry Andric assert((LHS->getOpcode() == Instruction::And ||
53432cab237bSDimitry Andric LHS->getOpcode() == Instruction::Or) &&
53442cab237bSDimitry Andric "Expected LHS to be 'and' or 'or'.");
53452cab237bSDimitry Andric
53462cab237bSDimitry Andric assert(Depth <= MaxDepth && "Hit recursion limit");
53472cab237bSDimitry Andric
53482cab237bSDimitry Andric // If the result of an 'or' is false, then we know both legs of the 'or' are
53492cab237bSDimitry Andric // false. Similarly, if the result of an 'and' is true, then we know both
53502cab237bSDimitry Andric // legs of the 'and' are true.
53512cab237bSDimitry Andric Value *ALHS, *ARHS;
53522cab237bSDimitry Andric if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) ||
53532cab237bSDimitry Andric (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) {
53542cab237bSDimitry Andric // FIXME: Make this non-recursion.
53552cab237bSDimitry Andric if (Optional<bool> Implication =
53562cab237bSDimitry Andric isImpliedCondition(ALHS, RHS, DL, LHSIsTrue, Depth + 1))
53572cab237bSDimitry Andric return Implication;
53582cab237bSDimitry Andric if (Optional<bool> Implication =
53592cab237bSDimitry Andric isImpliedCondition(ARHS, RHS, DL, LHSIsTrue, Depth + 1))
53602cab237bSDimitry Andric return Implication;
53612cab237bSDimitry Andric return None;
53622cab237bSDimitry Andric }
53632cab237bSDimitry Andric return None;
53642cab237bSDimitry Andric }
53652cab237bSDimitry Andric
isImpliedCondition(const Value * LHS,const Value * RHS,const DataLayout & DL,bool LHSIsTrue,unsigned Depth)53662cab237bSDimitry Andric Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
53672cab237bSDimitry Andric const DataLayout &DL, bool LHSIsTrue,
53682cab237bSDimitry Andric unsigned Depth) {
53692cab237bSDimitry Andric // Bail out when we hit the limit.
53702cab237bSDimitry Andric if (Depth == MaxDepth)
53712cab237bSDimitry Andric return None;
53722cab237bSDimitry Andric
53732cab237bSDimitry Andric // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
53742cab237bSDimitry Andric // example.
53752cab237bSDimitry Andric if (LHS->getType() != RHS->getType())
53762cab237bSDimitry Andric return None;
53772cab237bSDimitry Andric
53782cab237bSDimitry Andric Type *OpTy = LHS->getType();
53792cab237bSDimitry Andric assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!");
53802cab237bSDimitry Andric
53812cab237bSDimitry Andric // LHS ==> RHS by definition
53822cab237bSDimitry Andric if (LHS == RHS)
53832cab237bSDimitry Andric return LHSIsTrue;
53842cab237bSDimitry Andric
53852cab237bSDimitry Andric // FIXME: Extending the code below to handle vectors.
53862cab237bSDimitry Andric if (OpTy->isVectorTy())
53872cab237bSDimitry Andric return None;
53882cab237bSDimitry Andric
53892cab237bSDimitry Andric assert(OpTy->isIntegerTy(1) && "implied by above");
53902cab237bSDimitry Andric
53912cab237bSDimitry Andric // Both LHS and RHS are icmps.
53922cab237bSDimitry Andric const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
53932cab237bSDimitry Andric const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS);
53942cab237bSDimitry Andric if (LHSCmp && RHSCmp)
53952cab237bSDimitry Andric return isImpliedCondICmps(LHSCmp, RHSCmp, DL, LHSIsTrue, Depth);
53962cab237bSDimitry Andric
53972cab237bSDimitry Andric // The LHS should be an 'or' or an 'and' instruction. We expect the RHS to be
53982cab237bSDimitry Andric // an icmp. FIXME: Add support for and/or on the RHS.
53992cab237bSDimitry Andric const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS);
54002cab237bSDimitry Andric if (LHSBO && RHSCmp) {
54012cab237bSDimitry Andric if ((LHSBO->getOpcode() == Instruction::And ||
54022cab237bSDimitry Andric LHSBO->getOpcode() == Instruction::Or))
54032cab237bSDimitry Andric return isImpliedCondAndOr(LHSBO, RHSCmp, DL, LHSIsTrue, Depth);
54042cab237bSDimitry Andric }
54053ca95b02SDimitry Andric return None;
54067d523365SDimitry Andric }
5407b5893f02SDimitry Andric
isImpliedByDomCondition(const Value * Cond,const Instruction * ContextI,const DataLayout & DL)5408b5893f02SDimitry Andric Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
5409b5893f02SDimitry Andric const Instruction *ContextI,
5410b5893f02SDimitry Andric const DataLayout &DL) {
5411b5893f02SDimitry Andric assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
5412b5893f02SDimitry Andric if (!ContextI || !ContextI->getParent())
5413b5893f02SDimitry Andric return None;
5414b5893f02SDimitry Andric
5415b5893f02SDimitry Andric // TODO: This is a poor/cheap way to determine dominance. Should we use a
5416b5893f02SDimitry Andric // dominator tree (eg, from a SimplifyQuery) instead?
5417b5893f02SDimitry Andric const BasicBlock *ContextBB = ContextI->getParent();
5418b5893f02SDimitry Andric const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
5419b5893f02SDimitry Andric if (!PredBB)
5420b5893f02SDimitry Andric return None;
5421b5893f02SDimitry Andric
5422b5893f02SDimitry Andric // We need a conditional branch in the predecessor.
5423b5893f02SDimitry Andric Value *PredCond;
5424b5893f02SDimitry Andric BasicBlock *TrueBB, *FalseBB;
5425b5893f02SDimitry Andric if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
5426b5893f02SDimitry Andric return None;
5427b5893f02SDimitry Andric
5428b5893f02SDimitry Andric // The branch should get simplified. Don't bother simplifying this condition.
5429b5893f02SDimitry Andric if (TrueBB == FalseBB)
5430b5893f02SDimitry Andric return None;
5431b5893f02SDimitry Andric
5432b5893f02SDimitry Andric assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
5433b5893f02SDimitry Andric "Predecessor block does not point to successor?");
5434b5893f02SDimitry Andric
5435b5893f02SDimitry Andric // Is this condition implied by the predecessor condition?
5436b5893f02SDimitry Andric bool CondIsTrue = TrueBB == ContextBB;
5437b5893f02SDimitry Andric return isImpliedCondition(PredCond, Cond, DL, CondIsTrue);
5438b5893f02SDimitry Andric }
5439