Problem with the LLVM v8.0 IR 'select' instruction

Hi guys,

I am doing a hobby compiler using LLVM. I have a rather peculiar problem with the ‘select’ instruction, as far as I can see. Given this code:

Hi,

Hi guys,

I am doing a hobby compiler using LLVM. I have a rather peculiar problem with the ‘select’ instruction, as far as I can see. Given this code:


; Run-time Library Definitions
%Exception = type { i8 }

define private %Exception* @Start(i1* %$result) #0
{
prologue:
store i1 true, i1* %$result
br label %epilogue
epilogue:
ret %Exception* null
}

; Generated main() function.
define i32 @main(i32 %argc, i8** %argv)
{
prologue:
%0 = alloca i1
%1 = call %Exception @Start(i1 %0)
%2 = load i1, i1* %0
%3 = select i1 %2, i32 0, i32 1
ret i32 %3
}


I’d expect the ‘select’ instruction in main() to yield zero, not one.

Seems correct to me.

But when I compile the sample on x86_64, arm7, and aarch64, I always get code that yields one.

Can you provide a reproducer?
As far as I can tell the optimizer folds the code you provided above to yield 0: https://godbolt.org/z/T5wkSJ

Hi again,

It is probably me doing something wrong, but I have attached a trimmed down sample that shows the issue and the output that Clang 8.0.0 makes (using the binary release from llvm.org).

I invoke clang as follows: clang -S -O3 -o Evergrowing.s -Wall Evergrowing.ll

I tried using -O1, -O2, and -O3, but all where the same. Please notice that I renamed ‘main()’ to ‘mainfunc()’ to be sure Clang isn’t picking up ‘main()’ as a reserved, special function.

This on a Windows 10 x64 PC, but I got exactly the same behavior on arm7hf and aarch64.

Cheers,
Mikael Egevig

Evergrowing.ll (1.35 KB)

Evergrowing.s (427 Bytes)

I believe your issue is that @Start has the readnone attribute. This is telling the optimizers that @Start doesn’t read or write any memory.

Ouch!

Thanks for your help :slight_smile:

Cheers,
Mikael Egevig