blob: 5ef72d486e57d5eb2e24fa2086ca308b513ee75c [file] [log] [blame]
Daniel Dunbar897c6762010-06-07 23:20:08 +00001//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
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
Daniel Dunbar9b414d32010-06-15 17:48:49 +000010#include "clang/CodeGen/BackendUtil.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000011#include "clang/Basic/Diagnostic.h"
12#include "clang/Basic/TargetOptions.h"
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000013#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000014#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000015#include "clang/Frontend/FrontendDiagnostic.h"
16#include "llvm/Module.h"
17#include "llvm/PassManager.h"
Rafael Espindolacf565c52011-08-02 21:51:02 +000018#include "llvm/Analysis/Verifier.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000019#include "llvm/Assembly/PrintModulePass.h"
20#include "llvm/Bitcode/ReaderWriter.h"
21#include "llvm/CodeGen/RegAllocRegistry.h"
22#include "llvm/CodeGen/SchedulerRegistry.h"
Evan Cheng693769c2011-06-29 01:14:32 +000023#include "llvm/MC/SubtargetFeature.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/FormattedStream.h"
26#include "llvm/Support/PrettyStackTrace.h"
Evan Chenga6b40452011-08-24 18:09:14 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000028#include "llvm/Support/Timer.h"
29#include "llvm/Support/raw_ostream.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000030#include "llvm/Target/TargetData.h"
Rafael Espindolacf565c52011-08-02 21:51:02 +000031#include "llvm/Target/TargetLibraryInfo.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetOptions.h"
Nick Lewyckye8ba8d72011-04-21 23:44:07 +000034#include "llvm/Transforms/Instrumentation.h"
Rafael Espindolacf565c52011-08-02 21:51:02 +000035#include "llvm/Transforms/IPO.h"
36#include "llvm/Transforms/IPO/PassManagerBuilder.h"
37#include "llvm/Transforms/Scalar.h"
Daniel Dunbar897c6762010-06-07 23:20:08 +000038using namespace clang;
39using namespace llvm;
40
41namespace {
42
43class EmitAssemblyHelper {
David Blaikied6471f72011-09-25 23:23:43 +000044 DiagnosticsEngine &Diags;
Daniel Dunbar897c6762010-06-07 23:20:08 +000045 const CodeGenOptions &CodeGenOpts;
Nick Lewycky3aaeccc2011-12-02 22:17:00 +000046 const clang::TargetOptions &TargetOpts;
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000047 const LangOptions &LangOpts;
Daniel Dunbar897c6762010-06-07 23:20:08 +000048 Module *TheModule;
Daniel Dunbar897c6762010-06-07 23:20:08 +000049
50 Timer CodeGenerationTime;
51
Daniel Dunbardb2f2372010-09-17 07:35:16 +000052 mutable PassManager *CodeGenPasses;
Daniel Dunbar897c6762010-06-07 23:20:08 +000053 mutable PassManager *PerModulePasses;
54 mutable FunctionPassManager *PerFunctionPasses;
55
56private:
Daniel Dunbardb2f2372010-09-17 07:35:16 +000057 PassManager *getCodeGenPasses() const {
Daniel Dunbar897c6762010-06-07 23:20:08 +000058 if (!CodeGenPasses) {
Daniel Dunbardb2f2372010-09-17 07:35:16 +000059 CodeGenPasses = new PassManager();
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000060 CodeGenPasses->add(new TargetData(TheModule));
Daniel Dunbar897c6762010-06-07 23:20:08 +000061 }
62 return CodeGenPasses;
63 }
64
65 PassManager *getPerModulePasses() const {
66 if (!PerModulePasses) {
67 PerModulePasses = new PassManager();
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000068 PerModulePasses->add(new TargetData(TheModule));
Daniel Dunbar897c6762010-06-07 23:20:08 +000069 }
70 return PerModulePasses;
71 }
72
73 FunctionPassManager *getPerFunctionPasses() const {
74 if (!PerFunctionPasses) {
75 PerFunctionPasses = new FunctionPassManager(TheModule);
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000076 PerFunctionPasses->add(new TargetData(TheModule));
Daniel Dunbar897c6762010-06-07 23:20:08 +000077 }
78 return PerFunctionPasses;
79 }
80
81 void CreatePasses();
82
83 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
84 ///
85 /// \return True on success.
86 bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS);
87
88public:
David Blaikied6471f72011-09-25 23:23:43 +000089 EmitAssemblyHelper(DiagnosticsEngine &_Diags,
Nick Lewycky3aaeccc2011-12-02 22:17:00 +000090 const CodeGenOptions &CGOpts,
91 const clang::TargetOptions &TOpts,
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000092 const LangOptions &LOpts,
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000093 Module *M)
Dan Gohmanb18b8ad2011-07-05 22:02:36 +000094 : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +000095 TheModule(M), CodeGenerationTime("Code Generation Time"),
Daniel Dunbar897c6762010-06-07 23:20:08 +000096 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {}
97
98 ~EmitAssemblyHelper() {
99 delete CodeGenPasses;
100 delete PerModulePasses;
101 delete PerFunctionPasses;
102 }
103
104 void EmitAssembly(BackendAction Action, raw_ostream *OS);
105};
106
107}
108
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000109static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
110 if (Builder.OptLevel > 0)
111 PM.add(createObjCARCExpandPass());
112}
113
114static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
115 if (Builder.OptLevel > 0)
116 PM.add(createObjCARCOptPass());
117}
118
Kostya Serebryany1b4eca62011-11-16 17:34:26 +0000119static void addAddressSanitizerPass(const PassManagerBuilder &Builder,
120 PassManagerBase &PM) {
121 PM.add(createAddressSanitizerPass());
122}
123
Daniel Dunbar897c6762010-06-07 23:20:08 +0000124void EmitAssemblyHelper::CreatePasses() {
125 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
126 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
127
128 // Handle disabling of LLVM optimization, where we want to preserve the
129 // internal module before any optimization.
130 if (CodeGenOpts.DisableLLVMOpts) {
131 OptLevel = 0;
132 Inlining = CodeGenOpts.NoInlining;
133 }
Chris Lattner33c09d52011-05-21 20:40:11 +0000134
135 PassManagerBuilder PMBuilder;
Chris Lattner9ca02e52011-05-21 23:50:44 +0000136 PMBuilder.OptLevel = OptLevel;
137 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
Andrew Trick6445d622011-04-05 18:49:32 +0000138
Chris Lattner9ca02e52011-05-21 23:50:44 +0000139 PMBuilder.DisableSimplifyLibCalls = !CodeGenOpts.SimplifyLibCalls;
140 PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
141 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000142
143 // In ObjC ARC mode, add the main ARC optimization passes.
144 if (LangOpts.ObjCAutoRefCount) {
145 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
146 addObjCARCExpandPass);
147 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
148 addObjCARCOptPass);
149 }
Kostya Serebryany1b4eca62011-11-16 17:34:26 +0000150
Kostya Serebryanyb6196882011-11-22 01:28:36 +0000151 if (LangOpts.AddressSanitizer) {
Kostya Serebryany1b4eca62011-11-16 17:34:26 +0000152 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
153 addAddressSanitizerPass);
Kostya Serebryanye5dd2ea2011-11-30 22:20:21 +0000154 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
155 addAddressSanitizerPass);
Kostya Serebryany1b4eca62011-11-16 17:34:26 +0000156 }
Chris Lattner33c09d52011-05-21 20:40:11 +0000157
158 // Figure out TargetLibraryInfo.
Bill Wendling3621b312011-05-17 23:06:23 +0000159 Triple TargetTriple(TheModule->getTargetTriple());
Chris Lattner9ca02e52011-05-21 23:50:44 +0000160 PMBuilder.LibraryInfo = new TargetLibraryInfo(TargetTriple);
Chris Lattner98ec3f72011-02-18 22:34:47 +0000161 if (!CodeGenOpts.SimplifyLibCalls)
Chris Lattner9ca02e52011-05-21 23:50:44 +0000162 PMBuilder.LibraryInfo->disableAllFunctions();
Chris Lattner33c09d52011-05-21 20:40:11 +0000163
Daniel Dunbar897c6762010-06-07 23:20:08 +0000164 switch (Inlining) {
165 case CodeGenOptions::NoInlining: break;
166 case CodeGenOptions::NormalInlining: {
Daniel Dunbar897c6762010-06-07 23:20:08 +0000167 // FIXME: Derive these constants in a principled fashion.
168 unsigned Threshold = 225;
Chris Lattner33c09d52011-05-21 20:40:11 +0000169 if (CodeGenOpts.OptimizeSize == 1) // -Os
Daniel Dunbar897c6762010-06-07 23:20:08 +0000170 Threshold = 75;
Chris Lattner33c09d52011-05-21 20:40:11 +0000171 else if (CodeGenOpts.OptimizeSize == 2) // -Oz
Bob Wilsona0fa2032011-04-29 22:49:50 +0000172 Threshold = 25;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000173 else if (OptLevel > 2)
174 Threshold = 275;
Chris Lattner9ca02e52011-05-21 23:50:44 +0000175 PMBuilder.Inliner = createFunctionInliningPass(Threshold);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000176 break;
177 }
178 case CodeGenOptions::OnlyAlwaysInlining:
Chris Lattner33c09d52011-05-21 20:40:11 +0000179 // Respect always_inline.
Chris Lattner9ca02e52011-05-21 23:50:44 +0000180 PMBuilder.Inliner = createAlwaysInlinerPass();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000181 break;
182 }
183
Chris Lattner33c09d52011-05-21 20:40:11 +0000184
185 // Set up the per-function pass manager.
186 FunctionPassManager *FPM = getPerFunctionPasses();
187 if (CodeGenOpts.VerifyModule)
188 FPM->add(createVerifierPass());
189 PMBuilder.populateFunctionPassManager(*FPM);
Andrew Trick6445d622011-04-05 18:49:32 +0000190
Chris Lattner33c09d52011-05-21 20:40:11 +0000191 // Set up the per-module pass manager.
192 PassManager *MPM = getPerModulePasses();
Chris Lattnerdf619762011-02-18 22:20:38 +0000193
Nick Lewyckye8ba8d72011-04-21 23:44:07 +0000194 if (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) {
195 MPM->add(createGCOVProfilerPass(CodeGenOpts.EmitGcovNotes,
Bill Wendling3621b312011-05-17 23:06:23 +0000196 CodeGenOpts.EmitGcovArcs,
197 TargetTriple.isMacOSX()));
198
Nick Lewyckye8ba8d72011-04-21 23:44:07 +0000199 if (!CodeGenOpts.DebugInfo)
200 MPM->add(createStripSymbolsPass(true));
201 }
Chris Lattner33c09d52011-05-21 20:40:11 +0000202
203
204 PMBuilder.populateModulePassManager(*MPM);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000205}
206
207bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
208 formatted_raw_ostream &OS) {
209 // Create the TargetMachine for generating code.
210 std::string Error;
211 std::string Triple = TheModule->getTargetTriple();
212 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
213 if (!TheTarget) {
214 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
215 return false;
216 }
217
218 // FIXME: Expose these capabilities via actual APIs!!!! Aside from just
219 // being gross, this is also totally broken if we ever care about
220 // concurrency.
Daniel Dunbar1ad66482010-07-01 01:31:45 +0000221
Daniel Dunbar897c6762010-06-07 23:20:08 +0000222 TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose);
223
224 TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections);
225 TargetMachine::setDataSections (CodeGenOpts.DataSections);
226
227 // FIXME: Parse this earlier.
Benjamin Kramer77577ce2011-07-20 14:43:06 +0000228 llvm::CodeModel::Model CM;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000229 if (CodeGenOpts.CodeModel == "small") {
Benjamin Kramer77577ce2011-07-20 14:43:06 +0000230 CM = llvm::CodeModel::Small;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000231 } else if (CodeGenOpts.CodeModel == "kernel") {
Benjamin Kramer77577ce2011-07-20 14:43:06 +0000232 CM = llvm::CodeModel::Kernel;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000233 } else if (CodeGenOpts.CodeModel == "medium") {
Benjamin Kramer77577ce2011-07-20 14:43:06 +0000234 CM = llvm::CodeModel::Medium;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000235 } else if (CodeGenOpts.CodeModel == "large") {
Benjamin Kramer77577ce2011-07-20 14:43:06 +0000236 CM = llvm::CodeModel::Large;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000237 } else {
238 assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");
Benjamin Kramer77577ce2011-07-20 14:43:06 +0000239 CM = llvm::CodeModel::Default;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000240 }
241
242 std::vector<const char *> BackendArgs;
243 BackendArgs.push_back("clang"); // Fake program name.
244 if (!CodeGenOpts.DebugPass.empty()) {
245 BackendArgs.push_back("-debug-pass");
246 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
247 }
248 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
249 BackendArgs.push_back("-limit-float-precision");
250 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
251 }
252 if (llvm::TimePassesIsEnabled)
253 BackendArgs.push_back("-time-passes");
Daniel Dunbar3c66d302011-03-22 16:48:17 +0000254 for (unsigned i = 0, e = CodeGenOpts.BackendOptions.size(); i != e; ++i)
255 BackendArgs.push_back(CodeGenOpts.BackendOptions[i].c_str());
Chad Rosier1b906052011-08-26 00:26:29 +0000256 if (CodeGenOpts.NoGlobalMerge)
257 BackendArgs.push_back("-global-merge=false");
Daniel Dunbar897c6762010-06-07 23:20:08 +0000258 BackendArgs.push_back(0);
259 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
260 const_cast<char **>(&BackendArgs[0]));
261
262 std::string FeaturesStr;
Evan Cheng368691e2011-06-30 02:06:32 +0000263 if (TargetOpts.Features.size()) {
Daniel Dunbar897c6762010-06-07 23:20:08 +0000264 SubtargetFeatures Features;
Daniel Dunbar897c6762010-06-07 23:20:08 +0000265 for (std::vector<std::string>::const_iterator
266 it = TargetOpts.Features.begin(),
267 ie = TargetOpts.Features.end(); it != ie; ++it)
268 Features.AddFeature(*it);
269 FeaturesStr = Features.getString();
270 }
Evan Cheng2860e302011-07-19 06:37:41 +0000271
272 llvm::Reloc::Model RM = llvm::Reloc::Default;
273 if (CodeGenOpts.RelocationModel == "static") {
274 RM = llvm::Reloc::Static;
275 } else if (CodeGenOpts.RelocationModel == "pic") {
276 RM = llvm::Reloc::PIC_;
277 } else {
278 assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
279 "Invalid PIC model!");
280 RM = llvm::Reloc::DynamicNoPIC;
281 }
282
Evan Cheng9254bf72011-11-16 08:38:55 +0000283 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
284 switch (CodeGenOpts.OptimizationLevel) {
285 default: break;
286 case 0: OptLevel = CodeGenOpt::None; break;
287 case 3: OptLevel = CodeGenOpt::Aggressive; break;
288 }
289
Nick Lewycky3aaeccc2011-12-02 22:17:00 +0000290 llvm::TargetOptions Options;
291
292 // Set frame pointer elimination mode.
293 if (!CodeGenOpts.DisableFPElim) {
294 Options.NoFramePointerElim = false;
295 Options.NoFramePointerElimNonLeaf = false;
296 } else if (CodeGenOpts.OmitLeafFramePointer) {
297 Options.NoFramePointerElim = false;
298 Options.NoFramePointerElimNonLeaf = true;
299 } else {
300 Options.NoFramePointerElim = true;
301 Options.NoFramePointerElimNonLeaf = true;
302 }
303
304 // Set float ABI type.
305 if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp")
306 Options.FloatABIType = llvm::FloatABI::Soft;
307 else if (CodeGenOpts.FloatABI == "hard")
308 Options.FloatABIType = llvm::FloatABI::Hard;
309 else {
310 assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
311 Options.FloatABIType = llvm::FloatABI::Default;
312 }
313
314 Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
315 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
316 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
317 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
318 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
319 Options.UseSoftFloat = CodeGenOpts.SoftFloat;
Nick Lewycky4e785c92011-12-06 03:33:03 +0000320 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
321 Options.RealignStack = CodeGenOpts.StackRealignment;
Nick Lewycky3aaeccc2011-12-02 22:17:00 +0000322
Evan Cheng368691e2011-06-30 02:06:32 +0000323 TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
Nick Lewycky3aaeccc2011-12-02 22:17:00 +0000324 FeaturesStr, Options,
325 RM, CM, OptLevel);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000326
327 if (CodeGenOpts.RelaxAll)
328 TM->setMCRelaxAll(true);
Daniel Dunbar96932322011-03-28 22:49:28 +0000329 if (CodeGenOpts.SaveTempLabels)
330 TM->setMCSaveTempLabels(true);
Rafael Espindolaf24a1512011-04-30 18:35:43 +0000331 if (CodeGenOpts.NoDwarf2CFIAsm)
332 TM->setMCUseCFI(false);
Nick Lewyckyaaf2f362011-10-31 01:06:42 +0000333 if (!CodeGenOpts.NoDwarfDirectoryAsm)
334 TM->setMCUseDwarfDirectory(true);
Nick Lewyckyc3b90142011-06-21 00:14:18 +0000335 if (CodeGenOpts.NoExecStack)
336 TM->setMCNoExecStack(true);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000337
338 // Create the code generator passes.
Daniel Dunbardb2f2372010-09-17 07:35:16 +0000339 PassManager *PM = getCodeGenPasses();
Daniel Dunbar897c6762010-06-07 23:20:08 +0000340
341 // Normal mode, emit a .s or .o file by running the code generator. Note,
342 // this also adds codegenerator level optimization passes.
343 TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
344 if (Action == Backend_EmitObj)
345 CGFT = TargetMachine::CGFT_ObjectFile;
346 else if (Action == Backend_EmitMCNull)
347 CGFT = TargetMachine::CGFT_Null;
348 else
349 assert(Action == Backend_EmitAssembly && "Invalid action!");
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000350
351 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
352 // "codegen" passes so that it isn't run multiple times when there is
353 // inlining happening.
354 if (LangOpts.ObjCAutoRefCount)
355 PM->add(createObjCARCContractPass());
356
Evan Cheng9254bf72011-11-16 08:38:55 +0000357 if (TM->addPassesToEmitFile(*PM, OS, CGFT,
Daniel Dunbar897c6762010-06-07 23:20:08 +0000358 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
359 Diags.Report(diag::err_fe_unable_to_interface_with_target);
360 return false;
361 }
362
363 return true;
364}
365
366void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) {
367 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
368 llvm::formatted_raw_ostream FormattedOS;
369
370 CreatePasses();
371 switch (Action) {
372 case Backend_EmitNothing:
373 break;
374
375 case Backend_EmitBC:
376 getPerModulePasses()->add(createBitcodeWriterPass(*OS));
377 break;
378
379 case Backend_EmitLL:
380 FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
381 getPerModulePasses()->add(createPrintModulePass(&FormattedOS));
382 break;
383
384 default:
385 FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
386 if (!AddEmitPasses(Action, FormattedOS))
387 return;
388 }
389
Andrew Trick92b5d942011-04-05 18:56:55 +0000390 // Before executing passes, print the final values of the LLVM options.
391 cl::PrintOptionValues();
392
Daniel Dunbar897c6762010-06-07 23:20:08 +0000393 // Run passes. For now we do all passes at once, but eventually we
394 // would like to have the option of streaming code generation.
395
396 if (PerFunctionPasses) {
397 PrettyStackTraceString CrashInfo("Per-function optimization");
398
399 PerFunctionPasses->doInitialization();
400 for (Module::iterator I = TheModule->begin(),
401 E = TheModule->end(); I != E; ++I)
402 if (!I->isDeclaration())
403 PerFunctionPasses->run(*I);
404 PerFunctionPasses->doFinalization();
405 }
406
407 if (PerModulePasses) {
408 PrettyStackTraceString CrashInfo("Per-module optimization passes");
409 PerModulePasses->run(*TheModule);
410 }
411
412 if (CodeGenPasses) {
413 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbardb2f2372010-09-17 07:35:16 +0000414 CodeGenPasses->run(*TheModule);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000415 }
416}
417
David Blaikied6471f72011-09-25 23:23:43 +0000418void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
419 const CodeGenOptions &CGOpts,
Nick Lewycky3aaeccc2011-12-02 22:17:00 +0000420 const clang::TargetOptions &TOpts,
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000421 const LangOptions &LOpts,
422 Module *M,
Daniel Dunbar05a7f3d2010-06-07 23:21:04 +0000423 BackendAction Action, raw_ostream *OS) {
Dan Gohmanb18b8ad2011-07-05 22:02:36 +0000424 EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
Daniel Dunbar897c6762010-06-07 23:20:08 +0000425
426 AsmHelper.EmitAssembly(Action, OS);
427}