[llvm] [PatternMatch] Use dyn_cast in CastInst_match instead of checking opcode. NFC (PR #79878)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 29 10:22:05 PST 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/79878

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.

>From 2d37ef31a82c52621d67523cdaea027aa6c275d6 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Mon, 29 Jan 2024 10:14:24 -0800
Subject: [PATCH] [PatternMatch] Use dyn_cast in CastInst_match instead of
 checking opcode. 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/include/llvm/IR/PatternMatch.h | 62 ++++++++++++++---------------
 1 file changed, 29 insertions(+), 33 deletions(-)

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);
 }
 
 //===----------------------------------------------------------------------===//



More information about the llvm-commits mailing list