Skip to content

Commit 500a6c9

Browse files
committed
[Analysis] Move SimplifyQuery into separate header (NFC)
To allow reusing it between InstructionSimplify and ValueTracking.
1 parent 32ec6d9 commit 500a6c9

File tree

2 files changed

+119
-97
lines changed

2 files changed

+119
-97
lines changed

llvm/include/llvm/Analysis/InstructionSimplify.h

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
3232
#define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
3333

34-
#include "llvm/IR/PatternMatch.h"
34+
#include "llvm/Analysis/SimplifyQuery.h"
3535

3636
namespace llvm {
3737

@@ -52,102 +52,6 @@ class TargetLibraryInfo;
5252
class Type;
5353
class Value;
5454

55-
/// InstrInfoQuery provides an interface to query additional information for
56-
/// instructions like metadata or keywords like nsw, which provides conservative
57-
/// results if the users specified it is safe to use.
58-
struct InstrInfoQuery {
59-
InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
60-
InstrInfoQuery() = default;
61-
bool UseInstrInfo = true;
62-
63-
MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
64-
if (UseInstrInfo)
65-
return I->getMetadata(KindID);
66-
return nullptr;
67-
}
68-
69-
template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
70-
if (UseInstrInfo)
71-
return Op->hasNoUnsignedWrap();
72-
return false;
73-
}
74-
75-
template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
76-
if (UseInstrInfo)
77-
return Op->hasNoSignedWrap();
78-
return false;
79-
}
80-
81-
bool isExact(const BinaryOperator *Op) const {
82-
if (UseInstrInfo && isa<PossiblyExactOperator>(Op))
83-
return cast<PossiblyExactOperator>(Op)->isExact();
84-
return false;
85-
}
86-
87-
template <class InstT> bool hasNoSignedZeros(const InstT *Op) const {
88-
if (UseInstrInfo)
89-
return Op->hasNoSignedZeros();
90-
return false;
91-
}
92-
};
93-
94-
struct SimplifyQuery {
95-
const DataLayout &DL;
96-
const TargetLibraryInfo *TLI = nullptr;
97-
const DominatorTree *DT = nullptr;
98-
AssumptionCache *AC = nullptr;
99-
const Instruction *CxtI = nullptr;
100-
101-
// Wrapper to query additional information for instructions like metadata or
102-
// keywords like nsw, which provides conservative results if those cannot
103-
// be safely used.
104-
const InstrInfoQuery IIQ;
105-
106-
/// Controls whether simplifications are allowed to constrain the range of
107-
/// possible values for uses of undef. If it is false, simplifications are not
108-
/// allowed to assume a particular value for a use of undef for example.
109-
bool CanUseUndef = true;
110-
111-
SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
112-
: DL(DL), CxtI(CXTI) {}
113-
114-
SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
115-
const DominatorTree *DT = nullptr,
116-
AssumptionCache *AC = nullptr,
117-
const Instruction *CXTI = nullptr, bool UseInstrInfo = true,
118-
bool CanUseUndef = true)
119-
: DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo),
120-
CanUseUndef(CanUseUndef) {}
121-
122-
SimplifyQuery(const DataLayout &DL, const DominatorTree *DT,
123-
AssumptionCache *AC = nullptr,
124-
const Instruction *CXTI = nullptr, bool UseInstrInfo = true,
125-
bool CanUseUndef = true)
126-
: DL(DL), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo),
127-
CanUseUndef(CanUseUndef) {}
128-
129-
SimplifyQuery getWithInstruction(const Instruction *I) const {
130-
SimplifyQuery Copy(*this);
131-
Copy.CxtI = I;
132-
return Copy;
133-
}
134-
SimplifyQuery getWithoutUndef() const {
135-
SimplifyQuery Copy(*this);
136-
Copy.CanUseUndef = false;
137-
return Copy;
138-
}
139-
140-
/// If CanUseUndef is true, returns whether \p V is undef.
141-
/// Otherwise always return false.
142-
bool isUndefValue(Value *V) const {
143-
if (!CanUseUndef)
144-
return false;
145-
146-
using namespace PatternMatch;
147-
return match(V, m_Undef());
148-
}
149-
};
150-
15155
// NOTE: the explicit multiple argument versions of these functions are
15256
// deprecated.
15357
// Please use the SimplifyQuery versions in new code.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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

Comments
 (0)