HomePhabricator

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

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

Description

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

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

Differential Revision: https://reviews.llvm.org/D76645