LLVM  14.0.0git
SystemZLDCleanup.cpp
Go to the documentation of this file.
1 //===-- SystemZLDCleanup.cpp - Clean up local-dynamic TLS accesses --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass combines multiple accesses to local-dynamic TLS variables so that
10 // the TLS base address for the module is only fetched once per execution path
11 // through the function.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "SystemZTargetMachine.h"
24 
25 using namespace llvm;
26 
27 namespace {
28 
29 class SystemZLDCleanup : public MachineFunctionPass {
30 public:
31  static char ID;
32  SystemZLDCleanup(const SystemZTargetMachine &tm)
33  : MachineFunctionPass(ID), TII(nullptr), MF(nullptr) {}
34 
35  StringRef getPassName() const override {
36  return "SystemZ Local Dynamic TLS Access Clean-up";
37  }
38 
39  bool runOnMachineFunction(MachineFunction &MF) override;
40  void getAnalysisUsage(AnalysisUsage &AU) const override;
41 
42 private:
43  bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg);
44  MachineInstr *ReplaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg);
45  MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg);
46 
47  const SystemZInstrInfo *TII;
48  MachineFunction *MF;
49 };
50 
51 char SystemZLDCleanup::ID = 0;
52 
53 } // end anonymous namespace
54 
56  return new SystemZLDCleanup(TM);
57 }
58 
59 void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage &AU) const {
60  AU.setPreservesCFG();
63 }
64 
65 bool SystemZLDCleanup::runOnMachineFunction(MachineFunction &F) {
66  if (skipFunction(F.getFunction()))
67  return false;
68 
69  TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
70  MF = &F;
71 
73  if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
74  // No point folding accesses if there isn't at least two.
75  return false;
76  }
77 
78  MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
79  return VisitNode(DT->getRootNode(), 0);
80 }
81 
82 // Visit the dominator subtree rooted at Node in pre-order.
83 // If TLSBaseAddrReg is non-null, then use that to replace any
84 // TLS_LDCALL instructions. Otherwise, create the register
85 // when the first such instruction is seen, and then use it
86 // as we encounter more instructions.
87 bool SystemZLDCleanup::VisitNode(MachineDomTreeNode *Node,
88  unsigned TLSBaseAddrReg) {
89  MachineBasicBlock *BB = Node->getBlock();
90  bool Changed = false;
91 
92  // Traverse the current block.
93  for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
94  switch (I->getOpcode()) {
96  if (TLSBaseAddrReg)
97  I = ReplaceTLSCall(&*I, TLSBaseAddrReg);
98  else
99  I = SetRegister(&*I, &TLSBaseAddrReg);
100  Changed = true;
101  break;
102  default:
103  break;
104  }
105  }
106 
107  // Visit the children of this block in the dominator tree.
108  for (auto I = Node->begin(), E = Node->end(); I != E; ++I)
109  Changed |= VisitNode(*I, TLSBaseAddrReg);
110 
111  return Changed;
112 }
113 
114 // Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg,
115 // returning the new instruction.
116 MachineInstr *SystemZLDCleanup::ReplaceTLSCall(MachineInstr *I,
117  unsigned TLSBaseAddrReg) {
118  // Insert a Copy from TLSBaseAddrReg to R2.
119  MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
120  TII->get(TargetOpcode::COPY), SystemZ::R2D)
121  .addReg(TLSBaseAddrReg);
122 
123  // Erase the TLS_LDCALL instruction.
124  I->eraseFromParent();
125 
126  return Copy;
127 }
128 
129 // Create a virtual register in *TLSBaseAddrReg, and populate it by
130 // inserting a copy instruction after I. Returns the new instruction.
131 MachineInstr *SystemZLDCleanup::SetRegister(MachineInstr *I,
132  unsigned *TLSBaseAddrReg) {
133  // Create a virtual register for the TLS base address.
134  MachineRegisterInfo &RegInfo = MF->getRegInfo();
135  *TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass);
136 
137  // Insert a copy from R2 to TLSBaseAddrReg.
138  MachineInstr *Next = I->getNextNode();
139  MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
140  TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
141  .addReg(SystemZ::R2D);
142 
143  return Copy;
144 }
145 
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MachineRegisterInfo::createVirtualRegister
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Definition: MachineRegisterInfo.cpp:158
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::createSystemZLDCleanupPass
FunctionPass * createSystemZLDCleanupPass(SystemZTargetMachine &TM)
Definition: SystemZLDCleanup.cpp:55
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
TargetInstrInfo.h
llvm::MachineDominatorTree::getRootNode
MachineDomTreeNode * getRootNode() const
Definition: MachineDominators.h:100
llvm::SystemZInstrInfo
Definition: SystemZInstrInfo.h:174
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:102
F
#define F(x, y, z)
Definition: MD5.cpp:56
MachineRegisterInfo.h
TargetMachine.h
llvm::SystemZISD::TLS_LDCALL
@ TLS_LDCALL
Definition: SystemZISelLowering.h:40
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::SystemZMachineFunctionInfo::getNumLocalDynamicTLSAccesses
unsigned getNumLocalDynamicTLSAccesses() const
Definition: SystemZMachineFunctionInfo.h:94
SystemZTargetMachine.h
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
SystemZMachineFunctionInfo.h
llvm::codeview::FrameCookieKind::Copy
@ Copy
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::SystemZTargetMachine
Definition: SystemZTargetMachine.h:27
MachineFunctionPass.h
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::DomTreeNodeBase
Base class for the actual dominator tree node.
Definition: LiveIntervalCalc.h:24
llvm::SystemZMachineFunctionInfo
Definition: SystemZMachineFunctionInfo.h:27
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::MachineDominatorTree
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Definition: MachineDominators.h:45
TargetRegisterInfo.h
MachineDominators.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37