Skip to content

Incorrect optimization of assignment in present of memset #33435

@llvmbot

Description

@llvmbot
Bugzilla Link 34088
Resolution FIXED
Resolved on Aug 10, 2017 18:54
Version 5.0
OS Linux
Blocks #33196
Attachments An example to reproduce the problem.
Reporter LLVM Bugzilla Contributor
CC @topperc,@zmodem,@RKSimon

Extended Description

During optimization clang removes assignments that should not be eliminated.
I have the following code:

Init(&foo);
buffer = foo.bar.buffer;
memset(&foo, 0xCD, sizeof (foo));
foo.bar.buffer = buffer;
Print(&foo);

and if optimization is enabled, it becomes like this:

Init(&foo);
memset(&foo, 0xCD, sizeof (foo));
Print(&foo);

The bug happens only with target: i386-pc-linux-gnu and for some layout of
structures. (Perhaps, it can happen on x86_64 too for different structures,
but I have not encountered it only on i386 architecture.)

I have attached a full example that demonstrates the problem. It includes
the following files:
test1.h - defines some structures.
test1.c - the main code that is incorrectly optimized
test2.c - defines Init() and Print()
Makefile - builds and runs the example code

If optimization is not enabled, the program prints as expected:
ptr=(nil) size=0x0
however when any level optimization is enabled, I get the following output:
ptr=0xcdcdcdcd size=0xcdcdcdcd
i.e. the initial value of 'buffer' set in Init() is overwritten by memset.

clang information:
$ clang-5.0 -m32 --version
clang version 5.0.0-svn309965-1~exp1 (branches/release_50)
Target: i386-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:codegenIR generation bugs: mangling, exceptions, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions