blob: 259713f25749cfcd532de9686b5ed066a0baa2a2 [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
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000253 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, 0, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000254 Error = "Unable to interface with target machine!\n";
255 return false;
256 }
257 }
258
259 return true;
260}
261
262void BackendConsumer::CreatePasses() {
Daniel Dunbar70f92432008-10-23 05:50:47 +0000263 // In -O0 if checking is disabled, we don't even have per-function passes.
264 if (CompileOpts.VerifyModule)
265 getPerFunctionPasses()->add(createVerifierPass());
266
267 if (CompileOpts.OptimizationLevel > 0) {
268 FunctionPassManager *PM = getPerFunctionPasses();
269 PM->add(createCFGSimplificationPass());
270 if (CompileOpts.OptimizationLevel == 1)
271 PM->add(createPromoteMemoryToRegisterPass());
272 else
273 PM->add(createScalarReplAggregatesPass());
274 PM->add(createInstructionCombiningPass());
275 }
276
277 // For now we always create per module passes.
278 PassManager *PM = getPerModulePasses();
279 if (CompileOpts.OptimizationLevel > 0) {
280 if (CompileOpts.UnitAtATime)
281 PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
282 PM->add(createCFGSimplificationPass()); // Clean up disgusting code
283 PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
284 if (CompileOpts.UnitAtATime) {
285 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
286 PM->add(createGlobalDCEPass()); // Remove unused fns and globs
287 PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
288 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
289 }
290 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
291 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
292 if (CompileOpts.UnitAtATime) {
293 PM->add(createPruneEHPass()); // Remove dead EH info
Bill Wendling5c5a7ee2008-12-31 19:51:31 +0000294 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbar70f92432008-10-23 05:50:47 +0000295 }
296 if (CompileOpts.InlineFunctions)
297 PM->add(createFunctionInliningPass()); // Inline small functions
298 else
299 PM->add(createAlwaysInlinerPass()); // Respect always_inline
300 if (CompileOpts.OptimizationLevel > 2)
301 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
302 if (CompileOpts.SimplifyLibCalls)
303 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
304 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
305 PM->add(createJumpThreadingPass()); // Thread jumps.
306 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
307 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
308 PM->add(createInstructionCombiningPass()); // Combine silly seq's
309 PM->add(createCondPropagationPass()); // Propagate conditionals
310 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
311 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
312 PM->add(createReassociatePass()); // Reassociate expressions
313 PM->add(createLoopRotatePass()); // Rotate Loop
314 PM->add(createLICMPass()); // Hoist loop invariants
315 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel59db7602008-11-26 05:01:52 +0000316// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbar70f92432008-10-23 05:50:47 +0000317 PM->add(createInstructionCombiningPass());
318 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
319 PM->add(createLoopDeletionPass()); // Delete dead loops
320 if (CompileOpts.UnrollLoops)
321 PM->add(createLoopUnrollPass()); // Unroll small loops
322 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
323 PM->add(createGVNPass()); // Remove redundancies
324 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
325 PM->add(createSCCPPass()); // Constant prop with SCCP
326
327 // Run instcombine after redundancy elimination to exploit opportunities
328 // opened up by them.
329 PM->add(createInstructionCombiningPass());
330 PM->add(createCondPropagationPass()); // Propagate conditionals
331 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
332 PM->add(createAggressiveDCEPass()); // Delete dead instructions
333 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
334
335 if (CompileOpts.UnitAtATime) {
336 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
337 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
338 }
339
340 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
341 PM->add(createConstantMergePass()); // Merge dup global constants
342 } else {
Daniel Dunbarb087ae92008-11-13 05:29:02 +0000343 PM->add(createAlwaysInlinerPass());
Daniel Dunbar70f92432008-10-23 05:50:47 +0000344 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000345}
346
347/// EmitAssembly - Handle interaction with LLVM backend to generate
348/// actual machine code.
349void BackendConsumer::EmitAssembly() {
350 // Silently ignore if we weren't initialized for some reason.
351 if (!TheModule || !TheTargetData)
352 return;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000353
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000354
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000355 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000356
Daniel Dunbard611bac2008-10-27 20:40:41 +0000357 // Make sure IR generation is happy with the module. This is
358 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000359 Module *M = Gen->ReleaseModule();
360 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000361 // The module has been released by IR gen on failures, do not
362 // double free.
363 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000364 TheModule = 0;
365 return;
366 }
367
368 assert(TheModule == M && "Unexpected module change during IR generation");
369
370 CreatePasses();
371
372 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000373 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000374 // FIXME: Don't fail this way.
375 llvm::cerr << "ERROR: " << Error << "\n";
376 ::exit(1);
377 }
378
379 // Run passes. For now we do all passes at once, but eventually we
380 // would like to have the option of streaming code generation.
381
382 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000383 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000384
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000385 PerFunctionPasses->doInitialization();
386 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
387 if (!I->isDeclaration())
388 PerFunctionPasses->run(*I);
389 PerFunctionPasses->doFinalization();
390 }
391
Chris Lattner49f28ca2009-03-05 08:00:35 +0000392 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000393 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000394 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000395 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000396
397 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000398 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000399 CodeGenPasses->doInitialization();
400 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
401 if (!I->isDeclaration())
402 CodeGenPasses->run(*I);
403 CodeGenPasses->doFinalization();
404 }
405}
406
407ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
408 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000409 const LangOptions &LangOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000410 const CompileOptions &CompileOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000411 const std::string& InFile,
Eli Friedman66d6f042009-05-18 22:20:00 +0000412 llvm::raw_ostream* OS) {
413 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts, InFile, OS);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000414}