TargetMachine.h revision 42bf74be1402df7409efbea089310d4c276fde37
1//===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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
20namespace llvm {
21
22class TargetAsmInfo;
23class TargetData;
24class TargetSubtarget;
25class TargetInstrInfo;
26class TargetIntrinsicInfo;
27class TargetJITInfo;
28class TargetLowering;
29class TargetFrameInfo;
30class MachineCodeEmitter;
31class TargetRegisterInfo;
32class Module;
33class PassManagerBase;
34class PassManager;
35class Pass;
36class TargetMachOWriterInfo;
37class TargetELFWriterInfo;
38class raw_ostream;
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(0) { }
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 0; }
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 &) { 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  /// getTargetAsmInfo - Return target specific asm information.
123  ///
124  const TargetAsmInfo *getTargetAsmInfo() const {
125    if (!AsmInfo) AsmInfo = createTargetAsmInfo();
126    return AsmInfo;
127  }
128
129  /// getSubtarget - This method returns a pointer to the specified type of
130  /// TargetSubtarget.  In debug builds, it verifies that the object being
131  /// returned is of the correct type.
132  template<typename STC> const STC &getSubtarget() const {
133    const TargetSubtarget *TST = getSubtargetImpl();
134    assert(TST && dynamic_cast<const STC*>(TST) &&
135           "Not the right kind of subtarget!");
136    return *static_cast<const STC*>(TST);
137  }
138
139  /// getRegisterInfo - If register information is available, return it.  If
140  /// not, return null.  This is kept separate from RegInfo until RegInfo has
141  /// details of graph coloring register allocation removed from it.
142  ///
143  virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
144
145  /// getIntrinsicInfo - If intrinsic information is available, return it.  If
146  /// not, return null.
147  ///
148  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
149
150  /// getJITInfo - If this target supports a JIT, return information for it,
151  /// otherwise return null.
152  ///
153  virtual TargetJITInfo *getJITInfo() { return 0; }
154
155  /// getInstrItineraryData - Returns instruction itinerary data for the target
156  /// or specific subtarget.
157  ///
158  virtual const InstrItineraryData getInstrItineraryData() const {
159    return InstrItineraryData();
160  }
161
162  /// getMachOWriterInfo - If this target supports a Mach-O writer, return
163  /// information for it, otherwise return null.
164  ///
165  virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
166
167  /// getELFWriterInfo - If this target supports an ELF writer, return
168  /// information for it, otherwise return null.
169  ///
170  virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
171
172  /// getRelocationModel - Returns the code generation relocation model. The
173  /// choices are static, PIC, and dynamic-no-pic, and target default.
174  static Reloc::Model getRelocationModel();
175
176  /// setRelocationModel - Sets the code generation relocation model.
177  ///
178  static void setRelocationModel(Reloc::Model Model);
179
180  /// getCodeModel - Returns the code model. The choices are small, kernel,
181  /// medium, large, and target default.
182  static CodeModel::Model getCodeModel();
183
184  /// setCodeModel - Sets the code model.
185  ///
186  static void setCodeModel(CodeModel::Model Model);
187
188  /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
189  ///
190  static bool getAsmVerbosityDefault();
191
192  /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
193  /// is false.
194  static void setAsmVerbosityDefault(bool);
195
196  /// CodeGenFileType - These enums are meant to be passed into
197  /// addPassesToEmitFile to indicate what type of file to emit.
198  enum CodeGenFileType {
199    AssemblyFile, ObjectFile, DynamicLibrary
200  };
201
202  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
203  /// on this target.  User flag overrides.
204  virtual bool getEnableTailMergeDefault() const { return true; }
205
206  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
207  /// specified file emitted.  Typically this will involve several steps of code
208  /// generation.  If Fast is set to true, the code generator should emit code
209  /// as fast as possible, though the generated code may be less efficient.
210  /// This method should return FileModel::Error if emission of this file type
211  /// is not supported.
212  ///
213  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
214                                               raw_ostream &,
215                                               CodeGenFileType,
216                                               bool /*Fast*/) {
217    return FileModel::None;
218  }
219
220  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
221  /// to be split up (e.g., to add an object writer pass), this method can be
222  /// used to finish up adding passes to emit the file, if necessary.
223  ///
224  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
225                                         MachineCodeEmitter *, bool /*Fast*/) {
226    return true;
227  }
228
229  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
230  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
231  /// actually outputting the machine code and resolving things like the address
232  /// of functions.  This method returns true if machine code emission is
233  /// not supported.
234  ///
235  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
236                                          MachineCodeEmitter &,
237                                          bool /*Fast*/) {
238    return true;
239  }
240
241  /// addPassesToEmitWholeFile - This method can be implemented by targets that
242  /// require having the entire module at once.  This is not recommended, do not
243  /// use this.
244  virtual bool WantsWholeFile() const { return false; }
245  virtual bool addPassesToEmitWholeFile(PassManager &, raw_ostream &,
246                                        CodeGenFileType, bool /*Fast*/) {
247    return true;
248  }
249};
250
251/// LLVMTargetMachine - This class describes a target machine that is
252/// implemented with the LLVM target-independent code generator.
253///
254class LLVMTargetMachine : public TargetMachine {
255protected: // Can only create subclasses.
256  LLVMTargetMachine() { }
257
258  /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
259  /// both emitting to assembly files or machine code output.
260  ///
261  bool addCommonCodeGenPasses(PassManagerBase &, bool /*Fast*/);
262
263public:
264
265  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
266  /// specified file emitted.  Typically this will involve several steps of code
267  /// generation.  If Fast is set to true, the code generator should emit code
268  /// as fast as possible, though the generated code may be less efficient.
269  /// This method should return FileModel::Error if emission of this file type
270  /// is not supported.
271  ///
272  /// The default implementation of this method adds components from the
273  /// LLVM retargetable code generator, invoking the methods below to get
274  /// target-specific passes in standard locations.
275  ///
276  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
277                                               raw_ostream &Out,
278                                               CodeGenFileType FileType,
279                                               bool Fast);
280
281  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
282  /// to be split up (e.g., to add an object writer pass), this method can be
283  /// used to finish up adding passes to emit the file, if necessary.
284  ///
285  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
286                                         MachineCodeEmitter *MCE, bool Fast);
287
288  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
289  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
290  /// actually outputting the machine code and resolving things like the address
291  /// of functions.  This method returns true if machine code emission is
292  /// not supported.
293  ///
294  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
295                                          MachineCodeEmitter &MCE, bool Fast);
296
297  /// Target-Independent Code Generator Pass Configuration Options.
298
299  /// addInstSelector - This method should add any "last minute" LLVM->LLVM
300  /// passes, then install an instruction selector pass, which converts from
301  /// LLVM code to machine instructions.
302  virtual bool addInstSelector(PassManagerBase &, bool /*Fast*/) {
303    return true;
304  }
305
306  /// addPreRegAllocPasses - This method may be implemented by targets that want
307  /// to run passes immediately before register allocation. This should return
308  /// true if -print-machineinstrs should print after these passes.
309  virtual bool addPreRegAlloc(PassManagerBase &, bool /*Fast*/) {
310    return false;
311  }
312
313  /// addPostRegAllocPasses - This method may be implemented by targets that
314  /// want to run passes after register allocation but before prolog-epilog
315  /// insertion.  This should return true if -print-machineinstrs should print
316  /// after these passes.
317  virtual bool addPostRegAlloc(PassManagerBase &, bool /*Fast*/) {
318    return false;
319  }
320
321  /// addPreEmitPass - This pass may be implemented by targets that want to run
322  /// passes immediately before machine code is emitted.  This should return
323  /// true if -print-machineinstrs should print out the code after the passes.
324  virtual bool addPreEmitPass(PassManagerBase &, bool /*Fast*/) {
325    return false;
326  }
327
328
329  /// addAssemblyEmitter - This pass should be overridden by the target to add
330  /// the asmprinter, if asm emission is supported.  If this is not supported,
331  /// 'true' should be returned.
332  virtual bool addAssemblyEmitter(PassManagerBase &, bool /*Fast*/,
333                                  bool /* VerboseAsmDefault */, raw_ostream &) {
334    return true;
335  }
336
337  /// addCodeEmitter - This pass should be overridden by the target to add a
338  /// code emitter, if supported.  If this is not supported, 'true' should be
339  /// returned. If DumpAsm is true, the generated assembly is printed to cerr.
340  virtual bool addCodeEmitter(PassManagerBase &, bool /*Fast*/,
341                              bool /*DumpAsm*/, MachineCodeEmitter &) {
342    return true;
343  }
344
345  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
346  /// a code emitter (without setting flags), if supported.  If this is not
347  /// supported, 'true' should be returned.  If DumpAsm is true, the generated
348  /// assembly is printed to cerr.
349  virtual bool addSimpleCodeEmitter(PassManagerBase &, bool /*Fast*/,
350                                    bool /*DumpAsm*/, MachineCodeEmitter &) {
351    return true;
352  }
353
354  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
355  /// on this target.  User flag overrides.
356  virtual bool getEnableTailMergeDefault() const { return true; }
357};
358
359} // End llvm namespace
360
361#endif
362