//===-- ConstantRange.cpp - ConstantRange implementation ------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
//
// Represent a range of possible values that may occur when the program is run
// for an integral value. This keeps track of a lower and upper bound for the
//===----------------------------------------------------------------------===//
#include "llvm/Support/ConstantRange.h"
-#include "llvm/Type.h"
+#include "llvm/Constants.h"
#include "llvm/Instruction.h"
-#include "llvm/ConstantHandling.h"
+#include "llvm/Type.h"
+using namespace llvm;
+
+static bool LT(ConstantIntegral *A, ConstantIntegral *B) {
+ Constant *C = ConstantExpr::get(Instruction::SetLT, A, B);
+ assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
+ return cast<ConstantBool>(C)->getValue();
+}
+
+static bool GT(ConstantIntegral *A, ConstantIntegral *B) {
+ Constant *C = ConstantExpr::get(Instruction::SetGT, A, B);
+ assert(isa<ConstantBool>(C) && "Constant folding of integrals not impl??");
+ return cast<ConstantBool>(C)->getValue();
+}
+
+static ConstantIntegral *Min(ConstantIntegral *A, ConstantIntegral *B) {
+ return LT(A, B) ? A : B;
+}
+static ConstantIntegral *Max(ConstantIntegral *A, ConstantIntegral *B) {
+ return GT(A, B) ? A : B;
+}
+
/// Initialize a full (the default) or empty set for the specified type.
///
if (CI->getType() == Type::BoolTy)
return CI == ConstantBool::True ? ConstantBool::False : ConstantBool::True;
- // Otherwise use operator+ in the ConstantHandling Library.
- Constant *Result = *ConstantInt::get(CI->getType(), 1) + *CI;
- assert(Result && "ConstantHandling not implemented for integral plus!?");
+ Constant *Result = ConstantExpr::get(Instruction::Add, CI,
+ ConstantInt::get(CI->getType(), 1));
return cast<ConstantIntegral>(Result);
}
Upper = C;
return;
case Instruction::SetGT:
- Upper = ConstantIntegral::getMaxValue(C->getType());
Lower = Next(C);
+ Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
return;
case Instruction::SetLE:
Lower = ConstantIntegral::getMinValue(C->getType());
Upper = Next(C);
return;
case Instruction::SetGE:
- Upper = ConstantIntegral::getMaxValue(C->getType());
Lower = C;
+ Upper = ConstantIntegral::getMinValue(C->getType()); // Min = Next(Max)
return;
}
}
/// for example: [100, 8)
///
bool ConstantRange::isWrappedSet() const {
- return (*(Constant*)Lower > *(Constant*)Upper)->getValue();
+ return GT(Lower, Upper);
}
}
// Simply subtract the bounds...
- Constant *Result = *(Constant*)Upper - *(Constant*)Lower;
- assert(Result && "Subtraction of constant integers not implemented?");
- if (getType()->isSigned())
- return (uint64_t)cast<ConstantSInt>(Result)->getValue();
- else
- return cast<ConstantUInt>(Result)->getValue();
+ Constant *Result =
+ ConstantExpr::get(Instruction::Sub, (Constant*)Upper, (Constant*)Lower);
+ return cast<ConstantInt>(Result)->getRawValue();
}
const ConstantRange &RHS) {
assert(LHS.isWrappedSet() && !RHS.isWrappedSet());
- // Handle common special cases
- if (RHS.isEmptySet()) return RHS;
- if (RHS.isFullSet()) return LHS;
-
// Check to see if we overlap on the Left side of RHS...
//
- if ((*(Constant*)RHS.getLower() < *(Constant*)LHS.getUpper())->getValue()) {
+ if (LT(RHS.getLower(), LHS.getUpper())) {
// We do overlap on the left side of RHS, see if we overlap on the right of
// RHS...
- if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
+ if (GT(RHS.getUpper(), LHS.getLower())) {
// Ok, the result overlaps on both the left and right sides. See if the
// resultant interval will be smaller if we wrap or not...
//
} else {
// We don't overlap on the left side of RHS, see if we overlap on the right
// of RHS...
- if ((*(Constant*)RHS.getUpper() > *(Constant*)LHS.getLower())->getValue()) {
+ if (GT(RHS.getUpper(), LHS.getLower())) {
// Simple overlap...
return ConstantRange(LHS.getLower(), RHS.getUpper());
} else {
}
}
-
/// intersect - Return the range that results from the intersection of this
/// range with another range.
///
ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
assert(getType() == CR.getType() && "ConstantRange types don't agree!");
+ // Handle common special cases
+ if (isEmptySet() || CR.isFullSet()) return *this;
+ if (isFullSet() || CR.isEmptySet()) return CR;
if (!isWrappedSet()) {
if (!CR.isWrappedSet()) {
- const Constant &L = std::max(*(Constant*)Lower, *(Constant*)CR.Lower);
- const Constant &U = std::min(*(Constant*)Upper, *(Constant*)CR.Upper);
+ ConstantIntegral *L = Max(Lower, CR.Lower);
+ ConstantIntegral *U = Min(Upper, CR.Upper);
- if ((L < U)->getValue()) // If range isn't empty...
- return ConstantRange(cast<ConstantIntegral>((Constant*)&L),
- cast<ConstantIntegral>((Constant*)&U));
+ if (LT(L, U)) // If range isn't empty...
+ return ConstantRange(L, U);
else
return ConstantRange(getType(), false); // Otherwise, return empty set
} else
return intersect1Wrapped(*this, CR);
else {
// Both ranges are wrapped...
- const Constant &L = std::max(*(Constant*)Lower, *(Constant*)CR.Lower);
- const Constant &U = std::min(*(Constant*)Upper, *(Constant*)CR.Upper);
-
- return ConstantRange(cast<ConstantIntegral>((Constant*)&L),
- cast<ConstantIntegral>((Constant*)&U));
+ ConstantIntegral *L = Max(Lower, CR.Lower);
+ ConstantIntegral *U = Min(Upper, CR.Upper);
+ return ConstantRange(L, U);
}
}
return *this;