Skip to content

Commit 3f36055

Browse files
committed
[llvm] add GenericFloatingPointPredicateUtils
1 parent ba63150 commit 3f36055

13 files changed

+696
-465
lines changed

llvm/include/llvm/ADT/GenericFloatingPointPredicateUtils.h

Lines changed: 479 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===- llvm/Analysis/FloatingPointPredicateUtils.h ------------*- C++ -*00-===//
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_FLOATINGPOINTPREDICATEUTILS_H
10+
#define LLVM_ANALYSIS_FLOATINGPOINTPREDICATEUTILS_H
11+
12+
#include "llvm/ADT/GenericFloatingPointPredicateUtils.h"
13+
#include "llvm/IR/SSAContext.h"
14+
15+
namespace llvm {
16+
17+
using FloatingPointPredicateUtils =
18+
GenericFloatingPointPredicateUtils<SSAContext>;
19+
20+
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
21+
/// same result as an fcmp with the given operands.
22+
///
23+
/// If \p LookThroughSrc is true, consider the input value when computing the
24+
/// mask.
25+
///
26+
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair
27+
/// element will always be LHS.
28+
inline std::pair<Value *, FPClassTest>
29+
fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
30+
Value *RHS, bool LookThroughSrc = true) {
31+
return FloatingPointPredicateUtils::fcmpToClassTest(Pred, F, LHS, RHS,
32+
LookThroughSrc = true);
33+
}
34+
35+
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
36+
/// same result as an fcmp with the given operands.
37+
///
38+
/// If \p LookThroughSrc is true, consider the input value when computing the
39+
/// mask.
40+
///
41+
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair
42+
/// element will always be LHS.
43+
inline std::pair<Value *, FPClassTest>
44+
fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
45+
const APFloat *ConstRHS, bool LookThroughSrc = true) {
46+
return FloatingPointPredicateUtils::fcmpToClassTest(Pred, F, LHS, *ConstRHS,
47+
LookThroughSrc);
48+
}
49+
50+
inline std::tuple<Value *, FPClassTest, FPClassTest>
51+
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
52+
FPClassTest RHSClass, bool LookThroughSrc = true) {
53+
return FloatingPointPredicateUtils::fcmpImpliesClass(Pred, F, LHS, RHSClass,
54+
LookThroughSrc);
55+
}
56+
57+
inline std::tuple<Value *, FPClassTest, FPClassTest>
58+
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
59+
const APFloat &ConstRHS, bool LookThroughSrc = true) {
60+
return FloatingPointPredicateUtils::fcmpImpliesClass(Pred, F, LHS, ConstRHS,
61+
LookThroughSrc);
62+
}
63+
64+
inline std::tuple<Value *, FPClassTest, FPClassTest>
65+
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
66+
Value *RHS, bool LookThroughSrc = true) {
67+
return FloatingPointPredicateUtils::fcmpImpliesClass(Pred, F, LHS, RHS,
68+
LookThroughSrc);
69+
}
70+
71+
} // namespace llvm
72+
73+
#endif // LLVM_ANALYSIS_FLOATINGPOINTPREDICATEUTILS_H

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -213,49 +213,6 @@ Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB,
213213
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
214214
bool &TrueIfSigned);
215215

