TargetMachine.h revision a8c18890da312e810c687b78658dcd4c989b9776
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.
205  /// FIXME: This call-back isn't good! We should be using the correct encoding
206  /// regardless of the system. However, there are some systems which have bugs
207  /// that prevent this from occuring.
208  virtual DwarfLSDAEncoding::Encoding getLSDAEncoding() const {
209    return DwarfLSDAEncoding::Default;
210  }
211
212  /// CodeGenFileType - These enums are meant to be passed into
213  /// addPassesToEmitFile to indicate what type of file to emit.
214  enum CodeGenFileType {
215    AssemblyFile, ObjectFile, DynamicLibrary
216  };
217
218  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
219  /// on this target.  User flag overrides.
220  virtual bool getEnableTailMergeDefault() const { return true; }
221
222  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
223  /// specified file emitted.  Typically this will involve several steps of code
224  /// generation.
225  /// This method should return FileModel::Error if emission of this file type
226  /// is not supported.
227  ///
228  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
229                                               formatted_raw_ostream &,
230                                               CodeGenFileType,
231                                               CodeGenOpt::Level) {
232    return FileModel::None;
233  }
234
235  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
236  /// to be split up (e.g., to add an object writer pass), this method can be
237  /// used to finish up adding passes to emit the file, if necessary.
238  ///
239  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
240                                         MachineCodeEmitter *,
241                                         CodeGenOpt::Level) {
242    return true;
243  }
244
245  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
246  /// to be split up (e.g., to add an object writer pass), this method can be
247  /// used to finish up adding passes to emit the file, if necessary.
248  ///
249  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
250                                         JITCodeEmitter *,
251                                         CodeGenOpt::Level) {
252    return true;
253  }
254
255  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
256  /// to be split up (e.g., to add an object writer pass), this method can be
257  /// used to finish up adding passes to emit the file, if necessary.
258  ///
259  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
260                                         ObjectCodeEmitter *,
261                                         CodeGenOpt::Level) {
262    return true;
263  }
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(PassManagerBase &,
272                                          MachineCodeEmitter &,
273                                          CodeGenOpt::Level) {
274    return true;
275  }
276
277  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
278  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
279  /// actually outputting the machine code and resolving things like the address
280  /// of functions.  This method returns true if machine code emission is
281  /// not supported.
282  ///
283  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
284                                          JITCodeEmitter &,
285                                          CodeGenOpt::Level) {
286    return true;
287  }
288
289  /// addPassesToEmitWholeFile - This method can be implemented by targets that
290  /// require having the entire module at once.  This is not recommended, do not
291  /// use this.
292  virtual bool WantsWholeFile() const { return false; }
293  virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
294                                        CodeGenFileType,
295                                        CodeGenOpt::Level) {
296    return true;
297  }
298};
299
300/// LLVMTargetMachine - This class describes a target machine that is
301/// implemented with the LLVM target-independent code generator.
302///
303class LLVMTargetMachine : public TargetMachine {
304protected: // Can only create subclasses.
305  LLVMTargetMachine(const Target &T, const std::string &TargetTriple);
306
307  /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
308  /// both emitting to assembly files or machine code output.
309  ///
310  bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
311
312private:
313  // These routines are used by addPassesToEmitFileFinish and
314  // addPassesToEmitMachineCode to set the CodeModel if it's still marked
315  // as default.
316  virtual void setCodeModelForJIT();
317  virtual void setCodeModelForStatic();
318
319public:
320
321  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
322  /// specified file emitted.  Typically this will involve several steps of code
323  /// generation.  If OptLevel is None, the code generator should emit code as fast
324  /// as possible, though the generated code may be less efficient.  This method
325  /// should return FileModel::Error if emission of this file type is not
326  /// supported.
327  ///
328  /// The default implementation of this method adds components from the
329  /// LLVM retargetable code generator, invoking the methods below to get
330  /// target-specific passes in standard locations.
331  ///
332  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
333                                               formatted_raw_ostream &Out,
334                                               CodeGenFileType FileType,
335                                               CodeGenOpt::Level);
336
337  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
338  /// to be split up (e.g., to add an object writer pass), this method can be
339  /// used to finish up adding passes to emit the file, if necessary.
340  ///
341  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
342                                         MachineCodeEmitter *MCE,
343                                         CodeGenOpt::Level);
344
345  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
346  /// to be split up (e.g., to add an object writer pass), this method can be
347  /// used to finish up adding passes to emit the file, if necessary.
348  ///
349  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
350                                         JITCodeEmitter *JCE,
351                                         CodeGenOpt::Level);
352
353  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
354  /// to be split up (e.g., to add an object writer pass), this method can be
355  /// used to finish up adding passes to emit the file, if necessary.
356  ///
357  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
358                                         ObjectCodeEmitter *OCE,
359                                         CodeGenOpt::Level);
360
361  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
362  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
363  /// actually outputting the machine code and resolving things like the address
364  /// of functions.  This method returns true if machine code emission is
365  /// not supported.
366  ///
367  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
368                                          MachineCodeEmitter &MCE,
369                                          CodeGenOpt::Level);
370
371  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
372  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
373  /// actually outputting the machine code and resolving things like the address
374  /// of functions.  This method returns true if machine code emission is
375  /// not supported.
376  ///
377  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
378                                          JITCodeEmitter &MCE,
379                                          CodeGenOpt::Level);
380
381  /// Target-Independent Code Generator Pass Configuration Options.
382
383  /// addInstSelector - This method should add any "last minute" LLVM->LLVM
384  /// passes, then install an instruction selector pass, which converts from
385  /// LLVM code to machine instructions.
386  virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
387    return true;
388  }
389
390  /// addPreRegAlloc - This method may be implemented by targets that want to
391  /// run passes immediately before register allocation. This should return
392  /// true if -print-machineinstrs should print after these passes.
393  virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
394    return false;
395  }
396
397  /// addPostRegAlloc - This method may be implemented by targets that want
398  /// to run passes after register allocation but before prolog-epilog
399  /// insertion.  This should return true if -print-machineinstrs should print
400  /// after these passes.
401  virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
402    return false;
403  }
404
405  /// addPreSched2 - This method may be implemented by targets that want to
406  /// run passes after prolog-epilog insertion and before the second instruction
407  /// scheduling pass.  This should return true if -print-machineinstrs should
408  /// print after these passes.
409  virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
410    return false;
411  }
412
413  /// addPreEmitPass - This pass may be implemented by targets that want to run
414  /// passes immediately before machine code is emitted.  This should return
415  /// true if -print-machineinstrs should print out the code after the passes.
416  virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
417    return false;
418  }
419
420
421  /// addCodeEmitter - This pass should be overridden by the target to add a
422  /// code emitter, if supported.  If this is not supported, 'true' should be
423  /// returned.
424  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
425                              MachineCodeEmitter &) {
426    return true;
427  }
428
429  /// addCodeEmitter - This pass should be overridden by the target to add a
430  /// code emitter, if supported.  If this is not supported, 'true' should be
431  /// returned.
432  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
433                              JITCodeEmitter &) {
434    return true;
435  }
436
437  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
438  /// a code emitter (without setting flags), if supported.  If this is not
439  /// supported, 'true' should be returned.
440  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
441                                    MachineCodeEmitter &) {
442    return true;
443  }
444
445  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
446  /// a code emitter (without setting flags), if supported.  If this is not
447  /// supported, 'true' should be returned.
448  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
449                                    JITCodeEmitter &) {
450    return true;
451  }
452
453  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
454  /// a code emitter (without setting flags), if supported.  If this is not
455  /// supported, 'true' should be returned.
456  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
457                                    ObjectCodeEmitter &) {
458    return true;
459  }
460
461  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
462  /// on this target.  User flag overrides.
463  virtual bool getEnableTailMergeDefault() const { return true; }
464
465  /// addAssemblyEmitter - Helper function which creates a target specific
466  /// assembly printer, if available.
467  ///
468  /// \return Returns 'false' on success.
469  bool addAssemblyEmitter(PassManagerBase &, CodeGenOpt::Level,
470                          bool /* VerboseAsmDefault */,
471                          formatted_raw_ostream &);
472
473  /// addObjectFileEmitter - Helper function which creates a target specific
474  /// object files emitter, if available.  This interface is temporary, for
475  /// bringing up MCAssembler-based object file emitters.
476  ///
477  /// \return Returns 'false' on success.
478  bool addObjectFileEmitter(PassManagerBase &, CodeGenOpt::Level,
479                            formatted_raw_ostream &);
480};
481
482} // End llvm namespace
483
484#endif
485