blob: 0faabbe8d58e458178215da132ca2042de75593c [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"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000013#include "clang/Basic/TargetInfo.h"
14#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000015#include "clang/Frontend/CompileOptions.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000016#include "llvm/Module.h"
17#include "llvm/ModuleProvider.h"
18#include "llvm/PassManager.h"
19#include "llvm/ADT/OwningPtr.h"
20#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar70f92432008-10-23 05:50:47 +000021#include "llvm/Analysis/CallGraph.h"
22#include "llvm/Analysis/Verifier.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000023#include "llvm/Bitcode/ReaderWriter.h"
24#include "llvm/CodeGen/RegAllocRegistry.h"
25#include "llvm/CodeGen/SchedulerRegistry.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000026#include "llvm/Support/raw_ostream.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000027#include "llvm/Support/Compiler.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000028#include "llvm/Support/Timer.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000029#include "llvm/System/Path.h"
30#include "llvm/System/Program.h"
Daniel Dunbara034ba82009-02-17 19:47:34 +000031#include "llvm/Target/SubtargetFeature.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000032#include "llvm/Target/TargetData.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetMachineRegistry.h"
Daniel Dunbar70f92432008-10-23 05:50:47 +000035#include "llvm/Transforms/Scalar.h"
36#include "llvm/Transforms/IPO.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000037using namespace clang;
38using namespace llvm;
39
40namespace {
Chris Lattner49f28ca2009-03-05 08:00:35 +000041 class VISIBILITY_HIDDEN BackendConsumer : public ASTConsumer {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000042 BackendAction Action;
Daniel Dunbar70f92432008-10-23 05:50:47 +000043 CompileOptions CompileOpts;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000044 const std::string &InputFile;
45 std::string OutputFile;
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;
55 llvm::raw_ostream *AsmOutStream;
56
Nuno Lopesdd492672008-10-24 22:51:00 +000057 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000058 mutable FunctionPassManager *CodeGenPasses;
59 mutable PassManager *PerModulePasses;
60 mutable FunctionPassManager *PerFunctionPasses;
61
62 FunctionPassManager *getCodeGenPasses() const;
63 PassManager *getPerModulePasses() const;
64 FunctionPassManager *getPerFunctionPasses() const;
65
66 void CreatePasses();
67
68 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM
69 /// IR.
70 ///
Daniel Dunbard69bacc2008-10-21 23:49:24 +000071 /// \return True on success. On failure \arg Error will be set to
72 /// a user readable error message.
Daniel Dunbar4c877cc2008-10-23 05:59:43 +000073 bool AddEmitPasses(std::string &Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000074
75 void EmitAssembly();
76
77 public:
78 BackendConsumer(BackendAction action, Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +000079 const LangOptions &langopts, const CompileOptions &compopts,
Chris Lattner20126042009-03-09 22:00:34 +000080 const std::string &infile, const std::string &outfile) :
Daniel Dunbard69bacc2008-10-21 23:49:24 +000081 Action(action),
Daniel Dunbar70f92432008-10-23 05:50:47 +000082 CompileOpts(compopts),
Daniel Dunbard69bacc2008-10-21 23:49:24 +000083 InputFile(infile),
84 OutputFile(outfile),
Chris Lattner6f114eb2009-02-18 01:37:30 +000085 LLVMIRGeneration("LLVM IR Generation Time"),
86 CodeGenerationTime("Code Generation Time"),
Chris Lattnerbd360642009-03-26 05:00:52 +000087 Gen(CreateLLVMCodeGen(Diags, InputFile, compopts)),
Nuno Lopesdd492672008-10-24 22:51:00 +000088 TheModule(0), TheTargetData(0), AsmOutStream(0), ModuleProvider(0),
Chris Lattner44502662009-02-18 01:23:44 +000089 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
90
Chris Lattner6f114eb2009-02-18 01:37:30 +000091 // Enable -time-passes if -ftime-report is enabled.
Chris Lattner44502662009-02-18 01:23:44 +000092 llvm::TimePassesIsEnabled = CompileOpts.TimePasses;
93 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000094
95 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000096 delete AsmOutStream;
97 delete TheTargetData;
Nuno Lopesdd492672008-10-24 22:51:00 +000098 delete ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000099 delete CodeGenPasses;
100 delete PerModulePasses;
101 delete PerFunctionPasses;
102 }
103
Chris Lattner7bb0da02009-03-28 02:18:25 +0000104 virtual void Initialize(ASTContext &Ctx) {
105 Context = &Ctx;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000106
107 if (CompileOpts.TimePasses)
108 LLVMIRGeneration.startTimer();
109
Chris Lattner7bb0da02009-03-28 02:18:25 +0000110 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000111
112 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000113 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattner7bb0da02009-03-28 02:18:25 +0000114 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Chris Lattner6f114eb2009-02-18 01:37:30 +0000115
116 if (CompileOpts.TimePasses)
117 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000118 }
119
120 virtual void HandleTopLevelDecl(Decl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000121 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
122 Context->getSourceManager(),
123 "LLVM IR generation of declaration");
Chris Lattner6f114eb2009-02-18 01:37:30 +0000124 if (CompileOpts.TimePasses)
125 LLVMIRGeneration.startTimer();
126
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000127 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000128
129 if (CompileOpts.TimePasses)
130 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000131 }
132
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000133 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000134 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000135 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000136 if (CompileOpts.TimePasses)
137 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000138
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000139 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000140
Chris Lattner49f28ca2009-03-05 08:00:35 +0000141 if (CompileOpts.TimePasses)
142 LLVMIRGeneration.stopTimer();
143 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000144
Chris Lattner49f28ca2009-03-05 08:00:35 +0000145 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000146 EmitAssembly();
147
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000148 // Force a flush here in case we never get released.
149 if (AsmOutStream)
150 AsmOutStream->flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000151 }
152
153 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000154 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
155 Context->getSourceManager(),
156 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000157 Gen->HandleTagDeclDefinition(D);
158 }
159 };
160}
161
162FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
163 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000164 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000165 CodeGenPasses->add(new TargetData(*TheTargetData));
166 }
167
168 return CodeGenPasses;
169}
170
171PassManager *BackendConsumer::getPerModulePasses() const {
172 if (!PerModulePasses) {
173 PerModulePasses = new PassManager();
174 PerModulePasses->add(new TargetData(*TheTargetData));
175 }
176
177 return PerModulePasses;
178}
179
180FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
181 if (!PerFunctionPasses) {
Nuno Lopes7d43a312008-10-24 23:27:18 +0000182 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000183 PerFunctionPasses->add(new TargetData(*TheTargetData));
184 }
185
186 return PerFunctionPasses;
187}
188
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000189bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000190 if (Action == Backend_EmitNothing)
191 return true;
192
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000193 if (OutputFile == "-" || (InputFile == "-" && OutputFile.empty())) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000194 AsmOutStream = new raw_stdout_ostream();
195 sys::Program::ChangeStdoutToBinary();
196 } else {
197 if (OutputFile.empty()) {
198 llvm::sys::Path Path(InputFile);
199 Path.eraseSuffix();
200 if (Action == Backend_EmitBC) {
201 Path.appendSuffix("bc");
202 } else if (Action == Backend_EmitLL) {
203 Path.appendSuffix("ll");
204 } else {
205 Path.appendSuffix("s");
206 }
207 OutputFile = Path.toString();
208 }
209
Daniel Dunbar26fb2722008-11-13 05:09:21 +0000210 AsmOutStream = new raw_fd_ostream(OutputFile.c_str(), true, Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000211 if (!Error.empty())
212 return false;
213 }
214
215 if (Action == Backend_EmitBC) {
Daniel Dunbared2cb282008-10-22 17:40:45 +0000216 getPerModulePasses()->add(createBitcodeWriterPass(*AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000217 } else if (Action == Backend_EmitLL) {
Daniel Dunbar11292b02008-10-22 03:28:13 +0000218 getPerModulePasses()->add(createPrintModulePass(AsmOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000219 } else {
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000220 bool Fast = CompileOpts.OptimizationLevel == 0;
221
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000222 // Create the TargetMachine for generating code.
223 const TargetMachineRegistry::entry *TME =
224 TargetMachineRegistry::getClosestStaticTargetForModule(*TheModule, Error);
225 if (!TME) {
226 Error = std::string("Unable to get target machine: ") + Error;
227 return false;
228 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000229
230 std::string FeaturesStr;
231 if (CompileOpts.CPU.size() || CompileOpts.Features.size()) {
232 SubtargetFeatures Features;
233 Features.setCPU(CompileOpts.CPU);
234 for (std::vector<std::string>::iterator
235 it = CompileOpts.Features.begin(),
236 ie = CompileOpts.Features.end(); it != ie; ++it)
237 Features.AddFeature(*it);
238 FeaturesStr = Features.getString();
239 }
240 TargetMachine *TM = TME->CtorFn(*TheModule, FeaturesStr);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000241
242 // Set register scheduler & allocation policy.
243 RegisterScheduler::setDefault(createDefaultScheduler);
244 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
245 createLinearScanRegisterAllocator);
246
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000247 // From llvm-gcc:
248 // If there are passes we have to run on the entire module, we do codegen
249 // as a separate "pass" after that happens.
250 // FIXME: This is disabled right now until bugs can be worked out. Reenable
251 // this for fast -O0 compiles!
252 FunctionPassManager *PM = getCodeGenPasses();
253
254 // Normal mode, emit a .s file by running the code generator.
255 // Note, this also adds codegenerator level optimization passes.
256 switch (TM->addPassesToEmitFile(*PM, *AsmOutStream,
257 TargetMachine::AssemblyFile, Fast)) {
258 default:
259 case FileModel::Error:
260 Error = "Unable to interface with target machine!\n";
261 return false;
262 case FileModel::AsmFile:
263 break;
264 }
265
266 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, 0, Fast)) {
267 Error = "Unable to interface with target machine!\n";
268 return false;
269 }
270 }
271
272 return true;
273}
274
275void BackendConsumer::CreatePasses() {
Daniel Dunbar70f92432008-10-23 05:50:47 +0000276 // In -O0 if checking is disabled, we don't even have per-function passes.
277 if (CompileOpts.VerifyModule)
278 getPerFunctionPasses()->add(createVerifierPass());
279
280 if (CompileOpts.OptimizationLevel > 0) {
281 FunctionPassManager *PM = getPerFunctionPasses();
282 PM->add(createCFGSimplificationPass());
283 if (CompileOpts.OptimizationLevel == 1)
284 PM->add(createPromoteMemoryToRegisterPass());
285 else
286 PM->add(createScalarReplAggregatesPass());
287 PM->add(createInstructionCombiningPass());
288 }
289
290 // For now we always create per module passes.
291 PassManager *PM = getPerModulePasses();
292 if (CompileOpts.OptimizationLevel > 0) {
293 if (CompileOpts.UnitAtATime)
294 PM->add(createRaiseAllocationsPass()); // call %malloc -> malloc inst
295 PM->add(createCFGSimplificationPass()); // Clean up disgusting code
296 PM->add(createPromoteMemoryToRegisterPass()); // Kill useless allocas
297 if (CompileOpts.UnitAtATime) {
298 PM->add(createGlobalOptimizerPass()); // Optimize out global vars
299 PM->add(createGlobalDCEPass()); // Remove unused fns and globs
300 PM->add(createIPConstantPropagationPass()); // IP Constant Propagation
301 PM->add(createDeadArgEliminationPass()); // Dead argument elimination
302 }
303 PM->add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
304 PM->add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
305 if (CompileOpts.UnitAtATime) {
306 PM->add(createPruneEHPass()); // Remove dead EH info
Bill Wendling5c5a7ee2008-12-31 19:51:31 +0000307 PM->add(createFunctionAttrsPass()); // Set readonly/readnone attrs
Daniel Dunbar70f92432008-10-23 05:50:47 +0000308 }
309 if (CompileOpts.InlineFunctions)
310 PM->add(createFunctionInliningPass()); // Inline small functions
311 else
312 PM->add(createAlwaysInlinerPass()); // Respect always_inline
313 if (CompileOpts.OptimizationLevel > 2)
314 PM->add(createArgumentPromotionPass()); // Scalarize uninlined fn args
315 if (CompileOpts.SimplifyLibCalls)
316 PM->add(createSimplifyLibCallsPass()); // Library Call Optimizations
317 PM->add(createInstructionCombiningPass()); // Cleanup for scalarrepl.
318 PM->add(createJumpThreadingPass()); // Thread jumps.
319 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
320 PM->add(createScalarReplAggregatesPass()); // Break up aggregate allocas
321 PM->add(createInstructionCombiningPass()); // Combine silly seq's
322 PM->add(createCondPropagationPass()); // Propagate conditionals
323 PM->add(createTailCallEliminationPass()); // Eliminate tail calls
324 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
325 PM->add(createReassociatePass()); // Reassociate expressions
326 PM->add(createLoopRotatePass()); // Rotate Loop
327 PM->add(createLICMPass()); // Hoist loop invariants
328 PM->add(createLoopUnswitchPass(CompileOpts.OptimizeSize ? true : false));
Devang Patel59db7602008-11-26 05:01:52 +0000329// PM->add(createLoopIndexSplitPass()); // Split loop index
Daniel Dunbar70f92432008-10-23 05:50:47 +0000330 PM->add(createInstructionCombiningPass());
331 PM->add(createIndVarSimplifyPass()); // Canonicalize indvars
332 PM->add(createLoopDeletionPass()); // Delete dead loops
333 if (CompileOpts.UnrollLoops)
334 PM->add(createLoopUnrollPass()); // Unroll small loops
335 PM->add(createInstructionCombiningPass()); // Clean up after the unroller
336 PM->add(createGVNPass()); // Remove redundancies
337 PM->add(createMemCpyOptPass()); // Remove memcpy / form memset
338 PM->add(createSCCPPass()); // Constant prop with SCCP
339
340 // Run instcombine after redundancy elimination to exploit opportunities
341 // opened up by them.
342 PM->add(createInstructionCombiningPass());
343 PM->add(createCondPropagationPass()); // Propagate conditionals
344 PM->add(createDeadStoreEliminationPass()); // Delete dead stores
345 PM->add(createAggressiveDCEPass()); // Delete dead instructions
346 PM->add(createCFGSimplificationPass()); // Merge & remove BBs
347
348 if (CompileOpts.UnitAtATime) {
349 PM->add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
350 PM->add(createDeadTypeEliminationPass()); // Eliminate dead types
351 }
352
353 if (CompileOpts.OptimizationLevel > 1 && CompileOpts.UnitAtATime)
354 PM->add(createConstantMergePass()); // Merge dup global constants
355 } else {
Daniel Dunbarb087ae92008-11-13 05:29:02 +0000356 PM->add(createAlwaysInlinerPass());
Daniel Dunbar70f92432008-10-23 05:50:47 +0000357 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000358}
359
360/// EmitAssembly - Handle interaction with LLVM backend to generate
361/// actual machine code.
362void BackendConsumer::EmitAssembly() {
363 // Silently ignore if we weren't initialized for some reason.
364 if (!TheModule || !TheTargetData)
365 return;
Chris Lattner6f114eb2009-02-18 01:37:30 +0000366
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000367
Chris Lattner8b76c0d2009-02-18 18:22:50 +0000368 TimeRegion Region(CompileOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000369
Daniel Dunbard611bac2008-10-27 20:40:41 +0000370 // Make sure IR generation is happy with the module. This is
371 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000372 Module *M = Gen->ReleaseModule();
373 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000374 // The module has been released by IR gen on failures, do not
375 // double free.
376 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000377 TheModule = 0;
378 return;
379 }
380
381 assert(TheModule == M && "Unexpected module change during IR generation");
382
383 CreatePasses();
384
385 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000386 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000387 // FIXME: Don't fail this way.
388 llvm::cerr << "ERROR: " << Error << "\n";
389 ::exit(1);
390 }
391
392 // Run passes. For now we do all passes at once, but eventually we
393 // would like to have the option of streaming code generation.
394
395 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000396 PrettyStackTraceString CrashInfo("Per-function optimization");
Chris Lattner49f28ca2009-03-05 08:00:35 +0000397
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000398 PerFunctionPasses->doInitialization();
399 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
400 if (!I->isDeclaration())
401 PerFunctionPasses->run(*I);
402 PerFunctionPasses->doFinalization();
403 }
404
Chris Lattner49f28ca2009-03-05 08:00:35 +0000405 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000406 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000407 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000408 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000409
410 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000411 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000412 CodeGenPasses->doInitialization();
413 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
414 if (!I->isDeclaration())
415 CodeGenPasses->run(*I);
416 CodeGenPasses->doFinalization();
417 }
418}
419
420ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
421 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000422 const LangOptions &LangOpts,
Daniel Dunbar70f92432008-10-23 05:50:47 +0000423 const CompileOptions &CompileOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000424 const std::string& InFile,
Chris Lattner20126042009-03-09 22:00:34 +0000425 const std::string& OutFile) {
Chris Lattner05e7c6d2009-02-12 01:50:58 +0000426 // FIXME: If optimizing, disable all debug info generation. The LLVM
427 // optimizer and backend is not ready to handle it when optimizations
428 // are enabled.
429 if (CompileOpts.OptimizationLevel > 0)
Chris Lattner20126042009-03-09 22:00:34 +0000430 const_cast<CompileOptions&>(CompileOpts).DebugInfo = false;
Daniel Dunbara034ba82009-02-17 19:47:34 +0000431
432 return new BackendConsumer(Action, Diags, LangOpts, CompileOpts,
Chris Lattner20126042009-03-09 22:00:34 +0000433 InFile, OutFile);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000434}