This is an archive of the discontinued LLVM Phabricator instance.

[InstComineLoadStoreAlloca] Optimize stores to GEP off null base
ClosedPublic

Authored by anna on Dec 8 2017, 10:39 AM.

Details

Summary

Currently, in InstCombineLoadStoreAlloca, we have simplification rules
for the following cases:

  1. load off a null
  2. load off a GEP with null base
  3. store to a null

This patch adds support for the fourth case which is store into a GEP with null
base. Since this is UB as well (and directly analogous to the load off a
GEP with null base), we can substitute the stored val with
undef in instcombine, so that SimplifyCFG can optimize this code into
unreachable code.

Note: Right now, simplifyCFG hasn't been taught about optimizing this to
unreachable and adding an llvm.trap (this is already done for the above 3 cases).

I would like to confirm whether this is just a missed optimization, or
there is something fundamental that prevents this transformation.

Event Timeline

anna created this revision.Dec 8 2017, 10:39 AM
davide added inline comments.Dec 8 2017, 10:51 AM
lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
941–943

return isa<> ?

sanjoy accepted this revision.Dec 8 2017, 11:25 AM

lgtm

lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
935

Minor bikeshed -- how about checking SI.getPointerAddressSpace() == 0 with an early exit?

937

How about:

if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr))
  Ptr = GEPI->getOperand(0);
// Logic for Ptr here

?

This revision is now accepted and ready to land.Dec 8 2017, 11:25 AM
davide accepted this revision.Dec 8 2017, 11:27 AM

Forgot to click accept

This revision was automatically updated to reflect the committed changes.
anna marked 3 inline comments as done.Dec 12 2017, 6:18 AM