This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Combine ZExt (B - A) + ZExt(A) to ZExt(B)
ClosedPublic

Authored by StephenFan on Dec 13 2022, 5:03 AM.

Diff Detail

Event Timeline

StephenFan created this revision.Dec 13 2022, 5:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 13 2022, 5:03 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
StephenFan requested review of this revision.Dec 13 2022, 5:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 13 2022, 5:03 AM

Just a drive-by comment, but I believe it needs nuw on the sub.

Just a drive-by comment, but I believe it needs nuw on the sub.

Yes - the last test is a miscompile:
https://alive2.llvm.org/ce/z/xd8Pdp

Check NUW for sub.

Please pre-commit the baseline tests, so we just show the functional changes from this patch in the tests.

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
1452

Use the raw constructor instead of Builder to preserve the value name:

return new ZextInst(B, LHS->getType());
llvm/test/Transforms/InstCombine/add.ll
2350

Add "_commute" to the test name to make the difference clear.

2355

Change types to increase test coverage. You could use weird types like <2 x i5>.
We also want to verify at the limits of the fold, so zext by just 1 bit from an i2 (or even i1?).
It would also be good to add extra uses to at least one test because the extra uses don't change the transform. See examples in this file of call void @use(i8).

2370

Add another (negative) test where the sub operands are swapped?

  1. Use new instead of builder.
  2. Rename to commute
  3. Add zext <2 x i5> to <2 x i8> test
  4. Add zext i2 to i3 test
  5. Add test that has uses.
  6. Add negative test of commuted sub operands.

I think this is good now, but please pre-commit the baseline tests.

spatel accepted this revision.Dec 19 2022, 10:19 AM

LGTM

This revision is now accepted and ready to land.Dec 19 2022, 10:19 AM