[llvm] [IR] Add ICmpInst::isCommutative and FCmpInst::isCommutative static wrappers (PR #116398)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 15 09:00:48 PST 2024


https://github.com/RKSimon updated https://github.com/llvm/llvm-project/pull/116398

>From afffbdfa4d4d8dc4fdf050c735750f49ca228220 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Fri, 15 Nov 2024 15:21:06 +0000
Subject: [PATCH 1/2] [IR] Add ICmpInst::isCommutative and
 FCmpInst::isCommutative static wrappers

Add static variants that can used with the Predicate enum directly.
---
 llvm/include/llvm/IR/Instructions.h | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index bc29a4801e4ff4..ee6b24f376eebb 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -1248,9 +1248,13 @@ class ICmpInst: public CmpInst {
     return isEquality(getPredicate());
   }
 
+  /// @returns true if the predicate is commutative
+  /// Determine if this relation is commutative.
+  static bool isCommutative(Predicate P) { return isEquality(P); }
+
   /// @returns true if the predicate of this ICmpInst is commutative
   /// Determine if this relation is commutative.
-  bool isCommutative() const { return isEquality(); }
+  bool isCommutative() const { return isCommutative(getPredicate()); }
 
   /// Return true if the predicate is relational (not EQ or NE).
   ///
@@ -1369,7 +1373,7 @@ class FCmpInst: public CmpInst {
     AssertOK();
   }
 
-  /// @returns true if the predicate of this instruction is EQ or NE.
+  /// @returns true if the predicate is EQ or NE.
   /// Determine if this is an equality predicate.
   static bool isEquality(Predicate Pred) {
     return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
@@ -1380,16 +1384,18 @@ class FCmpInst: public CmpInst {
   /// Determine if this is an equality predicate.
   bool isEquality() const { return isEquality(getPredicate()); }
 
-  /// @returns true if the predicate of this instruction is commutative.
+
+  /// @returns true if the predicate is commutative.
   /// Determine if this is a commutative predicate.
-  bool isCommutative() const {
-    return isEquality() ||
-           getPredicate() == FCMP_FALSE ||
-           getPredicate() == FCMP_TRUE ||
-           getPredicate() == FCMP_ORD ||
-           getPredicate() == FCMP_UNO;
+  static bool isCommutative(Predicate Pred) {
+    return isEquality(Pred) || Pred == FCMP_FALSE || Pred == FCMP_TRUE ||
+           Pred == FCMP_ORD || Pred == FCMP_UNO;
   }
 
+  /// @returns true if the predicate of this instruction is commutative.
+  /// Determine if this is a commutative predicate.
+  bool isCommutative() const { return isCommutative(getPredicate()); }
+
   /// @returns true if the predicate is relational (not EQ or NE).
   /// Determine if this a relational predicate.
   bool isRelational() const { return !isEquality(); }

>From 45dbcd075baf3d7654c5413443fa0426f86fe00c Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Fri, 15 Nov 2024 17:00:33 +0000
Subject: [PATCH 2/2] remove extra newline

---
 llvm/include/llvm/IR/Instructions.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h
index ee6b24f376eebb..8eea659a00caf3 100644
--- a/llvm/include/llvm/IR/Instructions.h
+++ b/llvm/include/llvm/IR/Instructions.h
@@ -1384,7 +1384,6 @@ class FCmpInst: public CmpInst {
   /// Determine if this is an equality predicate.
   bool isEquality() const { return isEquality(getPredicate()); }
 
-
   /// @returns true if the predicate is commutative.
   /// Determine if this is a commutative predicate.
   static bool isCommutative(Predicate Pred) {



More information about the llvm-commits mailing list