Provide an ICmpInst::makeConstantRange to generate a ConstantRange value
authorReid Spencer <rspencer@reidspencer.com>
Wed, 28 Feb 2007 22:00:54 +0000 (22:00 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Wed, 28 Feb 2007 22:00:54 +0000 (22:00 +0000)
from a predicate and an APInt. This is removed from ConstantRange class
so that ConstantRange doesn't have to depend on lib/VMCore.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34760 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Instructions.h
lib/VMCore/Instructions.cpp

index c78426a239f09cfeefe7fa9494e9e0fc2cc3ad91..337ae2a463b78b07fb58c52a302935b114db8699 100644 (file)
@@ -24,6 +24,8 @@ class BasicBlock;
 class ConstantInt;
 class PointerType;
 class VectorType;
+class ConstantRange;
+class APInt;
 
 //===----------------------------------------------------------------------===//
 //                             AllocationInst Class
@@ -536,6 +538,10 @@ public:
   /// @brief Determine if the predicate is signed.
   static bool isSignedPredicate(Predicate pred);
 
+  /// Initialize a set of values that all satisfy the predicate with C. 
+  /// @brief Make a ConstantRange for a relation with a constant value.
+  static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
+
   /// Exchange the two operands to this instruction in such a way that it does
   /// not modify the semantics of the instruction. The predicate value may be
   /// changed to retain the same result if the predicate is order dependent
index e1af531d649d6c2d42e54217a343cabfe669628c..f4519d4dc2964480fba529c998f767c6a445bff0 100644 (file)
@@ -18,6 +18,7 @@
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
 #include "llvm/Support/CallSite.h"
+#include "llvm/Support/ConstantRange.h"
 using namespace llvm;
 
 unsigned CallSite::getCallingConv() const {
@@ -2217,6 +2218,41 @@ bool ICmpInst::isSignedPredicate(Predicate pred) {
   }
 }
 
+/// Initialize a set of values that all satisfy the condition with C.
+///
+ConstantRange 
+ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
+  APInt Lower(C);
+  APInt Upper(C);
+  uint32_t BitWidth = C.getBitWidth();
+  switch (pred) {
+  default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
+  case ICmpInst::ICMP_EQ: Upper++; break;
+  case ICmpInst::ICMP_NE: Lower++; break;
+  case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
+  case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
+  case ICmpInst::ICMP_UGT: 
+    Lower++; Upper = APInt::getMinValue(BitWidth);        // Min = Next(Max)
+    break;
+  case ICmpInst::ICMP_SGT:
+    Lower++; Upper = APInt::getSignedMinValue(BitWidth);  // Min = Next(Max)
+    break;
+  case ICmpInst::ICMP_ULE: 
+    Lower = APInt::getMinValue(BitWidth); Upper++; 
+    break;
+  case ICmpInst::ICMP_SLE: 
+    Lower = APInt::getSignedMinValue(BitWidth); Upper++; 
+    break;
+  case ICmpInst::ICMP_UGE:
+    Upper = APInt::getMinValue(BitWidth);        // Min = Next(Max)
+    break;
+  case ICmpInst::ICMP_SGE:
+    Upper = APInt::getSignedMinValue(BitWidth);  // Min = Next(Max)
+    break;
+  }
+  return ConstantRange(Lower, Upper);
+}
+
 FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
   switch (pred) {
     default: