X86TargetMachine.cpp revision 205407
117987Speter//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
237456Sbde//
31556Srgrimes//                     The LLVM Compiler Infrastructure
41556Srgrimes//
535783Sbde// This file is distributed under the University of Illinois Open Source
61556Srgrimes// License. See LICENSE.TXT for details.
725221Ssteve//
825221Ssteve//===----------------------------------------------------------------------===//
935783Sbde//
1035783Sbde// This file defines the X86 specific subclass of TargetMachine.
1135783Sbde//
1217987Speter//===----------------------------------------------------------------------===//
1325903Ssteve
1425221Ssteve#include "X86MCAsmInfo.h"
1517987Speter#include "X86TargetMachine.h"
161556Srgrimes#include "X86.h"
171556Srgrimes#include "llvm/PassManager.h"
1817988Speter#include "llvm/CodeGen/MachineFunction.h"
1917988Speter#include "llvm/CodeGen/Passes.h"
2017987Speter#include "llvm/Support/FormattedStream.h"
211556Srgrimes#include "llvm/Target/TargetOptions.h"
221556Srgrimes#include "llvm/Target/TargetRegistry.h"
2330113Sjkhusing namespace llvm;
2435783Sbde
2530113Sjkhstatic MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
261556Srgrimes  Triple TheTriple(TT);
2737456Sbde  switch (TheTriple.getOS()) {
2837456Sbde  case Triple::Darwin:
2930113Sjkh    return new X86MCAsmInfoDarwin(TheTriple);
3025903Ssteve  case Triple::MinGW32:
3117987Speter  case Triple::MinGW64:
321556Srgrimes  case Triple::Cygwin:
3325903Ssteve  case Triple::Win32:
3425903Ssteve    return new X86MCAsmInfoCOFF(TheTriple);
3517987Speter  default:
361556Srgrimes    return new X86ELFMCAsmInfo(TheTriple);
3728729Sbde  }
3828729Sbde}
3928729Sbde
4028729Sbdeextern "C" void LLVMInitializeX86Target() {
4128729Sbde  // Register the target.
4228729Sbde  RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
4328729Sbde  RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
4428729Sbde
4528729Sbde  // Register the target asm info.
4630113Sjkh  RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
4717987Speter  RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
481556Srgrimes
491556Srgrimes  // Register the code emitter.
5030113Sjkh  TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
511556Srgrimes                                      createX86_32MCCodeEmitter);
521556Srgrimes  TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
531556Srgrimes                                      createX86_64MCCodeEmitter);
5425903Ssteve
5525903Ssteve  // Register the asm backend.
561556Srgrimes  TargetRegistry::RegisterAsmBackend(TheX86_32Target,
571556Srgrimes                                     createX86_32AsmBackend);
58  TargetRegistry::RegisterAsmBackend(TheX86_64Target,
59                                     createX86_64AsmBackend);
60}
61
62
63X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
64                                         const std::string &FS)
65  : X86TargetMachine(T, TT, FS, false) {
66}
67
68
69X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
70                                         const std::string &FS)
71  : X86TargetMachine(T, TT, FS, true) {
72}
73
74/// X86TargetMachine ctor - Create an X86 target.
75///
76X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
77                                   const std::string &FS, bool is64Bit)
78  : LLVMTargetMachine(T, TT),
79    Subtarget(TT, FS, is64Bit),
80    DataLayout(Subtarget.getDataLayout()),
81    FrameInfo(TargetFrameInfo::StackGrowsDown,
82              Subtarget.getStackAlignment(),
83              (Subtarget.isTargetWin64() ? -40 :
84               (Subtarget.is64Bit() ? -8 : -4))),
85    InstrInfo(*this), JITInfo(*this), TLInfo(*this), ELFWriterInfo(*this) {
86  DefRelocModel = getRelocationModel();
87
88  // If no relocation model was picked, default as appropriate for the target.
89  if (getRelocationModel() == Reloc::Default) {
90    if (!Subtarget.isTargetDarwin())
91      setRelocationModel(Reloc::Static);
92    else if (Subtarget.is64Bit())
93      setRelocationModel(Reloc::PIC_);
94    else
95      setRelocationModel(Reloc::DynamicNoPIC);
96  }
97
98  assert(getRelocationModel() != Reloc::Default &&
99         "Relocation mode not picked");
100
101  // ELF and X86-64 don't have a distinct DynamicNoPIC model.  DynamicNoPIC
102  // is defined as a model for code which may be used in static or dynamic
103  // executables but not necessarily a shared library. On X86-32 we just
104  // compile in -static mode, in x86-64 we use PIC.
105  if (getRelocationModel() == Reloc::DynamicNoPIC) {
106    if (is64Bit)
107      setRelocationModel(Reloc::PIC_);
108    else if (!Subtarget.isTargetDarwin())
109      setRelocationModel(Reloc::Static);
110  }
111
112  // If we are on Darwin, disallow static relocation model in X86-64 mode, since
113  // the Mach-O file format doesn't support it.
114  if (getRelocationModel() == Reloc::Static &&
115      Subtarget.isTargetDarwin() &&
116      is64Bit)
117    setRelocationModel(Reloc::PIC_);
118
119  // Determine the PICStyle based on the target selected.
120  if (getRelocationModel() == Reloc::Static) {
121    // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
122    Subtarget.setPICStyle(PICStyles::None);
123  } else if (Subtarget.isTargetCygMing()) {
124    Subtarget.setPICStyle(PICStyles::None);
125  } else if (Subtarget.isTargetDarwin()) {
126    if (Subtarget.is64Bit())
127      Subtarget.setPICStyle(PICStyles::RIPRel);
128    else if (getRelocationModel() == Reloc::PIC_)
129      Subtarget.setPICStyle(PICStyles::StubPIC);
130    else {
131      assert(getRelocationModel() == Reloc::DynamicNoPIC);
132      Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
133    }
134  } else if (Subtarget.isTargetELF()) {
135    if (Subtarget.is64Bit())
136      Subtarget.setPICStyle(PICStyles::RIPRel);
137    else
138      Subtarget.setPICStyle(PICStyles::GOT);
139  }
140
141  // Finally, if we have "none" as our PIC style, force to static mode.
142  if (Subtarget.getPICStyle() == PICStyles::None)
143    setRelocationModel(Reloc::Static);
144}
145
146//===----------------------------------------------------------------------===//
147// Pass Pipeline Configuration
148//===----------------------------------------------------------------------===//
149
150bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
151                                       CodeGenOpt::Level OptLevel) {
152  // Install an instruction selector.
153  PM.add(createX86ISelDag(*this, OptLevel));
154
155  // Install a pass to insert x87 FP_REG_KILL instructions, as needed.
156  PM.add(createX87FPRegKillInserterPass());
157
158  return false;
159}
160
161bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
162                                      CodeGenOpt::Level OptLevel) {
163  return false;  // -print-machineinstr shouldn't print after this.
164}
165
166bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
167                                       CodeGenOpt::Level OptLevel) {
168  PM.add(createX86FloatingPointStackifierPass());
169  return true;  // -print-machineinstr should print after this.
170}
171
172bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
173                                      CodeGenOpt::Level OptLevel,
174                                      JITCodeEmitter &JCE) {
175  // FIXME: Move this to TargetJITInfo!
176  // On Darwin, do not override 64-bit setting made in X86TargetMachine().
177  if (DefRelocModel == Reloc::Default &&
178      (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
179    setRelocationModel(Reloc::Static);
180    Subtarget.setPICStyle(PICStyles::None);
181  }
182
183
184  PM.add(createX86JITCodeEmitterPass(*this, JCE));
185
186  return false;
187}
188
189void X86TargetMachine::setCodeModelForStatic() {
190
191    if (getCodeModel() != CodeModel::Default) return;
192
193    // For static codegen, if we're not already set, use Small codegen.
194    setCodeModel(CodeModel::Small);
195}
196
197
198void X86TargetMachine::setCodeModelForJIT() {
199
200  if (getCodeModel() != CodeModel::Default) return;
201
202  // 64-bit JIT places everything in the same buffer except external functions.
203  if (Subtarget.is64Bit())
204    setCodeModel(CodeModel::Large);
205  else
206    setCodeModel(CodeModel::Small);
207}
208