Skip to content

[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

Merged
merged 1 commit into from
Jan 29, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Jan 29, 2024

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.

…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.
@topperc topperc requested a review from nikic January 29, 2024 18:21
@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2024

@llvm/pr-subscribers-llvm-ir

Author: Craig Topper (topperc)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/79878.diff

1 Files Affected:

  • (modified) llvm/include/llvm/IR/PatternMatch.h (+29-33)
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);
 }
 
 //===----------------------------------------------------------------------===//

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@topperc topperc merged commit 2950594 into llvm:main Jan 29, 2024
@topperc topperc deleted the pr/castinst_match branch January 29, 2024 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants