Clang-4.0 generates redundant methods when initializing global variables

I am currently studying LLVM, observing how clang deals with complex situations. I wrote (top level, not in function):

int qaq = 666;
int tat = 233;

auto hh = qaq + tat;

And I use the command:

clang-4.0 003.cpp -emit-llvm -S -std=c++11

And clang generates these codes:

@qaq = global i32 666, align 4
@tat = global i32 233, align 4
@hh = global i32 0, align 4
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_003.cpp, i8* null }]

; Function Attrs: noinline uwtable
define internal void @__cxx_global_var_init() #0 section ".text.startup" {
  %1 = load i32, i32* @qaq, align 4
  %2 = load i32, i32* @tat, align 4
  %3 = add nsw i32 %1, %2
  store i32 %3, i32* @hh, align 4
  ret void
}

; Function Attrs: noinline uwtable
define internal void @_GLOBAL__sub_I_003.cpp() #0 section ".text.startup" {
  call void @__cxx_global_var_init()
  ret void
}

I got confused with _GLOBAL__sub_I_003.cpp: why does clang generate a function that actually only calls another function (and does nothing)? Even both of them have no parameters?

+4
source share
1 answer

Disclaimer: This is my interpretation of the logic, I am not part of the LLVM team.

, : .

:

int qaq = 666;
int tat = 233;

auto hh = qaq + tat;
auto ii = qaq - tat;

:

; Function Attrs: noinline uwtable
define internal void @__cxx_global_var_init() #0 section ".text.startup" !dbg !16 {
  %1 = load i32, i32* @qaq, align 4, !dbg !19
  %2 = load i32, i32* @tat, align 4, !dbg !20
  %3 = add nsw i32 %1, %2, !dbg !21
  store i32 %3, i32* @hh, align 4, !dbg !21
  ret void, !dbg !20
}

; Function Attrs: noinline uwtable
define internal void @__cxx_global_var_init.1() #0 section ".text.startup" !dbg !22 {
  %1 = load i32, i32* @qaq, align 4, !dbg !23
  %2 = load i32, i32* @tat, align 4, !dbg !24
  %3 = sub nsw i32 %1, %2, !dbg !25
  store i32 %3, i32* @ii, align 4, !dbg !25
  ret void, !dbg !24
}

; Function Attrs: noinline uwtable
define internal void @_GLOBAL__sub_I_example.cpp() #0 section ".text.startup" !dbg !26 {
  call void @__cxx_global_var_init(), !dbg !28
  call void @__cxx_global_var_init.1(), !dbg !29
  ret void
}

, , CLANG _GLOBAL__sub_I_example.cpp(). , / .

, , .

: " , ".

:

  • .
  • .

, , ?

  • .
  • .
  • .
  • .

, , .

+3

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


All Articles