TargetMachine.h revision 152ed053533b26194362994398c0ffad7e1c4109
1//===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file describes the general parts of a Target machine.
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 TargetData;
24class TargetSubtarget;
25class TargetInstrInfo;
26class TargetInstrDescriptor;
27class TargetJITInfo;
28class TargetLowering;
29class TargetFrameInfo;
30class MachineCodeEmitter;
31class MRegisterInfo;
32class Module;
33class FunctionPassManager;
34class PassManager;
35class Pass;
36
37// Relocation model types.
38namespace Reloc {
39  enum Model {
40    Default,
41    Static,
42    PIC,
43    DynamicNoPIC
44  };
45}
46
47// Code model types.
48namespace CodeModel {
49  enum Model {
50    Default,
51    Small,
52    Kernel,
53    Medium,
54    Large
55  };
56}
57
58//===----------------------------------------------------------------------===//
59///
60/// TargetMachine - Primary interface to the complete machine description for
61/// the target machine.  All target-specific information should be accessible
62/// through this interface.
63///
64class TargetMachine {
65  const std::string Name;
66
67  TargetMachine(const TargetMachine&);   // DO NOT IMPLEMENT
68  void operator=(const TargetMachine&);  // DO NOT IMPLEMENT
69protected: // Can only create subclasses...
70  TargetMachine(const std::string &name) : Name(name) { };
71
72  /// This constructor is used for targets that support arbitrary TargetData
73  /// layouts, like the C backend.  It initializes the TargetData to match that
74  /// of the specified module.
75  ///
76  TargetMachine(const std::string &name, const Module &M);
77
78  /// getSubtargetImpl - virtual method implemented by subclasses that returns
79  /// a reference to that target's TargetSubtarget-derived member variable.
80  virtual const TargetSubtarget *getSubtargetImpl() const { return 0; }
81public:
82  virtual ~TargetMachine();
83
84  /// getModuleMatchQuality - This static method should be implemented by
85  /// targets to indicate how closely they match the specified module.  This is
86  /// used by the LLC tool to determine which target to use when an explicit
87  /// -march option is not specified.  If a target returns zero, it will never
88  /// be chosen without an explicit -march option.
89  static unsigned getModuleMatchQuality(const Module &M) { return 0; }
90
91  /// getJITMatchQuality - This static method should be implemented by targets
92  /// that provide JIT capabilities to indicate how suitable they are for
93  /// execution on the current host.  If a value of 0 is returned, the target
94  /// will not be used unless an explicit -march option is used.
95  static unsigned getJITMatchQuality() { return 0; }
96
97
98  const std::string &getName() const { return Name; }
99
100  // Interfaces to the major aspects of target machine information:
101  // -- Instruction opcode and operand information
102  // -- Pipelines and scheduling information
103  // -- Stack frame information
104  // -- Selection DAG lowering information
105  //
106  virtual const TargetInstrInfo        *getInstrInfo() const { return 0; }
107  virtual const TargetFrameInfo        *getFrameInfo() const { return 0; }
108  virtual       TargetLowering    *getTargetLowering() const { return 0; }
109  virtual const TargetData            *getTargetData() const { return 0; }
110
111  /// getSubtarget - This method returns a pointer to the specified type of
112  /// TargetSubtarget.  In debug builds, it verifies that the object being
113  /// returned is of the correct type.
114  template<typename STC> const STC &getSubtarget() const {
115    const TargetSubtarget *TST = getSubtargetImpl();
116    assert(TST && dynamic_cast<const STC*>(TST) &&
117           "Not the right kind of subtarget!");
118    return *static_cast<const STC*>(TST);
119  }
120
121  /// getRegisterInfo - If register information is available, return it.  If
122  /// not, return null.  This is kept separate from RegInfo until RegInfo has
123  /// details of graph coloring register allocation removed from it.
124  ///
125  virtual const MRegisterInfo*          getRegisterInfo() const { return 0; }
126
127  /// getJITInfo - If this target supports a JIT, return information for it,
128  /// otherwise return null.
129  ///
130  virtual TargetJITInfo *getJITInfo() { return 0; }
131
132  /// getInstrItineraryData - Returns instruction itinerary data for the target
133  /// or specific subtarget.
134  ///
135  virtual const InstrItineraryData getInstrItineraryData() const {
136    return InstrItineraryData();
137  }
138
139  /// getRelocationModel - Returns the code generation relocation model. The
140  /// choices are static, PIC, and dynamic-no-pic, and target default.
141  static Reloc::Model getRelocationModel();
142
143  /// setRelocationModel - Sets the code generation relocation model.
144  static void setRelocationModel(Reloc::Model Model);
145
146  /// getCodeModel - Returns the code model. The choices are small, kernel,
147  /// medium, large, and target default.
148  static CodeModel::Model getCodeModel();
149
150  /// setCodeModel - Sets the code model.
151  static void setCodeModel(CodeModel::Model Model);
152
153  /// CodeGenFileType - These enums are meant to be passed into
154  /// addPassesToEmitFile to indicate what type of file to emit.
155  enum CodeGenFileType {
156    AssemblyFile, ObjectFile, DynamicLibrary
157  };
158
159  /// addPassesToEmitFile - Add passes to the specified pass manager to get
160  /// the specified file emitted.  Typically this will involve several steps of
161  /// code generation.  If Fast is set to true, the code generator should emit
162  /// code as fast as possible, without regard for compile time.  This method
163  /// should return true if emission of this file type is not supported.
164  ///
165  virtual bool addPassesToEmitFile(PassManager &PM, std::ostream &Out,
166                                   CodeGenFileType FileType, bool Fast) {
167    return true;
168  }
169
170  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
171  /// get machine code emitted.  This uses a MachineCodeEmitter object to handle
172  /// actually outputting the machine code and resolving things like the address
173  /// of functions.  This method should returns true if machine code emission is
174  /// not supported.
175  ///
176  virtual bool addPassesToEmitMachineCode(FunctionPassManager &PM,
177                                          MachineCodeEmitter &MCE) {
178    return true;
179  }
180};
181
182} // End llvm namespace
183
184#endif
185