blob: bc56029e73d231f8d53859add209aa6c49fba1fe [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"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000028#include "llvm/Support/Compiler.h"
Chris Lattner03eacc72009-07-14 20:39:15 +000029#include "llvm/Support/FormattedStream.h"
Daniel Dunbar10d861e2009-06-03 18:01:18 +000030#include "llvm/Support/StandardPasses.h"
Chris Lattner6f114eb2009-02-18 01:37:30 +000031#include "llvm/Support/Timer.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000032#include "llvm/System/Path.h"
33#include "llvm/System/Program.h"
Daniel Dunbara034ba82009-02-17 19:47:34 +000034#include "llvm/Target/SubtargetFeature.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000035#include "llvm/Target/TargetData.h"
36#include "llvm/Target/TargetMachine.h"
Daniel Dunbarf7d47c02009-07-15 20:25:38 +000037#include "llvm/Target/TargetRegistry.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;
Chandler Carruth2811ccf2009-11-12 17:24:48 +000044 CodeGenOptions CodeGenOpts;
Daniel Dunbard58c03f2009-11-15 06:48:46 +000045 TargetOptions TargetOpts;
Eli Friedman66d6f042009-05-18 22:20:00 +000046 llvm::raw_ostream *AsmOutStream;
Chris Lattner03eacc72009-07-14 20:39:15 +000047 llvm::formatted_raw_ostream FormattedOutStream;
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;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Daniel Dunbard69bacc2008-10-21 23:49:24 +000053 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Daniel Dunbard69bacc2008-10-21 23:49:24 +000055 llvm::Module *TheModule;
56 llvm::TargetData *TheTargetData;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000057
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();
Mike Stump1eb44332009-09-09 15:08:12 +000077
78 public:
79 BackendConsumer(BackendAction action, Diagnostic &Diags,
Chandler Carruth2811ccf2009-11-12 17:24:48 +000080 const LangOptions &langopts, const CodeGenOptions &compopts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +000081 const TargetOptions &targetopts, const std::string &infile,
82 llvm::raw_ostream* OS, LLVMContext& C) :
Mike Stump1eb44332009-09-09 15:08:12 +000083 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000084 CodeGenOpts(compopts),
Daniel Dunbard58c03f2009-11-15 06:48:46 +000085 TargetOpts(targetopts),
Chris Lattner03eacc72009-07-14 20:39:15 +000086 AsmOutStream(OS),
Chris Lattner6f114eb2009-02-18 01:37:30 +000087 LLVMIRGeneration("LLVM IR Generation Time"),
88 CodeGenerationTime("Code Generation Time"),
Owen Anderson42253cc2009-07-01 17:00:06 +000089 Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
Eli Friedman66d6f042009-05-18 22:20:00 +000090 TheModule(0), TheTargetData(0), ModuleProvider(0),
Chris Lattner44502662009-02-18 01:23:44 +000091 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner03eacc72009-07-14 20:39:15 +000093 if (AsmOutStream)
94 FormattedOutStream.setStream(*AsmOutStream,
95 formatted_raw_ostream::PRESERVE_STREAM);
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner6f114eb2009-02-18 01:37:30 +000097 // Enable -time-passes if -ftime-report is enabled.
Chandler Carruth2811ccf2009-11-12 17:24:48 +000098 llvm::TimePassesIsEnabled = CodeGenOpts.TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000099 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000100
101 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000102 delete TheTargetData;
Nuno Lopesdd492672008-10-24 22:51:00 +0000103 delete ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000104 delete CodeGenPasses;
105 delete PerModulePasses;
106 delete PerFunctionPasses;
107 }
108
Chris Lattner7bb0da02009-03-28 02:18:25 +0000109 virtual void Initialize(ASTContext &Ctx) {
110 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000112 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000113 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner7bb0da02009-03-28 02:18:25 +0000115 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000116
117 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000118 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattner7bb0da02009-03-28 02:18:25 +0000119 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000121 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000122 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner682bf922009-03-29 16:50:03 +0000125 virtual void HandleTopLevelDecl(DeclGroupRef D) {
126 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +0000127 Context->getSourceManager(),
128 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000130 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000131 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000132
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000133 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000134
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000135 if (CodeGenOpts.TimePasses)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000136 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000139 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000140 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000141 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000142 if (CodeGenOpts.TimePasses)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000143 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000144
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000145 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000146
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000147 if (CodeGenOpts.TimePasses)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000148 LLVMIRGeneration.stopTimer();
149 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000150
Chris Lattner49f28ca2009-03-05 08:00:35 +0000151 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000152 EmitAssembly();
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000154 // Force a flush here in case we never get released.
155 if (AsmOutStream)
Chris Lattner03eacc72009-07-14 20:39:15 +0000156 FormattedOutStream.flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000159 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000160 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
161 Context->getSourceManager(),
162 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000163 Gen->HandleTagDeclDefinition(D);
164 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000165
166 virtual void CompleteTentativeDefinition(VarDecl *D) {
167 Gen->CompleteTentativeDefinition(D);
168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169 };
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000170}
171
172FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
173 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000174 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000175 CodeGenPasses->add(new TargetData(*TheTargetData));
176 }
177
178 return CodeGenPasses;
179}
180
181PassManager *BackendConsumer::getPerModulePasses() const {
182 if (!PerModulePasses) {
183 PerModulePasses = new PassManager();
184 PerModulePasses->add(new TargetData(*TheTargetData));
185 }
186
187 return PerModulePasses;
188}
189
190FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
191 if (!PerFunctionPasses) {
Nuno Lopes7d43a312008-10-24 23:27:18 +0000192 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000193 PerFunctionPasses->add(new TargetData(*TheTargetData));
194 }
195
196 return PerFunctionPasses;
197}
198
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000199bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000200 if (Action == Backend_EmitNothing)
201 return true;
202
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000203 if (Action == Backend_EmitBC) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000204 getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000205 } else if (Action == Backend_EmitLL) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000206 getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000207 } else {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000208 bool Fast = CodeGenOpts.OptimizationLevel == 0;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000209
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000210 // Create the TargetMachine for generating code.
Daniel Dunbar82cfa7a2009-07-26 01:27:26 +0000211 std::string Triple = TheModule->getTargetTriple();
Daniel Dunbar9ab76fa2009-08-03 04:21:41 +0000212 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
Daniel Dunbarf7d47c02009-07-15 20:25:38 +0000213 if (!TheTarget) {
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000214 Error = std::string("Unable to get target machine: ") + Error;
215 return false;
216 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000217
218 std::string FeaturesStr;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000219 if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
Daniel Dunbara034ba82009-02-17 19:47:34 +0000220 SubtargetFeatures Features;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000221 Features.setCPU(TargetOpts.CPU);
Mike Stump1eb44332009-09-09 15:08:12 +0000222 for (std::vector<std::string>::iterator
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000223 it = TargetOpts.Features.begin(),
224 ie = TargetOpts.Features.end(); it != ie; ++it)
Daniel Dunbara034ba82009-02-17 19:47:34 +0000225 Features.AddFeature(*it);
226 FeaturesStr = Features.getString();
227 }
Daniel Dunbar2b1f59f2009-08-04 04:02:57 +0000228 TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000230 // Set register scheduler & allocation policy.
231 RegisterScheduler::setDefault(createDefaultScheduler);
Mike Stump1eb44332009-09-09 15:08:12 +0000232 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
233 createLinearScanRegisterAllocator);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000234
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000235 // From llvm-gcc:
236 // If there are passes we have to run on the entire module, we do codegen
237 // as a separate "pass" after that happens.
238 // FIXME: This is disabled right now until bugs can be worked out. Reenable
239 // this for fast -O0 compiles!
240 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000241 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
242
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000243 switch (CodeGenOpts.OptimizationLevel) {
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000244 default: break;
245 case 0: OptLevel = CodeGenOpt::None; break;
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000246 case 3: OptLevel = CodeGenOpt::Aggressive; break;
247 }
248
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000249 // Normal mode, emit a .s file by running the code generator.
250 // Note, this also adds codegenerator level optimization passes.
Chris Lattner03eacc72009-07-14 20:39:15 +0000251 switch (TM->addPassesToEmitFile(*PM, FormattedOutStream,
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000252 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000253 default:
254 case FileModel::Error:
255 Error = "Unable to interface with target machine!\n";
256 return false;
257 case FileModel::AsmFile:
258 break;
259 }
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Duncan Sands813a2bb2009-05-31 04:09:57 +0000261 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
262 OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000263 Error = "Unable to interface with target machine!\n";
264 return false;
265 }
266 }
267
268 return true;
269}
270
271void BackendConsumer::CreatePasses() {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000272 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
273 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000274
275 // Handle disabling of LLVM optimization, where we want to preserve the
276 // internal module before any optimization.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000277 if (CodeGenOpts.DisableLLVMOpts) {
Daniel Dunbar8d353142009-11-10 17:50:53 +0000278 OptLevel = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000279 Inlining = CodeGenOpts.NoInlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000280 }
281
Daniel Dunbar70f92432008-10-23 05:50:47 +0000282 // In -O0 if checking is disabled, we don't even have per-function passes.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000283 if (CodeGenOpts.VerifyModule)
Daniel Dunbar70f92432008-10-23 05:50:47 +0000284 getPerFunctionPasses()->add(createVerifierPass());
285
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000286 // Assume that standard function passes aren't run for -O0.
Daniel Dunbar8d353142009-11-10 17:50:53 +0000287 if (OptLevel > 0)
288 llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000289
290 llvm::Pass *InliningPass = 0;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000291 switch (Inlining) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000292 case CodeGenOptions::NoInlining: break;
293 case CodeGenOptions::NormalInlining: {
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000294 // Inline small functions
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000295 unsigned Threshold = (CodeGenOpts.OptimizeSize || OptLevel < 3) ? 50 : 200;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000296 InliningPass = createFunctionInliningPass(Threshold);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000297 break;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000298 }
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000299 case CodeGenOptions::OnlyAlwaysInlining:
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000300 InliningPass = createAlwaysInlinerPass(); // Respect always_inline
301 break;
Daniel Dunbar70f92432008-10-23 05:50:47 +0000302 }
303
304 // For now we always create per module passes.
305 PassManager *PM = getPerModulePasses();
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000306 llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize,
307 CodeGenOpts.UnitAtATime,
308 CodeGenOpts.UnrollLoops,
309 CodeGenOpts.SimplifyLibCalls,
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000310 /*HaveExceptions=*/true,
311 InliningPass);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000312}
313
314/// EmitAssembly - Handle interaction with LLVM backend to generate
Mike Stump1eb44332009-09-09 15:08:12 +0000315/// actual machine code.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000316void BackendConsumer::EmitAssembly() {
317 // Silently ignore if we weren't initialized for some reason.
318 if (!TheModule || !TheTargetData)
319 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000321 TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000322
Daniel Dunbard611bac2008-10-27 20:40:41 +0000323 // Make sure IR generation is happy with the module. This is
324 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000325 Module *M = Gen->ReleaseModule();
326 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000327 // The module has been released by IR gen on failures, do not
328 // double free.
329 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000330 TheModule = 0;
331 return;
332 }
333
334 assert(TheModule == M && "Unexpected module change during IR generation");
335
336 CreatePasses();
337
338 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000339 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000340 // FIXME: Don't fail this way.
Chris Lattnera85b3522009-08-23 05:57:09 +0000341 llvm::errs() << "ERROR: " << Error << "\n";
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000342 ::exit(1);
343 }
344
345 // Run passes. For now we do all passes at once, but eventually we
346 // would like to have the option of streaming code generation.
347
348 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000349 PrettyStackTraceString CrashInfo("Per-function optimization");
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000351 PerFunctionPasses->doInitialization();
352 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
353 if (!I->isDeclaration())
354 PerFunctionPasses->run(*I);
355 PerFunctionPasses->doFinalization();
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattner49f28ca2009-03-05 08:00:35 +0000358 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000359 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000360 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000363 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000364 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000365 CodeGenPasses->doInitialization();
366 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
367 if (!I->isDeclaration())
368 CodeGenPasses->run(*I);
369 CodeGenPasses->doFinalization();
370 }
371}
372
373ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
374 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000375 const LangOptions &LangOpts,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000376 const CodeGenOptions &CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000377 const TargetOptions &TargetOpts,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000378 const std::string& InFile,
Owen Anderson42253cc2009-07-01 17:00:06 +0000379 llvm::raw_ostream* OS,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000380 LLVMContext& C) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000381 return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000382 TargetOpts, InFile, OS, C);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000383}