Skip to content

Commit edd4c6c

Browse files
authored
[LoongArch] Make sure that the LoongArchISD::BSTRINS node uses the correct MSB value (#84454)
The `MSB` must not be greater than `GRLen`. Without this patch, newly added test cases will crash with LoongArch32, resulting in a 'cannot select' error.
1 parent 60dda1f commit edd4c6c

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,9 @@ static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
23662366
return DAG.getNode(
23672367
LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
23682368
DAG.getConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
2369-
DAG.getConstant((MaskIdx0 + MaskLen0 - 1), DL, GRLenVT),
2369+
DAG.getConstant(ValBits == 32 ? (MaskIdx0 + (MaskLen0 & 31) - 1)
2370+
: (MaskIdx0 + MaskLen0 - 1),
2371+
DL, GRLenVT),
23702372
DAG.getConstant(MaskIdx0, DL, GRLenVT));
23712373
}
23722374

llvm/test/CodeGen/LoongArch/bstrins_w.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ define i32 @pat5(i32 %a) nounwind {
145145
ret i32 %or
146146
}
147147

148+
;; The high bits of `const` are zero.
149+
define i32 @pat5_high_zeros(i32 %a) nounwind {
150+
; CHECK-LABEL: pat5_high_zeros:
151+
; CHECK: # %bb.0:
152+
; CHECK-NEXT: lu12i.w $a1, 1
153+
; CHECK-NEXT: ori $a1, $a1, 564
154+
; CHECK-NEXT: bstrins.w $a0, $a1, 31, 16
155+
; CHECK-NEXT: ret
156+
%and = and i32 %a, 65535 ; 0x0000ffff
157+
%or = or i32 %and, 305397760 ; 0x12340000
158+
ret i32 %or
159+
}
160+
148161
;; Pattern 6: a = b | ((c & mask) << shamt)
149162
;; In this testcase b is 0x10000002, but in fact we do not require b being a
150163
;; constant. As long as all positions in b to be overwritten by the incoming

0 commit comments

Comments
 (0)