blob: b7a79563b2d25748e7f61249af9b369284e42a0b [file] [log] [blame]
Chris Lattner02a3d832002-10-29 22:37:54 +00001//===-- X86TargetMachine.cpp - Define TargetMachine for the X86 -----------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00009//
Chris Lattner02a3d832002-10-29 22:37:54 +000010// This file defines the X86 specific subclass of TargetMachine.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86TargetMachine.h"
Chris Lattnera32b4052002-12-24 00:04:01 +000015#include "X86.h"
Chris Lattnerd7a85562002-10-30 00:47:49 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000017#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/PassManager.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000019#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000020#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000021#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetOptions.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000025extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000026 // Register the target.
27 RegisterTargetMachine<X86_32TargetMachine> X(TheX86_32Target);
28 RegisterTargetMachine<X86_64TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000029}
Douglas Gregor1b731d52009-06-16 20:12:29 +000030
David Blaikiea379b1812011-12-20 02:50:00 +000031void X86_32TargetMachine::anchor() { }
Jim Laskeyae92ce82006-09-07 23:39:26 +000032
Evan Cheng2129f592011-07-19 06:37:02 +000033X86_32TargetMachine::X86_32TargetMachine(const Target &T, StringRef TT,
34 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000035 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000036 Reloc::Model RM, CodeModel::Model CM,
37 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000038 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false),
Micah Villmowcdfe20b2012-10-08 16:38:25 +000039 DL(getSubtargetImpl()->isTargetDarwin() ?
Evan Cheng06fdaeb2011-10-14 20:36:23 +000040 "e-p:32:32-f64:32:64-i64:32:64-f80:128:128-f128:128:128-"
41 "n8:16:32-S128" :
Rafael Espindola66e08d42010-10-03 18:59:45 +000042 (getSubtargetImpl()->isTargetCygMing() ||
43 getSubtargetImpl()->isTargetWindows()) ?
Evan Cheng06fdaeb2011-10-14 20:36:23 +000044 "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"),
Rafael Espindola66e08d42010-10-03 18:59:45 +000048 InstrInfo(*this),
49 TSInfo(*this),
50 TLInfo(*this),
Nadav Rotem5dc203e2012-10-18 23:22:48 +000051 JITInfo(*this),
Nadav Rotem2289f2c2012-10-24 17:22:41 +000052 STTI(&TLInfo), VTTI(&TLInfo) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +000053}
54
David Blaikiea379b1812011-12-20 02:50:00 +000055void X86_64TargetMachine::anchor() { }
Evan Cheng11b0a5d2006-09-08 06:48:29 +000056
Evan Cheng2129f592011-07-19 06:37:02 +000057X86_64TargetMachine::X86_64TargetMachine(const Target &T, StringRef TT,
58 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000059 const TargetOptions &Options,
Evan Chengecb29082011-11-16 08:38:26 +000060 Reloc::Model RM, CodeModel::Model CM,
61 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000062 : X86TargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true),
Micah Villmowcdfe20b2012-10-08 16:38:25 +000063 DL("e-p:64:64-s:64-f64:64:64-i64:64:64-f80:128:128-f128:128:128-"
Evan Cheng06fdaeb2011-10-14 20:36:23 +000064 "n8:16:32:64-S128"),
Rafael Espindola66e08d42010-10-03 18:59:45 +000065 InstrInfo(*this),
66 TSInfo(*this),
67 TLInfo(*this),
Nadav Rotem5dc203e2012-10-18 23:22:48 +000068 JITInfo(*this),
Nadav Rotem2289f2c2012-10-24 17:22:41 +000069 STTI(&TLInfo), VTTI(&TLInfo){
Evan Cheng11b0a5d2006-09-08 06:48:29 +000070}
71
Chris Lattner7e3abf12009-07-09 03:32:31 +000072/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000073///
Evan Cheng2129f592011-07-19 06:37:02 +000074X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT,
75 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000076 const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000077 Reloc::Model RM, CodeModel::Model CM,
Evan Chengecb29082011-11-16 08:38:26 +000078 CodeGenOpt::Level OL,
Evan Chengefd9b422011-07-20 07:51:56 +000079 bool is64Bit)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000080 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
81 Subtarget(TT, CPU, FS, Options.StackAlignmentOverride, is64Bit),
Anton Korobeynikov2f931282011-01-10 12:39:04 +000082 FrameLowering(*this, Subtarget),
Andrew Trick8523b162012-02-01 23:20:51 +000083 InstrItins(Subtarget.getInstrItineraryData()){
Chris Lattner7e3abf12009-07-09 03:32:31 +000084 // Determine the PICStyle based on the target selected.
85 if (getRelocationModel() == Reloc::Static) {
86 // Unless we're in PIC or DynamicNoPIC mode, set the PIC style to None.
87 Subtarget.setPICStyle(PICStyles::None);
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000088 } else if (Subtarget.is64Bit()) {
89 // PIC in 64 bit mode is always rip-rel.
90 Subtarget.setPICStyle(PICStyles::RIPRel);
Chris Lattner7e3abf12009-07-09 03:32:31 +000091 } else if (Subtarget.isTargetCygMing()) {
Chris Lattner1c5bf9d2009-07-09 03:15:51 +000092 Subtarget.setPICStyle(PICStyles::None);
93 } else if (Subtarget.isTargetDarwin()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +000094 if (getRelocationModel() == Reloc::PIC_)
Chris Lattnerba4d7332009-07-10 20:58:47 +000095 Subtarget.setPICStyle(PICStyles::StubPIC);
96 else {
97 assert(getRelocationModel() == Reloc::DynamicNoPIC);
98 Subtarget.setPICStyle(PICStyles::StubDynamicNoPIC);
99 }
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000100 } else if (Subtarget.isTargetELF()) {
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000101 Subtarget.setPICStyle(PICStyles::GOT);
Anton Korobeynikov40d67c52008-02-20 11:22:39 +0000102 }
Anton Korobeynikovdb9820e2010-08-21 17:21:11 +0000103
Evan Cheng3a0c5e52011-06-23 17:54:54 +0000104 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000105 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +0000106 this->Options.FloatABIType = FloatABI::Hard;
Chris Lattnera1d312c2006-02-03 18:59:39 +0000107}
Chris Lattner02a3d832002-10-29 22:37:54 +0000108
Chris Lattner12e97302006-09-04 04:14:57 +0000109//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000110// Command line options for x86
111//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000112static cl::opt<bool>
113UseVZeroUpper("x86-use-vzeroupper",
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000114 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000115 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000116
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000117// Temporary option to control early if-conversion for x86 while adding machine
118// models.
119static cl::opt<bool>
120X86EarlyIfConv("x86-early-ifcvt",
121 cl::desc("Enable early if-conversion on X86"));
122
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000123//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000124// Pass Pipeline Configuration
125//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000126
Andrew Trickccb67362012-02-03 05:12:41 +0000127namespace {
128/// X86 Code Generator Pass Configuration Options.
129class X86PassConfig : public TargetPassConfig {
130public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000131 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
132 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000133
134 X86TargetMachine &getX86TargetMachine() const {
135 return getTM<X86TargetMachine>();
136 }
137
138 const X86Subtarget &getX86Subtarget() const {
139 return *getX86TargetMachine().getSubtargetImpl();
140 }
141
142 virtual bool addInstSelector();
143 virtual bool addPreRegAlloc();
144 virtual bool addPostRegAlloc();
145 virtual bool addPreEmitPass();
146};
147} // namespace
148
Andrew Trickf8ea1082012-02-04 02:56:59 +0000149TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen49e4d4b2012-07-04 00:09:58 +0000150 X86PassConfig *PC = new X86PassConfig(this, PM);
151
Jakob Stoklund Olesen0f6e8bb2012-10-03 00:51:32 +0000152 if (X86EarlyIfConv && Subtarget.hasCMov())
Jakob Stoklund Olesen49e4d4b2012-07-04 00:09:58 +0000153 PC->enablePass(&EarlyIfConverterID);
154
155 return PC;
Andrew Trickccb67362012-02-03 05:12:41 +0000156}
157
158bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000159 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000160 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000161
Hans Wennborg789acfb2012-06-01 16:27:21 +0000162 // For ELF, cleanup any local-dynamic TLS accesses.
163 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000164 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000165
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000166 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
Andrew Trickccb67362012-02-03 05:12:41 +0000167 if (!getX86Subtarget().is64Bit())
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000168 addPass(createGlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000169
Chris Lattner12e97302006-09-04 04:14:57 +0000170 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000171}
172
Andrew Trickccb67362012-02-03 05:12:41 +0000173bool X86PassConfig::addPreRegAlloc() {
Anton Korobeynikov26590112008-04-23 18:23:05 +0000174 return false; // -print-machineinstr shouldn't print after this.
175}
176
Andrew Trickccb67362012-02-03 05:12:41 +0000177bool X86PassConfig::addPostRegAlloc() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000178 addPass(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000179 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000180}
181
Andrew Trickccb67362012-02-03 05:12:41 +0000182bool X86PassConfig::addPreEmitPass() {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000183 bool ShouldPrint = false;
Andrew Trickccb67362012-02-03 05:12:41 +0000184 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000185 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Craig Topper07d8b5e2011-11-16 05:02:04 +0000186 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000187 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000188
Andrew Trickccb67362012-02-03 05:12:41 +0000189 if (getX86Subtarget().hasAVX() && UseVZeroUpper) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000190 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000191 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000192 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000193
194 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000195}
196
Bill Wendling026e5d72009-04-29 23:29:43 +0000197bool X86TargetMachine::addCodeEmitter(PassManagerBase &PM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +0000198 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000199 PM.add(createX86JITCodeEmitterPass(*this, JCE));
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000200
201 return false;
202}