return isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1) &&
isKnownNeverInfinity(Inst->getOperand(2), TLI, Depth + 1);
}
- case Instruction::UIToFP:
- // If the input type fits into the floating type the result is finite.
- return ilogb(APFloat::getLargest(
- Inst->getType()->getScalarType()->getFltSemantics())) >=
- (int)Inst->getOperand(0)->getType()->getScalarSizeInBits();
+ case Instruction::SIToFP:
+ case Instruction::UIToFP: {
+ // Get width of largest magnitude integer (remove a bit if signed).
+ // This still works for a signed minimum value because the largest FP
+ // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
+ int IntSize = Inst->getOperand(0)->getType()->getScalarSizeInBits();
+ if (Inst->getOpcode() == Instruction::SIToFP)
+ --IntSize;
+
+ // If the exponent of the largest finite FP value can hold the largest
+ // integer, the result of the cast must be finite.
+ Type *FPTy = Inst->getType()->getScalarType();
+ return ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize;
+ }
default:
break;
}
ret i1 %r
}
-; largest signed i16 = 2^15 - 1 = 32767
+; largest magnitude signed i16 = 2^15 - 1 = 32767 --> -32768
; largest half (max exponent = 15 -> 2^15 * (1 + 1023/1024) = 65504
define i1 @isKnownNeverInfinity_sitofp(i16 %x) {
; CHECK-LABEL: @isKnownNeverInfinity_sitofp(
-; CHECK-NEXT: [[F:%.*]] = sitofp i16 [[X:%.*]] to half
-; CHECK-NEXT: [[R:%.*]] = fcmp une half [[F]], 0xH7C00
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 true
;
%f = sitofp i16 %x to half
%r = fcmp une half %f, 0xH7c00
define i1 @isKnownNeverNegativeInfinity_sitofp(i16 %x) {
; CHECK-LABEL: @isKnownNeverNegativeInfinity_sitofp(
-; CHECK-NEXT: [[F:%.*]] = sitofp i16 [[X:%.*]] to half
-; CHECK-NEXT: [[R:%.*]] = fcmp oeq half [[F]], 0xHFC00
-; CHECK-NEXT: ret i1 [[R]]
+; CHECK-NEXT: ret i1 false
;
%f = sitofp i16 %x to half
%r = fcmp oeq half %f, 0xHfc00