Remove the 's' DataLayout specification
[oota-llvm.git] / lib / Target / X86 / X86TargetMachine.cpp
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.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "X86TargetMachine.h"
15 #include "X86.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetOptions.h"
23 using namespace llvm;
24
25 extern "C" void LLVMInitializeX86Target() {
26   // Register the target.
27   RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
28   RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
29 }
30
31 void X86_32TargetMachine::anchor() { }
32
33 static std::string computeDataLayout(const X86Subtarget &ST) {
34   // X86 is little endian
35   std::string Ret = "e";
36
37   // X86 and x32 have 32 bit pointers.
38   if (ST.isTarget64BitILP32() || !ST.is64Bit())
39     Ret += "-p:32:32";
40
41   // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
42   if (ST.is64Bit() || ST.isTargetCygMing() || ST.isTargetWindows() ||
43       ST.isTargetNaCl())
44     Ret += "-i64:64";
45   else
46     Ret += "-f64:32:64";
47
48   // Some ABIs align long double to 128 bits, others to 32.
49   if (ST.isTargetNaCl())
50     ; // No f80
51   else if (ST.is64Bit() || ST.isTargetDarwin())
52     Ret += "-f80:128";
53   else
54     Ret += "-f80:32";
55
56   // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
57   if (ST.is64Bit())
58     Ret += "-n8:16:32:64";
59   else
60     Ret += "-n8:16:32";
61
62   // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
63   if (!ST.is64Bit() && (ST.isTargetCygMing() || ST.isTargetWindows()))
64     Ret += "-S32";
65   else
66     Ret += "-S128";
67
68   return Ret;
69 }
70
71 X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
72                                          StringRef CPU, StringRef FS,
73                                          const TargetOptions &Options,
74                                          Reloc::Model RM, CodeModel::Model CM,
75                                          CodeGenOpt::Level OL)
76   : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
77     DL(computeDataLayout(*getSubtargetImpl())),
78     InstrInfo(*this),
79     TLInfo(*this),
80     TSInfo(*this),
81     JITInfo(*this) {
82   initAsmInfo();
83 }
84
85 void X86_64TargetMachine::anchor() { }
86
87 X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
88                                          StringRef CPU, StringRef FS,
89                                          const TargetOptions &Options,
90                                          Reloc::Model RM, CodeModel::Model CM,
91                                          CodeGenOpt::Level OL)
92   : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
93     // The x32 ABI dictates the ILP32 programming model for x64.
94     DL(computeDataLayout(*getSubtargetImpl())),
95     InstrInfo(*this),
96     TLInfo(*this),
97     TSInfo(*this),
98     JITInfo(*this) {
99   initAsmInfo();
100 }
101
102 /// X86TargetMachine ctor - Create an X86 target.
103 ///
104 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
105                                    StringRef CPU, StringRef FS,
106                                    const TargetOptions &Options,
107                                    Reloc::Model RM, CodeModel::Model CM,
108                                    CodeGenOpt::Level OL,
109                                    bool is64Bit)
110   : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
111     Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
112     FrameLowering(*this, Subtarget),
113     InstrItins(Subtarget.getInstrItineraryData()){
114   // Determine the PICStyle based on the target selected.
115   if (getRelocationModel() == Reloc::Static) {
116     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
117     Subtarget.setPICStyle(PICStyles::None);
118   } else if (Subtarget.is64Bit()) {
119     // PIC in 64 bit mode is always rip-rel.
120     Subtarget.setPICStyle(PICStyles::RIPRel);
121   } else if (Subtarget.isTargetCOFF()) {
122     Subtarget.setPICStyle(PICStyles::None);
123   } else if (Subtarget.isTargetDarwin()) {
124     if (getRelocationModel() == Reloc::PIC_)
125       Subtarget.setPICStyle(PICStyles::StubPIC);
126     else {
127       assert(getRelocationModel() == Reloc::DynamicNoPIC);
128       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
129     }
130   } else if (Subtarget.isTargetELF()) {
131     Subtarget.setPICStyle(PICStyles::GOT);
132   }
133
134   // default to hard float ABI
135   if (Options.FloatABIType == FloatABI::Default)
136     this->Options.FloatABIType = FloatABI::Hard;
137 }
138
139 //===----------------------------------------------------------------------===//
140 // Command line options for x86
141 //===----------------------------------------------------------------------===//
142 static cl::opt<bool>
143 UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
144   cl::desc("Minimize AVX to SSE transition penalty"),
145   cl::init(true));
146
147 // Temporary option to control early if-conversion for x86 while adding machine
148 // models.
149 static cl::opt<bool>
150 X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
151                cl::desc("Enable early if-conversion on X86"));
152
153 //===----------------------------------------------------------------------===//
154 // X86 Analysis Pass Setup
155 //===----------------------------------------------------------------------===//
156
157 void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
158   // Add first the target-independent BasicTTI pass, then our X86 pass. This
159   // allows the X86 pass to delegate to the target independent layer when
160   // appropriate.
161   PM.add(createBasicTargetTransformInfoPass(this));
162   PM.add(createX86TargetTransformInfoPass(this));
163 }
164
165
166 //===----------------------------------------------------------------------===//
167 // Pass Pipeline Configuration
168 //===----------------------------------------------------------------------===//
169
170 namespace {
171 /// X86 Code Generator Pass Configuration Options.
172 class X86PassConfig : public TargetPassConfig {
173 public:
174   X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
175     : TargetPassConfig(TM, PM) {}
176
177   X86TargetMachine &getX86TargetMachine() const {
178     return getTM<X86TargetMachine>();
179   }
180
181   const X86Subtarget &getX86Subtarget() const {
182     return *getX86TargetMachine().getSubtargetImpl();
183   }
184
185   virtual bool addInstSelector();
186   virtual bool addILPOpts();
187   virtual bool addPreRegAlloc();
188   virtual bool addPostRegAlloc();
189   virtual bool addPreEmitPass();
190 };
191 } // namespace
192
193 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
194   return new X86PassConfig(this, PM);
195 }
196
197 bool X86PassConfig::addInstSelector() {
198   // Install an instruction selector.
199   addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
200
201   // For ELF, cleanup any local-dynamic TLS accesses.
202   if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
203     addPass(createCleanupLocalDynamicTLSPass());
204
205   // For 32-bit, prepend instructions to set the "global base reg" for PIC.
206   if (!getX86Subtarget().is64Bit())
207     addPass(createGlobalBaseRegPass());
208
209   return false;
210 }
211
212 bool X86PassConfig::addILPOpts() {
213   if (X86EarlyIfConv && getX86Subtarget().hasCMov()) {
214     addPass(&EarlyIfConverterID);
215     return true;
216   }
217   return false;
218 }
219
220 bool X86PassConfig::addPreRegAlloc() {
221   return false;  // -print-machineinstr shouldn't print after this.
222 }
223
224 bool X86PassConfig::addPostRegAlloc() {
225   addPass(createX86FloatingPointStackifierPass());
226   return true;  // -print-machineinstr should print after this.
227 }
228
229 bool X86PassConfig::addPreEmitPass() {
230   bool ShouldPrint = false;
231   if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
232     addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
233     ShouldPrint = true;
234   }
235
236   if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
237     addPass(createX86IssueVZeroUpperPass());
238     ShouldPrint = true;
239   }
240
241   if (getOptLevel() != CodeGenOpt::None &&
242       getX86Subtarget().padShortFunctions()) {
243     addPass(createX86PadShortFunctions());
244     ShouldPrint = true;
245   }
246   if (getOptLevel() != CodeGenOpt::None &&
247       getX86Subtarget().LEAusesAG()){
248     addPass(createX86FixupLEAs());
249     ShouldPrint = true;
250   }
251
252   return ShouldPrint;
253 }
254
255 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
256                                       JITCodeEmitter &JCE) {
257   PM.add(createX86JITCodeEmitterPass(*this, JCE));
258
259   return false;
260 }