Skip to content

Commit 112c1c3

Browse files
committed
[IVDescriptor] Make sure the sign is included for negative extension.
At the moment, computeRecurrenceType does not include any sign bits in the maximum bit width. If the value can be negative, this means the sign bit will be missing and the sext won't properly extend the value. If the value can be negative, increment the bitwidth by one to make sure there is at least one sign bit in the result value. Note that the increment is also needed *if* the value is *known* to be negative, as a sign bit needs to be preserved for the sext to work. Note that this at the moment prevents vectorization, because the analysis computes i1 as type for the recurrence when looking through the AND in lookThroughAnd. Fixes PR51794, PR52485. Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D113056
1 parent 6938270 commit 112c1c3

File tree

2 files changed

+5
-30
lines changed

2 files changed

+5
-30
lines changed

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,9 @@ static std::pair<Type *, bool> computeRecurrenceType(Instruction *Exit,
146146
// meaning that we will use sext instructions instead of zext
147147
// instructions to restore the original type.
148148
IsSigned = true;
149-
if (!Bits.isNegative())
150-
// If the value is not known to be negative, we don't known what the
151-
// upper bit is, and therefore, we don't know what kind of extend we
152-
// will need. In this case, just increase the bit width by one bit and
153-
// use sext.
154-
++MaxBitWidth;
149+
// Make sure at at least one sign bit is included in the result, so it
150+
// will get properly sign-extended.
151+
++MaxBitWidth;
155152
}
156153
}
157154
if (!isPowerOf2_64(MaxBitWidth))

llvm/test/Transforms/LoopVectorize/reduction-small-size.ll

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,7 @@ for.end:
7474

7575
define i32 @pr51794_signed_negative(i16 %iv.start, i32 %xor.start) {
7676
; CHECK-LABEL: define {{.*}} @pr51794_signed_negative(
77-
; CHECK: [[XOR_START:%.+]] = insertelement <4 x i32> zeroinitializer, i32 %xor.start, i32 0
78-
; CHECK-LABEL: vector.body:
79-
; CHECK: [[XOR_RED:%.+]] = phi <4 x i32> [ [[XOR_START]], %vector.ph ], [ [[XOR_SEXT:%.+]], %vector.body ]
80-
; CHECK: [[AND:%.+]] = and <4 x i32> [[XOR_RED]], <i32 1, i32 1, i32 1, i32 1>
81-
; CHECK-NEXT: [[XOR:%.+]] = xor <4 x i32> [[AND]], <i32 -1, i32 -1, i32 -1, i32 -1>
82-
; CHECK: [[XOR_TRUNC:%.+]] = trunc <4 x i32> [[XOR]] to <4 x i1>
83-
; CHECK-NEXT: [[XOR_SEXT]] = sext <4 x i1> [[XOR_TRUNC]] to <4 x i32>
84-
;
85-
; CHECK-LABEL: middle.block:
86-
; CHECK-NEXT: [[RES_TRUNC:%.+]] = trunc <4 x i32> [[XOR_SEXT]] to <4 x i1>
87-
; CHECK-NEXT: [[RES_RED:%.+]] = call i1 @llvm.vector.reduce.xor.v4i1(<4 x i1> [[RES_TRUNC]])
88-
; CHECK-NEXT: sext i1 [[RES_RED]] to i32
77+
; CHECK-NOT: vector.body:
8978
;
9079
entry:
9180
br label %loop
@@ -106,18 +95,7 @@ exit:
10695

10796
define i32 @pr52485_signed_negative(i32 %xor.start) {
10897
; CHECK-LABEL: define {{.*}} @pr52485_signed_negative(
109-
; CHECK: [[XOR_START:%.+]] = insertelement <4 x i32> zeroinitializer, i32 %xor.start, i32 0
110-
; CHECK-LABEL: vector.body:
111-
; CHECK: [[XOR_RED:%.+]] = phi <4 x i32> [ [[XOR_START]], %vector.ph ], [ [[XOR_SEXT:%.+]], %vector.body ]
112-
; CHECK: [[AND:%.+]] = and <4 x i32> [[XOR_RED]], <i32 255, i32 255, i32 255, i32 255>
113-
; CHECK-NEXT: [[XOR:%.+]] = xor <4 x i32> [[AND]], <i32 -9, i32 -9, i32 -9, i32 -9>
114-
; CHECK: [[XOR_TRUNC:%.+]] = trunc <4 x i32> [[XOR]] to <4 x i8>
115-
; CHECK-NEXT: [[XOR_SEXT]] = sext <4 x i8> [[XOR_TRUNC]] to <4 x i32>
116-
;
117-
; CHECK-LABEL: middle.block:
118-
; CHECK-NEXT: [[RES_TRUNC:%.+]] = trunc <4 x i32> [[XOR_SEXT]] to <4 x i8>
119-
; CHECK-NEXT: [[RES_RED:%.+]] = call i8 @llvm.vector.reduce.xor.v4i8(<4 x i8> [[RES_TRUNC]])
120-
; CHECK-NEXT: sext i8 [[RES_RED]] to i32
98+
; CHECK-NOT: vector.body:
12199
;
122100
entry:
123101
br label %loop

0 commit comments

Comments
 (0)