blob: ae21489d5e669e9576cf10fc229f32eccf34d418 [file] [log] [blame]
Chris Lattnera916db12006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// 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.
Chris Lattnera916db12006-09-04 04:16:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Target/TargetMachine.h"
Tom Roedereb7a3032014-11-11 21:08:02 +000015#include "llvm/Analysis/JumpInstrTableInfo.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000016#include "llvm/Analysis/Passes.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000017#include "llvm/CodeGen/AsmPrinter.h"
Tom Roedereb7a3032014-11-11 21:08:02 +000018#include "llvm/CodeGen/ForwardControlFlowIntegrity.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000019#include "llvm/CodeGen/JumpInstrTables.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000020#include "llvm/CodeGen/MachineFunctionAnalysis.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/Passes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000023#include "llvm/IR/IRPrintingPasses.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000024#include "llvm/IR/Verifier.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000025#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000026#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000027#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000028#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/PassManager.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000031#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000032#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000034#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetLowering.h"
37#include "llvm/Target/TargetLoweringObjectFile.h"
38#include "llvm/Target/TargetOptions.h"
39#include "llvm/Target/TargetRegisterInfo.h"
40#include "llvm/Target/TargetSubtargetInfo.h"
41#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000042using namespace llvm;
43
Andrew Trickf8ea1082012-02-04 02:56:59 +000044// Enable or disable FastISel. Both options are needed, because
45// FastISel is enabled by default with -fast, and we wish to be
46// able to enable or disable fast-isel independently from -O0.
47static cl::opt<cl::boolOrDefault>
48EnableFastISelOption("fast-isel", cl::Hidden,
49 cl::desc("Enable the \"fast\" instruction selector"));
50
Rafael Espindola227144c2013-05-13 01:16:13 +000051void LLVMTargetMachine::initAsmInfo() {
Eric Christopherd9134482014-08-04 21:25:23 +000052 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
53 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
Torok Edwinbe5020e2011-09-30 13:07:47 +000054 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
55 // and if the old one gets included then MCAsmInfo will be NULL and
56 // we'll crash later.
57 // Provide the user with a useful error message about what's wrong.
Daniel Sanders753e1762014-02-13 14:44:26 +000058 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000059 "Make sure you include the correct TargetSelect.h"
60 "and that InitializeAllTargetMCs() is being invoked!");
Daniel Sanders753e1762014-02-13 14:44:26 +000061
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000062 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000063 TmpAsmInfo->setUseIntegratedAssembler(false);
64
David Blaikie70bd1fd2014-03-27 20:45:41 +000065 if (Options.CompressDebugSections)
66 TmpAsmInfo->setCompressDebugSections(true);
67
Daniel Sanders753e1762014-02-13 14:44:26 +000068 AsmInfo = TmpAsmInfo;
Chris Lattner9a6cf912009-08-12 07:22:17 +000069}
70
Rafael Espindola227144c2013-05-13 01:16:13 +000071LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
72 StringRef CPU, StringRef FS,
73 TargetOptions Options,
74 Reloc::Model RM, CodeModel::Model CM,
75 CodeGenOpt::Level OL)
76 : TargetMachine(T, Triple, CPU, FS, Options) {
77 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
78}
79
Chandler Carruth664e3542013-01-07 01:37:14 +000080void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
Bill Wendlingafc10362013-06-19 20:51:24 +000081 PM.add(createBasicTargetTransformInfoPass(this));
Chandler Carruth664e3542013-01-07 01:37:14 +000082}
83
Andrew Trickf8ea1082012-02-04 02:56:59 +000084/// addPassesToX helper drives creation and initialization of TargetPassConfig.
85static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
86 PassManagerBase &PM,
Bob Wilsoncac3b902012-07-02 19:48:45 +000087 bool DisableVerify,
88 AnalysisID StartAfter,
89 AnalysisID StopAfter) {
Tom Roeder44cb65f2014-06-05 19:29:43 +000090
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000091 // Add internal analysis passes from the target machine.
92 TM->addAnalysisPasses(PM);
93
Eric Christopher710c0ae2014-05-19 21:18:47 +000094 // Targets may override createPassConfig to provide a target-specific
95 // subclass.
Andrew Trickf8ea1082012-02-04 02:56:59 +000096 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilsoncac3b902012-07-02 19:48:45 +000097 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +000098
99 // Set PassConfig options provided by TargetMachine.
100 PassConfig->setDisableVerify(DisableVerify);
101
Andrew Trick34914912012-02-06 22:51:15 +0000102 PM.add(PassConfig);
103
Andrew Trickf8ea1082012-02-04 02:56:59 +0000104 PassConfig->addIRPasses();
105
Bill Wendlingc786b312012-11-30 22:08:55 +0000106 PassConfig->addCodeGenPrepare();
107
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000108 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000109
110 PassConfig->addISelPrepare();
111
112 // Install a MachineModuleInfo class, which is an immutable pass that holds
113 // all the per-module stuff we're generating, including MCContext.
Eric Christopherd9134482014-08-04 21:25:23 +0000114 MachineModuleInfo *MMI = new MachineModuleInfo(
115 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
116 &TM->getSubtargetImpl()->getTargetLowering()->getObjFileLowering());
Andrew Trickf8ea1082012-02-04 02:56:59 +0000117 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000118
119 // Set up a MachineFunction for the rest of CodeGen to work on.
120 PM.add(new MachineFunctionAnalysis(*TM));
121
122 // Enable FastISel with -fast, but allow that to be overridden.
123 if (EnableFastISelOption == cl::BOU_TRUE ||
124 (TM->getOptLevel() == CodeGenOpt::None &&
125 EnableFastISelOption != cl::BOU_FALSE))
126 TM->setFastISel(true);
127
128 // Ask the target for an isel.
129 if (PassConfig->addInstSelector())
Craig Topperc0196b12014-04-14 00:51:57 +0000130 return nullptr;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000131
132 PassConfig->addMachinePasses();
133
Andrew Trickdd37d522012-02-08 21:22:39 +0000134 PassConfig->setInitialized();
135
Bill Wendlingafc10362013-06-19 20:51:24 +0000136 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000137}
138
Chris Lattneredcf0652010-02-03 05:55:08 +0000139bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
140 formatted_raw_ostream &Out,
141 CodeGenFileType FileType,
Bob Wilsoncac3b902012-07-02 19:48:45 +0000142 bool DisableVerify,
143 AnalysisID StartAfter,
144 AnalysisID StopAfter) {
Tom Roeder44cb65f2014-06-05 19:29:43 +0000145 // Passes to handle jumptable function annotations. These can't be handled at
146 // JIT time, so we don't add them directly to addPassesToGenerateCode.
Tom Roedereb7a3032014-11-11 21:08:02 +0000147 PM.add(createJumpInstrTableInfoPass(
148 getSubtargetImpl()->getInstrInfo()->getJumpInstrTableEntryBound()));
Tom Roeder44cb65f2014-06-05 19:29:43 +0000149 PM.add(createJumpInstrTablesPass(Options.JTType));
Tom Roedereb7a3032014-11-11 21:08:02 +0000150 if (Options.FCFI)
151 PM.add(createForwardControlFlowIntegrityPass(
152 Options.JTType, Options.CFIType, Options.CFIEnforcing,
153 Options.getCFIFuncName()));
Tom Roeder44cb65f2014-06-05 19:29:43 +0000154
Dan Gohmanacb05542008-09-25 00:37:07 +0000155 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000156 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
157 StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000158 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000159 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000160
Bob Wilsoncac3b902012-07-02 19:48:45 +0000161 if (StopAfter) {
162 // FIXME: The intent is that this should eventually write out a YAML file,
163 // containing the LLVM IR, the machine-level IR (when stopping after a
164 // machine-level pass), and whatever other information is needed to
165 // deserialize the code and resume compilation. For now, just write the
166 // LLVM IR.
Chandler Carruth9d805132014-01-12 11:30:46 +0000167 PM.add(createPrintModulePass(Out));
Bob Wilsoncac3b902012-07-02 19:48:45 +0000168 return false;
169 }
170
Eric Christopherc21d3d52014-05-16 00:32:52 +0000171 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000172 Context->setAllowTemporaryLabels(false);
173
James Molloy4c493e82011-09-07 17:24:38 +0000174 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Eric Christopherd9134482014-08-04 21:25:23 +0000175 const MCAsmInfo &MAI = *getMCAsmInfo();
176 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
177 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000178 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000179
Chris Lattnera916db12006-09-04 04:16:09 +0000180 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000181 case CGFT_AssemblyFile: {
Chris Lattner249453f2010-02-03 00:29:55 +0000182 MCInstPrinter *InstPrinter =
Jim Grosbachfd93a592012-03-05 19:33:20 +0000183 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling551a6775d2013-06-18 06:07:26 +0000184 MII, MRI, STI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000185
186 // Create a code emitter if asked to show the encoding.
Craig Topperc0196b12014-04-14 00:51:57 +0000187 MCCodeEmitter *MCE = nullptr;
Eric Christopher5d376062014-05-15 23:27:49 +0000188 if (Options.MCOptions.ShowMCEncoding)
Bill Wendling551a6775d2013-06-18 06:07:26 +0000189 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000190
Bill Wendling550c76d2013-09-09 19:48:37 +0000191 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
192 TargetCPU);
Eric Christopher5d376062014-05-15 23:27:49 +0000193 MCStreamer *S = getTarget().createAsmStreamer(
Eric Christophereb719722014-05-20 23:59:50 +0000194 *Context, Out, Options.MCOptions.AsmVerbose,
195 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
196 Options.MCOptions.ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000197 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000198 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000199 }
Chris Lattner8856a672010-02-02 23:57:42 +0000200 case CGFT_ObjectFile: {
201 // Create the code emitter for the target if it exists. If not, .o file
202 // emission fails.
Bill Wendling551a6775d2013-06-18 06:07:26 +0000203 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
204 *Context);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000205 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky26e7efe2012-11-22 14:10:40 +0000206 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000207 if (!MCE || !MAB)
Chris Lattneredcf0652010-02-03 05:55:08 +0000208 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000209
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000210 AsmStreamer.reset(
211 getTarget()
212 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
213 STI, Options.MCOptions.MCRelaxAll));
Chris Lattner8856a672010-02-02 23:57:42 +0000214 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000215 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000216 case CGFT_Null:
217 // The Null output is intended for use for performance analysis and testing,
218 // not real users.
Rafael Espindola1fc003e2014-06-20 13:11:28 +0000219 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000220 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000221 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000222
Chris Lattnere468f882010-03-13 20:55:24 +0000223 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000224 FunctionPass *Printer =
225 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000226 if (!Printer)
Chris Lattneredcf0652010-02-03 05:55:08 +0000227 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000228
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000229 PM.add(Printer);
Jim Grosbachd1f44652010-08-13 16:55:08 +0000230
Chris Lattneredcf0652010-02-03 05:55:08 +0000231 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000232}
233
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000234/// addPassesToEmitMC - Add passes to the specified pass manager to get
235/// machine code emitted with the MCJIT. This method returns true if machine
236/// code is not supported. It fills the MCContext Ctx pointer which can be
237/// used to build custom MCStreamer.
238///
239bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
240 MCContext *&Ctx,
Jim Grosbach7b162492011-03-18 22:48:41 +0000241 raw_ostream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000242 bool DisableVerify) {
243 // Add common CodeGen passes.
Craig Topperc0196b12014-04-14 00:51:57 +0000244 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000245 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000246 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000247
Eric Christopherc21d3d52014-05-16 00:32:52 +0000248 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000249 Ctx->setAllowTemporaryLabels(false);
250
Jim Grosbach7b162492011-03-18 22:48:41 +0000251 // Create the code emitter for the target if it exists. If not, .o file
252 // emission fails.
Eric Christopherd9134482014-08-04 21:25:23 +0000253 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000254 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Eric Christopherd9134482014-08-04 21:25:23 +0000255 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
256 *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000257 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
258 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000259 if (!MCE || !MAB)
Jim Grosbach7b162492011-03-18 22:48:41 +0000260 return true;
261
David Blaikie94598322015-01-18 20:29:04 +0000262 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
263 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
264 Options.MCOptions.MCRelaxAll));
Jim Grosbach7b162492011-03-18 22:48:41 +0000265
266 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000267 FunctionPass *Printer =
268 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000269 if (!Printer)
Jim Grosbach7b162492011-03-18 22:48:41 +0000270 return true;
271
Jim Grosbach7b162492011-03-18 22:48:41 +0000272 PM.add(Printer);
273
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000274 return false; // success!
275}