|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
9 | 9 | #include "RISCVSelectionDAGInfo.h"
|
| 10 | +#include "RISCVSubtarget.h" |
| 11 | +#include "llvm/CodeGen/SelectionDAG.h" |
10 | 12 |
|
11 | 13 | #define GET_SDNODE_DESC
|
12 | 14 | #include "RISCVGenSDNodeInfo.inc"
|
@@ -62,3 +64,94 @@ void RISCVSelectionDAGInfo::verifyTargetNode(const SelectionDAG &DAG,
|
62 | 64 | }
|
63 | 65 | #endif
|
64 | 66 | }
|
| 67 | + |
| 68 | +SDValue RISCVSelectionDAGInfo::EmitTargetCodeForMemset( |
| 69 | + SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, |
| 70 | + SDValue Size, Align Alignment, bool isVolatile, bool AlwaysInline, |
| 71 | + MachinePointerInfo DstPtrInfo) const { |
| 72 | + const auto &Subtarget = DAG.getSubtarget<RISCVSubtarget>(); |
| 73 | + // We currently do this only for Xqcilsm |
| 74 | + if (!Subtarget.hasVendorXqcilsm()) |
| 75 | + return SDValue(); |
| 76 | + |
| 77 | + // Do this only if we know the size at compile time. |
| 78 | + ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 79 | + if (!ConstantSize) |
| 80 | + return SDValue(); |
| 81 | + |
| 82 | + uint64_t NumberOfBytesToWrite = ConstantSize->getZExtValue(); |
| 83 | + |
| 84 | + // Do this only if it is word aligned and we write a multiple of 4 bytes. |
| 85 | + if (!(Alignment >= 4) || !((NumberOfBytesToWrite & 3) == 0)) |
| 86 | + return SDValue(); |
| 87 | + |
| 88 | + SmallVector<SDValue, 8> OutChains; |
| 89 | + SDValue SrcValueReplicated = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src); |
| 90 | + int NumberOfWords = NumberOfBytesToWrite / 4; |
| 91 | + MachineFunction &MF = DAG.getMachineFunction(); |
| 92 | + auto Volatile = |
| 93 | + isVolatile ? MachineMemOperand::MOVolatile : MachineMemOperand::MONone; |
| 94 | + |
| 95 | + // Helper for constructing the QC_SETWMI instruction |
| 96 | + auto getSetwmiNode = [&](uint8_t SizeWords, uint8_t OffsetSetwmi) -> SDValue { |
| 97 | + SDValue Ops[] = {Chain, SrcValueReplicated, Dst, |
| 98 | + DAG.getTargetConstant(SizeWords, dl, MVT::i32), |
| 99 | + DAG.getTargetConstant(OffsetSetwmi, dl, MVT::i32)}; |
| 100 | + MachineMemOperand *BaseMemOperand = MF.getMachineMemOperand( |
| 101 | + DstPtrInfo.getWithOffset(OffsetSetwmi), |
| 102 | + MachineMemOperand::MOStore | Volatile, SizeWords * 4, Align(4)); |
| 103 | + return DAG.getMemIntrinsicNode(RISCVISD::QC_SETWMI, dl, |
| 104 | + DAG.getVTList(MVT::Other), Ops, MVT::i32, |
| 105 | + BaseMemOperand); |
| 106 | + }; |
| 107 | + |
| 108 | + // If i8 type and constant non-zero value. |
| 109 | + if ((Src.getValueType() == MVT::i8) && !isNullConstant(Src)) |
| 110 | + // Replicate byte to word by multiplication with 0x01010101. |
| 111 | + SrcValueReplicated = |
| 112 | + DAG.getNode(ISD::MUL, dl, MVT::i32, SrcValueReplicated, |
| 113 | + DAG.getConstant(0x01010101ul, dl, MVT::i32)); |
| 114 | + |
| 115 | + // We limit a QC_SETWMI to 16 words or less to improve interruptibility. |
| 116 | + // So for 1-16 words we use a single QC_SETWMI: |
| 117 | + // |
| 118 | + // QC_SETWMI reg1, N, 0(reg2) |
| 119 | + // |
| 120 | + // For 17-32 words we use two QC_SETWMI's with the first as 16 words and the |
| 121 | + // second for the remainder: |
| 122 | + // |
| 123 | + // QC_SETWMI reg1, 16, 0(reg2) |
| 124 | + // QC_SETWMI reg1, N, 64(reg2) |
| 125 | + // |
| 126 | + // For 33-48 words, we would like to use (16, 16, n), but that means the last |
| 127 | + // QC_SETWMI needs an offset of 128 which the instruction doesn't support. |
| 128 | + // So in this case we use a length of 15 for the second instruction and we do |
| 129 | + // the rest with the third instruction. |
| 130 | + // This means the maximum inlined number of words is 47 (for now): |
| 131 | + // |
| 132 | + // QC_SETWMI R2, R0, 16, 0 |
| 133 | + // QC_SETWMI R2, R0, 15, 64 |
| 134 | + // QC_SETWMI R2, R0, N, 124 |
| 135 | + // |
| 136 | + // For 48 words or more, call the target independent memset |
| 137 | + if (NumberOfWords >= 48) |
| 138 | + return SDValue(); |
| 139 | + |
| 140 | + if (NumberOfWords <= 16) { |
| 141 | + // 1 - 16 words |
| 142 | + return getSetwmiNode(NumberOfWords, 0); |
| 143 | + } |
| 144 | + |
| 145 | + if (NumberOfWords <= 32) { |
| 146 | + // 17 - 32 words |
| 147 | + OutChains.push_back(getSetwmiNode(NumberOfWords - 16, 64)); |
| 148 | + OutChains.push_back(getSetwmiNode(16, 0)); |
| 149 | + } else { |
| 150 | + // 33 - 47 words |
| 151 | + OutChains.push_back(getSetwmiNode(NumberOfWords - 31, 124)); |
| 152 | + OutChains.push_back(getSetwmiNode(15, 64)); |
| 153 | + OutChains.push_back(getSetwmiNode(16, 0)); |
| 154 | + } |
| 155 | + |
| 156 | + return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains); |
| 157 | +} |
0 commit comments