LLVM 24.0.0git
X86GlobalBaseReg.cpp
Go to the documentation of this file.
1//===- X86GlobalBaseReg.cpp - PIC Global Base Register Initialization -----===//
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 file contains the pass that initializes the PIC global base register
10// for x86-32.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86.h"
15#include "X86InstrInfo.h"
17#include "X86Subtarget.h"
18#include "X86TargetMachine.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "x86-global-base-reg"
26
27namespace {
28class X86GlobalBaseRegLegacy : public MachineFunctionPass {
29public:
30 static char ID;
31 X86GlobalBaseRegLegacy() : MachineFunctionPass(ID) {}
32
33 bool runOnMachineFunction(MachineFunction &MF) override;
34
35 StringRef getPassName() const override {
36 return "X86 PIC Global Base Reg Initialization";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesCFG();
42 }
43};
44} // end anonymous namespace
45
46char X86GlobalBaseRegLegacy::ID = 0;
47
49 return new X86GlobalBaseRegLegacy();
50}
51
53 const X86TargetMachine *TM =
54 static_cast<const X86TargetMachine *>(&MF.getTarget());
55 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
56
57 // Only emit a global base reg in PIC mode.
58 if (!TM->isPositionIndependent())
59 return false;
60
62 Register GlobalBaseReg = X86FI->getGlobalBaseReg();
63
64 // If we didn't need a GlobalBaseReg, don't insert code.
65 if (GlobalBaseReg == 0)
66 return false;
67
68 // Insert the set of GlobalBaseReg into the first MBB of the function
69 MachineBasicBlock &FirstMBB = MF.front();
71 DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
73 const X86InstrInfo *TII = STI.getInstrInfo();
74
75 Register PC;
76 if (STI.isPICStyleGOT())
77 PC = RegInfo.createVirtualRegister(&X86::GR32RegClass);
78 else
79 PC = GlobalBaseReg;
80
81 if (STI.is64Bit()) {
82 if (TM->getCodeModel() == CodeModel::Large) {
83 // In the large code model, we are aiming for this code, though the
84 // register allocation may vary:
85 // leaq .LN$pb(%rip), %rax
86 // movq $_GLOBAL_OFFSET_TABLE_ - .LN$pb, %rcx
87 // addq %rcx, %rax
88 // RAX now holds address of _GLOBAL_OFFSET_TABLE_.
89 Register PBReg = RegInfo.createVirtualRegister(&X86::GR64RegClass);
90 Register GOTReg = RegInfo.createVirtualRegister(&X86::GR64RegClass);
91 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::LEA64r), PBReg)
92 .addReg(X86::RIP)
93 .addImm(0)
94 .addReg(0)
96 .addReg(0);
97 std::prev(MBBI)->setPreInstrSymbol(MF, MF.getPICBaseSymbol());
98 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOV64ri), GOTReg)
99 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
101 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD64rr), PC)
102 .addReg(PBReg, RegState::Kill)
103 .addReg(GOTReg, RegState::Kill);
104 } else {
105 // In other code models, use a RIP-relative LEA to materialize the
106 // GOT.
107 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::LEA64r), PC)
108 .addReg(X86::RIP)
109 .addImm(0)
110 .addReg(0)
111 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_")
112 .addReg(0);
113 }
114 } else {
115 // Operand of MovePCtoStack is completely ignored by asm printer. It's
116 // only used in JIT code emission as displacement to pc.
117 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
118
119 // If we're using vanilla 'GOT' PIC style, we should use relative
120 // addressing not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
121 if (STI.isPICStyleGOT()) {
122 // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel],
123 // %some_register
124 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
125 .addReg(PC)
126 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
128 }
129 }
130
131 return true;
132}
133
134bool X86GlobalBaseRegLegacy::runOnMachineFunction(MachineFunction &MF) {
135 return initGlobalBaseReg(MF);
136}
137
138PreservedAnalyses
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
static bool initGlobalBaseReg(MachineFunction &MF)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MCSymbol * getPICBaseSymbol() const
getPICBaseSymbol - Return a function-local symbol to represent the PIC base.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
LLVM_ABI void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool isPositionIndependent() const
CodeModel::Model getCodeModel() const
Returns the code model.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
bool isPICStyleGOT() const
const X86InstrInfo * getInstrInfo() const override
@ MO_GOT_ABSOLUTE_ADDRESS
MO_GOT_ABSOLUTE_ADDRESS - On a symbol operand, this represents a relocation of: SYMBOL_LABEL + [.
@ MO_PIC_BASE_OFFSET
MO_PIC_BASE_OFFSET - On a symbol operand this indicates that the immediate should get the value of th...
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createX86GlobalBaseRegLegacyPass()