[Fixed Point Arithmetic] Refactor fixed point casts

Summary:
- Added names for some emitted values (such as "tobool" for
  the result of a cast to boolean).
- Replaced explicit IRBuilder request for doing sext/zext/trunc
  by using CreateIntCast instead.
- Simplify code for emitting satuation into one if-statement
  for clamping to max, and one if-statement for clamping to min.

Reviewers: leonardchan, ebevhan

Reviewed By: leonardchan

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D53707

llvm-svn: 345398
This commit is contained in:
Bjorn Pettersson 2018-10-26 16:12:12 +00:00
parent 672ba25d8b
commit b25340236c
3 changed files with 177 additions and 194 deletions

View File

@ -1029,7 +1029,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
// We do not need to check the padding bit on unsigned types if unsigned
// padding is enabled because overflow into this bit is undefined
// behavior.
return Builder.CreateIsNotNull(Src);
return Builder.CreateIsNotNull(Src, "tobool");
}
llvm_unreachable(
@ -1247,79 +1247,62 @@ Value *ScalarExprEmitter::EmitFixedPointConversion(Value *Src, QualType SrcTy,
unsigned DstWidth = DstFPSema.getWidth();
unsigned SrcScale = SrcFPSema.getScale();
unsigned DstScale = DstFPSema.getScale();
bool IsSigned = SrcFPSema.isSigned();
bool SrcIsSigned = SrcFPSema.isSigned();
bool DstIsSigned = DstFPSema.isSigned();
llvm::Type *DstIntTy = Builder.getIntNTy(DstWidth);
Value *Result = Src;
unsigned ResultWidth = SrcWidth;
if (!DstFPSema.isSaturated()) {
// Downscale
if (DstScale < SrcScale) {
if (IsSigned)
Result = Builder.CreateAShr(Result, SrcScale - DstScale);
else
Result = Builder.CreateLShr(Result, SrcScale - DstScale);
}
// Downscale.
if (DstScale < SrcScale)
Result = SrcIsSigned ?
Builder.CreateAShr(Result, SrcScale - DstScale, "downscale") :
Builder.CreateLShr(Result, SrcScale - DstScale, "downscale");
// Resize
llvm::Type *DstIntTy = Builder.getIntNTy(DstWidth);
if (IsSigned)
Result = Builder.CreateSExtOrTrunc(Result, DstIntTy);
else
Result = Builder.CreateZExtOrTrunc(Result, DstIntTy);
// Resize.
Result = Builder.CreateIntCast(Result, DstIntTy, SrcIsSigned, "resize");
// Upscale
// Upscale.
if (DstScale > SrcScale)
Result = Builder.CreateShl(Result, DstScale - SrcScale);
Result = Builder.CreateShl(Result, DstScale - SrcScale, "upscale");
} else {
// Adjust the number of fractional bits.
if (DstScale > SrcScale) {
// Need to extend first before scaling up
ResultWidth = SrcWidth + DstScale - SrcScale;
llvm::Type *UpscaledTy = Builder.getIntNTy(ResultWidth);
if (IsSigned)
Result = Builder.CreateSExt(Result, UpscaledTy);
else
Result = Builder.CreateZExt(Result, UpscaledTy);
Result = Builder.CreateShl(Result, DstScale - SrcScale);
Result = Builder.CreateIntCast(Result, UpscaledTy, SrcIsSigned, "resize");
Result = Builder.CreateShl(Result, DstScale - SrcScale, "upscale");
} else if (DstScale < SrcScale) {
if (IsSigned)
Result = Builder.CreateAShr(Result, SrcScale - DstScale);
else
Result = Builder.CreateLShr(Result, SrcScale - DstScale);
Result = SrcIsSigned ?
Builder.CreateAShr(Result, SrcScale - DstScale, "downscale") :
Builder.CreateLShr(Result, SrcScale - DstScale, "downscale");
}
if (DstFPSema.getIntegralBits() < SrcFPSema.getIntegralBits()) {
auto Max = ConstantInt::get(
// Handle saturation.
bool LessIntBits = DstFPSema.getIntegralBits() < SrcFPSema.getIntegralBits();
if (LessIntBits) {
Value *Max = ConstantInt::get(
CGF.getLLVMContext(),
APFixedPoint::getMax(DstFPSema).getValue().extOrTrunc(ResultWidth));
Value *TooHigh = IsSigned ? Builder.CreateICmpSGT(Result, Max)
: Builder.CreateICmpUGT(Result, Max);
Result = Builder.CreateSelect(TooHigh, Max, Result);
if (IsSigned) {
// Cannot overflow min to dest type is src is unsigned since all fixed
// point types can cover the unsigned min of 0.
auto Min = ConstantInt::get(
CGF.getLLVMContext(),
APFixedPoint::getMin(DstFPSema).getValue().extOrTrunc(ResultWidth));
Value *TooLow = Builder.CreateICmpSLT(Result, Min);
Result = Builder.CreateSelect(TooLow, Min, Result);
}
} else if (IsSigned && !DstFPSema.isSigned()) {
llvm::Type *ResultTy = Builder.getIntNTy(ResultWidth);
Value *Zero = ConstantInt::getNullValue(ResultTy);
Value *LTZero = Builder.CreateICmpSLT(Result, Zero);
Result = Builder.CreateSelect(LTZero, Zero, Result);
Value *TooHigh = SrcIsSigned ? Builder.CreateICmpSGT(Result, Max)
: Builder.CreateICmpUGT(Result, Max);
Result = Builder.CreateSelect(TooHigh, Max, Result, "satmax");
}
// Cannot overflow min to dest type if src is unsigned since all fixed
// point types can cover the unsigned min of 0.
if (SrcIsSigned && (LessIntBits || !DstIsSigned)) {
Value *Min = ConstantInt::get(
CGF.getLLVMContext(),
APFixedPoint::getMin(DstFPSema).getValue().extOrTrunc(ResultWidth));
Value *TooLow = Builder.CreateICmpSLT(Result, Min);
Result = Builder.CreateSelect(TooLow, Min, Result, "satmin");
}
// Final resizing to dst width
llvm::Type *DstIntTy = Builder.getIntNTy(DstWidth);
if (IsSigned)
Result = Builder.CreateSExtOrTrunc(Result, DstIntTy);
else
Result = Builder.CreateZExtOrTrunc(Result, DstIntTy);
// Resize the integer part to get the final destination size.
Result = Builder.CreateIntCast(Result, DstIntTy, SrcIsSigned, "resize");
}
return Result;
}

View File

@ -4,104 +4,104 @@
void TestFixedPointCastSameType() {
_Accum a = 2.5k;
_Accum a2 = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a2, align 4
a2 = (_Accum)a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a2, align 4
}
void TestFixedPointCastDown() {
long _Accum la = 2.5lk;
_Accum a = la;
// DEFAULT: [[LACCUM:%[0-9]+]] = load i64, i64* %la, align 8
// DEFAULT-NEXT: [[ACCUM_AS_I64:%[0-9]+]] = ashr i64 [[LACCUM]], 16
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = trunc i64 [[ACCUM_AS_I64]] to i32
// DEFAULT: [[LACCUM:%[0-9a-z]+]] = load i64, i64* %la, align 8
// DEFAULT-NEXT: [[ACCUM_AS_I64:%[0-9a-z]+]] = ashr i64 [[LACCUM]], 16
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = trunc i64 [[ACCUM_AS_I64]] to i32
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
a = (_Accum)la;
// DEFAULT: [[LACCUM:%[0-9]+]] = load i64, i64* %la, align 8
// DEFAULT-NEXT: [[ACCUM_AS_I64:%[0-9]+]] = ashr i64 [[LACCUM]], 16
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = trunc i64 [[ACCUM_AS_I64]] to i32
// DEFAULT: [[LACCUM:%[0-9a-z]+]] = load i64, i64* %la, align 8
// DEFAULT-NEXT: [[ACCUM_AS_I64:%[0-9a-z]+]] = ashr i64 [[LACCUM]], 16
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = trunc i64 [[ACCUM_AS_I64]] to i32
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
short _Accum sa = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[SACCUM_AS_I32:%[0-9]+]] = ashr i32 [[ACCUM]], 8
// DEFAULT-NEXT: [[SACCUM:%[0-9]+]] = trunc i32 [[SACCUM_AS_I32]] to i16
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[SACCUM_AS_I32:%[0-9a-z]+]] = ashr i32 [[ACCUM]], 8
// DEFAULT-NEXT: [[SACCUM:%[0-9a-z]+]] = trunc i32 [[SACCUM_AS_I32]] to i16
// DEFAULT-NEXT: store i16 [[SACCUM]], i16* %sa, align 2
sa = (short _Accum)a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[SACCUM_AS_I32:%[0-9]+]] = ashr i32 [[ACCUM]], 8
// DEFAULT-NEXT: [[SACCUM:%[0-9]+]] = trunc i32 [[SACCUM_AS_I32]] to i16
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[SACCUM_AS_I32:%[0-9a-z]+]] = ashr i32 [[ACCUM]], 8
// DEFAULT-NEXT: [[SACCUM:%[0-9a-z]+]] = trunc i32 [[SACCUM_AS_I32]] to i16
// DEFAULT-NEXT: store i16 [[SACCUM]], i16* %sa, align 2
}
void TestFixedPointCastUp() {
short _Accum sa = 2.5hk;
_Accum a = sa;
// DEFAULT: [[SACCUM:%[0-9]+]] = load i16, i16* %sa, align 2
// DEFAULT-NEXT: [[SACCUM_BUFF:%[0-9]+]] = sext i16 [[SACCUM]] to i32
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = shl i32 [[SACCUM_BUFF]], 8
// DEFAULT: [[SACCUM:%[0-9a-z]+]] = load i16, i16* %sa, align 2
// DEFAULT-NEXT: [[SACCUM_BUFF:%[0-9a-z]+]] = sext i16 [[SACCUM]] to i32
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = shl i32 [[SACCUM_BUFF]], 8
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
long _Accum la = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[ACCUM_BUFF:%[0-9]+]] = sext i32 [[ACCUM]] to i64
// DEFAULT-NEXT: [[LACCUM:%[0-9]+]] = shl i64 [[ACCUM_BUFF]], 16
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[ACCUM_BUFF:%[0-9a-z]+]] = sext i32 [[ACCUM]] to i64
// DEFAULT-NEXT: [[LACCUM:%[0-9a-z]+]] = shl i64 [[ACCUM_BUFF]], 16
// DEFAULT-NEXT: store i64 [[LACCUM]], i64* %la, align 8
a = (_Accum)sa;
// DEFAULT: [[SACCUM:%[0-9]+]] = load i16, i16* %sa, align 2
// DEFAULT-NEXT: [[SACCUM_BUFF:%[0-9]+]] = sext i16 [[SACCUM]] to i32
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = shl i32 [[SACCUM_BUFF]], 8
// DEFAULT: [[SACCUM:%[0-9a-z]+]] = load i16, i16* %sa, align 2
// DEFAULT-NEXT: [[SACCUM_BUFF:%[0-9a-z]+]] = sext i16 [[SACCUM]] to i32
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = shl i32 [[SACCUM_BUFF]], 8
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
la = (long _Accum)a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[ACCUM_BUFF:%[0-9]+]] = sext i32 [[ACCUM]] to i64
// DEFAULT-NEXT: [[LACCUM:%[0-9]+]] = shl i64 [[ACCUM_BUFF]], 16
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[ACCUM_BUFF:%[0-9a-z]+]] = sext i32 [[ACCUM]] to i64
// DEFAULT-NEXT: [[LACCUM:%[0-9a-z]+]] = shl i64 [[ACCUM_BUFF]], 16
// DEFAULT-NEXT: store i64 [[LACCUM]], i64* %la, align 8
}
void TestFixedPointCastSignedness() {
_Accum a = 2.5k;
unsigned _Accum ua = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[UACCUM:%[0-9]+]] = shl i32 [[ACCUM]], 1
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[UACCUM:%[0-9a-z]+]] = shl i32 [[ACCUM]], 1
// DEFAULT-NEXT: store i32 [[UACCUM]], i32* %ua, align 4
// SAME: TestFixedPointCastSignedness
// SAME: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// SAME: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// SAME-NEXT: store i32 [[ACCUM]], i32* %ua, align 4
a = ua;
// DEFAULT: [[UACCUM:%[0-9]+]] = load i32, i32* %ua, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = lshr i32 [[UACCUM]], 1
// DEFAULT: [[UACCUM:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = lshr i32 [[UACCUM]], 1
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
// SAME: [[ACCUM:%[0-9]+]] = load i32, i32* %ua, align 4
// SAME: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// SAME-NEXT: store i32 [[ACCUM]], i32* %a, align 4
ua = (unsigned _Accum)a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[UACCUM:%[0-9]+]] = shl i32 [[ACCUM]], 1
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[UACCUM:%[0-9a-z]+]] = shl i32 [[ACCUM]], 1
// DEFAULT-NEXT: store i32 [[UACCUM]], i32* %ua, align 4
a = (_Accum)ua;
// DEFAULT: [[UACCUM:%[0-9]+]] = load i32, i32* %ua, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = lshr i32 [[UACCUM]], 1
// DEFAULT: [[UACCUM:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = lshr i32 [[UACCUM]], 1
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
_Accum a2;
unsigned long _Accum ula = a2;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a2, align 4
// DEFAULT-NEXT: [[ACCUM_EXT:%[0-9]+]] = sext i32 [[ACCUM]] to i64
// DEFAULT-NEXT: [[LACCUM:%[0-9]+]] = shl i64 [[ACCUM_EXT]], 17
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a2, align 4
// DEFAULT-NEXT: [[ACCUM_EXT:%[0-9a-z]+]] = sext i32 [[ACCUM]] to i64
// DEFAULT-NEXT: [[LACCUM:%[0-9a-z]+]] = shl i64 [[ACCUM_EXT]], 17
// DEFAULT-NEXT: store i64 [[LACCUM]], i64* %ula, align 8
// SAME: [[ACCUM:%[0-9]+]] = load i32, i32* %a2, align 4
// SAME-NEXT: [[ACCUM_EXT:%[0-9]+]] = sext i32 [[ACCUM]] to i64
// SAME-NEXT: [[LACCUM:%[0-9]+]] = shl i64 [[ACCUM_EXT]], 16
// SAME: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a2, align 4
// SAME-NEXT: [[ACCUM_EXT:%[0-9a-z]+]] = sext i32 [[ACCUM]] to i64
// SAME-NEXT: [[LACCUM:%[0-9a-z]+]] = shl i64 [[ACCUM_EXT]], 16
// SAME-NEXT: store i64 [[LACCUM]], i64* %ula, align 8
}
@ -119,113 +119,113 @@ void TestFixedPointCastSaturation() {
// Casting down between types
sat_sa = sat_a;
// DEFAULT: [[OLD_ACCUM:%[0-9]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = ashr i32 [[OLD_ACCUM]], 8
// DEFAULT-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i32 [[ACCUM]], 32767
// DEFAULT-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MAX]], i32 32767, i32 [[ACCUM]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i32 [[RESULT]], -32768
// DEFAULT-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i32 -32768, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i32 [[RESULT2]] to i16
// DEFAULT: [[OLD_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = ashr i32 [[OLD_ACCUM]], 8
// DEFAULT-NEXT: [[USE_MAX:%[0-9a-z]+]] = icmp sgt i32 [[ACCUM]], 32767
// DEFAULT-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MAX]], i32 32767, i32 [[ACCUM]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i32 [[RESULT]], -32768
// DEFAULT-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i32 -32768, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i32 [[RESULT2]] to i16
// DEFAULT-NEXT: store i16 [[RESULT_TRUNC]], i16* %sat_sa, align 2
// Accum to Fract, decreasing scale
sat_sf = sat_a;
// DEFAULT: [[OLD_ACCUM:%[0-9]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[FRACT:%[0-9]+]] = ashr i32 [[OLD_ACCUM]], 8
// DEFAULT-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i32 [[FRACT]], 127
// DEFAULT-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MAX]], i32 127, i32 [[FRACT]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i32 [[RESULT]], -128
// DEFAULT-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i32 -128, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i32 [[RESULT2]] to i8
// DEFAULT: [[OLD_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[FRACT:%[0-9a-z]+]] = ashr i32 [[OLD_ACCUM]], 8
// DEFAULT-NEXT: [[USE_MAX:%[0-9a-z]+]] = icmp sgt i32 [[FRACT]], 127
// DEFAULT-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MAX]], i32 127, i32 [[FRACT]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i32 [[RESULT]], -128
// DEFAULT-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i32 -128, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i32 [[RESULT2]] to i8
// DEFAULT-NEXT: store i8 [[RESULT_TRUNC]], i8* %sat_sf, align 1
// Accum to Fract, same scale
sat_f = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i32 [[ACCUM]], 32767
// DEFAULT-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MAX]], i32 32767, i32 [[ACCUM]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i32 [[RESULT]], -32768
// DEFAULT-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i32 -32768, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i32 [[RESULT2]] to i16
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[USE_MAX:%[0-9a-z]+]] = icmp sgt i32 [[ACCUM]], 32767
// DEFAULT-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MAX]], i32 32767, i32 [[ACCUM]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i32 [[RESULT]], -32768
// DEFAULT-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i32 -32768, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i32 [[RESULT2]] to i16
// DEFAULT-NEXT: store i16 [[RESULT_TRUNC]], i16* %sat_f, align 2
// Accum to Fract, increasing scale
sat_lf = sat_a;
// DEFAULT: [[OLD_ACCUM:%[0-9]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = sext i32 [[OLD_ACCUM]] to i48
// DEFAULT-NEXT: [[FRACT:%[0-9]+]] = shl i48 [[ACCUM]], 16
// DEFAULT-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i48 [[FRACT]], 2147483647
// DEFAULT-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MAX]], i48 2147483647, i48 [[FRACT]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i48 [[RESULT]], -2147483648
// DEFAULT-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i48 -2147483648, i48 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i48 [[RESULT2]] to i32
// DEFAULT: [[OLD_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = sext i32 [[OLD_ACCUM]] to i48
// DEFAULT-NEXT: [[FRACT:%[0-9a-z]+]] = shl i48 [[ACCUM]], 16
// DEFAULT-NEXT: [[USE_MAX:%[0-9a-z]+]] = icmp sgt i48 [[FRACT]], 2147483647
// DEFAULT-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MAX]], i48 2147483647, i48 [[FRACT]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i48 [[RESULT]], -2147483648
// DEFAULT-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i48 -2147483648, i48 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i48 [[RESULT2]] to i32
// DEFAULT-NEXT: store i32 [[RESULT_TRUNC]], i32* %sat_lf, align 4
// Signed to unsigned, decreasing scale
_Sat _Accum sat_a2;
sat_usa = sat_a2;
// DEFAULT: [[OLD_ACCUM:%[0-9]+]] = load i32, i32* %sat_a2, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = ashr i32 [[OLD_ACCUM]], 7
// DEFAULT-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i32 [[ACCUM]], 65535
// DEFAULT-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MAX]], i32 65535, i32 [[ACCUM]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i32 [[RESULT]], 0
// DEFAULT-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i32 0, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i32 [[RESULT2]] to i16
// DEFAULT: [[OLD_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a2, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = ashr i32 [[OLD_ACCUM]], 7
// DEFAULT-NEXT: [[USE_MAX:%[0-9a-z]+]] = icmp sgt i32 [[ACCUM]], 65535
// DEFAULT-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MAX]], i32 65535, i32 [[ACCUM]]
// DEFAULT-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i32 [[RESULT]], 0
// DEFAULT-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i32 0, i32 [[RESULT]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i32 [[RESULT2]] to i16
// DEFAULT-NEXT: store i16 [[RESULT_TRUNC]], i16* %sat_usa, align 2
// SAME: [[OLD_ACCUM:%[0-9]+]] = load i32, i32* %sat_a2, align 4
// SAME-NEXT: [[ACCUM:%[0-9]+]] = ashr i32 [[OLD_ACCUM]], 8
// SAME-NEXT: [[USE_MAX:%[0-9]+]] = icmp sgt i32 [[ACCUM]], 32767
// SAME-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MAX]], i32 32767, i32 [[ACCUM]]
// SAME-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i32 [[RESULT]], 0
// SAME-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i32 0, i32 [[RESULT]]
// SAME-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i32 [[RESULT2]] to i16
// SAME: [[OLD_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a2, align 4
// SAME-NEXT: [[ACCUM:%[0-9a-z]+]] = ashr i32 [[OLD_ACCUM]], 8
// SAME-NEXT: [[USE_MAX:%[0-9a-z]+]] = icmp sgt i32 [[ACCUM]], 32767
// SAME-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MAX]], i32 32767, i32 [[ACCUM]]
// SAME-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i32 [[RESULT]], 0
// SAME-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i32 0, i32 [[RESULT]]
// SAME-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i32 [[RESULT2]] to i16
// SAME-NEXT: store i16 [[RESULT_TRUNC]], i16* %sat_usa, align 2
// Signed to unsigned, increasing scale
sat_ua = sat_a;
// DEFAULT: [[OLD_ACCUM:%[0-9]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[ACCUM_EXT:%[0-9]+]] = sext i32 [[OLD_ACCUM]] to i33
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = shl i33 [[ACCUM_EXT]], 1
// DEFAULT-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i33 [[ACCUM]], 0
// DEFAULT-NEXT: [[RESULT2:%[0-9]+]] = select i1 [[USE_MIN]], i33 0, i33 [[ACCUM]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9]+]] = trunc i33 [[RESULT2]] to i32
// DEFAULT: [[OLD_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: [[ACCUM_EXT:%[0-9a-z]+]] = sext i32 [[OLD_ACCUM]] to i33
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = shl i33 [[ACCUM_EXT]], 1
// DEFAULT-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i33 [[ACCUM]], 0
// DEFAULT-NEXT: [[RESULT2:%[0-9a-z]+]] = select i1 [[USE_MIN]], i33 0, i33 [[ACCUM]]
// DEFAULT-NEXT: [[RESULT_TRUNC:%[0-9a-z]+]] = trunc i33 [[RESULT2]] to i32
// DEFAULT-NEXT: store i32 [[RESULT_TRUNC]], i32* %sat_ua, align 4
// SAME: [[ACCUM:%[0-9]+]] = load i32, i32* %sat_a, align 4
// SAME-NEXT: [[USE_MIN:%[0-9]+]] = icmp slt i32 [[ACCUM]], 0
// SAME-NEXT: [[RESULT:%[0-9]+]] = select i1 [[USE_MIN]], i32 0, i32 [[ACCUM]]
// SAME: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a, align 4
// SAME-NEXT: [[USE_MIN:%[0-9a-z]+]] = icmp slt i32 [[ACCUM]], 0
// SAME-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[USE_MIN]], i32 0, i32 [[ACCUM]]
// SAME-NEXT: store i32 [[RESULT]], i32* %sat_ua, align 4
// Nothing when saturating to the same type and size
sat_a = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %sat_a, align 4
// Nothing when assigning back
a = sat_a;
// DEFAULT: [[SAT_ACCUM:%[0-9]+]] = load i32, i32* %sat_a, align 4
// DEFAULT: [[SAT_ACCUM:%[0-9a-z]+]] = load i32, i32* %sat_a, align 4
// DEFAULT-NEXT: store i32 [[SAT_ACCUM]], i32* %a, align 4
// No overflow when casting from fract to signed accum
sat_a = sat_f;
// DEFAULT: [[FRACT:%[0-9]+]] = load i16, i16* %sat_f, align 2
// DEFAULT-NEXT: [[FRACT_EXT:%[0-9]+]] = sext i16 [[FRACT]] to i32
// DEFAULT: [[FRACT:%[0-9a-z]+]] = load i16, i16* %sat_f, align 2
// DEFAULT-NEXT: [[FRACT_EXT:%[0-9a-z]+]] = sext i16 [[FRACT]] to i32
// DEFAULT-NEXT: store i32 [[FRACT_EXT]], i32* %sat_a, align 4
// Only get overflow checking if signed fract to unsigned accum
sat_ua = sat_sf;
// DEFAULT: [[FRACT:%[0-9]+]] = load i8, i8* %sat_sf, align 1
// DEFAULT-NEXT: [[FRACT_EXT:%[0-9]+]] = sext i8 [[FRACT]] to i17
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = shl i17 [[FRACT_EXT]], 9
// DEFAULT-NEXT: [[IS_NEG:%[0-9]+]] = icmp slt i17 [[ACCUM]], 0
// DEFAULT-NEXT: [[RESULT:%[0-9]+]] = select i1 [[IS_NEG]], i17 0, i17 [[ACCUM]]
// DEFAULT-NEXT: [[RESULT_EXT:%[0-9]+]] = sext i17 [[RESULT]] to i32
// DEFAULT: [[FRACT:%[0-9a-z]+]] = load i8, i8* %sat_sf, align 1
// DEFAULT-NEXT: [[FRACT_EXT:%[0-9a-z]+]] = sext i8 [[FRACT]] to i17
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = shl i17 [[FRACT_EXT]], 9
// DEFAULT-NEXT: [[IS_NEG:%[0-9a-z]+]] = icmp slt i17 [[ACCUM]], 0
// DEFAULT-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[IS_NEG]], i17 0, i17 [[ACCUM]]
// DEFAULT-NEXT: [[RESULT_EXT:%[0-9a-z]+]] = sext i17 [[RESULT]] to i32
// DEFAULT-NEXT: store i32 [[RESULT_EXT]], i32* %sat_ua, align 4
// SAME: [[FRACT:%[0-9]+]] = load i8, i8* %sat_sf, align 1
// SAME-NEXT: [[FRACT_EXT:%[0-9]+]] = sext i8 [[FRACT]] to i16
// SAME-NEXT: [[ACCUM:%[0-9]+]] = shl i16 [[FRACT_EXT]], 8
// SAME-NEXT: [[IS_NEG:%[0-9]+]] = icmp slt i16 [[ACCUM]], 0
// SAME-NEXT: [[RESULT:%[0-9]+]] = select i1 [[IS_NEG]], i16 0, i16 [[ACCUM]]
// SAME-NEXT: [[RESULT_EXT:%[0-9]+]] = sext i16 [[RESULT]] to i32
// SAME: [[FRACT:%[0-9a-z]+]] = load i8, i8* %sat_sf, align 1
// SAME-NEXT: [[FRACT_EXT:%[0-9a-z]+]] = sext i8 [[FRACT]] to i16
// SAME-NEXT: [[ACCUM:%[0-9a-z]+]] = shl i16 [[FRACT_EXT]], 8
// SAME-NEXT: [[IS_NEG:%[0-9a-z]+]] = icmp slt i16 [[ACCUM]], 0
// SAME-NEXT: [[RESULT:%[0-9a-z]+]] = select i1 [[IS_NEG]], i16 0, i16 [[ACCUM]]
// SAME-NEXT: [[RESULT_EXT:%[0-9a-z]+]] = sext i16 [[RESULT]] to i32
// SAME-NEXT: store i32 [[RESULT_EXT]], i32* %sat_ua, align 4
}
@ -241,43 +241,43 @@ void TestFixedPointCastBetFractAccum() {
// To lower scale
sf = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[FRACT:%[0-9]+]] = ashr i32 [[ACCUM]], 8
// DEFAULT-NEXT: [[FRACT_TRUNC:%[0-9]+]] = trunc i32 [[FRACT]] to i8
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[FRACT:%[0-9a-z]+]] = ashr i32 [[ACCUM]], 8
// DEFAULT-NEXT: [[FRACT_TRUNC:%[0-9a-z]+]] = trunc i32 [[FRACT]] to i8
// DEFAULT-NEXT: store i8 [[FRACT_TRUNC]], i8* %sf, align 1
// To higher scale
a = sf;
// DEFAULT: [[FRACT:%[0-9]+]] = load i8, i8* %sf, align 1
// DEFAULT-NEXT: [[FRACT_EXT:%[0-9]+]] = sext i8 [[FRACT]] to i32
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = shl i32 [[FRACT_EXT]], 8
// DEFAULT: [[FRACT:%[0-9a-z]+]] = load i8, i8* %sf, align 1
// DEFAULT-NEXT: [[FRACT_EXT:%[0-9a-z]+]] = sext i8 [[FRACT]] to i32
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = shl i32 [[FRACT_EXT]], 8
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
// To same scale
f = a;
// DEFAULT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[FRACT:%[0-9]+]] = trunc i32 [[ACCUM]] to i16
// DEFAULT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// DEFAULT-NEXT: [[FRACT:%[0-9a-z]+]] = trunc i32 [[ACCUM]] to i16
// DEFAULT-NEXT: store i16 [[FRACT]], i16* %f, align 2
a = f;
// DEFAULT: [[FRACT:%[0-9]+]] = load i16, i16* %f, align 2
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = sext i16 [[FRACT]] to i32
// DEFAULT: [[FRACT:%[0-9a-z]+]] = load i16, i16* %f, align 2
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = sext i16 [[FRACT]] to i32
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %a, align 4
// To unsigned
ua = uf;
// DEFAULT: [[FRACT:%[0-9]+]] = load i16, i16* %uf, align 2
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = zext i16 [[FRACT]] to i32
// DEFAULT: [[FRACT:%[0-9a-z]+]] = load i16, i16* %uf, align 2
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = zext i16 [[FRACT]] to i32
// DEFAULT-NEXT: store i32 [[ACCUM]], i32* %ua, align 4
// SAME: [[FRACT:%[0-9]+]] = load i16, i16* %uf, align 2
// SAME-NEXT: [[ACCUM:%[0-9]+]] = zext i16 [[FRACT]] to i32
// SAME: [[FRACT:%[0-9a-z]+]] = load i16, i16* %uf, align 2
// SAME-NEXT: [[ACCUM:%[0-9a-z]+]] = zext i16 [[FRACT]] to i32
// SAME-NEXT: store i32 [[ACCUM]], i32* %ua, align 4
uf = ua;
// DEFAULT: [[FRACT:%[0-9]+]] = load i32, i32* %ua, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9]+]] = trunc i32 [[FRACT]] to i16
// DEFAULT: [[FRACT:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// DEFAULT-NEXT: [[ACCUM:%[0-9a-z]+]] = trunc i32 [[FRACT]] to i16
// DEFAULT-NEXT: store i16 [[ACCUM]], i16* %uf, align 2
// SAME: [[FRACT:%[0-9]+]] = load i32, i32* %ua, align 4
// SAME-NEXT: [[ACCUM:%[0-9]+]] = trunc i32 [[FRACT]] to i16
// SAME: [[FRACT:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// SAME-NEXT: [[ACCUM:%[0-9a-z]+]] = trunc i32 [[FRACT]] to i16
// SAME-NEXT: store i16 [[ACCUM]], i16* %uf, align 2
}

