Skip to content

Commit 8d992ef

Browse files
committed
Address review feedback
1 parent c22fa96 commit 8d992ef

File tree

6 files changed

+144
-367
lines changed

6 files changed

+144
-367
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -282,22 +282,6 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
282282
return createCast(loc, cir::CastKind::bitcast, src, newTy);
283283
}
284284

285-
// TODO(cir): the following function was introduced to keep in sync with LLVM
286-
// codegen. CIR does not have "zext" operations. It should eventually be
287-
// renamed or removed. For now, we just add whatever cast is required here.
288-
mlir::Value createZExtOrBitCast(mlir::Location loc, mlir::Value src,
289-
mlir::Type newTy) {
290-
mlir::Type srcTy = src.getType();
291-
292-
if (srcTy == newTy)
293-
return src;
294-
295-
if (mlir::isa<cir::BoolType>(srcTy) && mlir::isa<cir::IntType>(newTy))
296-
return createBoolToInt(src, newTy);
297-
298-
llvm_unreachable("unhandled extension cast");
299-
}
300-
301285
//===--------------------------------------------------------------------===//
302286
// Binary Operators
303287
//===--------------------------------------------------------------------===//
@@ -322,6 +306,24 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
322306
return createBinop(loc, lhs, cir::BinOpKind::Or, rhs);
323307
}
324308

