blob: 697ba941af231a2d8b5fccdf982eadd31291d8cd [file] [log] [blame]
Daniel Dunbar85e44e22008-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
Eli Friedman22a4efd2009-05-18 22:50:54 +000010#include "clang/Frontend/ASTConsumers.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000011#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbar68952de2009-03-02 06:16:29 +000012#include "clang/Frontend/CompileOptions.h"
Chris Lattnera17991f2009-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 Dunbar85e44e22008-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 Dunbaraa7a0662008-10-23 05:50:47 +000022#include "llvm/Analysis/CallGraph.h"
23#include "llvm/Analysis/Verifier.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000024#include "llvm/Bitcode/ReaderWriter.h"
25#include "llvm/CodeGen/RegAllocRegistry.h"
26#include "llvm/CodeGen/SchedulerRegistry.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000027#include "llvm/Support/raw_ostream.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000028#include "llvm/Support/Compiler.h"
Chris Lattner2193db22009-02-18 01:37:30 +000029#include "llvm/Support/Timer.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000030#include "llvm/System/Path.h"
31#include "llvm/System/Program.h"
Daniel Dunbar9101a632009-02-17 19:47:34 +000032#include "llvm/Target/SubtargetFeature.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000033#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetMachineRegistry.h"
Daniel Dunbaraa7a0662008-10-23 05:50:47 +000036#include "llvm/Transforms/Scalar.h"
37#include "llvm/Transforms/IPO.h"
Daniel Dunbar85e44e22008-10-21 23:49:24 +000038using namespace clang;
39using namespace llvm;
40
41namespace {
Chris Lattnerc309ade2009-03-05 08:00:35 +000042 class VISIBILITY_HIDDEN BackendConsumer : public ASTConsumer {
Daniel Dunbar85e44e22008-10-21 23:49:24 +000043 BackendAction Action;
Daniel Dunbaraa7a0662008-10-23 05:50:47 +000044 CompileOptions CompileOpts;
Eli Friedmana9c310842009-05-18 22:20:00 +000045 llvm::raw_ostream *AsmOutStream;
Chris Lattnerc309ade2009-03-05 08:00:35 +000046 ASTContext *Context;
Daniel Dunbar1a27a192008-10-29 08:50:02 +000047
Chris Lattner2193db22009-02-18 01:37:30 +000048 Timer LLVMIRGeneration;
49 Timer CodeGenerationTime;
50
Daniel Dunbar85e44e22008-10-21 23:49:24 +000051 llvm::OwningPtr<CodeGenerator> Gen;
52
53 llvm::Module *TheModule;
54 llvm::TargetData *TheTargetData;
Daniel Dunbar85e44e22008-10-21 23:49:24 +000055
Nuno Lopes74d92782008-10-24 22:51:00 +000056 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbar85e44e22008-10-21 23:49:24 +000057 mutable FunctionPassManager *CodeGenPasses;
58 mutable PassManager *PerModulePasses;
59 mutable FunctionPassManager *PerFunctionPasses;
60
61 FunctionPassManager *getCodeGenPasses() const;
62 PassManager *getPerModulePasses() const;
63 FunctionPassManager *getPerFunctionPasses() const;
64
65 void CreatePasses();
66
67 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM
68 /// IR.
69 ///
Daniel Dunbar85e44e22008-10-21 23:49:24 +000070 /// \return True on success. On failure \arg Error will be set to
71 /// a user readable error message.
Daniel Dunbar655d5092008-10-23 05:59:43 +000072 bool AddEmitPasses(std::string &Error);
Daniel Dunbar85e44e22008-10-21 23:49:24 +000073
74 void EmitAssembly();
75
76 public:
77 BackendConsumer(BackendAction action, Diagnostic &Diags,
Daniel Dunbar9101a632009-02-17 19:47:34 +000078 const LangOptions &langopts, const CompileOptions &compopts,
Eli Friedmana9c310842009-05-18 22:20:00 +000079 const std::string &infile, llvm::raw_ostream* OS) :
Daniel Dunbar85e44e22008-10-21 23:49:24 +000080 Action(action),
Daniel Dunbaraa7a0662008-10-23 05:50:47 +000081 CompileOpts(compopts),
Eli Friedmana9c310842009-05-18 22:20:00 +000082 AsmOutStream(OS),
Chris Lattner2193db22009-02-18 01:37:30 +000083 LLVMIRGeneration("LLVM IR Generation Time"),
84 CodeGenerationTime("Code Generation Time"),
Eli Friedmana9c310842009-05-18 22:20:00 +000085 Gen(CreateLLVMCodeGen(Diags, infile, compopts)),
86 TheModule(0), TheTargetData(0), ModuleProvider(0),
Chris Lattnere8f70712009-02-18 01:23:44 +000087 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
88
Chris Lattner2193db22009-02-18 01:37:30 +000089 // Enable -time-passes if -ftime-report is enabled.
Chris Lattnere8f70712009-02-18 01:23:44 +000090 llvm::TimePassesIsEnabled = CompileOpts.TimePasses;
91 }
Daniel Dunbar85e44e22008-10-21 23:49:24 +000092
93 ~BackendConsumer() {
Daniel Dunbar85e44e22008-10-21 23:49:24 +000094 delete TheTargetData;
Nuno Lopes74d92782008-10-24 22:51:00 +000095 delete ModuleProvider;
Daniel Dunbar85e44e22008-10-21 23:49:24 +000096 delete CodeGenPasses;
97 delete PerModulePasses;
98 delete PerFunctionPasses;
99 }
100
Chris Lattnerfb88a5f2009-03-28 02:18:25 +0000101 virtual void Initialize(ASTContext &Ctx) {
102 Context = &Ctx;
Chris Lattner2193db22009-02-18 01:37:30 +0000103
104 if (CompileOpts.TimePasses)
105 LLVMIRGeneration.startTimer();
106
Chris Lattnerfb88a5f2009-03-28 02:18:25 +0000107 Gen->Initialize(Ctx);
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000108
109 TheModule = Gen->GetModule();
Nuno Lopesd41e2b12008-10-24 23:27:18 +0000110 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattnerfb88a5f2009-03-28 02:18:25 +0000111 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Chris Lattner2193db22009-02-18 01:37:30 +0000112
113 if (CompileOpts.TimePasses)
114 LLVMIRGeneration.stopTimer();
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000115 }
116
Chris Lattnera17991f2009-03-29 16:50:03 +0000117 virtual void HandleTopLevelDecl(DeclGroupRef D) {
118 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattnerc309ade2009-03-05 08:00:35 +0000119 Context->getSourceManager(),
120 "LLVM IR generation of declaration");
Chris Lattnera17991f2009-03-29 16:50:03 +0000121
Chris Lattner2193db22009-02-18 01:37:30 +0000122 if (CompileOpts.TimePasses)
123 LLVMIRGeneration.startTimer();
Chris Lattnera17991f2009-03-29 16:50:03 +0000124
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000125 Gen->HandleTopLevelDecl(D);
Chris Lattner2193db22009-02-18 01:37:30 +0000126
127 if (CompileOpts.TimePasses)
128 LLVMIRGeneration.stopTimer();
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000129 }
130
Chris Lattner2a594d02009-03-28 04:11:33 +0000131 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattnerc309ade2009-03-05 08:00:35 +0000132 {
Chris Lattner4e164172009-03-06 06:46:31 +0000133 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chris Lattnerc309ade2009-03-05 08:00:35 +0000134 if (CompileOpts.TimePasses)
135 LLVMIRGeneration.startTimer();
Chris Lattner2193db22009-02-18 01:37:30 +0000136
Chris Lattner2a594d02009-03-28 04:11:33 +0000137 Gen->HandleTranslationUnit(C);
Daniel Dunbar622d6d02008-11-11 06:35:39 +0000138
Chris Lattnerc309ade2009-03-05 08:00:35 +0000139 if (CompileOpts.TimePasses)
140 LLVMIRGeneration.stopTimer();
141 }
Chris Lattner2193db22009-02-18 01:37:30 +0000142
Chris Lattnerc309ade2009-03-05 08:00:35 +0000143 // EmitAssembly times and registers crash info itself.
Chris Lattner2193db22009-02-18 01:37:30 +0000144 EmitAssembly();
145
Daniel Dunbar622d6d02008-11-11 06:35:39 +0000146 // Force a flush here in case we never get released.
147 if (AsmOutStream)
148 AsmOutStream->flush();
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000149 }
150
151 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattnerc309ade2009-03-05 08:00:35 +0000152 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
153 Context->getSourceManager(),
154 "LLVM IR generation of declaration");
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000155 Gen->HandleTagDeclDefinition(D);
156 }
Douglas Gregor9cdb4a12009-04-21 17:11:58 +0000157
158 virtual void CompleteTentativeDefinition(VarDecl *D) {
159 Gen->CompleteTentativeDefinition(D);
160 }
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000161 };
162}
163
164FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
165 if (!CodeGenPasses) {
Nuno Lopes74d92782008-10-24 22:51:00 +0000166 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000167 CodeGenPasses->add(new TargetData(*TheTargetData));
168 }
169
170 return CodeGenPasses;
171}
172
173PassManager *BackendConsumer::getPerModulePasses() const {
174 if (!PerModulePasses) {
175 PerModulePasses = new PassManager();
176 PerModulePasses->add(new TargetData(*TheTargetData));
177 }
178
179 return PerModulePasses;
180}
181
182FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
183 if (!PerFunctionPasses) {
Nuno Lopesd41e2b12008-10-24 23:27:18 +0000184 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000185 PerFunctionPasses->add(new TargetData(*TheTargetData));
186 }
187
188 return PerFunctionPasses;
189}
190
Daniel Dunbar655d5092008-10-23 05:59:43 +0000191bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbard999a8e2009-02-26 22:39:37 +0000192 if (Action == Backend_EmitNothing)
193 return true;
194
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000195 if (Action == Backend_EmitBC) {
Daniel Dunbare5dbb072008-10-22 17:40:45 +0000196 getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream));
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000197 } else if (Action == Backend_EmitLL) {
Daniel Dunbar092a7442008-10-22 03:28:13 +0000198 getPerModulePasses()->add(createPrintModulePass(AsmOutStream));
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000199 } else {
Daniel Dunbar655d5092008-10-23 05:59:43 +0000200 bool Fast = CompileOpts.OptimizationLevel == 0;
201
Daniel Dunbaraed93f22008-10-22 18:29:51 +0000202 // Create the TargetMachine for generating code.
203 const TargetMachineRegistry::entry *TME =
204 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error);
205 if (!TME) {
206 Error = std::string("Unable to get target machine: ") + Error;
207 return false;
208 }
Daniel Dunbar9101a632009-02-17 19:47:34 +0000209
210 std::string FeaturesStr;
211 if (CompileOpts.CPU.size() || CompileOpts.Features.size()) {
212 SubtargetFeatures Features;
213 Features.setCPU(CompileOpts.CPU);
214 for (std::vector<std::string>::iterator
215 it = CompileOpts.Features.begin(),
216 ie = CompileOpts.Features.end(); it != ie; ++it)
217 Features.AddFeature(*it);
218 FeaturesStr = Features.getString();
219 }
220 TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr);
Daniel Dunbaraed93f22008-10-22 18:29:51 +0000221
222 // Set register scheduler & allocation policy.
223 RegisterScheduler::setDefault(createDefaultScheduler);
224 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
225 createLinearScanRegisterAllocator);
226
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000227 // From llvm-gcc:
228 // If there are passes we have to run on the entire module, we do codegen
229 // as a separate "pass" after that happens.
230 // FIXME: This is disabled right now until bugs can be worked out. Reenable
231 // this for fast -O0 compiles!
232 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling110ea4e2009-04-29 23:53:23 +0000233 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
234
235 switch (CompileOpts.OptimizationLevel) {
236 default: break;
237 case 0: OptLevel = CodeGenOpt::None; break;
Bill Wendling110ea4e2009-04-29 23:53:23 +0000238 case 3: OptLevel = CodeGenOpt::Aggressive; break;
239 }
240
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000241 // Normal mode, emit a .s file by running the code generator.
242 // Note, this also adds codegenerator level optimization passes.
243 switch (TM->addPassesToEmitFile(*PM, *AsmOutStream,
Bill Wendling110ea4e2009-04-29 23:53:23 +0000244 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000245 default:
246 case FileModel::Error:
247 Error = "Unable to interface with target machine!\n";
248 return false;
249 case FileModel::AsmFile:
250 break;
251 }
252
Duncan Sands61f1d9c2009-05-31 04:09:57 +0000253 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
254 OptLevel)) {
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000255 Error = "Unable to interface with target machine!\n";
256 return false;
257 }
258 }
259
260 return true;
261}
262
263void BackendConsumer::CreatePasses() {
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000264 // In -O0 if checking is disabled, we don't even have per-function passes.
265 if (CompileOpts.VerifyModule)
266 getPerFunctionPasses()->add(createVerifierPass());
267
268 if (CompileOpts.OptimizationLevel > 0) {
269 FunctionPassManager *PM = getPerFunctionPasses();
270 PM->add(createCFGSimplificationPass());
271 if (CompileOpts.OptimizationLevel == 1)
272 PM->add(createPromoteMemoryToRegisterPass());
273 else
274 PM->add(createScalarReplAggregatesPass());
275 PM->add(createInstructionCombiningPass());
276 }
277
278 // For now we always create per module passes.
279 PassManager *PM = getPerModulePasses();
280 if (CompileOpts.OptimizationLevel > 0) {
281 if (CompileOpts.UnitAtATime)
282 PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
283 PM->add(createCFGSimplificationPass()); // Clean up disgusting code
284 PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
285 if (CompileOpts.UnitAtATime) {
286 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
287 PM->add(createGlobalDCEPass()); // Remove unused fns and globs
288 PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
289 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
290 }
291 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
292 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
293 if (CompileOpts.UnitAtATime) {
294 PM->add(createPruneEHPass()); // Remove dead EH info
Bill Wendlingaccd9b72008-12-31 19:51:31 +0000295 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000296 }
Daniel Dunbard27b66b2009-06-02 22:07:45 +0000297 switch (CompileOpts.Inlining) {
298 case CompileOptions::NoInlining:
299 break;
300 case CompileOptions::NormalInlining:
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000301 PM->add(createFunctionInliningPass()); // Inline small functions
Daniel Dunbard27b66b2009-06-02 22:07:45 +0000302 break;
303 case CompileOptions::OnlyAlwaysInlining:
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000304 PM->add(createAlwaysInlinerPass()); // Respect always_inline
Daniel Dunbard27b66b2009-06-02 22:07:45 +0000305 break;
306 }
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000307 if (CompileOpts.OptimizationLevel > 2)
308 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
309 if (CompileOpts.SimplifyLibCalls)
310 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
311 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
312 PM->add(createJumpThreadingPass()); // Thread jumps.
313 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
314 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
315 PM->add(createInstructionCombiningPass()); // Combine silly seq's
316 PM->add(createCondPropagationPass()); // Propagate conditionals
317 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
318 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
319 PM->add(createReassociatePass()); // Reassociate expressions
320 PM->add(createLoopRotatePass()); // Rotate Loop
321 PM->add(createLICMPass()); // Hoist loop invariants
322 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel62237e72008-11-26 05:01:52 +0000323// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000324 PM->add(createInstructionCombiningPass());
325 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
326 PM->add(createLoopDeletionPass()); // Delete dead loops
327 if (CompileOpts.UnrollLoops)
328 PM->add(createLoopUnrollPass()); // Unroll small loops
329 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
330 PM->add(createGVNPass()); // Remove redundancies
331 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
332 PM->add(createSCCPPass()); // Constant prop with SCCP
333
334 // Run instcombine after redundancy elimination to exploit opportunities
335 // opened up by them.
336 PM->add(createInstructionCombiningPass());
337 PM->add(createCondPropagationPass()); // Propagate conditionals
338 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
339 PM->add(createAggressiveDCEPass()); // Delete dead instructions
340 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
341
342 if (CompileOpts.UnitAtATime) {
343 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
344 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
345 }
346
347 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
348 PM->add(createConstantMergePass()); // Merge dup global constants
349 } else {
Daniel Dunbard27b66b2009-06-02 22:07:45 +0000350 switch (CompileOpts.Inlining) {
351 case CompileOptions::NoInlining:
352 break;
353 case CompileOptions::NormalInlining:
354 PM->add(createFunctionInliningPass()); // Inline small functions
355 break;
356 case CompileOptions::OnlyAlwaysInlining:
357 PM->add(createAlwaysInlinerPass()); // Respect always_inline
358 break;
359 }
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000360 }
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000361}
362
363/// EmitAssembly - Handle interaction with LLVM backend to generate
364/// actual machine code.
365void BackendConsumer::EmitAssembly() {
366 // Silently ignore if we weren't initialized for some reason.
367 if (!TheModule || !TheTargetData)
368 return;
Chris Lattner2193db22009-02-18 01:37:30 +0000369
Chris Lattner908789c2009-02-18 18:22:50 +0000370
Chris Lattner908789c2009-02-18 18:22:50 +0000371 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000372
Daniel Dunbarbc147792008-10-27 20:40:41 +0000373 // Make sure IR generation is happy with the module. This is
374 // released by the module provider.
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000375 Module *M = Gen->ReleaseModule();
376 if (!M) {
Daniel Dunbarbc147792008-10-27 20:40:41 +0000377 // The module has been released by IR gen on failures, do not
378 // double free.
379 ModuleProvider->releaseModule();
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000380 TheModule = 0;
381 return;
382 }
383
384 assert(TheModule == M && "Unexpected module change during IR generation");
385
386 CreatePasses();
387
388 std::string Error;
Daniel Dunbar655d5092008-10-23 05:59:43 +0000389 if (!AddEmitPasses(Error)) {
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000390 // FIXME: Don't fail this way.
391 llvm::cerr << "ERROR: " << Error << "\n";
392 ::exit(1);
393 }
394
395 // Run passes. For now we do all passes at once, but eventually we
396 // would like to have the option of streaming code generation.
397
398 if (PerFunctionPasses) {
Chris Lattner4e164172009-03-06 06:46:31 +0000399 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattnerc309ade2009-03-05 08:00:35 +0000400
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000401 PerFunctionPasses->doInitialization();
402 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
403 if (!I->isDeclaration())
404 PerFunctionPasses->run(*I);
405 PerFunctionPasses->doFinalization();
406 }
407
Chris Lattnerc309ade2009-03-05 08:00:35 +0000408 if (PerModulePasses) {
Chris Lattner4e164172009-03-06 06:46:31 +0000409 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000410 PerModulePasses->run(*M);
Chris Lattnerc309ade2009-03-05 08:00:35 +0000411 }
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000412
413 if (CodeGenPasses) {
Chris Lattner4e164172009-03-06 06:46:31 +0000414 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000415 CodeGenPasses->doInitialization();
416 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
417 if (!I->isDeclaration())
418 CodeGenPasses->run(*I);
419 CodeGenPasses->doFinalization();
420 }
421}
422
423ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
424 Diagnostic &Diags,
Daniel Dunbar9101a632009-02-17 19:47:34 +0000425 const LangOptions &LangOpts,
Daniel Dunbaraa7a0662008-10-23 05:50:47 +0000426 const CompileOptions &CompileOpts,
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000427 const std::string& InFile,
Eli Friedmana9c310842009-05-18 22:20:00 +0000428 llvm::raw_ostream* OS) {
429 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts, InFile, OS);
Daniel Dunbar85e44e22008-10-21 23:49:24 +0000430}