TargetMachine.h revision 8bd6035750f1b290832a3b1c90766d9b45ed8d6b
1//===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the TargetMachine and LLVMTargetMachine classes.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TARGET_TARGETMACHINE_H
15#define LLVM_TARGET_TARGETMACHINE_H
16
17#include "llvm/Target/TargetInstrItineraries.h"
18#include <cassert>
19#include <string>
20
21namespace llvm {
22
23class TargetAsmInfo;
24class TargetData;
25class TargetSubtarget;
26class TargetInstrInfo;
27class TargetInstrDescriptor;
28class TargetJITInfo;
29class TargetLowering;
30class TargetFrameInfo;
31class MachineCodeEmitter;
32class MRegisterInfo;
33class Module;
34class FunctionPassManager;
35class PassManager;
36class Pass;
37class TargetMachOWriterInfo;
38class TargetELFWriterInfo;
39
40// Relocation model types.
41namespace Reloc {
42  enum Model {
43    Default,
44    Static,
45    PIC_,         // Cannot be named PIC due to collision with -DPIC
46    DynamicNoPIC
47  };
48}
49
50// Code model types.
51namespace CodeModel {
52  enum Model {
53    Default,
54    Small,
55    Kernel,
56    Medium,
57    Large
58  };
59}
60
61namespace FileModel {
62  enum Model {
63    Error,
64    None,
65    AsmFile,
66    MachOFile,
67    ElfFile
68  };
69}
70
71//===----------------------------------------------------------------------===//
72///
73/// TargetMachine - Primary interface to the complete machine description for
74/// the target machine.  All target-specific information should be accessible
75/// through this interface.
76///
77class TargetMachine {
78  TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
79  void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
80protected: // Can only create subclasses.
81  TargetMachine() : AsmInfo(NULL) { }
82
83  /// getSubtargetImpl - virtual method implemented by subclasses that returns
84  /// a reference to that target's TargetSubtarget-derived member variable.
85  virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
86
87  /// AsmInfo - Contains target specific asm information.
88  ///
89  mutable const TargetAsmInfo *AsmInfo;
90
91  /// createTargetAsmInfo - Create a new instance of target specific asm
92  /// information.
93  virtual const TargetAsmInfo *createTargetAsmInfo() const { return NULL; }
94
95public:
96  virtual ~TargetMachine();
97
98  /// getModuleMatchQuality - This static method should be implemented by
99  /// targets to indicate how closely they match the specified module.  This is
100  /// used by the LLC tool to determine which target to use when an explicit
101  /// -march option is not specified.  If a target returns zero, it will never
102  /// be chosen without an explicit -march option.
103  static unsigned getModuleMatchQuality(const Module &M) { return 0; }
104
105  /// getJITMatchQuality - This static method should be implemented by targets
106  /// that provide JIT capabilities to indicate how suitable they are for
107  /// execution on the current host.  If a value of 0 is returned, the target
108  /// will not be used unless an explicit -march option is used.
109  static unsigned getJITMatchQuality() { return 0; }
110
111  // Interfaces to the major aspects of target machine information:
112  // -- Instruction opcode and operand information
113  // -- Pipelines and scheduling information
114  // -- Stack frame information
115  // -- Selection DAG lowering information
116  //
117  virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
118  virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
119  virtual       TargetLowering    *getTargetLowering() const { return 0; }
120  virtual const TargetData            *getTargetData() const { return 0; }
121
122
123  /// getTargetAsmInfo - Return target specific asm information.
124  ///
125  const TargetAsmInfo *getTargetAsmInfo() const {
126    if (!AsmInfo) AsmInfo = createTargetAsmInfo();
127    return AsmInfo;
128  }
129
130  /// getSubtarget - This method returns a pointer to the specified type of
131  /// TargetSubtarget.  In debug builds, it verifies that the object being
132  /// returned is of the correct type.
133  template<typename STC> const STC &getSubtarget() const {
134    const TargetSubtarget *TST = getSubtargetImpl();
135    assert(TST && dynamic_cast<const STC*>(TST) &&
136           "Not the right kind of subtarget!");
137    return *static_cast<const STC*>(TST);
138  }
139
140  /// getRegisterInfo - If register information is available, return it.  If
141  /// not, return null.  This is kept separate from RegInfo until RegInfo has
142  /// details of graph coloring register allocation removed from it.
143  ///
144  virtual const MRegisterInfo *getRegisterInfo() const { return 0; }
145
146  /// getJITInfo - If this target supports a JIT, return information for it,
147  /// otherwise return null.
148  ///
149  virtual TargetJITInfo *getJITInfo() { return 0; }
150
151  /// getInstrItineraryData - Returns instruction itinerary data for the target
152  /// or specific subtarget.
153  ///
154  virtual const InstrItineraryData getInstrItineraryData() const {
155    return InstrItineraryData();
156  }
157
158  /// getMachOWriterInfo - If this target supports a Mach-O writer, return
159  /// information for it, otherwise return null.
160  ///
161  virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
162
163  /// getELFWriterInfo - If this target supports an ELF writer, return
164  /// information for it, otherwise return null.
165  ///
166  virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
167
168  /// getRelocationModel - Returns the code generation relocation model. The
169  /// choices are static, PIC, and dynamic-no-pic, and target default.
170  static Reloc::Model getRelocationModel();
171
172  /// setRelocationModel - Sets the code generation relocation model.
173  static void setRelocationModel(Reloc::Model Model);
174
175  /// getCodeModel - Returns the code model. The choices are small, kernel,
176  /// medium, large, and target default.
177  static CodeModel::Model getCodeModel();
178
179  /// setCodeModel - Sets the code model.
180  static void setCodeModel(CodeModel::Model Model);
181
182  /// CodeGenFileType - These enums are meant to be passed into
183  /// addPassesToEmitFile to indicate what type of file to emit.
184  enum CodeGenFileType {
185    AssemblyFile, ObjectFile, DynamicLibrary
186  };
187
188  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
189  /// on this target.  User flag overrides.
190  virtual const bool getEnableTailMergeDefault() const { return true; }
191
192  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
193  /// specified file emitted.  Typically this will involve several steps of code
194  /// generation.  If Fast is set to true, the code generator should emit code
195  /// as fast as possible, without regard for compile time.  This method should
196  /// return FileModel::Error if emission of this file type is not supported.
197  ///
198  virtual FileModel::Model addPassesToEmitFile(FunctionPassManager &PM,
199                                               std::ostream &Out,
200                                               CodeGenFileType FileType,
201                                               bool Fast) {
202    return FileModel::None;
203  }
204
205  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
206  /// to be split up (e.g., to add an object writer pass), this method can be
207  /// used to finish up adding passes to emit the file, if necessary.
208  ///
209  virtual bool addPassesToEmitFileFinish(FunctionPassManager &PM,
210                                         MachineCodeEmitter *MCE, bool Fast) {
211    return true;
212  }
213
214  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
215  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
216  /// actually outputting the machine code and resolving things like the address
217  /// of functions.  This method returns true if machine code emission is
218  /// not supported.
219  ///
220  virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
221                                          MachineCodeEmitter &MCE, bool Fast) {
222    return true;
223  }
224
225  /// addPassesToEmitWholeFile - This method can be implemented by targets that
226  /// require having the entire module at once.  This is not recommended, do not
227  /// use this.
228  virtual bool WantsWholeFile() const { return false; }
229  virtual bool addPassesToEmitWholeFile(PassManager &PM, std::ostream &Out,
230                                        CodeGenFileType FileType, bool Fast) {
231    return true;
232  }
233};
234
235/// LLVMTargetMachine - This class describes a target machine that is
236/// implemented with the LLVM target-independent code generator.
237///
238class LLVMTargetMachine : public TargetMachine {
239protected: // Can only create subclasses.
240    LLVMTargetMachine() { }
241public:
242
243  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
244  /// specified file emitted.  Typically this will involve several steps of code
245  /// generation.  If Fast is set to true, the code generator should emit code
246  /// as fast as possible, without regard for compile time.  This method should
247  /// return FileModel::Error if emission of this file type is not supported.
248  ///
249  /// The default implementation of this method adds components from the
250  /// LLVM retargetable code generator, invoking the methods below to get
251  /// target-specific passes in standard locations.
252  ///
253  virtual FileModel::Model addPassesToEmitFile(FunctionPassManager &PM,
254                                               std::ostream &Out,
255                                               CodeGenFileType FileType,
256                                               bool Fast);
257
258  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
259  /// to be split up (e.g., to add an object writer pass), this method can be
260  /// used to finish up adding passes to emit the file, if necessary.
261  ///
262  virtual bool addPassesToEmitFileFinish(FunctionPassManager &PM,
263                                         MachineCodeEmitter *MCE, bool Fast);
264
265  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
266  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
267  /// actually outputting the machine code and resolving things like the address
268  /// of functions.  This method returns true if machine code emission is
269  /// not supported.
270  ///
271  virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
272                                          MachineCodeEmitter &MCE, bool Fast);
273
274  /// Target-Independent Code Generator Pass Configuration Options.
275
276  /// addInstSelector - This method should add any "last minute" LLVM->LLVM
277  /// passes, then install an instruction selector pass, which converts from
278  /// LLVM code to machine instructions.
279  virtual bool addInstSelector(FunctionPassManager &PM, bool Fast) {
280    return true;
281  }
282
283  /// addPostRegAllocPasses - This method may be implemented by targets that
284  /// want to run passes after register allocation but before prolog-epilog
285  /// insertion.  This should return true if -print-machineinstrs should print
286  /// after these passes.
287  virtual bool addPostRegAlloc(FunctionPassManager &PM, bool Fast) {
288    return false;
289  }
290
291  /// addPreEmitPass - This pass may be implemented by targets that want to run
292  /// passes immediately before machine code is emitted.  This should return
293  /// true if -print-machineinstrs should print out the code after the passes.
294  virtual bool addPreEmitPass(FunctionPassManager &PM, bool Fast) {
295    return false;
296  }
297
298
299  /// addAssemblyEmitter - This pass should be overridden by the target to add
300  /// the asmprinter, if asm emission is supported.  If this is not supported,
301  /// 'true' should be returned.
302  virtual bool addAssemblyEmitter(FunctionPassManager &PM, bool Fast,
303                                  std::ostream &Out) {
304    return true;
305  }
306
307  /// addCodeEmitter - This pass should be overridden by the target to add a
308  /// code emitter, if supported.  If this is not supported, 'true' should be
309  /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
310  virtual bool addCodeEmitter(FunctionPassManager &PM, bool Fast, bool DumpAsm,
311                              MachineCodeEmitter &MCE) {
312    return true;
313  }
314
315  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
316  /// a code emitter (without setting flags), if supported.  If this is not
317  /// supported, 'true' should be returned.  If DumpAsm is true, the generated
318  /// assembly is printed to cerr.
319  virtual bool addSimpleCodeEmitter(FunctionPassManager &PM, bool Fast,
320                                    bool DumpAsm, MachineCodeEmitter &MCE) {
321    return true;
322  }
323
324  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
325  /// on this target.  User flag overrides.
326  virtual const bool getEnableTailMergeDefault() const { return true; }
327};
328
329} // End llvm namespace
330
331#endif
332