add a method to determine whether evaluation of a constant can trap.
authorChris Lattner <sabre@nondot.org>
Fri, 20 Oct 2006 00:27:06 +0000 (00:27 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 20 Oct 2006 00:27:06 +0000 (00:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31059 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constant.h
lib/VMCore/Constants.cpp

index 15722b62db04836a3083abd69c07d65adfca234d..24fb99036ecc66e24d54caf9f64af37d64dda316 100644 (file)
@@ -54,6 +54,10 @@ public:
   virtual bool isNullValue() const = 0;
 
   virtual void print(std::ostream &O) const;
+  
+  /// canTrap - Return true if evaluation of this constant could trap.  This is
+  /// true for things like constant expressions that could divide by zero.
+  bool canTrap() const;
 
   // Specialize get/setOperand for Constant's as their operands are always
   // constants as well.
index d187125233c50843ada17db15bb64b54f2765b7d..cf8f79e96d208d8e46b2c175addbf57cd1b5f7e6 100644 (file)
@@ -58,6 +58,33 @@ void Constant::destroyConstantImpl() {
   delete this;
 }
 
+/// canTrap - Return true if evaluation of this constant could trap.  This is
+/// true for things like constant expressions that could divide by zero.
+bool Constant::canTrap() const {
+  assert(getType()->isFirstClassType() && "Cannot evaluate aggregate vals!");
+  // The only thing that could possibly trap are constant exprs.
+  const ConstantExpr *CE = dyn_cast<ConstantExpr>(this);
+  if (!CE) return false;
+  
+  // ConstantExpr traps if any operands can trap. 
+  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
+    if (getOperand(i)->canTrap()) 
+      return true;
+
+  // Otherwise, only specific operations can trap.
+  switch (CE->getOpcode()) {
+  default:
+    return false;
+  case Instruction::Div:
+  case Instruction::Rem:
+    // Div and rem can trap if the RHS is not known to be non-zero.
+    if (!isa<ConstantInt>(getOperand(1)) || getOperand(1)->isNullValue())
+      return true;
+    return false;
+  }
+}
+
+
 // Static constructor to create a '0' constant of arbitrary type...
 Constant *Constant::getNullValue(const Type *Ty) {
   switch (Ty->getTypeID()) {