Skip to content

Commit b73229e

Browse files
committed
[SelectionDAG] Use logic right shift to avoid loop hang
Issue was reported in D149033, `Val` can be negative value and arithmetic right shift always keeps the sign bit. BTW, the redundant code `Val = -Val` is removed by this patch.
1 parent 9c48aa6 commit b73229e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -5459,9 +5459,6 @@ static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS,
54595459

54605460
if (DAG.getTargetLoweringInfo().isBeneficialToExpandPowI(
54615461
Val, DAG.shouldOptForSize())) {
5462-
// Get the exponent as a positive value.
5463-
if (Val < 0)
5464-
Val = -Val;
54655462
// We use the simple binary decomposition method to generate the multiply
54665463
// sequence. There are more optimal ways to do this (for example,
54675464
// powi(x,15) generates one more multiply than it should), but this has
@@ -5481,7 +5478,8 @@ static SDValue ExpandPowI(const SDLoc &DL, SDValue LHS, SDValue RHS,
54815478

54825479
CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
54835480
CurSquare, CurSquare);
5484-
Val >>= 1;
5481+
// Use logic right shift
5482+
Val = int(unsigned(Val) >> 1);
54855483
}
54865484

54875485
// If the original was negative, invert the result, producing 1/(x*x*x).
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; RUN: llc -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
2+
3+
define void @test_powi(ptr %p) nounwind {
4+
; CHECK-LABEL: powi:
5+
; CHECK: pushq %rax
6+
; CHECK-NEXT: movss {{.*#+}} xmm1 = mem[0],zero,zero,zero
7+
; CHECK-COUNT-31: mulss %xmm1, %xmm1
8+
; CHECK-NEXT: movss {{.*#+}} xmm0 = mem[0],zero,zero,zero
9+
; CHECK-NEXT: divss %xmm1, %xmm0
10+
; CHECK-NEXT: callq foo@PLT
11+
; CHECK-NEXT: popq %rax
12+
; CHECK-NEXT: retq
13+
bb:
14+
%load = load float, ptr %p, align 4
15+
%call1 = call contract float @llvm.powi.f32.i32(float %load, i32 -2147483648)
16+
%call2 = call i1 @foo(float %call1)
17+
ret void
18+
}
19+
20+
declare zeroext i1 @foo(float)
21+
declare float @llvm.powi.f32.i32(float, i32)

0 commit comments

Comments
 (0)