TargetMachine.h revision 0823d2a654cb3a075016f6efd21359ed4f5aca21
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    return *static_cast<const STC*>(getSubtargetImpl());
141  }
142
143  /// getRegisterInfo - If register information is available, return it.  If
144  /// not, return null.  This is kept separate from RegInfo until RegInfo has
145  /// details of graph coloring register allocation removed from it.
146  ///
147  virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
148
149  /// getIntrinsicInfo - If intrinsic information is available, return it.  If
150  /// not, return null.
151  ///
152  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
153
154  /// getJITInfo - If this target supports a JIT, return information for it,
155  /// otherwise return null.
156  ///
157  virtual TargetJITInfo *getJITInfo() { return 0; }
158
159  /// getInstrItineraryData - Returns instruction itinerary data for the target
160  /// or specific subtarget.
161  ///
162  virtual const InstrItineraryData getInstrItineraryData() const {
163    return InstrItineraryData();
164  }
165
166  /// getMachOWriterInfo - If this target supports a Mach-O writer, return
167  /// information for it, otherwise return null.
168  ///
169  virtual const TargetMachOWriterInfo *getMachOWriterInfo() const { return 0; }
170
171  /// getELFWriterInfo - If this target supports an ELF writer, return
172  /// information for it, otherwise return null.
173  ///
174  virtual const TargetELFWriterInfo *getELFWriterInfo() const { return 0; }
175
176  /// getRelocationModel - Returns the code generation relocation model. The
177  /// choices are static, PIC, and dynamic-no-pic, and target default.
178  static Reloc::Model getRelocationModel();
179
180  /// setRelocationModel - Sets the code generation relocation model.
181  ///
182  static void setRelocationModel(Reloc::Model Model);
183
184  /// getCodeModel - Returns the code model. The choices are small, kernel,
185  /// medium, large, and target default.
186  static CodeModel::Model getCodeModel();
187
188  /// setCodeModel - Sets the code model.
189  ///
190  static void setCodeModel(CodeModel::Model Model);
191
192  /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
193  ///
194  static bool getAsmVerbosityDefault();
195
196  /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
197  /// is false.
198  static void setAsmVerbosityDefault(bool);
199
200  /// getLSDAEncoding - Returns the LSDA pointer encoding. The choices are
201  /// 4-byte, 8-byte, and target default. The CIE is hard-coded to indicate that
202  /// the LSDA pointer in the FDE section is an "sdata4", and should be encoded
203  /// as a 4-byte pointer by default. However, some systems may require a
204  /// different size due to bugs or other conditions. We will default to a
205  /// 4-byte encoding unless the system tells us otherwise.
206  ///
207  /// FIXME: This call-back isn't good! We should be using the correct encoding
208  /// regardless of the system. However, there are some systems which have bugs
209  /// that prevent this from occuring.
210  virtual DwarfLSDAEncoding::Encoding getLSDAEncoding() const {
211    return DwarfLSDAEncoding::Default;
212  }
213
214  /// CodeGenFileType - These enums are meant to be passed into
215  /// addPassesToEmitFile to indicate what type of file to emit.
216  enum CodeGenFileType {
217    AssemblyFile, ObjectFile, DynamicLibrary
218  };
219
220  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
221  /// on this target.  User flag overrides.
222  virtual bool getEnableTailMergeDefault() const { return true; }
223
224  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
225  /// specified file emitted.  Typically this will involve several steps of code
226  /// generation.
227  /// This method should return FileModel::Error if emission of this file type
228  /// is not supported.
229  ///
230  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &,
231                                               formatted_raw_ostream &,
232                                               CodeGenFileType,
233                                               CodeGenOpt::Level) {
234    return FileModel::None;
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                                         MachineCodeEmitter *,
243                                         CodeGenOpt::Level) {
244    return true;
245  }
246
247  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
248  /// to be split up (e.g., to add an object writer pass), this method can be
249  /// used to finish up adding passes to emit the file, if necessary.
250  ///
251  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
252                                         JITCodeEmitter *,
253                                         CodeGenOpt::Level) {
254    return true;
255  }
256
257  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
258  /// to be split up (e.g., to add an object writer pass), this method can be
259  /// used to finish up adding passes to emit the file, if necessary.
260  ///
261  virtual bool addPassesToEmitFileFinish(PassManagerBase &,
262                                         ObjectCodeEmitter *,
263                                         CodeGenOpt::Level) {
264    return true;
265  }
266
267  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
268  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
269  /// actually outputting the machine code and resolving things like the address
270  /// of functions.  This method returns true if machine code emission is
271  /// not supported.
272  ///
273  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
274                                          MachineCodeEmitter &,
275                                          CodeGenOpt::Level) {
276    return true;
277  }
278
279  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
280  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
281  /// actually outputting the machine code and resolving things like the address
282  /// of functions.  This method returns true if machine code emission is
283  /// not supported.
284  ///
285  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
286                                          JITCodeEmitter &,
287                                          CodeGenOpt::Level) {
288    return true;
289  }
290
291  /// addPassesToEmitWholeFile - This method can be implemented by targets that
292  /// require having the entire module at once.  This is not recommended, do not
293  /// use this.
294  virtual bool WantsWholeFile() const { return false; }
295  virtual bool addPassesToEmitWholeFile(PassManager &, formatted_raw_ostream &,
296                                        CodeGenFileType,
297                                        CodeGenOpt::Level) {
298    return true;
299  }
300};
301
302/// LLVMTargetMachine - This class describes a target machine that is
303/// implemented with the LLVM target-independent code generator.
304///
305class LLVMTargetMachine : public TargetMachine {
306protected: // Can only create subclasses.
307  LLVMTargetMachine(const Target &T, const std::string &TargetTriple);
308
309  /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
310  /// both emitting to assembly files or machine code output.
311  ///
312  bool addCommonCodeGenPasses(PassManagerBase &, CodeGenOpt::Level);
313
314private:
315  // These routines are used by addPassesToEmitFileFinish and
316  // addPassesToEmitMachineCode to set the CodeModel if it's still marked
317  // as default.
318  virtual void setCodeModelForJIT();
319  virtual void setCodeModelForStatic();
320
321public:
322
323  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
324  /// specified file emitted.  Typically this will involve several steps of code
325  /// generation.  If OptLevel is None, the code generator should emit code as fast
326  /// as possible, though the generated code may be less efficient.  This method
327  /// should return FileModel::Error if emission of this file type is not
328  /// supported.
329  ///
330  /// The default implementation of this method adds components from the
331  /// LLVM retargetable code generator, invoking the methods below to get
332  /// target-specific passes in standard locations.
333  ///
334  virtual FileModel::Model addPassesToEmitFile(PassManagerBase &PM,
335                                               formatted_raw_ostream &Out,
336                                               CodeGenFileType FileType,
337                                               CodeGenOpt::Level);
338
339  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
340  /// to be split up (e.g., to add an object writer pass), this method can be
341  /// used to finish up adding passes to emit the file, if necessary.
342  ///
343  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
344                                         MachineCodeEmitter *MCE,
345                                         CodeGenOpt::Level);
346
347  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
348  /// to be split up (e.g., to add an object writer pass), this method can be
349  /// used to finish up adding passes to emit the file, if necessary.
350  ///
351  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
352                                         JITCodeEmitter *JCE,
353                                         CodeGenOpt::Level);
354
355  /// addPassesToEmitFileFinish - If the passes to emit the specified file had
356  /// to be split up (e.g., to add an object writer pass), this method can be
357  /// used to finish up adding passes to emit the file, if necessary.
358  ///
359  virtual bool addPassesToEmitFileFinish(PassManagerBase &PM,
360                                         ObjectCodeEmitter *OCE,
361                                         CodeGenOpt::Level);
362
363  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
364  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
365  /// actually outputting the machine code and resolving things like the address
366  /// of functions.  This method returns true if machine code emission is
367  /// not supported.
368  ///
369  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
370                                          MachineCodeEmitter &MCE,
371                                          CodeGenOpt::Level);
372
373  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
374  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
375  /// actually outputting the machine code and resolving things like the address
376  /// of functions.  This method returns true if machine code emission is
377  /// not supported.
378  ///
379  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
380                                          JITCodeEmitter &MCE,
381                                          CodeGenOpt::Level);
382
383  /// Target-Independent Code Generator Pass Configuration Options.
384
385  /// addInstSelector - This method should add any "last minute" LLVM->LLVM
386  /// passes, then install an instruction selector pass, which converts from
387  /// LLVM code to machine instructions.
388  virtual bool addInstSelector(PassManagerBase &, CodeGenOpt::Level) {
389    return true;
390  }
391
392  /// addPreRegAlloc - This method may be implemented by targets that want to
393  /// run passes immediately before register allocation. This should return
394  /// true if -print-machineinstrs should print after these passes.
395  virtual bool addPreRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
396    return false;
397  }
398
399  /// addPostRegAlloc - This method may be implemented by targets that want
400  /// to run passes after register allocation but before prolog-epilog
401  /// insertion.  This should return true if -print-machineinstrs should print
402  /// after these passes.
403  virtual bool addPostRegAlloc(PassManagerBase &, CodeGenOpt::Level) {
404    return false;
405  }
406
407  /// addPreSched2 - This method may be implemented by targets that want to
408  /// run passes after prolog-epilog insertion and before the second instruction
409  /// scheduling pass.  This should return true if -print-machineinstrs should
410  /// print after these passes.
411  virtual bool addPreSched2(PassManagerBase &, CodeGenOpt::Level) {
412    return false;
413  }
414
415  /// addPreEmitPass - This pass may be implemented by targets that want to run
416  /// passes immediately before machine code is emitted.  This should return
417  /// true if -print-machineinstrs should print out the code after the passes.
418  virtual bool addPreEmitPass(PassManagerBase &, CodeGenOpt::Level) {
419    return false;
420  }
421
422
423  /// addCodeEmitter - This pass should be overridden by the target to add a
424  /// code emitter, if supported.  If this is not supported, 'true' should be
425  /// returned.
426  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
427                              MachineCodeEmitter &) {
428    return true;
429  }
430
431  /// addCodeEmitter - This pass should be overridden by the target to add a
432  /// code emitter, if supported.  If this is not supported, 'true' should be
433  /// returned.
434  virtual bool addCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
435                              JITCodeEmitter &) {
436    return true;
437  }
438
439  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
440  /// a code emitter (without setting flags), if supported.  If this is not
441  /// supported, 'true' should be returned.
442  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
443                                    MachineCodeEmitter &) {
444    return true;
445  }
446
447  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
448  /// a code emitter (without setting flags), if supported.  If this is not
449  /// supported, 'true' should be returned.
450  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
451                                    JITCodeEmitter &) {
452    return true;
453  }
454
455  /// addSimpleCodeEmitter - This pass should be overridden by the target to add
456  /// a code emitter (without setting flags), if supported.  If this is not
457  /// supported, 'true' should be returned.
458  virtual bool addSimpleCodeEmitter(PassManagerBase &, CodeGenOpt::Level,
459                                    ObjectCodeEmitter &) {
460    return true;
461  }
462
463  /// getEnableTailMergeDefault - the default setting for -enable-tail-merge
464  /// on this target.  User flag overrides.
465  virtual bool getEnableTailMergeDefault() const { return true; }
466};
467
468} // End llvm namespace
469
470#endif
471