[llvm] a5ae3ed - [IR] CmpInst: add getUnsignedPredicate()

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 6 00:31:44 PST 2020


Author: Roman Lebedev
Date: 2020-11-06T11:31:08+03:00
New Revision: a5ae3edaa380702eb2be225721533c1aab2f4d3e

URL: https://github.com/llvm/llvm-project/commit/a5ae3edaa380702eb2be225721533c1aab2f4d3e
DIFF: https://github.com/llvm/llvm-project/commit/a5ae3edaa380702eb2be225721533c1aab2f4d3e.diff

LOG: [IR] CmpInst: add getUnsignedPredicate()

There's already getSignedPredicate(), it is not symmetrical to not have
it's opposite. I wanted to use it in new code, but it wasn't there..

Added: 
    

Modified: 
    llvm/include/llvm/IR/InstrTypes.h
    llvm/lib/IR/Instructions.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/InstrTypes.h b/llvm/include/llvm/IR/InstrTypes.h
index 810e87de232b..e1c35bfdb13d 100644
--- a/llvm/include/llvm/IR/InstrTypes.h
+++ b/llvm/include/llvm/IR/InstrTypes.h
@@ -920,6 +920,18 @@ class CmpInst : public Instruction {
     return getSignedPredicate(getPredicate());
   }
 
+  /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
+  /// @returns the unsigned version of the signed predicate pred.
+  static Predicate getUnsignedPredicate(Predicate pred);
+
+  /// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
+  /// @returns the unsigned version of the predicate for this instruction (which
+  /// has to be an signed predicate).
+  /// return the unsigned version of a predicate
+  Predicate getUnsignedPredicate() {
+    return getUnsignedPredicate(getPredicate());
+  }
+
   /// This is just a convenience.
   /// Determine if this is true when both operands are the same.
   bool isTrueWhenEqual() const {

diff  --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index b8663cdcbe83..950a1e9fb508 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -3847,7 +3847,7 @@ CmpInst::Predicate CmpInst::getNonStrictPredicate(Predicate pred) {
 }
 
 CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
-  assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
+  assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!");
 
   switch (pred) {
   default:
@@ -3863,6 +3863,23 @@ CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
   }
 }
 
+CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) {
+  assert(CmpInst::isSigned(pred) && "Call only with signed predicates!");
+
+  switch (pred) {
+  default:
+    llvm_unreachable("Unknown predicate!");
+  case CmpInst::ICMP_SLT:
+    return CmpInst::ICMP_ULT;
+  case CmpInst::ICMP_SLE:
+    return CmpInst::ICMP_ULE;
+  case CmpInst::ICMP_SGT:
+    return CmpInst::ICMP_UGT;
+  case CmpInst::ICMP_SGE:
+    return CmpInst::ICMP_UGE;
+  }
+}
+
 bool CmpInst::isUnsigned(Predicate predicate) {
   switch (predicate) {
     default: return false;


        


More information about the llvm-commits mailing list