LLVM 23.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"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "x86-global-base-reg"
27
28namespace {
29class X86GlobalBaseRegLegacy : public MachineFunctionPass {
30public:
31 static char ID;
32 X86GlobalBaseRegLegacy() : MachineFunctionPass(ID) {}
33
34 bool runOnMachineFunction(MachineFunction &MF) override;
35
36 StringRef getPassName() const override {
37 return "X86 PIC Global Base Reg Initialization";
38 }
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 AU.setPreservesCFG();
43 }
44};
45} // end anonymous namespace
46
47char X86GlobalBaseRegLegacy::ID = 0;
48
50 return new X86GlobalBaseRegLegacy();
51}
52
54 const X86TargetMachine *TM =
55 static_cast<const X86TargetMachine *>(&MF.getTarget());
56 const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
57
58 // Only emit a global base reg in PIC mode.
59 if (!TM->isPositionIndependent())
60 return false;
61
63 Register GlobalBaseReg = X86FI->getGlobalBaseReg();
64
65 // If we didn't need a GlobalBaseReg, don't insert code.
66 if (GlobalBaseReg == 0)
67 return false;
68
69 // Insert the set of GlobalBaseReg into the first MBB of the function
70 MachineBasicBlock &FirstMBB = MF.front();
72 DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
74 const X86InstrInfo *TII = STI.getInstrInfo();
75
76 Register PC;
77 if (STI.isPICStyleGOT())
78 PC = RegInfo.createVirtualRegister(&X86::GR32RegClass);
79 else
80 PC = GlobalBaseReg;
81
82 if (STI.is64Bit()) {
83 if (TM->getCodeModel() == CodeModel::Large) {
84 // In the large code model, we are aiming for this code, though the
85 // register allocation may vary:
86 // leaq .LN$pb(%rip), %rax
87 // movq $_GLOBAL_OFFSET_TABLE_ - .LN$pb, %rcx
88 // addq %rcx, %rax
89 // RAX now holds address of _GLOBAL_OFFSET_TABLE_.
90 Register PBReg = RegInfo.createVirtualRegister(&X86::GR64RegClass);
91 Register GOTReg = RegInfo.createVirtualRegister(&X86::GR64RegClass);
92 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::LEA64r), PBReg)
93 .addReg(X86::RIP)
94 .addImm(0)
95 .addReg(0)
97 .addReg(0);
98 std::prev(MBBI)->setPreInstrSymbol(MF, MF.getPICBaseSymbol());
99 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOV64ri), GOTReg)
100 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
102 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD64rr), PC)
103 .addReg(PBReg, RegState::Kill)
104 .addReg(GOTReg, RegState::Kill);
105 } else {
106 // In other code models, use a RIP-relative LEA to materialize the
107 // GOT.
108 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::LEA64r), PC)
109 .addReg(X86::RIP)
110 .addImm(0)
111 .addReg(0)
112 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_")
113 .addReg(0);
114 }
115 } else {
116 // Operand of MovePCtoStack is completely ignored by asm printer. It's
117 // only used in JIT code emission as displacement to pc.
118 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
119
120 // If we're using vanilla 'GOT' PIC style, we should use relative
121 // addressing not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
122 if (STI.isPICStyleGOT()) {
123 // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel],
124 // %some_register
125 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
126 .addReg(PC)
127 .addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
129 }
130 }
131
132 return true;
133}
134
135bool X86GlobalBaseRegLegacy::runOnMachineFunction(MachineFunction &MF) {
136 return initGlobalBaseReg(MF);
137}
138
139PreservedAnalyses
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:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:123
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ 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.
Definition Types.h:26
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()