dcdfabd
commit d6a810760ec61ddedf15445457edbbe288536a2f
dcdfabd
Author: Julian Seward <jseward@acm.org>
dcdfabd
Date:   Tue Dec 12 22:31:54 2017 +0100
dcdfabd
dcdfabd
    Fix false positive with s390x cgijnl instruction testing against sign bit.
dcdfabd
    
dcdfabd
    https://bugs.kde.org/show_bug.cgi?id=387712
dcdfabd
    
dcdfabd
    When the cgij "compare immediate and branch relative" instruction
dcdfabd
    compares 0 <=signed dep1, that means dep1 >=signed 0, so it is a test
dcdfabd
    against the most significant bit of dep1. So only that bit needs
dcdfabd
    to be defined.
dcdfabd
dcdfabd
diff --git a/VEX/priv/guest_s390_helpers.c b/VEX/priv/guest_s390_helpers.c
dcdfabd
index 4cccdec..aacd833 100644
dcdfabd
--- a/VEX/priv/guest_s390_helpers.c
dcdfabd
+++ b/VEX/priv/guest_s390_helpers.c
dcdfabd
@@ -1818,6 +1818,13 @@ isC64(const IRExpr *expr)
dcdfabd
    return expr->tag == Iex_Const && expr->Iex.Const.con->tag == Ico_U64;
dcdfabd
 }
dcdfabd
 
dcdfabd
+static inline Bool
dcdfabd
+isC64_exactly(const IRExpr *expr, ULong n)
dcdfabd
+{
dcdfabd
+   return expr->tag == Iex_Const && expr->Iex.Const.con->tag == Ico_U64
dcdfabd
+          && expr->Iex.Const.con->Ico.U64 == n;
dcdfabd
+}
dcdfabd
+
dcdfabd
 
dcdfabd
 /* The returned expression is NULL if no specialization was found. In that
dcdfabd
    case the helper function will be called. Otherwise, the expression has
dcdfabd
@@ -1895,9 +1902,25 @@ guest_s390x_spechelper(const HChar *function_name, IRExpr **args,
dcdfabd
          }
dcdfabd
          /* cc_dep1 > cc_dep2  ---->  cc_dep2 < cc_dep1 */
dcdfabd
          if (cond == 2 || cond == 2 + 1) {
dcdfabd
+            /* If we ever need the counterpart of the bug387712 fix just
dcdfabd
+               below, then here is the place.  We'll need to give an
dcdfabd
+               alternative expression for the case "cc_dep2 
dcdfabd
+               bit of simple testing, I've yet to see any such cases,
dcdfabd
+               however. */
dcdfabd
             return unop(Iop_1Uto32, binop(Iop_CmpLT64S, cc_dep2, cc_dep1));
dcdfabd
          }
dcdfabd
          if (cond == 8 + 2 || cond == 8 + 2 + 1) {
dcdfabd
+            if (isC64_exactly(cc_dep2, 0)) {
dcdfabd
+               /*     0    <=signed dep1
dcdfabd
+                  --> dep1 >=signed 0
dcdfabd
+                  --> m.s.bit of dep1 == 0 */
dcdfabd
+               /* See bug 387712.  This is an old trick from gcc to extract
dcdfabd
+                  the most significant bit of a word. */
dcdfabd
+               return unop(Iop_64to32,
dcdfabd
+                           binop(Iop_Xor64,
dcdfabd
+                                 binop(Iop_Shr64, cc_dep1, mkU8(63)),
dcdfabd
+                                 mkU64(1)));
dcdfabd
+            }
dcdfabd
             return unop(Iop_1Uto32, binop(Iop_CmpLE64S, cc_dep2, cc_dep1));
dcdfabd
          }
dcdfabd
          if (cond == 8 + 4 + 2 || cond == 8 + 4 + 2 + 1) {