Fixed some bugs.
[llvm/zpu.git] / lib / Target / X86 / X86TargetMachine.cpp
blob4e435eecb1e9b869badab038ea62977cfcce0f60
1 //===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
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 X86 specific subclass of TargetMachine.
12 //===----------------------------------------------------------------------===//
14 #include "X86MCAsmInfo.h"
15 #include "X86TargetMachine.h"
16 #include "X86.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Target/TargetRegistry.h"
25 using namespace llvm;
27 static MCAsmInfo *createMCAsmInfo(const Target &T, StringRef TT) {
28 Triple TheTriple(TT);
29 switch (TheTriple.getOS()) {
30 case Triple::Darwin:
31 return new X86MCAsmInfoDarwin(TheTriple);
32 case Triple::MinGW32:
33 case Triple::MinGW64:
34 case Triple::Cygwin:
35 case Triple::Win32:
36 return new X86MCAsmInfoCOFF(TheTriple);
37 default:
38 return new X86ELFMCAsmInfo(TheTriple);
42 static MCStreamer *createMCStreamer(const Target &T, const std::string &TT,
43 MCContext &Ctx, TargetAsmBackend &TAB,
44 raw_ostream &_OS,
45 MCCodeEmitter *_Emitter,
46 bool RelaxAll) {
47 Triple TheTriple(TT);
48 switch (TheTriple.getOS()) {
49 case Triple::Darwin:
50 return createMachOStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
51 case Triple::MinGW32:
52 case Triple::MinGW64:
53 case Triple::Cygwin:
54 case Triple::Win32:
55 return createWinCOFFStreamer(Ctx, TAB, *_Emitter, _OS, RelaxAll);
56 default:
57 return createELFStreamer(Ctx, TAB, _OS, _Emitter, RelaxAll);
61 extern "C" void LLVMInitializeX86Target() {
62 // Register the target.
63 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
64 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
66 // Register the target asm info.
67 RegisterAsmInfoFn A(TheX86_32Target, createMCAsmInfo);
68 RegisterAsmInfoFn B(TheX86_64Target, createMCAsmInfo);
70 // Register the code emitter.
71 TargetRegistry::RegisterCodeEmitter(TheX86_32Target,
72 createX86_32MCCodeEmitter);
73 TargetRegistry::RegisterCodeEmitter(TheX86_64Target,
74 createX86_64MCCodeEmitter);
76 // Register the asm backend.
77 TargetRegistry::RegisterAsmBackend(TheX86_32Target,
78 createX86_32AsmBackend);
79 TargetRegistry::RegisterAsmBackend(TheX86_64Target,
80 createX86_64AsmBackend);
82 // Register the object streamer.
83 TargetRegistry::RegisterObjectStreamer(TheX86_32Target,
84 createMCStreamer);
85 TargetRegistry::RegisterObjectStreamer(TheX86_64Target,
86 createMCStreamer);
90 X86_32TargetMachine::X86_32TargetMachine(const Target &T, const std::string &TT,
91 const std::string &FS)
92 : X86TargetMachine(T, TT, FS, false),
93 DataLayout(getSubtargetImpl()->isTargetDarwin() ?
94 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-n8:16:32" :
95 (getSubtargetImpl()->isTargetCygMing() ||
96 getSubtargetImpl()->isTargetWindows()) ?
97 "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-n8:16:32" :
98 "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-n8:16:32"),
99 InstrInfo(*this),
100 TSInfo(*this),
101 TLInfo(*this),
102 JITInfo(*this) {
106 X86_64TargetMachine::X86_64TargetMachine(const Target &T, const std::string &TT,
107 const std::string &FS)
108 : X86TargetMachine(T, TT, FS, true),
109 DataLayout("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-n8:16:32:64"),
110 InstrInfo(*this),
111 TSInfo(*this),
112 TLInfo(*this),
113 JITInfo(*this) {
116 /// X86TargetMachine ctor - Create an X86 target.
118 X86TargetMachine::X86TargetMachine(const Target &T, const std::string &TT,
119 const std::string &FS, bool is64Bit)
120 : LLVMTargetMachine(T, TT),
121 Subtarget(TT, FS, is64Bit),
122 FrameInfo(TargetFrameInfo::StackGrowsDown,
123 Subtarget.getStackAlignment(),
124 (Subtarget.isTargetWin64() ? -40 :
125 (Subtarget.is64Bit() ? -8 : -4))),
126 ELFWriterInfo(is64Bit, true) {
127 DefRelocModel = getRelocationModel();
129 // If no relocation model was picked, default as appropriate for the target.
130 if (getRelocationModel() == Reloc::Default) {
131 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
132 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
133 // use static relocation model by default.
134 if (Subtarget.isTargetDarwin()) {
135 if (Subtarget.is64Bit())
136 setRelocationModel(Reloc::PIC_);
137 else
138 setRelocationModel(Reloc::DynamicNoPIC);
139 } else if (Subtarget.isTargetWin64())
140 setRelocationModel(Reloc::PIC_);
141 else
142 setRelocationModel(Reloc::Static);
145 assert(getRelocationModel() != Reloc::Default &&
146 "Relocation mode not picked");
148 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
149 // is defined as a model for code which may be used in static or dynamic
150 // executables but not necessarily a shared library. On X86-32 we just
151 // compile in -static mode, in x86-64 we use PIC.
152 if (getRelocationModel() == Reloc::DynamicNoPIC) {
153 if (is64Bit)
154 setRelocationModel(Reloc::PIC_);
155 else if (!Subtarget.isTargetDarwin())
156 setRelocationModel(Reloc::Static);
159 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
160 // the Mach-O file format doesn't support it.
161 if (getRelocationModel() == Reloc::Static &&
162 Subtarget.isTargetDarwin() &&
163 is64Bit)
164 setRelocationModel(Reloc::PIC_);
166 // Determine the PICStyle based on the target selected.
167 if (getRelocationModel() == Reloc::Static) {
168 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
169 Subtarget.setPICStyle(PICStyles::None);
170 } else if (Subtarget.is64Bit()) {
171 // PIC in 64 bit mode is always rip-rel.
172 Subtarget.setPICStyle(PICStyles::RIPRel);
173 } else if (Subtarget.isTargetCygMing()) {
174 Subtarget.setPICStyle(PICStyles::None);
175 } else if (Subtarget.isTargetDarwin()) {
176 if (getRelocationModel() == Reloc::PIC_)
177 Subtarget.setPICStyle(PICStyles::StubPIC);
178 else {
179 assert(getRelocationModel() == Reloc::DynamicNoPIC);
180 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
182 } else if (Subtarget.isTargetELF()) {
183 Subtarget.setPICStyle(PICStyles::GOT);
186 // Finally, if we have "none" as our PIC style, force to static mode.
187 if (Subtarget.getPICStyle() == PICStyles::None)
188 setRelocationModel(Reloc::Static);
191 //===----------------------------------------------------------------------===//
192 // Pass Pipeline Configuration
193 //===----------------------------------------------------------------------===//
195 bool X86TargetMachine::addInstSelector(PassManagerBase &PM,
196 CodeGenOpt::Level OptLevel) {
197 // Install an instruction selector.
198 PM.add(createX86ISelDag(*this, OptLevel));
200 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
201 if (!Subtarget.is64Bit())
202 PM.add(createGlobalBaseRegPass());
204 return false;
207 bool X86TargetMachine::addPreRegAlloc(PassManagerBase &PM,
208 CodeGenOpt::Level OptLevel) {
209 PM.add(createX86MaxStackAlignmentHeuristicPass());
210 return false; // -print-machineinstr shouldn't print after this.
213 bool X86TargetMachine::addPostRegAlloc(PassManagerBase &PM,
214 CodeGenOpt::Level OptLevel) {
215 PM.add(createX86FloatingPointStackifierPass());
216 return true; // -print-machineinstr should print after this.
219 bool X86TargetMachine::addPreEmitPass(PassManagerBase &PM,
220 CodeGenOpt::Level OptLevel) {
221 if (OptLevel != CodeGenOpt::None && Subtarget.hasSSE2()) {
222 PM.add(createSSEDomainFixPass());
223 return true;
225 return false;
228 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
229 CodeGenOpt::Level OptLevel,
230 JITCodeEmitter &JCE) {
231 // FIXME: Move this to TargetJITInfo!
232 // On Darwin, do not override 64-bit setting made in X86TargetMachine().
233 if (DefRelocModel == Reloc::Default &&
234 (!Subtarget.isTargetDarwin() || !Subtarget.is64Bit())) {
235 setRelocationModel(Reloc::Static);
236 Subtarget.setPICStyle(PICStyles::None);
240 PM.add(createX86JITCodeEmitterPass(*this, JCE));
242 return false;
245 void X86TargetMachine::setCodeModelForStatic() {
247 if (getCodeModel() != CodeModel::Default) return;
249 // For static codegen, if we're not already set, use Small codegen.
250 setCodeModel(CodeModel::Small);
254 void X86TargetMachine::setCodeModelForJIT() {
256 if (getCodeModel() != CodeModel::Default) return;
258 // 64-bit JIT places everything in the same buffer except external functions.
259 if (Subtarget.is64Bit())
260 setCodeModel(CodeModel::Large);
261 else
262 setCodeModel(CodeModel::Small);