|
5 | 5 |
|
6 | 6 | int produce_int();
|
7 | 7 | void blackbox(const int &);
|
| 8 | +void consume(int); |
8 | 9 |
|
9 | 10 | void local_const_int() {
|
10 | 11 | const int x = produce_int();
|
@@ -85,3 +86,87 @@ int local_const_optimize() {
|
85 | 86 | // LLVM-NEXT: call void @_Z8blackboxRKi(ptr nonnull %[[#slot]])
|
86 | 87 | // LLVM-NEXT: ret i32 %[[#init]]
|
87 | 88 | // LLVM-NEXT: }
|
| 89 | + |
| 90 | +int local_scoped_const() { |
| 91 | + { |
| 92 | + const int x = produce_int(); |
| 93 | + blackbox(x); |
| 94 | + return x; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// CIR-LABEL: @_Z18local_scoped_constv() |
| 99 | +// CIR: cir.scope { |
| 100 | +// CIR-NEXT: %[[#x_slot:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init, const] |
| 101 | +// CIR-NEXT: %[[#init:]] = cir.call @_Z11produce_intv() : () -> !s32i |
| 102 | +// CIR-NEXT: cir.store %[[#init]], %[[#x_slot]] : !s32i, !cir.ptr<!s32i> tbaa([#tbaa]) |
| 103 | +// CIR-NEXT: cir.call @_Z8blackboxRKi(%[[#x_slot]]) : (!cir.ptr<!s32i>) -> () |
| 104 | +// CIR-NEXT: %[[#x_reload:]] = cir.load %[[#x_slot]] : !cir.ptr<!s32i>, !s32i tbaa([#tbaa]) |
| 105 | +// CIR-NEXT: cir.store %[[#x_reload]], %[[#ret_slot:]] : !s32i, !cir.ptr<!s32i> |
| 106 | +// CIR-NEXT: %[[#ret:]] = cir.load %[[#ret_slot]] : !cir.ptr<!s32i>, !s32i |
| 107 | +// CIR-NEXT: cir.return %[[#ret]] : !s32i |
| 108 | +// CIR-NEXT: } |
| 109 | +// CIR: } |
| 110 | + |
| 111 | +// LLVM-LABEL: @_Z18local_scoped_constv() |
| 112 | +// LLVM-NEXT: %[[#x_slot:]] = alloca i32, align 4 |
| 113 | +// LLVM-NEXT: %[[#init:]] = tail call i32 @_Z11produce_intv() |
| 114 | +// LLVM-NEXT: store i32 %[[#init]], ptr %[[#x_slot]], align 4, !invariant.group !{{.+}} |
| 115 | +// LLVM-NEXT: call void @_Z8blackboxRKi(ptr nonnull %[[#x_slot]]) |
| 116 | +// LLVM-NEXT: ret i32 %[[#init]] |
| 117 | +// LLVM-NEXT: } |
| 118 | + |
| 119 | +void local_const_in_loop() { |
| 120 | + for (int i = 0; i < 10; ++i) { |
| 121 | + const int x = produce_int(); |
| 122 | + blackbox(x); |
| 123 | + consume(x); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// CIR-LABEL: @_Z19local_const_in_loopv |
| 128 | +// CIR: cir.scope { |
| 129 | +// CIR: cir.for : cond { |
| 130 | +// CIR: } body { |
| 131 | +// CIR-NEXT: cir.scope { |
| 132 | +// CIR-NEXT: %[[#x_slot:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init, const] |
| 133 | +// CIR-NEXT: %[[#init:]] = cir.call @_Z11produce_intv() : () -> !s32i |
| 134 | +// CIR-NEXT: cir.store %[[#init]], %[[#x_slot]] : !s32i, !cir.ptr<!s32i> tbaa([#tbaa]) |
| 135 | +// CIR-NEXT: cir.call @_Z8blackboxRKi(%[[#x_slot]]) : (!cir.ptr<!s32i>) -> () |
| 136 | +// CIR-NEXT: %[[#x_reload:]] = cir.load %[[#x_slot]] : !cir.ptr<!s32i>, !s32i tbaa([#tbaa]) |
| 137 | +// CIR-NEXT: cir.call @_Z7consumei(%[[#x_reload]]) : (!s32i) -> () |
| 138 | +// CIR-NEXT: } |
| 139 | +// CIR-NEXT: cir.yield |
| 140 | +// CIR-NEXT: } step { |
| 141 | +// CIR: } |
| 142 | +// CIR-NEXT: } |
| 143 | +// CIR-NEXT: cir.return |
| 144 | +// CIR-NEXT: } |
| 145 | + |
| 146 | +// LLVM-LABEL: @_Z19local_const_in_loopv() |
| 147 | +// LLVM: %[[#x_ptr:]] = call ptr @llvm.launder.invariant.group.p0(ptr nonnull %1) |
| 148 | +// LLVM-NEXT: %[[#init:]] = call i32 @_Z11produce_intv() |
| 149 | +// LLVM-NEXT: store i32 %[[#init]], ptr %[[#x_ptr]], align 4, !invariant.group !{{.+}} |
| 150 | +// LLVM-NEXT: call void @_Z8blackboxRKi(ptr nonnull %[[#x_ptr]]) |
| 151 | +// LLVM-NEXT: call void @_Z7consumei(i32 %[[#init]]) |
| 152 | +// LLVM: } |
| 153 | + |
| 154 | +void local_const_in_while_condition() { |
| 155 | + while (const int x = produce_int()) { |
| 156 | + blackbox(x); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// LLVM-LABEL: @_Z30local_const_in_while_conditionv() |
| 161 | +// LLVM: %[[#x_slot:]] = alloca i32, align 4 |
| 162 | +// LLVM-NEXT: %[[#init:]] = tail call i32 @_Z11produce_intv() |
| 163 | +// LLVM-NEXT: store i32 %[[#init]], ptr %[[#x_slot]], align 4 |
| 164 | +// LLVM-NEXT: %[[loop_cond:.+]] = icmp eq i32 %[[#init]], 0 |
| 165 | +// LLVM-NEXT: br i1 %[[loop_cond]], label %{{.+}}, label %[[loop_body:.+]] |
| 166 | +// LLVM: [[loop_body]]: |
| 167 | +// LLVM-NEXT: call void @_Z8blackboxRKi(ptr nonnull %[[#x_slot]]) |
| 168 | +// LLVM-NEXT: %[[#next:]] = call i32 @_Z11produce_intv() |
| 169 | +// LLVM-NEXT: store i32 %[[#next]], ptr %[[#x_slot]], align 4 |
| 170 | +// LLVM-NEXT: %[[cond:.+]] = icmp eq i32 %[[#next]], 0 |
| 171 | +// LLVM-NEXT: br i1 %[[cond]], label %{{.+}}, label %[[loop_body]] |
| 172 | +// LLVM: } |
0 commit comments