This is an archive of the discontinued LLVM Phabricator instance.

Incorrect IR involving the use of bzero
AcceptedPublic

Authored by Bharathi on Nov 7 2017, 10:18 AM.

Details

Reviewers
rjmccall
Summary

This issue is specific to the use of bzero in a conditional expression as below.

bash-4.1$ cat test.c

typedef SIZE_TYPE size_t;
void bzero(void*, size_t);
void foo(void);

void test_bzero() {

char dst[20];
int _sz = 20, len = 20;
return (_sz
        ? ((_sz >= len)
           ? bzero(dst, len)
           : foo())
        : bzero(dst, len));

}

Compiling this test case results in the following error:

Instruction does not dominate all uses!

%arraydecay = getelementptr inbounds [20 x i8], [20 x i8]* %dst, i32 0, i32 0
%cond = phi i8* [ %arraydecay, %cond.end ], [ %arraydecay3, %cond.false2 ]

fatal error: error in backend: Broken function found, compilation aborted!

Inspecting the IR, the generation of Phi node appears to be incorrect. Looks like the phi node (for merging dst) is rather unnecessary since bzero returns void. In EmitBuiltinExpr(), bzero is replaced by a call to memset, which returns a pointer to the destination.

The proposed patch addresses this issue. The llvm test suite and spec runs are succcessful with the patch.

Diff Detail

Event Timeline

Bharathi created this revision.Nov 7 2017, 10:18 AM
rjmccall added inline comments.Nov 7 2017, 3:30 PM
test/CodeGen/bzero.c
18 ↗(On Diff #121923)

I think you could just add this to builtin.c, and please make the test directly call __builtin_bzero instead of relying on the recognition of bzero as a builtin. Also, please rename the test function to something like test_conditional_bzero.

Bharathi updated this revision to Diff 122008.Nov 7 2017, 4:15 PM

Addressed review comments.

rjmccall accepted this revision.Nov 7 2017, 9:52 PM

That looks great, thanks. Do you have commit privileges?

This revision is now accepted and ready to land.Nov 7 2017, 9:52 PM

I do not have commit privileges. Could you (rjmccall) or someone please commit this patch on my behalf ?
Thanks.

r317776. Thanks for the contribution!