Skip to content

Commit a16dac3

Browse files
committed
[AArch64][PAC] Rework discriminator analysis in AUT and AUTPAC
Make use of post-processing the discriminator components by custom inserter hook to eliminate duplication for DAGISel and GlobalISel and improve cross-BB analysis for DAGISel.
1 parent fe13858 commit a16dac3

File tree

5 files changed

+259
-72
lines changed

5 files changed

+259
-72
lines changed

llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,39 +1487,6 @@ void AArch64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc,
14871487
ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
14881488
}
14891489

1490-
static std::tuple<SDValue, SDValue>
1491-
extractPtrauthBlendDiscriminators(SDValue Disc, SelectionDAG *DAG) {
1492-
SDLoc DL(Disc);
1493-
SDValue AddrDisc;
1494-
SDValue ConstDisc;
1495-
1496-
// If this is a blend, remember the constant and address discriminators.
1497-
// Otherwise, it's either a constant discriminator, or a non-blended
1498-
// address discriminator.
1499-
if (Disc->getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
1500-
Disc->getConstantOperandVal(0) == Intrinsic::ptrauth_blend) {
1501-
AddrDisc = Disc->getOperand(1);
1502-
ConstDisc = Disc->getOperand(2);
1503-
} else {
1504-
ConstDisc = Disc;
1505-
}
1506-
1507-
// If the constant discriminator (either the blend RHS, or the entire
1508-
// discriminator value) isn't a 16-bit constant, bail out, and let the
1509-
// discriminator be computed separately.
1510-
auto *ConstDiscN = dyn_cast<ConstantSDNode>(ConstDisc);
1511-
if (!ConstDiscN || !isUInt<16>(ConstDiscN->getZExtValue()))
1512-
return std::make_tuple(DAG->getTargetConstant(0, DL, MVT::i64), Disc);
1513-
1514-
// If there's no address discriminator, use XZR directly.
1515-
if (!AddrDisc)
1516-
AddrDisc = DAG->getRegister(AArch64::XZR, MVT::i64);
1517-
1518-
return std::make_tuple(
1519-
DAG->getTargetConstant(ConstDiscN->getZExtValue(), DL, MVT::i64),
1520-
AddrDisc);
1521-
}
1522-
15231490
void AArch64DAGToDAGISel::SelectPtrauthAuth(SDNode *N) {
15241491
SDLoc DL(N);
15251492
// IntrinsicID is operand #0
@@ -1530,20 +1497,18 @@ void AArch64DAGToDAGISel::SelectPtrauthAuth(SDNode *N) {
15301497
unsigned AUTKeyC = cast<ConstantSDNode>(AUTKey)->getZExtValue();
15311498
AUTKey = CurDAG->getTargetConstant(AUTKeyC, DL, MVT::i64);
15321499

1533-
SDValue AUTAddrDisc, AUTConstDisc;
1534-
std::tie(AUTConstDisc, AUTAddrDisc) =
1535-
extractPtrauthBlendDiscriminators(AUTDisc, CurDAG);
1500+
SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i64);
15361501

15371502
if (!Subtarget->isX16X17Safer()) {
1538-
SDValue Ops[] = {Val, AUTKey, AUTConstDisc, AUTAddrDisc};
1503+
SDValue Ops[] = {Val, AUTKey, Zero, AUTDisc};
15391504

15401505
SDNode *AUT =
15411506
CurDAG->getMachineNode(AArch64::AUTxMxN, DL, MVT::i64, MVT::i64, Ops);
15421507
ReplaceNode(N, AUT);
15431508
} else {
15441509
SDValue X16Copy = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL,
15451510
AArch64::X16, Val, SDValue());
1546-
SDValue Ops[] = {AUTKey, AUTConstDisc, AUTAddrDisc, X16Copy.getValue(1)};
1511+
SDValue Ops[] = {AUTKey, Zero, AUTDisc, X16Copy.getValue(1)};
15471512

15481513
SDNode *AUT = CurDAG->getMachineNode(AArch64::AUTx16x17, DL, MVT::i64, Ops);
15491514
ReplaceNode(N, AUT);
@@ -1565,19 +1530,13 @@ void AArch64DAGToDAGISel::SelectPtrauthResign(SDNode *N) {
15651530
AUTKey = CurDAG->getTargetConstant(AUTKeyC, DL, MVT::i64);
15661531
PACKey = CurDAG->getTargetConstant(PACKeyC, DL, MVT::i64);
15671532

1568-
SDValue AUTAddrDisc, AUTConstDisc;
1569-
std::tie(AUTConstDisc, AUTAddrDisc) =
1570-
extractPtrauthBlendDiscriminators(AUTDisc, CurDAG);
1571-
1572-
SDValue PACAddrDisc, PACConstDisc;
1573-
std::tie(PACConstDisc, PACAddrDisc) =
1574-
extractPtrauthBlendDiscriminators(PACDisc, CurDAG);
1533+
SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i64);
15751534

15761535
SDValue X16Copy = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL,
15771536
AArch64::X16, Val, SDValue());
15781537

1579-
SDValue Ops[] = {AUTKey, AUTConstDisc, AUTAddrDisc, PACKey,
1580-
PACConstDisc, PACAddrDisc, X16Copy.getValue(1)};
1538+
SDValue Ops[] = {
1539+
AUTKey, Zero, AUTDisc, PACKey, Zero, PACDisc, X16Copy.getValue(1)};
15811540

