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