/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/CodeGen/LLVMTargetMachine.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file implements the LLVMTargetMachine class. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "llvm/Analysis/Passes.h" |
15 | | #include "llvm/CodeGen/AsmPrinter.h" |
16 | | #include "llvm/CodeGen/BasicTTIImpl.h" |
17 | | #include "llvm/CodeGen/MachineModuleInfo.h" |
18 | | #include "llvm/CodeGen/Passes.h" |
19 | | #include "llvm/CodeGen/TargetPassConfig.h" |
20 | | #include "llvm/IR/IRPrintingPasses.h" |
21 | | #include "llvm/IR/LegacyPassManager.h" |
22 | | #include "llvm/IR/Verifier.h" |
23 | | #include "llvm/MC/MCAsmInfo.h" |
24 | | #include "llvm/MC/MCContext.h" |
25 | | #include "llvm/MC/MCInstrInfo.h" |
26 | | #include "llvm/MC/MCStreamer.h" |
27 | | #include "llvm/MC/MCSubtargetInfo.h" |
28 | | #include "llvm/Support/CommandLine.h" |
29 | | #include "llvm/Support/ErrorHandling.h" |
30 | | #include "llvm/Support/FormattedStream.h" |
31 | | #include "llvm/Support/TargetRegistry.h" |
32 | | #include "llvm/Target/TargetLoweringObjectFile.h" |
33 | | #include "llvm/Target/TargetMachine.h" |
34 | | #include "llvm/Target/TargetOptions.h" |
35 | | #include "llvm/Transforms/Scalar.h" |
36 | | using namespace llvm; |
37 | | |
38 | 42.2k | void LLVMTargetMachine::initAsmInfo() { |
39 | 42.2k | MRI = TheTarget.createMCRegInfo(getTargetTriple().str()); |
40 | 42.2k | MII = TheTarget.createMCInstrInfo(); |
41 | 42.2k | // FIXME: Having an MCSubtargetInfo on the target machine is a hack due |
42 | 42.2k | // to some backends having subtarget feature dependent module level |
43 | 42.2k | // code generation. This is similar to the hack in the AsmPrinter for |
44 | 42.2k | // module level assembly etc. |
45 | 42.2k | STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(), |
46 | 42.2k | getTargetFeatureString()); |
47 | 42.2k | |
48 | 42.2k | MCAsmInfo *TmpAsmInfo = |
49 | 42.2k | TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str()); |
50 | 42.2k | // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, |
51 | 42.2k | // and if the old one gets included then MCAsmInfo will be NULL and |
52 | 42.2k | // we'll crash later. |
53 | 42.2k | // Provide the user with a useful error message about what's wrong. |
54 | 42.2k | assert(TmpAsmInfo && "MCAsmInfo not initialized. " |
55 | 42.2k | "Make sure you include the correct TargetSelect.h" |
56 | 42.2k | "and that InitializeAllTargetMCs() is being invoked!"); |
57 | 42.2k | |
58 | 42.2k | if (Options.DisableIntegratedAS) |
59 | 184 | TmpAsmInfo->setUseIntegratedAssembler(false); |
60 | 42.2k | |
61 | 42.2k | TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments); |
62 | 42.2k | |
63 | 42.2k | TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections); |
64 | 42.2k | |
65 | 42.2k | TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations); |
66 | 42.2k | |
67 | 42.2k | if (Options.ExceptionModel != ExceptionHandling::None) |
68 | 796 | TmpAsmInfo->setExceptionsType(Options.ExceptionModel); |
69 | 42.2k | |
70 | 42.2k | AsmInfo = TmpAsmInfo; |
71 | 42.2k | } |
72 | | |
73 | | LLVMTargetMachine::LLVMTargetMachine(const Target &T, |
74 | | StringRef DataLayoutString, |
75 | | const Triple &TT, StringRef CPU, |
76 | | StringRef FS, const TargetOptions &Options, |
77 | | Reloc::Model RM, CodeModel::Model CM, |
78 | | CodeGenOpt::Level OL) |
79 | 42.2k | : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { |
80 | 42.2k | this->RM = RM; |
81 | 42.2k | this->CMModel = CM; |
82 | 42.2k | this->OptLevel = OL; |
83 | 42.2k | } |
84 | | |
85 | 365 | TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() { |
86 | 4.94k | return TargetIRAnalysis([this](const Function &F) { |
87 | 4.94k | return TargetTransformInfo(BasicTTIImpl(this, F)); |
88 | 4.94k | }); |
89 | 365 | } |
90 | | |
91 | | /// addPassesToX helper drives creation and initialization of TargetPassConfig. |
92 | | static MCContext * |
93 | | addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, |
94 | | bool DisableVerify, bool &WillCompleteCodeGenPipeline, |
95 | 33.5k | raw_pwrite_stream &Out, MachineModuleInfo *MMI) { |
96 | 33.5k | // Targets may override createPassConfig to provide a target-specific |
97 | 33.5k | // subclass. |
98 | 33.5k | TargetPassConfig *PassConfig = TM->createPassConfig(PM); |
99 | 33.5k | // Set PassConfig options provided by TargetMachine. |
100 | 33.5k | PassConfig->setDisableVerify(DisableVerify); |
101 | 33.5k | WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline(); |
102 | 33.5k | PM.add(PassConfig); |
103 | 33.5k | if (!MMI) |
104 | 17.5k | MMI = new MachineModuleInfo(TM); |
105 | 33.5k | PM.add(MMI); |
106 | 33.5k | |
107 | 33.5k | if (PassConfig->addISelPasses()) |
108 | 0 | return nullptr; |
109 | 33.5k | PassConfig->addMachinePasses(); |
110 | 33.5k | PassConfig->setInitialized(); |
111 | 33.5k | if (!WillCompleteCodeGenPipeline) |
112 | 94 | PM.add(createPrintMIRPass(Out)); |
113 | 33.5k | |
114 | 33.5k | return &MMI->getContext(); |
115 | 33.5k | } |
116 | | |
117 | | bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, |
118 | | raw_pwrite_stream &Out, CodeGenFileType FileType, |
119 | 33.1k | MCContext &Context) { |
120 | 33.1k | if (Options.MCOptions.MCSaveTempLabels) |
121 | 0 | Context.setAllowTemporaryLabels(false); |
122 | 33.1k | |
123 | 33.1k | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
124 | 33.1k | const MCAsmInfo &MAI = *getMCAsmInfo(); |
125 | 33.1k | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
126 | 33.1k | const MCInstrInfo &MII = *getMCInstrInfo(); |
127 | 33.1k | |
128 | 33.1k | std::unique_ptr<MCStreamer> AsmStreamer; |
129 | 33.1k | |
130 | 33.1k | switch (FileType) { |
131 | 15.3k | case CGFT_AssemblyFile: { |
132 | 15.3k | MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( |
133 | 15.3k | getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); |
134 | 15.3k | |
135 | 15.3k | // Create a code emitter if asked to show the encoding. |
136 | 15.3k | MCCodeEmitter *MCE = nullptr; |
137 | 15.3k | if (Options.MCOptions.ShowMCEncoding) |
138 | 176 | MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); |
139 | 15.3k | |
140 | 15.3k | MCAsmBackend *MAB = |
141 | 15.3k | getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, |
142 | 15.3k | Options.MCOptions); |
143 | 15.3k | auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); |
144 | 15.3k | MCStreamer *S = getTarget().createAsmStreamer( |
145 | 15.3k | Context, std::move(FOut), Options.MCOptions.AsmVerbose, |
146 | 15.3k | Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB, |
147 | 15.3k | Options.MCOptions.ShowMCInst); |
148 | 15.3k | AsmStreamer.reset(S); |
149 | 15.3k | break; |
150 | 33.1k | } |
151 | 17.7k | case CGFT_ObjectFile: { |
152 | 17.7k | // Create the code emitter for the target if it exists. If not, .o file |
153 | 17.7k | // emission fails. |
154 | 17.7k | MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); |
155 | 17.7k | MCAsmBackend *MAB = |
156 | 17.7k | getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, |
157 | 17.7k | Options.MCOptions); |
158 | 17.7k | if (!MCE || 17.7k !MAB17.7k ) |
159 | 0 | return true; |
160 | 17.7k | |
161 | 17.7k | // Don't waste memory on names of temp labels. |
162 | 17.7k | Context.setUseNamesOnTempLabels(false); |
163 | 17.7k | |
164 | 17.7k | Triple T(getTargetTriple().str()); |
165 | 17.7k | AsmStreamer.reset(getTarget().createMCObjectStreamer( |
166 | 17.7k | T, Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll, |
167 | 17.7k | Options.MCOptions.MCIncrementalLinkerCompatible, |
168 | 17.7k | /*DWARFMustBeAtTheEnd*/ true)); |
169 | 17.7k | break; |
170 | 17.7k | } |
171 | 12 | case CGFT_Null: |
172 | 12 | // The Null output is intended for use for performance analysis and testing, |
173 | 12 | // not real users. |
174 | 12 | AsmStreamer.reset(getTarget().createNullStreamer(Context)); |
175 | 12 | break; |
176 | 33.1k | } |
177 | 33.1k | |
178 | 33.1k | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
179 | 33.1k | FunctionPass *Printer = |
180 | 33.1k | getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); |
181 | 33.1k | if (!Printer) |
182 | 0 | return true; |
183 | 33.1k | |
184 | 33.1k | PM.add(Printer); |
185 | 33.1k | return false; |
186 | 33.1k | } |
187 | | |
188 | | bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
189 | | raw_pwrite_stream &Out, |
190 | | CodeGenFileType FileType, |
191 | | bool DisableVerify, |
192 | 33.2k | MachineModuleInfo *MMI) { |
193 | 33.2k | // Add common CodeGen passes. |
194 | 33.2k | bool WillCompleteCodeGenPipeline = true; |
195 | 33.2k | MCContext *Context = addPassesToGenerateCode( |
196 | 33.2k | this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI); |
197 | 33.2k | if (!Context) |
198 | 0 | return true; |
199 | 33.2k | |
200 | 33.2k | if (33.2k WillCompleteCodeGenPipeline && 33.2k addAsmPrinter(PM, Out, FileType, *Context)33.1k ) |
201 | 0 | return true; |
202 | 33.2k | |
203 | 33.2k | PM.add(createFreeMachineFunctionPass()); |
204 | 33.2k | return false; |
205 | 33.2k | } |
206 | | |
207 | | /// addPassesToEmitMC - Add passes to the specified pass manager to get |
208 | | /// machine code emitted with the MCJIT. This method returns true if machine |
209 | | /// code is not supported. It fills the MCContext Ctx pointer which can be |
210 | | /// used to build custom MCStreamer. |
211 | | /// |
212 | | bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, |
213 | | raw_pwrite_stream &Out, |
214 | 269 | bool DisableVerify) { |
215 | 269 | // Add common CodeGen passes. |
216 | 269 | bool WillCompleteCodeGenPipeline = true; |
217 | 269 | Ctx = addPassesToGenerateCode(this, PM, DisableVerify, |
218 | 269 | WillCompleteCodeGenPipeline, Out, |
219 | 269 | /*MachineModuleInfo*/ nullptr); |
220 | 269 | if (!Ctx) |
221 | 0 | return true; |
222 | 269 | assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered"); |
223 | 269 | |
224 | 269 | if (Options.MCOptions.MCSaveTempLabels) |
225 | 0 | Ctx->setAllowTemporaryLabels(false); |
226 | 269 | |
227 | 269 | // Create the code emitter for the target if it exists. If not, .o file |
228 | 269 | // emission fails. |
229 | 269 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
230 | 269 | MCCodeEmitter *MCE = |
231 | 269 | getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); |
232 | 269 | MCAsmBackend *MAB = |
233 | 269 | getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU, |
234 | 269 | Options.MCOptions); |
235 | 269 | if (!MCE || 269 !MAB269 ) |
236 | 0 | return true; |
237 | 269 | |
238 | 269 | const Triple &T = getTargetTriple(); |
239 | 269 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
240 | 269 | std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( |
241 | 269 | T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll, |
242 | 269 | Options.MCOptions.MCIncrementalLinkerCompatible, |
243 | 269 | /*DWARFMustBeAtTheEnd*/ true)); |
244 | 269 | |
245 | 269 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
246 | 269 | FunctionPass *Printer = |
247 | 269 | getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); |
248 | 269 | if (!Printer) |
249 | 0 | return true; |
250 | 269 | |
251 | 269 | PM.add(Printer); |
252 | 269 | PM.add(createFreeMachineFunctionPass()); |
253 | 269 | |
254 | 269 | return false; // success! |
255 | 269 | } |