|
| 1 | +//===-- SimplifyQuery.h - Context for simplifications -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_ANALYSIS_SIMPLIFYQUERY_H |
| 10 | +#define LLVM_ANALYSIS_SIMPLIFYQUERY_H |
| 11 | + |
| 12 | +#include "llvm/IR/PatternMatch.h" |
| 13 | + |
| 14 | +namespace llvm { |
| 15 | + |
| 16 | +class AssumptionCache; |
| 17 | +class DominatorTree; |
| 18 | +class TargetLibraryInfo; |
| 19 | + |
| 20 | +/// InstrInfoQuery provides an interface to query additional information for |
| 21 | +/// instructions like metadata or keywords like nsw, which provides conservative |
| 22 | +/// results if the users specified it is safe to use. |
| 23 | +struct InstrInfoQuery { |
| 24 | + InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {} |
| 25 | + InstrInfoQuery() = default; |
| 26 | + bool UseInstrInfo = true; |
| 27 | + |
| 28 | + MDNode *getMetadata(const Instruction *I, unsigned KindID) const { |
| 29 | + if (UseInstrInfo) |
| 30 | + return I->getMetadata(KindID); |
| 31 | + return nullptr; |
| 32 | + } |
| 33 | + |
| 34 | + template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const { |
| 35 | + if (UseInstrInfo) |
| 36 | + return Op->hasNoUnsignedWrap(); |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + template <class InstT> bool hasNoSignedWrap(const InstT *Op) const { |
| 41 | + if (UseInstrInfo) |
| 42 | + return Op->hasNoSignedWrap(); |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + bool isExact(const BinaryOperator *Op) const { |
| 47 | + if (UseInstrInfo && isa<PossiblyExactOperator>(Op)) |
| 48 | + return cast<PossiblyExactOperator>(Op)->isExact(); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + template <class InstT> bool hasNoSignedZeros(const InstT *Op) const { |
| 53 | + if (UseInstrInfo) |
| 54 | + return Op->hasNoSignedZeros(); |
| 55 | + return false; |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +struct SimplifyQuery { |
| 60 | + const DataLayout &DL; |
| 61 | + const TargetLibraryInfo *TLI = nullptr; |
| 62 | + const DominatorTree *DT = nullptr; |
| 63 | + AssumptionCache *AC = nullptr; |
| 64 | + const Instruction *CxtI = nullptr; |
| 65 | + |
| 66 | + // Wrapper to query additional information for instructions like metadata or |
| 67 | + // keywords like nsw, which provides conservative results if those cannot |
| 68 | + // be safely used. |
| 69 | + const InstrInfoQuery IIQ; |
| 70 | + |
| 71 | + /// Controls whether simplifications are allowed to constrain the range of |
| 72 | + /// possible values for uses of undef. If it is false, simplifications are not |
| 73 | + /// allowed to assume a particular value for a use of undef for example. |
| 74 | + bool CanUseUndef = true; |
| 75 | + |
| 76 | + SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr) |
| 77 | + : DL(DL), CxtI(CXTI) {} |
| 78 | + |
| 79 | + SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI, |
| 80 | + const DominatorTree *DT = nullptr, |
| 81 | + AssumptionCache *AC = nullptr, |
| 82 | + const Instruction *CXTI = nullptr, bool UseInstrInfo = true, |
| 83 | + bool CanUseUndef = true) |
| 84 | + : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo), |
| 85 | + CanUseUndef(CanUseUndef) {} |
| 86 | + |
| 87 | + SimplifyQuery(const DataLayout &DL, const DominatorTree *DT, |
| 88 | + AssumptionCache *AC = nullptr, |
| 89 | + const Instruction *CXTI = nullptr, bool UseInstrInfo = true, |
| 90 | + bool CanUseUndef = true) |
| 91 | + : DL(DL), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo), |
| 92 | + CanUseUndef(CanUseUndef) {} |
| 93 | + |
| 94 | + SimplifyQuery getWithInstruction(const Instruction *I) const { |
| 95 | + SimplifyQuery Copy(*this); |
| 96 | + Copy.CxtI = I; |
| 97 | + return Copy; |
| 98 | + } |
| 99 | + SimplifyQuery getWithoutUndef() const { |
| 100 | + SimplifyQuery Copy(*this); |
| 101 | + Copy.CanUseUndef = false; |
| 102 | + return Copy; |
| 103 | + } |
| 104 | + |
| 105 | + /// If CanUseUndef is true, returns whether \p V is undef. |
| 106 | + /// Otherwise always return false. |
| 107 | + bool isUndefValue(Value *V) const { |
| 108 | + if (!CanUseUndef) |
| 109 | + return false; |
| 110 | + |
| 111 | + using namespace PatternMatch; |
| 112 | + return match(V, m_Undef()); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +} // end namespace llvm |
| 117 | + |
| 118 | +#endif |
0 commit comments