Skip to content

Commit ef036a7

Browse files
committed
[CHERIoT] Make it possible to use captable relocations for heavily used globals, for which it is a net improvement in code size.
1 parent 0a9cacb commit ef036a7

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,8 @@ bool RISCVExpandPseudo::expandAuipccInstPair(
707707
MF->getSubtarget<RISCVSubtarget>().isRV32E() && Symbol.isGlobal() &&
708708
isa<GlobalVariable>(Symbol.getGlobal()) &&
709709
(cast<GlobalVariable>(Symbol.getGlobal())->getSection() !=
710-
".compartment_imports"))
710+
".compartment_imports") &&
711+
FlagsHi != RISCVII::MO_CAPTAB_PCREL_HI)
711712
BuildMI(NewMBB, DL, TII->get(RISCV::CSetBoundsImm), DestReg)
712713
.addReg(DestReg)
713714
.addDisp(Symbol, 0, RISCVII::MO_CHERIOT_COMPARTMENT_SIZE);

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ static cl::opt<bool>
9494
"be combined with a shift"),
9595
cl::init(true));
9696

97+
static cl::opt<bool>
98+
CheriotUseCapTable("cheriot-use-cap-table", cl::Hidden,
99+
cl::desc("Use cap tables to access globals with many "
100+
"static references (CHERIoT)"),
101+
cl::init(false));
102+
97103
RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
98104
const RISCVSubtarget &STI)
99105
: TargetLowering(TM), Subtarget(STI) {
@@ -8977,14 +8983,17 @@ static SDValue getLargeExternalSymbol(ExternalSymbolSDNode *N, const SDLoc &DL,
89778983
template <class NodeTy>
89788984
SDValue RISCVTargetLowering::getAddr(NodeTy *N, EVT Ty, SelectionDAG &DAG,
89798985
bool IsLocal, bool CanDeriveFromPcc,
8980-
bool IsExternWeak) const {
8986+
bool IsExternWeak,
8987+
const GlobalValue *GV) const {
89818988
SDLoc DL(N);
89828989

89838990
if (RISCVABI::isCheriPureCapABI(Subtarget.getTargetABI())) {
89848991
bool IsCheriot = Subtarget.getTargetABI() == RISCVABI::ABI_CHERIOT ||
89858992
Subtarget.getTargetABI() == RISCVABI::ABI_CHERIOT_BAREMETAL;
89868993
SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
8987-
if ((IsLocal && CanDeriveFromPcc) || IsCheriot) {
8994+
bool NotHighUseGV = !GV || GV->getNumUses() < 4;
8995+
if ((IsLocal && CanDeriveFromPcc) ||
8996+
(IsCheriot && CheriotUseCapTable && NotHighUseGV)) {
89888997
// Use PC-relative addressing to access the symbol. This generates the
89898998
// pattern (PseudoCLLC sym), which expands to
89908999
// (cincoffsetimm (auipcc %pcrel_hi(sym)) %pcrel_lo(auipc)).
@@ -9121,7 +9130,7 @@ SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
91219130
}
91229131

91239132
return getAddr(N, Ty, DAG, GV->isDSOLocal(), /*CanDeriveFromPcc=*/false,
9124-
GV->hasExternalWeakLinkage());
9133+
GV->hasExternalWeakLinkage(), GV);
91259134
}
91269135

91279136
SDValue RISCVTargetLowering::lowerBlockAddress(SDValue Op,

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ class RISCVTargetLowering : public TargetLowering {
500500

501501
template <class NodeTy>
502502
SDValue getAddr(NodeTy *N, EVT Ty, SelectionDAG &DAG, bool IsLocal,
503-
bool CanDeriveFromPcc, bool IsExternWeak = false) const;
503+
bool CanDeriveFromPcc, bool IsExternWeak = false,
504+
const GlobalValue *GV = nullptr) const;
504505
SDValue getStaticTLSAddr(GlobalAddressSDNode *N, EVT Ty, SelectionDAG &DAG,
505506
bool NotLocal) const;
506507
SDValue getDynamicTLSAddr(GlobalAddressSDNode *N, EVT Ty,

0 commit comments

Comments
 (0)