Skip to content

Commit d43a6c7

Browse files
!fixup use llvm::absolutevalue, add more tests
1 parent 1261c5a commit d43a6c7

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

llvm/lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,16 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
231231
// Ensure we stay within the bounds of fp values that can be represented
232232
// as integers without gaps, i.e., 2^24 and 2^53 for IEEE-754 single and
233233
// double precision respectively (both on negative and positive side).
234-
const auto &SVFltSema = InitValueVal->getValueAPF().getSemantics();
235-
if (!APFloat::isIEEELikeFP(SVFltSema))
234+
const auto &InitValueFltSema = InitValueVal->getValueAPF().getSemantics();
235+
if (!APFloat::isIEEELikeFP(InitValueFltSema))
236236
return false;
237237

238-
uint64_t StartValPrecision = APFloat::semanticsPrecision(SVFltSema);
239-
if (StartValPrecision >= 64)
238+
uint64_t InitValuePrecision = APFloat::semanticsPrecision(InitValueFltSema);
239+
if (InitValuePrecision >= 64)
240240
return false;
241241

242-
uint64_t StartValIntegerLimit = 1LL << StartValPrecision;
243-
if (uint64_t(std::abs(InitValue)) > StartValIntegerLimit)
242+
uint64_t InitValueIntegerLimit = 1LL << InitValuePrecision;
243+
if (AbsoluteValue(InitValue) > InitValueIntegerLimit)
244244
return false;
245245

246246
// Check Incr uses. One user is PN and the other user is an exit condition
@@ -280,16 +280,16 @@ bool IndVarSimplify::handleFloatingPointIV(Loop *L, PHINode *PN) {
280280
!ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue))
281281
return false;
282282

283-
const auto &EVFltSema = ExitValueVal->getValueAPF().getSemantics();
284-
if (!APFloat::isIEEELikeFP(EVFltSema))
283+
const auto &ExitValueFltSema = ExitValueVal->getValueAPF().getSemantics();
284+
if (!APFloat::isIEEELikeFP(ExitValueFltSema))
285285
return false;
286286

287-
uint64_t ExitValPrecision = APFloat::semanticsPrecision(EVFltSema);
288-
if (ExitValPrecision >= 64)
287+
uint64_t ExitValuePrecision = APFloat::semanticsPrecision(ExitValueFltSema);
288+
if (ExitValuePrecision >= 64)
289289
return false;
290290

291-
uint64_t ExitValIntegerLimit = 1LL << ExitValPrecision;
292-
if (uint64_t(std::abs(ExitValue)) > ExitValIntegerLimit)
291+
uint64_t ExitValueIntegerLimit = 1LL << ExitValuePrecision;
292+
if (AbsoluteValue(ExitValue) > ExitValueIntegerLimit)
293293
return false;
294294

295295
// Find new predicate for integer comparison.

llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,61 @@ exit:
499499
ret void
500500
}
501501

502+
define void @test_fp_to_int_irrealizable_exitval_int64_min() {
503+
; CHECK-LABEL: @test_fp_to_int_irrealizable_exitval_int64_min(
504+
; CHECK-NEXT: entry:
505+
; CHECK-NEXT: br label [[LOOP:%.*]]
506+
; CHECK: loop:
507+
; CHECK-NEXT: [[IV:%.*]] = phi double [ 2.500000e+01, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
508+
; CHECK-NEXT: call void @opaque()
509+
; CHECK-NEXT: [[IV_NEXT]] = fadd double [[IV]], 1.700000e+01
510+
; CHECK-NEXT: [[CMP:%.*]] = fcmp ult double [[IV_NEXT]], 0xC3E0000000000000
511+
; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
512+
; CHECK: exit:
513+
; CHECK-NEXT: ret void
514+
;
515+
entry:
516+
br label %loop
517+
518+
loop:
519+
%iv = phi double [ 2.500000e+01, %entry ], [ %iv.next, %loop ]
520+
call void @opaque()
521+
%iv.next = fadd double %iv, 1.700000e+01
522+
%cmp = fcmp ult double %iv.next, 0xC3E0000000000000
523+
br i1 %cmp, label %exit, label %loop
524+
525+
exit:
526+
ret void
527+
}
528+
529+
; Init value is 2^53 fp. While it would be within the representable consecutive
530+
; integers, start/stride/exit values currently needs to fit in a signed i32.
531+
532+
define void @test_fp_to_int_maybe_realizable_initval_pow_2_53() {
533+
; CHECK-LABEL: @test_fp_to_int_maybe_realizable_initval_pow_2_53(
534+
; CHECK-NEXT: entry:
535+
; CHECK-NEXT: br label [[LOOP:%.*]]
536+
; CHECK: loop:
537+
; CHECK-NEXT: [[IV:%.*]] = phi double [ 0x4340000000000000, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
538+
; CHECK-NEXT: call void @opaque()
539+
; CHECK-NEXT: [[IV_NEXT]] = fadd double [[IV]], -1.700000e+01
540+
; CHECK-NEXT: [[CMP:%.*]] = fcmp ult double [[IV_NEXT]], 2.500000e+01
541+
; CHECK-NEXT: br i1 [[CMP]], label [[EXIT:%.*]], label [[LOOP]]
542+
; CHECK: exit:
543+
; CHECK-NEXT: ret void
544+
;
545+
entry:
546+
br label %loop
547+
548+
loop:
549+
%iv = phi double [ 0x4340000000000000, %entry ], [ %iv.next, %loop ]
550+
call void @opaque()
551+
%iv.next = fadd double %iv, -1.700000e+01
552+
%cmp = fcmp ult double %iv.next, 2.500000e+01
553+
br i1 %cmp, label %exit, label %loop
554+
555+
exit:
556+
ret void
557+
}
558+
502559
declare void @opaque()

0 commit comments

Comments
 (0)