blob: e72450af54b2c76afb43be96db41504233ca58be [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 Lattner962d5be2003-01-13 00:51:23 +000016#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/PassManager.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000018#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000019#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000020#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Target/TargetOptions.h"
Chris Lattner833c3c22003-12-20 01:22:19 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000024extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000025 // Register the target.
David Woodhouse1c3996a2014-01-08 00:08:50 +000026 RegisterTargetMachine<X86TargetMachine> X(TheX86_32Target);
27 RegisterTargetMachine<X86TargetMachine> Y(TheX86_64Target);
Daniel Dunbare8338102009-07-15 20:24:03 +000028}
Douglas Gregor1b731d52009-06-16 20:12:29 +000029
David Woodhouse1c3996a2014-01-08 00:08:50 +000030void X86TargetMachine::anchor() { }
Jim Laskeyae92ce82006-09-07 23:39:26 +000031
Chris Lattner7e3abf12009-07-09 03:32:31 +000032/// X86TargetMachine ctor - Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +000033///
Eric Christopher66f676e2014-06-05 22:10:58 +000034X86TargetMachine::X86TargetMachine(const Target &T, StringRef TT, StringRef CPU,
35 StringRef FS, const TargetOptions &Options,
Evan Chengefd9b422011-07-20 07:51:56 +000036 Reloc::Model RM, CodeModel::Model CM,
David Woodhouse1c3996a2014-01-08 00:08:50 +000037 CodeGenOpt::Level OL)
Eric Christopher66f676e2014-06-05 22:10:58 +000038 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Eric Christophera08f30b2014-06-09 17:08:19 +000039 Subtarget(TT, CPU, FS, *this, Options.StackAlignmentOverride) {
Evan Cheng3a0c5e52011-06-23 17:54:54 +000040 // default to hard float ABI
Nick Lewycky50f02cb2011-12-02 22:16:29 +000041 if (Options.FloatABIType == FloatABI::Default)
Andrew Trick808a7a62012-02-03 05:12:30 +000042 this->Options.FloatABIType = FloatABI::Hard;
David Woodhouse1c3996a2014-01-08 00:08:50 +000043
Yaron Kerend7ba46b2014-04-19 13:47:43 +000044 // Windows stack unwinder gets confused when execution flow "falls through"
45 // after a call to 'noreturn' function.
46 // To prevent that, we emit a trap for 'unreachable' IR instructions.
47 // (which on X86, happens to be the 'ud2' instruction)
48 if (Subtarget.isTargetWin64())
49 this->Options.TrapUnreachable = true;
50
David Woodhouse1c3996a2014-01-08 00:08:50 +000051 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +000052}
Chris Lattner02a3d832002-10-29 22:37:54 +000053
Chris Lattner12e97302006-09-04 04:14:57 +000054//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000055// Command line options for x86
56//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +000057static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000058UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000059 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +000060 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000061
62//===----------------------------------------------------------------------===//
Chandler Carruth664e3542013-01-07 01:37:14 +000063// X86 Analysis Pass Setup
64//===----------------------------------------------------------------------===//
65
66void X86TargetMachine::addAnalysisPasses(PassManagerBase &PM) {
67 // Add first the target-independent BasicTTI pass, then our X86 pass. This
68 // allows the X86 pass to delegate to the target independent layer when
69 // appropriate.
Bill Wendlingafc10362013-06-19 20:51:24 +000070 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +000071 PM.add(createX86TargetTransformInfoPass(this));
72}
73
74
75//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +000076// Pass Pipeline Configuration
77//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +000078
Andrew Trickccb67362012-02-03 05:12:41 +000079namespace {
80/// X86 Code Generator Pass Configuration Options.
81class X86PassConfig : public TargetPassConfig {
82public:
Andrew Trickf8ea1082012-02-04 02:56:59 +000083 X86PassConfig(X86TargetMachine *TM, PassManagerBase &PM)
84 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +000085
86 X86TargetMachine &getX86TargetMachine() const {
87 return getTM<X86TargetMachine>();
88 }
89
90 const X86Subtarget &getX86Subtarget() const {
91 return *getX86TargetMachine().getSubtargetImpl();
92 }
93
Tim Northover277066a2014-07-01 18:53:31 +000094 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +000095 bool addInstSelector() override;
96 bool addILPOpts() override;
97 bool addPreRegAlloc() override;
98 bool addPostRegAlloc() override;
99 bool addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000100};
101} // namespace
102
Andrew Trickf8ea1082012-02-04 02:56:59 +0000103TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000104 return new X86PassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000105}
106
Tim Northover277066a2014-07-01 18:53:31 +0000107void X86PassConfig::addIRPasses() {
108 addPass(createX86AtomicExpandPass(&getX86TargetMachine()));
109
110 TargetPassConfig::addIRPasses();
111}
112
Andrew Trickccb67362012-02-03 05:12:41 +0000113bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000114 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000115 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000116
Hans Wennborg789acfb2012-06-01 16:27:21 +0000117 // For ELF, cleanup any local-dynamic TLS accesses.
118 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000119 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000120
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000121 addPass(createX86GlobalBaseRegPass());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000122
Chris Lattner12e97302006-09-04 04:14:57 +0000123 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000124}
125
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000126bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000127 addPass(&EarlyIfConverterID);
128 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000129}
130
Andrew Trickccb67362012-02-03 05:12:41 +0000131bool X86PassConfig::addPreRegAlloc() {
Anton Korobeynikov26590112008-04-23 18:23:05 +0000132 return false; // -print-machineinstr shouldn't print after this.
133}
134
Andrew Trickccb67362012-02-03 05:12:41 +0000135bool X86PassConfig::addPostRegAlloc() {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000136 addPass(createX86FloatingPointStackifierPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000137 return true; // -print-machineinstr should print after this.
Chris Lattner02a3d832002-10-29 22:37:54 +0000138}
139
Andrew Trickccb67362012-02-03 05:12:41 +0000140bool X86PassConfig::addPreEmitPass() {
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000141 bool ShouldPrint = false;
Andrew Trickccb67362012-02-03 05:12:41 +0000142 if (getOptLevel() != CodeGenOpt::None && getX86Subtarget().hasSSE2()) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000143 addPass(createExecutionDependencyFixPass(&X86::VR128RegClass));
Craig Topper07d8b5e2011-11-16 05:02:04 +0000144 ShouldPrint = true;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000145 }
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000146
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000147 if (UseVZeroUpper) {
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000148 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000149 ShouldPrint = true;
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000150 }
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000151
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000152 if (getOptLevel() != CodeGenOpt::None) {
Preston Gurda01daac2013-01-08 18:27:24 +0000153 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000154 addPass(createX86FixupLEAs());
155 ShouldPrint = true;
156 }
Preston Gurda01daac2013-01-08 18:27:24 +0000157
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000158 return ShouldPrint;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000159}