Debian Codegraph @

clang/test/CIR/CodeGen/static-vars.cpp

raw
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --input-file=%t.cir %s
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t1.ll
4 // RUN: FileCheck --check-prefix=LLVM --input-file=%t1.ll %s
5 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t1.ll
6 // RUN: FileCheck --check-prefix=OGCG --input-file=%t1.ll %s
7
8 template<typename T>
9 struct implicitly_instantiated {
10 static T member;
11 };
12
13 template<typename T>
14 T implicitly_instantiated<T>::member = 12345;
15
16 int use_implicitly_instantiated() {
17 return implicitly_instantiated<int>::member;
18 }
19
20 // CHECK-DAG: cir.global linkonce_odr comdat @_ZN23implicitly_instantiatedIiE6memberE = #cir.int<12345> : !s32i
21 // LLVM-DAG: @_ZN23implicitly_instantiatedIiE6memberE = linkonce_odr global i32 12345, comdat
22 // OGCG-DAG: @_ZN23implicitly_instantiatedIiE6memberE = linkonce_odr global i32 12345, comdat
23
24 template<typename T>
25 struct explicitly_instantiated {
26 static T member;
27 };
28
29 template<typename T>
30 T explicitly_instantiated<T>::member = 54321;
31
32 template int explicitly_instantiated<int>::member;
33 // CHECK-DAG: cir.global weak_odr comdat @_ZN23explicitly_instantiatedIiE6memberE = #cir.int<54321> : !s32i
34 // LLVM-DAG: @_ZN23explicitly_instantiatedIiE6memberE = weak_odr global i32 54321, comdat
35 // OGCG-DAG: @_ZN23explicitly_instantiatedIiE6memberE = weak_odr global i32 54321, comdat
36
37 void func1(void) {
38 // Should lower default-initialized static vars.
39 static int i;
40 // CHECK-DAG: cir.global "private" internal dso_local @_ZZ5func1vE1i = #cir.int<0> : !s32i
41
42 // Should lower constant-initialized static vars.
43 static int j = 1;
44 // CHECK-DAG: cir.global "private" internal dso_local @_ZZ5func1vE1j = #cir.int<1> : !s32i
45
46 // Should properly shadow static vars in nested scopes.
47 {
48 static int j = 2;
49 // CHECK-DAG: cir.global "private" internal dso_local @_ZZ5func1vE1j_0 = #cir.int<2> : !s32i
50 }
51 {
52 static int j = 3;
53 // CHECK-DAG: cir.global "private" internal dso_local @_ZZ5func1vE1j_1 = #cir.int<3> : !s32i
54 }
55
56 // Should lower basic static vars arithmetics.
57 j++;
58 // CHECK-DAG: %[[#V2:]] = cir.get_global @_ZZ5func1vE1j : !cir.ptr<!s32i>
59 // CHECK-DAG: %[[#V3:]] = cir.load{{.*}} %[[#V2]] : !cir.ptr<!s32i>, !s32i
60 // CHECK-DAG: %[[#V4:]] = cir.unary(inc, %[[#V3]]) nsw : !s32i, !s32i
61 // CHECK-DAG: cir.store{{.*}} %[[#V4]], %[[#V2]] : !s32i, !cir.ptr<!s32i>
62 }
63
64 // Should shadow static vars on different functions.
65 void func2(void) {
66 static char i;
67 // CHECK-DAG: cir.global "private" internal dso_local @_ZZ5func2vE1i = #cir.int<0> : !s8i
68 static float j;
69 // CHECK-DAG: cir.global "private" internal dso_local @_ZZ5func2vE1j = #cir.fp<0.000000e+00> : !cir.float
70 }
71
72 // CHECK-DAG: cir.global linkonce_odr comdat @_ZZ4testvE1c = #cir.int<0> : !s32i
73
74 // LLVM-DAG: $_ZZ4testvE1c = comdat any
75 // LLVM-DAG: @_ZZ4testvE1c = linkonce_odr global i32 0, comdat, align 4
76 // OGCG-DAG: $_ZZ4testvE1c = comdat any
77 // OGCG-DAG: @_ZZ4testvE1c = linkonce_odr global i32 0, comdat, align 4
78
79 inline void test() { static int c; }
80 // CHECK-LABEL: @_Z4testv
81 // CHECK: {{%.*}} = cir.get_global @_ZZ4testvE1c : !cir.ptr<!s32i>
82 void foo() { test(); }