Skip to content

Commit 6631434

Browse files
committed
Refactor
1 parent 7af97c8 commit 6631434

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 59 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,44 +3353,67 @@ AArch64TargetLowering::EmitGetSMESaveSize(MachineInstr &MI,
33533353
return BB;
33543354
}
33553355

3356+
// Helper function to find the instruction that defined a virtual register,
3357+
// stripping and accumulating optional offset.
3358+
// If unable to find such instruction, returns nullptr (Offset is unspecified).
3359+
static MachineInstr *stripAndAccumulateOffset(const MachineRegisterInfo &MRI,
3360+
Register Reg, int64_t &Offset) {
3361+
Offset = 0;
3362+
while (Reg.isVirtual()) {
3363+
MachineInstr *DefMI = MRI.getVRegDef(Reg);
3364+
assert(DefMI && "Virtual register definition not found");
3365+
unsigned Opcode = DefMI->getOpcode();
3366+
3367+
if (Opcode == AArch64::COPY) {
3368+
Reg = DefMI->getOperand(1).getReg();
3369+
continue;
3370+
}
3371+
3372+
// If this is neither a copy, nor inc/dec instruction, we are done.
3373+
if (Opcode != AArch64::ADDXri && Opcode != AArch64::SUBXri)
3374+
return DefMI;
3375+
// Inc/dec with shifted immediates are not handled.
3376+
if (DefMI->getOperand(3).getImm() != 0)
3377+
return DefMI;
3378+
3379+
int64_t Imm = DefMI->getOperand(2).getImm();
3380+
Offset += (Opcode == AArch64::ADDXri) ? Imm : -Imm;
3381+
3382+
Reg = DefMI->getOperand(1).getReg();
3383+
}
3384+
return nullptr;
3385+
}
3386+
3387+
static std::pair<Register, unsigned>
3388+
detectBlendComponents(const MachineRegisterInfo &MRI, Register Reg) {
3389+
int64_t Offset = 0;
3390+
MachineInstr *MaybeBlend = stripAndAccumulateOffset(MRI, Reg, Offset);
3391+
// This should be a plain copy, without adding any offset.
3392+
if (!MaybeBlend || Offset != 0)
3393+
return std::make_pair(Reg, 0);
3394+
3395+
// Detect blend(addr, imm) which is lowered as MOVK addr, #imm, 48.
3396+
if (MaybeBlend->getOpcode() != AArch64::MOVKXi ||
3397+
MaybeBlend->getOperand(3).getImm() != 48)
3398+
return std::make_pair(Reg, 0);
3399+
3400+
return std::make_pair(MaybeBlend->getOperand(1).getReg(),
3401+
MaybeBlend->getOperand(2).getImm());
3402+
}
3403+
33563404
MachineBasicBlock *
33573405
AArch64TargetLowering::tryRewritingPAC(MachineInstr &MI,
33583406
MachineBasicBlock *BB) const {
33593407
const TargetInstrInfo *TII = Subtarget->getInstrInfo();
33603408
MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
33613409
const DebugLoc &DL = MI.getDebugLoc();
33623410

3363-
// Find the unique register definition, skipping copies.
3364-
auto GetUniqueDef = [&MRI](Register Reg) {
3365-
for (;;) {
3366-
MachineInstr *Def = MRI.getUniqueVRegDef(Reg);
3367-
if (!Def || Def->getOpcode() != AArch64::COPY)
3368-
return Def;
3369-
3370-
Reg = Def->getOperand(1).getReg();
3371-
}
3372-
};
3373-
// Find the unique register definition, skipping copies and increments.
3374-
auto GetUniqueDefPlusOffset =
3375-
[GetUniqueDef](Register Reg, int64_t &Offset) -> MachineInstr * {
3376-
for (;;) {
3377-
MachineInstr *Def = GetUniqueDef(Reg);
3378-
if (!Def || Def->getOpcode() != AArch64::ADDXri)
3379-
return Def;
3380-
3381-
if (Def->getOperand(3).getImm() != 0)
3382-
return nullptr; // shifted immediates are not handled
3383-
Reg = Def->getOperand(1).getReg();
3384-
Offset += Def->getOperand(2).getImm();
3385-
}
3386-
};
3387-
33883411
// Try to find a known address-setting instruction, accumulating the offset
33893412
// along the way. If no known pattern is found, keep everything as-is.
33903413

33913414
int64_t AddrOffset = 0;
33923415
MachineInstr *AddrDefInstr =
3393-
GetUniqueDefPlusOffset(MI.getOperand(1).getReg(), AddrOffset);
3416+
stripAndAccumulateOffset(MRI, MI.getOperand(1).getReg(), AddrOffset);
33943417
if (!AddrDefInstr)
33953418
return BB;
33963419

@@ -3407,32 +3430,25 @@ AArch64TargetLowering::tryRewritingPAC(MachineInstr &MI,
34073430
const GlobalValue *GV = AddrOp.getGlobal();
34083431
AddrOffset += AddrOp.getOffset();
34093432

3410-
// Detect discriminator blend computation, if any.
3411-
Register RegDisc = isPACWithZeroDisc(MI.getOpcode())
3412-
? AArch64::XZR
3413-
: MI.getOperand(2).getReg();
3414-
unsigned IntDisc = 0;
3415-
MachineInstr *MaybeBlendDef =
3416-
RegDisc == AArch64::XZR ? nullptr : GetUniqueDef(RegDisc);
3417-
if (MaybeBlendDef && MaybeBlendDef->getOpcode() == AArch64::MOVKXi &&
3418-
MaybeBlendDef->getOperand(3).getImm() == 48) {
3419-
RegDisc = MaybeBlendDef->getOperand(1).getReg();
3420-
IntDisc = MaybeBlendDef->getOperand(2).getImm();
3421-
}
3433+
// Analyze the discriminator operand.
3434+
Register OriginalDisc = isPACWithZeroDisc(MI.getOpcode())
3435+
? AArch64::XZR
3436+
: MI.getOperand(2).getReg();
3437+
auto [AddrDisc, IntDisc] = detectBlendComponents(MRI, OriginalDisc);
34223438

34233439
// MOVaddrPAC and LOADgotPAC pseudos are expanded so that they use X16/X17
34243440
// internally, thus their restrictions on the register class of $AddrDisc
3425-
// operand are stricter than those of real PAC* instructions.
3426-
if (RegDisc != AArch64::XZR) {
3441+
// operand are stricter than those of MOVKXi and PAC* instructions.
3442+
if (AddrDisc != AArch64::XZR) {
34273443
Register TmpReg = MRI.createVirtualRegister(&AArch64::GPR64noipRegClass);
3428-
BuildMI(*BB, MI, DL, TII->get(AArch64::COPY), TmpReg).addReg(RegDisc);
3429-
RegDisc = TmpReg;
3444+
BuildMI(*BB, MI, DL, TII->get(AArch64::COPY), TmpReg).addReg(AddrDisc);
3445+
AddrDisc = TmpReg;
34303446
}
34313447

34323448
BuildMI(*BB, MI, DL, TII->get(NewOpcode))
34333449
.addGlobalAddress(GV, AddrOffset, TargetFlags)
34343450
.addImm(getKeyForPACOpcode(MI.getOpcode()))
3435-
.addReg(RegDisc)
3451+
.addReg(AddrDisc)
34363452
.addImm(IntDisc);
34373453

34383454
BuildMI(*BB, MI, DL, TII->get(AArch64::COPY), MI.getOperand(0).getReg())

llvm/lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ class AArch64TargetLowering : public TargetLowering {
679679
MachineBasicBlock *BB) const;
680680
MachineBasicBlock *EmitGetSMESaveSize(MachineInstr &MI,
681681
MachineBasicBlock *BB) const;
682+
682683
MachineBasicBlock *tryRewritingPAC(MachineInstr &MI,
683684
MachineBasicBlock *BB) const;
684685

llvm/lib/Target/AArch64/AArch64InstrInfo.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,8 @@ let Predicates = [HasPAuth] in {
19951995

19961996
def LOADgotAUTH : Pseudo<(outs GPR64common:$dst), (ins i64imm:$addr), []>,
19971997
Sched<[WriteI, ReadI]> {
1998-
// Make it possible to eliminate dead instruction after folding it into LOADgotPAC.
1998+
// Make it possible to eliminate dead instruction after folding it
1999+
// into LOADgotPAC.
19992000
let hasSideEffects = 0;
20002001
let Defs = [X16,X17,NZCV];
20012002
let Size = 44;

0 commit comments

Comments
 (0)