#ifndef LLVM_IR_FIXEDPOINTBUILDER_H
#define LLVM_IR_FIXEDPOINTBUILDER_H
#include "llvm/ADT/APFixedPoint.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
namespace llvm {
template <class IRBuilderTy> class FixedPointBuilder {
IRBuilderTy &B;
Value *Convert(Value *Src, const FixedPointSemantics &SrcSema,
const FixedPointSemantics &DstSema, bool DstIsInteger) {
unsigned SrcWidth = SrcSema.getWidth();
unsigned DstWidth = DstSema.getWidth();
unsigned SrcScale = SrcSema.getScale();
unsigned DstScale = DstSema.getScale();
bool SrcIsSigned = SrcSema.isSigned();
bool DstIsSigned = DstSema.isSigned();
Type *DstIntTy = B.getIntNTy(DstWidth);
Value *Result = Src;
unsigned ResultWidth = SrcWidth;
if (DstScale < SrcScale) {
if (DstIsInteger && SrcIsSigned) {
Value *Zero = Constant::getNullValue(Result->getType());
Value *IsNegative = B.CreateICmpSLT(Result, Zero);
Value *LowBits = ConstantInt::get(
B.getContext(), APInt::getLowBitsSet(ResultWidth, SrcScale));
Value *Rounded = B.CreateAdd(Result, LowBits);
Result = B.CreateSelect(IsNegative, Rounded, Result);
}
Result = SrcIsSigned
? B.CreateAShr(Result, SrcScale - DstScale, "downscale")
: B.CreateLShr(Result, SrcScale - DstScale, "downscale");
}
if (!DstSema.isSaturated()) {
Result = B.CreateIntCast(Result, DstIntTy, SrcIsSigned, "resize");
if (DstScale > SrcScale)
Result = B.CreateShl(Result, DstScale - SrcScale, "upscale");
} else {
if (DstScale > SrcScale) {
ResultWidth = std::max(SrcWidth + DstScale - SrcScale, DstWidth);
Type *UpscaledTy = B.getIntNTy(ResultWidth);
Result = B.CreateIntCast(Result, UpscaledTy, SrcIsSigned, "resize");
Result = B.CreateShl(Result, DstScale - SrcScale, "upscale");
}
bool LessIntBits = DstSema.getIntegralBits() < SrcSema.getIntegralBits();
if (LessIntBits) {
Value *Max = ConstantInt::get(
B.getContext(),
APFixedPoint::getMax(DstSema).getValue().extOrTrunc(ResultWidth));
Value *TooHigh = SrcIsSigned ? B.CreateICmpSGT(Result, Max)
: B.CreateICmpUGT(Result, Max);
Result = B.CreateSelect(TooHigh, Max, Result, "satmax");
}
if (SrcIsSigned && (LessIntBits || !DstIsSigned)) {
Value *Min = ConstantInt::get(
B.getContext(),
APFixedPoint::getMin(DstSema).getValue().extOrTrunc(ResultWidth));
Value *TooLow = B.CreateICmpSLT(Result, Min);
Result = B.CreateSelect(TooLow, Min, Result, "satmin");
}
if (ResultWidth != DstWidth)
Result = B.CreateIntCast(Result, DstIntTy, SrcIsSigned, "resize");
}
return Result;
}
FixedPointSemantics
getCommonBinopSemantic(const FixedPointSemantics &LHSSema,
const FixedPointSemantics &RHSSema) {
auto C = LHSSema.getCommonSemantics(RHSSema);
bool BothPadded =
LHSSema.hasUnsignedPadding() && RHSSema.hasUnsignedPadding();
return FixedPointSemantics(
C.getWidth() + (unsigned)(BothPadded && C.isSaturated()), C.getScale(),
C.isSigned(), C.isSaturated(), BothPadded);
}
Type *getAccommodatingFloatType(Type *Ty, const FixedPointSemantics &Sema) {
const fltSemantics *FloatSema = &Ty->getFltSemantics();
while (!Sema.fitsInFloatSemantics(*FloatSema))
FloatSema = APFixedPoint::promoteFloatSemantics(FloatSema);
return Type::getFloatingPointTy(Ty->getContext(), *FloatSema);
}
public:
FixedPointBuilder(IRBuilderTy &Builder) : B(Builder) {}
Value *CreateFixedToFixed(Value *Src, const FixedPointSemantics &SrcSema,
const FixedPointSemantics &DstSema) {
return Convert(Src, SrcSema, DstSema, false);
}
Value *CreateFixedToInteger(Value *Src, const FixedPointSemantics &SrcSema,
unsigned DstWidth, bool DstIsSigned) {
return Convert(
Src, SrcSema,
FixedPointSemantics::GetIntegerSemantics(DstWidth, DstIsSigned), true);
}
Value *CreateIntegerToFixed(Value *Src, unsigned SrcIsSigned,
const FixedPointSemantics &DstSema) {
return Convert(Src,
FixedPointSemantics::GetIntegerSemantics(
Src->getType()->getScalarSizeInBits(), SrcIsSigned),
DstSema, false);
}
Value *CreateFixedToFloating(Value *Src, const FixedPointSemantics &SrcSema,
Type *DstTy) {
Value *Result;
Type *OpTy = getAccommodatingFloatType(DstTy, SrcSema);
Result = SrcSema.isSigned() ? B.CreateSIToFP(Src, OpTy)
: B.CreateUIToFP(Src, OpTy);
Result = B.CreateFMul(Result,
ConstantFP::get(OpTy, std::pow(2, -(int)SrcSema.getScale())));
if (OpTy != DstTy)
Result = B.CreateFPTrunc(Result, DstTy);
return Result;
}
Value *CreateFloatingToFixed(Value *Src, const FixedPointSemantics &DstSema) {
bool UseSigned = DstSema.isSigned() || DstSema.hasUnsignedPadding();
Value *Result = Src;
Type *OpTy = getAccommodatingFloatType(Src->getType(), DstSema);
if (OpTy != Src->getType())
Result = B.CreateFPExt(Result, OpTy);
Result = B.CreateFMul(Result,
ConstantFP::get(OpTy, std::pow(2, DstSema.getScale())));
Type *ResultTy = B.getIntNTy(DstSema.getWidth());
if (DstSema.isSaturated()) {
Intrinsic::ID IID =
UseSigned ? Intrinsic::fptosi_sat : Intrinsic::fptoui_sat;
Result = B.CreateIntrinsic(IID, {ResultTy, OpTy}, {Result});
} else {
Result = UseSigned ? B.CreateFPToSI(Result, ResultTy)
: B.CreateFPToUI(Result, ResultTy);
}
if (DstSema.isSaturated() && DstSema.hasUnsignedPadding()) {
Constant *Zero = Constant::getNullValue(Result->getType());
Result =
B.CreateSelect(B.CreateICmpSLT(Result, Zero), Zero, Result, "satmin");
}
return Result;
}
Value *CreateAdd(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
Value *Result;
if (CommonSema.isSaturated()) {
Intrinsic::ID IID = UseSigned ? Intrinsic::sadd_sat : Intrinsic::uadd_sat;
Result = B.CreateBinaryIntrinsic(IID, WideLHS, WideRHS);
} else {
Result = B.CreateAdd(WideLHS, WideRHS);
}
return CreateFixedToFixed(Result, CommonSema,
LHSSema.getCommonSemantics(RHSSema));
}
Value *CreateSub(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
Value *Result;
if (CommonSema.isSaturated()) {
Intrinsic::ID IID = UseSigned ? Intrinsic::ssub_sat : Intrinsic::usub_sat;
Result = B.CreateBinaryIntrinsic(IID, WideLHS, WideRHS);
} else {
Result = B.CreateSub(WideLHS, WideRHS);
}
if (CommonSema.isSaturated() && CommonSema.hasUnsignedPadding()) {
Constant *Zero = Constant::getNullValue(Result->getType());
Result =
B.CreateSelect(B.CreateICmpSLT(Result, Zero), Zero, Result, "satmin");
}
return CreateFixedToFixed(Result, CommonSema,
LHSSema.getCommonSemantics(RHSSema));
}
Value *CreateMul(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
Intrinsic::ID IID;
if (CommonSema.isSaturated()) {
IID = UseSigned ? Intrinsic::smul_fix_sat : Intrinsic::umul_fix_sat;
} else {
IID = UseSigned ? Intrinsic::smul_fix : Intrinsic::umul_fix;
}
Value *Result = B.CreateIntrinsic(
IID, {WideLHS->getType()},
{WideLHS, WideRHS, B.getInt32(CommonSema.getScale())});
return CreateFixedToFixed(Result, CommonSema,
LHSSema.getCommonSemantics(RHSSema));
}
Value *CreateDiv(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
Intrinsic::ID IID;
if (CommonSema.isSaturated()) {
IID = UseSigned ? Intrinsic::sdiv_fix_sat : Intrinsic::udiv_fix_sat;
} else {
IID = UseSigned ? Intrinsic::sdiv_fix : Intrinsic::udiv_fix;
}
Value *Result = B.CreateIntrinsic(
IID, {WideLHS->getType()},
{WideLHS, WideRHS, B.getInt32(CommonSema.getScale())});
return CreateFixedToFixed(Result, CommonSema,
LHSSema.getCommonSemantics(RHSSema));
}
Value *CreateShl(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS) {
bool UseSigned = LHSSema.isSigned() || LHSSema.hasUnsignedPadding();
RHS = B.CreateIntCast(RHS, LHS->getType(), false);
Value *Result;
if (LHSSema.isSaturated()) {
Intrinsic::ID IID = UseSigned ? Intrinsic::sshl_sat : Intrinsic::ushl_sat;
Result = B.CreateBinaryIntrinsic(IID, LHS, RHS);
} else {
Result = B.CreateShl(LHS, RHS);
}
return Result;
}
Value *CreateShr(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS) {
RHS = B.CreateIntCast(RHS, LHS->getType(), false);
return LHSSema.isSigned() ? B.CreateAShr(LHS, RHS) : B.CreateLShr(LHS, RHS);
}
Value *CreateEQ(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
return B.CreateICmpEQ(WideLHS, WideRHS);
}
Value *CreateNE(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
return B.CreateICmpNE(WideLHS, WideRHS);
}
Value *CreateLT(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
return CommonSema.isSigned() ? B.CreateICmpSLT(WideLHS, WideRHS)
: B.CreateICmpULT(WideLHS, WideRHS);
}
Value *CreateLE(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
return CommonSema.isSigned() ? B.CreateICmpSLE(WideLHS, WideRHS)
: B.CreateICmpULE(WideLHS, WideRHS);
}
Value *CreateGT(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
return CommonSema.isSigned() ? B.CreateICmpSGT(WideLHS, WideRHS)
: B.CreateICmpUGT(WideLHS, WideRHS);
}
Value *CreateGE(Value *LHS, const FixedPointSemantics &LHSSema,
Value *RHS, const FixedPointSemantics &RHSSema) {
auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
Value *WideLHS = CreateFixedToFixed(LHS, LHSSema, CommonSema);
Value *WideRHS = CreateFixedToFixed(RHS, RHSSema, CommonSema);
return CommonSema.isSigned() ? B.CreateICmpSGE(WideLHS, WideRHS)
: B.CreateICmpUGE(WideLHS, WideRHS);
}
};
}
#endif