Skip to content

Commit 656bcea

Browse files
committed
[AArch64][PAC] Expand blend(reg, imm) operation in aarch64-pauth pass
In preparation for implementing code generation for more @llvm.ptrauth.* intrinsics, move the expansion of blend(register, small integer) variant of @llvm.ptrauth.blend to the AArch64PointerAuth pass, where most other PAuth-related code generation takes place.
1 parent 17168f7 commit 656bcea

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,6 +1570,9 @@ def PAUTH_PROLOGUE : Pseudo<(outs), (ins), []>, Sched<[]>;
15701570
def PAUTH_EPILOGUE : Pseudo<(outs), (ins), []>, Sched<[]>;
15711571
}
15721572

1573+
def PAUTH_BLEND : Pseudo<(outs GPR64:$disc),
1574+
(ins GPR64:$addr_disc, i32imm:$int_disc), []>, Sched<[]>;
1575+
15731576
// These pointer authentication instructions require armv8.3a
15741577
let Predicates = [HasPAuth] in {
15751578

@@ -9188,12 +9191,10 @@ let Predicates = [HasMOPS, HasMTE], Defs = [NZCV], Size = 12, mayLoad = 0, maySt
91889191
//-----------------------------------------------------------------------------
91899192
// v8.3 Pointer Authentication late patterns
91909193

9191-
let Predicates = [HasPAuth] in {
91929194
def : Pat<(int_ptrauth_blend GPR64:$Rd, imm64_0_65535:$imm),
9193-
(MOVKXi GPR64:$Rd, (trunc_imm imm64_0_65535:$imm), 48)>;
9195+
(PAUTH_BLEND GPR64:$Rd, (trunc_imm imm64_0_65535:$imm))>;
91949196
def : Pat<(int_ptrauth_blend GPR64:$Rd, GPR64:$Rn),
91959197
(BFMXri GPR64:$Rd, GPR64:$Rn, 16, 15)>;
9196-
}
91979198

91989199
//-----------------------------------------------------------------------------
91999200

llvm/lib/Target/AArch64/AArch64PointerAuth.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class AArch64PointerAuth : public MachineFunctionPass {
4646
void authenticateLR(MachineFunction &MF,
4747
MachineBasicBlock::iterator MBBI) const;
4848

49+
/// Stores blend(AddrDisc, IntDisc) to the Result register.
50+
void emitBlend(MachineBasicBlock::iterator MBBI, Register Result,
51+
Register AddrDisc, unsigned IntDisc) const;
52+
53+
/// Expands PAUTH_BLEND pseudo instruction.
54+
void expandPAuthBlend(MachineBasicBlock::iterator MBBI) const;
55+
4956
bool checkAuthenticatedLR(MachineBasicBlock::iterator TI) const;
5057
};
5158

@@ -295,6 +302,32 @@ bool AArch64PointerAuth::checkAuthenticatedLR(
295302
return true;
296303
}
297304

305+
void AArch64PointerAuth::emitBlend(MachineBasicBlock::iterator MBBI,
306+
Register Result, Register AddrDisc,
307+
unsigned IntDisc) const {
308+
MachineBasicBlock &MBB = *MBBI->getParent();
309+
DebugLoc DL = MBBI->getDebugLoc();
310+
311+
if (Result != AddrDisc)
312+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), Result)
313+
.addReg(AArch64::XZR)
314+
.addReg(AddrDisc)
315+
.addImm(0);
316+
317+
BuildMI(MBB, MBBI, DL, TII->get(AArch64::MOVKXi), Result)
318+
.addReg(Result)
319+
.addImm(IntDisc)
320+
.addImm(48);
321+
}
322+
323+
void AArch64PointerAuth::expandPAuthBlend(
324+
MachineBasicBlock::iterator MBBI) const {
325+
Register ResultReg = MBBI->getOperand(0).getReg();
326+
Register AddrDisc = MBBI->getOperand(1).getReg();
327+
unsigned IntDisc = MBBI->getOperand(2).getImm();
328+
emitBlend(MBBI, ResultReg, AddrDisc, IntDisc);
329+
}
330+
298331
bool AArch64PointerAuth::runOnMachineFunction(MachineFunction &MF) {
299332
const auto *MFnI = MF.getInfo<AArch64FunctionInfo>();
300333

@@ -326,6 +359,7 @@ bool AArch64PointerAuth::runOnMachineFunction(MachineFunction &MF) {
326359
break;
327360
case AArch64::PAUTH_PROLOGUE:
328361
case AArch64::PAUTH_EPILOGUE:
362+
case AArch64::PAUTH_BLEND:
329363
assert(!MI.isBundled());
330364
PAuthPseudoInstrs.push_back(MI.getIterator());
331365
break;
@@ -342,6 +376,9 @@ bool AArch64PointerAuth::runOnMachineFunction(MachineFunction &MF) {
342376
authenticateLR(MF, It);
343377
HasAuthenticationInstrs = true;
344378
break;
379+
case AArch64::PAUTH_BLEND:
380+
expandPAuthBlend(It);
381+
break;
345382
default:
346383
llvm_unreachable("Unhandled opcode");
347384
}

0 commit comments

Comments
 (0)