blob: 1045f241bcdf1a0587b9b764100b50c4134a216c [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 Dunbar3be0d192009-12-03 09:12:54 +000018#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000019#include "llvm/Module.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000020#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 Dunbar821e2eb2009-12-12 23:01:36 +000034#include "llvm/Target/TargetOptions.h"
Daniel Dunbarf7d47c02009-07-15 20:25:38 +000035#include "llvm/Target/TargetRegistry.h"
Daniel Dunbard69bacc2008-10-21 23:49:24 +000036using namespace clang;
37using namespace llvm;
38
39namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000040 class BackendConsumer : public ASTConsumer {
Daniel Dunbar125bbbe2009-12-04 08:17:40 +000041 Diagnostic &Diags;
Daniel Dunbard69bacc2008-10-21 23:49:24 +000042 BackendAction Action;
Daniel Dunbar3636e1d2009-11-30 08:39:32 +000043 const CodeGenOptions &CodeGenOpts;
44 const LangOptions &LangOpts;
45 const 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
58 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
Daniel Dunbar125bbbe2009-12-04 08:17:40 +000068 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
Daniel Dunbard69bacc2008-10-21 23:49:24 +000069 ///
Daniel Dunbar3be0d192009-12-03 09:12:54 +000070 /// \return True on success.
71 bool AddEmitPasses();
Daniel Dunbard69bacc2008-10-21 23:49:24 +000072
73 void EmitAssembly();
Mike Stump1eb44332009-09-09 15:08:12 +000074
75 public:
Daniel Dunbar3be0d192009-12-03 09:12:54 +000076 BackendConsumer(BackendAction action, Diagnostic &_Diags,
Chandler Carruth2811ccf2009-11-12 17:24:48 +000077 const LangOptions &langopts, const CodeGenOptions &compopts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +000078 const TargetOptions &targetopts, bool TimePasses,
79 const std::string &infile, llvm::raw_ostream *OS,
80 LLVMContext& C) :
Daniel Dunbar3be0d192009-12-03 09:12:54 +000081 Diags(_Diags),
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)),
Jeffrey Yasskin80208062010-01-27 21:12:04 +000090 TheModule(0), TheTargetData(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;
Jeffrey Yasskin80208062010-01-27 21:12:04 +0000102 delete TheModule;
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();
Chris Lattner7bb0da02009-03-28 02:18:25 +0000117 TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000119 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000120 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner682bf922009-03-29 16:50:03 +0000123 virtual void HandleTopLevelDecl(DeclGroupRef D) {
124 PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
Chris Lattner49f28ca2009-03-05 08:00:35 +0000125 Context->getSourceManager(),
126 "LLVM IR generation of declaration");
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000128 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000129 LLVMIRGeneration.startTimer();
Chris Lattner682bf922009-03-29 16:50:03 +0000130
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000131 Gen->HandleTopLevelDecl(D);
Chris Lattner6f114eb2009-02-18 01:37:30 +0000132
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000133 if (llvm::TimePassesIsEnabled)
Chris Lattner6f114eb2009-02-18 01:37:30 +0000134 LLVMIRGeneration.stopTimer();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000135 }
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000137 virtual void HandleTranslationUnit(ASTContext &C) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000138 {
Chris Lattner14f234e2009-03-06 06:46:31 +0000139 PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000140 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000141 LLVMIRGeneration.startTimer();
Chris Lattner6f114eb2009-02-18 01:37:30 +0000142
Chris Lattnerdacbc5d2009-03-28 04:11:33 +0000143 Gen->HandleTranslationUnit(C);
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000144
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000145 if (llvm::TimePassesIsEnabled)
Chris Lattner49f28ca2009-03-05 08:00:35 +0000146 LLVMIRGeneration.stopTimer();
147 }
Chris Lattner6f114eb2009-02-18 01:37:30 +0000148
Chris Lattner49f28ca2009-03-05 08:00:35 +0000149 // EmitAssembly times and registers crash info itself.
Chris Lattner6f114eb2009-02-18 01:37:30 +0000150 EmitAssembly();
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Daniel Dunbard68ba0e2008-11-11 06:35:39 +0000152 // Force a flush here in case we never get released.
153 if (AsmOutStream)
Chris Lattner03eacc72009-07-14 20:39:15 +0000154 FormattedOutStream.flush();
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000157 virtual void HandleTagDeclDefinition(TagDecl *D) {
Chris Lattner49f28ca2009-03-05 08:00:35 +0000158 PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
159 Context->getSourceManager(),
160 "LLVM IR generation of declaration");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000161 Gen->HandleTagDeclDefinition(D);
162 }
Douglas Gregorb6c8c8b2009-04-21 17:11:58 +0000163
164 virtual void CompleteTentativeDefinition(VarDecl *D) {
165 Gen->CompleteTentativeDefinition(D);
166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167 };
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000168}
169
170FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
171 if (!CodeGenPasses) {
Jeffrey Yasskin80208062010-01-27 21:12:04 +0000172 CodeGenPasses = new FunctionPassManager(TheModule);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000173 CodeGenPasses->add(new TargetData(*TheTargetData));
174 }
175
176 return CodeGenPasses;
177}
178
179PassManager *BackendConsumer::getPerModulePasses() const {
180 if (!PerModulePasses) {
181 PerModulePasses = new PassManager();
182 PerModulePasses->add(new TargetData(*TheTargetData));
183 }
184
185 return PerModulePasses;
186}
187
188FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
189 if (!PerFunctionPasses) {
Jeffrey Yasskin80208062010-01-27 21:12:04 +0000190 PerFunctionPasses = new FunctionPassManager(TheModule);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000191 PerFunctionPasses->add(new TargetData(*TheTargetData));
192 }
193
194 return PerFunctionPasses;
195}
196
Daniel Dunbar3be0d192009-12-03 09:12:54 +0000197bool BackendConsumer::AddEmitPasses() {
Daniel Dunbare8e26002009-02-26 22:39:37 +0000198 if (Action == Backend_EmitNothing)
199 return true;
200
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000201 if (Action == Backend_EmitBC) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000202 getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000203 } else if (Action == Backend_EmitLL) {
Dan Gohmanb8d42392009-09-26 15:06:14 +0000204 getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream));
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000205 } else {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000206 bool Fast = CodeGenOpts.OptimizationLevel == 0;
Daniel Dunbar4c877cc2008-10-23 05:59:43 +0000207
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000208 // Create the TargetMachine for generating code.
Daniel Dunbar3be0d192009-12-03 09:12:54 +0000209 std::string Error;
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 Dunbar3be0d192009-12-03 09:12:54 +0000213 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000214 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.
Daniel Dunbar821e2eb2009-12-12 23:01:36 +0000220 llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim;
221 if (CodeGenOpts.FloatABI == "soft")
222 llvm::FloatABIType = llvm::FloatABI::Soft;
223 else if (CodeGenOpts.FloatABI == "hard")
224 llvm::FloatABIType = llvm::FloatABI::Hard;
225 else {
226 assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
227 llvm::FloatABIType = llvm::FloatABI::Default;
228 }
229 NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
230 llvm::UseSoftFloat = CodeGenOpts.SoftFloat;
231 UnwindTablesMandatory = CodeGenOpts.UnwindTables;
232
233 TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose);
234
235 // FIXME: Parse this earlier.
236 if (CodeGenOpts.RelocationModel == "static") {
237 TargetMachine::setRelocationModel(llvm::Reloc::Static);
238 } else if (CodeGenOpts.RelocationModel == "pic") {
239 TargetMachine::setRelocationModel(llvm::Reloc::PIC_);
240 } else {
241 assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
242 "Invalid PIC model!");
243 TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC);
244 }
245 // FIXME: Parse this earlier.
246 if (CodeGenOpts.CodeModel == "small") {
247 TargetMachine::setCodeModel(llvm::CodeModel::Small);
248 } else if (CodeGenOpts.CodeModel == "kernel") {
249 TargetMachine::setCodeModel(llvm::CodeModel::Kernel);
250 } else if (CodeGenOpts.CodeModel == "medium") {
251 TargetMachine::setCodeModel(llvm::CodeModel::Medium);
252 } else if (CodeGenOpts.CodeModel == "large") {
253 TargetMachine::setCodeModel(llvm::CodeModel::Large);
254 } else {
255 assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");
256 TargetMachine::setCodeModel(llvm::CodeModel::Default);
257 }
258
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000259 std::vector<const char *> BackendArgs;
260 BackendArgs.push_back("clang"); // Fake program name.
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000261 if (!CodeGenOpts.DebugPass.empty()) {
262 BackendArgs.push_back("-debug-pass");
263 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
264 }
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000265 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
266 BackendArgs.push_back("-limit-float-precision");
267 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
268 }
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000269 if (llvm::TimePassesIsEnabled)
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000270 BackendArgs.push_back("-time-passes");
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000271 BackendArgs.push_back(0);
272 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
273 (char**) &BackendArgs[0]);
274
Daniel Dunbara034ba82009-02-17 19:47:34 +0000275 std::string FeaturesStr;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000276 if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
Daniel Dunbara034ba82009-02-17 19:47:34 +0000277 SubtargetFeatures Features;
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000278 Features.setCPU(TargetOpts.CPU);
Daniel Dunbar3636e1d2009-11-30 08:39:32 +0000279 for (std::vector<std::string>::const_iterator
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000280 it = TargetOpts.Features.begin(),
281 ie = TargetOpts.Features.end(); it != ie; ++it)
Daniel Dunbara034ba82009-02-17 19:47:34 +0000282 Features.AddFeature(*it);
283 FeaturesStr = Features.getString();
284 }
Daniel Dunbar2b1f59f2009-08-04 04:02:57 +0000285 TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000287 // Set register scheduler & allocation policy.
288 RegisterScheduler::setDefault(createDefaultScheduler);
Mike Stump1eb44332009-09-09 15:08:12 +0000289 RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
290 createLinearScanRegisterAllocator);
Daniel Dunbar8b7650e2008-10-22 18:29:51 +0000291
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000292 // From llvm-gcc:
293 // If there are passes we have to run on the entire module, we do codegen
294 // as a separate "pass" after that happens.
295 // FIXME: This is disabled right now until bugs can be worked out. Reenable
296 // this for fast -O0 compiles!
297 FunctionPassManager *PM = getCodeGenPasses();
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000298 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
299
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000300 switch (CodeGenOpts.OptimizationLevel) {
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000301 default: break;
302 case 0: OptLevel = CodeGenOpt::None; break;
Bill Wendling6e9b8f62009-04-29 23:53:23 +0000303 case 3: OptLevel = CodeGenOpt::Aggressive; break;
304 }
305
Daniel Dunbarda1573f2010-02-03 01:18:43 +0000306 // Normal mode, emit a .s or .o file by running the code generator. Note,
307 // this also adds codegenerator level optimization passes.
308 TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
309 if (Action == Backend_EmitObj)
310 CGFT = TargetMachine::CGFT_ObjectFile;
Chris Lattnercd0507c2010-02-03 05:55:22 +0000311 if (TM->addPassesToEmitFile(*PM, FormattedOutStream, CGFT, OptLevel)) {
Daniel Dunbar3be0d192009-12-03 09:12:54 +0000312 Diags.Report(diag::err_fe_unable_to_interface_with_target);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000313 return false;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000314 }
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000315 }
316
317 return true;
318}
319
320void BackendConsumer::CreatePasses() {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000321 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
322 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000323
324 // Handle disabling of LLVM optimization, where we want to preserve the
325 // internal module before any optimization.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000326 if (CodeGenOpts.DisableLLVMOpts) {
Daniel Dunbar8d353142009-11-10 17:50:53 +0000327 OptLevel = 0;
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000328 Inlining = CodeGenOpts.NoInlining;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000329 }
330
Daniel Dunbar70f92432008-10-23 05:50:47 +0000331 // In -O0 if checking is disabled, we don't even have per-function passes.
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000332 if (CodeGenOpts.VerifyModule)
Daniel Dunbar70f92432008-10-23 05:50:47 +0000333 getPerFunctionPasses()->add(createVerifierPass());
334
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000335 // Assume that standard function passes aren't run for -O0.
Daniel Dunbar8d353142009-11-10 17:50:53 +0000336 if (OptLevel > 0)
337 llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000338
339 llvm::Pass *InliningPass = 0;
Daniel Dunbar8d353142009-11-10 17:50:53 +0000340 switch (Inlining) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000341 case CodeGenOptions::NoInlining: break;
342 case CodeGenOptions::NormalInlining: {
Daniel Dunbar90de51f2009-12-08 23:15:55 +0000343 // Set the inline threshold following llvm-gcc.
344 //
345 // FIXME: Derive these constants in a principled fashion.
346 unsigned Threshold = 200;
347 if (CodeGenOpts.OptimizeSize)
348 Threshold = 50;
349 else if (OptLevel > 2)
350 Threshold = 250;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000351 InliningPass = createFunctionInliningPass(Threshold);
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000352 break;
Eli Friedmanb9b7dd62009-06-11 20:33:41 +0000353 }
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000354 case CodeGenOptions::OnlyAlwaysInlining:
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000355 InliningPass = createAlwaysInlinerPass(); // Respect always_inline
356 break;
Daniel Dunbar70f92432008-10-23 05:50:47 +0000357 }
358
359 // For now we always create per module passes.
360 PassManager *PM = getPerModulePasses();
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000361 llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize,
362 CodeGenOpts.UnitAtATime,
363 CodeGenOpts.UnrollLoops,
Daniel Dunbar3636e1d2009-11-30 08:39:32 +0000364 /*SimplifyLibCalls=*/!LangOpts.NoBuiltin,
Daniel Dunbar10d861e2009-06-03 18:01:18 +0000365 /*HaveExceptions=*/true,
366 InliningPass);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000367}
368
369/// EmitAssembly - Handle interaction with LLVM backend to generate
Mike Stump1eb44332009-09-09 15:08:12 +0000370/// actual machine code.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000371void BackendConsumer::EmitAssembly() {
372 // Silently ignore if we weren't initialized for some reason.
373 if (!TheModule || !TheTargetData)
374 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000376 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000377
Daniel Dunbard611bac2008-10-27 20:40:41 +0000378 // Make sure IR generation is happy with the module. This is
379 // released by the module provider.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000380 Module *M = Gen->ReleaseModule();
381 if (!M) {
Daniel Dunbard611bac2008-10-27 20:40:41 +0000382 // The module has been released by IR gen on failures, do not
383 // double free.
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000384 TheModule = 0;
385 return;
386 }
387
388 assert(TheModule == M && "Unexpected module change during IR generation");
389
390 CreatePasses();
Daniel Dunbar3be0d192009-12-03 09:12:54 +0000391 if (!AddEmitPasses())
392 return;
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000393
394 // Run passes. For now we do all passes at once, but eventually we
395 // would like to have the option of streaming code generation.
396
397 if (PerFunctionPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000398 PrettyStackTraceString CrashInfo("Per-function optimization");
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000400 PerFunctionPasses->doInitialization();
401 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
402 if (!I->isDeclaration())
403 PerFunctionPasses->run(*I);
404 PerFunctionPasses->doFinalization();
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Chris Lattner49f28ca2009-03-05 08:00:35 +0000407 if (PerModulePasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000408 PrettyStackTraceString CrashInfo("Per-module optimization passes");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000409 PerModulePasses->run(*M);
Chris Lattner49f28ca2009-03-05 08:00:35 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000412 if (CodeGenPasses) {
Chris Lattner14f234e2009-03-06 06:46:31 +0000413 PrettyStackTraceString CrashInfo("Code generation");
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000414 CodeGenPasses->doInitialization();
415 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
416 if (!I->isDeclaration())
417 CodeGenPasses->run(*I);
418 CodeGenPasses->doFinalization();
419 }
420}
421
422ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
423 Diagnostic &Diags,
Daniel Dunbara034ba82009-02-17 19:47:34 +0000424 const LangOptions &LangOpts,
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000425 const CodeGenOptions &CodeGenOpts,
Daniel Dunbard58c03f2009-11-15 06:48:46 +0000426 const TargetOptions &TargetOpts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000427 bool TimePasses,
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000428 const std::string& InFile,
Owen Anderson42253cc2009-07-01 17:00:06 +0000429 llvm::raw_ostream* OS,
Owen Anderson8f1ca782009-07-01 23:14:14 +0000430 LLVMContext& C) {
Chandler Carruth2811ccf2009-11-12 17:24:48 +0000431 return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
Daniel Dunbarb33fbaa2009-11-30 08:39:52 +0000432 TargetOpts, TimePasses, InFile, OS, C);
Daniel Dunbard69bacc2008-10-21 23:49:24 +0000433}