blob: 44aa3a81a96ef50b2c04304a178bfa1897a2ff27 [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
Eli Friedman39d7c4d2009-05-18 22:50:54 +000010#include "clang/Frontend/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;
Eli Friedman66d6f042009-05-18 22:20:00 +000045 llvm::raw_ostream *AsmOutStream;
Chris Lattner49f28ca2009-03-05 08:00:35 +000046 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000047
Chris Lattner6f114eb2009-02-18 01:37:30 +000048 Timer LLVMIRGeneration;
49 Timer CodeGenerationTime;
50
Daniel Dunbard69bacc2008-10-21 23:49:24 +000051 llvm::OwningPtr<CodeGenerator> Gen;
52
53 llvm::Module *TheModule;
54 llvm::TargetData *TheTargetData;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000055
Nuno Lopesdd492672008-10-24 22:51:00 +000056 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbard69bacc2008-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 Dunbard69bacc2008-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 Dunbar4c877cc2008-10-23 05:59:43 +000072 bool AddEmitPasses(std::string &Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000073
74 void EmitAssembly();
75
76 public:
77 BackendConsumer(BackendAction action, Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +000078 const LangOptions &langopts, const CompileOptions &compopts,
Eli Friedman66d6f042009-05-18 22:20:00 +000079 const std::string &infile, llvm::raw_ostream* OS) :
Daniel Dunbard69bacc2008-10-21 23:49:24 +000080 Action(action),
Daniel Dunbar70f92432008-10-23 05:50:47 +000081 CompileOpts(compopts),
Eli Friedman66d6f042009-05-18 22:20:00 +000082 AsmOutStream(OS),
Chris Lattner6f114eb2009-02-18 01:37:30 +000083 LLVMIRGeneration("LLVM IR Generation Time"),
84 CodeGenerationTime("Code Generation Time"),
Eli Friedman66d6f042009-05-18 22:20:00 +000085 Gen(CreateLLVMCodeGen(Diags, infile, compopts)),
86 TheModule(0), TheTargetData(0), ModuleProvider(0),
Chris Lattner44502662009-02-18 01:23:44 +000087 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
88
Chris Lattner6f114eb2009-02-18 01:37:30 +000089 // Enable -time-passes if -ftime-report is enabled.
Chris Lattner44502662009-02-18 01:23:44 +000090 llvm::TimePassesIsEnabled = CompileOpts.TimePasses;
91 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000092
93 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000094 delete TheTargetData;
Nuno Lopesdd492672008-10-24 22:51:00 +000095 delete ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000096 delete CodeGenPasses;
97 delete PerModulePasses;
98 delete PerFunctionPasses;
99 }
100
Chris Lattner7bb0da02009-03-28 02:18:25 +0000101 virtual void Initialize(ASTContext &Ctx) {
102 Context = &Ctx;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000103
104 if (CompileOpts.TimePasses)
105 LLVMIRGeneration.startTimer();
106
Chris Lattner7bb0da02009-03-28 02:18:25 +0000107 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000108
109 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000110 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattner7bb0da02009-03-28 02:18:25 +0000111 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Chris Lattner6f114eb2009-02-18 01:37:30 +0000112
113 if (CompileOpts.TimePasses)
114 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000115 }
116
Chris Lattner682bf922009-03-29 16:50:03 +0000117 virtual void HandleTopLevelDecl(DeclGroupRef D) {
118 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +0000119 Context->getSourceManager(),
120 "LLVM IR generation of declaration");
Chris Lattner682bf922009-03-29 16:50:03 +0000121
Chris Lattner6f114eb2009-02-18 01:37:30 +0000122 if (CompileOpts.TimePasses)
123 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000124
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000125 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000126
127 if (CompileOpts.TimePasses)
128 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000129 }
130
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000131 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000132 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000133 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000134 if (CompileOpts.TimePasses)
135 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000136
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000137 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000138
Chris Lattner49f28ca2009-03-05 08:00:35 +0000139 if (CompileOpts.TimePasses)
140 LLVMIRGeneration.stopTimer();
141 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000142
Chris Lattner49f28ca2009-03-05 08:00:35 +0000143 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000144 EmitAssembly();
145
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000146 // Force a flush here in case we never get released.
147 if (AsmOutStream)
148 AsmOutStream->flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000149 }
150
151 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000152 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
153 Context->getSourceManager(),
154 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000155 Gen->HandleTagDeclDefinition(D);
156 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000157
158 virtual void CompleteTentativeDefinition(VarDecl *D) {
159 Gen->CompleteTentativeDefinition(D);
160 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000161 };
162}
163
164FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
165 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000166 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-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 Lopes7d43a312008-10-24 23:27:18 +0000184 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000185 PerFunctionPasses->add(new TargetData(*TheTargetData));
186 }
187
188 return PerFunctionPasses;
189}
190
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000191bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000192 if (Action == Backend_EmitNothing)
193 return true;
194
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000195 if (Action == Backend_EmitBC) {
Daniel Dunbared2cb282008-10-22 17:40:45 +0000196 getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000197 } else if (Action == Backend_EmitLL) {
Daniel Dunbar11292b02008-10-22 03:28:13 +0000198 getPerModulePasses()->add(createPrintModulePass(AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000199 } else {
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000200 bool Fast = CompileOpts.OptimizationLevel == 0;
201
Daniel Dunbar8b7650e2008-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 Dunbara034ba82009-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 Dunbar8b7650e2008-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 Dunbard69bacc2008-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 Wendling6e9b8f62009-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 Wendling6e9b8f62009-04-29 23:53:23 +0000238 case 3: OptLevel = CodeGenOpt::Aggressive; break;
239 }
240
Daniel Dunbard69bacc2008-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 Wendling6e9b8f62009-04-29 23:53:23 +0000244 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbard69bacc2008-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 Sands813a2bb2009-05-31 04:09:57 +0000253 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
254 OptLevel)) {
Daniel Dunbard69bacc2008-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 Dunbar70f92432008-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 Wendling5c5a7ee2008-12-31 19:51:31 +0000295 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbar70f92432008-10-23 05:50:47 +0000296 }
297 if (CompileOpts.InlineFunctions)
298 PM->add(createFunctionInliningPass()); // Inline small functions
299 else
300 PM->add(createAlwaysInlinerPass()); // Respect always_inline
301 if (CompileOpts.OptimizationLevel > 2)
302 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
303 if (CompileOpts.SimplifyLibCalls)
304 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
305 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
306 PM->add(createJumpThreadingPass()); // Thread jumps.
307 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
308 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
309 PM->add(createInstructionCombiningPass()); // Combine silly seq's
310 PM->add(createCondPropagationPass()); // Propagate conditionals
311 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
312 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
313 PM->add(createReassociatePass()); // Reassociate expressions
314 PM->add(createLoopRotatePass()); // Rotate Loop
315 PM->add(createLICMPass()); // Hoist loop invariants
316 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel59db7602008-11-26 05:01:52 +0000317// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbar70f92432008-10-23 05:50:47 +0000318 PM->add(createInstructionCombiningPass());
319 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
320 PM->add(createLoopDeletionPass()); // Delete dead loops
321 if (CompileOpts.UnrollLoops)
322 PM->add(createLoopUnrollPass()); // Unroll small loops
323 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
324 PM->add(createGVNPass()); // Remove redundancies
325 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
326 PM->add(createSCCPPass()); // Constant prop with SCCP
327
328 // Run instcombine after redundancy elimination to exploit opportunities
329 // opened up by them.
330 PM->add(createInstructionCombiningPass());
331 PM->add(createCondPropagationPass()); // Propagate conditionals
332 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
333 PM->add(createAggressiveDCEPass()); // Delete dead instructions
334 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
335
336 if (CompileOpts.UnitAtATime) {
337 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
338 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
339 }
340
341 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
342 PM->add(createConstantMergePass()); // Merge dup global constants
343 } else {
Daniel Dunbarb087ae92008-11-13 05:29:02 +0000344 PM->add(createAlwaysInlinerPass());
Daniel Dunbar70f92432008-10-23 05:50:47 +0000345 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000346}
347
348/// EmitAssembly - Handle interaction with LLVM backend to generate
349/// actual machine code.
350void BackendConsumer::EmitAssembly() {
351 // Silently ignore if we weren't initialized for some reason.
352 if (!TheModule || !TheTargetData)
353 return;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000354
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000355
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000356 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000357
Daniel Dunbard611bac2008-10-27 20:40:41 +0000358 // Make sure IR generation is happy with the module. This is
359 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000360 Module *M = Gen->ReleaseModule();
361 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000362 // The module has been released by IR gen on failures, do not
363 // double free.
364 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000365 TheModule = 0;
366 return;
367 }
368
369 assert(TheModule == M && "Unexpected module change during IR generation");
370
371 CreatePasses();
372
373 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000374 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000375 // FIXME: Don't fail this way.
376 llvm::cerr << "ERROR: " << Error << "\n";
377 ::exit(1);
378 }
379
380 // Run passes. For now we do all passes at once, but eventually we
381 // would like to have the option of streaming code generation.
382
383 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000384 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000385
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000386 PerFunctionPasses->doInitialization();
387 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
388 if (!I->isDeclaration())
389 PerFunctionPasses->run(*I);
390 PerFunctionPasses->doFinalization();
391 }
392
Chris Lattner49f28ca2009-03-05 08:00:35 +0000393 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000394 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000395 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000396 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000397
398 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000399 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000400 CodeGenPasses->doInitialization();
401 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
402 if (!I->isDeclaration())
403 CodeGenPasses->run(*I);
404 CodeGenPasses->doFinalization();
405 }
406}
407
408ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
409 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000410 const LangOptions &LangOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000411 const CompileOptions &CompileOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000412 const std::string& InFile,
Eli Friedman66d6f042009-05-18 22:20:00 +0000413 llvm::raw_ostream* OS) {
414 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts, InFile, OS);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000415}