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