Skip to content

Commit 2eb997d

Browse files
wangleiatllvmbot
authored andcommitted
[LoongArch] Make sure that the LoongArchISD::BSTRINS node uses the correct MSB value (llvm#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. (cherry picked from commit edd4c6c)
1 parent dba2a75 commit 2eb997d

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
@@ -2343,7 +2343,9 @@ static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
23432343
return DAG.getNode(
23442344
LoongArchISD::BSTRINS, DL, ValTy, N0.getOperand(0),
23452345
DAG.getConstant(CN1->getSExtValue() >> MaskIdx0, DL, ValTy),
2346-
DAG.getConstant((MaskIdx0 + MaskLen0 - 1), DL, GRLenVT),
2346+
DAG.getConstant(ValBits == 32 ? (MaskIdx0 + (MaskLen0 & 31) - 1)
2347+
: (MaskIdx0 + MaskLen0 - 1),
2348+
DL, GRLenVT),
23472349
DAG.getConstant(MaskIdx0, DL, GRLenVT));
23482350
}
23492351

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)