2014-03-04 12:08:18 +01:00
|
|
|
//===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2004-07-30 09:45:00 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2004-07-30 09:45:00 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides a simple and efficient mechanism for performing general
|
2018-02-18 17:19:22 +01:00
|
|
|
// tree-based pattern matches on the LLVM IR. The power of these routines is
|
2004-07-30 09:45:00 +02:00
|
|
|
// that it allows you to write concise patterns that are expressive and easy to
|
2018-02-18 17:19:22 +01:00
|
|
|
// understand. The other major advantage of this is that it allows you to
|
|
|
|
// trivially capture/bind elements in the pattern to variables. For example,
|
2004-07-30 09:45:00 +02:00
|
|
|
// you can do something like this:
|
|
|
|
//
|
|
|
|
// Value *Exp = ...
|
|
|
|
// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
|
|
|
|
// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
|
|
|
|
// m_And(m_Value(Y), m_ConstantInt(C2))))) {
|
|
|
|
// ... Pattern is matched and variables are bound ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This is primarily useful to things like the instruction combiner, but can
|
|
|
|
// also be useful for static analysis tools or code generators.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 12:08:18 +01:00
|
|
|
#ifndef LLVM_IR_PATTERNMATCH_H
|
|
|
|
#define LLVM_IR_PATTERNMATCH_H
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2017-05-13 00:25:07 +02:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
|
|
|
#include "llvm/ADT/APInt.h"
|
2014-03-04 12:01:28 +01:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2017-05-13 00:25:07 +02:00
|
|
|
#include "llvm/IR/Constant.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
2017-05-13 00:25:07 +02:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2014-10-25 20:20:17 +02:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Operator.h"
|
2017-05-13 00:25:07 +02:00
|
|
|
#include "llvm/IR/Value.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <cstdint>
|
2004-07-30 09:45:00 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2005-04-21 22:48:15 +02:00
|
|
|
namespace PatternMatch {
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
|
|
|
|
return const_cast<Pattern &>(P).match(V);
|
2004-07-30 09:45:00 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename SubPattern_t> struct OneUse_match {
|
2011-04-26 21:50:39 +02:00
|
|
|
SubPattern_t SubPattern;
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2011-04-26 21:50:39 +02:00
|
|
|
OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2011-04-26 21:50:39 +02:00
|
|
|
return V->hasOneUse() && SubPattern.match(V);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
|
|
|
|
return SubPattern;
|
|
|
|
}
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Class> struct class_match {
|
|
|
|
template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
|
2004-07-30 09:45:00 +02:00
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an arbitrary value and ignore it.
|
2011-02-09 18:00:45 +01:00
|
|
|
inline class_match<Value> m_Value() { return class_match<Value>(); }
|
2014-12-09 19:56:35 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an arbitrary binary operation and ignore it.
|
2014-12-09 17:36:10 +01:00
|
|
|
inline class_match<BinaryOperator> m_BinOp() {
|
|
|
|
return class_match<BinaryOperator>();
|
|
|
|
}
|
2014-12-09 19:56:35 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches any compare instruction and ignore it.
|
2014-12-09 19:56:35 +01:00
|
|
|
inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an arbitrary ConstantInt and ignore it.
|
2011-02-09 18:00:45 +01:00
|
|
|
inline class_match<ConstantInt> m_ConstantInt() {
|
|
|
|
return class_match<ConstantInt>();
|
|
|
|
}
|
2014-12-09 19:56:35 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an arbitrary undef constant.
|
2011-02-09 18:00:45 +01:00
|
|
|
inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an arbitrary Constant and ignore it.
|
2011-02-09 18:00:45 +01:00
|
|
|
inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2012-12-13 03:55:53 +01:00
|
|
|
/// Matching combinators
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LTy, typename RTy> struct match_combine_or {
|
2012-12-13 03:55:53 +01:00
|
|
|
LTy L;
|
|
|
|
RTy R;
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
|
2012-12-13 03:55:53 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
2012-12-13 03:55:53 +01:00
|
|
|
if (L.match(V))
|
|
|
|
return true;
|
|
|
|
if (R.match(V))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LTy, typename RTy> struct match_combine_and {
|
2012-12-13 03:55:53 +01:00
|
|
|
LTy L;
|
|
|
|
RTy R;
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
|
2012-12-13 03:55:53 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
2012-12-13 03:55:53 +01:00
|
|
|
if (L.match(V))
|
|
|
|
if (R.match(V))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Combine two pattern matchers matching L || R
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LTy, typename RTy>
|
2012-12-13 03:55:53 +01:00
|
|
|
inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
|
|
|
|
return match_combine_or<LTy, RTy>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Combine two pattern matchers matching L && R
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LTy, typename RTy>
|
2012-12-13 03:55:53 +01:00
|
|
|
inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
|
|
|
|
return match_combine_and<LTy, RTy>(L, R);
|
|
|
|
}
|
|
|
|
|
2011-02-09 18:00:45 +01:00
|
|
|
struct apint_match {
|
|
|
|
const APInt *&Res;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2011-02-09 18:00:45 +01:00
|
|
|
apint_match(const APInt *&R) : Res(R) {}
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (auto *CI = dyn_cast<ConstantInt>(V)) {
|
2011-02-09 18:00:45 +01:00
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 01:21:43 +01:00
|
|
|
if (V->getType()->isVectorTy())
|
2014-12-09 19:56:35 +01:00
|
|
|
if (const auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
|
2012-12-12 01:21:43 +01:00
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-09 18:00:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
[InstCombine] Canonicalize clamp of float types to minmax in fast mode.
Summary:
This commit allows matchSelectPattern to recognize clamp of float
arguments in the presence of FMF the same way as already done for
integers.
This case is a little different though. With integers, given the
min/max pattern is recognized, DAGBuilder starts selecting MIN/MAX
"automatically". That is not the case for float, because for them only
full FMINNAN/FMINNUM/FMAXNAN/FMAXNUM ISD nodes exist and they do care
about NaNs. On the other hand, some backends (e.g. X86) have only
FMIN/FMAX nodes that do not care about NaNS and the former NAN/NUM
nodes are illegal thus selection is not happening. So I decided to do
such kind of transformation in IR (InstCombiner) instead of
complicating the logic in the backend.
Reviewers: spatel, jmolloy, majnemer, efriedma, craig.topper
Reviewed By: efriedma
Subscribers: hiraditya, javed.absar, n.bozhenov, llvm-commits
Patch by Andrei Elovikov <andrei.elovikov@intel.com>
Differential Revision: https://reviews.llvm.org/D33186
llvm-svn: 310054
2017-08-04 14:22:17 +02:00
|
|
|
// Either constexpr if or renaming ConstantFP::getValueAPF to
|
|
|
|
// ConstantFP::getValue is needed to do it via single template
|
|
|
|
// function for both apint/apfloat.
|
|
|
|
struct apfloat_match {
|
|
|
|
const APFloat *&Res;
|
|
|
|
apfloat_match(const APFloat *&R) : Res(R) {}
|
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (auto *CI = dyn_cast<ConstantFP>(V)) {
|
|
|
|
Res = &CI->getValueAPF();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (V->getType()->isVectorTy())
|
|
|
|
if (const auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) {
|
|
|
|
Res = &CI->getValueAPF();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a ConstantInt or splatted ConstantVector, binding the
|
2011-02-09 18:00:45 +01:00
|
|
|
/// specified pointer to the contained APInt.
|
|
|
|
inline apint_match m_APInt(const APInt *&Res) { return Res; }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a ConstantFP or splatted ConstantVector, binding the
|
[InstCombine] Canonicalize clamp of float types to minmax in fast mode.
Summary:
This commit allows matchSelectPattern to recognize clamp of float
arguments in the presence of FMF the same way as already done for
integers.
This case is a little different though. With integers, given the
min/max pattern is recognized, DAGBuilder starts selecting MIN/MAX
"automatically". That is not the case for float, because for them only
full FMINNAN/FMINNUM/FMAXNAN/FMAXNUM ISD nodes exist and they do care
about NaNs. On the other hand, some backends (e.g. X86) have only
FMIN/FMAX nodes that do not care about NaNS and the former NAN/NUM
nodes are illegal thus selection is not happening. So I decided to do
such kind of transformation in IR (InstCombiner) instead of
complicating the logic in the backend.
Reviewers: spatel, jmolloy, majnemer, efriedma, craig.topper
Reviewed By: efriedma
Subscribers: hiraditya, javed.absar, n.bozhenov, llvm-commits
Patch by Andrei Elovikov <andrei.elovikov@intel.com>
Differential Revision: https://reviews.llvm.org/D33186
llvm-svn: 310054
2017-08-04 14:22:17 +02:00
|
|
|
/// specified pointer to the contained APFloat.
|
|
|
|
inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <int64_t Val> struct constantint_match {
|
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (const auto *CI = dyn_cast<ConstantInt>(V)) {
|
2009-01-06 00:45:50 +01:00
|
|
|
const APInt &CIV = CI->getValue();
|
2009-01-06 01:06:25 +01:00
|
|
|
if (Val >= 0)
|
2009-09-06 14:56:52 +02:00
|
|
|
return CIV == static_cast<uint64_t>(Val);
|
2009-01-06 00:45:50 +01:00
|
|
|
// If Val is negative, and CI is shorter than it, truncate to the right
|
|
|
|
// number of bits. If it is larger, then we have to sign extend. Just
|
|
|
|
// compare their negated values.
|
|
|
|
return -CIV == -Val;
|
|
|
|
}
|
|
|
|
return false;
|
2008-10-30 21:40:10 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a ConstantInt with a specific value.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
|
2011-02-09 18:00:45 +01:00
|
|
|
return constantint_match<Val>();
|
2008-10-30 21:40:10 +01:00
|
|
|
}
|
|
|
|
|
2018-03-12 19:17:01 +01:00
|
|
|
/// This helper class is used to match scalar and vector integer constants that
|
|
|
|
/// satisfy a specified predicate.
|
|
|
|
/// For vector constants, undefined elements are ignored.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Predicate> struct cst_pred_ty : public Predicate {
|
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (const auto *CI = dyn_cast<ConstantInt>(V))
|
2011-02-09 18:00:45 +01:00
|
|
|
return this->isValue(CI->getValue());
|
2018-02-06 19:39:23 +01:00
|
|
|
if (V->getType()->isVectorTy()) {
|
|
|
|
if (const auto *C = dyn_cast<Constant>(V)) {
|
2014-12-09 19:56:35 +01:00
|
|
|
if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
|
2012-12-12 01:21:43 +01:00
|
|
|
return this->isValue(CI->getValue());
|
2018-02-06 19:39:23 +01:00
|
|
|
|
|
|
|
// Non-splat vector constant: check each element for a match.
|
|
|
|
unsigned NumElts = V->getType()->getVectorNumElements();
|
|
|
|
assert(NumElts != 0 && "Constant vector with no elements?");
|
2018-11-20 17:08:19 +01:00
|
|
|
bool HasNonUndefElements = false;
|
2018-02-06 19:39:23 +01:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
Constant *Elt = C->getAggregateElement(i);
|
|
|
|
if (!Elt)
|
|
|
|
return false;
|
|
|
|
if (isa<UndefValue>(Elt))
|
|
|
|
continue;
|
|
|
|
auto *CI = dyn_cast<ConstantInt>(Elt);
|
|
|
|
if (!CI || !this->isValue(CI->getValue()))
|
|
|
|
return false;
|
2018-11-20 17:08:19 +01:00
|
|
|
HasNonUndefElements = true;
|
2018-02-06 19:39:23 +01:00
|
|
|
}
|
2018-11-20 17:08:19 +01:00
|
|
|
return HasNonUndefElements;
|
2018-02-06 19:39:23 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-20 05:47:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// This helper class is used to match scalar and vector constants that
|
2014-12-09 19:56:35 +01:00
|
|
|
/// satisfy a specified predicate, and bind them to an APInt.
|
|
|
|
template <typename Predicate> struct api_pred_ty : public Predicate {
|
2011-02-09 18:00:45 +01:00
|
|
|
const APInt *&Res;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2011-02-09 18:00:45 +01:00
|
|
|
api_pred_ty(const APInt *&R) : Res(R) {}
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (const auto *CI = dyn_cast<ConstantInt>(V))
|
2011-02-09 18:00:45 +01:00
|
|
|
if (this->isValue(CI->getValue())) {
|
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-12 01:21:43 +01:00
|
|
|
if (V->getType()->isVectorTy())
|
2014-12-09 19:56:35 +01:00
|
|
|
if (const auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
|
2012-12-12 01:21:43 +01:00
|
|
|
if (this->isValue(CI->getValue())) {
|
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-26 22:37:55 +01:00
|
|
|
|
2009-10-11 09:51:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2018-03-12 19:17:01 +01:00
|
|
|
/// This helper class is used to match scalar and vector floating-point
|
|
|
|
/// constants that satisfy a specified predicate.
|
|
|
|
/// For vector constants, undefined elements are ignored.
|
|
|
|
template <typename Predicate> struct cstfp_pred_ty : public Predicate {
|
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (const auto *CF = dyn_cast<ConstantFP>(V))
|
|
|
|
return this->isValue(CF->getValueAPF());
|
|
|
|
if (V->getType()->isVectorTy()) {
|
|
|
|
if (const auto *C = dyn_cast<Constant>(V)) {
|
|
|
|
if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
|
|
|
|
return this->isValue(CF->getValueAPF());
|
|
|
|
|
|
|
|
// Non-splat vector constant: check each element for a match.
|
|
|
|
unsigned NumElts = V->getType()->getVectorNumElements();
|
|
|
|
assert(NumElts != 0 && "Constant vector with no elements?");
|
2018-11-20 17:08:19 +01:00
|
|
|
bool HasNonUndefElements = false;
|
2018-03-12 19:17:01 +01:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
Constant *Elt = C->getAggregateElement(i);
|
|
|
|
if (!Elt)
|
|
|
|
return false;
|
|
|
|
if (isa<UndefValue>(Elt))
|
|
|
|
continue;
|
|
|
|
auto *CF = dyn_cast<ConstantFP>(Elt);
|
|
|
|
if (!CF || !this->isValue(CF->getValueAPF()))
|
|
|
|
return false;
|
2018-11-20 17:08:19 +01:00
|
|
|
HasNonUndefElements = true;
|
2018-03-12 19:17:01 +01:00
|
|
|
}
|
2018-11-20 17:08:19 +01:00
|
|
|
return HasNonUndefElements;
|
2018-03-12 19:17:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Encapsulate constant value queries for use in templated predicate matchers.
|
|
|
|
// This allows checking if constants match using compound predicates and works
|
|
|
|
// with vector constants, possibly with relaxed constraints. For example, ignore
|
|
|
|
// undef values.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-02-17 17:00:42 +01:00
|
|
|
|
2018-02-18 19:05:08 +01:00
|
|
|
struct is_all_ones {
|
|
|
|
bool isValue(const APInt &C) { return C.isAllOnesValue(); }
|
|
|
|
};
|
|
|
|
/// Match an integer or vector with all bits set.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-18 19:05:08 +01:00
|
|
|
inline cst_pred_ty<is_all_ones> m_AllOnes() {
|
|
|
|
return cst_pred_ty<is_all_ones>();
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
struct is_maxsignedvalue {
|
|
|
|
bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
|
|
|
|
};
|
|
|
|
/// Match an integer or vector with values having all bits except for the high
|
|
|
|
/// bit set (0x7f...).
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-18 17:19:22 +01:00
|
|
|
inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
|
|
|
|
return cst_pred_ty<is_maxsignedvalue>();
|
|
|
|
}
|
|
|
|
inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
|
|
|
|
return V;
|
|
|
|
}
|
2018-02-17 17:00:42 +01:00
|
|
|
|
2018-02-08 19:36:01 +01:00
|
|
|
struct is_negative {
|
|
|
|
bool isValue(const APInt &C) { return C.isNegative(); }
|
|
|
|
};
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an integer or vector of negative values.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-18 17:19:22 +01:00
|
|
|
inline cst_pred_ty<is_negative> m_Negative() {
|
|
|
|
return cst_pred_ty<is_negative>();
|
|
|
|
}
|
|
|
|
inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
|
|
|
|
return V;
|
|
|
|
}
|
2018-02-08 19:36:01 +01:00
|
|
|
|
2018-02-10 22:46:09 +01:00
|
|
|
struct is_nonnegative {
|
|
|
|
bool isValue(const APInt &C) { return C.isNonNegative(); }
|
|
|
|
};
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an integer or vector of nonnegative values.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-18 17:19:22 +01:00
|
|
|
inline cst_pred_ty<is_nonnegative> m_NonNegative() {
|
|
|
|
return cst_pred_ty<is_nonnegative>();
|
|
|
|
}
|
|
|
|
inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
|
|
|
|
return V;
|
|
|
|
}
|
2018-02-10 22:46:09 +01:00
|
|
|
|
2018-02-20 22:02:40 +01:00
|
|
|
struct is_one {
|
|
|
|
bool isValue(const APInt &C) { return C.isOneValue(); }
|
|
|
|
};
|
|
|
|
/// Match an integer 1 or a vector with all elements equal to 1.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-20 22:02:40 +01:00
|
|
|
inline cst_pred_ty<is_one> m_One() {
|
|
|
|
return cst_pred_ty<is_one>();
|
|
|
|
}
|
|
|
|
|
2018-04-22 19:07:44 +02:00
|
|
|
struct is_zero_int {
|
|
|
|
bool isValue(const APInt &C) { return C.isNullValue(); }
|
|
|
|
};
|
|
|
|
/// Match an integer 0 or a vector with all elements equal to 0.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
|
|
|
inline cst_pred_ty<is_zero_int> m_ZeroInt() {
|
|
|
|
return cst_pred_ty<is_zero_int>();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct is_zero {
|
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
auto *C = dyn_cast<Constant>(V);
|
|
|
|
return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
/// Match any null constant or a vector with all elements equal to 0.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
|
|
|
inline is_zero m_Zero() {
|
|
|
|
return is_zero();
|
|
|
|
}
|
|
|
|
|
2011-02-09 18:00:45 +01:00
|
|
|
struct is_power2 {
|
|
|
|
bool isValue(const APInt &C) { return C.isPowerOf2(); }
|
|
|
|
};
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an integer or vector power-of-2.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-18 17:19:22 +01:00
|
|
|
inline cst_pred_ty<is_power2> m_Power2() {
|
|
|
|
return cst_pred_ty<is_power2>();
|
|
|
|
}
|
|
|
|
inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
|
|
|
|
return V;
|
|
|
|
}
|
2007-12-20 05:47:44 +01:00
|
|
|
|
2018-02-06 19:39:23 +01:00
|
|
|
struct is_power2_or_zero {
|
|
|
|
bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
|
|
|
|
};
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an integer or vector of 0 or power-of-2 values.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-18 17:19:22 +01:00
|
|
|
inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
|
|
|
|
return cst_pred_ty<is_power2_or_zero>();
|
|
|
|
}
|
|
|
|
inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
|
|
|
|
return V;
|
|
|
|
}
|
2018-02-06 19:39:23 +01:00
|
|
|
|
2018-02-20 22:02:40 +01:00
|
|
|
struct is_sign_mask {
|
|
|
|
bool isValue(const APInt &C) { return C.isSignMask(); }
|
2014-12-20 04:04:38 +01:00
|
|
|
};
|
2018-02-20 22:02:40 +01:00
|
|
|
/// Match an integer or vector with only the sign bit(s) set.
|
2018-03-25 23:16:33 +02:00
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-02-20 22:02:40 +01:00
|
|
|
inline cst_pred_ty<is_sign_mask> m_SignMask() {
|
|
|
|
return cst_pred_ty<is_sign_mask>();
|
2018-02-18 17:19:22 +01:00
|
|
|
}
|
2014-12-20 04:04:38 +01:00
|
|
|
|
[InstCombine] Fold x & (-1 >> y) == x to x u<= (-1 >> y)
Summary:
https://bugs.llvm.org/show_bug.cgi?id=38123
This pattern will be produced by Implicit Integer Truncation sanitizer,
https://reviews.llvm.org/D48958
https://bugs.llvm.org/show_bug.cgi?id=21530
in unsigned case, therefore it is probably a good idea to improve it.
https://rise4fun.com/Alive/Rny
^ there are more opportunities for folds, i will follow up with them afterwards.
Caveat: this somehow exposes a missing opportunities
in `test/Transforms/InstCombine/icmp-logical.ll`
It seems, the problem is in `foldLogOpOfMaskedICmps()` in `InstCombineAndOrXor.cpp`.
But i'm not quite sure what is wrong, because it calls `getMaskedTypeForICmpPair()`,
which calls `decomposeBitTestICmp()` which should already work for these cases...
As @spatel notes in https://reviews.llvm.org/D49179#1158760,
that code is a rather complex mess, so we'll let it slide.
Reviewers: spatel, craig.topper
Reviewed By: spatel
Subscribers: yamauchi, majnemer, t.p.northover, llvm-commits
Differential Revision: https://reviews.llvm.org/D49179
llvm-svn: 336834
2018-07-11 21:05:04 +02:00
|
|
|
struct is_lowbit_mask {
|
|
|
|
bool isValue(const APInt &C) { return C.isMask(); }
|
|
|
|
};
|
|
|
|
/// Match an integer or vector with only the low bit(s) set.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
|
|
|
inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
|
|
|
|
return cst_pred_ty<is_lowbit_mask>();
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:18:47 +01:00
|
|
|
struct is_nan {
|
|
|
|
bool isValue(const APFloat &C) { return C.isNaN(); }
|
|
|
|
};
|
2018-03-25 23:16:33 +02:00
|
|
|
/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-03-12 23:18:47 +01:00
|
|
|
inline cstfp_pred_ty<is_nan> m_NaN() {
|
|
|
|
return cstfp_pred_ty<is_nan>();
|
|
|
|
}
|
|
|
|
|
2018-03-15 15:29:27 +01:00
|
|
|
struct is_any_zero_fp {
|
|
|
|
bool isValue(const APFloat &C) { return C.isZero(); }
|
|
|
|
};
|
2018-03-25 23:16:33 +02:00
|
|
|
/// Match a floating-point negative zero or positive zero.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
2018-03-15 15:29:27 +01:00
|
|
|
inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
|
|
|
|
return cstfp_pred_ty<is_any_zero_fp>();
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:16:33 +02:00
|
|
|
struct is_pos_zero_fp {
|
|
|
|
bool isValue(const APFloat &C) { return C.isPosZero(); }
|
|
|
|
};
|
|
|
|
/// Match a floating-point positive zero.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
|
|
|
inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
|
|
|
|
return cstfp_pred_ty<is_pos_zero_fp>();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct is_neg_zero_fp {
|
|
|
|
bool isValue(const APFloat &C) { return C.isNegZero(); }
|
|
|
|
};
|
|
|
|
/// Match a floating-point negative zero.
|
|
|
|
/// For vectors, this includes constants with undefined elements.
|
|
|
|
inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
|
|
|
|
return cstfp_pred_ty<is_neg_zero_fp>();
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-12-20 04:04:38 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Class> struct bind_ty {
|
2004-07-30 09:45:00 +02:00
|
|
|
Class *&VR;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2004-08-04 06:45:29 +02:00
|
|
|
bind_ty(Class *&V) : VR(V) {}
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (auto *CV = dyn_cast<Class>(V)) {
|
2004-07-30 09:45:00 +02:00
|
|
|
VR = CV;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a value, capturing it if we match.
|
2004-07-30 09:45:00 +02:00
|
|
|
inline bind_ty<Value> m_Value(Value *&V) { return V; }
|
2016-08-13 00:16:05 +02:00
|
|
|
inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
|
2007-12-20 05:47:44 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an instruction, capturing it if we match.
|
2015-04-10 23:07:09 +02:00
|
|
|
inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a binary operator, capturing it if we match.
|
2014-12-09 17:36:10 +01:00
|
|
|
inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a ConstantInt, capturing the value if we match.
|
2004-07-30 09:45:00 +02:00
|
|
|
inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a Constant, capturing the value if we match.
|
2011-02-09 18:00:45 +01:00
|
|
|
inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a ConstantFP, capturing the value if we match.
|
2012-12-12 01:23:43 +01:00
|
|
|
inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a specified Value*.
|
2008-11-16 05:38:30 +01:00
|
|
|
struct specificval_ty {
|
|
|
|
const Value *Val;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2008-11-16 05:38:30 +01:00
|
|
|
specificval_ty(const Value *V) : Val(V) {}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) { return V == Val; }
|
2008-11-16 05:38:30 +01:00
|
|
|
};
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match if we have a specific specified value.
|
2008-11-16 05:38:30 +01:00
|
|
|
inline specificval_ty m_Specific(const Value *V) { return V; }
|
2009-02-20 23:51:36 +01:00
|
|
|
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
/// Stores a reference to the Value *, not the Value * itself,
|
|
|
|
/// thus can be used in commutative matchers.
|
|
|
|
template <typename Class> struct deferredval_ty {
|
|
|
|
Class *const &Val;
|
|
|
|
|
|
|
|
deferredval_ty(Class *const &V) : Val(V) {}
|
|
|
|
|
|
|
|
template <typename ITy> bool match(ITy *const V) { return V == Val; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A commutative-friendly version of m_Specific().
|
|
|
|
inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
|
|
|
|
inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a specified floating point value or vector of all elements of
|
2014-12-09 19:56:35 +01:00
|
|
|
/// that value.
|
2012-12-12 01:23:43 +01:00
|
|
|
struct specific_fpval {
|
|
|
|
double Val;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2012-12-12 01:23:43 +01:00
|
|
|
specific_fpval(double V) : Val(V) {}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (const auto *CFP = dyn_cast<ConstantFP>(V))
|
2012-12-12 01:23:43 +01:00
|
|
|
return CFP->isExactlyValue(Val);
|
|
|
|
if (V->getType()->isVectorTy())
|
2014-12-09 19:56:35 +01:00
|
|
|
if (const auto *C = dyn_cast<Constant>(V))
|
|
|
|
if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
|
2012-12-12 01:23:43 +01:00
|
|
|
return CFP->isExactlyValue(Val);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a specific floating point value or vector with all elements
|
2014-12-09 19:56:35 +01:00
|
|
|
/// equal to the value.
|
2012-12-12 01:23:43 +01:00
|
|
|
inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a float 1.0 or vector with all elements equal to 1.0.
|
2012-12-12 01:23:43 +01:00
|
|
|
inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
|
|
|
|
|
2011-04-26 21:50:39 +02:00
|
|
|
struct bind_const_intval_ty {
|
|
|
|
uint64_t &VR;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2011-04-26 21:50:39 +02:00
|
|
|
bind_const_intval_ty(uint64_t &V) : VR(V) {}
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
if (const auto *CV = dyn_cast<ConstantInt>(V))
|
2017-06-28 20:07:29 +02:00
|
|
|
if (CV->getValue().ule(UINT64_MAX)) {
|
2011-04-26 21:50:39 +02:00
|
|
|
VR = CV->getZExtValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a specified integer value or vector of all elements of that
|
2014-12-09 19:56:35 +01:00
|
|
|
// value.
|
2014-08-27 22:06:19 +02:00
|
|
|
struct specific_intval {
|
|
|
|
uint64_t Val;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2014-08-27 22:06:19 +02:00
|
|
|
specific_intval(uint64_t V) : Val(V) {}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename ITy> bool match(ITy *V) {
|
|
|
|
const auto *CI = dyn_cast<ConstantInt>(V);
|
2014-08-27 22:06:19 +02:00
|
|
|
if (!CI && V->getType()->isVectorTy())
|
|
|
|
if (const auto *C = dyn_cast<Constant>(V))
|
|
|
|
CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
|
|
|
|
|
2017-06-27 17:39:40 +02:00
|
|
|
return CI && CI->getValue() == Val;
|
2014-08-27 22:06:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a specific integer value or vector with all elements equal to
|
2014-12-09 19:56:35 +01:00
|
|
|
/// the value.
|
2014-08-27 22:06:19 +02:00
|
|
|
inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match a ConstantInt and bind to its value. This does not match
|
2014-12-09 19:56:35 +01:00
|
|
|
/// ConstantInts wider than 64-bits.
|
2011-04-26 21:50:39 +02:00
|
|
|
inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2014-12-09 17:36:10 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matcher for any binary operator.
|
|
|
|
//
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS_t, typename RHS_t, bool Commutable = false>
|
|
|
|
struct AnyBinaryOp_match {
|
2014-12-09 17:36:10 +01:00
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
// The evaluation order is always stable, regardless of Commutability.
|
|
|
|
// The LHS is always matched first.
|
2014-12-09 17:36:10 +01:00
|
|
|
AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2014-12-09 17:36:10 +01:00
|
|
|
if (auto *I = dyn_cast<BinaryOperator>(V))
|
2017-06-25 00:59:10 +02:00
|
|
|
return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
(Commutable && L.match(I->getOperand(1)) &&
|
|
|
|
R.match(I->getOperand(0)));
|
2014-12-09 17:36:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
|
2014-12-09 17:36:10 +01:00
|
|
|
return AnyBinaryOp_match<LHS, RHS>(L, R);
|
|
|
|
}
|
|
|
|
|
2004-07-30 09:45:00 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2006-06-15 21:25:28 +02:00
|
|
|
// Matchers for specific binary operators.
|
2004-07-30 09:45:00 +02:00
|
|
|
//
|
|
|
|
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS_t, typename RHS_t, unsigned Opcode,
|
|
|
|
bool Commutable = false>
|
2004-07-30 09:45:00 +02:00
|
|
|
struct BinaryOp_match {
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
// The evaluation order is always stable, regardless of Commutability.
|
|
|
|
// The LHS is always matched first.
|
2004-07-30 09:45:00 +02:00
|
|
|
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2007-04-13 20:12:09 +02:00
|
|
|
if (V->getValueID() == Value::InstructionVal + Opcode) {
|
2014-12-09 19:56:35 +01:00
|
|
|
auto *I = cast<BinaryOperator>(V);
|
2017-06-25 00:59:10 +02:00
|
|
|
return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
(Commutable && L.match(I->getOperand(1)) &&
|
|
|
|
R.match(I->getOperand(0)));
|
2005-09-27 08:38:05 +02:00
|
|
|
}
|
2014-12-09 19:56:35 +01:00
|
|
|
if (auto *CE = dyn_cast<ConstantExpr>(V))
|
2017-06-25 00:59:10 +02:00
|
|
|
return CE->getOpcode() == Opcode &&
|
|
|
|
((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
(Commutable && L.match(CE->getOperand(1)) &&
|
|
|
|
R.match(CE->getOperand(0))));
|
2004-07-30 09:45:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-21 22:48:15 +02:00
|
|
|
};
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
|
|
|
|
const RHS &R) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
|
|
|
|
const RHS &R) {
|
2009-06-05 00:49:04 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
|
|
|
|
const RHS &R) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
|
|
|
|
const RHS &R) {
|
2009-06-05 00:49:04 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-10-24 16:45:18 +02:00
|
|
|
template <typename Op_t> struct FNeg_match {
|
|
|
|
Op_t X;
|
|
|
|
|
|
|
|
FNeg_match(const Op_t &Op) : X(Op) {}
|
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
auto *FPMO = dyn_cast<FPMathOperator>(V);
|
|
|
|
if (!FPMO || FPMO->getOpcode() != Instruction::FSub)
|
|
|
|
return false;
|
|
|
|
if (FPMO->hasNoSignedZeros()) {
|
|
|
|
// With 'nsz', any zero goes.
|
|
|
|
if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Without 'nsz', we need fsub -0.0, X exactly.
|
|
|
|
if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return X.match(FPMO->getOperand(1));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-05 17:36:55 +02:00
|
|
|
/// Match 'fneg X' as 'fsub -0.0, X'.
|
2018-10-24 16:45:18 +02:00
|
|
|
template <typename OpTy>
|
|
|
|
inline FNeg_match<OpTy>
|
|
|
|
m_FNeg(const OpTy &X) {
|
|
|
|
return FNeg_match<OpTy>(X);
|
2018-04-05 17:36:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 23:48:00 +02:00
|
|
|
/// Match 'fneg X' as 'fsub +-0.0, X'.
|
|
|
|
template <typename RHS>
|
|
|
|
inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
|
|
|
|
m_FNegNSZ(const RHS &X) {
|
|
|
|
return m_FSub(m_AnyZeroFP(), X);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
|
|
|
|
const RHS &R) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
|
|
|
|
const RHS &R) {
|
2009-06-05 00:49:04 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
|
|
|
|
const RHS &R) {
|
2006-10-26 08:15:43 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
|
|
|
|
const RHS &R) {
|
2006-10-26 08:15:43 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
|
|
|
|
const RHS &R) {
|
2006-10-26 08:15:43 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
|
2004-07-30 09:45:00 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
|
|
|
|
const RHS &R) {
|
2006-11-02 02:53:59 +01:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
|
|
|
|
const RHS &R) {
|
2006-11-02 02:53:59 +01:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
|
|
|
|
const RHS &R) {
|
2006-11-02 02:53:59 +01:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
|
2004-07-30 09:45:00 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
|
|
|
|
const RHS &R) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
|
|
|
|
const RHS &R) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
|
|
|
|
const RHS &R) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
|
|
|
|
const RHS &R) {
|
2007-02-02 03:16:23 +01:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
|
2004-11-13 20:32:45 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
|
|
|
|
const RHS &R) {
|
2007-02-02 03:16:23 +01:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
|
2006-11-08 07:47:33 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
|
|
|
|
const RHS &R) {
|
2007-02-02 03:16:23 +01:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
|
2006-11-08 07:47:33 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS_t, typename RHS_t, unsigned Opcode,
|
|
|
|
unsigned WrapFlags = 0>
|
2014-01-05 04:28:29 +01:00
|
|
|
struct OverflowingBinaryOp_match {
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
|
|
|
|
: L(LHS), R(RHS) {}
|
2014-01-05 04:28:29 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
|
2014-01-05 04:28:29 +01:00
|
|
|
if (Op->getOpcode() != Opcode)
|
|
|
|
return false;
|
|
|
|
if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
|
|
|
|
!Op->hasNoUnsignedWrap())
|
|
|
|
return false;
|
|
|
|
if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
|
|
|
|
!Op->hasNoSignedWrap())
|
|
|
|
return false;
|
|
|
|
return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>
|
|
|
|
m_NSWAdd(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>
|
|
|
|
m_NSWSub(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>
|
|
|
|
m_NSWMul(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>
|
|
|
|
m_NSWShl(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
|
|
|
|
OverflowingBinaryOperator::NoSignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>
|
|
|
|
m_NUWAdd(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>
|
|
|
|
m_NUWSub(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>
|
|
|
|
m_NUWMul(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>
|
|
|
|
m_NUWShl(const LHS &L, const RHS &R) {
|
|
|
|
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
|
|
|
|
OverflowingBinaryOperator::NoUnsignedWrap>(
|
|
|
|
L, R);
|
|
|
|
}
|
|
|
|
|
2017-06-24 08:27:14 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Class that matches a group of binary opcodes.
|
|
|
|
//
|
|
|
|
template <typename LHS_t, typename RHS_t, typename Predicate>
|
|
|
|
struct BinOpPred_match : Predicate {
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
|
|
|
BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
|
|
|
|
|
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (auto *I = dyn_cast<Instruction>(V))
|
2017-06-24 09:02:52 +02:00
|
|
|
return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
|
|
|
|
R.match(I->getOperand(1));
|
2017-06-24 08:27:14 +02:00
|
|
|
if (auto *CE = dyn_cast<ConstantExpr>(V))
|
2017-06-24 09:02:52 +02:00
|
|
|
return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
|
|
|
|
R.match(CE->getOperand(1));
|
2017-06-24 08:27:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct is_shift_op {
|
|
|
|
bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
|
|
|
|
};
|
|
|
|
|
2017-06-24 09:02:52 +02:00
|
|
|
struct is_right_shift_op {
|
|
|
|
bool isOpType(unsigned Opcode) {
|
|
|
|
return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct is_logical_shift_op {
|
|
|
|
bool isOpType(unsigned Opcode) {
|
|
|
|
return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-24 08:27:14 +02:00
|
|
|
struct is_bitwiselogic_op {
|
2017-06-24 09:02:52 +02:00
|
|
|
bool isOpType(unsigned Opcode) {
|
|
|
|
return Instruction::isBitwiseLogicOp(Opcode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct is_idiv_op {
|
|
|
|
bool isOpType(unsigned Opcode) {
|
|
|
|
return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
|
|
|
|
}
|
2017-06-24 08:27:14 +02:00
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches shift operations.
|
2017-06-24 08:27:14 +02:00
|
|
|
template <typename LHS, typename RHS>
|
2017-06-24 09:02:52 +02:00
|
|
|
inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
|
|
|
|
const RHS &R) {
|
2017-06-24 08:27:14 +02:00
|
|
|
return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches logical shift operations.
|
2017-06-24 09:02:52 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches logical shift operations.
|
2017-06-24 09:02:52 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
|
|
|
|
m_LogicalShift(const LHS &L, const RHS &R) {
|
|
|
|
return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches bitwise logic operations.
|
2017-06-24 08:27:14 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
|
|
|
|
m_BitwiseLogic(const LHS &L, const RHS &R) {
|
|
|
|
return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches integer division operations.
|
2017-06-24 09:02:52 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
|
|
|
|
}
|
|
|
|
|
2012-01-01 18:55:30 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Class that matches exact binary ops.
|
|
|
|
//
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename SubPattern_t> struct Exact_match {
|
2012-01-01 18:55:30 +01:00
|
|
|
SubPattern_t SubPattern;
|
|
|
|
|
|
|
|
Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2016-08-13 00:16:05 +02:00
|
|
|
if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
|
2012-01-01 18:55:30 +01:00
|
|
|
return PEO->isExact() && SubPattern.match(V);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
|
|
|
|
return SubPattern;
|
|
|
|
}
|
2012-01-01 18:55:30 +01:00
|
|
|
|
2006-12-23 07:05:41 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for CmpInst classes
|
|
|
|
//
|
|
|
|
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
|
|
|
|
bool Commutable = false>
|
2006-12-23 07:05:41 +01:00
|
|
|
struct CmpClass_match {
|
|
|
|
PredicateTy &Predicate;
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
// The evaluation order is always stable, regardless of Commutability.
|
|
|
|
// The LHS is always matched first.
|
2011-02-09 18:00:45 +01:00
|
|
|
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
|
2014-12-09 19:56:35 +01:00
|
|
|
: Predicate(Pred), L(LHS), R(RHS) {}
|
2006-12-23 07:05:41 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2016-08-13 00:16:05 +02:00
|
|
|
if (auto *I = dyn_cast<Class>(V))
|
2017-06-25 00:59:10 +02:00
|
|
|
if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
(Commutable && L.match(I->getOperand(1)) &&
|
|
|
|
R.match(I->getOperand(0)))) {
|
2006-12-23 07:05:41 +01:00
|
|
|
Predicate = I->getPredicate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
2014-12-09 17:36:10 +01:00
|
|
|
inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
|
|
|
|
m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
|
|
|
|
return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
2006-12-23 07:05:41 +01:00
|
|
|
inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
|
|
|
|
m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
|
2014-12-09 17:36:10 +01:00
|
|
|
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
|
2006-12-23 07:05:41 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
2006-12-23 07:05:41 +01:00
|
|
|
inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
|
|
|
|
m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
|
2014-12-09 17:36:10 +01:00
|
|
|
return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
|
2006-12-23 07:05:41 +01:00
|
|
|
}
|
|
|
|
|
2008-10-30 21:40:10 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-09-12 16:52:38 +02:00
|
|
|
// Matchers for instructions with a given opcode and number of operands.
|
2008-10-30 21:40:10 +01:00
|
|
|
//
|
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches instructions with Opcode and three operands.
|
|
|
|
template <typename T0, unsigned Opcode> struct OneOps_match {
|
|
|
|
T0 Op1;
|
|
|
|
|
|
|
|
OneOps_match(const T0 &Op1) : Op1(Op1) {}
|
|
|
|
|
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (V->getValueID() == Value::InstructionVal + Opcode) {
|
|
|
|
auto *I = cast<Instruction>(V);
|
|
|
|
return Op1.match(I->getOperand(0));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Matches instructions with Opcode and three operands.
|
|
|
|
template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
|
|
|
|
T0 Op1;
|
|
|
|
T1 Op2;
|
2008-10-30 21:40:10 +01:00
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
|
2008-10-30 21:40:10 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2018-09-12 16:52:38 +02:00
|
|
|
if (V->getValueID() == Value::InstructionVal + Opcode) {
|
|
|
|
auto *I = cast<Instruction>(V);
|
|
|
|
return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Matches instructions with Opcode and three operands.
|
|
|
|
template <typename T0, typename T1, typename T2, unsigned Opcode>
|
|
|
|
struct ThreeOps_match {
|
|
|
|
T0 Op1;
|
|
|
|
T1 Op2;
|
|
|
|
T2 Op3;
|
|
|
|
|
|
|
|
ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
|
|
|
|
: Op1(Op1), Op2(Op2), Op3(Op3) {}
|
|
|
|
|
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (V->getValueID() == Value::InstructionVal + Opcode) {
|
|
|
|
auto *I = cast<Instruction>(V);
|
|
|
|
return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
|
|
|
|
Op3.match(I->getOperand(2));
|
|
|
|
}
|
2008-10-30 21:40:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches SelectInst.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Cond, typename LHS, typename RHS>
|
2018-09-12 16:52:38 +02:00
|
|
|
inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
|
|
|
|
m_Select(const Cond &C, const LHS &L, const RHS &R) {
|
|
|
|
return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
|
2008-10-30 21:40:10 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// This matches a select of two constants, e.g.:
|
2014-12-09 19:56:35 +01:00
|
|
|
/// m_SelectCst<-1, 0>(m_Value(V))
|
|
|
|
template <int64_t L, int64_t R, typename Cond>
|
2018-09-12 16:52:38 +02:00
|
|
|
inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
|
|
|
|
Instruction::Select>
|
2009-01-06 00:53:12 +01:00
|
|
|
m_SelectCst(const Cond &C) {
|
2011-02-09 18:00:45 +01:00
|
|
|
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
|
2008-11-16 05:33:10 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches InsertElementInst.
|
2018-03-28 17:39:00 +02:00
|
|
|
template <typename Val_t, typename Elt_t, typename Idx_t>
|
2018-09-12 16:52:38 +02:00
|
|
|
inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
|
2018-03-28 17:39:00 +02:00
|
|
|
m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
|
2018-09-12 16:52:38 +02:00
|
|
|
return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
|
|
|
|
Val, Elt, Idx);
|
2018-03-28 17:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches ExtractElementInst.
|
2018-03-28 17:39:00 +02:00
|
|
|
template <typename Val_t, typename Idx_t>
|
2018-09-12 16:52:38 +02:00
|
|
|
inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
|
2018-03-28 17:39:00 +02:00
|
|
|
m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
|
2018-09-12 16:52:38 +02:00
|
|
|
return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
|
2018-03-28 17:39:00 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches ShuffleVectorInst.
|
2018-03-28 17:39:00 +02:00
|
|
|
template <typename V1_t, typename V2_t, typename Mask_t>
|
2018-09-12 16:52:38 +02:00
|
|
|
inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>
|
|
|
|
m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
|
|
|
|
return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2,
|
|
|
|
m);
|
|
|
|
}
|
2018-03-28 17:39:00 +02:00
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches LoadInst.
|
|
|
|
template <typename OpTy>
|
|
|
|
inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
|
|
|
|
return OneOps_match<OpTy, Instruction::Load>(Op);
|
|
|
|
}
|
2018-03-28 17:39:00 +02:00
|
|
|
|
2018-09-12 16:52:38 +02:00
|
|
|
/// Matches StoreInst.
|
|
|
|
template <typename ValueOpTy, typename PointerOpTy>
|
|
|
|
inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
|
|
|
|
m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
|
|
|
|
return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
|
|
|
|
PointerOp);
|
2018-03-28 17:39:00 +02:00
|
|
|
}
|
|
|
|
|
2008-01-08 08:02:44 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for CastInst classes
|
|
|
|
//
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Op_t, unsigned Opcode> struct CastClass_match {
|
2008-01-08 08:02:44 +01:00
|
|
|
Op_t Op;
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2008-01-08 08:02:44 +01:00
|
|
|
CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (auto *O = dyn_cast<Operator>(V))
|
2012-01-01 18:55:23 +01:00
|
|
|
return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
|
2008-01-08 08:02:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches BitCast.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
|
2010-08-26 23:35:52 +02:00
|
|
|
return CastClass_match<OpTy, Instruction::BitCast>(Op);
|
|
|
|
}
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches PtrToInt.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
|
2010-01-01 23:29:12 +01:00
|
|
|
return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
|
2008-01-08 08:02:44 +01:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches Trunc.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
|
2010-01-01 23:29:12 +01:00
|
|
|
return CastClass_match<OpTy, Instruction::Trunc>(Op);
|
|
|
|
}
|
2010-01-24 01:09:49 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches SExt.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
|
2010-01-24 01:09:49 +01:00
|
|
|
return CastClass_match<OpTy, Instruction::SExt>(Op);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches ZExt.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
|
2010-01-24 01:09:49 +01:00
|
|
|
return CastClass_match<OpTy, Instruction::ZExt>(Op);
|
|
|
|
}
|
2012-12-07 22:41:53 +01:00
|
|
|
|
2017-03-10 01:47:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
|
|
|
|
CastClass_match<OpTy, Instruction::SExt>>
|
|
|
|
m_ZExtOrSExt(const OpTy &Op) {
|
|
|
|
return m_CombineOr(m_ZExt(Op), m_SExt(Op));
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches UIToFP.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
|
Provide InstCombines for the following 3 cases:
A * (1 - (uitofp i1 C)) -> select C, 0, A
B * (uitofp i1 C) -> select C, B, 0
select C, 0, A + select C, B, 0 -> select C, B, A
These come up in code that has been hand-optimized from a select to a linear blend,
on platforms where that may have mattered. We want to undo such changes
with the following transform:
A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B
llvm-svn: 181216
2013-05-06 18:55:50 +02:00
|
|
|
return CastClass_match<OpTy, Instruction::UIToFP>(Op);
|
|
|
|
}
|
2009-02-20 23:51:36 +01:00
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches SIToFP.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
|
2013-07-26 19:55:00 +02:00
|
|
|
return CastClass_match<OpTy, Instruction::SIToFP>(Op);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches FPTrunc
|
2017-01-17 01:10:40 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches FPExt
|
2017-01-17 01:10:40 +01:00
|
|
|
template <typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::FPExt>(Op);
|
|
|
|
}
|
|
|
|
|
2004-07-30 09:45:00 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-02-09 18:00:45 +01:00
|
|
|
// Matchers for control flow.
|
2004-07-30 09:45:00 +02:00
|
|
|
//
|
|
|
|
|
2013-01-07 19:37:41 +01:00
|
|
|
struct br_match {
|
|
|
|
BasicBlock *&Succ;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
br_match(BasicBlock *&Succ) : Succ(Succ) {}
|
2013-01-07 19:37:41 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (auto *BI = dyn_cast<BranchInst>(V))
|
2013-01-07 19:37:41 +01:00
|
|
|
if (BI->isUnconditional()) {
|
|
|
|
Succ = BI->getSuccessor(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Cond_t> struct brc_match {
|
2004-07-30 09:45:00 +02:00
|
|
|
Cond_t Cond;
|
|
|
|
BasicBlock *&T, *&F;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2004-07-30 09:45:00 +02:00
|
|
|
brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
|
2014-12-09 19:56:35 +01:00
|
|
|
: Cond(C), T(t), F(f) {}
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (auto *BI = dyn_cast<BranchInst>(V))
|
2011-02-09 18:00:45 +01:00
|
|
|
if (BI->isConditional() && Cond.match(BI->getCondition())) {
|
|
|
|
T = BI->getSuccessor(0);
|
|
|
|
F = BI->getSuccessor(1);
|
|
|
|
return true;
|
2004-07-30 09:45:00 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Cond_t>
|
2009-01-02 21:26:30 +01:00
|
|
|
inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
|
2004-07-30 09:45:00 +02:00
|
|
|
return brc_match<Cond_t>(C, T, F);
|
|
|
|
}
|
|
|
|
|
2011-05-03 21:53:10 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
|
|
|
|
//
|
|
|
|
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
|
|
|
|
bool Commutable = false>
|
2011-05-03 21:53:10 +02:00
|
|
|
struct MaxMin_match {
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
// The evaluation order is always stable, regardless of Commutability.
|
|
|
|
// The LHS is always matched first.
|
2014-12-09 19:56:35 +01:00
|
|
|
MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
|
2011-05-03 21:53:10 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2015-11-04 09:36:53 +01:00
|
|
|
// Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
|
|
|
|
auto *SI = dyn_cast<SelectInst>(V);
|
|
|
|
if (!SI)
|
|
|
|
return false;
|
|
|
|
auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
|
|
|
|
if (!Cmp)
|
|
|
|
return false;
|
|
|
|
// At this point we have a select conditioned on a comparison. Check that
|
|
|
|
// it is the values returned by the select that are being compared.
|
|
|
|
Value *TrueVal = SI->getTrueValue();
|
|
|
|
Value *FalseVal = SI->getFalseValue();
|
|
|
|
Value *LHS = Cmp->getOperand(0);
|
|
|
|
Value *RHS = Cmp->getOperand(1);
|
|
|
|
if ((TrueVal != LHS || FalseVal != RHS) &&
|
|
|
|
(TrueVal != RHS || FalseVal != LHS))
|
|
|
|
return false;
|
|
|
|
typename CmpInst_t::Predicate Pred =
|
2017-06-13 19:18:45 +02:00
|
|
|
LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
|
2015-11-04 09:36:53 +01:00
|
|
|
// Does "(x pred y) ? x : y" represent the desired max/min operation?
|
|
|
|
if (!Pred_t::match(Pred))
|
|
|
|
return false;
|
|
|
|
// It does! Bind the operands.
|
2017-06-25 00:59:10 +02:00
|
|
|
return (L.match(LHS) && R.match(RHS)) ||
|
[PatternMatch] Stabilize the matching order of commutative matchers
Summary:
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the `LHS` and `RHS` matchers:
1. match `RHS` matcher to the `first` operand of binary operator,
2. and then match `LHS` matcher to the `second` operand of binary operator.
This works ok.
But it complicates writing of commutative matchers, where one would like to match
(`m_Value()`) the value on one side, and use (`m_Specific()`) it on the other side.
This is additionally complicated by the fact that `m_Specific()` stores the `Value *`,
not `Value **`, so it won't work at all out of the box.
The last problem is trivially solved by adding a new `m_c_Specific()` that stores the
`Value **`, not `Value *`. I'm choosing to add a new matcher, not change the existing
one because i guess all the current users are ok with existing behavior,
and this additional pointer indirection may have performance drawbacks.
Also, i'm storing pointer, not reference, because for some mysterious-to-me reason
it did not work with the reference.
The first one appears trivial, too.
Currently, we
1. match `LHS` matcher to the `first` operand of binary operator,
2. and then match `RHS` matcher to the `second` operand of binary operator.
If that does not match, we swap the ~~`LHS` and `RHS` matchers~~ **operands**:
1. match ~~`RHS`~~ **`LHS`** matcher to the ~~`first`~~ **`second`** operand of binary operator,
2. and then match ~~`LHS`~~ **`RHS`** matcher to the ~~`second`~ **`first`** operand of binary operator.
Surprisingly, `$ ninja check-llvm` still passes with this.
But i expect the bots will disagree..
The motivational unittest is included.
I'd like to use this in D45664.
Reviewers: spatel, craig.topper, arsenm, RKSimon
Reviewed By: craig.topper
Subscribers: xbolva00, wdng, llvm-commits
Differential Revision: https://reviews.llvm.org/D45828
llvm-svn: 331085
2018-04-27 23:23:20 +02:00
|
|
|
(Commutable && L.match(RHS) && R.match(LHS));
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying signed max predicates.
|
2011-05-03 21:53:10 +02:00
|
|
|
struct smax_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(ICmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying signed min predicates.
|
2011-05-03 21:53:10 +02:00
|
|
|
struct smin_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(ICmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying unsigned max predicates.
|
2011-05-03 21:53:10 +02:00
|
|
|
struct umax_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(ICmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying unsigned min predicates.
|
2011-05-03 21:53:10 +02:00
|
|
|
struct umin_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(ICmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying ordered max predicates.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
struct ofmax_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(FCmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying ordered min predicates.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
struct ofmin_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(FCmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying unordered max predicates.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
struct ufmax_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(FCmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Helper class for identifying unordered min predicates.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
struct ufmin_pred_ty {
|
2015-11-04 09:36:53 +01:00
|
|
|
static bool match(FCmpInst::Predicate Pred) {
|
|
|
|
return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
|
|
|
|
const RHS &R) {
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
|
|
|
|
const RHS &R) {
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
|
|
|
|
const RHS &R) {
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
|
2011-05-03 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
|
|
|
|
const RHS &R) {
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an 'ordered' floating point maximum function.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
/// 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
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
|
|
|
|
const RHS &R) {
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an 'ordered' floating point minimum function.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
/// 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.
|
|
|
|
///
|
2017-06-13 19:18:45 +02:00
|
|
|
/// min(L, R) iff L and R are not NaN
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
/// m_OrdFMin(L, R) = R iff L or R are NaN
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
|
|
|
|
const RHS &R) {
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an 'unordered' floating point maximum function.
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
/// 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
|
2017-06-13 19:18:45 +02:00
|
|
|
/// m_UnordFMax(L, R) = L iff L or R are NaN
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename LHS, typename RHS>
|
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
llvm-svn: 181143
2013-05-05 03:54:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an 'unordered' floating point minimum function.
|
2017-06-13 19:18:45 +02:00
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// min(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);
|
|
|
|
}
|
|
|
|
|
2015-04-10 23:07:09 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for overflow check patterns: e.g. (a + b) u< a
|
|
|
|
//
|
|
|
|
|
|
|
|
template <typename LHS_t, typename RHS_t, typename Sum_t>
|
|
|
|
struct UAddWithOverflow_match {
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
Sum_t S;
|
|
|
|
|
|
|
|
UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
|
|
|
|
: L(L), R(R), S(S) {}
|
|
|
|
|
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
Value *ICmpLHS, *ICmpRHS;
|
|
|
|
ICmpInst::Predicate Pred;
|
|
|
|
if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Value *AddLHS, *AddRHS;
|
|
|
|
auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
|
|
|
|
|
|
|
|
// (a + b) u< a, (a + b) u< b
|
|
|
|
if (Pred == ICmpInst::ICMP_ULT)
|
|
|
|
if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
|
|
|
|
return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
|
|
|
|
|
|
|
|
// a >u (a + b), b >u (a + b)
|
|
|
|
if (Pred == ICmpInst::ICMP_UGT)
|
|
|
|
if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
|
|
|
|
return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an icmp instruction checking for unsigned overflow on addition.
|
2015-04-10 23:07:09 +02:00
|
|
|
///
|
|
|
|
/// S is matched to the addition whose result is being checked for overflow, and
|
|
|
|
/// L and R are matched to the LHS and RHS of S.
|
|
|
|
template <typename LHS_t, typename RHS_t, typename Sum_t>
|
|
|
|
UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
|
|
|
|
m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
|
|
|
|
return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Opnd_t> struct Argument_match {
|
2012-12-13 04:13:36 +01:00
|
|
|
unsigned OpI;
|
|
|
|
Opnd_t Val;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
|
2012-12-13 04:13:36 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
2012-12-13 04:13:36 +01:00
|
|
|
CallSite CS(V);
|
|
|
|
return CS.isCall() && Val.match(CS.getArgument(OpI));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match an argument.
|
2014-12-09 19:56:35 +01:00
|
|
|
template <unsigned OpI, typename Opnd_t>
|
2012-12-13 04:13:36 +01:00
|
|
|
inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
|
|
|
|
return Argument_match<Opnd_t>(OpI, Op);
|
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Intrinsic matchers.
|
2012-12-13 04:13:36 +01:00
|
|
|
struct IntrinsicID_match {
|
|
|
|
unsigned ID;
|
2017-05-13 00:25:07 +02:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
|
2012-12-13 04:13:36 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
if (const auto *CI = dyn_cast<CallInst>(V))
|
|
|
|
if (const auto *F = CI->getCalledFunction())
|
2014-10-25 20:09:01 +02:00
|
|
|
return F->getIntrinsicID() == ID;
|
|
|
|
return false;
|
2012-12-13 04:13:36 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Intrinsic matches are combinations of ID matchers, and argument
|
|
|
|
/// matchers. Higher arity matcher are defined recursively in terms of and-ing
|
|
|
|
/// them with lower arity matchers. Here's some convenient typedefs for up to
|
|
|
|
/// several arguments, and more can be added as needed
|
|
|
|
template <typename T0 = void, typename T1 = void, typename T2 = void,
|
|
|
|
typename T3 = void, typename T4 = void, typename T5 = void,
|
|
|
|
typename T6 = void, typename T7 = void, typename T8 = void,
|
2014-12-09 19:56:35 +01:00
|
|
|
typename T9 = void, typename T10 = void>
|
|
|
|
struct m_Intrinsic_Ty;
|
|
|
|
template <typename T0> struct m_Intrinsic_Ty<T0> {
|
2017-05-13 00:25:07 +02:00
|
|
|
using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
|
2012-12-13 04:13:36 +01:00
|
|
|
};
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
|
2017-05-13 00:25:07 +02:00
|
|
|
using Ty =
|
|
|
|
match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
|
2012-12-13 04:13:36 +01:00
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2>
|
|
|
|
struct m_Intrinsic_Ty<T0, T1, T2> {
|
2017-05-13 00:25:07 +02:00
|
|
|
using Ty =
|
|
|
|
match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
|
|
|
|
Argument_match<T2>>;
|
2012-12-13 04:13:36 +01:00
|
|
|
};
|
|
|
|
template <typename T0, typename T1, typename T2, typename T3>
|
|
|
|
struct m_Intrinsic_Ty<T0, T1, T2, T3> {
|
2017-05-13 00:25:07 +02:00
|
|
|
using Ty =
|
|
|
|
match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
|
|
|
|
Argument_match<T3>>;
|
2012-12-13 04:13:36 +01:00
|
|
|
};
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Match intrinsic calls like this:
|
2014-12-09 19:56:35 +01:00
|
|
|
/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
|
|
|
|
template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
|
|
|
|
return IntrinsicID_match(IntrID);
|
|
|
|
}
|
2012-12-13 04:13:36 +01:00
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <Intrinsic::ID IntrID, typename T0>
|
|
|
|
inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
|
2012-12-13 04:13:36 +01:00
|
|
|
return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <Intrinsic::ID IntrID, typename T0, typename T1>
|
|
|
|
inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
|
|
|
|
const T1 &Op1) {
|
2012-12-13 04:13:36 +01:00
|
|
|
return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
|
2012-12-13 04:13:36 +01:00
|
|
|
inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
|
|
|
|
m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
|
|
|
|
return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
|
|
|
|
typename T3>
|
2012-12-13 04:13:36 +01:00
|
|
|
inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
|
|
|
|
m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
|
|
|
|
return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
// Helper intrinsic matching specializations.
|
2017-06-30 20:58:29 +02:00
|
|
|
template <typename Opnd0>
|
|
|
|
inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
|
|
|
|
return m_Intrinsic<Intrinsic::bitreverse>(Op0);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Opnd0>
|
|
|
|
inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
|
2012-12-13 04:13:36 +01:00
|
|
|
return m_Intrinsic<Intrinsic::bswap>(Op0);
|
|
|
|
}
|
|
|
|
|
2018-07-27 11:04:35 +02:00
|
|
|
template <typename Opnd0>
|
|
|
|
inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
|
|
|
|
return m_Intrinsic<Intrinsic::fabs>(Op0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Opnd0>
|
|
|
|
inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
|
|
|
|
return m_Intrinsic<Intrinsic::canonicalize>(Op0);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Opnd0, typename Opnd1>
|
|
|
|
inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
|
|
|
|
const Opnd1 &Op1) {
|
2014-10-22 01:00:20 +02:00
|
|
|
return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:56:35 +01:00
|
|
|
template <typename Opnd0, typename Opnd1>
|
|
|
|
inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
|
|
|
|
const Opnd1 &Op1) {
|
2014-10-22 01:00:20 +02:00
|
|
|
return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
|
|
|
|
}
|
|
|
|
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for two-operands operators with the operators in either order
|
|
|
|
//
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches a BinaryOperator with LHS and RHS in either order.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
|
|
|
|
return AnyBinaryOp_match<LHS, RHS, true>(L, R);
|
2017-04-12 07:49:28 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches an ICmp with a predicate over LHS and RHS in either order.
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
/// Does not swap the predicate.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
|
2017-06-25 00:59:10 +02:00
|
|
|
return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
|
|
|
|
R);
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches a Add with LHS and RHS in either order.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
|
2017-04-06 06:02:33 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches a Mul with LHS and RHS in either order.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
|
2017-04-06 06:02:33 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches an And with LHS and RHS in either order.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches an Or with LHS and RHS in either order.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 17:19:22 +01:00
|
|
|
/// Matches an Xor with LHS and RHS in either order.
|
2017-06-25 00:59:10 +02:00
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
|
|
|
|
const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
|
Add optimization for 'icmp slt (or A, B), A' and some related idioms based on knowledge of the sign bit for A and B.
No matter what value you OR in to A, the result of (or A, B) is going to be UGE A. When A and B are positive, it's SGE too. If A is negative, OR'ing a value into it can't make it positive, but can increase its value closer to -1, therefore (or A, B) is SGE A. Working through all possible combinations produces this truth table:
```
A is
+, -, +/-
F F F + B is
T F ? -
? F ? +/-
```
The related optimizations are flipping the 'slt' for 'sge' which always NOTs the result (if the result is known), and swapping the LHS and RHS while swapping the comparison predicate.
There are more idioms left to implement (aren't there always!) but I've stopped here because any more would risk becoming unreasonable for reviewers.
llvm-svn: 266939
2016-04-21 02:53:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-01 15:42:57 +02:00
|
|
|
/// Matches a 'Neg' as 'sub 0, V'.
|
|
|
|
template <typename ValTy>
|
|
|
|
inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
|
|
|
|
m_Neg(const ValTy &V) {
|
|
|
|
return m_Sub(m_ZeroInt(), V);
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:19:42 +01:00
|
|
|
/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
|
|
|
|
template <typename ValTy>
|
|
|
|
inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
|
|
|
|
m_Not(const ValTy &V) {
|
|
|
|
return m_c_Xor(V, m_AllOnes());
|
|
|
|
}
|
|
|
|
|
2016-12-15 20:13:37 +01:00
|
|
|
/// Matches an SMin with LHS and RHS in either order.
|
2016-12-19 17:28:53 +01:00
|
|
|
template <typename LHS, typename RHS>
|
2017-06-25 00:59:10 +02:00
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
|
2016-12-15 20:13:37 +01:00
|
|
|
m_c_SMin(const LHS &L, const RHS &R) {
|
2017-06-25 00:59:10 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
|
2016-12-15 20:13:37 +01:00
|
|
|
}
|
2016-12-19 17:28:53 +01:00
|
|
|
/// Matches an SMax with LHS and RHS in either order.
|
|
|
|
template <typename LHS, typename RHS>
|
2017-06-25 00:59:10 +02:00
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
|
2016-12-19 17:28:53 +01:00
|
|
|
m_c_SMax(const LHS &L, const RHS &R) {
|
2017-06-25 00:59:10 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
|
2016-12-19 17:28:53 +01:00
|
|
|
}
|
2016-12-19 18:32:37 +01:00
|
|
|
/// Matches a UMin with LHS and RHS in either order.
|
|
|
|
template <typename LHS, typename RHS>
|
2017-06-25 00:59:10 +02:00
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
|
2016-12-19 18:32:37 +01:00
|
|
|
m_c_UMin(const LHS &L, const RHS &R) {
|
2017-06-25 00:59:10 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
|
2016-12-19 18:32:37 +01:00
|
|
|
}
|
|
|
|
/// Matches a UMax with LHS and RHS in either order.
|
|
|
|
template <typename LHS, typename RHS>
|
2017-06-25 00:59:10 +02:00
|
|
|
inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
|
2016-12-19 18:32:37 +01:00
|
|
|
m_c_UMax(const LHS &L, const RHS &R) {
|
2017-06-25 00:59:10 +02:00
|
|
|
return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
|
2016-12-19 18:32:37 +01:00
|
|
|
}
|
2016-12-15 20:13:37 +01:00
|
|
|
|
2018-01-30 01:18:37 +01:00
|
|
|
/// Matches FAdd with LHS and RHS in either order.
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
|
|
|
|
m_c_FAdd(const LHS &L, const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Matches FMul with LHS and RHS in either order.
|
|
|
|
template <typename LHS, typename RHS>
|
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
|
|
|
|
m_c_FMul(const LHS &L, const RHS &R) {
|
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
|
|
|
|
}
|
|
|
|
|
2018-07-01 15:42:57 +02:00
|
|
|
template <typename Opnd_t> struct Signum_match {
|
|
|
|
Opnd_t Val;
|
|
|
|
Signum_match(const Opnd_t &V) : Val(V) {}
|
|
|
|
|
|
|
|
template <typename OpTy> bool match(OpTy *V) {
|
|
|
|
unsigned TypeSize = V->getType()->getScalarSizeInBits();
|
|
|
|
if (TypeSize == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned ShiftWidth = TypeSize - 1;
|
|
|
|
Value *OpL = nullptr, *OpR = nullptr;
|
|
|
|
|
|
|
|
// This is the representation of signum we match:
|
|
|
|
//
|
|
|
|
// signum(x) == (x >> 63) | (-x >>u 63)
|
|
|
|
//
|
|
|
|
// An i1 value is its own signum, so it's correct to match
|
|
|
|
//
|
|
|
|
// signum(x) == (x >> 0) | (-x >>u 0)
|
|
|
|
//
|
|
|
|
// for i1 values.
|
|
|
|
|
|
|
|
auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
|
|
|
|
auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
|
|
|
|
auto Signum = m_Or(LHS, RHS);
|
|
|
|
|
|
|
|
return Signum.match(V) && OpL == OpR && Val.match(OpL);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Matches a signum pattern.
|
|
|
|
///
|
|
|
|
/// signum(x) =
|
|
|
|
/// x > 0 -> 1
|
|
|
|
/// x == 0 -> 0
|
|
|
|
/// x < 0 -> -1
|
|
|
|
template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
|
|
|
|
return Signum_match<Val_t>(V);
|
|
|
|
}
|
|
|
|
|
2009-01-02 21:26:30 +01:00
|
|
|
} // end namespace PatternMatch
|
|
|
|
} // end namespace llvm
|
2004-07-30 09:45:00 +02:00
|
|
|
|
2017-05-13 00:25:07 +02:00
|
|
|
#endif // LLVM_IR_PATTERNMATCH_H
|