[llvm-commits] CVS: llvm/lib/VMCore/Constants.cpp

Chris Lattner sabre at nondot.org
Thu Oct 19 17:27:22 PDT 2006



Changes in directory llvm/lib/VMCore:

Constants.cpp updated: 1.163 -> 1.164
---
Log message:

add a method to determine whether evaluation of a constant can trap.


---
Diffs of the changes:  (+27 -0)

 Constants.cpp |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+)


Index: llvm/lib/VMCore/Constants.cpp
diff -u llvm/lib/VMCore/Constants.cpp:1.163 llvm/lib/VMCore/Constants.cpp:1.164
--- llvm/lib/VMCore/Constants.cpp:1.163	Thu Sep 28 18:34:27 2006
+++ llvm/lib/VMCore/Constants.cpp	Thu Oct 19 19:27:06 2006
@@ -58,6 +58,33 @@
   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()) {






More information about the llvm-commits mailing list