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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 29 20:35:07 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Craig Topper (topperc)

<details>
<summary>Changes</summary>

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.

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


1 Files Affected:

- (modified) llvm/include/llvm/IR/Operator.h (+60) 


``````````diff
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

``````````

</details>


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


More information about the llvm-commits mailing list