Skip to content

Commit 50c6ba7

Browse files
topperctstellar
authored andcommitted
[RISCV] Only try LUI+SH*ADD+ADDI for int materialization if LUI+ADDI+SH*ADD failed.
There's an assert in LUI+SH*ADD+ADDI materialization that makes sure the lower 12 bits aren't zero since that case should have been handled as LUI+ADDI+SH*ADD. But nothing prevented the LUI+SH*ADD+ADDI checks from running after the earlier code handled it. The sequence would be the same length or longer so it wouldn't replace the earlier sequence, but the assert happened before that was checked. The vector holding the sequence also wasn't reset before the second check so that guaranteed the sequence would never be found to be shorter. This patch fixes this by only trying the second expansion when the earlier fails. Fixes PR54812. Reviewed By: benshi001 Differential Revision: https://reviews.llvm.org/D123406 (cherry picked from commit 7004643)
1 parent dc30b0d commit 50c6ba7

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -302,32 +302,34 @@ InstSeq generateInstSeq(int64_t Val, const FeatureBitset &ActiveFeatures) {
302302
TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0));
303303
if (TmpSeq.size() < Res.size())
304304
Res = TmpSeq;
305-
}
306-
// Try to use LUI+SH*ADD+ADDI.
307-
int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull;
308-
int64_t Lo12 = SignExtend64<12>(Val);
309-
Div = 0;
310-
if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) {
311-
Div = 3;
312-
Opc = RISCV::SH1ADD;
313-
} else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) {
314-
Div = 5;
315-
Opc = RISCV::SH2ADD;
316-
} else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) {
317-
Div = 9;
318-
Opc = RISCV::SH3ADD;
319-
}
320-
// Build the new instruction sequence.
321-
if (Div > 0) {
322-
// For Val that has zero Lo12 (implies Val equals to Hi52) should has
323-
// already been processed to LUI+SH*ADD by previous optimization.
324-
assert(Lo12 != 0 &&
325-
"unexpected instruction sequence for immediate materialisation");
326-
generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq);
327-
TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0));
328-
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12));
329-
if (TmpSeq.size() < Res.size())
330-
Res = TmpSeq;
305+
} else {
306+
// Try to use LUI+SH*ADD+ADDI.
307+
int64_t Hi52 = ((uint64_t)Val + 0x800ull) & ~0xfffull;
308+
int64_t Lo12 = SignExtend64<12>(Val);
309+
Div = 0;
310+
if (isInt<32>(Hi52 / 3) && (Hi52 % 3) == 0) {
311+
Div = 3;
312+
Opc = RISCV::SH1ADD;
313+
} else if (isInt<32>(Hi52 / 5) && (Hi52 % 5) == 0) {
314+
Div = 5;
315+
Opc = RISCV::SH2ADD;
316+
} else if (isInt<32>(Hi52 / 9) && (Hi52 % 9) == 0) {
317+
Div = 9;
318+
Opc = RISCV::SH3ADD;
319+
}
320+
// Build the new instruction sequence.
321+
if (Div > 0) {
322+
// For Val that has zero Lo12 (implies Val equals to Hi52) should has
323+
// already been processed to LUI+SH*ADD by previous optimization.
324+
assert(Lo12 != 0 &&
325+
"unexpected instruction sequence for immediate materialisation");
326+
assert(TmpSeq.empty() && "Expected empty TmpSeq");
327+
generateInstSeqImpl(Hi52 / Div, ActiveFeatures, TmpSeq);
328+
TmpSeq.push_back(RISCVMatInt::Inst(Opc, 0));
329+
TmpSeq.push_back(RISCVMatInt::Inst(RISCV::ADDI, Lo12));
330+
if (TmpSeq.size() < Res.size())
331+
Res = TmpSeq;
332+
}
331333
}
332334
}
333335

llvm/test/CodeGen/RISCV/imm.ll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,3 +2401,39 @@ define i64 @li_rori_3() {
24012401
; RV64IZBS-NEXT: ret
24022402
ret i64 -2281701377
24032403
}
2404+
2405+
; This used to assert when compiled with Zba.
2406+
define i64 @PR54812() {
2407+
; RV32I-LABEL: PR54812:
2408+
; RV32I: # %bb.0:
2409+
; RV32I-NEXT: lui a0, 521599
2410+
; RV32I-NEXT: li a1, -1
2411+
; RV32I-NEXT: ret
2412+
;
2413+
; RV64I-LABEL: PR54812:
2414+
; RV64I: # %bb.0:
2415+
; RV64I-NEXT: lui a0, 1048447
2416+
; RV64I-NEXT: addiw a0, a0, 1407
2417+
; RV64I-NEXT: slli a0, a0, 12
2418+
; RV64I-NEXT: ret
2419+
;
2420+
; RV64IZBA-LABEL: PR54812:
2421+
; RV64IZBA: # %bb.0:
2422+
; RV64IZBA-NEXT: lui a0, 872917
2423+
; RV64IZBA-NEXT: sh1add a0, a0, a0
2424+
; RV64IZBA-NEXT: ret
2425+
;
2426+
; RV64IZBB-LABEL: PR54812:
2427+
; RV64IZBB: # %bb.0:
2428+
; RV64IZBB-NEXT: lui a0, 1048447
2429+
; RV64IZBB-NEXT: addiw a0, a0, 1407
2430+
; RV64IZBB-NEXT: slli a0, a0, 12
2431+
; RV64IZBB-NEXT: ret
2432+
;
2433+
; RV64IZBS-LABEL: PR54812:
2434+
; RV64IZBS: # %bb.0:
2435+
; RV64IZBS-NEXT: lui a0, 1045887
2436+
; RV64IZBS-NEXT: bclri a0, a0, 31
2437+
; RV64IZBS-NEXT: ret
2438+
ret i64 -2158497792;
2439+
}

0 commit comments

Comments
 (0)