Skip to content

Commit 33980c5

Browse files
authored
[CIR][NFC] Backport refactoring of unary plus/minus handling (#1499)
In the review for upstreaming unary operation support (llvm/llvm-project#131369), it was suggested that the VisitPlus and VisitMinus functions should be combined. This is a backport of that refactoring.
1 parent ccb6039 commit 33980c5

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -617,38 +617,26 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
617617
QualType promotionTy = PromotionType.isNull()
618618
? getPromotionType(E->getSubExpr()->getType())
619619
: PromotionType;
620-
auto result = VisitPlus(E, promotionTy);
620+
auto result = emitUnaryPlusOrMinus(E, cir::UnaryOpKind::Plus, promotionTy);
621621
if (result && !promotionTy.isNull())
622622
return emitUnPromotedValue(result, E->getType());
623623
return result;
624624
}
625625

626-
mlir::Value VisitPlus(const UnaryOperator *E,
627-
QualType PromotionType = QualType()) {
628-
// This differs from gcc, though, most likely due to a bug in gcc.
629-
TestAndClearIgnoreResultAssign();
630-
631-
mlir::Value operand;
632-
if (!PromotionType.isNull())
633-
operand = CGF.emitPromotedScalarExpr(E->getSubExpr(), PromotionType);
634-
else
635-
operand = Visit(E->getSubExpr());
636-
637-
return emitUnaryOp(E, cir::UnaryOpKind::Plus, operand);
638-
}
639-
640626
mlir::Value VisitUnaryMinus(const UnaryOperator *E,
641627
QualType PromotionType = QualType()) {
642628
QualType promotionTy = PromotionType.isNull()
643629
? getPromotionType(E->getSubExpr()->getType())
644630
: PromotionType;
645-
auto result = VisitMinus(E, promotionTy);
631+
auto result = emitUnaryPlusOrMinus(E, cir::UnaryOpKind::Minus, promotionTy);
646632
if (result && !promotionTy.isNull())
647633
return emitUnPromotedValue(result, E->getType());
648634
return result;
649635
}
650636

651-
mlir::Value VisitMinus(const UnaryOperator *E, QualType PromotionType) {
637+
mlir::Value emitUnaryPlusOrMinus(const UnaryOperator *E,
638+
cir::UnaryOpKind kind,
639+
QualType PromotionType) {
652640
TestAndClearIgnoreResultAssign();
653641

654642
mlir::Value operand;
@@ -657,10 +645,12 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
657645
else
658646
operand = Visit(E->getSubExpr());
659647

648+
bool nsw =
649+
kind == cir::UnaryOpKind::Minus && E->getType()->isSignedIntegerType();
650+
660651
// NOTE: LLVM codegen will lower this directly to either a FNeg
661652
// or a Sub instruction. In CIR this will be handled later in LowerToLLVM.
662-
return emitUnaryOp(E, cir::UnaryOpKind::Minus, operand,
663-
/*nsw=*/E->getType()->isSignedIntegerType());
653+
return emitUnaryOp(E, kind, operand, nsw);
664654
}
665655

666656
mlir::Value VisitUnaryNot(const UnaryOperator *E) {
@@ -2334,9 +2324,9 @@ mlir::Value ScalarExprEmitter::emitPromoted(const Expr *E,
23342324
case UO_Real:
23352325
llvm_unreachable("NYI");
23362326
case UO_Minus:
2337-
return VisitMinus(UO, PromotionType);
2327+
return emitUnaryPlusOrMinus(UO, cir::UnaryOpKind::Minus, PromotionType);
23382328
case UO_Plus:
2339-
return VisitPlus(UO, PromotionType);
2329+
return emitUnaryPlusOrMinus(UO, cir::UnaryOpKind::Plus, PromotionType);
23402330
default:
23412331
break;
23422332
}

0 commit comments

Comments
 (0)