TargetMachine.h revision 8f4e22ff9a17b750fef179ed1572e9bfd01ed9a2
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 Target;
23class TargetAsmInfo;
24class TargetData;
25class TargetSubtarget;
26class TargetInstrInfo;
27class TargetIntrinsicInfo;
28class TargetJITInfo;
29class TargetLowering;
30class TargetFrameInfo;
31class MachineCodeEmitter;
32class JITCodeEmitter;
33class ObjectCodeEmitter;
34class TargetRegisterInfo;
35class PassManagerBase;
36class PassManager;
37class Pass;
38class TargetMachOWriterInfo;
39class TargetELFWriterInfo;
40class formatted_raw_ostream;
41
42// Relocation model types.
43namespace Reloc {
44  enum Model {
45    Default,
46    Static,
47    PIC_,         // Cannot be named PIC due to collision with -DPIC
48    DynamicNoPIC
49  };
50}
51
52// Code model types.
53namespace CodeModel {
54  enum Model {
55    Default,
56    Small,
57    Kernel,
58    Medium,
59    Large
60  };
61}
62
63namespace FileModel {
64  enum Model {
65    Error,
66    None,
67    AsmFile,
68    MachOFile,
69    ElfFile
70  };
71}
72
73// Code generation optimization level.
74namespace CodeGenOpt {
75  enum Level {
76    Default,
77    None,
78    Aggressive
79  };
80}
81
82
83// Possible float ABI settings. Used with FloatABIType in TargetOptions.h.
84namespace FloatABI {
85  enum ABIType {
86    Default, // Target-specific (either soft of hard depending on triple, etc).
87    Soft, // Soft float.
88    Hard  // Hard float.
89  };
90}
91
92//===----------------------------------------------------------------------===//
93///
94/// TargetMachine - Primary interface to the complete machine description for
95/// the target machine.  All target-specific information should be accessible
96/// through this interface.
97///
98class TargetMachine {
99  TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
100  void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
101protected: // Can only create subclasses.
102  TargetMachine(const Target &);
103
104  /// getSubtargetImpl - virtual method implemented by subclasses that returns
105  /// a reference to that target's TargetSubtarget-derived member variable.
106  virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
107
108  /// TheTarget - The Target that this machine was created for.
109  const Target &TheTarget;
110
111  /// AsmInfo - Contains target specific asm information.
112  ///
113  mutable const TargetAsmInfo *AsmInfo;
114
115  /// createTargetAsmInfo - Create a new instance of target specific asm
116  /// information.
117  virtual const TargetAsmInfo *createTargetAsmInfo() const { return 0; }
118
119public:
120  virtual ~TargetMachine();
121
122  const Target &getTarget() const { return TheTarget; }
123
124  // Interfaces to the major aspects of target machine information:
125  // -- Instruction opcode and operand information
126  // -- Pipelines and scheduling information
127  // -- Stack frame information
128  // -- Selection DAG lowering information
129  //
130  virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
131  virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
132  virtual       TargetLowering    *getTargetLowering() const { return 0; }
133  virtual const TargetData            *getTargetData() const { return 0; }
134
135  /// getTargetAsmInfo - Return target specific asm information.
136  ///
137  const TargetAsmInfo *getTargetAsmInfo() const {
138    if (!AsmInfo) AsmInfo = createTargetAsmInfo();
139    return AsmInfo;
140  }
141
142  /// getSubtarget - This method returns a pointer to the specified type of
143  /// TargetSubtarget.  In debug builds, it verifies that the object being
144  /// returned is of the correct type.
145  template<typename STC> const STC &getSubtarget() const {
146    const TargetSubtarget *TST = getSubtargetImpl();
147    assert(TST && dynamic_cast<const STC*>(TST) &&
148           "Not the right kind of subtarget!");
149    return *static_cast<const STC*>(TST);
150  }
151
152  /// getRegisterInfo - If register information is available, return it.  If
153  /// not, return null.  This is kept separate from RegInfo until RegInfo has
154  /// details of graph coloring register allocation removed from it.
155  ///
156  virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
157
158  /// getIntrinsicInfo - If intrinsic information is available, return it.  If
159  /// not, return null.
160  ///
161  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
162
163  /// getJITInfo - If this target supports a JIT, return information for it,
164  /// otherwise return null.
165  ///
166  virtual TargetJITInfo *getJITInfo() { return 0; }
167
168  /// getInstrItineraryData - Returns instruction itinerary data for the target
169  /// or specific subtarget.
170  ///
171  virtual const InstrItineraryData getInstrItineraryData() const {
172    return InstrItineraryData();
173  }
174
175  /// getMachOWriterInfo - If this target supports a Mach-O writer, return
176  /// information for it, otherwise return null.
177  ///
178  virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
179
180  /// getELFWriterInfo - If this target supports an ELF writer, return
181  /// information for it, otherwise return null.
182  ///
183  virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
184
185  /// getRelocationModel - Returns the code generation relocation model. The
186  /// choices are static, PIC, and dynamic-no-pic, and target default.
187  static Reloc::Model getRelocationModel();
188
189  /// setRelocationModel - Sets the code generation relocation model.
190  ///
191  static void setRelocationModel(Reloc::Model Model);
192
193  /// getCodeModel - Returns the code model. The choices are small, kernel,
194  /// medium, large, and target default.
195  static CodeModel::Model getCodeModel();
196
197  /// setCodeModel - Sets the code model.
198  ///
199  static void setCodeModel(CodeModel::Model Model);
200
201  /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
202  ///
203  static bool getAsmVerbosityDefault();
204
205  /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
206  /// is false.
207  static void setAsmVerbosityDefault(bool);
208
209  /// CodeGenFileType - These enums are meant to be passed into
210  /// addPassesToEmitFile to indicate what type of file to emit.
211  enum CodeGenFileType {
212    AssemblyFile, ObjectFile, DynamicLibrary
213  };
214
215  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
216  /// on this target.  User flag overrides.
217  virtual bool getEnableTailMergeDefault() const { return true; }
218
219  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
220  /// specified file emitted.  Typically this will involve several steps of code
221  /// generation.  If Fast is set to true, the code generator should emit code
222  /// as fast as possible, though the generated code may be less efficient.
223  /// This method should return FileModel::Error if emission of this file type
224  /// is not supported.
225  ///
226  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
227                                               formatted_raw_ostream &,
228                                               CodeGenFileType,
229                                               CodeGenOpt::Level) {
230    return FileModel::None;
231  }
232
233  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
234  /// to be split up (e.g., to add an object writer pass), this method can be
235  /// used to finish up adding passes to emit the file, if necessary.
236  ///
237  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
238                                         MachineCodeEmitter *,
239                                         CodeGenOpt::Level) {
240    return true;
241  }
242
243  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
244  /// to be split up (e.g., to add an object writer pass), this method can be
245  /// used to finish up adding passes to emit the file, if necessary.
246  ///
247  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
248                                         JITCodeEmitter *,
249                                         CodeGenOpt::Level) {
250    return true;
251  }
252
253  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
254  /// to be split up (e.g., to add an object writer pass), this method can be
255  /// used to finish up adding passes to emit the file, if necessary.
256  ///
257  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
258                                         ObjectCodeEmitter *,
259                                         CodeGenOpt::Level) {
260    return true;
261  }
262
263  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
264  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
265  /// actually outputting the machine code and resolving things like the address
266  /// of functions.  This method returns true if machine code emission is
267  /// not supported.
268  ///
269  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
270                                          MachineCodeEmitter &,
271                                          CodeGenOpt::Level) {
272    return true;
273  }
274
275  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
276  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
277  /// actually outputting the machine code and resolving things like the address
278  /// of functions.  This method returns true if machine code emission is
279  /// not supported.
280  ///
281  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
282                                          JITCodeEmitter &,
283                                          CodeGenOpt::Level) {
284    return true;
285  }
286
287  /// addPassesToEmitWholeFile - This method can be implemented by targets that
288  /// require having the entire module at once.  This is not recommended, do not
289  /// use this.
290  virtual bool WantsWholeFile() const { return false; }
291  virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
292                                        CodeGenFileType,
293                                        CodeGenOpt::Level) {
294    return true;
295  }
296};
297
298/// LLVMTargetMachine - This class describes a target machine that is
299/// implemented with the LLVM target-independent code generator.
300///
301class LLVMTargetMachine : public TargetMachine {
302protected: // Can only create subclasses.
303  LLVMTargetMachine(const Target &T) : TargetMachine(T) { }
304
305  /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
306  /// both emitting to assembly files or machine code output.
307  ///
308  bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
309
310public:
311
312  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
313  /// specified file emitted.  Typically this will involve several steps of code
314  /// generation.  If OptLevel is None, the code generator should emit code as fast
315  /// as possible, though the generated code may be less efficient.  This method
316  /// should return FileModel::Error if emission of this file type is not
317  /// supported.
318  ///
319  /// The default implementation of this method adds components from the
320  /// LLVM retargetable code generator, invoking the methods below to get
321  /// target-specific passes in standard locations.
322  ///
323  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
324                                               formatted_raw_ostream &Out,
325                                               CodeGenFileType FileType,
326                                               CodeGenOpt::Level);
327
328  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
329  /// to be split up (e.g., to add an object writer pass), this method can be
330  /// used to finish up adding passes to emit the file, if necessary.
331  ///
332  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
333                                         MachineCodeEmitter *MCE,
334                                         CodeGenOpt::Level);
335
336  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
337  /// to be split up (e.g., to add an object writer pass), this method can be
338  /// used to finish up adding passes to emit the file, if necessary.
339  ///
340  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
341                                         JITCodeEmitter *JCE,
342                                         CodeGenOpt::Level);
343
344  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
345  /// to be split up (e.g., to add an object writer pass), this method can be
346  /// used to finish up adding passes to emit the file, if necessary.
347  ///
348  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
349                                         ObjectCodeEmitter *OCE,
350                                         CodeGenOpt::Level);
351
352  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
353  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
354  /// actually outputting the machine code and resolving things like the address
355  /// of functions.  This method returns true if machine code emission is
356  /// not supported.
357  ///
358  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
359                                          MachineCodeEmitter &MCE,
360                                          CodeGenOpt::Level);
361
362  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
363  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
364  /// actually outputting the machine code and resolving things like the address
365  /// of functions.  This method returns true if machine code emission is
366  /// not supported.
367  ///
368  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
369                                          JITCodeEmitter &MCE,
370                                          CodeGenOpt::Level);
371
372  /// Target-Independent Code Generator Pass Configuration Options.
373
374  /// addInstSelector - This method should add any "last minute" LLVM->LLVM
375  /// passes, then install an instruction selector pass, which converts from
376  /// LLVM code to machine instructions.
377  virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
378    return true;
379  }
380
381  /// addPreRegAllocPasses - This method may be implemented by targets that want
382  /// to run passes immediately before register allocation. This should return
383  /// true if -print-machineinstrs should print after these passes.
384  virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
385    return false;
386  }
387
388  /// addPostRegAllocPasses - This method may be implemented by targets that
389  /// want to run passes after register allocation but before prolog-epilog
390  /// insertion.  This should return true if -print-machineinstrs should print
391  /// after these passes.
392  virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
393    return false;
394  }
395
396  /// addPreEmitPass - This pass may be implemented by targets that want to run
397  /// passes immediately before machine code is emitted.  This should return
398  /// true if -print-machineinstrs should print out the code after the passes.
399  virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
400    return false;
401  }
402
403
404  /// addCodeEmitter - This pass should be overridden by the target to add a
405  /// code emitter, if supported.  If this is not supported, 'true' should be
406  /// returned.
407  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
408                              MachineCodeEmitter &) {
409    return true;
410  }
411
412  /// addCodeEmitter - This pass should be overridden by the target to add a
413  /// code emitter, if supported.  If this is not supported, 'true' should be
414  /// returned.
415  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
416                              JITCodeEmitter &) {
417    return true;
418  }
419
420  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
421  /// a code emitter (without setting flags), if supported.  If this is not
422  /// supported, 'true' should be returned.
423  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
424                                    MachineCodeEmitter &) {
425    return true;
426  }
427
428  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
429  /// a code emitter (without setting flags), if supported.  If this is not
430  /// supported, 'true' should be returned.
431  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
432                                    JITCodeEmitter &) {
433    return true;
434  }
435
436  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
437  /// a code emitter (without setting flags), if supported.  If this is not
438  /// supported, 'true' should be returned.
439  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
440                                    ObjectCodeEmitter &) {
441    return true;
442  }
443
444  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
445  /// on this target.  User flag overrides.
446  virtual bool getEnableTailMergeDefault() const { return true; }
447
448  /// addAssemblyEmitter - Helper function which creates a target specific
449  /// assembly printer, if available.
450  ///
451  /// \return Returns 'false' on success.
452  bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
453                          bool /* VerboseAsmDefault */,
454                          formatted_raw_ostream &);
455};
456
457} // End llvm namespace
458
459#endif
460