File: in_place_construct.d

package info (click to toggle)
ldc 1%3A1.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 59,248 kB
  • sloc: cpp: 61,598; ansic: 14,545; sh: 1,014; makefile: 972; asm: 510; objc: 135; exp: 48; python: 12
file content (121 lines) | stat: -rw-r--r-- 4,511 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Tests in-place construction of variables.

// RUN: %ldc -c -output-ll -of=%t.ll %s && FileCheck %s < %t.ll

// 256 bits, returned via sret:
struct S
{
    long a, b, c, d;
}

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct13returnLiteralFZSQBm1S
S returnLiteral()
{
    // make sure the literal is emitted directly into the sret pointee
    // CHECK: %1 = getelementptr inbounds {{.*}}%in_place_construct.S* %.sret_arg, i32 0, i32 0
    // CHECK: store i64 1, i64* %1
    // CHECK: %2 = getelementptr inbounds {{.*}}%in_place_construct.S* %.sret_arg, i32 0, i32 1
    // CHECK: store i64 2, i64* %2
    // CHECK: %3 = getelementptr inbounds {{.*}}%in_place_construct.S* %.sret_arg, i32 0, i32 2
    // CHECK: store i64 3, i64* %3
    // CHECK: %4 = getelementptr inbounds {{.*}}%in_place_construct.S* %.sret_arg, i32 0, i32 3
    // CHECK: store i64 4, i64* %4
    return S(1, 2, 3, 4);
}

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct12returnRValueFZSQBl1S
S returnRValue()
{
    // make sure the sret pointer is forwarded
    // CHECK: call {{.*}}_D18in_place_construct13returnLiteralFZSQBm1S
    // CHECK-SAME: %in_place_construct.S* {{.*}} %.sret_arg
    return returnLiteral();
}

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct10returnNRVOFZSQBj1S
S returnNRVO()
{
    // make sure NRVO zero-initializes the sret pointee directly
    // CHECK: %1 = bitcast %in_place_construct.S* %.sret_arg to i8*
    // CHECK: call void @llvm.memset.{{.*}}(i8*{{[a-z0-9 ]*}} %1, i8 0,
    const S r;
    return r;
}

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct15withOutContractFZSQBo1S
S withOutContract()
out { assert(__result.a == 0); }
body
{
    // make sure NRVO zero-initializes the sret pointee directly
    // CHECK: %1 = bitcast %in_place_construct.S* %.sret_arg to i8*
    // CHECK: call void @llvm.memset.{{.*}}(i8*{{[a-z0-9 ]*}} %1, i8 0,
    const S r;
    return r;

    // make sure `__result` inside the out contract is just an alias to the sret pointee
    // CHECK: %2 = getelementptr inbounds {{.*}}%in_place_construct.S* %.sret_arg, i32 0, i32 0
    // CHECK: %3 = load {{.*}}i64* %2
    // CHECK: %4 = icmp eq i64 %3, 0
}

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct7structsFZv
void structs()
{
    // CHECK: %literal = alloca %in_place_construct.S
    // CHECK: %a = alloca %in_place_construct.S
    // CHECK: %b = alloca %in_place_construct.S
    // CHECK: %c = alloca %in_place_construct.S

    // make sure the literal is emitted directly into the lvalue
    // CHECK: %1 = getelementptr inbounds {{.*}}%in_place_construct.S* %literal, i32 0, i32 0
    // CHECK: store i64 5, i64* %1
    // CHECK: %2 = getelementptr inbounds {{.*}}%in_place_construct.S* %literal, i32 0, i32 1
    // CHECK: store i64 6, i64* %2
    // CHECK: %3 = getelementptr inbounds {{.*}}%in_place_construct.S* %literal, i32 0, i32 2
    // CHECK: store i64 7, i64* %3
    // CHECK: %4 = getelementptr inbounds {{.*}}%in_place_construct.S* %literal, i32 0, i32 3
    // CHECK: store i64 8, i64* %4
    const literal = S(5, 6, 7, 8);

    // make sure the variables are in-place constructed via sret
    // CHECK: call {{.*}}_D18in_place_construct13returnLiteralFZSQBm1S
    // CHECK-SAME: %in_place_construct.S* {{.*}} %a
    const a = returnLiteral();
    // CHECK: call {{.*}}_D18in_place_construct12returnRValueFZSQBl1S
    // CHECK-SAME: %in_place_construct.S* {{.*}} %b
    const b = returnRValue();
    // CHECK: call {{.*}}_D18in_place_construct10returnNRVOFZSQBj1S
    // CHECK-SAME: %in_place_construct.S* {{.*}} %c
    const c = returnNRVO();

    withOutContract();
}

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct12staticArraysFZv
void staticArrays()
{
    // CHECK: %sa = alloca [2 x i32]

    // make sure static array literals are in-place constructed too
    // CHECK: store [2 x i32] [i32 1, i32 2], [2 x i32]* %sa
    const(int[2]) sa = [ 1, 2 ];
}

struct Container { S s; }

// CHECK-LABEL: define{{.*}} @{{.*}}_D18in_place_construct19hierarchyOfLiteralsFZv
void hierarchyOfLiterals()
{
    // CHECK: %sa = alloca [1 x %in_place_construct.Container]
    // CHECK: store [1 x %in_place_construct.Container] [%in_place_construct.Container { %in_place_construct.S { i64 11, i64 12, i64 13, i64 14 } }], [1 x %in_place_construct.Container]* %sa
    Container[1] sa = [ Container(S(11, 12, 13, 14)) ];
}

// CHECK-LABEL: define{{.*}} @{{.*}}_Dmain
void main()
{
    structs();
    staticArrays();
    hierarchyOfLiterals();
}