216-
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
217-
/// same result as an fcmp with the given operands.
218-
///
219-
/// If \p LookThroughSrc is true, consider the input value when computing the
220-
/// mask.
221-
///
222-
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair
223-
/// element will always be LHS.
224-
std::pair<Value *, FPClassTest> fcmpToClassTest(CmpInst::Predicate Pred,
225-
const Function &F, Value *LHS,
226-
Value *RHS,
227-
bool LookThroughSrc = true);
228-
std::pair<Value *, FPClassTest> fcmpToClassTest(CmpInst::Predicate Pred,
229-
const Function &F, Value *LHS,
230-
const APFloat *ConstRHS,
231-
bool LookThroughSrc = true);
232-
233-
/// Compute the possible floating-point classes that \p LHS could be based on
234-
/// fcmp \Pred \p LHS, \p RHS.
235-
///
236-
/// \returns { TestedValue, ClassesIfTrue, ClassesIfFalse }
237-
///
238-
/// If the compare returns an exact class test, ClassesIfTrue == ~ClassesIfFalse
239-
///
240-
/// This is a less exact version of fcmpToClassTest (e.g. fcmpToClassTest will
241-
/// only succeed for a test of x > 0 implies positive, but not x > 1).
242-
///
243-
/// If \p LookThroughSrc is true, consider the input value when computing the
244-
/// mask. This may look through sign bit operations.
245-
///
246-
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair
247-
/// element will always be LHS.
248-
///
249-
std::tuple<Value *, FPClassTest, FPClassTest>
250-
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
251-
Value *RHS, bool LookThroughSrc = true);
252-
std::tuple<Value *, FPClassTest, FPClassTest>
253-
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
254-
FPClassTest RHS, bool LookThroughSrc = true);
255-
std::tuple<Value *, FPClassTest, FPClassTest>
256-
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
257-
const APFloat &RHS, bool LookThroughSrc = true);
258-
259216
/// Determine which floating-point classes are valid for \p V, and return them
260217
/// in KnownFPClass bit sets.
261218
///
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===-- MachineFloatingPointModeUtils.h -----*- 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_CODEGEN_MACHINEFLOATINGPOINTPREDICATEUTILS_H
10+
#define LLVM_CODEGEN_MACHINEFLOATINGPOINTPREDICATEUTILS_H
11+
12+
#include "llvm/ADT/GenericFloatingPointPredicateUtils.h"
13+
#include "llvm/CodeGen/MachineSSAContext.h"
14+
15+
namespace llvm {
16+
17+
using MachineFloatingPointPredicateUtils =
18+
GenericFloatingPointPredicateUtils<MachineSSAContext>;
19+
20+
/// Compute the possible floating-point classes that \p LHS could be based on
21+
/// fcmp \Pred \p LHS, \p RHS.
22+
///
23+
/// \returns { TestedValue, ClassesIfTrue, ClassesIfFalse }
24+
///
25+
/// If the compare returns an exact class test, ClassesIfTrue ==
26+
/// ~ClassesIfFalse
27+
///
28+
/// This is a less exact version of fcmpToClassTest (e.g. fcmpToClassTest will
29+
/// only succeed for a test of x > 0 implies positive, but not x > 1).
30+
///
31+
/// If \p LookThroughSrc is true, consider the input value when computing the
32+
/// mask. This may look through sign bit operations.
33+
///
34+
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first
35+
/// pair element will always be LHS.
36+
///
37+
inline std::tuple<Register, FPClassTest, FPClassTest>
38+
fcmpImpliesClass(CmpInst::Predicate Pred, const MachineFunction &MF,
39+
Register LHS, Register RHS, bool LookThroughSrc = true) {
40+
return MachineFloatingPointPredicateUtils::fcmpImpliesClass(
41+
Pred, MF, LHS, RHS, LookThroughSrc);
42+
}
43+
44+
} // namespace llvm
45+
46+
#endif // LLVM_CODEGEN_MACHINEFLOATINGPOINTPREDICATEUTILS_H

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ add_llvm_component_library(LLVMAnalysis
7474
DXILResource.cpp
7575
DXILMetadataAnalysis.cpp
7676
EphemeralValuesCache.cpp
77+
FloatingPointPredicateUtils.cpp
7778
FunctionPropertiesAnalysis.cpp
7879
GlobalsModRef.cpp
7980
GuardUtils.cpp
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===- FloatingPointPredicateUtils.cpp - -----------*- C++
2+
//-*--------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "llvm/Analysis/FloatingPointPredicateUtils.h"
11+
#include "llvm/IR/PatternMatch.h"
12+
#include <optional>
13+
14+
namespace llvm {
15+
16+
using namespace PatternMatch;
17+
18+
template <>
19+
DenormalMode FloatingPointPredicateUtils::queryDenormalMode(const Function &F,
20+
Value *Val) {
21+
Type *Ty = Val->getType()->getScalarType();
22+
return F.getDenormalMode(Ty->getFltSemantics());
23+
}
24+
25+
template <>
26+
bool FloatingPointPredicateUtils::lookThroughFAbs(const Function &F, Value *LHS,
27+
Value *&Src) {
28+
return match(LHS, m_FAbs(m_Value(Src)));
29+
}
30+
31+
template <>
32+
std::optional<APFloat>
33+
FloatingPointPredicateUtils::matchConstantFloat(const Function &F, Value *Val) {
34+
const APFloat *ConstVal;
35+
36+
if (!match(Val, m_APFloatAllowPoison(ConstVal)))
37+
return std::nullopt;
38+
39+
return *ConstVal;
40+
}
41+
42+
} // namespace llvm

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Analysis/CaptureTracking.h"
2727
#include "llvm/Analysis/CmpInstAnalysis.h"
2828
#include "llvm/Analysis/ConstantFolding.h"
29+
#include "llvm/Analysis/FloatingPointPredicateUtils.h"
2930
#include "llvm/Analysis/InstSimplifyFolder.h"
3031
#include "llvm/Analysis/Loads.h"
3132
#include "llvm/Analysis/LoopAnalysisManager.h"

0 commit comments

Comments
 (0)