View File

@ -27,26 +27,26 @@ void func() {
b = (_Bool)0.5ur;
b = (_Bool)0.0ur;
// CHECK-NEXT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: %frombool = zext i1 [[NOTZERO]] to i8
// CHECK-NEXT: store i8 %frombool, i8* %b, align 1
// CHECK-NEXT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9a-z]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: [[FROMBOOL:%[0-9a-z]+]] = zext i1 [[NOTZERO]] to i8
// CHECK-NEXT: store i8 [[FROMBOOL]], i8* %b, align 1
b = a;
// CHECK-NEXT: [[ACCUM:%[0-9]+]] = load i32, i32* %ua, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: %frombool1 = zext i1 [[NOTZERO]] to i8
// CHECK-NEXT: store i8 %frombool1, i8* %b, align 1
// CHECK-NEXT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9a-z]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: [[FROMBOOL:%[0-9a-z]+]] = zext i1 [[NOTZERO]] to i8
// CHECK-NEXT: store i8 [[FROMBOOL]], i8* %b, align 1
b = ua;
// CHECK-NEXT: [[ACCUM:%[0-9]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %a, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9a-z]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: br i1 [[NOTZERO]], label %if.then, label %if.end
if (a) {
}
// CHECK: [[ACCUM:%[0-9]+]] = load i32, i32* %ua, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK: [[ACCUM:%[0-9a-z]+]] = load i32, i32* %ua, align 4
// CHECK-NEXT: [[NOTZERO:%[0-9a-z]+]] = icmp ne i32 [[ACCUM]], 0
// CHECK-NEXT: br i1 [[NOTZERO]], label %if.then{{[0-9]+}}, label %if.end{{[0-9]+}}
if (ua) {
}