14#ifndef LLVM_IR_FIXEDPOINTBUILDER_H
15#define LLVM_IR_FIXEDPOINTBUILDER_H
37 unsigned SrcWidth = SrcSema.
getWidth();
38 unsigned DstWidth = DstSema.
getWidth();
39 unsigned SrcScale = SrcSema.
getScale();
40 unsigned DstScale = DstSema.
getScale();
41 bool SrcIsSigned = SrcSema.
isSigned();
42 bool DstIsSigned = DstSema.
isSigned();
47 unsigned ResultWidth = SrcWidth;
50 if (DstScale < SrcScale) {
54 if (DstIsInteger && SrcIsSigned) {
56 Value *IsNegative = B.CreateICmpSLT(Result,
Zero);
57 Value *LowBits = ConstantInt::get(
59 Value *Rounded = B.CreateAdd(Result, LowBits);
60 Result = B.CreateSelect(IsNegative, Rounded, Result);
63 unsigned Shift = SrcScale - DstScale;
64 assert(Shift <= ResultWidth &&
"Shift cannot exceed result width");
65 if (Shift < ResultWidth) {
66 Result = SrcIsSigned ? B.CreateAShr(Result, Shift,
"downscale")
67 : B.CreateLShr(Result, Shift,
"downscale");
75 Result = B.CreateIntCast(Result, DstIntTy, SrcIsSigned,
"resize");
78 if (DstScale > SrcScale)
79 Result = B.CreateShl(Result, DstScale - SrcScale,
"upscale");
82 if (DstScale > SrcScale) {
84 ResultWidth = std::max(SrcWidth + DstScale - SrcScale, DstWidth);
86 Result = B.CreateIntCast(Result, UpscaledTy, SrcIsSigned,
"resize");
87 Result = B.CreateShl(Result, DstScale - SrcScale,
"upscale");
93 Value *Max = ConstantInt::get(
96 Value *TooHigh = SrcIsSigned ? B.CreateICmpSGT(Result, Max)
97 : B.CreateICmpUGT(Result, Max);
98 Result = B.CreateSelect(TooHigh, Max, Result,
"satmax");
102 if (SrcIsSigned && (LessIntBits || !DstIsSigned)) {
103 Value *Min = ConstantInt::get(
106 Value *TooLow = B.CreateICmpSLT(Result, Min);
107 Result = B.CreateSelect(TooLow, Min, Result,
"satmin");
111 if (ResultWidth != DstWidth)
112 Result = B.CreateIntCast(Result, DstIntTy, SrcIsSigned,
"resize");
126 C.getWidth() + (
unsigned)(BothPadded &&
C.isSaturated()),
C.getScale(),
127 C.isSigned(),
C.isSaturated(), BothPadded);
150 return Convert(Src, SrcSema, DstSema,
false);
160 unsigned DstWidth,
bool DstIsSigned) {
175 Src->getType()->getScalarSizeInBits(), SrcIsSigned),
182 Type *
OpTy = getAccommodatingFloatType(DstTy, SrcSema);
186 : B.CreateUIToFP(Src,
OpTy);
189 Result = B.CreateFMul(Result,
190 ConstantFP::get(
OpTy, std::pow(2, -(
int)SrcSema.
getScale())));
192 Result = B.CreateFPTrunc(Result, DstTy);
199 Type *
OpTy = getAccommodatingFloatType(Src->getType(), DstSema);
200 if (
OpTy != Src->getType())
201 Result = B.CreateFPExt(Result,
OpTy);
204 Result = B.CreateFMul(Result,
205 ConstantFP::get(
OpTy, std::pow(2, DstSema.
getScale())));
210 UseSigned ? Intrinsic::fptosi_sat : Intrinsic::fptoui_sat;
211 Result = B.CreateIntrinsic(IID, {ResultTy,
OpTy}, {Result});
213 Result = UseSigned ? B.CreateFPToSI(Result, ResultTy)
214 : B.CreateFPToUI(Result, ResultTy);
222 B.CreateSelect(B.CreateICmpSLT(Result,
Zero),
Zero, Result,
"satmin");
235 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
236 bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
242 if (CommonSema.isSaturated()) {
243 Intrinsic::ID IID = UseSigned ? Intrinsic::sadd_sat : Intrinsic::uadd_sat;
244 Result = B.CreateBinaryIntrinsic(IID, WideLHS, WideRHS);
246 Result = B.CreateAdd(WideLHS, WideRHS);
261 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
262 bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
268 if (CommonSema.isSaturated()) {
269 Intrinsic::ID IID = UseSigned ? Intrinsic::ssub_sat : Intrinsic::usub_sat;
270 Result = B.CreateBinaryIntrinsic(IID, WideLHS, WideRHS);
272 Result = B.CreateSub(WideLHS, WideRHS);
277 if (CommonSema.isSaturated() && CommonSema.hasUnsignedPadding()) {
280 B.CreateSelect(B.CreateICmpSLT(Result,
Zero),
Zero, Result,
"satmin");
295 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
296 bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
302 if (CommonSema.isSaturated()) {
303 IID = UseSigned ? Intrinsic::smul_fix_sat : Intrinsic::umul_fix_sat;
305 IID = UseSigned ? Intrinsic::smul_fix : Intrinsic::umul_fix;
307 Value *Result = B.CreateIntrinsic(
309 {WideLHS, WideRHS, B.getInt32(CommonSema.getScale())});
323 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
324 bool UseSigned = CommonSema.isSigned() || CommonSema.hasUnsignedPadding();
330 if (CommonSema.isSaturated()) {
331 IID = UseSigned ? Intrinsic::sdiv_fix_sat : Intrinsic::udiv_fix_sat;
333 IID = UseSigned ? Intrinsic::sdiv_fix : Intrinsic::udiv_fix;
335 Value *Result = B.CreateIntrinsic(
337 {WideLHS, WideRHS, B.getInt32(CommonSema.getScale())});
351 RHS = B.CreateIntCast(
RHS,
LHS->getType(),
false);
355 Intrinsic::ID IID = UseSigned ? Intrinsic::sshl_sat : Intrinsic::ushl_sat;
356 Result = B.CreateBinaryIntrinsic(IID,
LHS,
RHS);
358 Result = B.CreateShl(
LHS,
RHS);
370 RHS = B.CreateIntCast(
RHS,
LHS->getType(),
false);
382 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
387 return B.CreateICmpEQ(WideLHS, WideRHS);
397 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
402 return B.CreateICmpNE(WideLHS, WideRHS);
412 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
417 return CommonSema.isSigned() ? B.CreateICmpSLT(WideLHS, WideRHS)
418 : B.CreateICmpULT(WideLHS, WideRHS);
428 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
433 return CommonSema.isSigned() ? B.CreateICmpSLE(WideLHS, WideRHS)
434 : B.CreateICmpULE(WideLHS, WideRHS);
444 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
449 return CommonSema.isSigned() ? B.CreateICmpSGT(WideLHS, WideRHS)
450 : B.CreateICmpUGT(WideLHS, WideRHS);
460 auto CommonSema = getCommonBinopSemantic(LHSSema, RHSSema);
465 return CommonSema.isSigned() ? B.CreateICmpSGE(WideLHS, WideRHS)
466 : B.CreateICmpUGE(WideLHS, WideRHS);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Defines the fixed point number interface.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static LLVM_ABI APFixedPoint getMin(const FixedPointSemantics &Sema)
static LLVM_ABI const fltSemantics * promoteFloatSemantics(const fltSemantics *S)
Given a floating point semantic, return the next floating point semantic with a larger exponent and l...
static LLVM_ABI APFixedPoint getMax(const FixedPointSemantics &Sema)
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
This is an important base class in LLVM.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Value * CreateFixedToFloating(Value *Src, const FixedPointSemantics &SrcSema, Type *DstTy)
Value * CreateDiv(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Divide two fixed-point values and return the result in their common semantic.
Value * CreateFloatingToFixed(Value *Src, const FixedPointSemantics &DstSema)
Value * CreateGT(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Compare two fixed-point values as LHS > RHS.
Value * CreateFixedToInteger(Value *Src, const FixedPointSemantics &SrcSema, unsigned DstWidth, bool DstIsSigned)
Convert an integer value representing a fixed-point number to an integer with the given bit width and...
Value * CreateShl(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS)
Left shift a fixed-point value by an unsigned integer value.
Value * CreateNE(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Compare two fixed-point values for inequality.
Value * CreateLT(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Compare two fixed-point values as LHS < RHS.
FixedPointBuilder(IRBuilderTy &Builder)
Value * CreateEQ(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Compare two fixed-point values for equality.
Value * CreateShr(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS)
Right shift a fixed-point value by an unsigned integer value.
Value * CreateLE(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Compare two fixed-point values as LHS <= RHS.
Value * CreateAdd(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Add two fixed-point values and return the result in their common semantic.
Value * CreateGE(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Compare two fixed-point values as LHS >= RHS.
Value * CreateMul(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Multiply two fixed-point values and return the result in their common semantic.
Value * CreateIntegerToFixed(Value *Src, unsigned SrcIsSigned, const FixedPointSemantics &DstSema)
Convert an integer value with the given signedness to an integer value representing the given fixed-p...
Value * CreateSub(Value *LHS, const FixedPointSemantics &LHSSema, Value *RHS, const FixedPointSemantics &RHSSema)
Subtract two fixed-point values and return the result in their common semantic.
Value * CreateFixedToFixed(Value *Src, const FixedPointSemantics &SrcSema, const FixedPointSemantics &DstSema)
Convert an integer value representing a fixed-point number from one fixed-point semantic to another f...
The fixed point semantics work similarly to fltSemantics.
unsigned getWidth() const
bool hasUnsignedPadding() const
unsigned getScale() const
unsigned getIntegralBits() const
Return the number of integral bits represented by these semantics.
LLVM_ABI FixedPointSemantics getCommonSemantics(const FixedPointSemantics &Other) const
Return the FixedPointSemantics that allows for calculating the full precision semantic that can preci...
LLVM_ABI bool fitsInFloatSemantics(const fltSemantics &FloatSema) const
Returns true if this fixed-point semantic with its value bits interpreted as an integer can fit in th...
static FixedPointSemantics GetIntegerSemantics(unsigned Width, bool IsSigned)
Return the FixedPointSemantics for an integer type.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI Type * getFloatingPointTy(LLVMContext &C, const fltSemantics &S)
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
This is an optimization pass for GlobalISel generic memory operations.