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