Skip to content

[feature] Generalized pattern rewriting #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions llvm/include/llvm/IR/MatcherCast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef LLVM_IR_MATCHERCAST_H
#define LLVM_IR_MATCHERCAST_H

//===- MatcherCast.h - Match on the LLVM IR --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Parameterized class hierachy for templatized pattern matching.
//
//===----------------------------------------------------------------------===//


namespace llvm {
namespace PatternMatch {


// type modification
template<typename Matcher, typename DestClass>
struct MatcherCast {
using ActualCastType = DestClass;
};

// whether the Value \p Obj behaves like a \p Class.
template<typename MatcherClass, typename Class>
bool match_isa(const Value* Obj) {
using UnconstClass = typename std::remove_cv<Class>::type;
using DestClass = typename MatcherCast<MatcherClass, UnconstClass>::ActualCastType;
return isa<const DestClass>(Obj);
}

template<typename MatcherClass, typename Class>
auto match_cast(const Value* Obj) {
using UnconstClass = typename std::remove_cv<Class>::type;
using DestClass = typename MatcherCast<MatcherClass, UnconstClass>::ActualCastType;
return cast<const DestClass>(Obj);
}
template<typename MatcherClass, typename Class>
auto match_dyn_cast(const Value* Obj) {
using UnconstClass = typename std::remove_cv<Class>::type;
using DestClass = typename MatcherCast<MatcherClass, UnconstClass>::ActualCastType;
return dyn_cast<const DestClass>(Obj);
}

template<typename MatcherClass, typename Class>
auto match_cast(Value* Obj) {
using UnconstClass = typename std::remove_cv<Class>::type;
using DestClass = typename MatcherCast<MatcherClass, UnconstClass>::ActualCastType;
return cast<DestClass>(Obj);
}
template<typename MatcherClass, typename Class>
auto match_dyn_cast(Value* Obj) {
using UnconstClass = typename std::remove_cv<Class>::type;
using DestClass = typename MatcherCast<MatcherClass, UnconstClass>::ActualCastType;
return dyn_cast<DestClass>(Obj);
}


} // namespace PatternMatch

} // namespace llvm

#endif // LLVM_IR_MATCHERCAST_H

Loading