Why does clang align certain things

Suppose I have a base program in c and I compile it with clang, for example:

#include "stdio.h"
int x = 0x7FFFFFFF;
int main(void)
{

    printf("%d\n",x);
}

Compiling using clang -emit-llvm temp.c -fno-rtti -O3 -Scalls 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 strings that reference a variable x. If you notice, it seems that it is xdeclared as an aligned variable (it is aligned to 4).

How does clang know what intneeds to be aligned? I argue that clang has no idea about aligning any variables, because it depends on which backend you are using. For example, I could use the backend for an 8-bit machine, then it would not have to be aligned at all.

, : clang , , ?

+4
1

LLVM MAN

, , . :

target datalayout = "layout specification"

, - ('-'). . :

[...]

n < size1 > : < size2 > : < size3 > ...

. , n32 32- PowerPC, n32: 64 PowerPC 64 n8: 16: 32: 64 X86-64. .

LLVM , :

, .

  • , seek , , .
  • , . , , i7 i8 ( ), i65 i256 i64 ( ).
  • , seek , , , . , < 128 x double > , , 64 < 2 x double > .

++ **

[...]

, 2. , , , . , .

0

Source: https://habr.com/ru/post/1652995/


All Articles