TargetMachine.h revision 48814681d72242e0179d7100f263952fdf4f51d6
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    None,        // -O0
78    Less,        // -O1
79    Default,     // -O2, -Os
80    Aggressive   // -O3
81  };
82}
83
84// Specify if we should encode the LSDA pointer in the FDE as 4- or 8-bytes.
85namespace DwarfLSDAEncoding {
86  enum Encoding {
87    Default,
88    FourByte,
89    EightByte
90  };
91}
92
93//===----------------------------------------------------------------------===//
94///
95/// TargetMachine - Primary interface to the complete machine description for
96/// the target machine.  All target-specific information should be accessible
97/// through this interface.
98///
99class TargetMachine {
100  TargetMachine(const TargetMachine &);   // DO NOT IMPLEMENT
101  void operator=(const TargetMachine &);  // DO NOT IMPLEMENT
102protected: // Can only create subclasses.
103  TargetMachine(const Target &);
104
105  /// getSubtargetImpl - virtual method implemented by subclasses that returns
106  /// a reference to that target's TargetSubtarget-derived member variable.
107  virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
108
109  /// TheTarget - The Target that this machine was created for.
110  const Target &TheTarget;
111
112  /// AsmInfo - Contains target specific asm information.
113  ///
114  const MCAsmInfo *AsmInfo;
115
116public:
117  virtual ~TargetMachine();
118
119  const Target &getTarget() const { return TheTarget; }
120
121  // Interfaces to the major aspects of target machine information:
122  // -- Instruction opcode and operand information
123  // -- Pipelines and scheduling information
124  // -- Stack frame information
125  // -- Selection DAG lowering information
126  //
127  virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
128  virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
129  virtual       TargetLowering    *getTargetLowering() const { return 0; }
130  virtual const TargetData            *getTargetData() const { return 0; }
131
132  /// getMCAsmInfo - Return target specific asm information.
133  ///
134  const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
135
136  /// getSubtarget - This method returns a pointer to the specified type of
137  /// TargetSubtarget.  In debug builds, it verifies that the object being
138  /// returned is of the correct type.
139  template<typename STC> const STC &getSubtarget() const {
140    const TargetSubtarget *TST = getSubtargetImpl();
141    assert(TST && dynamic_cast<const STC*>(TST) &&
142           "Not the right kind of subtarget!");
143    return *static_cast<const STC*>(TST);
144  }
145
146  /// getRegisterInfo - If register information is available, return it.  If
147  /// not, return null.  This is kept separate from RegInfo until RegInfo has
148  /// details of graph coloring register allocation removed from it.
149  ///
150  virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
151
152  /// getIntrinsicInfo - If intrinsic information is available, return it.  If
153  /// not, return null.
154  ///
155  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
156
157  /// getJITInfo - If this target supports a JIT, return information for it,
158  /// otherwise return null.
159  ///
160  virtual TargetJITInfo *getJITInfo() { return 0; }
161
162  /// getInstrItineraryData - Returns instruction itinerary data for the target
163  /// or specific subtarget.
164  ///
165  virtual const InstrItineraryData getInstrItineraryData() const {
166    return InstrItineraryData();
167  }
168
169  /// getMachOWriterInfo - If this target supports a Mach-O writer, return
170  /// information for it, otherwise return null.
171  ///
172  virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
173
174  /// getELFWriterInfo - If this target supports an ELF writer, return
175  /// information for it, otherwise return null.
176  ///
177  virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
178
179  /// getRelocationModel - Returns the code generation relocation model. The
180  /// choices are static, PIC, and dynamic-no-pic, and target default.
181  static Reloc::Model getRelocationModel();
182
183  /// setRelocationModel - Sets the code generation relocation model.
184  ///
185  static void setRelocationModel(Reloc::Model Model);
186
187  /// getCodeModel - Returns the code model. The choices are small, kernel,
188  /// medium, large, and target default.
189  static CodeModel::Model getCodeModel();
190
191  /// setCodeModel - Sets the code model.
192  ///
193  static void setCodeModel(CodeModel::Model Model);
194
195  /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
196  ///
197  static bool getAsmVerbosityDefault();
198
199  /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
200  /// is false.
201  static void setAsmVerbosityDefault(bool);
202
203  /// getLSDAEncoding - Returns the LSDA pointer encoding. The choices are
204  /// 4-byte, 8-byte, and target default. The CIE is hard-coded to indicate that
205  /// the LSDA pointer in the FDE section is an "sdata4", and should be encoded
206  /// as a 4-byte pointer by default. However, some systems may require a
207  /// different size due to bugs or other conditions. We will default to a
208  /// 4-byte encoding unless the system tells us otherwise.
209  ///
210  /// FIXME: This call-back isn't good! We should be using the correct encoding
211  /// regardless of the system. However, there are some systems which have bugs
212  /// that prevent this from occuring.
213  virtual DwarfLSDAEncoding::Encoding getLSDAEncoding() const {
214    return DwarfLSDAEncoding::Default;
215  }
216
217  /// CodeGenFileType - These enums are meant to be passed into
218  /// addPassesToEmitFile to indicate what type of file to emit.
219  enum CodeGenFileType {
220    AssemblyFile, ObjectFile, DynamicLibrary
221  };
222
223  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
224  /// on this target.  User flag overrides.
225  virtual bool getEnableTailMergeDefault() const { return true; }
226
227  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
228  /// specified file emitted.  Typically this will involve several steps of code
229  /// generation.
230  /// This method should return FileModel::Error if emission of this file type
231  /// is not supported.
232  ///
233  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
234                                               formatted_raw_ostream &,
235                                               CodeGenFileType,
236                                               CodeGenOpt::Level) {
237    return FileModel::None;
238  }
239
240  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
241  /// to be split up (e.g., to add an object writer pass), this method can be
242  /// used to finish up adding passes to emit the file, if necessary.
243  ///
244  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
245                                         MachineCodeEmitter *,
246                                         CodeGenOpt::Level) {
247    return true;
248  }
249
250  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
251  /// to be split up (e.g., to add an object writer pass), this method can be
252  /// used to finish up adding passes to emit the file, if necessary.
253  ///
254  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
255                                         JITCodeEmitter *,
256                                         CodeGenOpt::Level) {
257    return true;
258  }
259
260  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
261  /// to be split up (e.g., to add an object writer pass), this method can be
262  /// used to finish up adding passes to emit the file, if necessary.
263  ///
264  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
265                                         ObjectCodeEmitter *,
266                                         CodeGenOpt::Level) {
267    return true;
268  }
269
270  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
271  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
272  /// actually outputting the machine code and resolving things like the address
273  /// of functions.  This method returns true if machine code emission is
274  /// not supported.
275  ///
276  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
277                                          MachineCodeEmitter &,
278                                          CodeGenOpt::Level) {
279    return true;
280  }
281
282  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
283  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
284  /// actually outputting the machine code and resolving things like the address
285  /// of functions.  This method returns true if machine code emission is
286  /// not supported.
287  ///
288  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
289                                          JITCodeEmitter &,
290                                          CodeGenOpt::Level) {
291    return true;
292  }
293
294  /// addPassesToEmitWholeFile - This method can be implemented by targets that
295  /// require having the entire module at once.  This is not recommended, do not
296  /// use this.
297  virtual bool WantsWholeFile() const { return false; }
298  virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
299                                        CodeGenFileType,
300                                        CodeGenOpt::Level) {
301    return true;
302  }
303};
304
305/// LLVMTargetMachine - This class describes a target machine that is
306/// implemented with the LLVM target-independent code generator.
307///
308class LLVMTargetMachine : public TargetMachine {
309protected: // Can only create subclasses.
310  LLVMTargetMachine(const Target &T, const std::string &TargetTriple);
311
312  /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
313  /// both emitting to assembly files or machine code output.
314  ///
315  bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
316
317private:
318  // These routines are used by addPassesToEmitFileFinish and
319  // addPassesToEmitMachineCode to set the CodeModel if it's still marked
320  // as default.
321  virtual void setCodeModelForJIT();
322  virtual void setCodeModelForStatic();
323
324public:
325
326  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
327  /// specified file emitted.  Typically this will involve several steps of code
328  /// generation.  If OptLevel is None, the code generator should emit code as fast
329  /// as possible, though the generated code may be less efficient.  This method
330  /// should return FileModel::Error if emission of this file type is not
331  /// supported.
332  ///
333  /// The default implementation of this method adds components from the
334  /// LLVM retargetable code generator, invoking the methods below to get
335  /// target-specific passes in standard locations.
336  ///
337  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
338                                               formatted_raw_ostream &Out,
339                                               CodeGenFileType FileType,
340                                               CodeGenOpt::Level);
341
342  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
343  /// to be split up (e.g., to add an object writer pass), this method can be
344  /// used to finish up adding passes to emit the file, if necessary.
345  ///
346  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
347                                         MachineCodeEmitter *MCE,
348                                         CodeGenOpt::Level);
349
350  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
351  /// to be split up (e.g., to add an object writer pass), this method can be
352  /// used to finish up adding passes to emit the file, if necessary.
353  ///
354  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
355                                         JITCodeEmitter *JCE,
356                                         CodeGenOpt::Level);
357
358  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
359  /// to be split up (e.g., to add an object writer pass), this method can be
360  /// used to finish up adding passes to emit the file, if necessary.
361  ///
362  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
363                                         ObjectCodeEmitter *OCE,
364                                         CodeGenOpt::Level);
365
366  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
367  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
368  /// actually outputting the machine code and resolving things like the address
369  /// of functions.  This method returns true if machine code emission is
370  /// not supported.
371  ///
372  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
373                                          MachineCodeEmitter &MCE,
374                                          CodeGenOpt::Level);
375
376  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
377  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
378  /// actually outputting the machine code and resolving things like the address
379  /// of functions.  This method returns true if machine code emission is
380  /// not supported.
381  ///
382  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
383                                          JITCodeEmitter &MCE,
384                                          CodeGenOpt::Level);
385
386  /// Target-Independent Code Generator Pass Configuration Options.
387
388  /// addInstSelector - This method should add any "last minute" LLVM->LLVM
389  /// passes, then install an instruction selector pass, which converts from
390  /// LLVM code to machine instructions.
391  virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
392    return true;
393  }
394
395  /// addPreRegAlloc - This method may be implemented by targets that want to
396  /// run passes immediately before register allocation. This should return
397  /// true if -print-machineinstrs should print after these passes.
398  virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
399    return false;
400  }
401
402  /// addPostRegAlloc - This method may be implemented by targets that want
403  /// to run passes after register allocation but before prolog-epilog
404  /// insertion.  This should return true if -print-machineinstrs should print
405  /// after these passes.
406  virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
407    return false;
408  }
409
410  /// addPreSched2 - This method may be implemented by targets that want to
411  /// run passes after prolog-epilog insertion and before the second instruction
412  /// scheduling pass.  This should return true if -print-machineinstrs should
413  /// print after these passes.
414  virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
415    return false;
416  }
417
418  /// addPreEmitPass - This pass may be implemented by targets that want to run
419  /// passes immediately before machine code is emitted.  This should return
420  /// true if -print-machineinstrs should print out the code after the passes.
421  virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
422    return false;
423  }
424
425
426  /// addCodeEmitter - This pass should be overridden by the target to add a
427  /// code emitter, if supported.  If this is not supported, 'true' should be
428  /// returned.
429  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
430                              MachineCodeEmitter &) {
431    return true;
432  }
433
434  /// addCodeEmitter - This pass should be overridden by the target to add a
435  /// code emitter, if supported.  If this is not supported, 'true' should be
436  /// returned.
437  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
438                              JITCodeEmitter &) {
439    return true;
440  }
441
442  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
443  /// a code emitter (without setting flags), if supported.  If this is not
444  /// supported, 'true' should be returned.
445  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
446                                    MachineCodeEmitter &) {
447    return true;
448  }
449
450  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
451  /// a code emitter (without setting flags), if supported.  If this is not
452  /// supported, 'true' should be returned.
453  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
454                                    JITCodeEmitter &) {
455    return true;
456  }
457
458  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
459  /// a code emitter (without setting flags), if supported.  If this is not
460  /// supported, 'true' should be returned.
461  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
462                                    ObjectCodeEmitter &) {
463    return true;
464  }
465
466  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
467  /// on this target.  User flag overrides.
468  virtual bool getEnableTailMergeDefault() const { return true; }
469
470  /// addAssemblyEmitter - Helper function which creates a target specific
471  /// assembly printer, if available.
472  ///
473  /// \return Returns 'false' on success.
474  bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
475                          bool /* VerboseAsmDefault */,
476                          formatted_raw_ostream &);
477
478  /// addObjectFileEmitter - Helper function which creates a target specific
479  /// object files emitter, if available.  This interface is temporary, for
480  /// bringing up MCAssembler-based object file emitters.
481  ///
482  /// \return Returns 'false' on success.
483  bool addObjectFileEmitter(PassManagerBase &, CodeGenOpt::Level,
484                            formatted_raw_ostream &);
485};
486
487} // End llvm namespace
488
489#endif
490