-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[llvm] add GenericFloatingPointPredicateUtils #140254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tgymnich
merged 4 commits into
llvm:main
from
tgymnich:tim/floating-point-predicate-utils
May 21, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
479 changes: 479 additions & 0 deletions
479
llvm/include/llvm/ADT/GenericFloatingPointPredicateUtils.h
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//===- llvm/Analysis/FloatingPointPredicateUtils.h ------------*- C++ -*00-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_ANALYSIS_FLOATINGPOINTPREDICATEUTILS_H | ||
#define LLVM_ANALYSIS_FLOATINGPOINTPREDICATEUTILS_H | ||
|
||
#include "llvm/ADT/GenericFloatingPointPredicateUtils.h" | ||
#include "llvm/IR/SSAContext.h" | ||
|
||
namespace llvm { | ||
|
||
using FloatingPointPredicateUtils = | ||
GenericFloatingPointPredicateUtils<SSAContext>; | ||
|
||
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the | ||
/// same result as an fcmp with the given operands. | ||
/// | ||
/// If \p LookThroughSrc is true, consider the input value when computing the | ||
/// mask. | ||
/// | ||
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair | ||
/// element will always be LHS. | ||
inline std::pair<Value *, FPClassTest> | ||
fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS, | ||
Value *RHS, bool LookThroughSrc = true) { | ||
return FloatingPointPredicateUtils::fcmpToClassTest(Pred, F, LHS, RHS, | ||
LookThroughSrc = true); | ||
} | ||
|
||
/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the | ||
/// same result as an fcmp with the given operands. | ||
/// | ||
/// If \p LookThroughSrc is true, consider the input value when computing the | ||
/// mask. | ||
/// | ||
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first pair | ||
/// element will always be LHS. | ||
inline std::pair<Value *, FPClassTest> | ||
fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS, | ||
const APFloat *ConstRHS, bool LookThroughSrc = true) { | ||
return FloatingPointPredicateUtils::fcmpToClassTest(Pred, F, LHS, *ConstRHS, | ||
LookThroughSrc); | ||
} | ||
|
||
inline std::tuple<Value *, FPClassTest, FPClassTest> | ||
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, | ||
FPClassTest RHSClass, bool LookThroughSrc = true) { | ||
return FloatingPointPredicateUtils::fcmpImpliesClass(Pred, F, LHS, RHSClass, | ||
LookThroughSrc); | ||
} | ||
|
||
inline std::tuple<Value *, FPClassTest, FPClassTest> | ||
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, | ||
const APFloat &ConstRHS, bool LookThroughSrc = true) { | ||
return FloatingPointPredicateUtils::fcmpImpliesClass(Pred, F, LHS, ConstRHS, | ||
LookThroughSrc); | ||
} | ||
|
||
inline std::tuple<Value *, FPClassTest, FPClassTest> | ||
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, | ||
Value *RHS, bool LookThroughSrc = true) { | ||
return FloatingPointPredicateUtils::fcmpImpliesClass(Pred, F, LHS, RHS, | ||
LookThroughSrc); | ||
} | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_ANALYSIS_FLOATINGPOINTPREDICATEUTILS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
llvm/include/llvm/CodeGen/MachineFloatingPointPredicateUtils.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===-- MachineFloatingPointModeUtils.h -----*- C++ ---------------------*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CODEGEN_MACHINEFLOATINGPOINTPREDICATEUTILS_H | ||
#define LLVM_CODEGEN_MACHINEFLOATINGPOINTPREDICATEUTILS_H | ||
|
||
#include "llvm/ADT/GenericFloatingPointPredicateUtils.h" | ||
#include "llvm/CodeGen/MachineSSAContext.h" | ||
|
||
namespace llvm { | ||
|
||
using MachineFloatingPointPredicateUtils = | ||
GenericFloatingPointPredicateUtils<MachineSSAContext>; | ||
|
||
/// Compute the possible floating-point classes that \p LHS could be based on | ||
/// fcmp \Pred \p LHS, \p RHS. | ||
/// | ||
/// \returns { TestedValue, ClassesIfTrue, ClassesIfFalse } | ||
/// | ||
/// If the compare returns an exact class test, ClassesIfTrue == | ||
/// ~ClassesIfFalse | ||
/// | ||
/// This is a less exact version of fcmpToClassTest (e.g. fcmpToClassTest will | ||
/// only succeed for a test of x > 0 implies positive, but not x > 1). | ||
/// | ||
/// If \p LookThroughSrc is true, consider the input value when computing the | ||
/// mask. This may look through sign bit operations. | ||
/// | ||
/// If \p LookThroughSrc is false, ignore the source value (i.e. the first | ||
/// pair element will always be LHS. | ||
/// | ||
inline std::tuple<Register, FPClassTest, FPClassTest> | ||
fcmpImpliesClass(CmpInst::Predicate Pred, const MachineFunction &MF, | ||
Register LHS, Register RHS, bool LookThroughSrc = true) { | ||
return MachineFloatingPointPredicateUtils::fcmpImpliesClass( | ||
Pred, MF, LHS, RHS, LookThroughSrc); | ||
} | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_CODEGEN_MACHINEFLOATINGPOINTPREDICATEUTILS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//===- FloatingPointPredicateUtils.cpp - -----------*- C++ -*--------------===// | ||
tgymnich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Analysis/FloatingPointPredicateUtils.h" | ||
#include "llvm/IR/PatternMatch.h" | ||
#include <optional> | ||
|
||
namespace llvm { | ||
|
||
using namespace PatternMatch; | ||
|
||
template <> | ||
DenormalMode FloatingPointPredicateUtils::queryDenormalMode(const Function &F, | ||
Value *Val) { | ||
Type *Ty = Val->getType()->getScalarType(); | ||
return F.getDenormalMode(Ty->getFltSemantics()); | ||
} | ||
|
||
template <> | ||
bool FloatingPointPredicateUtils::lookThroughFAbs(const Function &F, Value *LHS, | ||
Value *&Src) { | ||
return match(LHS, m_FAbs(m_Value(Src))); | ||
} | ||
|
||
template <> | ||
std::optional<APFloat> | ||
FloatingPointPredicateUtils::matchConstantFloat(const Function &F, Value *Val) { | ||
const APFloat *ConstVal; | ||
|
||
if (!match(Val, m_APFloatAllowPoison(ConstVal))) | ||
return std::nullopt; | ||
|
||
return *ConstVal; | ||
} | ||
tgymnich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} // namespace llvm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.