Suppose I have a basic program in c, and I compile it with clang, like the following:
#include "stdio.h"
int x = 0x7FFFFFFF;
int main(void)
{
printf("%d\n",x);
}
Compiling this with clang -emit-llvm temp.c -fno-rtti -O3 -S
produces the following bitcode:
; ModuleID = 'temp.c'
target datalayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
target triple = "i686-pc-linux-gnu"
@x = global i32 2147483647, align 4
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
; Function Attrs: nounwind
define i32 @main() #0 {
entry:
%0 = load i32, i32* @x, align 4, !tbaa !1
%call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %0) #1
ret i32 0
}
; Function Attrs: nounwind
declare i32 @printf(i8* nocapture readonly, ...) #0
attributes #0 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="pentium4" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind }
!llvm.ident = !{!0}
!0 = !{!"clang version 3.7.1 "}
!1 = !{!2, !2, i64 0}
!2 = !{!"int", !3, i64 0}
!3 = !{!"omnipotent char", !4, i64 0}
!4 = !{!"Simple C/C++ TBAA"}
Now my real question is about the lines that reference the variable x
. If you notice, it seems that x
is declared as an aligned variable (it is aligned to 4).
How does clang know that int
s should be aligned? My claim is that clang does not have any clue about the alignment of any variables, because this depends on what backend you use. For example I could use a backend for an 8 bit machine, then it would not need to be aligned at all.
So my question is: Why does clang guess at alignment, seeing as it really is not possible?