blob: 31c6c5027be201070d5252a302781873d66d3d5e [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;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000041 const CodeGenOptions &CodeGenOpts;
42 const LangOptions &LangOpts;
43 const TargetOptions &TargetOpts;
Eli Friedman66d6f042009-05-18 22:20:00 +000044 llvm::raw_ostream *AsmOutStream;
Chris Lattner03eacc72009-07-14 20:39:15 +000045 llvm::formatted_raw_ostream FormattedOutStream;
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;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Daniel Dunbard69bacc2008-10-21 23:49:24 +000051 llvm::OwningPtr<CodeGenerator> Gen;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Daniel Dunbard69bacc2008-10-21 23:49:24 +000053 llvm::Module *TheModule;
54 llvm::TargetData *TheTargetData;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000055
Nuno Lopesdd492672008-10-24 22:51:00 +000056 mutable llvm::ModuleProvider *ModuleProvider;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000057 mutable FunctionPassManager *CodeGenPasses;
58 mutable PassManager *PerModulePasses;
59 mutable FunctionPassManager *PerFunctionPasses;
60
61 FunctionPassManager *getCodeGenPasses() const;
62 PassManager *getPerModulePasses() const;
63 FunctionPassManager *getPerFunctionPasses() const;
64
65 void CreatePasses();
66
67 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM
68 /// IR.
69 ///
Daniel Dunbard69bacc2008-10-21 23:49:24 +000070 /// \return True on success. On failure \arg Error will be set to
71 /// a user readable error message.
Daniel Dunbar4c877cc2008-10-23 05:59:43 +000072 bool AddEmitPasses(std::string &Error);
Daniel Dunbard69bacc2008-10-21 23:49:24 +000073
74 void EmitAssembly();
Mike Stump1eb44332009-09-09 15:08:12 +000075
76 public:
77 BackendConsumer(BackendAction action, Diagnostic &Diags,
Chandler Carruth2811ccf2009-11-12 17:24:48 +000078 const LangOptions &langopts, const CodeGenOptions &compopts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000079 const TargetOptions &targetopts, bool TimePasses,
80 const std::string &infile, llvm::raw_ostream *OS,
81 LLVMContext& C) :
Mike Stump1eb44332009-09-09 15:08:12 +000082 Action(action),
Chandler Carruth2811ccf2009-11-12 17:24:48 +000083 CodeGenOpts(compopts),
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000084 LangOpts(langopts),
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
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000097 llvm::TimePassesIsEnabled = TimePasses;
Chris Lattner44502662009-02-18 01:23:44 +000098 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +000099
100 ~BackendConsumer() {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000101 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
Chris Lattner7bb0da02009-03-28 02:18:25 +0000108 virtual void Initialize(ASTContext &Ctx) {
109 Context = &Ctx;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000111 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000112 LLVMIRGeneration.startTimer();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner7bb0da02009-03-28 02:18:25 +0000114 Gen->Initialize(Ctx);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000115
116 TheModule = Gen->GetModule();
Nuno Lopes7d43a312008-10-24 23:27:18 +0000117 ModuleProvider = new ExistingModuleProvider(TheModule);
Chris Lattner7bb0da02009-03-28 02:18:25 +0000118 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000120 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000121 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattner682bf922009-03-29 16:50:03 +0000124 virtual void HandleTopLevelDecl(DeclGroupRef D) {
125 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +0000126 Context->getSourceManager(),
127 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000129 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000130 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000131
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000132 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000133
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000134 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000135 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000136 }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000138 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000139 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000140 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000141 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000142 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000143
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000144 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000145
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000146 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000147 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000153 // Force a flush here in case we never get released.
154 if (AsmOutStream)
Chris Lattner03eacc72009-07-14 20:39:15 +0000155 FormattedOutStream.flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000158 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 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000164
165 virtual void CompleteTentativeDefinition(VarDecl *D) {
166 Gen->CompleteTentativeDefinition(D);
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168 };
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000169}
170
171FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
172 if (!CodeGenPasses) {
Nuno Lopesdd492672008-10-24 22:51:00 +0000173 CodeGenPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000174 CodeGenPasses->add(new TargetData(*TheTargetData));
175 }
176
177 return CodeGenPasses;
178}
179
180PassManager *BackendConsumer::getPerModulePasses() const {
181 if (!PerModulePasses) {
182 PerModulePasses = new PassManager();
183 PerModulePasses->add(new TargetData(*TheTargetData));
184 }
185
186 return PerModulePasses;
187}
188
189FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
190 if (!PerFunctionPasses) {
Nuno Lopes7d43a312008-10-24 23:27:18 +0000191 PerFunctionPasses = new FunctionPassManager(ModuleProvider);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000192 PerFunctionPasses->add(new TargetData(*TheTargetData));
193 }
194
195 return PerFunctionPasses;
196}
197
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000198bool BackendConsumer::AddEmitPasses(std::string &Error) {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000199 if (Action == Backend_EmitNothing)
200 return true;
201
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000202 if (Action == Backend_EmitBC) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000203 getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000204 } else if (Action == Backend_EmitLL) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000205 getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000206 } else {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000207 bool Fast = CodeGenOpts.OptimizationLevel == 0;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000208
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000209 // Create the TargetMachine for generating code.
Daniel Dunbar82cfa7a2009-07-26 01:27:26 +0000210 std::string Triple = TheModule->getTargetTriple();
Daniel Dunbar9ab76fa2009-08-03 04:21:41 +0000211 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
Daniel Dunbarf7d47c02009-07-15 20:25:38 +0000212 if (!TheTarget) {
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000213 Error = std::string("Unable to get target machine: ") + Error;
214 return false;
215 }
Daniel Dunbara034ba82009-02-17 19:47:34 +0000216
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000217 // FIXME: Expose these capabilities via actual APIs!!!! Aside from just
218 // being gross, this is also totally broken if we ever care about
219 // concurrency.
220 std::vector<const char *> BackendArgs;
221 BackendArgs.push_back("clang"); // Fake program name.
222 if (CodeGenOpts.AsmVerbose)
223 BackendArgs.push_back("-asm-verbose");
224 if (!CodeGenOpts.CodeModel.empty()) {
225 BackendArgs.push_back("-code-model");
226 BackendArgs.push_back(CodeGenOpts.CodeModel.c_str());
227 }
228 if (!CodeGenOpts.DebugPass.empty()) {
229 BackendArgs.push_back("-debug-pass");
230 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
231 }
232 if (CodeGenOpts.DisableFPElim)
233 BackendArgs.push_back("-disable-fp-elim");
234 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
235 BackendArgs.push_back("-limit-float-precision");
236 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
237 }
238 if (CodeGenOpts.NoZeroInitializedInBSS)
239 BackendArgs.push_back("-nozero-initialized-in-bss");
240 BackendArgs.push_back("-relocation-model");
241 BackendArgs.push_back(CodeGenOpts.RelocationModel.c_str());
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000242 if (llvm::TimePassesIsEnabled)
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000243 BackendArgs.push_back("-time-passes");
244 if (CodeGenOpts.UnwindTables)
245 BackendArgs.push_back("-unwind-tables");
246 BackendArgs.push_back(0);
247 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
248 (char**) &BackendArgs[0]);
249
Daniel Dunbara034ba82009-02-17 19:47:34 +0000250 std::string FeaturesStr;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000251 if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
Daniel Dunbara034ba82009-02-17 19:47:34 +0000252 SubtargetFeatures Features;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000253 Features.setCPU(TargetOpts.CPU);
Daniel Dunbar3636e1d2009-11-30 08:39:32 +0000254 for (std::vector<std::string>::const_iterator
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000255 it = TargetOpts.Features.begin(),
256 ie = TargetOpts.Features.end(); it != ie; ++it)
Daniel Dunbara034ba82009-02-17 19:47:34 +0000257 Features.AddFeature(*it);
258 FeaturesStr = Features.getString();
259 }
Daniel Dunbar2b1f59f2009-08-04 04:02:57 +0000260 TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000262 // Set register scheduler & allocation policy.
263 RegisterScheduler::setDefault(createDefaultScheduler);
Mike Stump1eb44332009-09-09 15:08:12 +0000264 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
265 createLinearScanRegisterAllocator);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000266
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000267 // From llvm-gcc:
268 // If there are passes we have to run on the entire module, we do codegen
269 // as a separate "pass" after that happens.
270 // FIXME: This is disabled right now until bugs can be worked out. Reenable
271 // this for fast -O0 compiles!
272 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000273 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
274
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000275 switch (CodeGenOpts.OptimizationLevel) {
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000276 default: break;
277 case 0: OptLevel = CodeGenOpt::None; break;
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000278 case 3: OptLevel = CodeGenOpt::Aggressive; break;
279 }
280
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000281 // Normal mode, emit a .s file by running the code generator.
282 // Note, this also adds codegenerator level optimization passes.
Chris Lattner03eacc72009-07-14 20:39:15 +0000283 switch (TM->addPassesToEmitFile(*PM, FormattedOutStream,
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000284 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000285 default:
286 case FileModel::Error:
287 Error = "Unable to interface with target machine!\n";
288 return false;
289 case FileModel::AsmFile:
290 break;
291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Duncan Sands813a2bb2009-05-31 04:09:57 +0000293 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
294 OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000295 Error = "Unable to interface with target machine!\n";
296 return false;
297 }
298 }
299
300 return true;
301}
302
303void BackendConsumer::CreatePasses() {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000304 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
305 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000306
307 // Handle disabling of LLVM optimization, where we want to preserve the
308 // internal module before any optimization.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000309 if (CodeGenOpts.DisableLLVMOpts) {
Daniel Dunbar8d353142009-11-10 17:50:53 +0000310 OptLevel = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000311 Inlining = CodeGenOpts.NoInlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000312 }
313
Daniel Dunbar70f92432008-10-23 05:50:47 +0000314 // In -O0 if checking is disabled, we don't even have per-function passes.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000315 if (CodeGenOpts.VerifyModule)
Daniel Dunbar70f92432008-10-23 05:50:47 +0000316 getPerFunctionPasses()->add(createVerifierPass());
317
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000318 // Assume that standard function passes aren't run for -O0.
Daniel Dunbar8d353142009-11-10 17:50:53 +0000319 if (OptLevel > 0)
320 llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000321
322 llvm::Pass *InliningPass = 0;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000323 switch (Inlining) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000324 case CodeGenOptions::NoInlining: break;
325 case CodeGenOptions::NormalInlining: {
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000326 // Inline small functions
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000327 unsigned Threshold = (CodeGenOpts.OptimizeSize || OptLevel < 3) ? 50 : 200;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000328 InliningPass = createFunctionInliningPass(Threshold);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000329 break;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000330 }
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000331 case CodeGenOptions::OnlyAlwaysInlining:
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000332 InliningPass = createAlwaysInlinerPass(); // Respect always_inline
333 break;
Daniel Dunbar70f92432008-10-23 05:50:47 +0000334 }
335
336 // For now we always create per module passes.
337 PassManager *PM = getPerModulePasses();
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000338 llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize,
339 CodeGenOpts.UnitAtATime,
340 CodeGenOpts.UnrollLoops,
Daniel Dunbar3636e1d2009-11-30 08:39:32 +0000341 /*SimplifyLibCalls=*/!LangOpts.NoBuiltin,
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000342 /*HaveExceptions=*/true,
343 InliningPass);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000344}
345
346/// EmitAssembly - Handle interaction with LLVM backend to generate
Mike Stump1eb44332009-09-09 15:08:12 +0000347/// actual machine code.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000348void BackendConsumer::EmitAssembly() {
349 // Silently ignore if we weren't initialized for some reason.
350 if (!TheModule || !TheTargetData)
351 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000353 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000354
Daniel Dunbard611bac2008-10-27 20:40:41 +0000355 // Make sure IR generation is happy with the module. This is
356 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000357 Module *M = Gen->ReleaseModule();
358 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000359 // The module has been released by IR gen on failures, do not
360 // double free.
361 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000362 TheModule = 0;
363 return;
364 }
365
366 assert(TheModule == M && "Unexpected module change during IR generation");
367
368 CreatePasses();
369
370 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000371 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000372 // FIXME: Don't fail this way.
Chris Lattnera85b3522009-08-23 05:57:09 +0000373 llvm::errs() << "ERROR: " << Error << "\n";
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000374 ::exit(1);
375 }
376
377 // Run passes. For now we do all passes at once, but eventually we
378 // would like to have the option of streaming code generation.
379
380 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000381 PrettyStackTraceString CrashInfo("Per-function optimization");
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000383 PerFunctionPasses->doInitialization();
384 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
385 if (!I->isDeclaration())
386 PerFunctionPasses->run(*I);
387 PerFunctionPasses->doFinalization();
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner49f28ca2009-03-05 08:00:35 +0000390 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000391 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000392 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000395 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000396 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000397 CodeGenPasses->doInitialization();
398 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
399 if (!I->isDeclaration())
400 CodeGenPasses->run(*I);
401 CodeGenPasses->doFinalization();
402 }
403}
404
405ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
406 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000407 const LangOptions &LangOpts,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000408 const CodeGenOptions &CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000409 const TargetOptions &TargetOpts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000410 bool TimePasses,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000411 const std::string& InFile,
Owen Anderson42253cc2009-07-01 17:00:06 +0000412 llvm::raw_ostream* OS,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000413 LLVMContext& C) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000414 return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000415 TargetOpts, TimePasses, InFile, OS, C);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000416}