[llvm] [IR] Add more efficient getOperand methods to some of the Operator subclasses. (PR #79943)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 29 21:52:59 PST 2024


https://github.com/topperc updated https://github.com/llvm/llvm-project/pull/79943

>From bf711e11cd0184fba7348c519a7b4e7c994f3682 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Mon, 29 Jan 2024 19:17:52 -0800
Subject: [PATCH 1/2] [IR] Add more efficient getOperand methods to some of the
 Operator subclasses.

ConstantExpr does not use HungOffUses. If we know that the
Instruction the Operator subclass can represent also does not
use HungOffUses, we can be more efficient than falling back to
User::getOperand.
---
 llvm/include/llvm/IR/Operator.h | 60 +++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/llvm/include/llvm/IR/Operator.h b/llvm/include/llvm/IR/Operator.h
index 12733529a74dc..6e81ee9870f5b 100644
--- a/llvm/include/llvm/IR/Operator.h
+++ b/llvm/include/llvm/IR/Operator.h
@@ -94,6 +94,9 @@ class OverflowingBinaryOperator : public Operator {
   }
 
 public:
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
   /// Test whether this operation is known to never
   /// undergo unsigned overflow, aka the nuw property.
   bool hasNoUnsignedWrap() const {
@@ -124,6 +127,13 @@ class OverflowingBinaryOperator : public Operator {
   }
 };
 
+template <>
+struct OperandTraits<OverflowingBinaryOperator> :
+  public FixedNumOperandTraits<OverflowingBinaryOperator, 2> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(OverflowingBinaryOperator, Value)
+
 /// A udiv or sdiv instruction, which can be marked as "exact",
 /// indicating that no bits are destroyed.
 class PossiblyExactOperator : public Operator {
@@ -141,6 +151,9 @@ class PossiblyExactOperator : public Operator {
   }
 
 public:
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
   /// Test whether this division is known to be exact, with zero remainder.
   bool isExact() const {
     return SubclassOptionalData & IsExact;
@@ -165,6 +178,13 @@ class PossiblyExactOperator : public Operator {
   }
 };
 
+template <>
+struct OperandTraits<PossiblyExactOperator> :
+  public FixedNumOperandTraits<PossiblyExactOperator, 2> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PossiblyExactOperator, Value)
+
 /// Utility class for floating point operations which can have
 /// information about relaxed accuracy requirements attached to them.
 class FPMathOperator : public Operator {
@@ -383,6 +403,9 @@ class GEPOperator
   }
 
 public:
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
   /// Test whether this is an inbounds GEP, as defined by LangRef.html.
   bool isInBounds() const {
     return SubclassOptionalData & IsInBounds;
@@ -506,12 +529,22 @@ class GEPOperator
                      APInt &ConstantOffset) const;
 };
 
+template <>
+struct OperandTraits<GEPOperator> :
+  public VariadicOperandTraits<GEPOperator, 1> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GEPOperator, Value)
+
 class PtrToIntOperator
     : public ConcreteOperator<Operator, Instruction::PtrToInt> {
   friend class PtrToInt;
   friend class ConstantExpr;
 
 public:
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
   Value *getPointerOperand() {
     return getOperand(0);
   }
@@ -534,12 +567,22 @@ class PtrToIntOperator
   }
 };
 
+template <>
+struct OperandTraits<PtrToIntOperator> :
+  public FixedNumOperandTraits<PtrToIntOperator, 1> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PtrToIntOperator, Value)
+
 class BitCastOperator
     : public ConcreteOperator<Operator, Instruction::BitCast> {
   friend class BitCastInst;
   friend class ConstantExpr;
 
 public:
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
   Type *getSrcTy() const {
     return getOperand(0)->getType();
   }
@@ -549,12 +592,22 @@ class BitCastOperator
   }
 };
 
