This is an archive of the discontinued LLVM Phabricator instance.

[instsimplify] Assume storage for byval args doesn't overlap allocas, globals, or other byval args
ClosedPublic

Authored by reames on Feb 18 2022, 8:12 AM.

Details

Summary

This allows us to discharge many pointer comparisons based on byval arguments.

Diff Detail

Event Timeline

reames created this revision.Feb 18 2022, 8:12 AM
reames requested review of this revision.Feb 18 2022, 8:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 18 2022, 8:12 AM
fhahn added inline comments.Feb 18 2022, 8:22 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2674–2679

Could this lead to a regression where CtxI is nullptr but LHS is an instruction?

llvm/test/Transforms/InstSimplify/compare.ll
2740

TODO can be dropped now.

2761

TODO can be dropped now.

reames added inline comments.Feb 18 2022, 8:28 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2674–2679

In theory, yes. I'll rewrite to assume either alloca or argument for now.

reames updated this revision to Diff 409943.Feb 18 2022, 8:32 AM

Address review comments

nikic added inline comments.Feb 18 2022, 9:15 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2571

Can't this function be simplified to isByValArgOrAllocaOrGlobalVar(V1) && isByValArgOrAllocaOrGlobalVar(V2)? Is there some reason your implementation is going out of the way to not handle the GlobalVariable + GlobalVariable case? (Note that GlobalVariable != GlobalObject, so this does not included aliases).

reames added inline comments.Feb 18 2022, 9:30 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2571

The bit of code your commenting on was the original structure - I moved it, but didn't change it.

When I posted this, I hadn't dug into the global piece in detail. I have now. Global are slightly tricky because not all them are guaranteed disjoint - or at least that's what the code in constant folding seems to imply? We don't actually get here with globals - they're constantexprs - but I didn't want to risk adding rarely executed potentially divergent behavior.

I think that your suggestion probably is okay, but I'd strongly prefer to go with the current structure for now, and restructure in a separate change for risk isolation. My hope is to eventually pull this out in a way where we can reuse it in constant folding as well.

nikic added inline comments.Feb 18 2022, 9:49 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2571

Hm yeah, I guess the two global case is not legal for unnamed_addr globals, which may be merged.

I think it may still be worthwhile to write this in a way that explicitly excludes that case, rather than listing different permutations of possible combinations. Something like this, maybe?

auto IsDisjointObject = [](Value *V) {
  auto *Arg = dyn_cast<Argument>(V);
  return isa<AllocaInst>(V) || isa<GlobalVariable>(V) || (Arg && Arg->hasByValAttr());
};
if (!IsDisjointObject(V1) || !IsDisjointObject(V2))
  return false;

// Two globals may have the same address.
// TODO: Make this specific to unnamed_addr globals.
return !isa<GlobalVariable>(V1) || !isa<GlobalVariable>(V2);
nikic accepted this revision.Feb 18 2022, 9:53 AM

LGTM

llvm/lib/Analysis/InstructionSimplify.cpp
2571

But I think it's also okay as-is...

This revision is now accepted and ready to land.Feb 18 2022, 9:53 AM
fhahn accepted this revision.Feb 18 2022, 10:35 AM

LGTM, thanks!

Since you're in the area, have you seen https://github.com/llvm/llvm-project/issues/45725 ?

I had not. Skimmed it, doesn't seem directly related. I'm not even sure there's a bug there, much less one immediately relevant here.

llvm/lib/Analysis/InstructionSimplify.cpp
2571

I will play with some options here and try posting something in another review.

This revision was landed with ongoing or failed builds.Feb 18 2022, 11:08 AM
This revision was automatically updated to reflect the committed changes.
reames added inline comments.Feb 18 2022, 11:34 AM
llvm/lib/Analysis/InstructionSimplify.cpp
2571

I ended up landing basically exactly your suggestion with a comment explaining the assumption we make about globals here, and we expect not to see the two globals case. See 3a6be12.

I wrote
https://github.com/llvm/llvm-project/issues/54615
about a crash that started happening with this patch.

Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2022, 6:25 AM