204 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir2// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR3// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll4// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM5// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll6// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG7 8struct empty{};9 10struct Point {11 int x;12 int y;13 char c[3];14 int z;15 [[no_unique_address]] empty e;16};17 18struct [[gnu::packed]] packed {19 char c;20 int i;21};22 23struct [[gnu::packed]] alignas(8) packed_and_aligned {24 short s;25 char c;26 float f;27};28 29struct simple {30 int a, b;31};32 33// Byte-aligned bitfields34struct byte_aligned_bitfields {35 unsigned int a : 8;36 unsigned int b : 8;37 unsigned int c : 16;38};39 40struct signed_byte_aligned_bitfields {41 int x : 8;42 int y : 8;43};44 45struct single_byte_bitfield {46 unsigned char a : 8;47};48 49// Partial bitfields (sub-byte)50struct partial_bitfields {51 unsigned int a : 3;52 unsigned int b : 5;53 unsigned int c : 8;54};55 56struct signed_partial_bitfields {57 int x : 4;58 int y : 4;59};60 61struct mixed_partial_bitfields {62 unsigned char a : 1;63 unsigned char b : 1;64 unsigned char c : 1;65 unsigned char d : 5;66};67 68void function() {69 constexpr static empty e;70 71 constexpr static Point p1{10, 20, {99, 88, 77}, 40, e};72 73 constexpr static Point array[] {74 {123, 456, {11, 22, 33}, 789, {}},75 {10, 20, {0, 0 ,0}, 40}76 };77 78 constexpr static packed p2 {123, 456};79 constexpr static packed packed_array[] {80 p2, p281 };82 83 constexpr static packed_and_aligned paa {1, 2, 3.f};84 constexpr static packed_and_aligned paa_array[2] {85 {1, 2, 3.f}86 };87 88 constexpr static simple s{0, -1};89 constexpr static simple simple_array[] {90 s, {1111, 2222}, s91 };92 93 // Byte-aligned bitfield tests94 constexpr static byte_aligned_bitfields ba_bf1 = {0xFF, 0xAA, 0x1234};95 constexpr static signed_byte_aligned_bitfields ba_bf2 = {-1, 127};96 constexpr static single_byte_bitfield ba_bf3 = {42};97 98 // Partial bitfield tests99 constexpr static partial_bitfields p_bf1 = {1, 2, 3};100 constexpr static signed_partial_bitfields p_bf2 = {-1, 7};101 constexpr static mixed_partial_bitfields p_bf3 = {1, 0, 1, 15};102}103 104// Anonymous struct type definitions for bitfields105// CIR-DAG: !rec_anon_struct = !cir.record<struct {!u8i, !u8i, !u8i, !u8i}>106// CIR-DAG: !rec_anon_struct1 = !cir.record<struct {!u8i, !u8i, !cir.array<!u8i x 2>}>107 108// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE1e = #cir.zero : !rec_empty109// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE1s = #cir.const_record<{#cir.int<0> : !s32i, #cir.int<-1> : !s32i}> : !rec_simple110// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE2p1 = #cir.const_record<{#cir.int<10> : !s32i, #cir.int<20> : !s32i, #cir.const_array<[#cir.int<99> : !s8i, #cir.int<88> : !s8i, #cir.int<77> : !s8i]> : !cir.array<!s8i x 3>, #cir.int<40> : !s32i}> : !rec_Point111// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE2p2 = #cir.const_record<{#cir.int<123> : !s8i, #cir.int<456> : !s32i}> : !rec_packed112// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE3paa = #cir.const_record<{#cir.int<1> : !s16i, #cir.int<2> : !s8i, #cir.fp<3.000000e+00> : !cir.float, #cir.zero : !u8i}> : !rec_packed_and_aligned113 114// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE5array = #cir.const_array<[115// CIR-DAG-SAME: #cir.const_record<{#cir.int<123> : !s32i, #cir.int<456> : !s32i, #cir.const_array<[#cir.int<11> : !s8i, #cir.int<22> : !s8i, #cir.int<33> : !s8i]> : !cir.array<!s8i x 3>, #cir.int<789> : !s32i}> : !rec_Point116// CIR-DAG-SAME: #cir.const_record<{#cir.int<10> : !s32i, #cir.int<20> : !s32i, #cir.zero : !cir.array<!s8i x 3>, #cir.int<40> : !s32i}> : !rec_Point117// CIR-DAG-SAME: ]> : !cir.array<!rec_Point x 2>118 119// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE12simple_array = #cir.const_array<[120// CIR-DAG-SAME: #cir.const_record<{#cir.int<0> : !s32i, #cir.int<-1> : !s32i}> : !rec_simple,121// CIR-DAG-SAME: #cir.const_record<{#cir.int<1111> : !s32i, #cir.int<2222> : !s32i}> : !rec_simple,122// CIR-DAG-SAME: #cir.const_record<{#cir.int<0> : !s32i, #cir.int<-1> : !s32i}> : !rec_simple123// CIR-DAG-SAME: ]> : !cir.array<!rec_simple x 3>124 125// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE12packed_array = #cir.const_array<[126// CIR-DAG-SAME: #cir.const_record<{#cir.int<123> : !s8i, #cir.int<456> : !s32i}> : !rec_packed,127// CIR-DAG-SAME: #cir.const_record<{#cir.int<123> : !s8i, #cir.int<456> : !s32i}> : !rec_packed128// CIR-DAG-SAME: ]> : !cir.array<!rec_packed x 2>129 130// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE9paa_array = #cir.const_array<[131// CIR-DAG-SAME: #cir.const_record<{#cir.int<1> : !s16i, #cir.int<2> : !s8i, #cir.fp<3.000000e+00> : !cir.float, #cir.zero : !u8i}> : !rec_packed_and_aligned,132// CIR-DAG-SAME: #cir.zero : !rec_packed_and_aligned133// CIR-DAG-SAME: ]> : !cir.array<!rec_packed_and_aligned x 2>134 135// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE6ba_bf1 = #cir.const_record<{136// CIR-DAG-SAME: #cir.int<255> : !u8i,137// CIR-DAG-SAME: #cir.int<170> : !u8i,138// CIR-DAG-SAME: #cir.int<52> : !u8i,139// CIR-DAG-SAME: #cir.int<18> : !u8i140// CIR-DAG-SAME: }> : !rec_anon_struct141// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE6ba_bf2 = #cir.const_record<{142// CIR-DAG-SAME: #cir.int<255> : !u8i,143// CIR-DAG-SAME: #cir.int<127> : !u8i,144// CIR-DAG-SAME: #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array<!u8i x 2>145// CIR-DAG-SAME: }> : !rec_anon_struct1146// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE6ba_bf3 = #cir.const_record<{147// CIR-DAG-SAME: #cir.int<42> : !u8i148// CIR-DAG-SAME: }> : !rec_single_byte_bitfield149// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE5p_bf1 = #cir.const_record<{150// CIR-DAG-SAME: #cir.int<17> : !u8i,151// CIR-DAG-SAME: #cir.int<3> : !u8i,152// CIR-DAG-SAME: #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array<!u8i x 2>153// CIR-DAG-SAME: }> : !rec_anon_struct1154// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE5p_bf2 = #cir.const_record<{155// CIR-DAG-SAME: #cir.int<127> : !u8i,156// CIR-DAG-SAME: #cir.const_array<[#cir.zero : !u8i, #cir.zero : !u8i, #cir.zero : !u8i]> : !cir.array<!u8i x 3>157// CIR-DAG-SAME: }> : !rec_signed_partial_bitfields158// CIR-DAG: cir.global "private" constant internal dso_local @_ZZ8functionvE5p_bf3 = #cir.const_record<{159// CIR-DAG-SAME: #cir.int<125> : !u8i160// CIR-DAG-SAME: }> : !rec_mixed_partial_bitfields161 162// CIR-LABEL: cir.func dso_local @_Z8functionv()163// CIR: cir.return164 165 166// LLVM-DAG: @_ZZ8functionvE12packed_array = internal constant [2 x %struct.packed] [%struct.packed <{ i8 123, i32 456 }>, %struct.packed <{ i8 123, i32 456 }>]167// LLVM-DAG: @_ZZ8functionvE12simple_array = internal constant [3 x %struct.simple] [%struct.simple { i32 0, i32 -1 }, %struct.simple { i32 1111, i32 2222 }, %struct.simple { i32 0, i32 -1 }]168// LLVM-DAG: @_ZZ8functionvE1e = internal constant %struct.empty zeroinitializer169// LLVM-DAG: @_ZZ8functionvE1s = internal constant %struct.simple { i32 0, i32 -1 }170// LLVM-DAG: @_ZZ8functionvE2p1 = internal constant %struct.Point { i32 10, i32 20, [3 x i8] c"cXM", i32 40 }171// LLVM-DAG: @_ZZ8functionvE2p2 = internal constant %struct.packed <{ i8 123, i32 456 }>172// LLVM-DAG: @_ZZ8functionvE3paa = internal constant %struct.packed_and_aligned <{ i16 1, i8 2, float 3.000000e+00, i8 0 }>173// LLVM-DAG: @_ZZ8functionvE5array = internal constant [2 x %struct.Point] [%struct.Point { i32 123, i32 456, [3 x i8] c"\0B\16!", i32 789 }, %struct.Point { i32 10, i32 20, [3 x i8] zeroinitializer, i32 40 }]174// LLVM-DAG: @_ZZ8functionvE9paa_array = internal constant [2 x %struct.packed_and_aligned] [%struct.packed_and_aligned <{ i16 1, i8 2, float 3.000000e+00, i8 0 }>, %struct.packed_and_aligned zeroinitializer]175// LLVM-DAG: @_ZZ8functionvE6ba_bf1 = internal constant { i8, i8, i8, i8 } { i8 -1, i8 -86, i8 52, i8 18 }176// LLVM-DAG: @_ZZ8functionvE6ba_bf2 = internal constant { i8, i8, [2 x i8] } { i8 -1, i8 127, [2 x i8] zeroinitializer }177// LLVM-DAG: @_ZZ8functionvE6ba_bf3 = internal constant %struct.single_byte_bitfield { i8 42 }178// LLVM-DAG: @_ZZ8functionvE5p_bf1 = internal constant { i8, i8, [2 x i8] } { i8 17, i8 3, [2 x i8] zeroinitializer }179// LLVM-DAG: @_ZZ8functionvE5p_bf2 = internal constant %struct.signed_partial_bitfields { i8 127, [3 x i8] zeroinitializer }180// LLVM-DAG: @_ZZ8functionvE5p_bf3 = internal constant %struct.mixed_partial_bitfields { i8 125 }181 182// LLVM-LABEL: define{{.*}} void @_Z8functionv183// LLVM: ret void184 185 186// OGCG-DAG: @_ZZ8functionvE12packed_array = internal constant [2 x %struct.packed] [%struct.packed <{ i8 123, i32 456 }>, %struct.packed <{ i8 123, i32 456 }>]187// OGCG-DAG: @_ZZ8functionvE12simple_array = internal constant [3 x %struct.simple] [%struct.simple { i32 0, i32 -1 }, %struct.simple { i32 1111, i32 2222 }, %struct.simple { i32 0, i32 -1 }]188// OGCG-DAG: @_ZZ8functionvE1e = internal constant %struct.empty zeroinitializer189// OGCG-DAG: @_ZZ8functionvE1s = internal constant %struct.simple { i32 0, i32 -1 }190// OGCG-DAG: @_ZZ8functionvE2p1 = internal constant %struct.Point { i32 10, i32 20, [3 x i8] c"cXM", i32 40 }191// OGCG-DAG: @_ZZ8functionvE2p2 = internal constant %struct.packed <{ i8 123, i32 456 }>192// OGCG-DAG: @_ZZ8functionvE3paa = internal constant %struct.packed_and_aligned <{ i16 1, i8 2, float 3.000000e+00, i8 undef }>193// OGCG-DAG: @_ZZ8functionvE5array = internal constant [2 x %struct.Point] [%struct.Point { i32 123, i32 456, [3 x i8] c"\0B\16!", i32 789 }, %struct.Point { i32 10, i32 20, [3 x i8] zeroinitializer, i32 40 }]194// OGCG-DAG: @_ZZ8functionvE9paa_array = internal constant [2 x %struct.packed_and_aligned] [%struct.packed_and_aligned <{ i16 1, i8 2, float 3.000000e+00, i8 undef }>, %struct.packed_and_aligned <{ i16 0, i8 0, float 0.000000e+00, i8 undef }>]195// OGCG-DAG: @_ZZ8functionvE6ba_bf1 = internal constant { i8, i8, i8, i8 } { i8 -1, i8 -86, i8 52, i8 18 }196// OGCG-DAG: @_ZZ8functionvE6ba_bf2 = internal constant { i8, i8, [2 x i8] } { i8 -1, i8 127, [2 x i8] undef }197// OGCG-DAG: @_ZZ8functionvE6ba_bf3 = internal constant %struct.single_byte_bitfield { i8 42 }198// OGCG-DAG: @_ZZ8functionvE5p_bf1 = internal constant { i8, i8, [2 x i8] } { i8 17, i8 3, [2 x i8] undef }199// OGCG-DAG: @_ZZ8functionvE5p_bf2 = internal constant %struct.signed_partial_bitfields { i8 127, [3 x i8] undef }200// OGCG-DAG: @_ZZ8functionvE5p_bf3 = internal constant %struct.mixed_partial_bitfields { i8 125 }201 202// OGCG-LABEL: define{{.*}} void @_Z8functionv203// OGCG: ret void204