+template <>
+struct OperandTraits<BitCastOperator> :
+  public FixedNumOperandTraits<BitCastOperator, 1> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitCastOperator, Value)
+
 class AddrSpaceCastOperator
     : public ConcreteOperator<Operator, Instruction::AddrSpaceCast> {
   friend class AddrSpaceCastInst;
   friend class ConstantExpr;
 
 public:
+  /// Transparently provide more efficient getOperand methods.
+  DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
+
   Value *getPointerOperand() { return getOperand(0); }
 
   const Value *getPointerOperand() const { return getOperand(0); }
@@ -568,6 +621,13 @@ class AddrSpaceCastOperator
   }
 };
 
+template <>
+struct OperandTraits<AddrSpaceCastOperator> :
+  public FixedNumOperandTraits<AddrSpaceCastOperator, 1> {
+};
+
+DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AddrSpaceCastOperator, Value)
+
 } // end namespace llvm
 
 #endif // LLVM_IR_OPERATOR_H

>From 49fc85394b0f7512849c116e8d4eda59d0ec560a Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Mon, 29 Jan 2024 21:52:40 -0800
Subject: [PATCH 2/2] fixup! clang-format

---
 llvm/include/llvm/IR/Operator.h | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/llvm/include/llvm/IR/Operator.h b/llvm/include/llvm/IR/Operator.h
index 6e81ee9870f5b..7168128648d8e 100644
--- a/llvm/include/llvm/IR/Operator.h
+++ b/llvm/include/llvm/IR/Operator.h
@@ -128,9 +128,8 @@ class OverflowingBinaryOperator : public Operator {
 };
 
 template <>
-struct OperandTraits<OverflowingBinaryOperator> :
-  public FixedNumOperandTraits<OverflowingBinaryOperator, 2> {
-};
+struct OperandTraits<OverflowingBinaryOperator>
+    : public FixedNumOperandTraits<OverflowingBinaryOperator, 2> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(OverflowingBinaryOperator, Value)
 
@@ -179,9 +178,8 @@ class PossiblyExactOperator : public Operator {
 };
 
 template <>
-struct OperandTraits<PossiblyExactOperator> :
-  public FixedNumOperandTraits<PossiblyExactOperator, 2> {
-};
+struct OperandTraits<PossiblyExactOperator>
+    : public FixedNumOperandTraits<PossiblyExactOperator, 2> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PossiblyExactOperator, Value)
 
@@ -530,9 +528,8 @@ class GEPOperator
 };
 
 template <>
-struct OperandTraits<GEPOperator> :
-  public VariadicOperandTraits<GEPOperator, 1> {
-};
+struct OperandTraits<GEPOperator>
+    : public VariadicOperandTraits<GEPOperator, 1> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GEPOperator, Value)
 
@@ -568,9 +565,8 @@ class PtrToIntOperator
 };
 
 template <>
-struct OperandTraits<PtrToIntOperator> :
-  public FixedNumOperandTraits<PtrToIntOperator, 1> {
-};
+struct OperandTraits<PtrToIntOperator>
+    : public FixedNumOperandTraits<PtrToIntOperator, 1> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PtrToIntOperator, Value)
 
@@ -593,9 +589,8 @@ class BitCastOperator
 };
 
 template <>
-struct OperandTraits<BitCastOperator> :
-  public FixedNumOperandTraits<BitCastOperator, 1> {
-};
+struct OperandTraits<BitCastOperator>
+    : public FixedNumOperandTraits<BitCastOperator, 1> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitCastOperator, Value)
 
@@ -622,9 +617,8 @@ class AddrSpaceCastOperator
 };
 
 template <>
-struct OperandTraits<AddrSpaceCastOperator> :
-  public FixedNumOperandTraits<AddrSpaceCastOperator, 1> {
-};
+struct OperandTraits<AddrSpaceCastOperator>
+    : public FixedNumOperandTraits<AddrSpaceCastOperator, 1> {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AddrSpaceCastOperator, Value)
 



More information about the llvm-commits mailing list