This is an archive of the discontinued LLVM Phabricator instance.

[CGP] Look through bitcasts when duplicating returns for tail calls
ClosedPublic

Authored by thegameg on Apr 17 2019, 2:48 PM.

Details

Summary

The simple case of:

int *callee();
void *caller(void *a) {
  if (a == NULL)
    return callee();
  return a;
}

would generate a regular call instead of a tail call because we don't
look through the bitcast of the call to callee when duplicating the
return blocks.

Diff Detail

Repository
rL LLVM

Event Timeline

thegameg created this revision.Apr 17 2019, 2:48 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 17 2019, 2:48 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
spatel added inline comments.Apr 23 2019, 12:50 PM
llvm/lib/CodeGen/CodeGenPrepare.cpp
2035–2037 ↗(On Diff #195631)

There is a peekThroughBitcast() util function in instcombine. Might be worth lifting to some common location, so we don't have to repeat it in other IR passes.

llvm/test/CodeGen/X86/tailcall-cgp-dup.ll
110–112 ↗(On Diff #195631)

It's nice to have a codegen test, but the transform occurs in IR, so I'd prefer to see an 'opt -codegenprepare' test for this too. Preferably, that test (and this test) would be added to trunk before this patch, so we just see the improvement here. Finally, I prefer that FileCheck assertions are auto-generated using the scripts in the llvm/utils folder.

thegameg updated this revision to Diff 196328.Apr 23 2019, 2:27 PM
  • Use Value:: stripPointerCasts instead of checking for BitCastInst.
  • Add opt -codegenprepare test and full codegen test separately (to commit before this).
  • Use utils/update_test_checks.py and utils/update_llc_test_checks.py for the new tests.
thegameg marked 4 inline comments as done.Apr 23 2019, 2:29 PM
thegameg added inline comments.
llvm/lib/CodeGen/CodeGenPrepare.cpp
2035–2037 ↗(On Diff #195631)

Value::stripPointerCasts seems to be doing similar things, so I used that here.

llvm/test/CodeGen/X86/tailcall-cgp-dup.ll
110–112 ↗(On Diff #195631)

Thanks, I agree, this is much nicer.

spatel accepted this revision.Apr 23 2019, 2:42 PM

LGTM.

Note that there is a dedicated folder for CGP IR tests at 'llvm/test/Transforms/CodeGenPrepare' with target subfolders under there, but there's also precedence for doing it this way, so we see IR and asm in the same file.

This revision is now accepted and ready to land.Apr 23 2019, 2:42 PM
This revision was automatically updated to reflect the committed changes.
thegameg marked 2 inline comments as done.

LGTM.

Thanks!

Note that there is a dedicated folder for CGP IR tests at 'llvm/test/Transforms/CodeGenPrepare' with target subfolders under there, but there's also precedence for doing it this way, so we see IR and asm in the same file.

Oh, good to know, thanks!