Skip to content
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

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

Merged
merged 2 commits into from
Jan 30, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Jan 30, 2024

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.

…bclasses.

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.
@topperc topperc requested a review from nikic January 30, 2024 04:34
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 30, 2024

@llvm/pr-subscribers-llvm-ir

Author: Craig Topper (topperc)

Changes

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

Copy link

github-actions bot commented Jan 30, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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 8369f61 into llvm:main Jan 30, 2024
3 of 4 checks passed
@topperc topperc deleted the pr/operator-getoperand branch January 30, 2024 17:38
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.

None yet

3 participants