Yes, the full test case is:
static int x = 100;
int y = whatever;
int main() {
x = -101;
y = whatever that’s not whatever above;
printf(“%d\n”, y);
printf(“%d\n”, x);
return 0;
}
You are correct, in the above test case the globalopt does not make the transformation….
I’m more annoyed that it does not turn x into an alloca. With this code:
$ cat /tmp/test.c
static int x = 100;
int y = 2;
int main() {
x = -101;
y = 1;
printf(“%d\n”, y);
printf(“%d\n”, x);
return 0;
}
$ clang -O3 -S -emit-llvm -o - /tmp/test.c
; ModuleID = ‘/tmp/test.c’
source_filename = “/tmp/test.c”
target datalayout = “e-m:o-i64:64-f80:128-n8:16:32:64-S128”
target triple = “x86_64-apple-macosx10.12.0”
@y = global i32 2, align 4
@x = internal unnamed_addr global i1 false, align 4
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
; Function Attrs: nounwind ssp uwtable
define i32 @main() #0 {
store i1 true, i1* @x, align 4
store i32 1, i32* @y, align 4, !tbaa !2
%1 = tail call i32 (i8*, …) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 1)
%2 = load i1, i1* @x, align 4
%3 = select i1 %2, i32 -101, i32 100
%4 = tail call i32 (i8*, …) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
ret i32 0
}
however, I think the original issue still stands, it really shouldn’t be doing it here either.
Yes.