LLVM 20.0.0git
M68kSubtarget.cpp
Go to the documentation of this file.
1//===-- M68kSubtarget.cpp - M68k Subtarget Information ----------*- C++ -*-===//
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/// \file
10/// This file implements the M68k specific subclass of TargetSubtargetInfo.
11///
12//===----------------------------------------------------------------------===//
13
14#include "M68kSubtarget.h"
18
19#include "M68k.h"
20#include "M68kMachineFunction.h"
21#include "M68kRegisterInfo.h"
22#include "M68kTargetMachine.h"
23
25#include "llvm/IR/Attributes.h"
26#include "llvm/IR/Function.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "m68k-subtarget"
34
35#define GET_SUBTARGETINFO_TARGET_DESC
36#define GET_SUBTARGETINFO_CTOR
37#include "M68kGenSubtargetInfo.inc"
38
39extern bool FixGlobalBaseReg;
40
41/// Select the M68k CPU for the given triple and cpu name.
43 if (CPU.empty() || CPU == "generic") {
44 CPU = "M68000";
45 }
46 return CPU;
47}
48
49void M68kSubtarget::anchor() {}
50
52 const M68kTargetMachine &TM)
53 : M68kGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS), TM(TM), TSInfo(),
54 InstrInfo(initializeSubtargetDependencies(CPU, TT, FS, TM)),
55 FrameLowering(*this, this->getStackAlignment()), TLInfo(TM, *this),
56 TargetTriple(TT) {
58 Legalizer.reset(new M68kLegalizerInfo(*this));
59
60 auto *RBI = new M68kRegisterBankInfo(*getRegisterInfo());
61 RegBankInfo.reset(RBI);
63}
64
66 return CallLoweringInfo.get();
67}
68
70 return InstSelector.get();
71}
72
74 return Legalizer.get();
75}
76
78 return RegBankInfo.get();
79}
80
83}
84
85bool M68kSubtarget::isLegalToCallImmediateAddr() const { return true; }
86
88 StringRef CPU, Triple TT, StringRef FS, const M68kTargetMachine &TM) {
89 std::string CPUName = selectM68kCPU(TT, CPU).str();
90
91 // Parse features string.
92 ParseSubtargetFeatures(CPUName, CPUName, FS);
93
94 // Initialize scheduling itinerary for the specified CPU.
95 InstrItins = getInstrItineraryForCPU(CPUName);
96
98
99 return *this;
100}
101
102//===----------------------------------------------------------------------===//
103// Code Model
104//
105// Key assumptions:
106// - Whenever possible we use pc-rel encoding since it is smaller(16 bit) than
107// absolute(32 bit).
108// - GOT is reachable within 16 bit offset for both Small and Medium models.
109// - Code section is reachable within 16 bit offset for both models.
110//
111// ---------------------+-------------------------+--------------------------
112// | Small | Medium
113// +-------------------------+------------+-------------
114// | Static | PIC | Static | PIC
115// ---------------------+------------+------------+------------+-------------
116// branch | pc-rel | pc-rel | pc-rel | pc-rel
117// ---------------------+------------+------------+------------+-------------
118// call global | absolute | @PLT | absolute | @PLT
119// ---------------------+------------+------------+------------+-------------
120// call internal | pc-rel | pc-rel | pc-rel | pc-rel
121// ---------------------+------------+------------+------------+-------------
122// data local | pc-rel | pc-rel | ~pc-rel | ^pc-rel
123// ---------------------+------------+------------+------------+-------------
124// data local big* | pc-rel | pc-rel | absolute | @GOTOFF
125// ---------------------+------------+------------+------------+-------------
126// data global | pc-rel | @GOTPCREL | ~pc-rel | @GOTPCREL
127// ---------------------+------------+------------+------------+-------------
128// data global big* | pc-rel | @GOTPCREL | absolute | @GOTPCREL
129// ---------------------+------------+------------+------------+-------------
130// | Large |
131// +-------------------------+
132// | Static | PIC |
133// ---------------------+------------+------------+
134// branch | absolute | pc-rel |
135// ---------------------+------------+------------+
136// call global | absolute | @PLT |
137// ---------------------+------------+------------+
138// call internal | absolute | pc-rel |
139// ---------------------+------------+------------+
140// data local | absolute | @GOTOFF |
141// ---------------------+------------+------------+
142// data local big* | absolute | @GOTOFF |
143// ---------------------+------------+------------+
144// data global | absolute | @GOTOFF |
145// ---------------------+------------+------------+
146// data global big* | absolute | @GOTOFF |
147// ---------------------+------------+------------+
148//
149// * Big data potentially cannot be reached within 16 bit offset and requires
150// special handling for old(x00 and x10) CPUs. Normally these symbols go into
151// separate .ldata section which mapped after normal .data and .text, but I
152// don't really know how this must be done for M68k atm... will try to dig
153// this info out from GCC. For now CPUs prior to M68020 will use static ref
154// for Static Model and @GOT based references for PIC.
155//
156// ~ These are absolute for older CPUs for now.
157// ^ These are @GOTOFF for older CPUs for now.
158//===----------------------------------------------------------------------===//
159
160/// Classify a blockaddress reference for the current subtarget according to how
161/// we should reference it in a non-pcrel context.
163 switch (TM.getCodeModel()) {
164 default:
165 llvm_unreachable("Unsupported code model");
166 case CodeModel::Small:
168 case CodeModel::Medium: {
170 }
171 case CodeModel::Large: {
172 if (isPositionIndependent()) {
174 } else {
176 }
177 }
178 }
179}
180
181unsigned char
183 switch (TM.getCodeModel()) {
184 default:
185 llvm_unreachable("Unsupported code model");
186 case CodeModel::Small:
187 case CodeModel::Kernel: {
189 }
190 case CodeModel::Medium: {
191 if (isPositionIndependent()) {
192 // On M68020 and better we can fit big any data offset into dips field.
193 if (atLeastM68020()) {
195 }
196 // Otherwise we could check the data size and make sure it will fit into
197 // 16 bit offset. For now we will be conservative and go with @GOTOFF
198 return M68kII::MO_GOTOFF;
199 } else {
200 if (atLeastM68020()) {
202 }
204 }
205 }
206 case CodeModel::Large: {
207 if (isPositionIndependent()) {
208 return M68kII::MO_GOTOFF;
209 } else {
211 }
212 }
213 }
214}
215
217 if (TM.shouldAssumeDSOLocal(nullptr))
218 return classifyLocalReference(nullptr);
219
221 return M68kII::MO_GOTPCREL;
222
223 return M68kII::MO_GOT;
224}
225
226unsigned char
228 return classifyGlobalReference(GV, *GV->getParent());
229}
230
232 const Module &M) const {
233 if (TM.shouldAssumeDSOLocal(GV))
234 return classifyLocalReference(GV);
235
236 switch (TM.getCodeModel()) {
237 default:
238 llvm_unreachable("Unsupported code model");
239 case CodeModel::Small:
240 case CodeModel::Kernel: {
242 return M68kII::MO_GOTPCREL;
244 }
245 case CodeModel::Medium: {
247 return M68kII::MO_GOTPCREL;
248
249 if (atLeastM68020())
251
253 }
254 case CodeModel::Large: {
256 return M68kII::MO_GOTOFF;
257
259 }
260 }
261}
262
264 if (isPositionIndependent()) {
265 // The only time we want to use GOTOFF(used when with EK_Custom32) is when
266 // the potential delta between the jump target and table base can be larger
267 // than displacement field, which is True for older CPUs(16 bit disp)
268 // in Medium model(can have large data way beyond 16 bit).
272
274 }
275
276 // In non-pic modes, just use the address of a block.
278}
279
280unsigned char
283}
284
285unsigned char
287 const Module &M) const {
288 // local always use pc-rel referencing
289 if (TM.shouldAssumeDSOLocal(GV))
290 return M68kII::MO_NO_FLAG;
291
292 // If the function is marked as non-lazy, generate an indirect call
293 // which loads from the GOT directly. This avoids run-time overhead
294 // at the cost of eager binding.
295 auto *F = dyn_cast_or_null<Function>(GV);
296 if (F && F->hasFnAttribute(Attribute::NonLazyBind)) {
297 return M68kII::MO_GOTPCREL;
298 }
299
300 // Ensure that we don't emit PLT relocations when in non-pic modes.
302}
This file contains the simple types necessary to represent the attributes associated with functions a...
This file implements the lowering of LLVM calls to machine code calls for GlobalISel.
This file declares the targeting of the MachineLegalizer class for M68k.
This file declares the M68k specific subclass of MachineFunctionInfo.
This file declares the targeting of the RegisterBankInfo class for M68k.
This file contains the M68k implementation of the TargetRegisterInfo class.
bool FixGlobalBaseReg
static StringRef selectM68kCPU(Triple TT, StringRef CPU)
Select the M68k CPU for the given triple and cpu name.
This file declares the M68k specific subclass of TargetSubtargetInfo.
This file declares the M68k specific subclass of TargetMachine.
This file contains the entry points for global functions defined in the M68k target library,...
#define F(x, y, z)
Definition: MD5.cpp:55
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
This class provides the information for the target register banks.
unsigned char classifyExternalReference(const Module &M) const
Classify a external variable reference for the current subtarget according to how we should reference...
InstrItineraryData InstrItins
Definition: M68kSubtarget.h:59
const M68kTargetLowering * getTargetLowering() const override
M68kSubtarget(const Triple &TT, StringRef CPU, StringRef FS, const M68kTargetMachine &_TM)
This constructor initializes the data members to match that of the specified triple.
unsigned char classifyBlockAddressReference() const
Classify a blockaddress reference for the current subtarget according to how we should reference it i...
const LegalizerInfo * getLegalizerInfo() const override
const RegisterBankInfo * getRegBankInfo() const override
bool isLegalToCallImmediateAddr() const
Return true if the subtarget allows calls to immediate address.
std::unique_ptr< CallLowering > CallLoweringInfo
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
Parses features string setting specified subtarget options.
const M68kTargetMachine & TM
Definition: M68kSubtarget.h:64
unsigned char classifyGlobalReference(const GlobalValue *GV, const Module &M) const
Classify a global variable reference for the current subtarget according to how we should reference i...
const CallLowering * getCallLowering() const override
unsigned getJumpTableEncoding() const
InstructionSelector * getInstructionSelector() const override
std::unique_ptr< RegisterBankInfo > RegBankInfo
unsigned char classifyLocalReference(const GlobalValue *GV) const
Classify a global variable reference for the current subtarget according to how we should reference i...
unsigned stackAlignment
The minimum alignment known to hold of the stack frame on entry to the function and which must be mai...
Definition: M68kSubtarget.h:73
const M68kRegisterInfo * getRegisterInfo() const override
bool atLeastM68020() const
Definition: M68kSubtarget.h:89
bool isPositionIndependent() const
M68kSubtarget & initializeSubtargetDependencies(StringRef CPU, Triple TT, StringRef FS, const M68kTargetMachine &TM)
unsigned char classifyGlobalFunctionReference(const GlobalValue *GV, const Module &M) const
Classify a global function reference for the current subtarget.
std::unique_ptr< InstructionSelector > InstSelector
@ EK_LabelDifference32
EK_LabelDifference32 - Each entry is the address of the block minus the address of the jump table.
@ EK_Custom32
EK_Custom32 - Each entry is a 32-bit value that is custom lowered by the TargetLowering::LowerCustomJ...
@ EK_BlockAddress
EK_BlockAddress - Each entry is a plain address of block, e.g.: .word LBB123.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Holds all the information related to register banks.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:229
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
bool isPositionIndependent() const
bool shouldAssumeDSOLocal(const GlobalValue *GV) const
CodeModel::Model getCodeModel() const
Returns the code model.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_GOTOFF
On a symbol operand this indicates that the immediate is the offset to the location of the symbol nam...
Definition: M68kBaseInfo.h:147
@ MO_PLT
On a symbol operand this indicates that the immediate is offset to the PLT entry of symbol name from ...
Definition: M68kBaseInfo.h:159
@ MO_GOT
On a symbol operand this indicates that the immediate is the offset to the GOT entry for the symbol n...
Definition: M68kBaseInfo.h:141
@ MO_ABSOLUTE_ADDRESS
On a symbol operand this indicates that the immediate is the absolute address of the symbol.
Definition: M68kBaseInfo.h:131
@ MO_GOTPCREL
On a symbol operand this indicates that the immediate is offset to the GOT entry for the symbol name ...
Definition: M68kBaseInfo.h:153
@ MO_PC_RELATIVE_ADDRESS
On a symbol operand this indicates that the immediate is the pc-relative address of the symbol.
Definition: M68kBaseInfo.h:135
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
InstructionSelector * createM68kInstructionSelector(const M68kTargetMachine &TM, const M68kSubtarget &Subtarget, const M68kRegisterBankInfo &RBI)