Skip to content

Commit d147f2b

Browse files
committed
The actual fix
1 parent d3a92e9 commit d147f2b

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,16 @@ bool RISCVDAGToDAGISel::trySignedBitfieldInsertInMask(SDNode *Node) {
689689
if (!isShiftedMask_32(C1) || isInt<12>(C1))
690690
return false;
691691

692+
// INSBI will clobber the input register in N0. Bail out if we need a copy to
693+
// preserve this value.
694+
SDValue N0 = Node->getOperand(0);
695+
if (!N0.hasOneUse())
696+
return false;
697+
692698
// If C1 is a shifted mask (but can't be formed as an ORI),
693699
// use a bitfield insert of -1.
694700
// Transform (or x, C1)
695-
// -> (qc.insbi x, width, shift)
701+
// -> (qc.insbi x, -1, width, shift)
696702
const unsigned Leading = llvm::countl_zero((uint32_t)C1);
697703
const unsigned Trailing = llvm::countr_zero((uint32_t)C1);
698704
const unsigned Width = 32 - Leading - Trailing;
@@ -705,7 +711,7 @@ bool RISCVDAGToDAGISel::trySignedBitfieldInsertInMask(SDNode *Node) {
705711
SDLoc DL(Node);
706712
MVT VT = Node->getSimpleValueType(0);
707713

708-
SDValue Ops[] = {CurDAG->getSignedTargetConstant(-1, DL, VT),
714+
SDValue Ops[] = {N0, CurDAG->getSignedTargetConstant(-1, DL, VT),
709715
CurDAG->getTargetConstant(Width, DL, VT),
710716
CurDAG->getTargetConstant(Trailing, DL, VT)};
711717
SDNode *BitIns = CurDAG->getMachineNode(RISCV::QC_INSBI, DL, VT, Ops);

llvm/lib/Target/RISCV/RISCVInstrInfoXqci.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,11 @@ let Predicates = [HasVendorXqcibi, IsRV32] in {
845845
let Predicates = [HasVendorXqcibm, IsRV32] in {
846846
let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
847847
def QC_INSBRI : QCIRVInstRI<0b1, simm11, "qc.insbri">;
848-
def QC_INSBI : RVInstIBase<0b001, OPC_CUSTOM_0, (outs GPRNoX0:$rd),
849-
(ins simm5:$imm5, uimm5_plus1:$width,
848+
def QC_INSBI : RVInstIBase<0b001, OPC_CUSTOM_0, (outs GPRNoX0:$rd_wb),
849+
(ins GPRNoX0:$rd, simm5:$imm5, uimm5_plus1:$width,
850850
uimm5:$shamt), "qc.insbi",
851851
"$rd, $imm5, $width, $shamt"> {
852+
let Constraints = "$rd = $rd_wb";
852853
bits<5> imm5;
853854
bits<5> shamt;
854855
bits<5> width;

llvm/test/CodeGen/RISCV/xqcibm-cto-clo-brev.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ define i16 @test_cttz_i16(i16 %a) nounwind {
105105
;
106106
; RV32ZBBXQCIBM-LABEL: test_cttz_i16:
107107
; RV32ZBBXQCIBM: # %bb.0:
108+
; RV32ZBBXQCIBM-NEXT: not a0, a0
108109
; RV32ZBBXQCIBM-NEXT: qc.insbi a0, -1, 1, 16
109110
; RV32ZBBXQCIBM-NEXT: ctz a0, a0
110111
; RV32ZBBXQCIBM-NEXT: ret

llvm/test/CodeGen/RISCV/xqcibm-insert.ll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ define i32 @test_insbi_mask_mv(i32 %a, i32 %b) nounwind {
5757
;
5858
; RV32IXQCIBM-LABEL: test_insbi_mask_mv:
5959
; RV32IXQCIBM: # %bb.0:
60+
; RV32IXQCIBM-NEXT: mv a0, a1
6061
; RV32IXQCIBM-NEXT: qc.insbi a0, -1, 16, 0
6162
; RV32IXQCIBM-NEXT: ret
6263
;
6364
; RV32IXQCIBMZBS-LABEL: test_insbi_mask_mv:
6465
; RV32IXQCIBMZBS: # %bb.0:
66+
; RV32IXQCIBMZBS-NEXT: mv a0, a1
6567
; RV32IXQCIBMZBS-NEXT: qc.insbi a0, -1, 16, 0
6668
; RV32IXQCIBMZBS-NEXT: ret
6769
%or = or i32 %b, 65535
@@ -99,14 +101,16 @@ define i32 @test_insbi_shifted_mask_multiple_uses(i32 %a) nounwind {
99101
;
100102
; RV32IXQCIBM-LABEL: test_insbi_shifted_mask_multiple_uses:
101103
; RV32IXQCIBM: # %bb.0:
102-
; RV32IXQCIBM-NEXT: qc.insbi a1, -1, 4, 12
104+
; RV32IXQCIBM-NEXT: lui a1, 15
105+
; RV32IXQCIBM-NEXT: or a1, a1, a0
103106
; RV32IXQCIBM-NEXT: addi a0, a0, 10
104107
; RV32IXQCIBM-NEXT: xor a0, a0, a1
105108
; RV32IXQCIBM-NEXT: ret
106109
;
107110
; RV32IXQCIBMZBS-LABEL: test_insbi_shifted_mask_multiple_uses:
108111
; RV32IXQCIBMZBS: # %bb.0:
109-
; RV32IXQCIBMZBS-NEXT: qc.insbi a1, -1, 4, 12
112+
; RV32IXQCIBMZBS-NEXT: lui a1, 15
113+
; RV32IXQCIBMZBS-NEXT: or a1, a1, a0
110114
; RV32IXQCIBMZBS-NEXT: addi a0, a0, 10
111115
; RV32IXQCIBMZBS-NEXT: xor a0, a0, a1
112116
; RV32IXQCIBMZBS-NEXT: ret

0 commit comments

Comments
 (0)