blob: 3fcf84cb7ab17673232c9791e48408f1d146ec45 [file] [log] [blame]
Daniel Dunbard69bacc2008-10-21 23:49:24 +00001//===--- Backend.cpp - Interface to LLVM backend technologies -------------===//
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#include "ASTConsumers.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000011#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000012#include "clang/Frontend/CompileOptions.h"
Chris Lattner682bf922009-03-29 16:50:03 +000013#include "clang/AST/ASTContext.h"
14#include "clang/AST/ASTConsumer.h"
15#include "clang/AST/DeclGroup.h"
16#include "clang/Basic/TargetInfo.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000017#include "llvm/Module.h"
18#include "llvm/ModuleProvider.h"
19#include "llvm/PassManager.h"
20#include "llvm/ADT/OwningPtr.h"
21#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar70f92432008-10-23 05:50:47 +000022#include "llvm/Analysis/CallGraph.h"
23#include "llvm/Analysis/Verifier.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000024#include "llvm/Bitcode/ReaderWriter.h"
25#include "llvm/CodeGen/RegAllocRegistry.h"
26#include "llvm/CodeGen/SchedulerRegistry.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000028#include "llvm/Support/Compiler.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000029#include "llvm/Support/Timer.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000030#include "llvm/System/Path.h"
31#include "llvm/System/Program.h"
Daniel Dunbara034ba82009-02-17 19:47:34 +000032#include "llvm/Target/SubtargetFeature.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000033#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetMachineRegistry.h"
Daniel Dunbar70f92432008-10-23 05:50:47 +000036#include "llvm/Transforms/Scalar.h"
37#include "llvm/Transforms/IPO.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000038using namespace clang;
39using namespace llvm;
40
41namespace {
Chris Lattner49f28ca2009-03-05 08:00:35 +000042 class VISIBILITY_HIDDEN BackendConsumer : public ASTConsumer {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000043 BackendAction Action;
Daniel Dunbar70f92432008-10-23 05:50:47 +000044 CompileOptions CompileOpts;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000045 const std::string &InputFile;
46 std::string OutputFile;
Chris Lattner49f28ca2009-03-05 08:00:35 +000047 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000048
Chris Lattner6f114eb2009-02-18 01:37:30 +000049 Timer LLVMIRGeneration;
50 Timer CodeGenerationTime;
51
Daniel Dunbard69bacc2008-10-21 23:49:24 +000052 llvm::OwningPtr<CodeGenerator> Gen;
53
54 llvm::Module *TheModule;
55 llvm::TargetData *TheTargetData;
56 llvm::raw_ostream *AsmOutStream;
57
Nuno Lopesdd492672008-10-24 22:51:00 +000058 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000059 mutable FunctionPassManager *CodeGenPasses;
60 mutable PassManager *PerModulePasses;
61 mutable FunctionPassManager *PerFunctionPasses;
62
63 FunctionPassManager *getCodeGenPasses() const;
64 PassManager *getPerModulePasses() const;
65 FunctionPassManager *getPerFunctionPasses() const;
66
67 void CreatePasses();
68
69 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM
70 /// IR.
71 ///
Daniel Dunbard69bacc2008-10-21 23:49:24 +000072 /// \return True on success. On failure \arg Error will be set to
73 /// a user readable error message.
Daniel Dunbar4c877cc2008-10-23 05:59:43 +000074 bool AddEmitPasses(std::string &Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000075
76 void EmitAssembly();
77
78 public:
79 BackendConsumer(BackendAction action, Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +000080 const LangOptions &langopts, const CompileOptions &compopts,
Chris Lattner20126042009-03-09 22:00:34 +000081 const std::string &infile, const std::string &outfile) :
Daniel Dunbard69bacc2008-10-21 23:49:24 +000082 Action(action),
Daniel Dunbar70f92432008-10-23 05:50:47 +000083 CompileOpts(compopts),
Daniel Dunbard69bacc2008-10-21 23:49:24 +000084 InputFile(infile),
85 OutputFile(outfile),
Chris Lattner6f114eb2009-02-18 01:37:30 +000086 LLVMIRGeneration("LLVM IR Generation Time"),
87 CodeGenerationTime("Code Generation Time"),
Chris Lattnerbd360642009-03-26 05:00:52 +000088 Gen(CreateLLVMCodeGen(Diags, InputFile, compopts)),
Nuno Lopesdd492672008-10-24 22:51:00 +000089 TheModule(0), TheTargetData(0), AsmOutStream(0), ModuleProvider(0),
Chris Lattner44502662009-02-18 01:23:44 +000090 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
91
Chris Lattner6f114eb2009-02-18 01:37:30 +000092 // Enable -time-passes if -ftime-report is enabled.
Chris Lattner44502662009-02-18 01:23:44 +000093 llvm::TimePassesIsEnabled = CompileOpts.TimePasses;
94 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000095
96 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000097 delete AsmOutStream;
98 delete TheTargetData;
Nuno Lopesdd492672008-10-24 22:51:00 +000099 delete ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000100 delete CodeGenPasses;
101 delete PerModulePasses;
102 delete PerFunctionPasses;
103 }
104
Chris Lattner7bb0da02009-03-28 02:18:25 +0000105 virtual void Initialize(ASTContext &Ctx) {
106 Context = &Ctx;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000107
108 if (CompileOpts.TimePasses)
109 LLVMIRGeneration.startTimer();
110
Chris Lattner7bb0da02009-03-28 02:18:25 +0000111 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000112
113 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000114 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattner7bb0da02009-03-28 02:18:25 +0000115 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Chris Lattner6f114eb2009-02-18 01:37:30 +0000116
117 if (CompileOpts.TimePasses)
118 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000119 }
120
Chris Lattner682bf922009-03-29 16:50:03 +0000121 virtual void HandleTopLevelDecl(DeclGroupRef D) {
122 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +0000123 Context->getSourceManager(),
124 "LLVM IR generation of declaration");
Chris Lattner682bf922009-03-29 16:50:03 +0000125
Chris Lattner6f114eb2009-02-18 01:37:30 +0000126 if (CompileOpts.TimePasses)
127 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000128
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000129 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000130
131 if (CompileOpts.TimePasses)
132 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000133 }
134
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000135 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000136 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000137 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000138 if (CompileOpts.TimePasses)
139 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000140
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000141 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000142
Chris Lattner49f28ca2009-03-05 08:00:35 +0000143 if (CompileOpts.TimePasses)
144 LLVMIRGeneration.stopTimer();
145 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000146
Chris Lattner49f28ca2009-03-05 08:00:35 +0000147 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000148 EmitAssembly();
149
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000150 // Force a flush here in case we never get released.
151 if (AsmOutStream)
152 AsmOutStream->flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000153 }
154
155 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000156 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
157 Context->getSourceManager(),
158 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000159 Gen->HandleTagDeclDefinition(D);
160 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000161
162 virtual void CompleteTentativeDefinition(VarDecl *D) {
163 Gen->CompleteTentativeDefinition(D);
164 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000165 };
166}
167
168FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
169 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000170 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000171 CodeGenPasses->add(new TargetData(*TheTargetData));
172 }
173
174 return CodeGenPasses;
175}
176
177PassManager *BackendConsumer::getPerModulePasses() const {
178 if (!PerModulePasses) {
179 PerModulePasses = new PassManager();
180 PerModulePasses->add(new TargetData(*TheTargetData));
181 }
182
183 return PerModulePasses;
184}
185
186FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
187 if (!PerFunctionPasses) {
Nuno Lopes7d43a312008-10-24 23:27:18 +0000188 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000189 PerFunctionPasses->add(new TargetData(*TheTargetData));
190 }
191
192 return PerFunctionPasses;
193}
194
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000195bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000196 if (Action == Backend_EmitNothing)
197 return true;
198
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000199 if (OutputFile == "-" || (InputFile == "-" && OutputFile.empty())) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000200 AsmOutStream = new raw_stdout_ostream();
201 sys::Program::ChangeStdoutToBinary();
202 } else {
203 if (OutputFile.empty()) {
204 llvm::sys::Path Path(InputFile);
205 Path.eraseSuffix();
206 if (Action == Backend_EmitBC) {
207 Path.appendSuffix("bc");
208 } else if (Action == Backend_EmitLL) {
209 Path.appendSuffix("ll");
210 } else {
211 Path.appendSuffix("s");
212 }
213 OutputFile = Path.toString();
214 }
215
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000216 AsmOutStream = new raw_fd_ostream(OutputFile.c_str(), true, Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000217 if (!Error.empty())
218 return false;
219 }
220
221 if (Action == Backend_EmitBC) {
Daniel Dunbared2cb282008-10-22 17:40:45 +0000222 getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000223 } else if (Action == Backend_EmitLL) {
Daniel Dunbar11292b02008-10-22 03:28:13 +0000224 getPerModulePasses()->add(createPrintModulePass(AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000225 } else {
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000226 bool Fast = CompileOpts.OptimizationLevel == 0;
227
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000228 // Create the TargetMachine for generating code.
229 const TargetMachineRegistry::entry *TME =
230 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error);
231 if (!TME) {
232 Error = std::string("Unable to get target machine: ") + Error;
233 return false;
234 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000235
236 std::string FeaturesStr;
237 if (CompileOpts.CPU.size() || CompileOpts.Features.size()) {
238 SubtargetFeatures Features;
239 Features.setCPU(CompileOpts.CPU);
240 for (std::vector<std::string>::iterator
241 it = CompileOpts.Features.begin(),
242 ie = CompileOpts.Features.end(); it != ie; ++it)
243 Features.AddFeature(*it);
244 FeaturesStr = Features.getString();
245 }
246 TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000247
248 // Set register scheduler & allocation policy.
249 RegisterScheduler::setDefault(createDefaultScheduler);
250 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
251 createLinearScanRegisterAllocator);
252
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000253 // From llvm-gcc:
254 // If there are passes we have to run on the entire module, we do codegen
255 // as a separate "pass" after that happens.
256 // FIXME: This is disabled right now until bugs can be worked out. Reenable
257 // this for fast -O0 compiles!
258 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000259 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
260
261 switch (CompileOpts.OptimizationLevel) {
262 default: break;
263 case 0: OptLevel = CodeGenOpt::None; break;
264 case 1: OptLevel = CodeGenOpt::One; break;
265 case 2: OptLevel = CodeGenOpt::Two; break;
266 case 3: OptLevel = CodeGenOpt::Aggressive; break;
267 }
268
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000269 // Normal mode, emit a .s file by running the code generator.
270 // Note, this also adds codegenerator level optimization passes.
271 switch (TM->addPassesToEmitFile(*PM, *AsmOutStream,
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000272 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000273 default:
274 case FileModel::Error:
275 Error = "Unable to interface with target machine!\n";
276 return false;
277 case FileModel::AsmFile:
278 break;
279 }
280
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000281 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, 0, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000282 Error = "Unable to interface with target machine!\n";
283 return false;
284 }
285 }
286
287 return true;
288}
289
290void BackendConsumer::CreatePasses() {
Daniel Dunbar70f92432008-10-23 05:50:47 +0000291 // In -O0 if checking is disabled, we don't even have per-function passes.
292 if (CompileOpts.VerifyModule)
293 getPerFunctionPasses()->add(createVerifierPass());
294
295 if (CompileOpts.OptimizationLevel > 0) {
296 FunctionPassManager *PM = getPerFunctionPasses();
297 PM->add(createCFGSimplificationPass());
298 if (CompileOpts.OptimizationLevel == 1)
299 PM->add(createPromoteMemoryToRegisterPass());
300 else
301 PM->add(createScalarReplAggregatesPass());
302 PM->add(createInstructionCombiningPass());
303 }
304
305 // For now we always create per module passes.
306 PassManager *PM = getPerModulePasses();
307 if (CompileOpts.OptimizationLevel > 0) {
308 if (CompileOpts.UnitAtATime)
309 PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
310 PM->add(createCFGSimplificationPass()); // Clean up disgusting code
311 PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
312 if (CompileOpts.UnitAtATime) {
313 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
314 PM->add(createGlobalDCEPass()); // Remove unused fns and globs
315 PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
316 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
317 }
318 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
319 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
320 if (CompileOpts.UnitAtATime) {
321 PM->add(createPruneEHPass()); // Remove dead EH info
Bill Wendling5c5a7ee2008-12-31 19:51:31 +0000322 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbar70f92432008-10-23 05:50:47 +0000323 }
324 if (CompileOpts.InlineFunctions)
325 PM->add(createFunctionInliningPass()); // Inline small functions
326 else
327 PM->add(createAlwaysInlinerPass()); // Respect always_inline
328 if (CompileOpts.OptimizationLevel > 2)
329 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
330 if (CompileOpts.SimplifyLibCalls)
331 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
332 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
333 PM->add(createJumpThreadingPass()); // Thread jumps.
334 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
335 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
336 PM->add(createInstructionCombiningPass()); // Combine silly seq's
337 PM->add(createCondPropagationPass()); // Propagate conditionals
338 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
339 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
340 PM->add(createReassociatePass()); // Reassociate expressions
341 PM->add(createLoopRotatePass()); // Rotate Loop
342 PM->add(createLICMPass()); // Hoist loop invariants
343 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel59db7602008-11-26 05:01:52 +0000344// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbar70f92432008-10-23 05:50:47 +0000345 PM->add(createInstructionCombiningPass());
346 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
347 PM->add(createLoopDeletionPass()); // Delete dead loops
348 if (CompileOpts.UnrollLoops)
349 PM->add(createLoopUnrollPass()); // Unroll small loops
350 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
351 PM->add(createGVNPass()); // Remove redundancies
352 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
353 PM->add(createSCCPPass()); // Constant prop with SCCP
354
355 // Run instcombine after redundancy elimination to exploit opportunities
356 // opened up by them.
357 PM->add(createInstructionCombiningPass());
358 PM->add(createCondPropagationPass()); // Propagate conditionals
359 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
360 PM->add(createAggressiveDCEPass()); // Delete dead instructions
361 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
362
363 if (CompileOpts.UnitAtATime) {
364 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
365 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
366 }
367
368 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
369 PM->add(createConstantMergePass()); // Merge dup global constants
370 } else {
Daniel Dunbarb087ae92008-11-13 05:29:02 +0000371 PM->add(createAlwaysInlinerPass());
Daniel Dunbar70f92432008-10-23 05:50:47 +0000372 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000373}
374
375/// EmitAssembly - Handle interaction with LLVM backend to generate
376/// actual machine code.
377void BackendConsumer::EmitAssembly() {
378 // Silently ignore if we weren't initialized for some reason.
379 if (!TheModule || !TheTargetData)
380 return;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000381
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000382
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000383 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000384
Daniel Dunbard611bac2008-10-27 20:40:41 +0000385 // Make sure IR generation is happy with the module. This is
386 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000387 Module *M = Gen->ReleaseModule();
388 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000389 // The module has been released by IR gen on failures, do not
390 // double free.
391 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000392 TheModule = 0;
393 return;
394 }
395
396 assert(TheModule == M && "Unexpected module change during IR generation");
397
398 CreatePasses();
399
400 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000401 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000402 // FIXME: Don't fail this way.
403 llvm::cerr << "ERROR: " << Error << "\n";
404 ::exit(1);
405 }
406
407 // Run passes. For now we do all passes at once, but eventually we
408 // would like to have the option of streaming code generation.
409
410 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000411 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000412
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000413 PerFunctionPasses->doInitialization();
414 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
415 if (!I->isDeclaration())
416 PerFunctionPasses->run(*I);
417 PerFunctionPasses->doFinalization();
418 }
419
Chris Lattner49f28ca2009-03-05 08:00:35 +0000420 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000421 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000422 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000423 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000424
425 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000426 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000427 CodeGenPasses->doInitialization();
428 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
429 if (!I->isDeclaration())
430 CodeGenPasses->run(*I);
431 CodeGenPasses->doFinalization();
432 }
433}
434
435ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
436 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000437 const LangOptions &LangOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000438 const CompileOptions &CompileOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000439 const std::string& InFile,
Chris Lattner20126042009-03-09 22:00:34 +0000440 const std::string& OutFile) {
Daniel Dunbara034ba82009-02-17 19:47:34 +0000441 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts,
Chris Lattner20126042009-03-09 22:00:34 +0000442 InFile, OutFile);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000443}