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

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