https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120403

--- Comment #4 from Shivam Gupta <shivam98.tkg at gmail dot com> ---
Hello,

I have been experimenting with a prototype tree pass for this issue, I planned
to do this in two phases.

Phase 1 – Linearize equality chains (new tree pass)
The idea is to transform the conditional equality chain:
  <bb 2> [local count: 1073741822]:
  _1 = s1.x;
  _2 = s2.x;
  if (_1 == _2)
    goto <bb 3>; [34.00%]
  else
    goto <bb 4>; [66.00%]

  <bb 3> [local count: 365072224]:
  _3 = s1.y;
  _4 = s2.y;
  _7 = _3 == _4;

  <bb 4> [local count: 1073741824]:
  # iftmp.0_5 = PHI <_7(3), 0(2)>
  return iftmp.0_5;

into a straight-line XOR/OR sequence:
  <bb 2> [local count: 1073741824]:
  _1 = s1.x;
  _2 = s2.x;
  _3 = s1.y;
  _4 = s2.y;
  _7 = _1 ^ _2;
  _8 = _3 ^ _4;
  _9 = _7 | _8;
  _10 = _9 == 0;
  return _10;

This eliminates the branch and PHI node entirely. The resulting IR is becomes
simpler.

Phase 2 – Widening
Once the chain is linearized, look for opportunities to widen the XOR/OR
sequence across multiple fields into a single wider comparison — similar to
what LLVM does to produce cmp rdi, rsi for the struct s case and cmp edi, esi
for struct t.

I'm wondering whether a dedicated tree pass (running before if-combine) would
be cleaner, given that the widening step in Phase 2 may need to reason about
field layout and alignment separately.

Reply via email to