Blame SOURCES/valgrind-3.14.0-transform-popcount64-ctznat64.patch

560544
commit cb5d7e047598bff6d0f1d707a70d9fb1a1c7f0e2
560544
Author: Julian Seward <jseward@acm.org>
560544
Date:   Tue Nov 20 11:46:55 2018 +0100
560544
560544
    VEX/priv/ir_opt.c
560544
    
560544
    fold_Expr: transform PopCount64(And64(Add64(x,-1),Not64(x))) into CtzNat64(x).
560544
    
560544
    This is part of the fix for bug 386945.
560544
560544
diff --git a/VEX/priv/ir_opt.c b/VEX/priv/ir_opt.c
560544
index f40870b..23964be 100644
560544
--- a/VEX/priv/ir_opt.c
560544
+++ b/VEX/priv/ir_opt.c
560544
@@ -1377,6 +1377,8 @@ static IRExpr* fold_Expr ( IRExpr** env, IRExpr* e )
560544
    case Iex_Unop:
560544
       /* UNARY ops */
560544
       if (e->Iex.Unop.arg->tag == Iex_Const) {
560544
+
560544
+         /* cases where the arg is a const */
560544
          switch (e->Iex.Unop.op) {
560544
          case Iop_1Uto8:
560544
             e2 = IRExpr_Const(IRConst_U8(toUChar(
560544
@@ -1690,8 +1692,56 @@ static IRExpr* fold_Expr ( IRExpr** env, IRExpr* e )
560544
 
560544
          default: 
560544
             goto unhandled;
560544
-      }
560544
-      }
560544
+         } // switch (e->Iex.Unop.op)
560544
+
560544
+      } else {
560544
+
560544
+         /* other cases (identities, etc) */
560544
+         switch (e->Iex.Unop.op) {
560544
+         case Iop_PopCount64: {
560544
+            // PopCount64( And64( Add64(x,-1), Not64(x) ) ) ==> CtzNat64(x)
560544
+            // bindings:
560544
+            //   a1:And64( a11:Add64(a111:x,a112:-1), a12:Not64(a121:x) )
560544
+            IRExpr* a1 = chase(env, e->Iex.Unop.arg);
560544
+            if (!a1)
560544
+               goto nomatch;
560544
+            if (a1->tag != Iex_Binop || a1->Iex.Binop.op != Iop_And64)
560544
+               goto nomatch;
560544
+            // a1 is established
560544
+            IRExpr* a11 = chase(env, a1->Iex.Binop.arg1);
560544
+            if (!a11)
560544
+               goto nomatch;
560544
+            if (a11->tag != Iex_Binop || a11->Iex.Binop.op != Iop_Add64)
560544
+               goto nomatch;
560544
+            // a11 is established
560544
+            IRExpr* a12 = chase(env, a1->Iex.Binop.arg2);
560544
+            if (!a12)
560544
+               goto nomatch;
560544
+            if (a12->tag != Iex_Unop || a12->Iex.Unop.op != Iop_Not64)
560544
+               goto nomatch;
560544
+            // a12 is established
560544
+            IRExpr* a111 = a11->Iex.Binop.arg1;
560544
+            IRExpr* a112 = chase(env, a11->Iex.Binop.arg2);
560544
+            IRExpr* a121 = a12->Iex.Unop.arg;
560544
+            if (!a111 || !a112 || !a121)
560544
+               goto nomatch;
560544
+            // a111 and a121 need to be the same temp.
560544
+            if (!eqIRAtom(a111, a121))
560544
+               goto nomatch;
560544
+            // Finally, a112 must be a 64-bit version of -1.
560544
+            if (!isOnesU(a112))
560544
+               goto nomatch;
560544
+            // Match established.  Transform.
560544
+            e2 = IRExpr_Unop(Iop_CtzNat64, a111);
560544
+            break;
560544
+           nomatch:
560544
+            break;
560544
+         }
560544
+         default:
560544
+            break;
560544
+         } // switch (e->Iex.Unop.op)
560544
+
560544
+      } // if (e->Iex.Unop.arg->tag == Iex_Const)
560544
       break;
560544
 
560544
    case Iex_Binop: