Add a new interface to allow IR-level passes to access codegen-specific information.
[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/PassManager.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Target/TargetOptions.h"
22 #include "llvm/Support/TargetRegistry.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 X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
34                                          StringRef CPU, StringRef FS,
35                                          const TargetOptions &Options,
36                                          Reloc::Model RM, CodeModel::Model CM,
37                                          CodeGenOpt::Level OL)
38   : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
39     DL(getSubtargetImpl()->isTargetDarwin() ?
40                "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-"
41                "n8:16:32-S128" :
42                (getSubtargetImpl()->isTargetCygMing() ||
43                 getSubtargetImpl()->isTargetWindows()) ?
44                "e-p:32:32-f64:64:64-i64:64:64-f80:32:32-f128:128:128-"
45                "n8:16:32-S32" :
46                "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-f128:128:128-"
47                "n8:16:32-S128"),
48     InstrInfo(*this),
49     TSInfo(*this),
50     TLInfo(*this),
51     JITInfo(*this),
52     STTI(&TLInfo) {
53 }
54
55 void X86_64TargetMachine::anchor() { }
56
57 X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
58                                          StringRef CPU, StringRef FS,
59                                          const TargetOptions &Options,
60                                          Reloc::Model RM, CodeModel::Model CM,
61                                          CodeGenOpt::Level OL)
62   : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
63     DL("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-"
64                "n8:16:32:64-S128"),
65     InstrInfo(*this),
66     TSInfo(*this),
67     TLInfo(*this),
68     JITInfo(*this),
69     STTI(&TLInfo) {
70 }
71
72 /// X86TargetMachine ctor - Create an X86 target.
73 ///
74 X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
75                                    StringRef CPU, StringRef FS,
76                                    const TargetOptions &Options,
77                                    Reloc::Model RM, CodeModel::Model CM,
78                                    CodeGenOpt::Level OL,
79                                    bool is64Bit)
80   : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
81     Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
82     FrameLowering(*this, Subtarget),
83     ELFWriterInfo(is64Bit, true),
84     InstrItins(Subtarget.getInstrItineraryData()){
85   // Determine the PICStyle based on the target selected.
86   if (getRelocationModel() == Reloc::Static) {
87     // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
88     Subtarget.setPICStyle(PICStyles::None);
89   } else if (Subtarget.is64Bit()) {
90     // PIC in 64 bit mode is always rip-rel.
91     Subtarget.setPICStyle(PICStyles::RIPRel);
92   } else if (Subtarget.isTargetCygMing()) {
93     Subtarget.setPICStyle(PICStyles::None);
94   } else if (Subtarget.isTargetDarwin()) {
95     if (getRelocationModel() == Reloc::PIC_)
96       Subtarget.setPICStyle(PICStyles::StubPIC);
97     else {
98       assert(getRelocationModel() == Reloc::DynamicNoPIC);
99       Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
100     }
101   } else if (Subtarget.isTargetELF()) {
102     Subtarget.setPICStyle(PICStyles::GOT);
103   }
104
105   // default to hard float ABI
106   if (Options.FloatABIType == FloatABI::Default)
107     this->Options.FloatABIType = FloatABI::Hard;
108 }
109
110 //===----------------------------------------------------------------------===//
111 // Command line options for x86
112 //===----------------------------------------------------------------------===//
113 static cl::opt<bool>
114 UseVZeroUpper("x86-use-vzeroupper",
115   cl::desc("Minimize AVX to SSE transition penalty"),
116   cl::init(true));
117
118 // Temporary option to control early if-conversion for x86 while adding machine
119 // models.
120 static cl::opt<bool>
121 X86EarlyIfConv("x86-early-ifcvt",
122                cl::desc("Enable early if-conversion on X86"));
123
124 //===----------------------------------------------------------------------===//
125 // Pass Pipeline Configuration
126 //===----------------------------------------------------------------------===//
127
128 namespace {
129 /// X86 Code Generator Pass Configuration Options.
130 class X86PassConfig : public TargetPassConfig {
131 public:
132   X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
133     : TargetPassConfig(TM, PM) {}
134
135   X86TargetMachine &getX86TargetMachine() const {
136     return getTM<X86TargetMachine>();
137   }
138
139   const X86Subtarget &getX86Subtarget() const {
140     return *getX86TargetMachine().getSubtargetImpl();
141   }
142
143   virtual bool addInstSelector();
144   virtual bool addPreRegAlloc();
145   virtual bool addPostRegAlloc();
146   virtual bool addPreEmitPass();
147 };
148 } // namespace
149
150 TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
151   X86PassConfig *PC = new X86PassConfig(this, PM);
152
153   if (X86EarlyIfConv && Subtarget.hasCMov())
154     PC->enablePass(&EarlyIfConverterID);
155
156   return PC;
157 }
158
159 bool X86PassConfig::addInstSelector() {
160   // Install an instruction selector.
161   addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
162
163   // For ELF, cleanup any local-dynamic TLS accesses.
164   if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
165     addPass(createCleanupLocalDynamicTLSPass());
166
167   // For 32-bit, prepend instructions to set the "global base reg" for PIC.
168   if (!getX86Subtarget().is64Bit())
169     addPass(createGlobalBaseRegPass());
170
171   return false;
172 }
173
174 bool X86PassConfig::addPreRegAlloc() {
175   addPass(createX86MaxStackAlignmentHeuristicPass());
176   return false;  // -print-machineinstr shouldn't print after this.
177 }
178
179 bool X86PassConfig::addPostRegAlloc() {
180   addPass(createX86FloatingPointStackifierPass());
181   return true;  // -print-machineinstr should print after this.
182 }
183
184 bool X86PassConfig::addPreEmitPass() {
185   bool ShouldPrint = false;
186   if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
187     addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
188     ShouldPrint = true;
189   }
190
191   if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
192     addPass(createX86IssueVZeroUpperPass());
193     ShouldPrint = true;
194   }
195
196   return ShouldPrint;
197 }
198
199 bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
200                                       JITCodeEmitter &JCE) {
201   PM.add(createX86JITCodeEmitterPass(*this, JCE));
202
203   return false;
204 }