2008-10-22 07:49:24 +08:00
|
|
|
//===--- Backend.cpp - Interface to LLVM backend technologies -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 06:50:54 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2009-03-30 00:50:03 +08:00
|
|
|
#include "clang/AST/DeclGroup.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
|
|
|
#include "clang/CodeGen/CodeGenOptions.h"
|
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
2009-12-03 17:12:54 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/ModuleProvider.h"
|
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2008-10-23 13:50:47 +08:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2009-07-15 04:39:15 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2009-06-04 02:01:18 +08:00
|
|
|
#include "llvm/Support/StandardPasses.h"
|
2009-02-18 09:37:30 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2009-02-18 03:47:34 +08:00
|
|
|
#include "llvm/Target/SubtargetFeature.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-12-13 07:01:36 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2009-07-16 04:25:38 +08:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 18:07:24 +08:00
|
|
|
class BackendConsumer : public ASTConsumer {
|
2009-12-04 16:17:40 +08:00
|
|
|
Diagnostic &Diags;
|
2008-10-22 07:49:24 +08:00
|
|
|
BackendAction Action;
|
2009-11-30 16:39:32 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
|
|
|
const LangOptions &LangOpts;
|
|
|
|
const TargetOptions &TargetOpts;
|
2009-05-19 06:20:00 +08:00
|
|
|
llvm::raw_ostream *AsmOutStream;
|
2009-07-15 04:39:15 +08:00
|
|
|
llvm::formatted_raw_ostream FormattedOutStream;
|
2009-03-05 16:00:35 +08:00
|
|
|
ASTContext *Context;
|
2008-10-29 16:50:02 +08:00
|
|
|
|
2009-02-18 09:37:30 +08:00
|
|
|
Timer LLVMIRGeneration;
|
|
|
|
Timer CodeGenerationTime;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
llvm::OwningPtr<CodeGenerator> Gen;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
llvm::Module *TheModule;
|
|
|
|
llvm::TargetData *TheTargetData;
|
|
|
|
|
2008-10-25 06:51:00 +08:00
|
|
|
mutable llvm::ModuleProvider *ModuleProvider;
|
2008-10-22 07:49:24 +08:00
|
|
|
mutable FunctionPassManager *CodeGenPasses;
|
|
|
|
mutable PassManager *PerModulePasses;
|
|
|
|
mutable FunctionPassManager *PerFunctionPasses;
|
|
|
|
|
|
|
|
FunctionPassManager *getCodeGenPasses() const;
|
|
|
|
PassManager *getPerModulePasses() const;
|
|
|
|
FunctionPassManager *getPerFunctionPasses() const;
|
|
|
|
|
|
|
|
void CreatePasses();
|
|
|
|
|
2009-12-04 16:17:40 +08:00
|
|
|
/// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
|
2008-10-22 07:49:24 +08:00
|
|
|
///
|
2009-12-03 17:12:54 +08:00
|
|
|
/// \return True on success.
|
|
|
|
bool AddEmitPasses();
|
2008-10-22 07:49:24 +08:00
|
|
|
|
|
|
|
void EmitAssembly();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
public:
|
2009-12-03 17:12:54 +08:00
|
|
|
BackendConsumer(BackendAction action, Diagnostic &_Diags,
|
2009-11-13 01:24:48 +08:00
|
|
|
const LangOptions &langopts, const CodeGenOptions &compopts,
|
2009-11-30 16:39:52 +08:00
|
|
|
const TargetOptions &targetopts, bool TimePasses,
|
|
|
|
const std::string &infile, llvm::raw_ostream *OS,
|
|
|
|
LLVMContext& C) :
|
2009-12-03 17:12:54 +08:00
|
|
|
Diags(_Diags),
|
2009-09-09 23:08:12 +08:00
|
|
|
Action(action),
|
2009-11-13 01:24:48 +08:00
|
|
|
CodeGenOpts(compopts),
|
2009-11-30 16:39:32 +08:00
|
|
|
LangOpts(langopts),
|
2009-11-15 14:48:46 +08:00
|
|
|
TargetOpts(targetopts),
|
2009-07-15 04:39:15 +08:00
|
|
|
AsmOutStream(OS),
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration("LLVM IR Generation Time"),
|
|
|
|
CodeGenerationTime("Code Generation Time"),
|
2009-07-02 01:00:06 +08:00
|
|
|
Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)),
|
2009-05-19 06:20:00 +08:00
|
|
|
TheModule(0), TheTargetData(0), ModuleProvider(0),
|
2009-02-18 09:23:44 +08:00
|
|
|
CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-15 04:39:15 +08:00
|
|
|
if (AsmOutStream)
|
|
|
|
FormattedOutStream.setStream(*AsmOutStream,
|
|
|
|
formatted_raw_ostream::PRESERVE_STREAM);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
llvm::TimePassesIsEnabled = TimePasses;
|
2009-02-18 09:23:44 +08:00
|
|
|
}
|
2008-10-22 07:49:24 +08:00
|
|
|
|
|
|
|
~BackendConsumer() {
|
|
|
|
delete TheTargetData;
|
2008-10-25 06:51:00 +08:00
|
|
|
delete ModuleProvider;
|
2008-10-22 07:49:24 +08:00
|
|
|
delete CodeGenPasses;
|
|
|
|
delete PerModulePasses;
|
|
|
|
delete PerFunctionPasses;
|
|
|
|
}
|
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
virtual void Initialize(ASTContext &Ctx) {
|
|
|
|
Context = &Ctx;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
Gen->Initialize(Ctx);
|
2008-10-22 07:49:24 +08:00
|
|
|
|
|
|
|
TheModule = Gen->GetModule();
|
2008-10-25 07:27:18 +08:00
|
|
|
ModuleProvider = new ExistingModuleProvider(TheModule);
|
2009-03-28 10:18:25 +08:00
|
|
|
TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-30 00:50:03 +08:00
|
|
|
virtual void HandleTopLevelDecl(DeclGroupRef D) {
|
|
|
|
PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
|
2009-03-05 16:00:35 +08:00
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-03-30 00:50:03 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTopLevelDecl(D);
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-02-18 09:37:30 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &C) {
|
2009-03-05 16:00:35 +08:00
|
|
|
{
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-03-05 16:00:35 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
Gen->HandleTranslationUnit(C);
|
2008-11-11 14:35:39 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-03-05 16:00:35 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-03-05 16:00:35 +08:00
|
|
|
// EmitAssembly times and registers crash info itself.
|
2009-02-18 09:37:30 +08:00
|
|
|
EmitAssembly();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-11 14:35:39 +08:00
|
|
|
// Force a flush here in case we never get released.
|
|
|
|
if (AsmOutStream)
|
2009-07-15 04:39:15 +08:00
|
|
|
FormattedOutStream.flush();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
virtual void HandleTagDeclDefinition(TagDecl *D) {
|
2009-03-05 16:00:35 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTagDeclDefinition(D);
|
|
|
|
}
|
2009-04-22 01:11:58 +08:00
|
|
|
|
|
|
|
virtual void CompleteTentativeDefinition(VarDecl *D) {
|
|
|
|
Gen->CompleteTentativeDefinition(D);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPassManager *BackendConsumer::getCodeGenPasses() const {
|
|
|
|
if (!CodeGenPasses) {
|
2008-10-25 06:51:00 +08:00
|
|
|
CodeGenPasses = new FunctionPassManager(ModuleProvider);
|
2008-10-22 07:49:24 +08:00
|
|
|
CodeGenPasses->add(new TargetData(*TheTargetData));
|
|
|
|
}
|
|
|
|
|
|
|
|
return CodeGenPasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
PassManager *BackendConsumer::getPerModulePasses() const {
|
|
|
|
if (!PerModulePasses) {
|
|
|
|
PerModulePasses = new PassManager();
|
|
|
|
PerModulePasses->add(new TargetData(*TheTargetData));
|
|
|
|
}
|
|
|
|
|
|
|
|
return PerModulePasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPassManager *BackendConsumer::getPerFunctionPasses() const {
|
|
|
|
if (!PerFunctionPasses) {
|
2008-10-25 07:27:18 +08:00
|
|
|
PerFunctionPasses = new FunctionPassManager(ModuleProvider);
|
2008-10-22 07:49:24 +08:00
|
|
|
PerFunctionPasses->add(new TargetData(*TheTargetData));
|
|
|
|
}
|
|
|
|
|
|
|
|
return PerFunctionPasses;
|
|
|
|
}
|
|
|
|
|
2009-12-03 17:12:54 +08:00
|
|
|
bool BackendConsumer::AddEmitPasses() {
|
2009-02-27 06:39:37 +08:00
|
|
|
if (Action == Backend_EmitNothing)
|
|
|
|
return true;
|
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
if (Action == Backend_EmitBC) {
|
2009-09-26 23:06:14 +08:00
|
|
|
getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream));
|
2008-10-22 07:49:24 +08:00
|
|
|
} else if (Action == Backend_EmitLL) {
|
2009-09-26 23:06:14 +08:00
|
|
|
getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream));
|
2008-10-22 07:49:24 +08:00
|
|
|
} else {
|
2009-11-13 01:24:48 +08:00
|
|
|
bool Fast = CodeGenOpts.OptimizationLevel == 0;
|
2008-10-23 13:59:43 +08:00
|
|
|
|
2008-10-23 02:29:51 +08:00
|
|
|
// Create the TargetMachine for generating code.
|
2009-12-03 17:12:54 +08:00
|
|
|
std::string Error;
|
2009-07-26 09:27:26 +08:00
|
|
|
std::string Triple = TheModule->getTargetTriple();
|
2009-08-03 12:21:41 +08:00
|
|
|
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
|
2009-07-16 04:25:38 +08:00
|
|
|
if (!TheTarget) {
|
2009-12-03 17:12:54 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_create_target) << Error;
|
2008-10-23 02:29:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-02-18 03:47:34 +08:00
|
|
|
|
2009-11-29 15:18:39 +08:00
|
|
|
// FIXME: Expose these capabilities via actual APIs!!!! Aside from just
|
|
|
|
// being gross, this is also totally broken if we ever care about
|
|
|
|
// concurrency.
|
2009-12-13 07:01:36 +08:00
|
|
|
llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim;
|
|
|
|
if (CodeGenOpts.FloatABI == "soft")
|
|
|
|
llvm::FloatABIType = llvm::FloatABI::Soft;
|
|
|
|
else if (CodeGenOpts.FloatABI == "hard")
|
|
|
|
llvm::FloatABIType = llvm::FloatABI::Hard;
|
|
|
|
else {
|
|
|
|
assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
|
|
|
|
llvm::FloatABIType = llvm::FloatABI::Default;
|
|
|
|
}
|
|
|
|
NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
|
|
|
|
llvm::UseSoftFloat = CodeGenOpts.SoftFloat;
|
|
|
|
UnwindTablesMandatory = CodeGenOpts.UnwindTables;
|
|
|
|
|
|
|
|
TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose);
|
|
|
|
|
|
|
|
// FIXME: Parse this earlier.
|
|
|
|
if (CodeGenOpts.RelocationModel == "static") {
|
|
|
|
TargetMachine::setRelocationModel(llvm::Reloc::Static);
|
|
|
|
} else if (CodeGenOpts.RelocationModel == "pic") {
|
|
|
|
TargetMachine::setRelocationModel(llvm::Reloc::PIC_);
|
|
|
|
} else {
|
|
|
|
assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
|
|
|
|
"Invalid PIC model!");
|
|
|
|
TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC);
|
|
|
|
}
|
|
|
|
// FIXME: Parse this earlier.
|
|
|
|
if (CodeGenOpts.CodeModel == "small") {
|
|
|
|
TargetMachine::setCodeModel(llvm::CodeModel::Small);
|
|
|
|
} else if (CodeGenOpts.CodeModel == "kernel") {
|
|
|
|
TargetMachine::setCodeModel(llvm::CodeModel::Kernel);
|
|
|
|
} else if (CodeGenOpts.CodeModel == "medium") {
|
|
|
|
TargetMachine::setCodeModel(llvm::CodeModel::Medium);
|
|
|
|
} else if (CodeGenOpts.CodeModel == "large") {
|
|
|
|
TargetMachine::setCodeModel(llvm::CodeModel::Large);
|
|
|
|
} else {
|
|
|
|
assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");
|
|
|
|
TargetMachine::setCodeModel(llvm::CodeModel::Default);
|
|
|
|
}
|
|
|
|
|
2009-11-29 15:18:39 +08:00
|
|
|
std::vector<const char *> BackendArgs;
|
|
|
|
BackendArgs.push_back("clang"); // Fake program name.
|
|
|
|
if (!CodeGenOpts.DebugPass.empty()) {
|
|
|
|
BackendArgs.push_back("-debug-pass");
|
|
|
|
BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
|
|
|
|
}
|
|
|
|
if (!CodeGenOpts.LimitFloatPrecision.empty()) {
|
|
|
|
BackendArgs.push_back("-limit-float-precision");
|
|
|
|
BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
|
|
|
|
}
|
2009-11-30 16:39:52 +08:00
|
|
|
if (llvm::TimePassesIsEnabled)
|
2009-11-29 15:18:39 +08:00
|
|
|
BackendArgs.push_back("-time-passes");
|
|
|
|
BackendArgs.push_back(0);
|
|
|
|
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
|
|
|
|
(char**) &BackendArgs[0]);
|
|
|
|
|
2009-02-18 03:47:34 +08:00
|
|
|
std::string FeaturesStr;
|
2009-11-15 14:48:46 +08:00
|
|
|
if (TargetOpts.CPU.size() || TargetOpts.Features.size()) {
|
2009-02-18 03:47:34 +08:00
|
|
|
SubtargetFeatures Features;
|
2009-11-15 14:48:46 +08:00
|
|
|
Features.setCPU(TargetOpts.CPU);
|
2009-11-30 16:39:32 +08:00
|
|
|
for (std::vector<std::string>::const_iterator
|
2009-11-15 14:48:46 +08:00
|
|
|
it = TargetOpts.Features.begin(),
|
|
|
|
ie = TargetOpts.Features.end(); it != ie; ++it)
|
2009-02-18 03:47:34 +08:00
|
|
|
Features.AddFeature(*it);
|
|
|
|
FeaturesStr = Features.getString();
|
|
|
|
}
|
2009-08-04 12:02:57 +08:00
|
|
|
TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-23 02:29:51 +08:00
|
|
|
// Set register scheduler & allocation policy.
|
|
|
|
RegisterScheduler::setDefault(createDefaultScheduler);
|
2009-09-09 23:08:12 +08:00
|
|
|
RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator :
|
|
|
|
createLinearScanRegisterAllocator);
|
2008-10-23 02:29:51 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
// From llvm-gcc:
|
|
|
|
// If there are passes we have to run on the entire module, we do codegen
|
|
|
|
// as a separate "pass" after that happens.
|
|
|
|
// FIXME: This is disabled right now until bugs can be worked out. Reenable
|
|
|
|
// this for fast -O0 compiles!
|
|
|
|
FunctionPassManager *PM = getCodeGenPasses();
|
2009-04-30 07:53:23 +08:00
|
|
|
CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
|
|
|
|
|
2009-11-13 01:24:48 +08:00
|
|
|
switch (CodeGenOpts.OptimizationLevel) {
|
2009-04-30 07:53:23 +08:00
|
|
|
default: break;
|
|
|
|
case 0: OptLevel = CodeGenOpt::None; break;
|
|
|
|
case 3: OptLevel = CodeGenOpt::Aggressive; break;
|
|
|
|
}
|
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
// Normal mode, emit a .s file by running the code generator.
|
|
|
|
// Note, this also adds codegenerator level optimization passes.
|
2009-07-15 04:39:15 +08:00
|
|
|
switch (TM->addPassesToEmitFile(*PM, FormattedOutStream,
|
2009-04-30 07:53:23 +08:00
|
|
|
TargetMachine::AssemblyFile, OptLevel)) {
|
2008-10-22 07:49:24 +08:00
|
|
|
default:
|
|
|
|
case FileModel::Error:
|
2009-12-03 17:12:54 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_interface_with_target);
|
2008-10-22 07:49:24 +08:00
|
|
|
return false;
|
|
|
|
case FileModel::AsmFile:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-31 12:09:57 +08:00
|
|
|
if (TM->addPassesToEmitFileFinish(*CodeGenPasses, (MachineCodeEmitter *)0,
|
|
|
|
OptLevel)) {
|
2009-12-03 17:12:54 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_interface_with_target);
|
2008-10-22 07:49:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::CreatePasses() {
|
2009-11-13 01:24:48 +08:00
|
|
|
unsigned OptLevel = CodeGenOpts.OptimizationLevel;
|
|
|
|
CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining;
|
2009-11-11 01:50:53 +08:00
|
|
|
|
|
|
|
// Handle disabling of LLVM optimization, where we want to preserve the
|
|
|
|
// internal module before any optimization.
|
2009-11-13 01:24:48 +08:00
|
|
|
if (CodeGenOpts.DisableLLVMOpts) {
|
2009-11-11 01:50:53 +08:00
|
|
|
OptLevel = 0;
|
2009-11-13 01:24:48 +08:00
|
|
|
Inlining = CodeGenOpts.NoInlining;
|
2009-11-11 01:50:53 +08:00
|
|
|
}
|
|
|
|
|
2008-10-23 13:50:47 +08:00
|
|
|
// In -O0 if checking is disabled, we don't even have per-function passes.
|
2009-11-13 01:24:48 +08:00
|
|
|
if (CodeGenOpts.VerifyModule)
|
2008-10-23 13:50:47 +08:00
|
|
|
getPerFunctionPasses()->add(createVerifierPass());
|
|
|
|
|
2009-06-04 02:01:18 +08:00
|
|
|
// Assume that standard function passes aren't run for -O0.
|
2009-11-11 01:50:53 +08:00
|
|
|
if (OptLevel > 0)
|
|
|
|
llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel);
|
2009-06-04 02:01:18 +08:00
|
|
|
|
|
|
|
llvm::Pass *InliningPass = 0;
|
2009-11-11 01:50:53 +08:00
|
|
|
switch (Inlining) {
|
2009-11-13 01:24:48 +08:00
|
|
|
case CodeGenOptions::NoInlining: break;
|
|
|
|
case CodeGenOptions::NormalInlining: {
|
2009-12-09 07:15:55 +08:00
|
|
|
// Set the inline threshold following llvm-gcc.
|
|
|
|
//
|
|
|
|
// FIXME: Derive these constants in a principled fashion.
|
|
|
|
unsigned Threshold = 200;
|
|
|
|
if (CodeGenOpts.OptimizeSize)
|
|
|
|
Threshold = 50;
|
|
|
|
else if (OptLevel > 2)
|
|
|
|
Threshold = 250;
|
2009-06-12 04:33:41 +08:00
|
|
|
InliningPass = createFunctionInliningPass(Threshold);
|
2009-06-04 02:01:18 +08:00
|
|
|
break;
|
2009-06-12 04:33:41 +08:00
|
|
|
}
|
2009-11-13 01:24:48 +08:00
|
|
|
case CodeGenOptions::OnlyAlwaysInlining:
|
2009-06-04 02:01:18 +08:00
|
|
|
InliningPass = createAlwaysInlinerPass(); // Respect always_inline
|
|
|
|
break;
|
2008-10-23 13:50:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For now we always create per module passes.
|
|
|
|
PassManager *PM = getPerModulePasses();
|
2009-11-13 01:24:48 +08:00
|
|
|
llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize,
|
|
|
|
CodeGenOpts.UnitAtATime,
|
|
|
|
CodeGenOpts.UnrollLoops,
|
2009-11-30 16:39:32 +08:00
|
|
|
/*SimplifyLibCalls=*/!LangOpts.NoBuiltin,
|
2009-06-04 02:01:18 +08:00
|
|
|
/*HaveExceptions=*/true,
|
|
|
|
InliningPass);
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitAssembly - Handle interaction with LLVM backend to generate
|
2009-09-09 23:08:12 +08:00
|
|
|
/// actual machine code.
|
2008-10-22 07:49:24 +08:00
|
|
|
void BackendConsumer::EmitAssembly() {
|
|
|
|
// Silently ignore if we weren't initialized for some reason.
|
|
|
|
if (!TheModule || !TheTargetData)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-30 16:39:52 +08:00
|
|
|
TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2008-10-28 04:40:41 +08:00
|
|
|
// Make sure IR generation is happy with the module. This is
|
|
|
|
// released by the module provider.
|
2008-10-22 07:49:24 +08:00
|
|
|
Module *M = Gen->ReleaseModule();
|
|
|
|
if (!M) {
|
2008-10-28 04:40:41 +08:00
|
|
|
// The module has been released by IR gen on failures, do not
|
|
|
|
// double free.
|
|
|
|
ModuleProvider->releaseModule();
|
2008-10-22 07:49:24 +08:00
|
|
|
TheModule = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(TheModule == M && "Unexpected module change during IR generation");
|
|
|
|
|
|
|
|
CreatePasses();
|
2009-12-03 17:12:54 +08:00
|
|
|
if (!AddEmitPasses())
|
|
|
|
return;
|
2008-10-22 07:49:24 +08:00
|
|
|
|
|
|
|
// Run passes. For now we do all passes at once, but eventually we
|
|
|
|
// would like to have the option of streaming code generation.
|
|
|
|
|
|
|
|
if (PerFunctionPasses) {
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-function optimization");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
PerFunctionPasses->doInitialization();
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isDeclaration())
|
|
|
|
PerFunctionPasses->run(*I);
|
|
|
|
PerFunctionPasses->doFinalization();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-05 16:00:35 +08:00
|
|
|
if (PerModulePasses) {
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-module optimization passes");
|
2008-10-22 07:49:24 +08:00
|
|
|
PerModulePasses->run(*M);
|
2009-03-05 16:00:35 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
if (CodeGenPasses) {
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
2008-10-22 07:49:24 +08:00
|
|
|
CodeGenPasses->doInitialization();
|
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isDeclaration())
|
|
|
|
CodeGenPasses->run(*I);
|
|
|
|
CodeGenPasses->doFinalization();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTConsumer *clang::CreateBackendConsumer(BackendAction Action,
|
|
|
|
Diagnostic &Diags,
|
2009-02-18 03:47:34 +08:00
|
|
|
const LangOptions &LangOpts,
|
2009-11-13 01:24:48 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts,
|
2009-11-15 14:48:46 +08:00
|
|
|
const TargetOptions &TargetOpts,
|
2009-11-30 16:39:52 +08:00
|
|
|
bool TimePasses,
|
2008-10-22 07:49:24 +08:00
|
|
|
const std::string& InFile,
|
2009-07-02 01:00:06 +08:00
|
|
|
llvm::raw_ostream* OS,
|
2009-07-02 07:14:14 +08:00
|
|
|
LLVMContext& C) {
|
2009-11-13 01:24:48 +08:00
|
|
|
return new BackendConsumer(Action, Diags, LangOpts, CodeGenOpts,
|
2009-11-30 16:39:52 +08:00
|
|
|
TargetOpts, TimePasses, InFile, OS, C);
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|