hhvm icon indicating copy to clipboard operation
hhvm copied to clipboard

AArch64: Fold shqri/testl/jcc into cmp/jcc

Open david-arm opened this issue 1 month ago • 6 comments

We often see patterns like this:

shrqi 32, %v0, %v1 testl 0x80000000, %v1 jcc CC_E, ...

but really this is the same as asking if %v0 is >= 0. Rewriting this in the form

cmp 0, %v0 jcc CC_GE, ...

saves two instructions, i.e. a lsr and a mov.

david-arm avatar Nov 07 '25 12:11 david-arm

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D86516135. (Because this pull request was imported automatically, there will not be any future comments.)

meta-codesync[bot] avatar Nov 07 '25 12:11 meta-codesync[bot]

This fails to compile with

fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:117:10: error: unused variable 'bit_num' [-Werror,-Wunused-variable]
  117 |     auto bit_num = folly::findLastSet<uint64_t>(Val) - 1;
      |          ^~~~~~~
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:99:19: error: cannot refer to type member 'Kind' in 'HPHP::jit::Vconst const' with '.'
   99 |   assert(op_const.Kind != Vconst::Double);
      |                   ^
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:113:10: note: in instantiation of function template specialization 'HPHP::jit::arm::(anonymous namespace)::get_const_int<HPHP::jit::Vr<HPHP::jit::Reg32>>' requested here
  113 |     if (!get_const_int(env, tstl.s0, Val) || folly::popcount(Val) != 1)
      |          ^
buck-out/v2/gen/fbcode/hphp/runtime/vm/__headers__/fc799a07123e9b1d/buck-headers/hphp/runtime/vm/jit/vasm-unit.h:96:8: note: member 'Kind' declared here
   96 |   enum Kind { Quad, Long, Byte, Double };
      |        ^
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:99:19: error: cannot refer to type member 'Kind' in 'HPHP::jit::Vconst const' with '.'
   99 |   assert(op_const.Kind != Vconst::Double);
      |                   ^
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:131:8: note: in instantiation of function template specialization 'HPHP::jit::arm::(anonymous namespace)::get_const_int<HPHP::jit::Vr<HPHP::jit::Reg64>>' requested here
  131 |   if (!get_const_int(env, inst.s0, Val) || Val != 0x8000000000000000ull)
      |        ^
buck-out/v2/gen/fbcode/hphp/runtime/vm/__headers__/fc799a07123e9b1d/buck-headers/hphp/runtime/vm/jit/vasm-unit.h:96:8: note: member 'Kind' declared here
   96 |   enum Kind { Quad, Long, Byte, Double };
      |        ^
3 errors generated.

meteorfox avatar Nov 10 '25 22:11 meteorfox

This fails to compile with

fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:117:10: error: unused variable 'bit_num' [-Werror,-Wunused-variable]
  117 |     auto bit_num = folly::findLastSet<uint64_t>(Val) - 1;
      |          ^~~~~~~
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:99:19: error: cannot refer to type member 'Kind' in 'HPHP::jit::Vconst const' with '.'
   99 |   assert(op_const.Kind != Vconst::Double);
      |                   ^
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:113:10: note: in instantiation of function template specialization 'HPHP::jit::arm::(anonymous namespace)::get_const_int<HPHP::jit::Vr<HPHP::jit::Reg32>>' requested here
  113 |     if (!get_const_int(env, tstl.s0, Val) || folly::popcount(Val) != 1)
      |          ^
buck-out/v2/gen/fbcode/hphp/runtime/vm/__headers__/fc799a07123e9b1d/buck-headers/hphp/runtime/vm/jit/vasm-unit.h:96:8: note: member 'Kind' declared here
   96 |   enum Kind { Quad, Long, Byte, Double };
      |        ^
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:99:19: error: cannot refer to type member 'Kind' in 'HPHP::jit::Vconst const' with '.'
   99 |   assert(op_const.Kind != Vconst::Double);
      |                   ^
fbcode/hphp/runtime/vm/jit/vasm-simplify-arm.cpp:131:8: note: in instantiation of function template specialization 'HPHP::jit::arm::(anonymous namespace)::get_const_int<HPHP::jit::Vr<HPHP::jit::Reg64>>' requested here
  131 |   if (!get_const_int(env, inst.s0, Val) || Val != 0x8000000000000000ull)
      |        ^
buck-out/v2/gen/fbcode/hphp/runtime/vm/__headers__/fc799a07123e9b1d/buck-headers/hphp/runtime/vm/jit/vasm-unit.h:96:8: note: member 'Kind' declared here
   96 |   enum Kind { Quad, Long, Byte, Double };
      |        ^
3 errors generated.

OK thanks I'll take a look! It seemed to pass for me downstream, so presumably I'm using a different build setup.

david-arm avatar Nov 11 '25 08:11 david-arm

Yeah the assert(op_const.Kind != Vconst::Double); is really daft and it's weird my build didn't complain about it. Also, looks like we should be building with -Werror too.

david-arm avatar Nov 11 '25 08:11 david-arm

@david-arm has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar Nov 11 '25 09:11 facebook-github-bot

Thanks it builds and passes tests now

meteorfox avatar Nov 13 '25 11:11 meteorfox

@david-arm has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar Dec 15 '25 08:12 facebook-github-bot