[SeparateConstOffsetFromGEP] Fix: `b - a` matched `a - b` during reuniteExts
authorLiren Peng <liren.plr@gmail.com>
Wed, 15 Feb 2023 02:18:41 +0000 (02:18 +0000)
committerMarsPLR <liren.plr@gmail.com>
Wed, 15 Feb 2023 02:33:31 +0000 (02:33 +0000)
commit06f06644efb003e0748e94ac11cb230271a006f8
tree7529d8da69adee95081d831d102eca7e82620ad8
parentf93da3962eed5e7ae3dc0d73ffe76e8402c42eab
[SeparateConstOffsetFromGEP] Fix: `b - a` matched `a - b` during reuniteExts

During the SeparateConstOffsetFromGEP pass, a - b and b - a will be
considered equivalent in some instances.

An example- the IR contains:

  BB1:
      %add = add %a, 511
      br label %BB2
  BB2:
      %sub2 = sub %b,  %a
      br label %BB3
  BB3:
      %sub1 = sub %add, %b
      %gep = getelementptr float, ptr %p, %sub1

Step 1 in the SeparateConstOffsetFromGEP pass, after split constant index:

  BB1:
      %add = add %a, 511
      br label %BB2
  BB2:
      %sub2 = sub %b,  %a
      br label %BB3
  BB3:
      %sub.t = sub %a, %b
      %gep.base = getelementptr float, ptr %p, %sub.t
      %gep = getelementptr float, ptr %gep.base, 511

Step 2, after reuniteExts:

  BB1:
      br label %BB2
  BB2:
      %sub2 = sub %b,  %a
      br label %BB3
  BB3:
      %gep.base = getelementptr float, ptr %p, %sub2
      %gep = getelementptr float, ptr %gep.base, 511

Obviously, reuniteExts treated a - b and b - a as equivalent.
This patch fixes that.

Reviewed By: nikic, spatel

Differential Revision: https://reviews.llvm.org/D143542
llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
llvm/test/Transforms/SeparateConstOffsetFromGEP/split-gep-sub.ll