Skip to content

Commit 1c75693

Browse files
authored
[DAGCombiner] Bail out if BitWidthDiff > BitWidth when folding cltz(and) - BitWidthDiff (#166607)
Fixes #166596 We cannot use `APInt::isMask` if `numBits` exceeds the bitwidth of APInt or `numBits` is zero. We avoid such a case by guaranteeing BitWidthDiff < BitWidth.
1 parent 9100c92 commit 1c75693

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,6 +4046,8 @@ static SDValue foldSubCtlzNot(SDNode *N, SelectionDAG &DAG) {
40464046
m_ConstInt(AndMask)))) {
40474047
// Type Legalisation Pattern:
40484048
// (sub (ctlz (and (xor Op XorMask) AndMask)) BitWidthDiff)
4049+
if (BitWidthDiff.getZExtValue() >= BitWidth)
4050+
return SDValue();
40494051
unsigned AndMaskWidth = BitWidth - BitWidthDiff.getZExtValue();
40504052
if (!(AndMask.isMask(AndMaskWidth) && XorMask.countr_one() >= AndMaskWidth))
40514053
return SDValue();

llvm/test/CodeGen/RISCV/zicond-opts.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,35 @@ define i64 @test_inv_and_eqz(i64 %f, i64 %x, i1 %cond) {
263263
%7 = and i64 %6, %f
264264
ret i64 %7
265265
}
266+
267+
define i32 @pr166596(i32 %conv.i, i1 %iszero) #0 {
268+
; RV32ZICOND-LABEL: pr166596:
269+
; RV32ZICOND: # %bb.0: # %entry
270+
; RV32ZICOND-NEXT: andi a1, a1, 1
271+
; RV32ZICOND-NEXT: xori a0, a0, 1
272+
; RV32ZICOND-NEXT: zext.h a0, a0
273+
; RV32ZICOND-NEXT: clz a0, a0
274+
; RV32ZICOND-NEXT: addi a0, a0, 41
275+
; RV32ZICOND-NEXT: czero.nez a0, a0, a1
276+
; RV32ZICOND-NEXT: addi a0, a0, -9
277+
; RV32ZICOND-NEXT: ret
278+
;
279+
; RV64ZICOND-LABEL: pr166596:
280+
; RV64ZICOND: # %bb.0: # %entry
281+
; RV64ZICOND-NEXT: andi a1, a1, 1
282+
; RV64ZICOND-NEXT: xori a0, a0, 1
283+
; RV64ZICOND-NEXT: zext.h a0, a0
284+
; RV64ZICOND-NEXT: clz a0, a0
285+
; RV64ZICOND-NEXT: addi a0, a0, 9
286+
; RV64ZICOND-NEXT: czero.nez a0, a0, a1
287+
; RV64ZICOND-NEXT: addi a0, a0, -9
288+
; RV64ZICOND-NEXT: ret
289+
entry:
290+
%not.i = xor i32 %conv.i, 1
291+
%conv2.i = trunc i32 %not.i to i16
292+
%conv22 = zext i16 %conv2.i to i64
293+
%0 = call i64 @llvm.ctlz.i64(i64 %conv22, i1 false)
294+
%cast = trunc i64 %0 to i32
295+
%clzg = select i1 %iszero, i32 -9, i32 %cast
296+
ret i32 %clzg
297+
}

0 commit comments

Comments
 (0)