LLVM 24.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#include "llvm/IR/Operator.h"
15
16namespace llvm {
17
18class AssumptionCache;
20class DominatorTree;
22
23/// InstrInfoQuery provides an interface to query additional information for
24/// instructions like metadata or keywords like nsw, which provides conservative
25/// results if the users specified it is safe to use.
27 InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
28 InstrInfoQuery() = default;
29 bool UseInstrInfo = true;
30
31 MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
32 if (UseInstrInfo)
33 return I->getMetadata(KindID);
34 return nullptr;
35 }
36
37 template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
38 if (UseInstrInfo)
39 return Op->hasNoUnsignedWrap();
40 return false;
41 }
42
43 template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
44 if (UseInstrInfo)
45 return Op->hasNoSignedWrap();
46 return false;
47 }
48
49 bool isExact(const BinaryOperator *Op) const {
51 return cast<PossiblyExactOperator>(Op)->isExact();
52 return false;
53 }
54
55 template <class InstT> bool hasNoSignedZeros(const InstT *Op) const {
56 if (UseInstrInfo)
57 return Op->hasNoSignedZeros();
58 return false;
59 }
60};
61
62/// Evaluate query assuming this condition holds.
70
72private:
73 const Function *CtxF = nullptr;
74
75public:
76 const DataLayout &DL;
77 const TargetLibraryInfo *TLI = nullptr;
78 const DominatorTree *DT = nullptr;
79 AssumptionCache *AC = nullptr;
80 const Instruction *CtxI = nullptr;
81 const DomConditionCache *DC = nullptr;
82 const CondContext *CC = nullptr;
83
84 // Wrapper to query additional information for instructions like metadata or
85 // keywords like nsw, which provides conservative results if those cannot
86 // be safely used.
88
89 /// Controls whether simplifications are allowed to constrain the range of
90 /// possible values for uses of undef. If it is false, simplifications are not
91 /// allowed to assume a particular value for a use of undef for example.
92 bool CanUseUndef = true;
93 bool AllowEphemerals = false;
94
95 SimplifyQuery(const DataLayout &DL, const Instruction *CtxI = nullptr)
96 : DL(DL), CtxI(CtxI) {}
97
99 const DominatorTree *DT = nullptr,
100 AssumptionCache *AC = nullptr,
101 const Instruction *CtxI = nullptr, bool UseInstrInfo = true,
102 bool CanUseUndef = true, const DomConditionCache *DC = nullptr)
103 : DL(DL), TLI(TLI), DT(DT), AC(AC), CtxI(CtxI), DC(DC), IIQ(UseInstrInfo),
105
107 AssumptionCache *AC = nullptr,
108 const Instruction *CtxI = nullptr, bool UseInstrInfo = true,
109 bool CanUseUndef = true)
110 : DL(DL), DT(DT), AC(AC), CtxI(CtxI), IIQ(UseInstrInfo),
112
114 SimplifyQuery Copy(*this);
115 Copy.CtxI = I;
116 return Copy;
117 }
119 SimplifyQuery Copy(*this);
120 Copy.CtxF = F;
121 return Copy;
122 }
123 const Function *getFunction() const {
124 if (CtxF)
125 return CtxF;
126 if (CtxI)
127 return CtxI->getFunction();
128 return nullptr;
129 }
131 SimplifyQuery Copy(*this);
132 Copy.CanUseUndef = false;
133 return Copy;
134 }
136 SimplifyQuery Copy(*this);
137 Copy.AllowEphemerals = AllowEphemerals;
138 return Copy;
139 }
140
141 /// If CanUseUndef is true, returns whether \p V is undef.
142 /// Otherwise always return false.
143 LLVM_ABI bool isUndefValue(Value *V) const;
144
146 SimplifyQuery Copy(*this);
147 Copy.DC = nullptr;
148 return Copy;
149 }
150
152 SimplifyQuery Copy(*this);
153 Copy.CC = &CC;
154 return Copy;
155 }
156
158 SimplifyQuery Copy(*this);
159 Copy.CC = nullptr;
160 return Copy;
161 }
162};
163
164} // end namespace llvm
165
166#endif
#define LLVM_ABI
Definition Compiler.h:215
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallPtrSet class.
A cache of @llvm.assume calls within a function.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
Metadata node.
Definition Metadata.h:1081
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Provides information about what library functions are available for the current target.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Evaluate query assuming this condition holds.
CondContext(Value *Cond)
SmallPtrSet< Value *, 4 > AffectedValues
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
InstrInfoQuery()=default
bool hasNoUnsignedWrap(const InstT *Op) const
SimplifyQuery(const DataLayout &DL, const DominatorTree *DT, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, bool UseInstrInfo=true, bool CanUseUndef=true)
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
bool CanUseUndef
Controls whether simplifications are allowed to constrain the range of possible values for uses of un...
SimplifyQuery getWithFunction(const Function *F) const
SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, bool UseInstrInfo=true, bool CanUseUndef=true, const DomConditionCache *DC=nullptr)
const DominatorTree * DT
SimplifyQuery getWithCondContext(const CondContext &CC) const
SimplifyQuery allowEphemerals(bool AllowEphemerals) const
SimplifyQuery getWithInstruction(const Instruction *I) const
LLVM_ABI bool isUndefValue(Value *V) const
If CanUseUndef is true, returns whether V is undef.
AssumptionCache * AC
const DomConditionCache * DC
const Instruction * CtxI
const Function * getFunction() const
const TargetLibraryInfo * TLI
SimplifyQuery getWithoutUndef() const
const InstrInfoQuery IIQ
SimplifyQuery getWithoutDomCondCache() const
SimplifyQuery(const DataLayout &DL, const Instruction *CtxI=nullptr)
const CondContext * CC