[llvm-dev] Mem2reg: load before single store

László Radnai via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 14 00:19:12 PDT 2020


Hi all!

While playing with LLVM, I've found a weird behavior in mem2reg pass.

When optimizing single stores, undefined value is placed before any load
preceding the store (based on basicblock's ordering and simple dominator
analysis, if I remember correctly).

This is the line that is responsible for the behavior: (LLVM9 does the same)

https://llvm.org/doxygen/PromoteMemoryToRegister_8cpp_source.html#l00629

A problem arises, and I am not sure if it is really a problem or just weird
C-compliant behavior.

int a; // or, equally, int a=0;

int main(){
 int b;
 if (b) // (*)
   b=a;
 if (b)
   printf("This will be called");
}

The first load of variable b, before the single store (the first branching)
is replaced by undef, so the second branch will be replaced by a phi node,
if the (*) branch is taken, the value is 0, else undef.

I'm concerned that this is an LLVM bug.

Reproduction:

clang -S -emit-llvm test.c
opt -mem2reg test.ll

I'm not at the computer right now, so I cannot show the exact generated
code.

Radnai László
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200914/5a6c769f/attachment.html>


More information about the llvm-dev mailing list