1193323Sed//===-- llvm/Target/TargetMachine.h - Target Information --------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file defines the TargetMachine and LLVMTargetMachine classes.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef LLVM_TARGET_TARGETMACHINE_H
15193323Sed#define LLVM_TARGET_TARGETMACHINE_H
16193323Sed
17249423Sdim#include "llvm/ADT/StringRef.h"
18239462Sdim#include "llvm/Pass.h"
19234353Sdim#include "llvm/Support/CodeGen.h"
20234353Sdim#include "llvm/Target/TargetOptions.h"
21193323Sed#include <cassert>
22198090Srdivacky#include <string>
23193323Sed
24193323Sednamespace llvm {
25193323Sed
26224145Sdimclass InstrItineraryData;
27224145Sdimclass JITCodeEmitter;
28234353Sdimclass GlobalValue;
29224145Sdimclass MCAsmInfo;
30226633Sdimclass MCCodeGenInfo;
31224145Sdimclass MCContext;
32224145Sdimclass PassManagerBase;
33198090Srdivackyclass Target;
34243830Sdimclass DataLayout;
35251662Sdimclass TargetLibraryInfo;
36224145Sdimclass TargetFrameLowering;
37193323Sedclass TargetInstrInfo;
38193323Sedclass TargetIntrinsicInfo;
39193323Sedclass TargetJITInfo;
40193323Sedclass TargetLowering;
41234353Sdimclass TargetPassConfig;
42224145Sdimclass TargetRegisterInfo;
43207618Srdivackyclass TargetSelectionDAGInfo;
44224145Sdimclass TargetSubtargetInfo;
45249423Sdimclass ScalarTargetTransformInfo;
46249423Sdimclass VectorTargetTransformInfo;
47198090Srdivackyclass formatted_raw_ostream;
48221345Sdimclass raw_ostream;
49193323Sed
50193323Sed//===----------------------------------------------------------------------===//
51193323Sed///
52193323Sed/// TargetMachine - Primary interface to the complete machine description for
53193323Sed/// the target machine.  All target-specific information should be accessible
54193323Sed/// through this interface.
55193323Sed///
56193323Sedclass TargetMachine {
57243830Sdim  TargetMachine(const TargetMachine &) LLVM_DELETED_FUNCTION;
58243830Sdim  void operator=(const TargetMachine &) LLVM_DELETED_FUNCTION;
59193323Sedprotected: // Can only create subclasses.
60224145Sdim  TargetMachine(const Target &T, StringRef TargetTriple,
61234353Sdim                StringRef CPU, StringRef FS, const TargetOptions &Options);
62193323Sed
63198090Srdivacky  /// TheTarget - The Target that this machine was created for.
64198090Srdivacky  const Target &TheTarget;
65218893Sdim
66224145Sdim  /// TargetTriple, TargetCPU, TargetFS - Triple string, CPU name, and target
67224145Sdim  /// feature strings the TargetMachine instance is created with.
68224145Sdim  std::string TargetTriple;
69224145Sdim  std::string TargetCPU;
70224145Sdim  std::string TargetFS;
71224145Sdim
72226633Sdim  /// CodeGenInfo - Low level target information such as relocation model.
73226633Sdim  const MCCodeGenInfo *CodeGenInfo;
74226633Sdim
75193323Sed  /// AsmInfo - Contains target specific asm information.
76193323Sed  ///
77198090Srdivacky  const MCAsmInfo *AsmInfo;
78208599Srdivacky
79208599Srdivacky  unsigned MCRelaxAll : 1;
80218893Sdim  unsigned MCNoExecStack : 1;
81221345Sdim  unsigned MCSaveTempLabels : 1;
82218893Sdim  unsigned MCUseLoc : 1;
83221345Sdim  unsigned MCUseCFI : 1;
84234353Sdim  unsigned MCUseDwarfDirectory : 1;
85208599Srdivacky
86193323Sedpublic:
87193323Sed  virtual ~TargetMachine();
88193323Sed
89198090Srdivacky  const Target &getTarget() const { return TheTarget; }
90193323Sed
91224145Sdim  const StringRef getTargetTriple() const { return TargetTriple; }
92224145Sdim  const StringRef getTargetCPU() const { return TargetCPU; }
93224145Sdim  const StringRef getTargetFeatureString() const { return TargetFS; }
94224145Sdim
95249423Sdim  /// getSubtargetImpl - virtual method implemented by subclasses that returns
96249423Sdim  /// a reference to that target's TargetSubtargetInfo-derived member variable.
97249423Sdim  virtual const TargetSubtargetInfo *getSubtargetImpl() const { return 0; }
98234353Sdim
99249423Sdim  mutable TargetOptions Options;
100249423Sdim
101249423Sdim  /// \brief Reset the target options based on the function's attributes.
102249423Sdim  void resetTargetOptions(const MachineFunction *MF) const;
103249423Sdim
104193323Sed  // Interfaces to the major aspects of target machine information:
105193323Sed  // -- Instruction opcode and operand information
106193323Sed  // -- Pipelines and scheduling information
107193323Sed  // -- Stack frame information
108193323Sed  // -- Selection DAG lowering information
109193323Sed  //
110218893Sdim  virtual const TargetInstrInfo         *getInstrInfo() const { return 0; }
111218893Sdim  virtual const TargetFrameLowering *getFrameLowering() const { return 0; }
112207618Srdivacky  virtual const TargetLowering    *getTargetLowering() const { return 0; }
113207618Srdivacky  virtual const TargetSelectionDAGInfo *getSelectionDAGInfo() const{ return 0; }
114243830Sdim  virtual const DataLayout             *getDataLayout() const { return 0; }
115218893Sdim
116198090Srdivacky  /// getMCAsmInfo - Return target specific asm information.
117193323Sed  ///
118198090Srdivacky  const MCAsmInfo *getMCAsmInfo() const { return AsmInfo; }
119218893Sdim
120193323Sed  /// getSubtarget - This method returns a pointer to the specified type of
121224145Sdim  /// TargetSubtargetInfo.  In debug builds, it verifies that the object being
122193323Sed  /// returned is of the correct type.
123193323Sed  template<typename STC> const STC &getSubtarget() const {
124202878Srdivacky    return *static_cast<const STC*>(getSubtargetImpl());
125193323Sed  }
126193323Sed
127193323Sed  /// getRegisterInfo - If register information is available, return it.  If
128193323Sed  /// not, return null.  This is kept separate from RegInfo until RegInfo has
129193323Sed  /// details of graph coloring register allocation removed from it.
130193323Sed  ///
131193323Sed  virtual const TargetRegisterInfo *getRegisterInfo() const { return 0; }
132218893Sdim
133193323Sed  /// getIntrinsicInfo - If intrinsic information is available, return it.  If
134193323Sed  /// not, return null.
135193323Sed  ///
136193323Sed  virtual const TargetIntrinsicInfo *getIntrinsicInfo() const { return 0; }
137193323Sed
138193323Sed  /// getJITInfo - If this target supports a JIT, return information for it,
139193323Sed  /// otherwise return null.
140193323Sed  ///
141193323Sed  virtual TargetJITInfo *getJITInfo() { return 0; }
142218893Sdim
143193323Sed  /// getInstrItineraryData - Returns instruction itinerary data for the target
144193323Sed  /// or specific subtarget.
145193323Sed  ///
146218893Sdim  virtual const InstrItineraryData *getInstrItineraryData() const {
147218893Sdim    return 0;
148193323Sed  }
149193323Sed
150208599Srdivacky  /// hasMCRelaxAll - Check whether all machine code instructions should be
151208599Srdivacky  /// relaxed.
152208599Srdivacky  bool hasMCRelaxAll() const { return MCRelaxAll; }
153208599Srdivacky
154208599Srdivacky  /// setMCRelaxAll - Set whether all machine code instructions should be
155208599Srdivacky  /// relaxed.
156208599Srdivacky  void setMCRelaxAll(bool Value) { MCRelaxAll = Value; }
157208599Srdivacky
158221345Sdim  /// hasMCSaveTempLabels - Check whether temporary labels will be preserved
159221345Sdim  /// (i.e., not treated as temporary).
160221345Sdim  bool hasMCSaveTempLabels() const { return MCSaveTempLabels; }
161221345Sdim
162221345Sdim  /// setMCSaveTempLabels - Set whether temporary labels will be preserved
163221345Sdim  /// (i.e., not treated as temporary).
164221345Sdim  void setMCSaveTempLabels(bool Value) { MCSaveTempLabels = Value; }
165221345Sdim
166218893Sdim  /// hasMCNoExecStack - Check whether an executable stack is not needed.
167218893Sdim  bool hasMCNoExecStack() const { return MCNoExecStack; }
168218893Sdim
169218893Sdim  /// setMCNoExecStack - Set whether an executabel stack is not needed.
170218893Sdim  void setMCNoExecStack(bool Value) { MCNoExecStack = Value; }
171218893Sdim
172218893Sdim  /// hasMCUseLoc - Check whether we should use dwarf's .loc directive.
173218893Sdim  bool hasMCUseLoc() const { return MCUseLoc; }
174218893Sdim
175218893Sdim  /// setMCUseLoc - Set whether all we should use dwarf's .loc directive.
176218893Sdim  void setMCUseLoc(bool Value) { MCUseLoc = Value; }
177218893Sdim
178221345Sdim  /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives.
179221345Sdim  bool hasMCUseCFI() const { return MCUseCFI; }
180221345Sdim
181221345Sdim  /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives.
182221345Sdim  void setMCUseCFI(bool Value) { MCUseCFI = Value; }
183221345Sdim
184234353Sdim  /// hasMCUseDwarfDirectory - Check whether we should use .file directives with
185234353Sdim  /// explicit directories.
186234353Sdim  bool hasMCUseDwarfDirectory() const { return MCUseDwarfDirectory; }
187234353Sdim
188234353Sdim  /// setMCUseDwarfDirectory - Set whether all we should use .file directives
189234353Sdim  /// with explicit directories.
190234353Sdim  void setMCUseDwarfDirectory(bool Value) { MCUseDwarfDirectory = Value; }
191234353Sdim
192193323Sed  /// getRelocationModel - Returns the code generation relocation model. The
193193323Sed  /// choices are static, PIC, and dynamic-no-pic, and target default.
194226633Sdim  Reloc::Model getRelocationModel() const;
195193323Sed
196193323Sed  /// getCodeModel - Returns the code model. The choices are small, kernel,
197193323Sed  /// medium, large, and target default.
198226633Sdim  CodeModel::Model getCodeModel() const;
199193323Sed
200234353Sdim  /// getTLSModel - Returns the TLS model which should be used for the given
201234353Sdim  /// global variable.
202234353Sdim  TLSModel::Model getTLSModel(const GlobalValue *GV) const;
203234353Sdim
204234353Sdim  /// getOptLevel - Returns the optimization level: None, Less,
205234353Sdim  /// Default, or Aggressive.
206234353Sdim  CodeGenOpt::Level getOptLevel() const;
207234353Sdim
208234353Sdim  void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
209234353Sdim
210234353Sdim  bool shouldPrintMachineCode() const { return Options.PrintMachineCode; }
211234353Sdim
212193323Sed  /// getAsmVerbosityDefault - Returns the default value of asm verbosity.
213193323Sed  ///
214193323Sed  static bool getAsmVerbosityDefault();
215193323Sed
216193323Sed  /// setAsmVerbosityDefault - Set the default value of asm verbosity. Default
217193323Sed  /// is false.
218193323Sed  static void setAsmVerbosityDefault(bool);
219193323Sed
220207618Srdivacky  /// getDataSections - Return true if data objects should be emitted into their
221207618Srdivacky  /// own section, corresponds to -fdata-sections.
222207618Srdivacky  static bool getDataSections();
223207618Srdivacky
224207618Srdivacky  /// getFunctionSections - Return true if functions should be emitted into
225207618Srdivacky  /// their own section, corresponding to -ffunction-sections.
226207618Srdivacky  static bool getFunctionSections();
227207618Srdivacky
228207618Srdivacky  /// setDataSections - Set if the data are emit into separate sections.
229207618Srdivacky  static void setDataSections(bool);
230207618Srdivacky
231207618Srdivacky  /// setFunctionSections - Set if the functions are emit into separate
232207618Srdivacky  /// sections.
233207618Srdivacky  static void setFunctionSections(bool);
234207618Srdivacky
235249423Sdim  /// \brief Register analysis passes for this target with a pass manager.
236249423Sdim  virtual void addAnalysisPasses(PassManagerBase &) {}
237249423Sdim
238193323Sed  /// CodeGenFileType - These enums are meant to be passed into
239203954Srdivacky  /// addPassesToEmitFile to indicate what type of file to emit, and returned by
240203954Srdivacky  /// it to indicate what type of file could actually be made.
241193323Sed  enum CodeGenFileType {
242203954Srdivacky    CGFT_AssemblyFile,
243203954Srdivacky    CGFT_ObjectFile,
244203954Srdivacky    CGFT_Null         // Do not emit any output.
245193323Sed  };
246193323Sed
247193323Sed  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
248193323Sed  /// specified file emitted.  Typically this will involve several steps of code
249203954Srdivacky  /// generation.  This method should return true if emission of this file type
250203954Srdivacky  /// is not supported, or false on success.
251203954Srdivacky  virtual bool addPassesToEmitFile(PassManagerBase &,
252203954Srdivacky                                   formatted_raw_ostream &,
253204642Srdivacky                                   CodeGenFileType,
254239462Sdim                                   bool /*DisableVerify*/ = true,
255239462Sdim                                   AnalysisID StartAfter = 0,
256239462Sdim                                   AnalysisID StopAfter = 0) {
257193323Sed    return true;
258193323Sed  }
259193323Sed
260193323Sed  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
261203954Srdivacky  /// get machine code emitted.  This uses a JITCodeEmitter object to handle
262193323Sed  /// actually outputting the machine code and resolving things like the address
263193323Sed  /// of functions.  This method returns true if machine code emission is
264193323Sed  /// not supported.
265193323Sed  ///
266193323Sed  virtual bool addPassesToEmitMachineCode(PassManagerBase &,
267193323Sed                                          JITCodeEmitter &,
268234353Sdim                                          bool /*DisableVerify*/ = true) {
269193323Sed    return true;
270193323Sed  }
271212904Sdim
272212904Sdim  /// addPassesToEmitMC - Add passes to the specified pass manager to get
273212904Sdim  /// machine code emitted with the MCJIT. This method returns true if machine
274212904Sdim  /// code is not supported. It fills the MCContext Ctx pointer which can be
275212904Sdim  /// used to build custom MCStreamer.
276212904Sdim  ///
277212904Sdim  virtual bool addPassesToEmitMC(PassManagerBase &,
278212904Sdim                                 MCContext *&,
279221345Sdim                                 raw_ostream &,
280234353Sdim                                 bool /*DisableVerify*/ = true) {
281212904Sdim    return true;
282212904Sdim  }
283193323Sed};
284193323Sed
285193323Sed/// LLVMTargetMachine - This class describes a target machine that is
286193323Sed/// implemented with the LLVM target-independent code generator.
287193323Sed///
288193323Sedclass LLVMTargetMachine : public TargetMachine {
289193323Sedprotected: // Can only create subclasses.
290224145Sdim  LLVMTargetMachine(const Target &T, StringRef TargetTriple,
291234353Sdim                    StringRef CPU, StringRef FS, TargetOptions Options,
292234353Sdim                    Reloc::Model RM, CodeModel::Model CM,
293234353Sdim                    CodeGenOpt::Level OL);
294218893Sdim
295234353Sdimpublic:
296249423Sdim  /// \brief Register analysis passes for this target with a pass manager.
297249423Sdim  ///
298249423Sdim  /// This registers target independent analysis passes.
299249423Sdim  virtual void addAnalysisPasses(PassManagerBase &PM);
300249423Sdim
301234353Sdim  /// createPassConfig - Create a pass configuration object to be used by
302234353Sdim  /// addPassToEmitX methods for generating a pipeline of CodeGen passes.
303234353Sdim  virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);
304193323Sed
305193323Sed  /// addPassesToEmitFile - Add passes to the specified pass manager to get the
306193323Sed  /// specified file emitted.  Typically this will involve several steps of code
307234353Sdim  /// generation.
308203954Srdivacky  virtual bool addPassesToEmitFile(PassManagerBase &PM,
309203954Srdivacky                                   formatted_raw_ostream &Out,
310203954Srdivacky                                   CodeGenFileType FileType,
311239462Sdim                                   bool DisableVerify = true,
312239462Sdim                                   AnalysisID StartAfter = 0,
313239462Sdim                                   AnalysisID StopAfter = 0);
314218893Sdim
315193323Sed  /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
316203954Srdivacky  /// get machine code emitted.  This uses a JITCodeEmitter object to handle
317193323Sed  /// actually outputting the machine code and resolving things like the address
318193323Sed  /// of functions.  This method returns true if machine code emission is
319193323Sed  /// not supported.
320193323Sed  ///
321193323Sed  virtual bool addPassesToEmitMachineCode(PassManagerBase &PM,
322193323Sed                                          JITCodeEmitter &MCE,
323204642Srdivacky                                          bool DisableVerify = true);
324212904Sdim
325212904Sdim  /// addPassesToEmitMC - Add passes to the specified pass manager to get
326212904Sdim  /// machine code emitted with the MCJIT. This method returns true if machine
327212904Sdim  /// code is not supported. It fills the MCContext Ctx pointer which can be
328212904Sdim  /// used to build custom MCStreamer.
329212904Sdim  ///
330212904Sdim  virtual bool addPassesToEmitMC(PassManagerBase &PM,
331212904Sdim                                 MCContext *&Ctx,
332221345Sdim                                 raw_ostream &OS,
333212904Sdim                                 bool DisableVerify = true);
334218893Sdim
335193323Sed  /// addCodeEmitter - This pass should be overridden by the target to add a
336193323Sed  /// code emitter, if supported.  If this is not supported, 'true' should be
337198090Srdivacky  /// returned.
338234353Sdim  virtual bool addCodeEmitter(PassManagerBase &,
339198090Srdivacky                              JITCodeEmitter &) {
340193323Sed    return true;
341193323Sed  }
342193323Sed};
343193323Sed
344193323Sed} // End llvm namespace
345193323Sed
346193323Sed#endif
347