Skip to content

Commit ca45c15

Browse files
committed
Reduce the amount of repetition
1 parent b6f7492 commit ca45c15

File tree

1 file changed

+33
-51
lines changed

1 file changed

+33
-51
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7471,63 +7471,45 @@ SDValue RISCVTargetLowering::lowerINIT_TRAMPOLINE(SDValue Op,
74717471
};
74727472

74737473
SDValue OutChains[6];
7474-
SDValue Addr = Trmp;
7475-
7476-
// auipc t2, 0
7477-
// Loads the current PC into t2.
7478-
OutChains[0] = DAG.getTruncStore(
7479-
Root, dl,
7480-
DAG.getConstant(
7481-
GetEncoding(MCInstBuilder(RISCV::AUIPC).addReg(RISCV::X7).addImm(0)),
7482-
dl, MVT::i64),
7483-
Addr, MachinePointerInfo(TrmpAddr), MVT::i32);
7484-
7485-
// ld t0, 24(t2)
7486-
// Loads the function address into t0. Note that we are using offsets
7487-
// pc-relative to the first instruction of the trampoline.
7488-
Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7489-
DAG.getConstant(4, dl, MVT::i64));
7490-
OutChains[1] = DAG.getTruncStore(
7491-
Root, dl,
7492-
DAG.getConstant(GetEncoding(MCInstBuilder(RISCV::LD)
7493-
.addReg(RISCV::X5)
7494-
.addReg(RISCV::X7)
7495-
.addImm(FunctionAddressOffset)),
7496-
dl, MVT::i64),
7497-
Addr, MachinePointerInfo(TrmpAddr, 4), MVT::i32);
7498-
7499-
// ld t2, 16(t2)
7500-
// Load the value of the static chain.
7501-
Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7502-
DAG.getConstant(8, dl, MVT::i64));
7503-
OutChains[2] = DAG.getTruncStore(
7504-
Root, dl,
7505-
DAG.getConstant(GetEncoding(MCInstBuilder(RISCV::LD)
7506-
.addReg(RISCV::X7)
7507-
.addReg(RISCV::X7)
7508-
.addImm(StaticChainOffset)),
7509-
dl, MVT::i64),
7510-
Addr, MachinePointerInfo(TrmpAddr, 8), MVT::i32);
7511-
7512-
// jalr t0
7513-
// Jump to the function.
7514-
Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7515-
DAG.getConstant(12, dl, MVT::i64));
7516-
OutChains[3] =
7517-
DAG.getTruncStore(Root, dl,
7518-
DAG.getConstant(GetEncoding(MCInstBuilder(RISCV::JALR)
7519-
.addReg(RISCV::X0)
7520-
.addReg(RISCV::X5)
7521-
.addImm(0)),
7522-
dl, MVT::i64),
7523-
Addr, MachinePointerInfo(TrmpAddr, 12), MVT::i32);
7474+
7475+
uint32_t Encodings[] = {
7476+
// auipc t2, 0
7477+
// Loads the current PC into t2.
7478+
GetEncoding(MCInstBuilder(RISCV::AUIPC).addReg(RISCV::X7).addImm(0)),
7479+
// ld t0, 24(t2)
7480+
// Loads the function address into t0. Note that we are using offsets
7481+
// pc-relative to the first instruction of the trampoline.
7482+
GetEncoding(
7483+
MCInstBuilder(RISCV::LD).addReg(RISCV::X5).addReg(RISCV::X7).addImm(
7484+
FunctionAddressOffset)),
7485+
// ld t2, 16(t2)
7486+
// Load the value of the static chain.
7487+
GetEncoding(
7488+
MCInstBuilder(RISCV::LD).addReg(RISCV::X7).addReg(RISCV::X7).addImm(
7489+
StaticChainOffset)),
7490+
// jalr t0
7491+
// Jump to the function.
7492+
GetEncoding(MCInstBuilder(RISCV::JALR)
7493+
.addReg(RISCV::X0)
7494+
.addReg(RISCV::X5)
7495+
.addImm(0))};
7496+
7497+
// Store encoded instructions.
7498+
for (auto [Idx, Encoding] : llvm::enumerate(Encodings)) {
7499+
SDValue Addr = Idx > 0 ? DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7500+
DAG.getConstant(Idx * 4, dl, MVT::i64))
7501+
: Trmp;
7502+
OutChains[Idx] = DAG.getTruncStore(
7503+
Root, dl, DAG.getConstant(Encoding, dl, MVT::i64), Addr,
7504+
MachinePointerInfo(TrmpAddr, Idx * 4), MVT::i32);
7505+
}
75247506

75257507
// Now store the variable part of the trampoline.
75267508
SDValue FunctionAddress = Op.getOperand(2);
75277509
SDValue StaticChain = Op.getOperand(3);
75287510

75297511
// Store the given static chain in the trampoline buffer.
7530-
Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7512+
SDValue Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
75317513
DAG.getConstant(StaticChainOffset, dl, MVT::i64));
75327514
OutChains[4] = DAG.getStore(Root, dl, StaticChain, Addr,
75337515
MachinePointerInfo(TrmpAddr, StaticChainOffset));

0 commit comments

Comments
 (0)