@@ -802,6 +802,36 @@ class CIRUnaryOpLowering : public mlir::OpConversionPattern<cir::UnaryOp> {
802802public:
803803 using OpConversionPattern<cir::UnaryOp>::OpConversionPattern;
804804
805+ mlir::Operation *
806+ addImmediate (cir::UnaryOp op, mlir::Type type, mlir::Value input, int64_t n,
807+ mlir::ConversionPatternRewriter &rewriter) const {
808+ if (type.isFloat ()) {
809+ auto imm = mlir::arith::ConstantOp::create (rewriter, op.getLoc (),
810+ mlir::FloatAttr::get (type, n));
811+ return rewriter.replaceOpWithNewOp <mlir::arith::AddFOp>(op, type, input,
812+ imm);
813+ }
814+ auto imm = mlir::arith::ConstantOp::create (rewriter, op.getLoc (),
815+ mlir::IntegerAttr::get (type, n));
816+ return rewriter.replaceOpWithNewOp <mlir::arith::AddIOp>(op, type, input,
817+ imm);
818+ }
819+
820+ mlir::Operation *
821+ subByImmediate (cir::UnaryOp op, mlir::Type type, mlir::Value input, int64_t n,
822+ mlir::ConversionPatternRewriter &rewriter) const {
823+ if (type.isFloat ()) {
824+ auto imm = mlir::arith::ConstantOp::create (rewriter, op.getLoc (),
825+ mlir::FloatAttr::get (type, n));
826+ return rewriter.replaceOpWithNewOp <mlir::arith::SubFOp>(op, type, imm,
827+ input);
828+ }
829+ auto imm = mlir::arith::ConstantOp::create (rewriter, op.getLoc (),
830+ mlir::IntegerAttr::get (type, n));
831+ return rewriter.replaceOpWithNewOp <mlir::arith::SubIOp>(op, type, imm,
832+ input);
833+ }
834+
805835 mlir::LogicalResult
806836 matchAndRewrite (cir::UnaryOp op, OpAdaptor adaptor,
807837 mlir::ConversionPatternRewriter &rewriter) const override {
@@ -810,36 +840,28 @@ class CIRUnaryOpLowering : public mlir::OpConversionPattern<cir::UnaryOp> {
810840
811841 switch (op.getKind ()) {
812842 case cir::UnaryOpKind::Inc: {
813- auto One = mlir::arith::ConstantOp::create (
814- rewriter, op.getLoc (), mlir::IntegerAttr::get (type, 1 ));
815- rewriter.replaceOpWithNewOp <mlir::arith::AddIOp>(op, type, input, One);
843+ addImmediate (op, type, input, 1 , rewriter);
816844 break ;
817845 }
818846 case cir::UnaryOpKind::Dec: {
819- auto One = mlir::arith::ConstantOp::create (
820- rewriter, op.getLoc (), mlir::IntegerAttr::get (type, 1 ));
821- rewriter.replaceOpWithNewOp <mlir::arith::SubIOp>(op, type, input, One);
847+ addImmediate (op, type, input, -1 , rewriter);
822848 break ;
823849 }
824850 case cir::UnaryOpKind::Plus: {
825851 rewriter.replaceOp (op, op.getInput ());
826852 break ;
827853 }
828854 case cir::UnaryOpKind::Minus: {
829- auto Zero = mlir::arith::ConstantOp::create (
830- rewriter, op.getLoc (), mlir::IntegerAttr::get (type, 0 ));
831- rewriter.replaceOpWithNewOp <mlir::arith::SubIOp>(op, type, Zero, input);
855+ subByImmediate (op, type, input, 0 , rewriter);
832856 break ;
833857 }
834858 case cir::UnaryOpKind::Not: {
835- auto MinusOne = mlir::arith::ConstantOp::create (
859+ auto o = mlir::arith::ConstantOp::create (
836860 rewriter, op.getLoc (), mlir::IntegerAttr::get (type, -1 ));
837- rewriter.replaceOpWithNewOp <mlir::arith::XOrIOp>(op, type, MinusOne,
838- input);
861+ rewriter.replaceOpWithNewOp <mlir::arith::XOrIOp>(op, type, o, input);
839862 break ;
840863 }
841864 }
842-
843865 return mlir::LogicalResult::success ();
844866 }
845867};
0 commit comments