Home | History | Annotate | Download | only in CodeGen
      1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O1 -no-struct-path-tbaa -disable-llvm-optzns %s -emit-llvm -o - | FileCheck %s
      2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -O1 -disable-llvm-optzns %s -emit-llvm -o - | FileCheck %s -check-prefix=PATH
      3 // Test TBAA metadata generated by front-end.
      4 
      5 typedef unsigned char uint8_t;
      6 typedef unsigned short uint16_t;
      7 typedef unsigned int uint32_t;
      8 typedef unsigned long long uint64_t;
      9 class StructA
     10 {
     11 public:
     12    uint16_t f16;
     13    uint32_t f32;
     14    uint16_t f16_2;
     15    uint32_t f32_2;
     16 };
     17 class StructB
     18 {
     19 public:
     20    uint16_t f16;
     21    StructA a;
     22    uint32_t f32;
     23 };
     24 class StructC
     25 {
     26 public:
     27    uint16_t f16;
     28    StructB b;
     29    uint32_t f32;
     30 };
     31 class StructD
     32 {
     33 public:
     34    uint16_t f16;
     35    StructB b;
     36    uint32_t f32;
     37    uint8_t f8;
     38 };
     39 
     40 class StructS
     41 {
     42 public:
     43    uint16_t f16;
     44    uint32_t f32;
     45 };
     46 class StructS2 : public StructS
     47 {
     48 public:
     49    uint16_t f16_2;
     50    uint32_t f32_2;
     51 };
     52 
     53 uint32_t g(uint32_t *s, StructA *A, uint64_t count) {
     54 // CHECK: define i32 @{{.*}}(
     55 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
     56 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
     57 // PATH: define i32 @{{.*}}(
     58 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32:!.*]]
     59 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32:!.*]]
     60   *s = 1;
     61   A->f32 = 4;
     62   return *s;
     63 }
     64 
     65 uint32_t g2(uint32_t *s, StructA *A, uint64_t count) {
     66 // CHECK: define i32 @{{.*}}(
     67 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
     68 // CHECK: store i16 4, i16* %{{.*}}, align 2, !tbaa [[TAG_i16:!.*]]
     69 // PATH: define i32 @{{.*}}(
     70 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
     71 // PATH: store i16 4, i16* %{{.*}}, align 2, !tbaa [[TAG_A_f16:!.*]]
     72   *s = 1;
     73   A->f16 = 4;
     74   return *s;
     75 }
     76 
     77 uint32_t g3(StructA *A, StructB *B, uint64_t count) {
     78 // CHECK: define i32 @{{.*}}(
     79 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
     80 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
     81 // PATH: define i32 @{{.*}}(
     82 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
     83 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32:!.*]]
     84   A->f32 = 1;
     85   B->a.f32 = 4;
     86   return A->f32;
     87 }
     88 
     89 uint32_t g4(StructA *A, StructB *B, uint64_t count) {
     90 // CHECK: define i32 @{{.*}}(
     91 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
     92 // CHECK: store i16 4, i16* %{{.*}}, align 2, !tbaa [[TAG_i16]]
     93 // PATH: define i32 @{{.*}}(
     94 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
     95 // PATH: store i16 4, i16* %{{.*}}, align 2, !tbaa [[TAG_B_a_f16:!.*]]
     96   A->f32 = 1;
     97   B->a.f16 = 4;
     98   return A->f32;
     99 }
    100 
    101 uint32_t g5(StructA *A, StructB *B, uint64_t count) {
    102 // CHECK: define i32 @{{.*}}(
    103 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    104 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    105 // PATH: define i32 @{{.*}}(
    106 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
    107 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_f32:!.*]]
    108   A->f32 = 1;
    109   B->f32 = 4;
    110   return A->f32;
    111 }
    112 
    113 uint32_t g6(StructA *A, StructB *B, uint64_t count) {
    114 // CHECK: define i32 @{{.*}}(
    115 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    116 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    117 // PATH: define i32 @{{.*}}(
    118 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
    119 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32_2:!.*]]
    120   A->f32 = 1;
    121   B->a.f32_2 = 4;
    122   return A->f32;
    123 }
    124 
    125 uint32_t g7(StructA *A, StructS *S, uint64_t count) {
    126 // CHECK: define i32 @{{.*}}(
    127 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    128 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    129 // PATH: define i32 @{{.*}}(
    130 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
    131 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32:!.*]]
    132   A->f32 = 1;
    133   S->f32 = 4;
    134   return A->f32;
    135 }
    136 
    137 uint32_t g8(StructA *A, StructS *S, uint64_t count) {
    138 // CHECK: define i32 @{{.*}}(
    139 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    140 // CHECK: store i16 4, i16* %{{.*}}, align 2, !tbaa [[TAG_i16]]
    141 // PATH: define i32 @{{.*}}(
    142 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_A_f32]]
    143 // PATH: store i16 4, i16* %{{.*}}, align 2, !tbaa [[TAG_S_f16:!.*]]
    144   A->f32 = 1;
    145   S->f16 = 4;
    146   return A->f32;
    147 }
    148 
    149 uint32_t g9(StructS *S, StructS2 *S2, uint64_t count) {
    150 // CHECK: define i32 @{{.*}}(
    151 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    152 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    153 // PATH: define i32 @{{.*}}(
    154 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
    155 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32:!.*]]
    156   S->f32 = 1;
    157   S2->f32 = 4;
    158   return S->f32;
    159 }
    160 
    161 uint32_t g10(StructS *S, StructS2 *S2, uint64_t count) {
    162 // CHECK: define i32 @{{.*}}(
    163 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    164 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    165 // PATH: define i32 @{{.*}}(
    166 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_S_f32]]
    167 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_S2_f32_2:!.*]]
    168   S->f32 = 1;
    169   S2->f32_2 = 4;
    170   return S->f32;
    171 }
    172 
    173 uint32_t g11(StructC *C, StructD *D, uint64_t count) {
    174 // CHECK: define i32 @{{.*}}(
    175 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    176 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    177 // PATH: define i32 @{{.*}}(
    178 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_C_b_a_f32:!.*]]
    179 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_D_b_a_f32:!.*]]
    180   C->b.a.f32 = 1;
    181   D->b.a.f32 = 4;
    182   return C->b.a.f32;
    183 }
    184 
    185 uint32_t g12(StructC *C, StructD *D, uint64_t count) {
    186 // CHECK: define i32 @{{.*}}(
    187 // CHECK: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    188 // CHECK: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_i32]]
    189 // TODO: differentiate the two accesses.
    190 // PATH: define i32 @{{.*}}(
    191 // PATH: store i32 1, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32]]
    192 // PATH: store i32 4, i32* %{{.*}}, align 4, !tbaa [[TAG_B_a_f32]]
    193   StructB *b1 = &(C->b);
    194   StructB *b2 = &(D->b);
    195   // b1, b2 have different context.
    196   b1->a.f32 = 1;
    197   b2->a.f32 = 4;
    198   return b1->a.f32;
    199 }
    200 
    201 // CHECK: [[TYPE_char:!.*]] = metadata !{metadata !"omnipotent char", metadata [[TAG_cxx_tbaa:!.*]],
    202 // CHECK: [[TAG_cxx_tbaa]] = metadata !{metadata !"Simple C/C++ TBAA"}
    203 // CHECK: [[TAG_i32]] = metadata !{metadata [[TYPE_i32:!.*]], metadata [[TYPE_i32]], i64 0}
    204 // CHECK: [[TYPE_i32]] = metadata !{metadata !"int", metadata [[TYPE_char]],
    205 // CHECK: [[TAG_i16]] = metadata !{metadata [[TYPE_i16:!.*]], metadata [[TYPE_i16]], i64 0}
    206 // CHECK: [[TYPE_i16]] = metadata !{metadata !"short", metadata [[TYPE_char]],
    207 
    208 // PATH: [[TYPE_CHAR:!.*]] = metadata !{metadata !"omnipotent char", metadata
    209 // PATH: [[TAG_i32]] = metadata !{metadata [[TYPE_INT:!.*]], metadata [[TYPE_INT]], i64 0}
    210 // PATH: [[TYPE_INT]] = metadata !{metadata !"int", metadata [[TYPE_CHAR]]
    211 // PATH: [[TAG_A_f32]] = metadata !{metadata [[TYPE_A:!.*]], metadata [[TYPE_INT]], i64 4}
    212 // PATH: [[TYPE_A]] = metadata !{metadata !"_ZTS7StructA", metadata [[TYPE_SHORT:!.*]], i64 0, metadata [[TYPE_INT]], i64 4, metadata [[TYPE_SHORT]], i64 8, metadata [[TYPE_INT]], i64 12}
    213 // PATH: [[TYPE_SHORT:!.*]] = metadata !{metadata !"short", metadata [[TYPE_CHAR]]
    214 // PATH: [[TAG_A_f16]] = metadata !{metadata [[TYPE_A]], metadata [[TYPE_SHORT]], i64 0}
    215 // PATH: [[TAG_B_a_f32]] = metadata !{metadata [[TYPE_B:!.*]], metadata [[TYPE_INT]], i64 8}
    216 // PATH: [[TYPE_B]] = metadata !{metadata !"_ZTS7StructB", metadata [[TYPE_SHORT]], i64 0, metadata [[TYPE_A]], i64 4, metadata [[TYPE_INT]], i64 20}
    217 // PATH: [[TAG_B_a_f16]] = metadata !{metadata [[TYPE_B]], metadata [[TYPE_SHORT]], i64 4}
    218 // PATH: [[TAG_B_f32]] = metadata !{metadata [[TYPE_B]], metadata [[TYPE_INT]], i64 20}
    219 // PATH: [[TAG_B_a_f32_2]] = metadata !{metadata [[TYPE_B]], metadata [[TYPE_INT]], i64 16}
    220 // PATH: [[TAG_S_f32]] = metadata !{metadata [[TYPE_S:!.*]], metadata [[TYPE_INT]], i64 4}
    221 // PATH: [[TYPE_S]] = metadata !{metadata !"_ZTS7StructS", metadata [[TYPE_SHORT]], i64 0, metadata [[TYPE_INT]], i64 4}
    222 // PATH: [[TAG_S_f16]] = metadata !{metadata [[TYPE_S]], metadata [[TYPE_SHORT]], i64 0}
    223 // PATH: [[TAG_S2_f32_2]] = metadata !{metadata [[TYPE_S2:!.*]], metadata [[TYPE_INT]], i64 12}
    224 // PATH: [[TYPE_S2]] = metadata !{metadata !"_ZTS8StructS2", metadata [[TYPE_SHORT]], i64 8, metadata [[TYPE_INT]], i64 12}
    225 // PATH: [[TAG_C_b_a_f32]] = metadata !{metadata [[TYPE_C:!.*]], metadata [[TYPE_INT]], i64 12}
    226 // PATH: [[TYPE_C]] = metadata !{metadata !"_ZTS7StructC", metadata [[TYPE_SHORT]], i64 0, metadata [[TYPE_B]], i64 4, metadata [[TYPE_INT]], i64 28}
    227 // PATH: [[TAG_D_b_a_f32]] = metadata !{metadata [[TYPE_D:!.*]], metadata [[TYPE_INT]], i64 12}
    228 // PATH: [[TYPE_D]] = metadata !{metadata !"_ZTS7StructD", metadata [[TYPE_SHORT]], i64 0, metadata [[TYPE_B]], i64 4, metadata [[TYPE_INT]], i64 28, metadata [[TYPE_CHAR]], i64 32}
    229