blob: 85aeaebea7263686f4bda6d6bca2fc2b24755569 [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"
Chris Lattner682bf922009-03-29 16:50:03 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000012#include "clang/AST/ASTContext.h"
Chris Lattner682bf922009-03-29 16:50:03 +000013#include "clang/AST/DeclGroup.h"
14#include "clang/Basic/TargetInfo.h"
Daniel Dunbard58c03f2009-11-15 06:48:46 +000015#include "clang/Basic/TargetOptions.h"
16#include "clang/CodeGen/CodeGenOptions.h"
17#include "clang/CodeGen/ModuleBuilder.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000018#include "llvm/Module.h"
19#include "llvm/ModuleProvider.h"
20#include "llvm/PassManager.h"
21#include "llvm/ADT/OwningPtr.h"
22#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar70f92432008-10-23 05:50:47 +000023#include "llvm/Analysis/CallGraph.h"
24#include "llvm/Analysis/Verifier.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000025#include "llvm/Bitcode/ReaderWriter.h"
26#include "llvm/CodeGen/RegAllocRegistry.h"
27#include "llvm/CodeGen/SchedulerRegistry.h"
Chris Lattner03eacc72009-07-14 20:39:15 +000028#include "llvm/Support/FormattedStream.h"
Daniel Dunbar10d861e2009-06-03 18:01:18 +000029#include "llvm/Support/StandardPasses.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000030#include "llvm/Support/Timer.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"
Daniel Dunbarf7d47c02009-07-15 20:25:38 +000034#include "llvm/Target/TargetRegistry.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000035using namespace clang;
36using namespace llvm;
37
38namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000039 class BackendConsumer : public ASTConsumer {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000040 BackendAction Action;
Chandler Carruth2811ccf2009-11-12 17:24:48 +000041 CodeGenOptions CodeGenOpts;
Daniel Dunbard58c03f2009-11-15 06:48:46 +000042 TargetOptions TargetOpts;
Eli Friedman66d6f042009-05-18 22:20:00 +000043 llvm::raw_ostream *AsmOutStream;
Chris Lattner03eacc72009-07-14 20:39:15 +000044 llvm::formatted_raw_ostream FormattedOutStream;
Chris Lattner49f28ca2009-03-05 08:00:35 +000045 ASTContext *Context;
Daniel Dunbar90f41302008-10-29 08:50:02 +000046
Chris Lattner6f114eb2009-02-18 01:37:30 +000047 Timer LLVMIRGeneration;
48 Timer CodeGenerationTime;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Daniel Dunbard69bacc2008-10-21 23:49:24 +000050 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000051
Daniel Dunbard69bacc2008-10-21 23:49:24 +000052 llvm::Module *TheModule;
53 llvm::TargetData *TheTargetData;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000054
Nuno Lopesdd492672008-10-24 22:51:00 +000055 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000056 mutable FunctionPassManager *CodeGenPasses;
57 mutable PassManager *PerModulePasses;
58 mutable FunctionPassManager *PerFunctionPasses;
59
60 FunctionPassManager *getCodeGenPasses() const;
61 PassManager *getPerModulePasses() const;
62 FunctionPassManager *getPerFunctionPasses() const;
63
64 void CreatePasses();
65
66 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM
67 /// IR.
68 ///
Daniel Dunbard69bacc2008-10-21 23:49:24 +000069 /// \return True on success. On failure \arg Error will be set to
70 /// a user readable error message.
Daniel Dunbar4c877cc2008-10-23 05:59:43 +000071 bool AddEmitPasses(std::string &Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000072
73 void EmitAssembly();
Mike Stump1eb44332009-09-09 15:08:12 +000074
75 public:
76 BackendConsumer(BackendAction action, Diagnostic &Diags,
Chandler Carruth2811ccf2009-11-12 17:24:48 +000077 const LangOptions &langopts, const CodeGenOptions &compopts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +000078 const TargetOptions &targetopts, const std::string &infile,
79 llvm::raw_ostream* OS, LLVMContext& C) :
Mike Stump1eb44332009-09-09 15:08:12 +000080 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000081 CodeGenOpts(compopts),
Daniel Dunbard58c03f2009-11-15 06:48:46 +000082 TargetOpts(targetopts),
Chris Lattner03eacc72009-07-14 20:39:15 +000083 AsmOutStream(OS),
Chris Lattner6f114eb2009-02-18 01:37:30 +000084 LLVMIRGeneration("LLVM IR Generation Time"),
85 CodeGenerationTime("Code Generation Time"),
Owen Anderson42253cc2009-07-01 17:00:06 +000086 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
Eli Friedman66d6f042009-05-18 22:20:00 +000087 TheModule(0), TheTargetData(0), ModuleProvider(0),
Chris Lattner44502662009-02-18 01:23:44 +000088 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner03eacc72009-07-14 20:39:15 +000090 if (AsmOutStream)
91 FormattedOutStream.setStream(*AsmOutStream,
92 formatted_raw_ostream::PRESERVE_STREAM);
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner6f114eb2009-02-18 01:37:30 +000094 // Enable -time-passes if -ftime-report is enabled.
Chandler Carruth2811ccf2009-11-12 17:24:48 +000095 llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000096 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000097
98 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +000099 delete TheTargetData;
Nuno Lopesdd492672008-10-24 22:51:00 +0000100 delete ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000101 delete CodeGenPasses;
102 delete PerModulePasses;
103 delete PerFunctionPasses;
104 }
105
Chris Lattner7bb0da02009-03-28 02:18:25 +0000106 virtual void Initialize(ASTContext &Ctx) {
107 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000109 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000110 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chris Lattner7bb0da02009-03-28 02:18:25 +0000112 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000113
114 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000115 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattner7bb0da02009-03-28 02:18:25 +0000116 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000118 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000119 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000120 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattner682bf922009-03-29 16:50:03 +0000122 virtual void HandleTopLevelDecl(DeclGroupRef D) {
123 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +0000124 Context->getSourceManager(),
125 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000127 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000128 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000129
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000130 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000131
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000132 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000133 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000134 }
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000136 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000137 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000138 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000139 if (CodeGenOpts.TimePasses)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000140 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000141
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000142 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000143
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000144 if (CodeGenOpts.TimePasses)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000145 LLVMIRGeneration.stopTimer();
146 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000147
Chris Lattner49f28ca2009-03-05 08:00:35 +0000148 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000149 EmitAssembly();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000151 // Force a flush here in case we never get released.
152 if (AsmOutStream)
Chris Lattner03eacc72009-07-14 20:39:15 +0000153 FormattedOutStream.flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000156 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000157 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
158 Context->getSourceManager(),
159 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000160 Gen->HandleTagDeclDefinition(D);
161 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000162
163 virtual void CompleteTentativeDefinition(VarDecl *D) {
164 Gen->CompleteTentativeDefinition(D);
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166 };
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000167}
168
169FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
170 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000171 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000172 CodeGenPasses->add(new TargetData(*TheTargetData));
173 }
174
175 return CodeGenPasses;
176}
177
178PassManager *BackendConsumer::getPerModulePasses() const {
179 if (!PerModulePasses) {
180 PerModulePasses = new PassManager();
181 PerModulePasses->add(new TargetData(*TheTargetData));
182 }
183
184 return PerModulePasses;
185}
186
187FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
188 if (!PerFunctionPasses) {
Nuno Lopes7d43a312008-10-24 23:27:18 +0000189 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000190 PerFunctionPasses->add(new TargetData(*TheTargetData));
191 }
192
193 return PerFunctionPasses;
194}
195
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000196bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000197 if (Action == Backend_EmitNothing)
198 return true;
199
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000200 if (Action == Backend_EmitBC) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000201 getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000202 } else if (Action == Backend_EmitLL) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000203 getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000204 } else {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000205 bool Fast = CodeGenOpts.OptimizationLevel == 0;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000206
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000207 // Create the TargetMachine for generating code.
Daniel Dunbar82cfa7a2009-07-26 01:27:26 +0000208 std::string Triple = TheModule->getTargetTriple();
Daniel Dunbar9ab76fa2009-08-03 04:21:41 +0000209 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
Daniel Dunbarf7d47c02009-07-15 20:25:38 +0000210 if (!TheTarget) {
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000211 Error = std::string("Unable to get target machine: ") + Error;
212 return false;
213 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000214
215 std::string FeaturesStr;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000216 if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
Daniel Dunbara034ba82009-02-17 19:47:34 +0000217 SubtargetFeatures Features;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000218 Features.setCPU(TargetOpts.CPU);
Mike Stump1eb44332009-09-09 15:08:12 +0000219 for (std::vector<std::string>::iterator
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000220 it = TargetOpts.Features.begin(),
221 ie = TargetOpts.Features.end(); it != ie; ++it)
Daniel Dunbara034ba82009-02-17 19:47:34 +0000222 Features.AddFeature(*it);
223 FeaturesStr = Features.getString();
224 }
Daniel Dunbar2b1f59f2009-08-04 04:02:57 +0000225 TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000227 // Set register scheduler & allocation policy.
228 RegisterScheduler::setDefault(createDefaultScheduler);
Mike Stump1eb44332009-09-09 15:08:12 +0000229 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
230 createLinearScanRegisterAllocator);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000231
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000232 // From llvm-gcc:
233 // If there are passes we have to run on the entire module, we do codegen
234 // as a separate "pass" after that happens.
235 // FIXME: This is disabled right now until bugs can be worked out. Reenable
236 // this for fast -O0 compiles!
237 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000238 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
239
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000240 switch (CodeGenOpts.OptimizationLevel) {
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000241 default: break;
242 case 0: OptLevel = CodeGenOpt::None; break;
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000243 case 3: OptLevel = CodeGenOpt::Aggressive; break;
244 }
245
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000246 // Normal mode, emit a .s file by running the code generator.
247 // Note, this also adds codegenerator level optimization passes.
Chris Lattner03eacc72009-07-14 20:39:15 +0000248 switch (TM->addPassesToEmitFile(*PM, FormattedOutStream,
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000249 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000250 default:
251 case FileModel::Error:
252 Error = "Unable to interface with target machine!\n";
253 return false;
254 case FileModel::AsmFile:
255 break;
256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Duncan Sands813a2bb2009-05-31 04:09:57 +0000258 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
259 OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000260 Error = "Unable to interface with target machine!\n";
261 return false;
262 }
263 }
264
265 return true;
266}
267
268void BackendConsumer::CreatePasses() {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000269 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
270 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000271
272 // Handle disabling of LLVM optimization, where we want to preserve the
273 // internal module before any optimization.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000274 if (CodeGenOpts.DisableLLVMOpts) {
Daniel Dunbar8d353142009-11-10 17:50:53 +0000275 OptLevel = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000276 Inlining = CodeGenOpts.NoInlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000277 }
278
Daniel Dunbar70f92432008-10-23 05:50:47 +0000279 // In -O0 if checking is disabled, we don't even have per-function passes.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000280 if (CodeGenOpts.VerifyModule)
Daniel Dunbar70f92432008-10-23 05:50:47 +0000281 getPerFunctionPasses()->add(createVerifierPass());
282
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000283 // Assume that standard function passes aren't run for -O0.
Daniel Dunbar8d353142009-11-10 17:50:53 +0000284 if (OptLevel > 0)
285 llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000286
287 llvm::Pass *InliningPass = 0;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000288 switch (Inlining) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000289 case CodeGenOptions::NoInlining: break;
290 case CodeGenOptions::NormalInlining: {
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000291 // Inline small functions
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000292 unsigned Threshold = (CodeGenOpts.OptimizeSize || OptLevel < 3) ? 50 : 200;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000293 InliningPass = createFunctionInliningPass(Threshold);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000294 break;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000295 }
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000296 case CodeGenOptions::OnlyAlwaysInlining:
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000297 InliningPass = createAlwaysInlinerPass(); // Respect always_inline
298 break;
Daniel Dunbar70f92432008-10-23 05:50:47 +0000299 }
300
301 // For now we always create per module passes.
302 PassManager *PM = getPerModulePasses();
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000303 llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize,
304 CodeGenOpts.UnitAtATime,
305 CodeGenOpts.UnrollLoops,
306 CodeGenOpts.SimplifyLibCalls,
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000307 /*HaveExceptions=*/true,
308 InliningPass);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000309}
310
311/// EmitAssembly - Handle interaction with LLVM backend to generate
Mike Stump1eb44332009-09-09 15:08:12 +0000312/// actual machine code.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000313void BackendConsumer::EmitAssembly() {
314 // Silently ignore if we weren't initialized for some reason.
315 if (!TheModule || !TheTargetData)
316 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000318 TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000319
Daniel Dunbard611bac2008-10-27 20:40:41 +0000320 // Make sure IR generation is happy with the module. This is
321 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000322 Module *M = Gen->ReleaseModule();
323 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000324 // The module has been released by IR gen on failures, do not
325 // double free.
326 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000327 TheModule = 0;
328 return;
329 }
330
331 assert(TheModule == M && "Unexpected module change during IR generation");
332
333 CreatePasses();
334
335 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000336 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000337 // FIXME: Don't fail this way.
Chris Lattnera85b3522009-08-23 05:57:09 +0000338 llvm::errs() << "ERROR: " << Error << "\n";
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000339 ::exit(1);
340 }
341
342 // Run passes. For now we do all passes at once, but eventually we
343 // would like to have the option of streaming code generation.
344
345 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000346 PrettyStackTraceString CrashInfo("Per-function optimization");
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000348 PerFunctionPasses->doInitialization();
349 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
350 if (!I->isDeclaration())
351 PerFunctionPasses->run(*I);
352 PerFunctionPasses->doFinalization();
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner49f28ca2009-03-05 08:00:35 +0000355 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000356 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000357 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000360 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000361 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000362 CodeGenPasses->doInitialization();
363 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
364 if (!I->isDeclaration())
365 CodeGenPasses->run(*I);
366 CodeGenPasses->doFinalization();
367 }
368}
369
370ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
371 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000372 const LangOptions &LangOpts,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000373 const CodeGenOptions &CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000374 const TargetOptions &TargetOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000375 const std::string& InFile,
Owen Anderson42253cc2009-07-01 17:00:06 +0000376 llvm::raw_ostream* OS,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000377 LLVMContext& C) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000378 return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000379 TargetOpts, InFile, OS, C);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000380}