Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 1 | //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the LLVMTargetMachine class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetMachine.h" |
Daniel Dunbar | 7894578 | 2009-08-13 23:48:47 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/AsmPrinter.h" |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunctionAnalysis.h" |
| 17 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Passes.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 19 | #include "llvm/IR/IRPrintingPasses.h" |
Chris Lattner | af76e59 | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstrInfo.h" |
Chris Lattner | 56591ab | 2010-02-02 23:37:42 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/PassManager.h" |
Chris Lattner | 31442f9 | 2007-03-31 00:24:43 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Andrew Trick | d542265 | 2012-02-04 02:56:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/Support/FormattedStream.h" |
Evan Cheng | 3e74d6f | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetInstrInfo.h" |
| 31 | #include "llvm/Target/TargetLowering.h" |
| 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
| 33 | #include "llvm/Target/TargetOptions.h" |
| 34 | #include "llvm/Target/TargetRegisterInfo.h" |
| 35 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 36 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 39 | // Enable or disable FastISel. Both options are needed, because |
| 40 | // FastISel is enabled by default with -fast, and we wish to be |
| 41 | // able to enable or disable fast-isel independently from -O0. |
| 42 | static cl::opt<cl::boolOrDefault> |
| 43 | EnableFastISelOption("fast-isel", cl::Hidden, |
| 44 | cl::desc("Enable the \"fast\" instruction selector")); |
| 45 | |
Rafael Espindola | 4a97170 | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 46 | void LLVMTargetMachine::initAsmInfo() { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 47 | MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(), |
| 48 | TargetTriple); |
Torok Edwin | d398bae | 2011-09-30 13:07:47 +0000 | [diff] [blame] | 49 | // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, |
| 50 | // and if the old one gets included then MCAsmInfo will be NULL and |
| 51 | // we'll crash later. |
| 52 | // Provide the user with a useful error message about what's wrong. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 53 | assert(TmpAsmInfo && "MCAsmInfo not initialized. " |
Jim Grosbach | ca30f75 | 2011-10-25 20:30:48 +0000 | [diff] [blame] | 54 | "Make sure you include the correct TargetSelect.h" |
| 55 | "and that InitializeAllTargetMCs() is being invoked!"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 56 | |
| 57 | if (Options.DisableIntegratedAS) |
| 58 | TmpAsmInfo->setUseIntegratedAssembler(false); |
| 59 | |
| 60 | if (Options.CompressDebugSections) |
| 61 | TmpAsmInfo->setCompressDebugSections(true); |
| 62 | |
| 63 | AsmInfo = TmpAsmInfo; |
Chris Lattner | a7ac47c | 2009-08-12 07:22:17 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Rafael Espindola | 4a97170 | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 66 | LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple, |
| 67 | StringRef CPU, StringRef FS, |
| 68 | TargetOptions Options, |
| 69 | Reloc::Model RM, CodeModel::Model CM, |
| 70 | CodeGenOpt::Level OL) |
| 71 | : TargetMachine(T, Triple, CPU, FS, Options) { |
| 72 | CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL); |
| 73 | } |
| 74 | |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 75 | void LLVMTargetMachine::addAnalysisPasses(PassManagerBase &PM) { |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 76 | PM.add(createBasicTargetTransformInfoPass(this)); |
Chandler Carruth | aeef83c | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 79 | /// addPassesToX helper drives creation and initialization of TargetPassConfig. |
| 80 | static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM, |
| 81 | PassManagerBase &PM, |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 82 | bool DisableVerify, |
| 83 | AnalysisID StartAfter, |
| 84 | AnalysisID StopAfter) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 85 | // Add internal analysis passes from the target machine. |
| 86 | TM->addAnalysisPasses(PM); |
| 87 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 88 | // Targets may override createPassConfig to provide a target-specific |
| 89 | // subclass. |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 90 | TargetPassConfig *PassConfig = TM->createPassConfig(PM); |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 91 | PassConfig->setStartStopPasses(StartAfter, StopAfter); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 92 | |
| 93 | // Set PassConfig options provided by TargetMachine. |
| 94 | PassConfig->setDisableVerify(DisableVerify); |
| 95 | |
Andrew Trick | 6939fde | 2012-02-06 22:51:15 +0000 | [diff] [blame] | 96 | PM.add(PassConfig); |
| 97 | |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 98 | PassConfig->addIRPasses(); |
| 99 | |
Bill Wendling | 08510b1 | 2012-11-30 22:08:55 +0000 | [diff] [blame] | 100 | PassConfig->addCodeGenPrepare(); |
| 101 | |
Bob Wilson | 564fbf6 | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 102 | PassConfig->addPassesToHandleExceptions(); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 103 | |
| 104 | PassConfig->addISelPrepare(); |
| 105 | |
| 106 | // Install a MachineModuleInfo class, which is an immutable pass that holds |
| 107 | // all the per-module stuff we're generating, including MCContext. |
| 108 | MachineModuleInfo *MMI = |
| 109 | new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(), |
| 110 | &TM->getTargetLowering()->getObjFileLowering()); |
| 111 | PM.add(MMI); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 112 | |
| 113 | // Set up a MachineFunction for the rest of CodeGen to work on. |
| 114 | PM.add(new MachineFunctionAnalysis(*TM)); |
| 115 | |
| 116 | // Enable FastISel with -fast, but allow that to be overridden. |
| 117 | if (EnableFastISelOption == cl::BOU_TRUE || |
| 118 | (TM->getOptLevel() == CodeGenOpt::None && |
| 119 | EnableFastISelOption != cl::BOU_FALSE)) |
| 120 | TM->setFastISel(true); |
| 121 | |
| 122 | // Ask the target for an isel. |
| 123 | if (PassConfig->addInstSelector()) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 124 | return nullptr; |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 125 | |
| 126 | PassConfig->addMachinePasses(); |
| 127 | |
Andrew Trick | ffea03f | 2012-02-08 21:22:39 +0000 | [diff] [blame] | 128 | PassConfig->setInitialized(); |
| 129 | |
Bill Wendling | ea44281 | 2013-06-19 20:51:24 +0000 | [diff] [blame] | 130 | return &MMI->getContext(); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 133 | bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
| 134 | formatted_raw_ostream &Out, |
| 135 | CodeGenFileType FileType, |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 136 | bool DisableVerify, |
| 137 | AnalysisID StartAfter, |
| 138 | AnalysisID StopAfter) { |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 139 | // Add common CodeGen passes. |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 140 | MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, |
| 141 | StartAfter, StopAfter); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 142 | if (!Context) |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 143 | return true; |
Bill Wendling | 04523ea | 2007-02-08 01:36:53 +0000 | [diff] [blame] | 144 | |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 145 | if (StopAfter) { |
| 146 | // FIXME: The intent is that this should eventually write out a YAML file, |
| 147 | // containing the LLVM IR, the machine-level IR (when stopping after a |
| 148 | // machine-level pass), and whatever other information is needed to |
| 149 | // deserialize the code and resume compilation. For now, just write the |
| 150 | // LLVM IR. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 151 | PM.add(createPrintModulePass(Out)); |
Bob Wilson | 30a507a | 2012-07-02 19:48:45 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 155 | if (Options.MCOptions.MCSaveTempLabels) |
Daniel Dunbar | a7b8c2b | 2011-03-28 22:49:19 +0000 | [diff] [blame] | 156 | Context->setAllowTemporaryLabels(false); |
| 157 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 158 | const MCAsmInfo &MAI = *getMCAsmInfo(); |
Benjamin Kramer | 7948531 | 2012-05-20 11:24:27 +0000 | [diff] [blame] | 159 | const MCRegisterInfo &MRI = *getRegisterInfo(); |
Bill Wendling | 4ca0dda | 2013-06-18 06:07:26 +0000 | [diff] [blame] | 160 | const MCInstrInfo &MII = *getInstrInfo(); |
James Molloy | b950585 | 2011-09-07 17:24:38 +0000 | [diff] [blame] | 161 | const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 162 | std::unique_ptr<MCStreamer> AsmStreamer; |
Chris Lattner | 6cafdcc | 2010-02-02 23:45:17 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 164 | switch (FileType) { |
Chris Lattner | 6c8d6ec | 2010-02-03 00:29:55 +0000 | [diff] [blame] | 165 | case CGFT_AssemblyFile: { |
Chris Lattner | 6c8d6ec | 2010-02-03 00:29:55 +0000 | [diff] [blame] | 166 | MCInstPrinter *InstPrinter = |
Jim Grosbach | c6449b6 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 167 | getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, |
Bill Wendling | 4ca0dda | 2013-06-18 06:07:26 +0000 | [diff] [blame] | 168 | MII, MRI, STI); |
Daniel Dunbar | 3538c80 | 2010-05-18 17:22:19 +0000 | [diff] [blame] | 169 | |
| 170 | // Create a code emitter if asked to show the encoding. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 171 | MCCodeEmitter *MCE = nullptr; |
| 172 | if (Options.MCOptions.ShowMCEncoding) |
Bill Wendling | 4ca0dda | 2013-06-18 06:07:26 +0000 | [diff] [blame] | 173 | MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context); |
Daniel Dunbar | 3538c80 | 2010-05-18 17:22:19 +0000 | [diff] [blame] | 174 | |
Bill Wendling | da11df0 | 2013-09-09 19:48:37 +0000 | [diff] [blame] | 175 | MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), |
| 176 | TargetCPU); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 177 | MCStreamer *S = getTarget().createAsmStreamer( |
| 178 | *Context, Out, Options.MCOptions.AsmVerbose, |
| 179 | Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, |
| 180 | Options.MCOptions.ShowMCInst); |
Rafael Espindola | 195a0ce | 2010-11-19 02:26:16 +0000 | [diff] [blame] | 181 | AsmStreamer.reset(S); |
Chris Lattner | b5c5160 | 2010-02-02 19:14:27 +0000 | [diff] [blame] | 182 | break; |
Chris Lattner | 6c8d6ec | 2010-02-03 00:29:55 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | ac7798e | 2010-02-02 23:57:42 +0000 | [diff] [blame] | 184 | case CGFT_ObjectFile: { |
| 185 | // Create the code emitter for the target if it exists. If not, .o file |
| 186 | // emission fails. |
Bill Wendling | 4ca0dda | 2013-06-18 06:07:26 +0000 | [diff] [blame] | 187 | MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, |
| 188 | *Context); |
Bill Wendling | c3cee57 | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 189 | MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), |
Eli Bendersky | 9f696c8 | 2012-11-22 14:10:40 +0000 | [diff] [blame] | 190 | TargetCPU); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 191 | if (!MCE || !MAB) |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 192 | return true; |
Daniel Dunbar | cb8326d | 2010-05-26 21:48:55 +0000 | [diff] [blame] | 193 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 194 | AsmStreamer.reset(getTarget().createMCObjectStreamer( |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 195 | getTargetTriple(), *Context, *MAB, Out, MCE, STI, |
| 196 | Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack)); |
Chris Lattner | ac7798e | 2010-02-02 23:57:42 +0000 | [diff] [blame] | 197 | break; |
Chris Lattner | 0823d2a | 2010-02-02 18:44:12 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 199 | case CGFT_Null: |
| 200 | // The Null output is intended for use for performance analysis and testing, |
| 201 | // not real users. |
| 202 | AsmStreamer.reset(createNullStreamer(*Context)); |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 203 | break; |
Chris Lattner | 4787705 | 2006-09-04 04:16:09 +0000 | [diff] [blame] | 204 | } |
Daniel Dunbar | fdb5a86 | 2010-05-23 17:44:06 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 11d53c1 | 2010-03-13 20:55:24 +0000 | [diff] [blame] | 206 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
Chris Lattner | b23569a | 2010-04-04 08:18:47 +0000 | [diff] [blame] | 207 | FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 208 | if (!Printer) |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 209 | return true; |
Jim Grosbach | c92bb50 | 2010-08-13 16:55:08 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 11d53c1 | 2010-03-13 20:55:24 +0000 | [diff] [blame] | 211 | // If successful, createAsmPrinter took ownership of AsmStreamer. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 212 | AsmStreamer.release(); |
Jim Grosbach | c92bb50 | 2010-08-13 16:55:08 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 6cafdcc | 2010-02-02 23:45:17 +0000 | [diff] [blame] | 214 | PM.add(Printer); |
Jim Grosbach | c92bb50 | 2010-08-13 16:55:08 +0000 | [diff] [blame] | 215 | |
Chris Lattner | 5669e30 | 2010-02-03 05:55:08 +0000 | [diff] [blame] | 216 | return false; |
Dan Gohman | 02dae4b | 2008-09-25 00:37:07 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 219 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to |
Chris Lattner | 3813d8a | 2010-02-02 22:31:11 +0000 | [diff] [blame] | 220 | /// get machine code emitted. This uses a JITCodeEmitter object to handle |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 221 | /// actually outputting the machine code and resolving things like the address |
Eric Christopher | 2565de9 | 2013-10-08 16:47:11 +0000 | [diff] [blame] | 222 | /// of functions. This method should return true if machine code emission is |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 223 | /// not supported. |
| 224 | /// |
| 225 | bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM, |
| 226 | JITCodeEmitter &JCE, |
Dan Gohman | 8772f50 | 2010-02-28 00:41:59 +0000 | [diff] [blame] | 227 | bool DisableVerify) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 228 | // Add common CodeGen passes. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 229 | MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, |
| 230 | nullptr); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 231 | if (!Context) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 232 | return true; |
| 233 | |
Evan Cheng | b95fc31 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 234 | addCodeEmitter(PM, JCE); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 235 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 236 | return false; // success! |
| 237 | } |
| 238 | |
Reid Kleckner | c96a82a | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 239 | /// addPassesToEmitMC - Add passes to the specified pass manager to get |
| 240 | /// machine code emitted with the MCJIT. This method returns true if machine |
| 241 | /// code is not supported. It fills the MCContext Ctx pointer which can be |
| 242 | /// used to build custom MCStreamer. |
| 243 | /// |
| 244 | bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, |
| 245 | MCContext *&Ctx, |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 246 | raw_ostream &Out, |
Reid Kleckner | c96a82a | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 247 | bool DisableVerify) { |
| 248 | // Add common CodeGen passes. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 249 | Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr); |
Andrew Trick | 061efcf | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 250 | if (!Ctx) |
Reid Kleckner | c96a82a | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 251 | return true; |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 252 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 253 | if (Options.MCOptions.MCSaveTempLabels) |
Daniel Dunbar | a7b8c2b | 2011-03-28 22:49:19 +0000 | [diff] [blame] | 254 | Ctx->setAllowTemporaryLabels(false); |
| 255 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 256 | // Create the code emitter for the target if it exists. If not, .o file |
| 257 | // emission fails. |
Benjamin Kramer | 6514551 | 2012-05-20 17:24:08 +0000 | [diff] [blame] | 258 | const MCRegisterInfo &MRI = *getRegisterInfo(); |
Evan Cheng | 59ee62d | 2011-07-11 03:57:24 +0000 | [diff] [blame] | 259 | const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); |
Jim Grosbach | 918f55f | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 260 | MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI, |
| 261 | STI, *Ctx); |
Bill Wendling | c3cee57 | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 262 | MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), |
| 263 | TargetCPU); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 264 | if (!MCE || !MAB) |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 265 | return true; |
| 266 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 267 | std::unique_ptr<MCStreamer> AsmStreamer; |
| 268 | AsmStreamer.reset(getTarget().createMCObjectStreamer( |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 269 | getTargetTriple(), *Ctx, *MAB, Out, MCE, STI, |
| 270 | Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack)); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 271 | |
| 272 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
| 273 | FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 274 | if (!Printer) |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 275 | return true; |
| 276 | |
| 277 | // If successful, createAsmPrinter took ownership of AsmStreamer. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 278 | AsmStreamer.release(); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 279 | |
| 280 | PM.add(Printer); |
| 281 | |
Reid Kleckner | c96a82a | 2010-07-22 05:58:53 +0000 | [diff] [blame] | 282 | return false; // success! |
| 283 | } |