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 | @PLT | @PLT | @PLT | @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//
131// * Big data potentially cannot be reached within 16 bit offset and requires
132// special handling for old(x00 and x10) CPUs. Normally these symbols go into
133// separate .ldata section which mapped after normal .data and .text, but I
134// don't really know how this must be done for M68k atm... will try to dig
135// this info out from GCC. For now CPUs prior to M68020 will use static ref
136// for Static Model and @GOT based references for PIC.
137//
138// ~ These are absolute for older CPUs for now.
139// ^ These are @GOTOFF for older CPUs for now.
140//===----------------------------------------------------------------------===//
141
142/// Classify a blockaddress reference for the current subtarget according to how
143/// we should reference it in a non-pcrel context.
145 // Unless we start to support Large Code Model branching is always pc-rel
147}
148
149unsigned char
151 switch (TM.getCodeModel()) {
152 default:
153 llvm_unreachable("Unsupported code model");
154 case CodeModel::Small:
155 case CodeModel::Kernel: {
157 }
158 case CodeModel::Medium: {
159 if (isPositionIndependent()) {
160 // On M68020 and better we can fit big any data offset into dips field.
161 if (atLeastM68020()) {
163 }
164 // Otherwise we could check the data size and make sure it will fit into
165 // 16 bit offset. For now we will be conservative and go with @GOTOFF
166 return M68kII::MO_GOTOFF;
167 } else {
168 if (atLeastM68020()) {
170 }
172 }
173 }
174 }
175}
176
178 if (TM.shouldAssumeDSOLocal(nullptr))
179 return classifyLocalReference(nullptr);
180
182 return M68kII::MO_GOTPCREL;
183
184 return M68kII::MO_GOT;
185}
186
187unsigned char
189 return classifyGlobalReference(GV, *GV->getParent());
190}
191
193 const Module &M) const {
194 if (TM.shouldAssumeDSOLocal(GV))
195 return classifyLocalReference(GV);
196
197 switch (TM.getCodeModel()) {
198 default:
199 llvm_unreachable("Unsupported code model");
200 case CodeModel::Small:
201 case CodeModel::Kernel: {
203 return M68kII::MO_GOTPCREL;
205 }
206 case CodeModel::Medium: {
208 return M68kII::MO_GOTPCREL;
209
210 if (atLeastM68020())
212
214 }
215 }
216}
217
219 if (isPositionIndependent()) {
220 // The only time we want to use GOTOFF(used when with EK_Custom32) is when
221 // the potential delta between the jump target and table base can be larger
222 // than displacement field, which is True for older CPUs(16 bit disp)
223 // in Medium model(can have large data way beyond 16 bit).
226
228 }
229
230 // In non-pic modes, just use the address of a block.
232}
233
234unsigned char
237}
238
239unsigned char
241 const Module &M) const {
242 // local always use pc-rel referencing
243 if (TM.shouldAssumeDSOLocal(GV))
244 return M68kII::MO_NO_FLAG;
245
246 // If the function is marked as non-lazy, generate an indirect call
247 // which loads from the GOT directly. This avoids run-time overhead
248 // at the cost of eager binding.
249 auto *F = dyn_cast_or_null<Function>(GV);
250 if (F && F->hasFnAttribute(Attribute::NonLazyBind)) {
251 return M68kII::MO_GOTPCREL;
252 }
253
254 // Ensure that we don't emit PLT relocations when in non-pic modes.
256}
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:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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)