Skip to content

Commit 77ddcf7

Browse files
authored
[SystemZ] Fix bitwidth problem in FindReplicatedImm(). (#115383)
A test case emerged with an i32 truncating store of an i64 constant operand, where the i64 constant did not fit in 32 bits, which caused FindReplicatedImm() to crash. Make sure to truncate the APInt in these cases.
1 parent 25d1ac1 commit 77ddcf7

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

llvm/lib/Target/SystemZ/SystemZISelLowering.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7223,7 +7223,16 @@ SDValue SystemZTargetLowering::combineSTORE(
72237223
if (C->getAPIntValue().getBitWidth() > 64 || C->isAllOnes() ||
72247224
isInt<16>(C->getSExtValue()) || MemVT.getStoreSize() <= 2)
72257225
return;
7226-
SystemZVectorConstantInfo VCI(APInt(TotBytes * 8, C->getZExtValue()));
7226+
7227+
APInt Val = C->getAPIntValue();
7228+
// Truncate Val in case of a truncating store.
7229+
if (!llvm::isUIntN(TotBytes * 8, Val.getZExtValue())) {
7230+
assert(SN->isTruncatingStore() &&
7231+
"Non-truncating store and immediate value does not fit?");
7232+
Val = Val.trunc(TotBytes * 8);
7233+
}
7234+
7235+
SystemZVectorConstantInfo VCI(APInt(TotBytes * 8, Val.getZExtValue()));
72277236
if (VCI.isVectorConstantLegal(Subtarget) &&
72287237
VCI.Opcode == SystemZISD::REPLICATE) {
72297238
Word = DAG.getConstant(VCI.OpVals[0], SDLoc(SN), MVT::i32);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -mtriple=s390x-linux-gnu -mcpu=z16 | FileCheck %s
3+
;
4+
; Test that SystemZTargetLowering::combineSTORE() does not crash on a
5+
; truncated immediate.
6+
7+
@G1 = external global i64, align 8
8+
@G2 = external global i64, align 8
9+
10+
define void @func_5(ptr %Dst) {
11+
; CHECK-LABEL: func_5:
12+
; CHECK: # %bb.0:
13+
; CHECK-NEXT: lgrl %r1, G2@GOT
14+
; CHECK-NEXT: llihl %r0, 50
15+
; CHECK-NEXT: oill %r0, 2
16+
; CHECK-NEXT: stg %r0, 0(%r1)
17+
; CHECK-NEXT: lgrl %r1, G1@GOT
18+
; CHECK-NEXT: stg %r0, 0(%r1)
19+
; CHECK-NEXT: mvhi 0(%r2), 2
20+
; CHECK-NEXT: br %r14
21+
store i64 214748364802, ptr @G2, align 8
22+
store i64 214748364802, ptr @G1, align 8
23+
%1 = load i32, ptr getelementptr inbounds (i8, ptr @G1, i64 4), align 4
24+
store i32 %1, ptr %Dst, align 4
25+
ret void
26+
}

0 commit comments

Comments
 (0)