Debian Codegraph @

clang/test/CIR/CodeGen/forrange.cpp

raw
1 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3
4 struct Element {};
5 struct Container {};
6
7 Element *begin(Container &);
8 Element *end(Container &);
9
10 void for_range() {
11 Container c;
12 for (Element &e : c)
13 ;
14 }
15
16 // CIR: cir.func{{.*}} @_Z5beginR9Container(!cir.ptr<!rec_Container>) -> !cir.ptr<!rec_Element>
17 // CIR: cir.func{{.*}} @_Z3endR9Container(!cir.ptr<!rec_Container>) -> !cir.ptr<!rec_Element
18
19 // CIR: cir.func{{.*}} @_Z9for_rangev()
20 // CIR: %[[C_ADDR:.*]] = cir.alloca !rec_Container{{.*}} ["c"]
21 // CIR: cir.scope {
22 // CIR: %[[RANGE_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Container>{{.*}} ["__range1", init, const]
23 // CIR: %[[BEGIN_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["__begin1", init]
24 // CIR: %[[END_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["__end1", init]
25 // CIR: %[[E_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["e", init, const]
26 // CIR: cir.store{{.*}} %[[C_ADDR]], %[[RANGE_ADDR]]
27 // CIR: %[[C_REF:.*]] = cir.load{{.*}} %[[RANGE_ADDR]]
28 // CIR: %[[BEGIN:.*]] = cir.call @_Z5beginR9Container(%[[C_REF]])
29 // CIR: cir.store{{.*}} %[[BEGIN]], %[[BEGIN_ADDR]]
30 // CIR: %[[C_REF2:.*]] = cir.load{{.*}} %[[RANGE_ADDR]]
31 // CIR: %[[END:.*]] = cir.call @_Z3endR9Container(%[[C_REF2]])
32 // CIR: cir.store{{.*}} %[[END]], %[[END_ADDR]]
33 // CIR: cir.for : cond {
34 // CIR: %[[BEGIN:.*]] = cir.load{{.*}} %[[BEGIN_ADDR]]
35 // CIR: %[[END:.*]] = cir.load{{.*}} %[[END_ADDR]]
36 // CIR: %[[CMP:.*]] = cir.cmp(ne, %[[BEGIN]], %[[END]])
37 // CIR: cir.condition(%[[CMP]])
38 // CIR: } body {
39 // CIR: %[[E:.*]] = cir.load deref{{.*}} %[[BEGIN_ADDR]]
40 // CIR: cir.store{{.*}} %[[E]], %[[E_ADDR]]
41 // CIR: cir.yield
42 // CIR: } step {
43 // CIR: %[[BEGIN:.*]] = cir.load{{.*}} %[[BEGIN_ADDR]]
44 // CIR: %[[STEP:.*]] = cir.const #cir.int<1>
45 // CIR: %[[NEXT:.*]] = cir.ptr_stride %[[BEGIN]], %[[STEP]]
46 // CIR: cir.store{{.*}} %[[NEXT]], %[[BEGIN_ADDR]]
47 // CIR: cir.yield
48 // CIR: }
49 // CIR: }
50
51 struct C2 {
52 Element *begin();
53 Element *end();
54 };
55
56 void for_range2() {
57 C2 c;
58 for (Element &e : c)
59 ;
60 }
61
62 // CIR: cir.func{{.*}} @_Z10for_range2v()
63 // CIR: %[[C_ADDR:.*]] = cir.alloca !rec_C2{{.*}} ["c"]
64 // CIR: cir.scope {
65 // CIR: %[[RANGE_ADDR:.*]] = cir.alloca !cir.ptr<!rec_C2>{{.*}} ["__range1", init, const]
66 // CIR: %[[BEGIN_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["__begin1", init]
67 // CIR: %[[END_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["__end1", init]
68 // CIR: %[[E_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["e", init, const]
69 // CIR: cir.store{{.*}} %[[C_ADDR]], %[[RANGE_ADDR]]
70 // CIR: %[[C_REF:.*]] = cir.load{{.*}} %[[RANGE_ADDR]]
71 // CIR: %[[BEGIN:.*]] = cir.call @_ZN2C25beginEv(%[[C_REF]])
72 // CIR: cir.store{{.*}} %[[BEGIN]], %[[BEGIN_ADDR]]
73 // CIR: %[[C_REF2:.*]] = cir.load{{.*}} %[[RANGE_ADDR]]
74 // CIR: %[[END:.*]] = cir.call @_ZN2C23endEv(%[[C_REF2]])
75 // CIR: cir.store{{.*}} %[[END]], %[[END_ADDR]]
76 // CIR: cir.for : cond {
77 // CIR: %[[BEGIN:.*]] = cir.load{{.*}} %[[BEGIN_ADDR]]
78 // CIR: %[[END:.*]] = cir.load{{.*}} %[[END_ADDR]]
79 // CIR: %[[CMP:.*]] = cir.cmp(ne, %[[BEGIN]], %[[END]])
80 // CIR: cir.condition(%[[CMP]])
81 // CIR: } body {
82 // CIR: %[[E:.*]] = cir.load deref{{.*}} %[[BEGIN_ADDR]]
83 // CIR: cir.store{{.*}} %[[E]], %[[E_ADDR]]
84 // CIR: cir.yield
85 // CIR: } step {
86 // CIR: %[[BEGIN:.*]] = cir.load{{.*}} %[[BEGIN_ADDR]]
87 // CIR: %[[STEP:.*]] = cir.const #cir.int<1>
88 // CIR: %[[NEXT:.*]] = cir.ptr_stride %[[BEGIN]], %[[STEP]]
89 // CIR: cir.store{{.*}} %[[NEXT]], %[[BEGIN_ADDR]]
90 // CIR: cir.yield
91 // CIR: }
92 // CIR: }
93
94 // Iterator class definition
95 class Iterator {
96 public:
97 Element& operator*();
98 Iterator& operator++();
99 bool operator!=(const Iterator& other) const;
100 };
101
102 class C3 {
103 public:
104 Iterator begin();
105 Iterator end();
106 };
107
108 void for_range3() {
109 C3 c;
110 for (Element& e : c)
111 ;
112 }
113
114 // CIR: cir.func{{.*}} @_Z10for_range3v()
115 // CIR: %[[C_ADDR:.*]] = cir.alloca !rec_C3{{.*}} ["c"]
116 // CIR: cir.scope {
117 // CIR: %[[RANGE_ADDR:.*]] = cir.alloca !cir.ptr<!rec_C3>{{.*}} ["__range1", init, const]
118 // CIR: %[[BEGIN_ADDR:.*]] = cir.alloca !rec_Iterator, !cir.ptr<!rec_Iterator>{{.*}} ["__begin1", init]
119 // CIR: %[[END_ADDR:.*]] = cir.alloca !rec_Iterator, !cir.ptr<!rec_Iterator>{{.*}} ["__end1", init]
120 // CIR: %[[E_ADDR:.*]] = cir.alloca !cir.ptr<!rec_Element>{{.*}} ["e", init, const]
121 // CIR: cir.store{{.*}} %[[C_ADDR]], %[[RANGE_ADDR]]
122 // CIR: cir.for : cond {
123 // CIR: %[[ITER_NE:.*]] = cir.call @_ZNK8IteratorneERKS_(%[[BEGIN_ADDR]], %[[END_ADDR]])
124 // CIR: cir.condition(%[[ITER_NE]])
125 // CIR: } body {
126 // CIR: %[[E:.*]] = cir.call @_ZN8IteratordeEv(%[[BEGIN_ADDR]])
127 // CIR: cir.store{{.*}} %[[E]], %[[E_ADDR]]
128 // CIR: cir.yield
129 // CIR: } step {
130 // CIR: %[[ITER_NEXT:.*]] = cir.call @_ZN8IteratorppEv(%[[BEGIN_ADDR]])
131 // CIR: cir.yield
132 // CIR: }
133 // CIR: }