-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[PatternMatch] Use dyn_cast in CastInst_match instead of checking opcode. NFC #79878
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
Conversation
…ode. NFC Using dyn_cast allows us to use CastInst::getOperand instead of Instruction::getOperand. This is more efficient since CastInst::getOperand doesn't need to check how the operands are stored. Instruction::getOperand has to consider HungOffUses.
@llvm/pr-subscribers-llvm-ir Author: Craig Topper (topperc) ChangesUsing dyn_cast allows us to use CastInst::getOperand instead of Instruction::getOperand. This is more efficient since CastInst::getOperand doesn't need to check how the operands are stored. Instruction::getOperand has to consider HungOffUses. Full diff: https://github.com/llvm/llvm-project/pull/79878.diff 1 Files Affected:
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 90d99a6031c834b..878079c4fe4e8ea 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -1663,14 +1663,14 @@ template <typename Op_t, unsigned Opcode> struct CastOperator_match {
}
};
-template <typename Op_t, unsigned Opcode> struct CastInst_match {
+template <typename Op_t, typename Class> struct CastInst_match {
Op_t Op;
CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {}
template <typename OpTy> bool match(OpTy *V) {
- if (auto *I = dyn_cast<Instruction>(V))
- return I->getOpcode() == Opcode && Op.match(I->getOperand(0));
+ if (auto *I = dyn_cast<Class>(V))
+ return Op.match(I->getOperand(0));
return false;
}
};
@@ -1698,9 +1698,8 @@ template <typename Op_t> struct NNegZExt_match {
NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {}
template <typename OpTy> bool match(OpTy *V) {
- if (auto *I = dyn_cast<Instruction>(V))
- return I->getOpcode() == Instruction::ZExt && I->hasNonNeg() &&
- Op.match(I->getOperand(0));
+ if (auto *I = dyn_cast<ZExtInst>(V))
+ return I->hasNonNeg() && Op.match(I->getOperand(0));
return false;
}
};
@@ -1746,14 +1745,14 @@ m_TruncOrSelf(const OpTy &Op) {
/// Matches SExt.
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::SExt>(Op);
+inline CastInst_match<OpTy, SExtInst> m_SExt(const OpTy &Op) {
+ return CastInst_match<OpTy, SExtInst>(Op);
}
/// Matches ZExt.
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::ZExt>(Op);
+inline CastInst_match<OpTy, ZExtInst> m_ZExt(const OpTy &Op) {
+ return CastInst_match<OpTy, ZExtInst>(Op);
}
template <typename OpTy>
@@ -1762,70 +1761,67 @@ inline NNegZExt_match<OpTy> m_NNegZExt(const OpTy &Op) {
}
template <typename OpTy>
-inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>, OpTy>
+inline match_combine_or<CastInst_match<OpTy, ZExtInst>, OpTy>
m_ZExtOrSelf(const OpTy &Op) {
return m_CombineOr(m_ZExt(Op), Op);
}
template <typename OpTy>
-inline match_combine_or<CastInst_match<OpTy, Instruction::SExt>, OpTy>
+inline match_combine_or<CastInst_match<OpTy, SExtInst>, OpTy>
m_SExtOrSelf(const OpTy &Op) {
return m_CombineOr(m_SExt(Op), Op);
}
/// Match either "sext" or "zext nneg".
template <typename OpTy>
-inline match_combine_or<CastInst_match<OpTy, Instruction::SExt>,
- NNegZExt_match<OpTy>>
+inline match_combine_or<CastInst_match<OpTy, SExtInst>, NNegZExt_match<OpTy>>
m_SExtLike(const OpTy &Op) {
return m_CombineOr(m_SExt(Op), m_NNegZExt(Op));
}
template <typename OpTy>
-inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>,
- CastInst_match<OpTy, Instruction::SExt>>
+inline match_combine_or<CastInst_match<OpTy, ZExtInst>,
+ CastInst_match<OpTy, SExtInst>>
m_ZExtOrSExt(const OpTy &Op) {
return m_CombineOr(m_ZExt(Op), m_SExt(Op));
}
template <typename OpTy>
-inline match_combine_or<
- match_combine_or<CastInst_match<OpTy, Instruction::ZExt>,
- CastInst_match<OpTy, Instruction::SExt>>,
- OpTy>
+inline match_combine_or<match_combine_or<CastInst_match<OpTy, ZExtInst>,
+ CastInst_match<OpTy, SExtInst>>,
+ OpTy>
m_ZExtOrSExtOrSelf(const OpTy &Op) {
return m_CombineOr(m_ZExtOrSExt(Op), Op);
}
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::UIToFP>(Op);
+inline CastInst_match<OpTy, UIToFPInst> m_UIToFP(const OpTy &Op) {
+ return CastInst_match<OpTy, UIToFPInst>(Op);
}
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::SIToFP>(Op);
+inline CastInst_match<OpTy, SIToFPInst> m_SIToFP(const OpTy &Op) {
+ return CastInst_match<OpTy, SIToFPInst>(Op);
}
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::FPToUI>(Op);
+inline CastInst_match<OpTy, FPToUIInst> m_FPToUI(const OpTy &Op) {
+ return CastInst_match<OpTy, FPToUIInst>(Op);
}
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::FPToSI>(Op);
+inline CastInst_match<OpTy, FPToSIInst> m_FPToSI(const OpTy &Op) {
+ return CastInst_match<OpTy, FPToSIInst>(Op);
}
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::FPTrunc>
-m_FPTrunc(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::FPTrunc>(Op);
+inline CastInst_match<OpTy, FPTruncInst> m_FPTrunc(const OpTy &Op) {
+ return CastInst_match<OpTy, FPTruncInst>(Op);
}
template <typename OpTy>
-inline CastInst_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
- return CastInst_match<OpTy, Instruction::FPExt>(Op);
+inline CastInst_match<OpTy, FPExtInst> m_FPExt(const OpTy &Op) {
+ return CastInst_match<OpTy, FPExtInst>(Op);
}
//===----------------------------------------------------------------------===//
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Using dyn_cast allows us to use CastInst::getOperand instead of Instruction::getOperand. This is more efficient since CastInst::getOperand doesn't need to check how the operands are stored. Instruction::getOperand has to consider HungOffUses.