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