blob: 4c5b741a27fd090eef4f4aa1fde3a7a3b3dd01d9 [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/AST/ASTContext.h"
12#include "clang/AST/ASTConsumer.h"
13#include "clang/AST/TranslationUnit.h"
14#include "clang/Basic/TargetInfo.h"
15#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000016#include "clang/Frontend/CompileOptions.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;
Daniel Dunbar90f41302008-10-29 08:50:02 +000047 bool GenerateDebugInfo;
Chris Lattner49f28ca2009-03-05 08:00:35 +000048 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000049
Chris Lattner6f114eb2009-02-18 01:37:30 +000050 Timer LLVMIRGeneration;
51 Timer CodeGenerationTime;
52
Daniel Dunbard69bacc2008-10-21 23:49:24 +000053 llvm::OwningPtr<CodeGenerator> Gen;
54
55 llvm::Module *TheModule;
56 llvm::TargetData *TheTargetData;
57 llvm::raw_ostream *AsmOutStream;
58
Nuno Lopesdd492672008-10-24 22:51:00 +000059 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000060 mutable FunctionPassManager *CodeGenPasses;
61 mutable PassManager *PerModulePasses;
62 mutable FunctionPassManager *PerFunctionPasses;
63
64 FunctionPassManager *getCodeGenPasses() const;
65 PassManager *getPerModulePasses() const;
66 FunctionPassManager *getPerFunctionPasses() const;
67
68 void CreatePasses();
69
70 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM
71 /// IR.
72 ///
Daniel Dunbard69bacc2008-10-21 23:49:24 +000073 /// \return True on success. On failure \arg Error will be set to
74 /// a user readable error message.
Daniel Dunbar4c877cc2008-10-23 05:59:43 +000075 bool AddEmitPasses(std::string &Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000076
77 void EmitAssembly();
78
79 public:
80 BackendConsumer(BackendAction action, Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +000081 const LangOptions &langopts, const CompileOptions &compopts,
Chris Lattner49f28ca2009-03-05 08:00:35 +000082 const std::string &infile, const std::string &outfile,
83 bool debug) :
Daniel Dunbard69bacc2008-10-21 23:49:24 +000084 Action(action),
Daniel Dunbar70f92432008-10-23 05:50:47 +000085 CompileOpts(compopts),
Daniel Dunbard69bacc2008-10-21 23:49:24 +000086 InputFile(infile),
87 OutputFile(outfile),
Daniel Dunbar90f41302008-10-29 08:50:02 +000088 GenerateDebugInfo(debug),
Chris Lattner6f114eb2009-02-18 01:37:30 +000089 LLVMIRGeneration("LLVM IR Generation Time"),
90 CodeGenerationTime("Code Generation Time"),
Daniel Dunbara034ba82009-02-17 19:47:34 +000091 Gen(CreateLLVMCodeGen(Diags, langopts, InputFile, GenerateDebugInfo)),
Nuno Lopesdd492672008-10-24 22:51:00 +000092 TheModule(0), TheTargetData(0), AsmOutStream(0), ModuleProvider(0),
Chris Lattner44502662009-02-18 01:23:44 +000093 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
94
Chris Lattner6f114eb2009-02-18 01:37:30 +000095 // Enable -time-passes if -ftime-report is enabled.
Chris Lattner44502662009-02-18 01:23:44 +000096 llvm::TimePassesIsEnabled = CompileOpts.TimePasses;
97 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000098
99 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000100 delete AsmOutStream;
101 delete TheTargetData;
Nuno Lopesdd492672008-10-24 22:51:00 +0000102 delete ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000103 delete CodeGenPasses;
104 delete PerModulePasses;
105 delete PerFunctionPasses;
106 }
107
108 virtual void InitializeTU(TranslationUnit& TU) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000109 Context = &TU.getContext();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000110
111 if (CompileOpts.TimePasses)
112 LLVMIRGeneration.startTimer();
113
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000114 Gen->InitializeTU(TU);
115
116 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000117 ModuleProvider = new ExistingModuleProvider(TheModule);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000118 TheTargetData =
119 new llvm::TargetData(TU.getContext().Target.getTargetDescription());
Chris Lattner6f114eb2009-02-18 01:37:30 +0000120
121 if (CompileOpts.TimePasses)
122 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000123 }
124
125 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000126 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
127 Context->getSourceManager(),
128 "LLVM IR generation of declaration");
Chris Lattner6f114eb2009-02-18 01:37:30 +0000129 if (CompileOpts.TimePasses)
130 LLVMIRGeneration.startTimer();
131
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000132 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000133
134 if (CompileOpts.TimePasses)
135 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000136 }
137
138 virtual void HandleTranslationUnit(TranslationUnit& TU) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000139 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000140 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000141 if (CompileOpts.TimePasses)
142 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000143
Chris Lattner49f28ca2009-03-05 08:00:35 +0000144 Gen->HandleTranslationUnit(TU);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000145
Chris Lattner49f28ca2009-03-05 08:00:35 +0000146 if (CompileOpts.TimePasses)
147 LLVMIRGeneration.stopTimer();
148 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000149
Chris Lattner49f28ca2009-03-05 08:00:35 +0000150 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000151 EmitAssembly();
152
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000153 // Force a flush here in case we never get released.
154 if (AsmOutStream)
155 AsmOutStream->flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000156 }
157
158 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000159 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
160 Context->getSourceManager(),
161 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000162 Gen->HandleTagDeclDefinition(D);
163 }
164 };
165}
166
167FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
168 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000169 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000170 CodeGenPasses->add(new TargetData(*TheTargetData));
171 }
172
173 return CodeGenPasses;
174}
175
176PassManager *BackendConsumer::getPerModulePasses() const {
177 if (!PerModulePasses) {
178 PerModulePasses = new PassManager();
179 PerModulePasses->add(new TargetData(*TheTargetData));
180 }
181
182 return PerModulePasses;
183}
184
185FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
186 if (!PerFunctionPasses) {
Nuno Lopes7d43a312008-10-24 23:27:18 +0000187 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000188 PerFunctionPasses->add(new TargetData(*TheTargetData));
189 }
190
191 return PerFunctionPasses;
192}
193
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000194bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000195 if (Action == Backend_EmitNothing)
196 return true;
197
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000198 if (OutputFile == "-" || (InputFile == "-" && OutputFile.empty())) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000199 AsmOutStream = new raw_stdout_ostream();
200 sys::Program::ChangeStdoutToBinary();
201 } else {
202 if (OutputFile.empty()) {
203 llvm::sys::Path Path(InputFile);
204 Path.eraseSuffix();
205 if (Action == Backend_EmitBC) {
206 Path.appendSuffix("bc");
207 } else if (Action == Backend_EmitLL) {
208 Path.appendSuffix("ll");
209 } else {
210 Path.appendSuffix("s");
211 }
212 OutputFile = Path.toString();
213 }
214
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000215 AsmOutStream = new raw_fd_ostream(OutputFile.c_str(), true, Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000216 if (!Error.empty())
217 return false;
218 }
219
220 if (Action == Backend_EmitBC) {
Daniel Dunbared2cb282008-10-22 17:40:45 +0000221 getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000222 } else if (Action == Backend_EmitLL) {
Daniel Dunbar11292b02008-10-22 03:28:13 +0000223 getPerModulePasses()->add(createPrintModulePass(AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000224 } else {
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000225 bool Fast = CompileOpts.OptimizationLevel == 0;
226
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000227 // Create the TargetMachine for generating code.
228 const TargetMachineRegistry::entry *TME =
229 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error);
230 if (!TME) {
231 Error = std::string("Unable to get target machine: ") + Error;
232 return false;
233 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000234
235 std::string FeaturesStr;
236 if (CompileOpts.CPU.size() || CompileOpts.Features.size()) {
237 SubtargetFeatures Features;
238 Features.setCPU(CompileOpts.CPU);
239 for (std::vector<std::string>::iterator
240 it = CompileOpts.Features.begin(),
241 ie = CompileOpts.Features.end(); it != ie; ++it)
242 Features.AddFeature(*it);
243 FeaturesStr = Features.getString();
244 }
245 TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000246
247 // Set register scheduler & allocation policy.
248 RegisterScheduler::setDefault(createDefaultScheduler);
249 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
250 createLinearScanRegisterAllocator);
251
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000252 // From llvm-gcc:
253 // If there are passes we have to run on the entire module, we do codegen
254 // as a separate "pass" after that happens.
255 // FIXME: This is disabled right now until bugs can be worked out. Reenable
256 // this for fast -O0 compiles!
257 FunctionPassManager *PM = getCodeGenPasses();
258
259 // Normal mode, emit a .s file by running the code generator.
260 // Note, this also adds codegenerator level optimization passes.
261 switch (TM->addPassesToEmitFile(*PM, *AsmOutStream,
262 TargetMachine::AssemblyFile, Fast)) {
263 default:
264 case FileModel::Error:
265 Error = "Unable to interface with target machine!\n";
266 return false;
267 case FileModel::AsmFile:
268 break;
269 }
270
271 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, 0, Fast)) {
272 Error = "Unable to interface with target machine!\n";
273 return false;
274 }
275 }
276
277 return true;
278}
279
280void BackendConsumer::CreatePasses() {
Daniel Dunbar70f92432008-10-23 05:50:47 +0000281 // In -O0 if checking is disabled, we don't even have per-function passes.
282 if (CompileOpts.VerifyModule)
283 getPerFunctionPasses()->add(createVerifierPass());
284
285 if (CompileOpts.OptimizationLevel > 0) {
286 FunctionPassManager *PM = getPerFunctionPasses();
287 PM->add(createCFGSimplificationPass());
288 if (CompileOpts.OptimizationLevel == 1)
289 PM->add(createPromoteMemoryToRegisterPass());
290 else
291 PM->add(createScalarReplAggregatesPass());
292 PM->add(createInstructionCombiningPass());
293 }
294
295 // For now we always create per module passes.
296 PassManager *PM = getPerModulePasses();
297 if (CompileOpts.OptimizationLevel > 0) {
298 if (CompileOpts.UnitAtATime)
299 PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
300 PM->add(createCFGSimplificationPass()); // Clean up disgusting code
301 PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
302 if (CompileOpts.UnitAtATime) {
303 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
304 PM->add(createGlobalDCEPass()); // Remove unused fns and globs
305 PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
306 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
307 }
308 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
309 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
310 if (CompileOpts.UnitAtATime) {
311 PM->add(createPruneEHPass()); // Remove dead EH info
Bill Wendling5c5a7ee2008-12-31 19:51:31 +0000312 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbar70f92432008-10-23 05:50:47 +0000313 }
314 if (CompileOpts.InlineFunctions)
315 PM->add(createFunctionInliningPass()); // Inline small functions
316 else
317 PM->add(createAlwaysInlinerPass()); // Respect always_inline
318 if (CompileOpts.OptimizationLevel > 2)
319 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
320 if (CompileOpts.SimplifyLibCalls)
321 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
322 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
323 PM->add(createJumpThreadingPass()); // Thread jumps.
324 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
325 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
326 PM->add(createInstructionCombiningPass()); // Combine silly seq's
327 PM->add(createCondPropagationPass()); // Propagate conditionals
328 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
329 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
330 PM->add(createReassociatePass()); // Reassociate expressions
331 PM->add(createLoopRotatePass()); // Rotate Loop
332 PM->add(createLICMPass()); // Hoist loop invariants
333 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel59db7602008-11-26 05:01:52 +0000334// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbar70f92432008-10-23 05:50:47 +0000335 PM->add(createInstructionCombiningPass());
336 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
337 PM->add(createLoopDeletionPass()); // Delete dead loops
338 if (CompileOpts.UnrollLoops)
339 PM->add(createLoopUnrollPass()); // Unroll small loops
340 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
341 PM->add(createGVNPass()); // Remove redundancies
342 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
343 PM->add(createSCCPPass()); // Constant prop with SCCP
344
345 // Run instcombine after redundancy elimination to exploit opportunities
346 // opened up by them.
347 PM->add(createInstructionCombiningPass());
348 PM->add(createCondPropagationPass()); // Propagate conditionals
349 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
350 PM->add(createAggressiveDCEPass()); // Delete dead instructions
351 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
352
353 if (CompileOpts.UnitAtATime) {
354 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
355 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
356 }
357
358 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
359 PM->add(createConstantMergePass()); // Merge dup global constants
360 } else {
Daniel Dunbarb087ae92008-11-13 05:29:02 +0000361 PM->add(createAlwaysInlinerPass());
Daniel Dunbar70f92432008-10-23 05:50:47 +0000362 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000363}
364
365/// EmitAssembly - Handle interaction with LLVM backend to generate
366/// actual machine code.
367void BackendConsumer::EmitAssembly() {
368 // Silently ignore if we weren't initialized for some reason.
369 if (!TheModule || !TheTargetData)
370 return;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000371
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000372
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000373 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000374
Daniel Dunbard611bac2008-10-27 20:40:41 +0000375 // Make sure IR generation is happy with the module. This is
376 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000377 Module *M = Gen->ReleaseModule();
378 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000379 // The module has been released by IR gen on failures, do not
380 // double free.
381 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000382 TheModule = 0;
383 return;
384 }
385
386 assert(TheModule == M && "Unexpected module change during IR generation");
387
388 CreatePasses();
389
390 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000391 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000392 // FIXME: Don't fail this way.
393 llvm::cerr << "ERROR: " << Error << "\n";
394 ::exit(1);
395 }
396
397 // Run passes. For now we do all passes at once, but eventually we
398 // would like to have the option of streaming code generation.
399
400 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000401 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000402
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000403 PerFunctionPasses->doInitialization();
404 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
405 if (!I->isDeclaration())
406 PerFunctionPasses->run(*I);
407 PerFunctionPasses->doFinalization();
408 }
409
Chris Lattner49f28ca2009-03-05 08:00:35 +0000410 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000411 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000412 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000413 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000414
415 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000416 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000417 CodeGenPasses->doInitialization();
418 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
419 if (!I->isDeclaration())
420 CodeGenPasses->run(*I);
421 CodeGenPasses->doFinalization();
422 }
423}
424
425ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
426 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000427 const LangOptions &LangOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000428 const CompileOptions &CompileOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000429 const std::string& InFile,
430 const std::string& OutFile,
431 bool GenerateDebugInfo) {
Chris Lattner05e7c6d2009-02-12 01:50:58 +0000432 // FIXME: If optimizing, disable all debug info generation. The LLVM
433 // optimizer and backend is not ready to handle it when optimizations
434 // are enabled.
435 if (CompileOpts.OptimizationLevel > 0)
436 GenerateDebugInfo = false;
Daniel Dunbara034ba82009-02-17 19:47:34 +0000437
438 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000439 InFile, OutFile, GenerateDebugInfo);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000440}