blob: f071d229d8964edd345166aed04a0c4c1bf05139 [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
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000014#include "MCTargetDesc/X86MCTargetDesc.h"
Chris Lattnera32b4052002-12-24 00:04:01 +000015#include "X86.h"
Zvi Rackover76dbf262016-11-15 06:34:33 +000016#include "X86CallLowering.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000017#include "X86LegalizerInfo.h"
Evandro Menezes94edf022017-02-01 02:54:34 +000018#include "X86MacroFusion.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000019#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
Aditya Nandakumara2719322014-11-13 09:26:31 +000021#include "X86TargetObjectFile.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000022#include "X86TargetTransformInfo.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000023#include "llvm/ADT/Optional.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000024#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/ADT/SmallString.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000026#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Triple.h"
28#include "llvm/Analysis/TargetTransformInfo.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000029#include "llvm/CodeGen/ExecutionDepsFix.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000030#include "llvm/CodeGen/GlobalISel/CallLowering.h"
Zvi Rackover76dbf262016-11-15 06:34:33 +000031#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
Matthias Braune6ff30b2017-03-18 05:08:58 +000032#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
Igor Bregerb4442f32017-02-10 07:05:56 +000033#include "llvm/CodeGen/GlobalISel/Legalizer.h"
34#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Matthias Braun115efcd2016-11-28 20:11:54 +000035#include "llvm/CodeGen/MachineScheduler.h"
Chris Lattner962d5be2003-01-13 00:51:23 +000036#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000037#include "llvm/CodeGen/TargetPassConfig.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000038#include "llvm/IR/Attributes.h"
39#include "llvm/IR/DataLayout.h"
Eric Christopher3faf2f12014-10-06 06:45:36 +000040#include "llvm/IR/Function.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000041#include "llvm/Pass.h"
42#include "llvm/Support/CodeGen.h"
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +000043#include "llvm/Support/CommandLine.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000044#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000046#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetOptions.h"
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000048#include <memory>
49#include <string>
50
Chris Lattner833c3c22003-12-20 01:22:19 +000051using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000052
Sanjay Patel08829ba2015-06-10 20:32:21 +000053static cl::opt<bool> EnableMachineCombinerPass("x86-machine-combiner",
54 cl::desc("Enable the machine combiner pass"),
55 cl::init(true), cl::Hidden);
56
David Majnemer0ad363e2015-08-18 19:07:12 +000057namespace llvm {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000058
David Majnemer0ad363e2015-08-18 19:07:12 +000059void initializeWinEHStatePassPass(PassRegistry &);
Lama Saba2ea271b2017-05-18 08:11:50 +000060void initializeFixupLEAPassPass(PassRegistry &);
Matthias Braune6ff30b2017-03-18 05:08:58 +000061void initializeX86ExecutionDepsFixPass(PassRegistry &);
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000062
63} // end namespace llvm
David Majnemer0ad363e2015-08-18 19:07:12 +000064
NAKAMURA Takumi0544fe72011-02-17 12:23:50 +000065extern "C" void LLVMInitializeX86Target() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +000066 // Register the target.
Mehdi Aminif42454b2016-10-09 23:00:34 +000067 RegisterTargetMachine<X86TargetMachine> X(getTheX86_32Target());
68 RegisterTargetMachine<X86TargetMachine> Y(getTheX86_64Target());
David Majnemer0ad363e2015-08-18 19:07:12 +000069
70 PassRegistry &PR = *PassRegistry::getPassRegistry();
Zvi Rackover76dbf262016-11-15 06:34:33 +000071 initializeGlobalISel(PR);
David Majnemer0ad363e2015-08-18 19:07:12 +000072 initializeWinEHStatePassPass(PR);
Ahmed Bougacha068ac4a2016-05-07 01:11:10 +000073 initializeFixupBWInstPassPass(PR);
Gadi Haber19c4fc52016-12-28 10:12:48 +000074 initializeEvexToVexInstPassPass(PR);
Lama Saba2ea271b2017-05-18 08:11:50 +000075 initializeFixupLEAPassPass(PR);
Matthias Braune6ff30b2017-03-18 05:08:58 +000076 initializeX86ExecutionDepsFixPass(PR);
Daniel Dunbare8338102009-07-15 20:24:03 +000077}
Douglas Gregor1b731d52009-06-16 20:12:29 +000078
Aditya Nandakumara2719322014-11-13 09:26:31 +000079static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
80 if (TT.isOSBinFormatMachO()) {
81 if (TT.getArch() == Triple::x86_64)
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000082 return llvm::make_unique<X86_64MachoTargetObjectFile>();
83 return llvm::make_unique<TargetLoweringObjectFileMachO>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000084 }
85
Davide Italianoa9f85d62016-09-26 22:53:15 +000086 if (TT.isOSFreeBSD())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000087 return llvm::make_unique<X86FreeBSDTargetObjectFile>();
Nikolai Bozhenovb7bf3862017-05-09 10:14:03 +000088 if (TT.isOSLinux() || TT.isOSNaCl() || TT.isOSIAMCU())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000089 return llvm::make_unique<X86LinuxNaClTargetObjectFile>();
Davide Italiano9b8e3d32017-06-21 20:36:32 +000090 if (TT.isOSSolaris())
91 return llvm::make_unique<X86SolarisTargetObjectFile>();
Petr Hoseke023d622016-10-06 05:17:26 +000092 if (TT.isOSFuchsia())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000093 return llvm::make_unique<X86FuchsiaTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000094 if (TT.isOSBinFormatELF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000095 return llvm::make_unique<X86ELFTargetObjectFile>();
Pat Gavlinb3990952015-08-14 22:41:43 +000096 if (TT.isKnownWindowsMSVCEnvironment() || TT.isWindowsCoreCLREnvironment())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000097 return llvm::make_unique<X86WindowsTargetObjectFile>();
Aditya Nandakumara2719322014-11-13 09:26:31 +000098 if (TT.isOSBinFormatCOFF())
Eugene Zelenkofbd13c52017-02-02 22:55:55 +000099 return llvm::make_unique<TargetLoweringObjectFileCOFF>();
Aditya Nandakumara2719322014-11-13 09:26:31 +0000100 llvm_unreachable("unknown subtarget type");
101}
102
Eric Christopher8b770652015-01-26 19:03:15 +0000103static std::string computeDataLayout(const Triple &TT) {
104 // X86 is little endian
105 std::string Ret = "e";
106
107 Ret += DataLayout::getManglingComponent(TT);
108 // X86 and x32 have 32 bit pointers.
109 if ((TT.isArch64Bit() &&
110 (TT.getEnvironment() == Triple::GNUX32 || TT.isOSNaCl())) ||
111 !TT.isArch64Bit())
112 Ret += "-p:32:32";
113
114 // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
115 if (TT.isArch64Bit() || TT.isOSWindows() || TT.isOSNaCl())
116 Ret += "-i64:64";
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000117 else if (TT.isOSIAMCU())
118 Ret += "-i64:32-f64:32";
Eric Christopher8b770652015-01-26 19:03:15 +0000119 else
120 Ret += "-f64:32:64";
121
122 // Some ABIs align long double to 128 bits, others to 32.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000123 if (TT.isOSNaCl() || TT.isOSIAMCU())
Eric Christopher8b770652015-01-26 19:03:15 +0000124 ; // No f80
125 else if (TT.isArch64Bit() || TT.isOSDarwin())
126 Ret += "-f80:128";
127 else
128 Ret += "-f80:32";
129
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000130 if (TT.isOSIAMCU())
131 Ret += "-f128:32";
132
Eric Christopher8b770652015-01-26 19:03:15 +0000133 // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
134 if (TT.isArch64Bit())
135 Ret += "-n8:16:32:64";
136 else
137 Ret += "-n8:16:32";
138
139 // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
Andrey Turetskiy2396c382016-02-10 11:57:06 +0000140 if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
Reid Kleckner60d52322015-04-30 22:11:59 +0000141 Ret += "-a:0:32-S32";
Eric Christopher8b770652015-01-26 19:03:15 +0000142 else
143 Ret += "-S128";
144
145 return Ret;
146}
147
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000148static Reloc::Model getEffectiveRelocModel(const Triple &TT,
149 Optional<Reloc::Model> RM) {
150 bool is64Bit = TT.getArch() == Triple::x86_64;
151 if (!RM.hasValue()) {
152 // Darwin defaults to PIC in 64 bit mode and dynamic-no-pic in 32 bit mode.
153 // Win64 requires rip-rel addressing, thus we force it to PIC. Otherwise we
154 // use static relocation model by default.
155 if (TT.isOSDarwin()) {
156 if (is64Bit)
157 return Reloc::PIC_;
158 return Reloc::DynamicNoPIC;
159 }
160 if (TT.isOSWindows() && is64Bit)
161 return Reloc::PIC_;
162 return Reloc::Static;
163 }
164
165 // ELF and X86-64 don't have a distinct DynamicNoPIC model. DynamicNoPIC
166 // is defined as a model for code which may be used in static or dynamic
167 // executables but not necessarily a shared library. On X86-32 we just
168 // compile in -static mode, in x86-64 we use PIC.
169 if (*RM == Reloc::DynamicNoPIC) {
170 if (is64Bit)
171 return Reloc::PIC_;
172 if (!TT.isOSDarwin())
173 return Reloc::Static;
174 }
175
176 // If we are on Darwin, disallow static relocation model in X86-64 mode, since
177 // the Mach-O file format doesn't support it.
178 if (*RM == Reloc::Static && TT.isOSDarwin() && is64Bit)
179 return Reloc::PIC_;
180
181 return *RM;
182}
183
Rafael Espindola79e238a2017-08-03 02:16:21 +0000184static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM,
185 bool JIT, bool Is64Bit) {
186 if (CM)
187 return *CM;
188 if (JIT)
189 return Is64Bit ? CodeModel::Large : CodeModel::Small;
190 return CodeModel::Small;
191}
192
Rafael Espindola38af4d62016-05-18 16:00:24 +0000193/// Create an X86 target.
Chris Lattner02a3d832002-10-29 22:37:54 +0000194///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000195X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
196 StringRef CPU, StringRef FS,
197 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000198 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000199 Optional<CodeModel::Model> CM,
200 CodeGenOpt::Level OL, bool JIT)
201 : LLVMTargetMachine(
202 T, computeDataLayout(TT), TT, CPU, FS, Options,
203 getEffectiveRelocModel(TT, RM),
204 getEffectiveCodeModel(CM, JIT, TT.getArch() == Triple::x86_64), OL),
Eric Christopher5653e5d2016-09-20 22:19:33 +0000205 TLOF(createTLOF(getTargetTriple())) {
Reid Klecknerae44e872015-10-09 01:13:17 +0000206 // Windows stack unwinder gets confused when execution flow "falls through"
207 // after a call to 'noreturn' function.
208 // To prevent that, we emit a trap for 'unreachable' IR instructions.
209 // (which on X86, happens to be the 'ud2' instruction)
Paul Robinsonf81836b2016-03-24 00:10:03 +0000210 // On PS4, the "return address" of a 'noreturn' call must still be within
211 // the calling function, and TrapUnreachable is an easy way to get that.
Eric Christopheref579d22016-09-20 16:04:59 +0000212 // The check here for 64-bit windows is a bit icky, but as we're unlikely
213 // to ever want to mix 32 and 64-bit windows code in a single module
214 // this should be fine.
215 if ((TT.isOSWindows() && TT.getArch() == Triple::x86_64) || TT.isPS4())
Reid Klecknerae44e872015-10-09 01:13:17 +0000216 this->Options.TrapUnreachable = true;
217
David Woodhouse1c3996a2014-01-08 00:08:50 +0000218 initAsmInfo();
Chris Lattnera1d312c2006-02-03 18:59:39 +0000219}
Chris Lattner02a3d832002-10-29 22:37:54 +0000220
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000221X86TargetMachine::~X86TargetMachine() = default;
Reid Kleckner357600e2014-11-20 23:37:18 +0000222
Eric Christopher3faf2f12014-10-06 06:45:36 +0000223const X86Subtarget *
224X86TargetMachine::getSubtargetImpl(const Function &F) const {
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000225 Attribute CPUAttr = F.getFnAttribute("target-cpu");
226 Attribute FSAttr = F.getFnAttribute("target-features");
Eric Christopher3faf2f12014-10-06 06:45:36 +0000227
David Majnemer498f2fd2016-05-20 20:41:24 +0000228 StringRef CPU = !CPUAttr.hasAttribute(Attribute::None)
229 ? CPUAttr.getValueAsString()
230 : (StringRef)TargetCPU;
231 StringRef FS = !FSAttr.hasAttribute(Attribute::None)
232 ? FSAttr.getValueAsString()
233 : (StringRef)TargetFS;
234
235 SmallString<512> Key;
236 Key.reserve(CPU.size() + FS.size());
237 Key += CPU;
238 Key += FS;
Eric Christopher3faf2f12014-10-06 06:45:36 +0000239
240 // FIXME: This is related to the code below to reset the target options,
241 // we need to know whether or not the soft float flag is set on the
242 // function before we can generate a subtarget. We also need to use
243 // it as a key for the subtarget since that can be the only difference
244 // between two functions.
Eric Christopher824f42f2015-05-12 01:26:05 +0000245 bool SoftFloat =
Eric Christopher824f42f2015-05-12 01:26:05 +0000246 F.getFnAttribute("use-soft-float").getValueAsString() == "true";
247 // If the soft float attribute is set on the function turn on the soft float
248 // subtarget feature.
249 if (SoftFloat)
David Majnemer498f2fd2016-05-20 20:41:24 +0000250 Key += FS.empty() ? "+soft-float" : ",+soft-float";
Eric Christopher3faf2f12014-10-06 06:45:36 +0000251
David Majnemer498f2fd2016-05-20 20:41:24 +0000252 FS = Key.substr(CPU.size());
David Majnemerca290232016-05-20 18:16:06 +0000253
254 auto &I = SubtargetMap[Key];
Eric Christopher3faf2f12014-10-06 06:45:36 +0000255 if (!I) {
256 // This needs to be done before we create a new subtarget since any
257 // creation will depend on the TM and the code generation flags on the
258 // function that reside in TargetOptions.
259 resetTargetOptions(F);
Daniel Sandersc81f4502015-06-16 15:44:21 +0000260 I = llvm::make_unique<X86Subtarget>(TargetTriple, CPU, FS, *this,
Daniel Sandersa1b2db792017-05-19 11:08:33 +0000261 Options.StackAlignmentOverride);
Eric Christopher3faf2f12014-10-06 06:45:36 +0000262 }
263 return I.get();
264}
265
Chris Lattner12e97302006-09-04 04:14:57 +0000266//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000267// Command line options for x86
268//===----------------------------------------------------------------------===//
Benjamin Kramer7859d2e2011-09-03 03:45:06 +0000269static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +0000270UseVZeroUpper("x86-use-vzeroupper", cl::Hidden,
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000271 cl::desc("Minimize AVX to SSE transition penalty"),
Eli Friedman20439a42011-11-17 00:21:52 +0000272 cl::init(true));
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000273
274//===----------------------------------------------------------------------===//
Chandler Carruth93dcdc42015-01-31 11:17:59 +0000275// X86 TTI query.
Chandler Carruth664e3542013-01-07 01:37:14 +0000276//===----------------------------------------------------------------------===//
277
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000278TargetIRAnalysis X86TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000279 return TargetIRAnalysis([this](const Function &F) {
280 return TargetTransformInfo(X86TTIImpl(this, F));
281 });
Chandler Carruth664e3542013-01-07 01:37:14 +0000282}
283
Chandler Carruth664e3542013-01-07 01:37:14 +0000284//===----------------------------------------------------------------------===//
Chris Lattner12e97302006-09-04 04:14:57 +0000285// Pass Pipeline Configuration
286//===----------------------------------------------------------------------===//
Chris Lattner1d6ba3e2003-08-05 16:34:44 +0000287
Andrew Trickccb67362012-02-03 05:12:41 +0000288namespace {
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000289
Andrew Trickccb67362012-02-03 05:12:41 +0000290/// X86 Code Generator Pass Configuration Options.
291class X86PassConfig : public TargetPassConfig {
292public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000293 X86PassConfig(X86TargetMachine &TM, PassManagerBase &PM)
Andrew Trickf8ea1082012-02-04 02:56:59 +0000294 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000295
296 X86TargetMachine &getX86TargetMachine() const {
297 return getTM<X86TargetMachine>();
298 }
299
Matthias Braun115efcd2016-11-28 20:11:54 +0000300 ScheduleDAGInstrs *
301 createMachineScheduler(MachineSchedContext *C) const override {
302 ScheduleDAGMILive *DAG = createGenericSchedLive(C);
Evandro Menezes94edf022017-02-01 02:54:34 +0000303 DAG->addMutation(createX86MacroFusionDAGMutation());
Matthias Braun115efcd2016-11-28 20:11:54 +0000304 return DAG;
305 }
306
Tim Northover277066a2014-07-01 18:53:31 +0000307 void addIRPasses() override;
Craig Topper2d9361e2014-03-09 07:44:38 +0000308 bool addInstSelector() override;
Zvi Rackover76dbf262016-11-15 06:34:33 +0000309 bool addIRTranslator() override;
310 bool addLegalizeMachineIR() override;
311 bool addRegBankSelect() override;
312 bool addGlobalInstructionSelect() override;
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000313 bool addILPOpts() override;
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000314 bool addPreISel() override;
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000315 void addPreRegAlloc() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000316 void addPostRegAlloc() override;
317 void addPreEmitPass() override;
Quentin Colombet494eb602015-05-22 18:10:47 +0000318 void addPreSched2() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000319};
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000320
Matthias Braune6ff30b2017-03-18 05:08:58 +0000321class X86ExecutionDepsFix : public ExecutionDepsFix {
322public:
323 static char ID;
324 X86ExecutionDepsFix() : ExecutionDepsFix(ID, X86::VR128XRegClass) {}
325 StringRef getPassName() const override {
326 return "X86 Execution Dependency Fix";
327 }
328};
329char X86ExecutionDepsFix::ID;
330
Eugene Zelenkofbd13c52017-02-02 22:55:55 +0000331} // end anonymous namespace
Andrew Trickccb67362012-02-03 05:12:41 +0000332
Matthias Braune6ff30b2017-03-18 05:08:58 +0000333INITIALIZE_PASS(X86ExecutionDepsFix, "x86-execution-deps-fix",
Davide Italiano200e5e12017-03-21 19:17:23 +0000334 "X86 Execution Dependency Fix", false, false)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000335
Andrew Trickf8ea1082012-02-04 02:56:59 +0000336TargetPassConfig *X86TargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000337 return new X86PassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000338}
339
Tim Northover277066a2014-07-01 18:53:31 +0000340void X86PassConfig::addIRPasses() {
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000341 addPass(createAtomicExpandPass());
Tim Northover277066a2014-07-01 18:53:31 +0000342
343 TargetPassConfig::addIRPasses();
David L Kreitzer01a057a2016-10-14 18:20:41 +0000344
345 if (TM->getOptLevel() != CodeGenOpt::None)
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000346 addPass(createInterleavedAccessPass());
Tim Northover277066a2014-07-01 18:53:31 +0000347}
348
Andrew Trickccb67362012-02-03 05:12:41 +0000349bool X86PassConfig::addInstSelector() {
Nate Begemanbe1f3142005-08-18 23:53:15 +0000350 // Install an instruction selector.
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000351 addPass(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
Dan Gohman19145312008-10-25 17:46:52 +0000352
Hans Wennborg789acfb2012-06-01 16:27:21 +0000353 // For ELF, cleanup any local-dynamic TLS accesses.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000354 if (TM->getTargetTriple().isOSBinFormatELF() &&
Eric Christopher24f3f652015-02-05 19:27:04 +0000355 getOptLevel() != CodeGenOpt::None)
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000356 addPass(createCleanupLocalDynamicTLSPass());
Hans Wennborg789acfb2012-06-01 16:27:21 +0000357
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000358 addPass(createX86GlobalBaseRegPass());
Chris Lattner12e97302006-09-04 04:14:57 +0000359 return false;
Brian Gaekeac94bab2003-06-18 21:43:21 +0000360}
361
Zvi Rackover76dbf262016-11-15 06:34:33 +0000362bool X86PassConfig::addIRTranslator() {
363 addPass(new IRTranslator());
364 return false;
365}
366
367bool X86PassConfig::addLegalizeMachineIR() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000368 addPass(new Legalizer());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000369 return false;
370}
371
372bool X86PassConfig::addRegBankSelect() {
Igor Bregerb4442f32017-02-10 07:05:56 +0000373 addPass(new RegBankSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000374 return false;
375}
376
377bool X86PassConfig::addGlobalInstructionSelect() {
Igor Bregerf7359d82017-02-22 12:25:09 +0000378 addPass(new InstructionSelect());
Zvi Rackover76dbf262016-11-15 06:34:33 +0000379 return false;
380}
Zvi Rackover76dbf262016-11-15 06:34:33 +0000381
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000382bool X86PassConfig::addILPOpts() {
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000383 addPass(&EarlyIfConverterID);
Sanjay Patel08829ba2015-06-10 20:32:21 +0000384 if (EnableMachineCombinerPass)
385 addPass(&MachineCombinerID);
Amjad Aboud4563c062017-07-16 17:39:56 +0000386 addPass(createX86CmovConverterPass());
Eric Christopher6b0fcfe2014-05-21 23:40:26 +0000387 return true;
Jakob Stoklund Olesen213a2f82013-01-17 00:58:38 +0000388}
389
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000390bool X86PassConfig::addPreISel() {
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000391 // Only add this pass for 32-bit x86 Windows.
Daniel Sandersc81f4502015-06-16 15:44:21 +0000392 const Triple &TT = TM->getTargetTriple();
Reid Kleckner5b8ebfb2015-05-29 20:43:10 +0000393 if (TT.isOSWindows() && TT.getArch() == Triple::x86)
Reid Kleckner0738a9c2015-05-05 17:44:16 +0000394 addPass(createX86WinEHStatePass());
395 return true;
396}
397
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000398void X86PassConfig::addPreRegAlloc() {
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000399 if (getOptLevel() != CodeGenOpt::None) {
Dehao Chen6b737dd2017-05-31 23:25:25 +0000400 addPass(&LiveRangeShrinkID);
Nico Weberead8f8f2016-07-14 15:07:44 +0000401 addPass(createX86FixupSetCC());
402 addPass(createX86OptimizeLEAs());
Nico Weber5bb28422016-07-14 15:40:22 +0000403 addPass(createX86CallFrameOptimization());
Michael Kupersteincfbac5f2016-07-11 20:40:44 +0000404 }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000405
Hans Wennborg8eb336c2016-05-18 16:10:17 +0000406 addPass(createX86WinAllocaExpander());
Michael Kuperstein13fbd452015-02-01 16:56:04 +0000407}
408
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000409void X86PassConfig::addPostRegAlloc() {
Rafael Espindola01c73612014-12-11 20:03:57 +0000410 addPass(createX86FloatingPointStackifierPass());
Rafael Espindola01c73612014-12-11 20:03:57 +0000411}
Bruno Cardoso Lopes2a3ffb52011-08-23 01:14:17 +0000412
Quentin Colombet494eb602015-05-22 18:10:47 +0000413void X86PassConfig::addPreSched2() { addPass(createX86ExpandPseudoPass()); }
414
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000415void X86PassConfig::addPreEmitPass() {
Eric Christopher24f3f652015-02-05 19:27:04 +0000416 if (getOptLevel() != CodeGenOpt::None)
Matthias Braune6ff30b2017-03-18 05:08:58 +0000417 addPass(new X86ExecutionDepsFix());
Rafael Espindola01c73612014-12-11 20:03:57 +0000418
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000419 if (UseVZeroUpper)
Matthias Braunb2f23882014-12-11 23:18:03 +0000420 addPass(createX86IssueVZeroUpperPass());
Bruno Cardoso Lopes62d79872011-09-15 18:27:32 +0000421
Eric Christopher0d5c99e2014-05-22 01:46:02 +0000422 if (getOptLevel() != CodeGenOpt::None) {
Kevin B. Smith6a833502016-02-11 19:43:04 +0000423 addPass(createX86FixupBWInsts());
Matthias Braunb2f23882014-12-11 23:18:03 +0000424 addPass(createX86PadShortFunctions());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000425 addPass(createX86FixupLEAs());
Gadi Haber19c4fc52016-12-28 10:12:48 +0000426 addPass(createX86EvexToVexInsts());
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000427 }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000428}