Skip to content

Commit 7c51e42

Browse files
authored
[CIR] Upstream scalar support for ParenExpr (#136332)
This change adds support for handling ParenExpr in scalar expressions. A few more places will need to be updated after structure assignment and complex type support is in place.
1 parent 7cc4472 commit 7c51e42

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
116116
return {};
117117
}
118118

119+
mlir::Value VisitParenExpr(ParenExpr *pe) { return Visit(pe->getSubExpr()); }
120+
119121
/// Emits the address of the l-value, then loads and returns the result.
120122
mlir::Value emitLoadOfLValue(const Expr *e) {
121123
LValue lv = cgf.emitLValue(e);

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ LValue CIRGenFunction::emitLValue(const Expr *e) {
515515
return emitUnaryOpLValue(cast<UnaryOperator>(e));
516516
case Expr::BinaryOperatorClass:
517517
return emitBinaryOperatorLValue(cast<BinaryOperator>(e));
518+
case Expr::ParenExprClass:
519+
return emitLValue(cast<ParenExpr>(e)->getSubExpr());
518520
case Expr::DeclRefExprClass:
519521
return emitDeclRefLValue(cast<DeclRefExpr>(e));
520522
}

clang/test/CIR/CodeGen/basic.c

+63
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,66 @@ int f6(void) {
169169
// OGCG-NEXT: entry:
170170
// OGCG-NEXT: %[[GV:.*]] = load i32, ptr @gv, align 4
171171
// OGCG-NEXT: ret i32 %[[GV]]
172+
173+
int f7(int a, int b, int c) {
174+
return a + (b + c);
175+
}
176+
177+
// CIR: cir.func @f7
178+
// CIR: %[[A_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
179+
// CIR: %[[B_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
180+
// CIR: %[[C_PTR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
181+
// CIR: %[[A:.*]] = cir.load %[[A_PTR]] : !cir.ptr<!s32i>, !s32i
182+
// CIR: %[[B:.*]] = cir.load %[[B_PTR]] : !cir.ptr<!s32i>, !s32i
183+
// CIR: %[[C:.*]] = cir.load %[[C_PTR]] : !cir.ptr<!s32i>, !s32i
184+
// CIR: %[[B_PLUS_C:.*]] = cir.binop(add, %[[B]], %[[C]]) nsw : !s32i
185+
// CIR: %[[RETVAL:.*]] = cir.binop(add, %[[A]], %[[B_PLUS_C]]) nsw : !s32i
186+
187+
// LLVM: define i32 @f7
188+
// LLVM: %[[A_PTR:.*]] = alloca i32, i64 1, align 4
189+
// LLVM: %[[B_PTR:.*]] = alloca i32, i64 1, align 4
190+
// LLVM: %[[C_PTR:.*]] = alloca i32, i64 1, align 4
191+
// LLVM: %[[A:.*]] = load i32, ptr %[[A_PTR]], align 4
192+
// LLVM: %[[B:.*]] = load i32, ptr %[[B_PTR]], align 4
193+
// LLVM: %[[C:.*]] = load i32, ptr %[[C_PTR]], align 4
194+
// LLVM: %[[B_PLUS_C:.*]] = add nsw i32 %[[B]], %[[C]]
195+
// LLVM: %[[RETVAL:.*]] = add nsw i32 %[[A]], %[[B_PLUS_C]]
196+
197+
// OGCG: define{{.*}} i32 @f7
198+
// OGCG: entry:
199+
// OGCG: %[[A_PTR:.*]] = alloca i32, align 4
200+
// OGCG: %[[B_PTR:.*]] = alloca i32, align 4
201+
// OGCG: %[[C_PTR:.*]] = alloca i32, align 4
202+
// OGCG: %[[A:.*]] = load i32, ptr %[[A_PTR]], align 4
203+
// OGCG: %[[B:.*]] = load i32, ptr %[[B_PTR]], align 4
204+
// OGCG: %[[C:.*]] = load i32, ptr %[[C_PTR]], align 4
205+
// OGCG: %[[B_PLUS_C:.*]] = add nsw i32 %[[B]], %[[C]]
206+
// OGCG: %[[RETVAL:.*]] = add nsw i32 %[[A]], %[[B_PLUS_C]]
207+
208+
int f8(int *p) {
209+
(*p) = 2;
210+
return (*p);
211+
}
212+
213+
// CIR: cir.func @f8
214+
// CIR: %[[P_PTR:.*]] = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["p", init]
215+
// CIR: %[[TWO:.*]] = cir.const #cir.int<2> : !s32i
216+
// CIR: %[[P:.*]] = cir.load deref %[[P_PTR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
217+
// CIR: cir.store %[[TWO]], %[[P]] : !s32i, !cir.ptr<!s32i>
218+
// CIR: %[[P2:.*]] = cir.load deref %[[P_PTR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
219+
// CIR: %[[STAR_P:.*]] = cir.load %[[P2]] : !cir.ptr<!s32i>, !s32i
220+
221+
// LLVM: define i32 @f8
222+
// LLVM: %[[P_PTR:.*]] = alloca ptr, i64 1, align 8
223+
// LLVM: %[[P:.*]] = load ptr, ptr %[[P_PTR]], align 8
224+
// LLVM: store i32 2, ptr %[[P]], align 4
225+
// LLVM: %[[P2:.*]] = load ptr, ptr %[[P_PTR]], align 8
226+
// LLVM: %[[STAR_P:.*]] = load i32, ptr %[[P2]], align 4
227+
228+
// OGCG: define{{.*}} i32 @f8
229+
// OGCG: entry:
230+
// OGCG: %[[P_PTR:.*]] = alloca ptr, align 8
231+
// OGCG: %[[P:.*]] = load ptr, ptr %[[P_PTR]], align 8
232+
// OGCG: store i32 2, ptr %[[P]], align 4
233+
// OGCG: %[[P2:.*]] = load ptr, ptr %[[P_PTR]], align 8
234+
// OGCG: %[[STAR_P:.*]] = load i32, ptr %[[P2]], align 4

0 commit comments

Comments
 (0)