LLVM 19.0.0git
SimplifyQuery.h
Go to the documentation of this file.
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
13
14namespace llvm {
15
16class AssumptionCache;
17class DomConditionCache;
18class DominatorTree;
19class TargetLibraryInfo;
20
21/// InstrInfoQuery provides an interface to query additional information for
22/// instructions like metadata or keywords like nsw, which provides conservative
23/// results if the users specified it is safe to use.
25 InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
26 InstrInfoQuery() = default;
27 bool UseInstrInfo = true;
28
29 MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
30 if (UseInstrInfo)
31 return I->getMetadata(KindID);
32 return nullptr;
33 }
34
35 template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
36 if (UseInstrInfo)
37 return Op->hasNoUnsignedWrap();
38 return false;
39 }
40
41 template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
42 if (UseInstrInfo)
43 return Op->hasNoSignedWrap();
44 return false;
45 }
46
47 bool isExact(const BinaryOperator *Op) const {
48 if (UseInstrInfo && isa<PossiblyExactOperator>(Op))
49 return cast<PossiblyExactOperator>(Op)->isExact();
50 return false;
51 }
52
53 template <class InstT> bool hasNoSignedZeros(const InstT *Op) const {
54 if (UseInstrInfo)
55 return Op->hasNoSignedZeros();
56 return false;
57 }
58};
59
61 const DataLayout &DL;
62 const TargetLibraryInfo *TLI = nullptr;
63 const DominatorTree *DT = nullptr;
64 AssumptionCache *AC = nullptr;
65 const Instruction *CxtI = nullptr;
66 const DomConditionCache *DC = nullptr;
67
68 // Wrapper to query additional information for instructions like metadata or
69 // keywords like nsw, which provides conservative results if those cannot
70 // be safely used.
72
73 /// Controls whether simplifications are allowed to constrain the range of
74 /// possible values for uses of undef. If it is false, simplifications are not
75 /// allowed to assume a particular value for a use of undef for example.
76 bool CanUseUndef = true;
77
78 SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
79 : DL(DL), CxtI(CXTI) {}
80
82 const DominatorTree *DT = nullptr,
83 AssumptionCache *AC = nullptr,
84 const Instruction *CXTI = nullptr, bool UseInstrInfo = true,
85 bool CanUseUndef = true, const DomConditionCache *DC = nullptr)
86 : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), DC(DC), IIQ(UseInstrInfo),
88
90 AssumptionCache *AC = nullptr,
91 const Instruction *CXTI = nullptr, bool UseInstrInfo = true,
92 bool CanUseUndef = true)
93 : DL(DL), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo),
95
97 SimplifyQuery Copy(*this);
98 Copy.CxtI = I;
99 return Copy;
100 }
102 SimplifyQuery Copy(*this);
103 Copy.CanUseUndef = false;
104 return Copy;
105 }
106
107 /// If CanUseUndef is true, returns whether \p V is undef.
108 /// Otherwise always return false.
109 bool isUndefValue(Value *V) const {
110 if (!CanUseUndef)
111 return false;
112
113 using namespace PatternMatch;
114 return match(V, m_Undef());
115 }
116};
117
118} // end namespace llvm
119
120#endif
#define I(x, y, z)
Definition: MD5.cpp:58
A cache of @llvm.assume calls within a function.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
Metadata node.
Definition: Metadata.h:1067
Provides information about what library functions are available for the current target.
LLVM Value Representation.
Definition: Value.h:74
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:136
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
Definition: SimplifyQuery.h:24
bool isExact(const BinaryOperator *Op) const
Definition: SimplifyQuery.h:47
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
Definition: SimplifyQuery.h:29
bool hasNoSignedZeros(const InstT *Op) const
Definition: SimplifyQuery.h:53
InstrInfoQuery(bool UMD)
Definition: SimplifyQuery.h:25
bool hasNoSignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:41
InstrInfoQuery()=default
bool hasNoUnsignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:35
const DataLayout & DL
Definition: SimplifyQuery.h:61
const Instruction * CxtI
Definition: SimplifyQuery.h:65
SimplifyQuery(const DataLayout &DL, const Instruction *CXTI=nullptr)
Definition: SimplifyQuery.h:78
bool CanUseUndef
Controls whether simplifications are allowed to constrain the range of possible values for uses of un...
Definition: SimplifyQuery.h:76
const DominatorTree * DT
Definition: SimplifyQuery.h:63
SimplifyQuery getWithInstruction(const Instruction *I) const
Definition: SimplifyQuery.h:96
SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CXTI=nullptr, bool UseInstrInfo=true, bool CanUseUndef=true, const DomConditionCache *DC=nullptr)
Definition: SimplifyQuery.h:81
bool isUndefValue(Value *V) const
If CanUseUndef is true, returns whether V is undef.
AssumptionCache * AC
Definition: SimplifyQuery.h:64
const DomConditionCache * DC
Definition: SimplifyQuery.h:66
SimplifyQuery(const DataLayout &DL, const DominatorTree *DT, AssumptionCache *AC=nullptr, const Instruction *CXTI=nullptr, bool UseInstrInfo=true, bool CanUseUndef=true)
Definition: SimplifyQuery.h:89
const TargetLibraryInfo * TLI
Definition: SimplifyQuery.h:62
SimplifyQuery getWithoutUndef() const
const InstrInfoQuery IIQ
Definition: SimplifyQuery.h:71