Skip to content

Commit a47b123

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 a47b123

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ 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 static references (CHERIoT)"),
100+
cl::init(false));
101+
97102
RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
98103
const RISCVSubtarget &STI)
99104
: TargetLowering(TM), Subtarget(STI) {
@@ -8977,14 +8982,17 @@ static SDValue getLargeExternalSymbol(ExternalSymbolSDNode *N, const SDLoc &DL,
89778982
template <class NodeTy>
89788983
SDValue RISCVTargetLowering::getAddr(NodeTy *N, EVT Ty, SelectionDAG &DAG,
89798984
bool IsLocal, bool CanDeriveFromPcc,
8980-
bool IsExternWeak) const {
8985+
bool IsExternWeak,
8986+
const GlobalValue *GV) const {
89818987
SDLoc DL(N);
89828988

89838989
if (RISCVABI::isCheriPureCapABI(Subtarget.getTargetABI())) {
89848990
bool IsCheriot = Subtarget.getTargetABI() == RISCVABI::ABI_CHERIOT ||
89858991
Subtarget.getTargetABI() == RISCVABI::ABI_CHERIOT_BAREMETAL;
89868992
SDValue Addr = getTargetNode(N, DL, Ty, DAG, 0);
8987-
if ((IsLocal && CanDeriveFromPcc) || IsCheriot) {
8993+
bool NotHighUseGV = !GV || GV->getNumUses() < 4;
8994+
if ((IsLocal && CanDeriveFromPcc) ||
8995+
(IsCheriot && CheriotUseCapTable && NotHighUseGV)) {
89888996
// Use PC-relative addressing to access the symbol. This generates the
89898997
// pattern (PseudoCLLC sym), which expands to
89908998
// (cincoffsetimm (auipcc %pcrel_hi(sym)) %pcrel_lo(auipc)).
@@ -9121,7 +9129,7 @@ SDValue RISCVTargetLowering::lowerGlobalAddress(SDValue Op,
91219129
}
91229130

91239131
return getAddr(N, Ty, DAG, GV->isDSOLocal(), /*CanDeriveFromPcc=*/false,
9124-
GV->hasExternalWeakLinkage());
9132+
GV->hasExternalWeakLinkage(), GV);
91259133
}
91269134

91279135
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)