#include "SystemZMachineFunctionInfo.h"
#include "SystemZTargetMachine.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
namespace {
class SystemZLDCleanup : public MachineFunctionPass {
public:
static char ID;
SystemZLDCleanup() : MachineFunctionPass(ID), TII(nullptr), MF(nullptr) {
initializeSystemZLDCleanupPass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &MF) override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
private:
bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg);
MachineInstr *ReplaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg);
MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg);
const SystemZInstrInfo *TII;
MachineFunction *MF;
};
char SystemZLDCleanup::ID = 0;
}
INITIALIZE_PASS(SystemZLDCleanup, "systemz-ld-cleanup",
"SystemZ Local Dynamic TLS Access Clean-up", false, false)
FunctionPass *llvm::createSystemZLDCleanupPass(SystemZTargetMachine &TM) {
return new SystemZLDCleanup();
}
void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired<MachineDominatorTree>();
MachineFunctionPass::getAnalysisUsage(AU);
}
bool SystemZLDCleanup::runOnMachineFunction(MachineFunction &F) {
if (skipFunction(F.getFunction()))
return false;
TII = F.getSubtarget<SystemZSubtarget>().getInstrInfo();
MF = &F;
SystemZMachineFunctionInfo* MFI = F.getInfo<SystemZMachineFunctionInfo>();
if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
return false;
}
MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
return VisitNode(DT->getRootNode(), 0);
}
bool SystemZLDCleanup::VisitNode(MachineDomTreeNode *Node,
unsigned TLSBaseAddrReg) {
MachineBasicBlock *BB = Node->getBlock();
bool Changed = false;
for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
switch (I->getOpcode()) {
case SystemZ::TLS_LDCALL:
if (TLSBaseAddrReg)
I = ReplaceTLSCall(&*I, TLSBaseAddrReg);
else
I = SetRegister(&*I, &TLSBaseAddrReg);
Changed = true;
break;
default:
break;
}
}
for (auto I = Node->begin(), E = Node->end(); I != E; ++I)
Changed |= VisitNode(*I, TLSBaseAddrReg);
return Changed;
}
MachineInstr *SystemZLDCleanup::ReplaceTLSCall(MachineInstr *I,
unsigned TLSBaseAddrReg) {
MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
TII->get(TargetOpcode::COPY), SystemZ::R2D)
.addReg(TLSBaseAddrReg);
I->eraseFromParent();
return Copy;
}
MachineInstr *SystemZLDCleanup::SetRegister(MachineInstr *I,
unsigned *TLSBaseAddrReg) {
MachineRegisterInfo &RegInfo = MF->getRegInfo();
*TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass);
MachineInstr *Next = I->getNextNode();
MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
.addReg(SystemZ::R2D);
return Copy;
}