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