SaurabhJha created this revision. SaurabhJha requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
As prelude to this patch https://reviews.llvm.org/D99037, we want to move the int-float conversion into a separate function that can be reused by matrix cast Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D100051 Files: clang/lib/CodeGen/CGExprScalar.cpp Index: clang/lib/CodeGen/CGExprScalar.cpp =================================================================== --- clang/lib/CodeGen/CGExprScalar.cpp +++ clang/lib/CodeGen/CGExprScalar.cpp @@ -348,6 +348,10 @@ EmitImplicitIntegerSignChangeChecks( SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)) {} }; + Value *EmitCastBetweenScalarTypes(Value *Src, QualType SrcType, + QualType DstType, llvm::Type *SrcTy, + llvm::Type *DstTy, + ScalarConversionOpts Opts); Value * EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc, @@ -1191,6 +1195,38 @@ {Src, Dst}); } +Value *ScalarExprEmitter::EmitCastBetweenScalarTypes( + Value *Src, QualType SrcType, QualType DstType, llvm::Type *SrcTy, + llvm::Type *DstTy, ScalarConversionOpts Opts) { + Value *Res = nullptr; + + if (isa<llvm::IntegerType>(SrcTy)) { + bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); + if (SrcType->isBooleanType() && Opts.TreatBooleanAsSigned) { + InputSigned = true; + } + if (isa<llvm::IntegerType>(DstTy)) + Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); + else if (InputSigned) + Res = Builder.CreateSIToFP(Src, DstTy, "conv"); + else + Res = Builder.CreateUIToFP(Src, DstTy, "conv"); + } else if (isa<llvm::IntegerType>(DstTy)) { + assert(SrcTy->isFloatingPointTy() && "Unknown real conversion"); + if (DstType->isSignedIntegerOrEnumerationType()) + Res = Builder.CreateFPToSI(Src, DstTy, "conv"); + else + Res = Builder.CreateFPToUI(Src, DstTy, "conv"); + } else { + if (DstTy->getTypeID() < SrcTy->getTypeID()) + Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); + else + Res = Builder.CreateFPExt(Src, DstTy, "conv"); + } + + return Res; +} + /// Emit a conversion from the specified type to the specified destination type, /// both of which are LLVM scalar types. Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, @@ -1384,31 +1420,7 @@ DstTy = CGF.FloatTy; } - if (isa<llvm::IntegerType>(SrcTy)) { - bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); - if (SrcType->isBooleanType() && Opts.TreatBooleanAsSigned) { - InputSigned = true; - } - if (isa<llvm::IntegerType>(DstTy)) - Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); - else if (InputSigned) - Res = Builder.CreateSIToFP(Src, DstTy, "conv"); - else - Res = Builder.CreateUIToFP(Src, DstTy, "conv"); - } else if (isa<llvm::IntegerType>(DstTy)) { - assert(SrcTy->isFloatingPointTy() && "Unknown real conversion"); - if (DstType->isSignedIntegerOrEnumerationType()) - Res = Builder.CreateFPToSI(Src, DstTy, "conv"); - else - Res = Builder.CreateFPToUI(Src, DstTy, "conv"); - } else { - assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() && - "Unknown real conversion"); - if (DstTy->getTypeID() < SrcTy->getTypeID()) - Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); - else - Res = Builder.CreateFPExt(Src, DstTy, "conv"); - } + Res = EmitCastBetweenScalarTypes(Src, SrcType, DstType, SrcTy, DstTy, Opts); if (DstTy != ResTy) { if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) {
Index: clang/lib/CodeGen/CGExprScalar.cpp =================================================================== --- clang/lib/CodeGen/CGExprScalar.cpp +++ clang/lib/CodeGen/CGExprScalar.cpp @@ -348,6 +348,10 @@ EmitImplicitIntegerSignChangeChecks( SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)) {} }; + Value *EmitCastBetweenScalarTypes(Value *Src, QualType SrcType, + QualType DstType, llvm::Type *SrcTy, + llvm::Type *DstTy, + ScalarConversionOpts Opts); Value * EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy, SourceLocation Loc, @@ -1191,6 +1195,38 @@ {Src, Dst}); } +Value *ScalarExprEmitter::EmitCastBetweenScalarTypes( + Value *Src, QualType SrcType, QualType DstType, llvm::Type *SrcTy, + llvm::Type *DstTy, ScalarConversionOpts Opts) { + Value *Res = nullptr; + + if (isa<llvm::IntegerType>(SrcTy)) { + bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); + if (SrcType->isBooleanType() && Opts.TreatBooleanAsSigned) { + InputSigned = true; + } + if (isa<llvm::IntegerType>(DstTy)) + Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); + else if (InputSigned) + Res = Builder.CreateSIToFP(Src, DstTy, "conv"); + else + Res = Builder.CreateUIToFP(Src, DstTy, "conv"); + } else if (isa<llvm::IntegerType>(DstTy)) { + assert(SrcTy->isFloatingPointTy() && "Unknown real conversion"); + if (DstType->isSignedIntegerOrEnumerationType()) + Res = Builder.CreateFPToSI(Src, DstTy, "conv"); + else + Res = Builder.CreateFPToUI(Src, DstTy, "conv"); + } else { + if (DstTy->getTypeID() < SrcTy->getTypeID()) + Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); + else + Res = Builder.CreateFPExt(Src, DstTy, "conv"); + } + + return Res; +} + /// Emit a conversion from the specified type to the specified destination type, /// both of which are LLVM scalar types. Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType, @@ -1384,31 +1420,7 @@ DstTy = CGF.FloatTy; } - if (isa<llvm::IntegerType>(SrcTy)) { - bool InputSigned = SrcType->isSignedIntegerOrEnumerationType(); - if (SrcType->isBooleanType() && Opts.TreatBooleanAsSigned) { - InputSigned = true; - } - if (isa<llvm::IntegerType>(DstTy)) - Res = Builder.CreateIntCast(Src, DstTy, InputSigned, "conv"); - else if (InputSigned) - Res = Builder.CreateSIToFP(Src, DstTy, "conv"); - else - Res = Builder.CreateUIToFP(Src, DstTy, "conv"); - } else if (isa<llvm::IntegerType>(DstTy)) { - assert(SrcTy->isFloatingPointTy() && "Unknown real conversion"); - if (DstType->isSignedIntegerOrEnumerationType()) - Res = Builder.CreateFPToSI(Src, DstTy, "conv"); - else - Res = Builder.CreateFPToUI(Src, DstTy, "conv"); - } else { - assert(SrcTy->isFloatingPointTy() && DstTy->isFloatingPointTy() && - "Unknown real conversion"); - if (DstTy->getTypeID() < SrcTy->getTypeID()) - Res = Builder.CreateFPTrunc(Src, DstTy, "conv"); - else - Res = Builder.CreateFPExt(Src, DstTy, "conv"); - } + Res = EmitCastBetweenScalarTypes(Src, SrcType, DstType, SrcTy, DstTy, Opts); if (DstTy != ResTy) { if (CGF.getContext().getTargetInfo().useFP16ConversionIntrinsics()) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits