blob: 9dc109da65ef83bc016823a97b63709673ab5107 [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");
Daniel Dunbar3b315262009-11-30 08:42:00 +0000234 if (!CodeGenOpts.FloatABI.empty()) {
235 BackendArgs.push_back("-float-abi");
236 BackendArgs.push_back(CodeGenOpts.FloatABI.c_str());
237 }
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000238 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
239 BackendArgs.push_back("-limit-float-precision");
240 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
241 }
242 if (CodeGenOpts.NoZeroInitializedInBSS)
243 BackendArgs.push_back("-nozero-initialized-in-bss");
Daniel Dunbar3b315262009-11-30 08:42:00 +0000244 if (CodeGenOpts.SoftFloat)
245 BackendArgs.push_back("-soft-float");
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000246 BackendArgs.push_back("-relocation-model");
247 BackendArgs.push_back(CodeGenOpts.RelocationModel.c_str());
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000248 if (llvm::TimePassesIsEnabled)
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000249 BackendArgs.push_back("-time-passes");
250 if (CodeGenOpts.UnwindTables)
251 BackendArgs.push_back("-unwind-tables");
252 BackendArgs.push_back(0);
253 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
254 (char**) &BackendArgs[0]);
255
Daniel Dunbara034ba82009-02-17 19:47:34 +0000256 std::string FeaturesStr;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000257 if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
Daniel Dunbara034ba82009-02-17 19:47:34 +0000258 SubtargetFeatures Features;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000259 Features.setCPU(TargetOpts.CPU);
Daniel Dunbar3636e1d2009-11-30 08:39:32 +0000260 for (std::vector<std::string>::const_iterator
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000261 it = TargetOpts.Features.begin(),
262 ie = TargetOpts.Features.end(); it != ie; ++it)
Daniel Dunbara034ba82009-02-17 19:47:34 +0000263 Features.AddFeature(*it);
264 FeaturesStr = Features.getString();
265 }
Daniel Dunbar2b1f59f2009-08-04 04:02:57 +0000266 TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000268 // Set register scheduler & allocation policy.
269 RegisterScheduler::setDefault(createDefaultScheduler);
Mike Stump1eb44332009-09-09 15:08:12 +0000270 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
271 createLinearScanRegisterAllocator);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000272
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000273 // From llvm-gcc:
274 // If there are passes we have to run on the entire module, we do codegen
275 // as a separate "pass" after that happens.
276 // FIXME: This is disabled right now until bugs can be worked out. Reenable
277 // this for fast -O0 compiles!
278 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000279 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
280
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000281 switch (CodeGenOpts.OptimizationLevel) {
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000282 default: break;
283 case 0: OptLevel = CodeGenOpt::None; break;
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000284 case 3: OptLevel = CodeGenOpt::Aggressive; break;
285 }
286
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000287 // Normal mode, emit a .s file by running the code generator.
288 // Note, this also adds codegenerator level optimization passes.
Chris Lattner03eacc72009-07-14 20:39:15 +0000289 switch (TM->addPassesToEmitFile(*PM, FormattedOutStream,
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000290 TargetMachine::AssemblyFile, OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000291 default:
292 case FileModel::Error:
293 Error = "Unable to interface with target machine!\n";
294 return false;
295 case FileModel::AsmFile:
296 break;
297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Duncan Sands813a2bb2009-05-31 04:09:57 +0000299 if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
300 OptLevel)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000301 Error = "Unable to interface with target machine!\n";
302 return false;
303 }
304 }
305
306 return true;
307}
308
309void BackendConsumer::CreatePasses() {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000310 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
311 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000312
313 // Handle disabling of LLVM optimization, where we want to preserve the
314 // internal module before any optimization.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000315 if (CodeGenOpts.DisableLLVMOpts) {
Daniel Dunbar8d353142009-11-10 17:50:53 +0000316 OptLevel = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000317 Inlining = CodeGenOpts.NoInlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000318 }
319
Daniel Dunbar70f92432008-10-23 05:50:47 +0000320 // In -O0 if checking is disabled, we don't even have per-function passes.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000321 if (CodeGenOpts.VerifyModule)
Daniel Dunbar70f92432008-10-23 05:50:47 +0000322 getPerFunctionPasses()->add(createVerifierPass());
323
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000324 // Assume that standard function passes aren't run for -O0.
Daniel Dunbar8d353142009-11-10 17:50:53 +0000325 if (OptLevel > 0)
326 llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000327
328 llvm::Pass *InliningPass = 0;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000329 switch (Inlining) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000330 case CodeGenOptions::NoInlining: break;
331 case CodeGenOptions::NormalInlining: {
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000332 // Inline small functions
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000333 unsigned Threshold = (CodeGenOpts.OptimizeSize || OptLevel < 3) ? 50 : 200;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000334 InliningPass = createFunctionInliningPass(Threshold);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000335 break;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000336 }
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000337 case CodeGenOptions::OnlyAlwaysInlining:
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000338 InliningPass = createAlwaysInlinerPass(); // Respect always_inline
339 break;
Daniel Dunbar70f92432008-10-23 05:50:47 +0000340 }
341
342 // For now we always create per module passes.
343 PassManager *PM = getPerModulePasses();
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000344 llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize,
345 CodeGenOpts.UnitAtATime,
346 CodeGenOpts.UnrollLoops,
Daniel Dunbar3636e1d2009-11-30 08:39:32 +0000347 /*SimplifyLibCalls=*/!LangOpts.NoBuiltin,
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000348 /*HaveExceptions=*/true,
349 InliningPass);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000350}
351
352/// EmitAssembly - Handle interaction with LLVM backend to generate
Mike Stump1eb44332009-09-09 15:08:12 +0000353/// actual machine code.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000354void BackendConsumer::EmitAssembly() {
355 // Silently ignore if we weren't initialized for some reason.
356 if (!TheModule || !TheTargetData)
357 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000359 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000360
Daniel Dunbard611bac2008-10-27 20:40:41 +0000361 // Make sure IR generation is happy with the module. This is
362 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000363 Module *M = Gen->ReleaseModule();
364 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000365 // The module has been released by IR gen on failures, do not
366 // double free.
367 ModuleProvider->releaseModule();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000368 TheModule = 0;
369 return;
370 }
371
372 assert(TheModule == M && "Unexpected module change during IR generation");
373
374 CreatePasses();
375
376 std::string Error;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000377 if (!AddEmitPasses(Error)) {
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000378 // FIXME: Don't fail this way.
Chris Lattnera85b3522009-08-23 05:57:09 +0000379 llvm::errs() << "ERROR: " << Error << "\n";
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000380 ::exit(1);
381 }
382
383 // Run passes. For now we do all passes at once, but eventually we
384 // would like to have the option of streaming code generation.
385
386 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000387 PrettyStackTraceString CrashInfo("Per-function optimization");
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000389 PerFunctionPasses->doInitialization();
390 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
391 if (!I->isDeclaration())
392 PerFunctionPasses->run(*I);
393 PerFunctionPasses->doFinalization();
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattner49f28ca2009-03-05 08:00:35 +0000396 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000397 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000398 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000401 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000402 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000403 CodeGenPasses->doInitialization();
404 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
405 if (!I->isDeclaration())
406 CodeGenPasses->run(*I);
407 CodeGenPasses->doFinalization();
408 }
409}
410
411ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
412 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000413 const LangOptions &LangOpts,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000414 const CodeGenOptions &CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000415 const TargetOptions &TargetOpts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000416 bool TimePasses,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000417 const std::string& InFile,
Owen Anderson42253cc2009-07-01 17:00:06 +0000418 llvm::raw_ostream* OS,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000419 LLVMContext& C) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000420 return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000421 TargetOpts, TimePasses, InFile, OS, C);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000422}