From af5854993be8a873bd147d541e7b00da0b1e4183 Mon Sep 17 00:00:00 2001 From: Andy Kaylor Date: Fri, 14 Mar 2025 13:34:50 -0700 Subject: [PATCH] [CIR][NFC] Backport refactoring of unary plus/minus handling In the review for upstreaming unary operation support (https://github.com/llvm/llvm-project/pull/131369), it was suggested that the VisitPlus and VisitMinus functions should be combined. This is a backport of that refactoring. --- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 32 ++++++++-------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index d9dc455cf374..e9bd52f55d8f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -617,38 +617,26 @@ class ScalarExprEmitter : public StmtVisitor { QualType promotionTy = PromotionType.isNull() ? getPromotionType(E->getSubExpr()->getType()) : PromotionType; - auto result = VisitPlus(E, promotionTy); + auto result = emitUnaryPlusOrMinus(E, cir::UnaryOpKind::Plus, promotionTy); if (result && !promotionTy.isNull()) return emitUnPromotedValue(result, E->getType()); return result; } - mlir::Value VisitPlus(const UnaryOperator *E, - QualType PromotionType = QualType()) { - // This differs from gcc, though, most likely due to a bug in gcc. - TestAndClearIgnoreResultAssign(); - - mlir::Value operand; - if (!PromotionType.isNull()) - operand = CGF.emitPromotedScalarExpr(E->getSubExpr(), PromotionType); - else - operand = Visit(E->getSubExpr()); - - return emitUnaryOp(E, cir::UnaryOpKind::Plus, operand); - } - mlir::Value VisitUnaryMinus(const UnaryOperator *E, QualType PromotionType = QualType()) { QualType promotionTy = PromotionType.isNull() ? getPromotionType(E->getSubExpr()->getType()) : PromotionType; - auto result = VisitMinus(E, promotionTy); + auto result = emitUnaryPlusOrMinus(E, cir::UnaryOpKind::Minus, promotionTy); if (result && !promotionTy.isNull()) return emitUnPromotedValue(result, E->getType()); return result; } - mlir::Value VisitMinus(const UnaryOperator *E, QualType PromotionType) { + mlir::Value emitUnaryPlusOrMinus(const UnaryOperator *E, + cir::UnaryOpKind kind, + QualType PromotionType) { TestAndClearIgnoreResultAssign(); mlir::Value operand; @@ -657,10 +645,12 @@ class ScalarExprEmitter : public StmtVisitor { else operand = Visit(E->getSubExpr()); + bool nsw = + kind == cir::UnaryOpKind::Minus && E->getType()->isSignedIntegerType(); + // NOTE: LLVM codegen will lower this directly to either a FNeg // or a Sub instruction. In CIR this will be handled later in LowerToLLVM. - return emitUnaryOp(E, cir::UnaryOpKind::Minus, operand, - /*nsw=*/E->getType()->isSignedIntegerType()); + return emitUnaryOp(E, kind, operand, nsw); } mlir::Value VisitUnaryNot(const UnaryOperator *E) { @@ -2334,9 +2324,9 @@ mlir::Value ScalarExprEmitter::emitPromoted(const Expr *E, case UO_Real: llvm_unreachable("NYI"); case UO_Minus: - return VisitMinus(UO, PromotionType); + return emitUnaryPlusOrMinus(UO, cir::UnaryOpKind::Minus, PromotionType); case UO_Plus: - return VisitPlus(UO, PromotionType); + return emitUnaryPlusOrMinus(UO, cir::UnaryOpKind::Plus, PromotionType); default: break; }