This is an archive of the discontinued LLVM Phabricator instance.

[GlobalOpt] Treat null-check of loaded value as use of global (PR35760)
ClosedPublic

Authored by vsk on Mar 23 2020, 3:18 PM.

Details

Summary

PR35760 shows an example program which, when compiled with clang -O0
or gcc at any optimization level, prints '0'. However, llvm transforms
the program in a way that causes it to print '1'.

Fix the issue by having AllUsesOfValueWillTrapIfNull return false when
analyzing a load from a global which is used by an icmp. This special
case was untested [0] so this is just deleting dead code.

An alternative fix might be to change the GlobalStatus analysis for the
global to report "Stored" instead of "StoredOnce". However, "StoredOnce"
is appropriate when only one value other than the initializer is stored
to the global.

[0]
http://lab.llvm.org:8080/coverage/coverage-reports/coverage/Users/buildslave/jenkins/workspace/coverage/llvm-project/llvm/lib/Transforms/IPO/GlobalOpt.cpp.html#L662

https://bugs.llvm.org/show_bug.cgi?id=35760

Diff Detail

Event Timeline

vsk created this revision.Mar 23 2020, 3:18 PM
efriedma accepted this revision.Mar 23 2020, 4:34 PM

LGTM

We could theoretically optimize this to two globals: one for the actual allocation, and a boolean representing whether it's been allocated. But that would be a different transform, and I don't expect you to implement it.

llvm/test/Transforms/GlobalOpt/null-check-is-use-pr35760.ll
48

Please delete unnecessary metadata.

This revision is now accepted and ready to land.Mar 23 2020, 4:34 PM
vsk marked an inline comment as done.Mar 23 2020, 10:35 PM

Thanks!

llvm/test/Transforms/GlobalOpt/null-check-is-use-pr35760.ll
48

I'll fix this before committing.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2020, 10:50 PM