309+
mlir::Value createSelect(mlir::Location loc, mlir::Value condition,
310+
mlir::Value trueValue, mlir::Value falseValue) {
311+
assert(trueValue.getType() == falseValue.getType() &&
312+
"trueValue and falseValue should have the same type");
313+
return create<cir::SelectOp>(loc, trueValue.getType(), condition, trueValue,
314+
falseValue);
315+
}
316+
317+
mlir::Value createLogicalAnd(mlir::Location loc, mlir::Value lhs,
318+
mlir::Value rhs) {
319+
return createSelect(loc, lhs, rhs, getBool(false, loc));
320+
}
321+
322+
mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs,
323+
mlir::Value rhs) {
324+
return createSelect(loc, lhs, getBool(true, loc), rhs);
325+
}
326+
325327
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
326328
OverflowBehavior ob = OverflowBehavior::None) {
327329
auto op =

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,21 @@ Address CIRGenFunction::emitArrayToPointerDecay(const Expr *e) {
11971197
return Address(ptr, addr.getAlignment());
11981198
}
11991199

1200+
/// Emit the operand of a glvalue conditional operator. This is either a glvalue
1201+
/// or a (possibly-parenthesized) throw-expression. If this is a throw, no
1202+
/// LValue is returned and the current block has been terminated.
1203+
static std::optional<LValue> emitLValueOrThrowExpression(CIRGenFunction &cgf,
1204+
const Expr *operand) {
1205+
if (isa<CXXThrowExpr>(operand->IgnoreParens())) {
1206+
assert(!cir::MissingFeatures::throwOp());
1207+
cgf.cgm.errorNYI(operand->getSourceRange(),
1208+
"throw expressions in conditional operator");
1209+
return std::nullopt;
1210+
}
1211+
1212+
return cgf.emitLValue(operand);
1213+
}
1214+
12001215
// Handle the case where the condition is a constant evaluatable simple integer,
12011216
// which means we don't have to separately handle the true/false blocks.
12021217
static std::optional<LValue> handleConditionalOperatorLValueSimpleCase(
@@ -1208,40 +1223,21 @@ static std::optional<LValue> handleConditionalOperatorLValueSimpleCase(
12081223
if (!condExprBool)
12091224
std::swap(live, dead);
12101225

1226+
// If there's a label in the "dead" branch we can't eliminate that as it
1227+
// could be a used jump target.
12111228
if (!cgf.containsLabel(dead)) {
12121229
// If the true case is live, we need to track its region.
12131230
if (condExprBool) {
12141231
assert(!cir::MissingFeatures::incrementProfileCounter());
12151232
}
12161233
// If a throw expression we emit it and return an undefined lvalue
12171234
// because it can't be used.
1218-
if (isa<CXXThrowExpr>(live->IgnoreParens())) {
1219-
assert(!cir::MissingFeatures::throwOp());
1220-
cgf.cgm.errorNYI(live->getSourceRange(),
1221-
"throw expressions in conditional operator");
1222-
return std::nullopt;
1223-
}
1224-
return cgf.emitLValue(live);
1235+
return emitLValueOrThrowExpression(cgf, live);
12251236
}
12261237
}
12271238
return std::nullopt;
12281239
}
12291240

1230-
/// Emit the operand of a glvalue conditional operator. This is either a glvalue
1231-
/// or a (possibly-parenthesized) throw-expression. If this is a throw, no
1232-
/// LValue is returned and the current block has been terminated.
1233-
static std::optional<LValue> emitLValueOrThrowExpression(CIRGenFunction &cgf,
1234-
const Expr *operand) {
1235-
if (isa<CXXThrowExpr>(operand->IgnoreParens())) {
1236-
assert(!cir::MissingFeatures::throwOp());
1237-
cgf.cgm.errorNYI(operand->getSourceRange(),
1238-
"throw expressions in conditional operator");
1239-
return std::nullopt;
1240-
}
1241-
1242-
return cgf.emitLValue(operand);
1243-
}
1244-
12451241
// Create and generate the 3 blocks for a conditional operator.
12461242
// Leaves the 'current block' in the continuation basic block.
12471243
template <typename FuncTy>
@@ -1252,7 +1248,6 @@ CIRGenFunction::emitConditionalBlocks(const AbstractConditionalOperator *e,
12521248
CIRGenFunction &cgf = *this;
12531249
ConditionalEvaluation eval(cgf);
12541250
mlir::Location loc = cgf.getLoc(e->getSourceRange());
1255-
CIRGenBuilderTy &builder = cgf.getBuilder();
12561251
Expr *trueExpr = e->getTrueExpr();
12571252
Expr *falseExpr = e->getFalseExpr();
12581253

@@ -1266,10 +1261,10 @@ CIRGenFunction::emitConditionalBlocks(const AbstractConditionalOperator *e,
12661261
cgf.curLexScope->setAsTernary();
12671262

12681263
assert(!cir::MissingFeatures::incrementProfileCounter());
1269-
eval.begin(cgf);
1264+
eval.beginEvaluation();
12701265
branchInfo = branchGenFunc(cgf, expr);
12711266
mlir::Value branch = branchInfo->getPointer();
1272-
eval.end(cgf);
1267+
eval.endEvaluation();
12731268

12741269
if (branch) {
12751270
yieldTy = branch.getType();
@@ -1315,47 +1310,6 @@ CIRGenFunction::emitConditionalBlocks(const AbstractConditionalOperator *e,
13151310
return info;
13161311
}
13171312

1318-
LValue CIRGenFunction::emitConditionalOperatorLValue(
1319-
const AbstractConditionalOperator *expr) {
1320-
if (!expr->isGLValue()) {
1321-
// ?: here should be an aggregate.
1322-
assert(hasAggregateEvaluationKind(expr->getType()) &&
1323-
"Unexpected conditional operator!");
1324-
return emitAggExprToLValue(expr);
1325-
}
1326-
1327-
OpaqueValueMapping binding(*this, expr);
1328-
if (std::optional<LValue> res =
1329-
handleConditionalOperatorLValueSimpleCase(*this, expr))
1330-
return *res;
1331-
1332-
ConditionalInfo info =
1333-
emitConditionalBlocks(expr, [](CIRGenFunction &cgf, const Expr *e) {
1334-
return emitLValueOrThrowExpression(cgf, e);
1335-
});
1336-
1337-
if ((info.lhs && !info.lhs->isSimple()) ||
1338-
(info.rhs && !info.rhs->isSimple())) {
1339-
cgm.errorNYI(expr->getSourceRange(), "unsupported conditional operator");
1340-
return {};
1341-
}
1342-
1343-
if (info.lhs && info.rhs) {
1344-
Address lhsAddr = info.lhs->getAddress();
1345-
Address rhsAddr = info.rhs->getAddress();
1346-
Address result(info.result, lhsAddr.getElementType(),
1347-
std::min(lhsAddr.getAlignment(), rhsAddr.getAlignment()));
1348-
AlignmentSource alignSource =
1349-
std::max(info.lhs->getBaseInfo().getAlignmentSource(),
1350-
info.rhs->getBaseInfo().getAlignmentSource());
1351-
assert(!cir::MissingFeatures::opTBAA());
1352-
return makeAddrLValue(result, expr->getType(), LValueBaseInfo(alignSource));
1353-
}
1354-
assert((info.lhs || info.rhs) &&
1355-
"both operands of glvalue conditional are throw-expressions?");
1356-
return info.lhs ? *info.lhs : *info.rhs;
1357-
}
1358-
13591313
/// Emit an `if` on a boolean condition, filling `then` and `else` into
13601314
/// appropriated regions.
13611315
mlir::LogicalResult CIRGenFunction::emitIfOnBoolExpr(const Expr *cond,

0 commit comments

Comments
 (0)