summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2006-10-20 00:27:06 +0000
committerChris Lattner <sabre@nondot.org>2006-10-20 00:27:06 +0000
commit23dd1f6325678d153d8a486974c655e3a6cb5fda (patch)
treebc6d0221c309df81f1a3fba06e9fbf5401ce010e /llvm/lib
parentb5d19927c9eec937a3a5db0c56087fa5b3005630 (diff)
downloadbcm5719-llvm-23dd1f6325678d153d8a486974c655e3a6cb5fda.tar.gz
bcm5719-llvm-23dd1f6325678d153d8a486974c655e3a6cb5fda.zip
add a method to determine whether evaluation of a constant can trap.
llvm-svn: 31059
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/VMCore/Constants.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp
index d187125233c..cf8f79e96d2 100644
--- a/llvm/lib/VMCore/Constants.cpp
+++ b/llvm/lib/VMCore/Constants.cpp
@@ -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()) {
OpenPOWER on IntegriCloud