diff options
author | Arnold Schwaighofer <aschwaighofer@apple.com> | 2013-05-05 01:54:46 +0000 |
---|---|---|
committer | Arnold Schwaighofer <aschwaighofer@apple.com> | 2013-05-05 01:54:46 +0000 |
commit | e79d92c592d75e210dbe3abe1a975e859d17e953 (patch) | |
tree | 6ea7c95b07fdeafb0b747d87f96c3e7ffd8e75d4 | |
parent | f852472823fd2182a3ca54bdf4d30ad8a6a6cd57 (diff) |
PatternMatch: Matcher for (un)ordered floating point min/max
Add support for matching 'ordered' and 'unordered' floating point min/max
constructs.
In LLVM we can express min/max functions as a combination of compare and select.
We have support for matching such constructs for integers but not for floating
point. In floating point math there is no total order because of the presence of
'NaN'. Therefore, we have to be careful to preserve the original fcmp semantics
when interpreting floating point compare select combinations as a minimum or
maximum function. The resulting 'ordered/unordered' floating point maximum
function has to select the same value as the select/fcmp combination it is based
on.
ordered_max(x,y) = max(x,y) iff x and y are not NaN, y otherwise
unordered_max(x,y) = max(x,y) iff x and y are not NaN, x otherwise
ordered_min(x,y) = min(x,y) iff x and y are not NaN, y otherwise
unordered_min(x,y) = min(x,y) iff x and y are not NaN, x otherwise
This matches the behavior of the underlying select(fcmp(olt/ult/.., L, R), L, R)
construct.
Any code using this predicate has to preserve this semantics.
A follow-up patch will use this to implement floating point min/max reductions
in the vectorizer.
radar://13723044
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181143 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Support/PatternMatch.h | 110 | ||||
-rw-r--r-- | unittests/IR/PatternMatch.cpp | 265 |
2 files changed, 364 insertions, 11 deletions
diff --git a/include/llvm/Support/PatternMatch.h b/include/llvm/Support/PatternMatch.h index 9fbe4349b3..a83a068462 100644 --- a/include/llvm/Support/PatternMatch.h +++ b/include/llvm/Support/PatternMatch.h @@ -830,7 +830,7 @@ inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). // -template<typename LHS_t, typename RHS_t, typename Pred_t> +template<typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t> struct MaxMin_match { LHS_t L; RHS_t R; @@ -844,7 +844,7 @@ struct MaxMin_match { SelectInst *SI = dyn_cast<SelectInst>(V); if (!SI) return false; - ICmpInst *Cmp = dyn_cast<ICmpInst>(SI->getCondition()); + CmpInst_t *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); if (!Cmp) return false; // At this point we have a select conditioned on a comparison. Check that @@ -856,7 +856,7 @@ struct MaxMin_match { if ((TrueVal != LHS || FalseVal != RHS) && (TrueVal != RHS || FalseVal != LHS)) return false; - ICmpInst::Predicate Pred = LHS == TrueVal ? + typename CmpInst_t::Predicate Pred = LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate(); // Does "(x pred y) ? x : y" represent the desired max/min operation? if (!Pred_t::match(Pred)) @@ -894,28 +894,116 @@ struct umin_pred_ty { } }; +/// ofmax_pred_ty - Helper class for identifying ordered max predicates. +struct ofmax_pred_ty { + static bool match(FCmpInst::Predicate Pred) { + return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; + } +}; + +/// ofmin_pred_ty - Helper class for identifying ordered min predicates. +struct ofmin_pred_ty { + static bool match(FCmpInst::Predicate Pred) { + return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; + } +}; + +/// ufmax_pred_ty - Helper class for identifying unordered max predicates. +struct ufmax_pred_ty { + static bool match(FCmpInst::Predicate Pred) { + return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; + } +}; + +/// ufmin_pred_ty - Helper class for identifying unordered min predicates. +struct ufmin_pred_ty { + static bool match(FCmpInst::Predicate Pred) { + return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; + } +}; + template<typename LHS, typename RHS> -inline MaxMin_match<LHS, RHS, smax_pred_ty> +inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, const RHS &R) { - return MaxMin_match<LHS, RHS, smax_pred_ty>(L, R); + return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); } template<typename LHS, typename RHS> -inline MaxMin_match<LHS, RHS, smin_pred_ty> +inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, const RHS &R) { - return MaxMin_match<LHS, RHS, smin_pred_ty>(L, R); + return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); } template<typename LHS, typename RHS> -inline MaxMin_match<LHS, RHS, umax_pred_ty> +inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, const RHS &R) { - return MaxMin_match<LHS, RHS, umax_pred_ty>(L, R); + return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); } template<typename LHS, typename RHS> -inline MaxMin_match<LHS, RHS, umin_pred_ty> +inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, const RHS &R) { - return MaxMin_match<LHS, RHS, umin_pred_ty>(L, R); + return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); +} + +/// \brief Match an 'ordered' floating point maximum attribute. +/// Floating point has one special value 'NaN'. Therefore, there is no total +/// order. However, if we can ignore the 'NaN' value (for example, because of a +/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' +/// semantics. In the presence of 'NaN' we have to preserve the original +/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. +/// +/// max(L, R) iff L and R are not NaN +/// m_OrdFMax(L, R) = R iff L or R are NaN +template<typename LHS, typename RHS> +inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> +m_OrdFMax(const LHS &L, const RHS &R) { + return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); +} + +/// \brief Match an 'ordered' floating point minimum attribute. +/// Floating point has one special value 'NaN'. Therefore, there is no total +/// order. However, if we can ignore the 'NaN' value (for example, because of a +/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' +/// semantics. In the presence of 'NaN' we have to preserve the original +/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. +/// +/// max(L, R) iff L and R are not NaN +/// m_OrdFMin(L, R) = R iff L or R are NaN +template<typename LHS, typename RHS> +inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> +m_OrdFMin(const LHS &L, const RHS &R) { + return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); +} + +/// \brief Match an 'unordered' floating point maximum attribute. +/// Floating point has one special value 'NaN'. Therefore, there is no total +/// order. However, if we can ignore the 'NaN' value (for example, because of a +/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' +/// semantics. In the presence of 'NaN' we have to preserve the original +/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. +/// +/// max(L, R) iff L and R are not NaN +/// m_UnordFMin(L, R) = L iff L or R are NaN +template<typename LHS, typename RHS> +inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> +m_UnordFMax(const LHS &L, const RHS &R) { + return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); +} + +/// \brief Match an 'unordered' floating point minimum attribute. +/// Floating point has one special value 'NaN'. Therefore, there is no total +/// order. However, if we can ignore the 'NaN' value (for example, because of a +/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' +/// semantics. In the presence of 'NaN' we have to preserve the original +/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. +/// +/// max(L, R) iff L and R are not NaN +/// m_UnordFMin(L, R) = L iff L or R are NaN +template<typename LHS, typename RHS> +inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> +m_UnordFMin(const LHS &L, const RHS &R) { + return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); } template<typename Opnd_t> diff --git a/unittests/IR/PatternMatch.cpp b/unittests/IR/PatternMatch.cpp new file mode 100644 index 0000000000..7c6d8ce615 --- /dev/null +++ b/unittests/IR/PatternMatch.cpp @@ -0,0 +1,265 @@ +//===---- llvm/unittest/IR/PatternMatch.cpp - PatternMatch unit tests ----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/STLExtras.h" +#include "llvm/Analysis/ValueTracking.h" +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/MDBuilder.h" +#include "llvm/IR/Operator.h" +#include "llvm/Support/NoFolder.h" +#include "llvm/Support/PatternMatch.h" +#include "gtest/gtest.h" + +using namespace llvm::PatternMatch; + +namespace llvm { +namespace { + +/// Ordered floating point minimum/maximum tests. + +static void m_OrdFMin_expect_match_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_TRUE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)).match(Select)); + EXPECT_EQ(L, MatchL); + EXPECT_EQ(R, MatchR); + delete Select; + delete Cmp; +} + +static void m_OrdFMin_expect_nomatch_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_FALSE(m_OrdFMin(m_Value(MatchL), m_Value(MatchR)).match(Select)); + delete Select; + delete Cmp; +} + +static void m_OrdFMax_expect_match_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_TRUE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)).match(Select)); + EXPECT_EQ(L, MatchL); + EXPECT_EQ(R, MatchR); + delete Select; + delete Cmp; +} + +static void m_OrdFMax_expect_nomatch_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_FALSE(m_OrdFMax(m_Value(MatchL), m_Value(MatchR)).match(Select)); + delete Select; + delete Cmp; +} + + + +TEST(PatternMatchTest, FloatingPointOrderedMin) { + LLVMContext &C(getGlobalContext()); + IRBuilder<true, NoFolder> Builder(C); + + Type *FltTy = Builder.getFloatTy(); + Value *L = ConstantFP::get(FltTy, 1.0); + Value *R = ConstantFP::get(FltTy, 2.0); + + // Test OLT. + Value *Cmp = Builder.CreateFCmpOLT(L, R); + Value *Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMin_expect_match_and_delete(Cmp, Select, L, R); + + // Test OLE. + Cmp = Builder.CreateFCmpOLE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMin_expect_match_and_delete(Cmp, Select, L, R); + + // Test no match on OGE. + Cmp = Builder.CreateFCmpOGE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMin_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test no match on OGT. + Cmp = Builder.CreateFCmpOGT(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMin_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test match on OGE with inverted select. + Cmp = Builder.CreateFCmpOGE(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_OrdFMin_expect_match_and_delete(Cmp, Select, L, R); + + // Test match on OGT with inverted select. + Cmp = Builder.CreateFCmpOGT(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_OrdFMin_expect_match_and_delete(Cmp, Select, L, R); +} + +TEST(PatternMatchTest, FloatingPointOrderedMax) { + LLVMContext &C(getGlobalContext()); + IRBuilder<true, NoFolder> Builder(C); + + Type *FltTy = Builder.getFloatTy(); + Value *L = ConstantFP::get(FltTy, 1.0); + Value *R = ConstantFP::get(FltTy, 2.0); + + // Test OGT. + Value *Cmp = Builder.CreateFCmpOGT(L, R); + Value *Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMax_expect_match_and_delete(Cmp, Select, L, R); + + // Test OGE. + Cmp = Builder.CreateFCmpOGE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMax_expect_match_and_delete(Cmp, Select, L, R); + + // Test no match on OLE. + Cmp = Builder.CreateFCmpOLE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMax_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test no match on OLT. + Cmp = Builder.CreateFCmpOLT(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_OrdFMax_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test match on OLE with inverted select. + Cmp = Builder.CreateFCmpOLE(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_OrdFMax_expect_match_and_delete(Cmp, Select, L, R); + + // Test match on OLT with inverted select. + Cmp = Builder.CreateFCmpOLT(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_OrdFMax_expect_match_and_delete(Cmp, Select, L, R); +} + +/// Unordered floating point minimum/maximum tests. + +static void m_UnordFMin_expect_match_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_TRUE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)).match(Select)); + EXPECT_EQ(L, MatchL); + EXPECT_EQ(R, MatchR); + delete Select; + delete Cmp; +} + +static void m_UnordFMin_expect_nomatch_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_FALSE(m_UnordFMin(m_Value(MatchL), m_Value(MatchR)).match(Select)); + delete Select; + delete Cmp; +} + +static void m_UnordFMax_expect_match_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_TRUE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)).match(Select)); + EXPECT_EQ(L, MatchL); + EXPECT_EQ(R, MatchR); + delete Select; + delete Cmp; +} + +static void m_UnordFMax_expect_nomatch_and_delete(Value *Cmp, Value *Select, + Value *L, Value *R) { + Value *MatchL, *MatchR; + EXPECT_FALSE(m_UnordFMax(m_Value(MatchL), m_Value(MatchR)).match(Select)); + delete Select; + delete Cmp; +} + +TEST(PatternMatchTest, FloatingPointUnorderedMin) { + LLVMContext &C(getGlobalContext()); + IRBuilder<true, NoFolder> Builder(C); + + Type *FltTy = Builder.getFloatTy(); + Value *L = ConstantFP::get(FltTy, 1.0); + Value *R = ConstantFP::get(FltTy, 2.0); + + // Test ULT. + Value *Cmp = Builder.CreateFCmpULT(L, R); + Value *Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMin_expect_match_and_delete(Cmp, Select, L, R); + + // Test ULE. + Cmp = Builder.CreateFCmpULE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMin_expect_match_and_delete(Cmp, Select, L, R); + + // Test no match on UGE. + Cmp = Builder.CreateFCmpUGE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMin_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test no match on UGT. + Cmp = Builder.CreateFCmpUGT(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMin_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test match on UGE with inverted select. + Cmp = Builder.CreateFCmpUGE(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_UnordFMin_expect_match_and_delete(Cmp, Select, L, R); + + // Test match on UGT with inverted select. + Cmp = Builder.CreateFCmpUGT(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_UnordFMin_expect_match_and_delete(Cmp, Select, L, R); +} + +TEST(PatternMatchTest, FloatingPointUnorderedMax) { + LLVMContext &C(getGlobalContext()); + IRBuilder<true, NoFolder> Builder(C); + + Type *FltTy = Builder.getFloatTy(); + Value *L = ConstantFP::get(FltTy, 1.0); + Value *R = ConstantFP::get(FltTy, 2.0); + + // Test UGT. + Value *Cmp = Builder.CreateFCmpUGT(L, R); + Value *Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMax_expect_match_and_delete(Cmp, Select, L, R); + + // Test UGE. + Cmp = Builder.CreateFCmpUGE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMax_expect_match_and_delete(Cmp, Select, L, R); + + // Test no match on ULE. + Cmp = Builder.CreateFCmpULE(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMax_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test no match on ULT. + Cmp = Builder.CreateFCmpULT(L, R); + Select = Builder.CreateSelect(Cmp, L, R); + m_UnordFMax_expect_nomatch_and_delete(Cmp, Select, L, R); + + // Test match on ULE with inverted select. + Cmp = Builder.CreateFCmpULE(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_UnordFMax_expect_match_and_delete(Cmp, Select, L, R); + + // Test match on ULT with inverted select. + Cmp = Builder.CreateFCmpULT(L, R); + Select = Builder.CreateSelect(Cmp, R, L); + m_UnordFMax_expect_match_and_delete(Cmp, Select, L, R); +} + +} // anonymous namespace. +} // llvm namespace. |