15821541
SDNode *AUTPAC = CurDAG->getMachineNode(AArch64::AUTPAC, DL, MVT::i64, Ops);
15831542
ReplaceNode(N, AUTPAC);

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3274,10 +3274,20 @@ MachineBasicBlock *AArch64TargetLowering::EmitInstrWithCustomInserter(
32743274
case AArch64::MOVT_TIZ_PSEUDO:
32753275
return EmitZTInstr(MI, BB, AArch64::MOVT_TIZ, /*Op0IsDef=*/true);
32763276

3277+
case AArch64::AUT:
3278+
fixupBlendComponents(MI, BB, MI.getOperand(1), MI.getOperand(2),
3279+
&AArch64::GPR64noipRegClass);
3280+
return BB;
32773281
case AArch64::PAC:
32783282
fixupPtrauthDiscriminator(MI, BB, MI.getOperand(3), MI.getOperand(4),
32793283
&AArch64::GPR64noipRegClass);
32803284
return BB;
3285+
case AArch64::AUTPAC:
3286+
fixupBlendComponents(MI, BB, MI.getOperand(1), MI.getOperand(2),
3287+
&AArch64::GPR64noipRegClass);
3288+
fixupBlendComponents(MI, BB, MI.getOperand(4), MI.getOperand(5),
3289+
&AArch64::GPR64noipRegClass);
3290+
return BB;
32813291
}
32823292
}
32833293

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,7 @@ let Predicates = [HasPAuth] in {
21372137
let Size = 32;
21382138
let Defs = [X16,X17,NZCV];
21392139
let Uses = [X16];
2140+
let usesCustomInserter = 1;
21402141
}
21412142

21422143
def AUTxMxN : Pseudo<(outs GPR64:$AuthVal, GPR64common:$Scratch),
@@ -2187,6 +2188,7 @@ let Predicates = [HasPAuth] in {
21872188
let Size = 48;
21882189
let Defs = [X16,X17,NZCV];
21892190
let Uses = [X16];
2191+
let usesCustomInserter = 1;
21902192
}
21912193

21922194
// Materialize a signed global address, with adrp+add and PAC.

llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6725,25 +6725,15 @@ bool AArch64InstructionSelector::selectIntrinsic(MachineInstr &I,
67256725
uint64_t PACKey = I.getOperand(5).getImm();
67266726
Register PACDisc = I.getOperand(6).getReg();
67276727

6728-
Register AUTAddrDisc = AUTDisc;
6729-
uint16_t AUTConstDiscC = 0;
6730-
std::tie(AUTConstDiscC, AUTAddrDisc) =
6731-
extractPtrauthBlendDiscriminators(AUTDisc, MRI);
6732-
6733-
Register PACAddrDisc = PACDisc;
6734-
uint16_t PACConstDiscC = 0;
6735-
std::tie(PACConstDiscC, PACAddrDisc) =
6736-
extractPtrauthBlendDiscriminators(PACDisc, MRI);
6737-
67386728
MIB.buildCopy({AArch64::X16}, {ValReg});
67396729
MIB.buildInstr(TargetOpcode::IMPLICIT_DEF, {AArch64::X17}, {});
67406730
MIB.buildInstr(AArch64::AUTPAC)
67416731
.addImm(AUTKey)
6742-
.addImm(AUTConstDiscC)
6743-
.addUse(AUTAddrDisc)
6732+
.addImm(0)
6733+
.addUse(AUTDisc)
67446734
.addImm(PACKey)
6745-
.addImm(PACConstDiscC)
6746-
.addUse(PACAddrDisc)
6735+
.addImm(0)
6736+
.addUse(PACDisc)
67476737
.constrainAllUses(TII, TRI, RBI);
67486738
MIB.buildCopy({DstReg}, Register(AArch64::X16));
67496739

@@ -6757,18 +6747,13 @@ bool AArch64InstructionSelector::selectIntrinsic(MachineInstr &I,
67576747
uint64_t AUTKey = I.getOperand(3).getImm();
67586748
Register AUTDisc = I.getOperand(4).getReg();
67596749

6760-
Register AUTAddrDisc = AUTDisc;
6761-
uint16_t AUTConstDiscC = 0;
6762-
std::tie(AUTConstDiscC, AUTAddrDisc) =
6763-
extractPtrauthBlendDiscriminators(AUTDisc, MRI);
6764-
67656750
if (STI.isX16X17Safer()) {
67666751
MIB.buildCopy({AArch64::X16}, {ValReg});
67676752
MIB.buildInstr(TargetOpcode::IMPLICIT_DEF, {AArch64::X17}, {});
67686753
MIB.buildInstr(AArch64::AUTx16x17)
67696754
.addImm(AUTKey)
6770-
.addImm(AUTConstDiscC)
6771-
.addUse(AUTAddrDisc)
6755+
.addImm(0)
6756+
.addUse(AUTDisc)
67726757
.constrainAllUses(TII, TRI, RBI);
67736758
MIB.buildCopy({DstReg}, Register(AArch64::X16));
67746759
} else {
@@ -6779,8 +6764,8 @@ bool AArch64InstructionSelector::selectIntrinsic(MachineInstr &I,
67796764
.addDef(ScratchReg)
67806765
.addUse(ValReg)
67816766
.addImm(AUTKey)
6782-
.addImm(AUTConstDiscC)
6783-
.addUse(AUTAddrDisc)
6767+
.addImm(0)
6768+
.addUse(AUTDisc)
67846769
.constrainAllUses(TII, TRI, RBI);
67856770
}
67866771

0 commit comments

Comments
 (0)