about summary refs log tree commit diff homepage
path: root/lib
diff options
context:
space:
mode:
authorCristian Cadar <c.cadar@imperial.ac.uk>2015-09-21 18:52:15 +0200
committerCristian Cadar <c.cadar@imperial.ac.uk>2015-09-21 18:52:15 +0200
commite8fded10ccdaea3d9d72d178506d6aebb799394e (patch)
tree1a3314b28fe9d021eee1c8555e4fff4e81104e63 /lib
parent090cd7310cf8257081c87a64985d588bd749566b (diff)
parent5c211bf969a6ae6b549d0a53daf4c99870c82210 (diff)
downloadklee-e8fded10ccdaea3d9d72d178506d6aebb799394e.tar.gz
Merge pull request #274 from MartinNowack/fix_sdiv_1
Fix signed division by constant 1/ -1
Diffstat (limited to 'lib')
-rw-r--r--lib/Solver/STPBuilder.cpp12
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/Solver/STPBuilder.cpp b/lib/Solver/STPBuilder.cpp
index a5e4c5ad..c2f23c0a 100644
--- a/lib/Solver/STPBuilder.cpp
+++ b/lib/Solver/STPBuilder.cpp
@@ -382,6 +382,7 @@ ExprHandle STPBuilder::constructUDivByConstant(ExprHandle expr_n, unsigned width
  * @return n/d without doing explicit division
  */
 ExprHandle STPBuilder::constructSDivByConstant(ExprHandle expr_n, unsigned width, uint64_t d) {
+  // Refactor using APInt::ms APInt::magic();
   assert(width==32 && "can only compute udiv constants for 32-bit division");
 
   // Compute the constants needed to compute n/d for constant d w/o division by d.
@@ -673,11 +674,14 @@ ExprHandle STPBuilder::constructActual(ref<Expr> e, int *width_out) {
     assert(*width_out!=1 && "uncanonicalized sdiv");
 
     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(de->right))
-      if (optimizeDivides)
-	if (*width_out == 32) //only works for 32-bit division
-	  return constructSDivByConstant( left, *width_out, 
+      if (optimizeDivides) {
+        llvm::APInt divisor = CE->getAPValue();
+        if (divisor != llvm::APInt(CE->getWidth(),1, false /*unsigned*/) &&
+            divisor != llvm::APInt(CE->getWidth(), -1, true /*signed*/))
+            if (*width_out == 32) //only works for 32-bit division
+               return constructSDivByConstant( left, *width_out,
                                           CE->getZExtValue(32));
-
+      }
     // XXX need to test for proper handling of sign, not sure I
     // trust STP
     ExprHandle right = construct(de->right, width_out);