blob: 3b3be31866ee2c386d5304123f8fcd94171f523e [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 }
161 };
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 (OutputFile == "-" || (InputFile == "-" && OutputFile.empty())) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000196 AsmOutStream = new raw_stdout_ostream();
197 sys::Program::ChangeStdoutToBinary();
198 } else {
199 if (OutputFile.empty()) {
200 llvm::sys::Path Path(InputFile);
201 Path.eraseSuffix();
202 if (Action == Backend_EmitBC) {
203 Path.appendSuffix("bc");
204 } else if (Action == Backend_EmitLL) {
205 Path.appendSuffix("ll");
206 } else {
207 Path.appendSuffix("s");
208 }
209 OutputFile = Path.toString();
210 }
211
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000212 AsmOutStream = new raw_fd_ostream(OutputFile.c_str(), true, Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000213 if (!Error.empty())
214 return false;
215 }
216
217 if (Action == Backend_EmitBC) {
Daniel Dunbared2cb282008-10-22 17:40:45 +0000218 getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000219 } else if (Action == Backend_EmitLL) {
Daniel Dunbar11292b02008-10-22 03:28:13 +0000220 getPerModulePasses()->add(createPrintModulePass(AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000221 } else {
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000222 bool Fast = CompileOpts.OptimizationLevel == 0;
223
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000224 // Create the TargetMachine for generating code.
225 const TargetMachineRegistry::entry *TME =
226 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error);
227 if (!TME) {
228 Error = std::string("Unable to get target machine: ") + Error;
229 return false;
230 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000231
232 std::string FeaturesStr;
233 if (CompileOpts.CPU.size() || CompileOpts.Features.size()) {
234 SubtargetFeatures Features;
235 Features.setCPU(CompileOpts.CPU);
236 for (std::vector<std::string>::iterator
237 it = CompileOpts.Features.begin(),
238 ie = CompileOpts.Features.end(); it != ie; ++it)
239 Features.AddFeature(*it);
240 FeaturesStr = Features.getString();
241 }
242 TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000243
244 // Set register scheduler & allocation policy.
245 RegisterScheduler::setDefault(createDefaultScheduler);
246 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
247 createLinearScanRegisterAllocator);
248
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000249 // From llvm-gcc:
250 // If there are passes we have to run on the entire module, we do codegen
251 // as a separate "pass" after that happens.
252 // FIXME: This is disabled right now until bugs can be worked out. Reenable
253 // this for fast -O0 compiles!
254 FunctionPassManager *PM = getCodeGenPasses();
255
256 // Normal mode, emit a .s file by running the code generator.
257 // Note, this also adds codegenerator level optimization passes.
258 switch (TM->addPassesToEmitFile(*PM, *AsmOutStream,
259 TargetMachine::AssemblyFile, Fast)) {
260 default:
261 case FileModel::Error:
262 Error = "Unable to interface with target machine!\n";
263 return false;
264 case FileModel::AsmFile:
265 break;
266 }
267
268 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, 0, Fast)) {
269 Error = "Unable to interface with target machine!\n";
270 return false;
271 }
272 }
273
274 return true;
275}
276
277void BackendConsumer::CreatePasses() {
Daniel Dunbar70f92432008-10-23 05:50:47 +0000278 // In -O0 if checking is disabled, we don't even have per-function passes.
279 if (CompileOpts.VerifyModule)
280 getPerFunctionPasses()->add(createVerifierPass());
281
282 if (CompileOpts.OptimizationLevel > 0) {
283 FunctionPassManager *PM = getPerFunctionPasses();
284 PM->add(createCFGSimplificationPass());
285 if (CompileOpts.OptimizationLevel == 1)
286 PM->add(createPromoteMemoryToRegisterPass());
287 else
288 PM->add(createScalarReplAggregatesPass());
289 PM->add(createInstructionCombiningPass());
290 }
291
292 // For now we always create per module passes.
293 PassManager *PM = getPerModulePasses();
294 if (CompileOpts.OptimizationLevel > 0) {
295 if (CompileOpts.UnitAtATime)
296 PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
297 PM->add(createCFGSimplificationPass()); // Clean up disgusting code
298 PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
299 if (CompileOpts.UnitAtATime) {
300 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
301 PM->add(createGlobalDCEPass()); // Remove unused fns and globs
302 PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
303 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
304 }
305 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
306 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
307 if (CompileOpts.UnitAtATime) {
308 PM->add(createPruneEHPass()); // Remove dead EH info
Bill Wendling5c5a7ee2008-12-31 19:51:31 +0000309 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbar70f92432008-10-23 05:50:47 +0000310 }
311 if (CompileOpts.InlineFunctions)
312 PM->add(createFunctionInliningPass()); // Inline small functions
313 else
314 PM->add(createAlwaysInlinerPass()); // Respect always_inline
315 if (CompileOpts.OptimizationLevel > 2)
316 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
317 if (CompileOpts.SimplifyLibCalls)
318 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
319 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
320 PM->add(createJumpThreadingPass()); // Thread jumps.
321 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
322 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
323 PM->add(createInstructionCombiningPass()); // Combine silly seq's
324 PM->add(createCondPropagationPass()); // Propagate conditionals
325 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
326 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
327 PM->add(createReassociatePass()); // Reassociate expressions
328 PM->add(createLoopRotatePass()); // Rotate Loop
329 PM->add(createLICMPass()); // Hoist loop invariants
330 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel59db7602008-11-26 05:01:52 +0000331// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbar70f92432008-10-23 05:50:47 +0000332 PM->add(createInstructionCombiningPass());
333 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
334 PM->add(createLoopDeletionPass()); // Delete dead loops
335 if (CompileOpts.UnrollLoops)
336 PM->add(createLoopUnrollPass()); // Unroll small loops
337 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
338 PM->add(createGVNPass()); // Remove redundancies
339 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
340 PM->add(createSCCPPass()); // Constant prop with SCCP
341
342 // Run instcombine after redundancy elimination to exploit opportunities
343 // opened up by them.
344 PM->add(createInstructionCombiningPass());
345 PM->add(createCondPropagationPass()); // Propagate conditionals
346 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
347 PM->add(createAggressiveDCEPass()); // Delete dead instructions
348 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
349
350 if (CompileOpts.UnitAtATime) {
351 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
352 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
353 }
354
355 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
356 PM->add(createConstantMergePass()); // Merge dup global constants
357 } else {
Daniel Dunbarb087ae92008-11-13 05:29:02 +0000358 PM->add(createAlwaysInlinerPass());
Daniel Dunbar70f92432008-10-23 05:50:47 +0000359 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000360}
361
362/// EmitAssembly - Handle interaction with LLVM backend to generate
363/// actual machine code.
364void BackendConsumer::EmitAssembly() {
365 // Silently ignore if we weren't initialized for some reason.
366 if (!TheModule || !TheTargetData)
367 return;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000368
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000369
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000370 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000371
Daniel Dunbard611bac2008-10-27 20:40:41 +0000372 // Make sure IR generation is happy with the module. This is
373 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000374 Module *M = Gen->ReleaseModule();
375 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000376 // The module has been released by IR gen on failures, do not
377 // double free.
378 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000379 TheModule = 0;
380 return;
381 }
382
383 assert(TheModule == M && "Unexpected module change during IR generation");
384
385 CreatePasses();
386
387 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000388 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000389 // FIXME: Don't fail this way.
390 llvm::cerr << "ERROR: " << Error << "\n";
391 ::exit(1);
392 }
393
394 // Run passes. For now we do all passes at once, but eventually we
395 // would like to have the option of streaming code generation.
396
397 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000398 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000399
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000400 PerFunctionPasses->doInitialization();
401 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
402 if (!I->isDeclaration())
403 PerFunctionPasses->run(*I);
404 PerFunctionPasses->doFinalization();
405 }
406
Chris Lattner49f28ca2009-03-05 08:00:35 +0000407 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000408 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000409 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000410 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000411
412 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000413 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000414 CodeGenPasses->doInitialization();
415 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
416 if (!I->isDeclaration())
417 CodeGenPasses->run(*I);
418 CodeGenPasses->doFinalization();
419 }
420}
421
422ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
423 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000424 const LangOptions &LangOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000425 const CompileOptions &CompileOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000426 const std::string& InFile,
Chris Lattner20126042009-03-09 22:00:34 +0000427 const std::string& OutFile) {
Chris Lattner05e7c6d2009-02-12 01:50:58 +0000428 // FIXME: If optimizing, disable all debug info generation. The LLVM
429 // optimizer and backend is not ready to handle it when optimizations
430 // are enabled.
431 if (CompileOpts.OptimizationLevel > 0)
Chris Lattner20126042009-03-09 22:00:34 +0000432 const_cast<CompileOptions&>(CompileOpts).DebugInfo = false;
Daniel Dunbara034ba82009-02-17 19:47:34 +0000433
434 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts,
Chris Lattner20126042009-03-09 22:00:34 +0000435 InFile, OutFile);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000436}