2010-06-08 07:20:08 +08:00
|
|
|
//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/BackendUtil.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2011-07-06 06:02:36 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2010-06-16 07:19:56 +08:00
|
|
|
#include "clang/Frontend/CodeGenOptions.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2013-12-27 16:11:08 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2014-05-08 10:28:32 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2015-01-15 10:16:55 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-01-31 19:18:46 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2014-01-13 15:47:38 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-12 19:11:50 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2015-02-13 17:57:03 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 17:26:48 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2011-06-29 09:14:32 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2014-08-05 05:33:42 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2013-01-28 09:36:00 +08:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2015-01-09 13:10:20 +08:00
|
|
|
#include "llvm/Transforms/Utils/SymbolRewriter.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2010-06-08 07:20:08 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class EmitAssemblyHelper {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2010-06-08 07:20:08 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
2011-12-03 06:17:00 +08:00
|
|
|
const clang::TargetOptions &TargetOpts;
|
2011-07-06 06:02:36 +08:00
|
|
|
const LangOptions &LangOpts;
|
2010-06-08 07:20:08 +08:00
|
|
|
Module *TheModule;
|
|
|
|
|
|
|
|
Timer CodeGenerationTime;
|
|
|
|
|
2015-02-13 17:47:49 +08:00
|
|
|
mutable legacy::PassManager *CodeGenPasses;
|
|
|
|
mutable legacy::PassManager *PerModulePasses;
|
|
|
|
mutable legacy::FunctionPassManager *PerFunctionPasses;
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
private:
|
2015-02-01 20:26:23 +08:00
|
|
|
TargetIRAnalysis getTargetIRAnalysis() const {
|
2015-01-31 19:18:46 +08:00
|
|
|
if (TM)
|
2015-02-01 20:26:23 +08:00
|
|
|
return TM->getTargetIRAnalysis();
|
2015-01-31 19:18:46 +08:00
|
|
|
|
2015-02-01 20:26:23 +08:00
|
|
|
return TargetIRAnalysis();
|
2015-01-31 19:18:46 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManager *getCodeGenPasses() const {
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!CodeGenPasses) {
|
2015-02-13 17:47:49 +08:00
|
|
|
CodeGenPasses = new legacy::PassManager();
|
2014-09-11 05:27:41 +08:00
|
|
|
CodeGenPasses->add(new DataLayoutPass());
|
2015-02-01 20:26:23 +08:00
|
|
|
CodeGenPasses->add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
return CodeGenPasses;
|
|
|
|
}
|
|
|
|
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManager *getPerModulePasses() const {
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!PerModulePasses) {
|
2015-02-13 17:47:49 +08:00
|
|
|
PerModulePasses = new legacy::PassManager();
|
2014-09-11 05:27:41 +08:00
|
|
|
PerModulePasses->add(new DataLayoutPass());
|
2015-02-01 20:26:23 +08:00
|
|
|
PerModulePasses->add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
return PerModulePasses;
|
|
|
|
}
|
|
|
|
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::FunctionPassManager *getPerFunctionPasses() const {
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!PerFunctionPasses) {
|
2015-02-13 17:47:49 +08:00
|
|
|
PerFunctionPasses = new legacy::FunctionPassManager(TheModule);
|
2014-09-11 05:27:41 +08:00
|
|
|
PerFunctionPasses->add(new DataLayoutPass());
|
2015-02-01 20:26:23 +08:00
|
|
|
PerFunctionPasses->add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
return PerFunctionPasses;
|
|
|
|
}
|
|
|
|
|
2013-12-21 04:26:53 +08:00
|
|
|
void CreatePasses();
|
2012-10-24 08:53:38 +08:00
|
|
|
|
2012-10-24 11:52:31 +08:00
|
|
|
/// CreateTargetMachine - Generates the TargetMachine.
|
|
|
|
/// Returns Null if it is unable to create the target machine.
|
|
|
|
/// Some of our clang tests specify triples which are not built
|
|
|
|
/// into clang. This is okay because these tests check the generated
|
|
|
|
/// IR, and they require DataLayout which depends on the triple.
|
|
|
|
/// In this case, we allow this method to fail and not report an error.
|
|
|
|
/// When MustCreateTM is used, we print an error if we are unable to load
|
|
|
|
/// the requested target.
|
|
|
|
TargetMachine *CreateTargetMachine(bool MustCreateTM);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
/// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
|
|
|
|
///
|
|
|
|
/// \return True on success.
|
2013-12-21 04:26:53 +08:00
|
|
|
bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
public:
|
2011-09-26 07:23:43 +08:00
|
|
|
EmitAssemblyHelper(DiagnosticsEngine &_Diags,
|
2011-12-03 06:17:00 +08:00
|
|
|
const CodeGenOptions &CGOpts,
|
|
|
|
const clang::TargetOptions &TOpts,
|
2011-07-06 06:02:36 +08:00
|
|
|
const LangOptions &LOpts,
|
2010-06-08 07:21:04 +08:00
|
|
|
Module *M)
|
2011-07-06 06:02:36 +08:00
|
|
|
: Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
|
2010-06-08 07:21:04 +08:00
|
|
|
TheModule(M), CodeGenerationTime("Code Generation Time"),
|
2014-05-21 13:09:00 +08:00
|
|
|
CodeGenPasses(nullptr), PerModulePasses(nullptr),
|
|
|
|
PerFunctionPasses(nullptr) {}
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
~EmitAssemblyHelper() {
|
|
|
|
delete CodeGenPasses;
|
|
|
|
delete PerModulePasses;
|
|
|
|
delete PerFunctionPasses;
|
2013-12-21 04:26:53 +08:00
|
|
|
if (CodeGenOpts.DisableFree)
|
Provide a BuryPointer for unique_ptrs.
In theory, it'd be nice if we could move to a case where all buried
pointers were buried via unique_ptr to demonstrate that the program had
finished with the value (that we could really have cleanly deallocated
it) but instead chose to bury it.
I think the main reason that's not possible right now is the various
IntrusiveRefCntPtrs in the Frontend, sharing ownership for a variety of
compiler bits (see the various similar
"CompilerInstance::releaseAndLeak*" functions). I have yet to figure out
their correct ownership semantics - but perhaps, even if the
intrusiveness can be removed, the shared ownership may yet remain and
that would lead to a non-unique burying as is there today. (though we
could model that a little better - by passing in a shared_ptr, etc -
rather than needing the two step that's currently used in those other
releaseAndLeak* functions)
This might be a bit more robust if BuryPointer took the boolean:
BuryPointer(bool, unique_ptr<T>)
and the choice to bury was made internally - that way, even when
DisableFree was not set, the unique_ptr would still be null in the
caller and there'd be no chance of accidentally having a different
codepath where the value is used after burial in !DisableFree, but it
becomes null only in DisableFree, etc...
llvm-svn: 216742
2014-08-30 00:53:14 +08:00
|
|
|
BuryPointer(std::move(TM));
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<TargetMachine> TM;
|
2013-12-21 04:26:53 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
void EmitAssembly(BackendAction Action, raw_ostream *OS);
|
|
|
|
};
|
|
|
|
|
2012-12-28 17:31:34 +08:00
|
|
|
// We need this wrapper to access LangOpts and CGOpts from extension functions
|
|
|
|
// that we add to the PassManagerBuilder.
|
2012-11-30 06:36:21 +08:00
|
|
|
class PassManagerBuilderWrapper : public PassManagerBuilder {
|
|
|
|
public:
|
2012-12-04 03:12:58 +08:00
|
|
|
PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
|
|
|
|
const LangOptions &LangOpts)
|
|
|
|
: PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
|
|
|
|
const CodeGenOptions &getCGOpts() const { return CGOpts; }
|
2012-11-30 06:36:21 +08:00
|
|
|
const LangOptions &getLangOpts() const { return LangOpts; }
|
|
|
|
private:
|
2012-12-04 03:12:58 +08:00
|
|
|
const CodeGenOptions &CGOpts;
|
2012-11-30 06:36:21 +08:00
|
|
|
const LangOptions &LangOpts;
|
|
|
|
};
|
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2012-01-18 04:54:51 +08:00
|
|
|
static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
|
|
|
|
if (Builder.OptLevel > 0)
|
|
|
|
PM.add(createObjCARCAPElimPass());
|
|
|
|
}
|
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
|
|
|
|
if (Builder.OptLevel > 0)
|
|
|
|
PM.add(createObjCARCExpandPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
|
|
|
|
if (Builder.OptLevel > 0)
|
|
|
|
PM.add(createObjCARCOptPass());
|
|
|
|
}
|
|
|
|
|
2013-11-13 20:22:39 +08:00
|
|
|
static void addSampleProfileLoaderPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2013-11-13 20:22:39 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper &>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
PM.add(createSampleProfileLoaderPass(CGOpts.SampleProfileFile));
|
|
|
|
}
|
|
|
|
|
2014-03-04 04:06:18 +08:00
|
|
|
static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2014-03-04 04:06:18 +08:00
|
|
|
PM.add(createAddDiscriminatorsPass());
|
|
|
|
}
|
|
|
|
|
2012-05-23 01:19:45 +08:00
|
|
|
static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2012-11-23 18:39:49 +08:00
|
|
|
PM.add(createBoundsCheckingPass());
|
2012-05-23 01:19:45 +08:00
|
|
|
}
|
|
|
|
|
2014-11-12 06:15:07 +08:00
|
|
|
static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2014-11-12 06:15:07 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
PM.add(createSanitizerCoverageModulePass(CGOpts.SanitizeCoverage));
|
|
|
|
}
|
|
|
|
|
2012-11-30 06:36:21 +08:00
|
|
|
static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2014-06-14 01:53:44 +08:00
|
|
|
PM.add(createAddressSanitizerFunctionPass());
|
2014-07-08 08:50:49 +08:00
|
|
|
PM.add(createAddressSanitizerModulePass());
|
2011-11-17 01:34:26 +08:00
|
|
|
}
|
|
|
|
|
2012-12-03 21:20:43 +08:00
|
|
|
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2012-12-24 16:42:34 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
2014-06-03 02:08:08 +08:00
|
|
|
PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins));
|
2013-01-31 17:53:29 +08:00
|
|
|
|
|
|
|
// MemorySanitizer inserts complex instrumentation that mostly follows
|
|
|
|
// the logic of the original code, but operates on "shadow" values.
|
|
|
|
// It can benefit from re-running some general purpose optimization passes.
|
|
|
|
if (Builder.OptLevel > 0) {
|
|
|
|
PM.add(createEarlyCSEPass());
|
|
|
|
PM.add(createReassociatePass());
|
|
|
|
PM.add(createLICMPass());
|
|
|
|
PM.add(createGVNPass());
|
|
|
|
PM.add(createInstructionCombiningPass());
|
|
|
|
PM.add(createDeadStoreEliminationPass());
|
|
|
|
}
|
2012-12-03 21:20:43 +08:00
|
|
|
}
|
|
|
|
|
2012-03-02 06:27:08 +08:00
|
|
|
static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2014-06-03 02:08:08 +08:00
|
|
|
PM.add(createThreadSanitizerPass());
|
2012-03-02 06:27:08 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 06:47:34 +08:00
|
|
|
static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2013-08-15 02:54:18 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
2014-10-16 04:22:54 +08:00
|
|
|
const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
|
2015-02-05 01:40:08 +08:00
|
|
|
PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
|
2013-08-08 06:47:34 +08:00
|
|
|
}
|
|
|
|
|
2015-01-24 10:25:21 +08:00
|
|
|
static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
|
|
|
|
const CodeGenOptions &CodeGenOpts) {
|
|
|
|
TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
|
2014-08-22 01:58:42 +08:00
|
|
|
if (!CodeGenOpts.SimplifyLibCalls)
|
2015-01-24 10:25:21 +08:00
|
|
|
TLII->disableAllFunctions();
|
|
|
|
return TLII;
|
2014-08-22 01:58:42 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 13:10:20 +08:00
|
|
|
static void addSymbolRewriterPass(const CodeGenOptions &Opts,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManager *MPM) {
|
2015-01-09 13:10:20 +08:00
|
|
|
llvm::SymbolRewriter::RewriteDescriptorList DL;
|
|
|
|
|
|
|
|
llvm::SymbolRewriter::RewriteMapParser MapParser;
|
|
|
|
for (const auto &MapFile : Opts.RewriteMapFiles)
|
|
|
|
MapParser.parse(MapFile, &DL);
|
|
|
|
|
|
|
|
MPM->add(createRewriteSymbolsPass(DL));
|
|
|
|
}
|
|
|
|
|
2013-12-21 04:26:53 +08:00
|
|
|
void EmitAssemblyHelper::CreatePasses() {
|
2010-06-08 07:20:08 +08:00
|
|
|
unsigned OptLevel = CodeGenOpts.OptimizationLevel;
|
2012-10-24 04:05:01 +08:00
|
|
|
CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
// Handle disabling of LLVM optimization, where we want to preserve the
|
|
|
|
// internal module before any optimization.
|
|
|
|
if (CodeGenOpts.DisableLLVMOpts) {
|
|
|
|
OptLevel = 0;
|
|
|
|
Inlining = CodeGenOpts.NoInlining;
|
|
|
|
}
|
2012-11-30 06:36:21 +08:00
|
|
|
|
2012-12-04 03:12:58 +08:00
|
|
|
PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.OptLevel = OptLevel;
|
|
|
|
PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
|
2013-06-25 09:49:44 +08:00
|
|
|
PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
|
|
|
|
PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
|
|
|
|
PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
|
2011-05-22 04:40:11 +08:00
|
|
|
|
2014-04-18 09:05:25 +08:00
|
|
|
PMBuilder.DisableTailCalls = CodeGenOpts.DisableTailCalls;
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
|
|
|
|
PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
|
2014-10-24 08:49:29 +08:00
|
|
|
PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
|
2013-11-18 00:03:29 +08:00
|
|
|
PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
|
2011-07-06 06:02:36 +08:00
|
|
|
|
2014-03-04 04:06:18 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
addAddDiscriminatorsPass);
|
|
|
|
|
2013-11-13 20:22:39 +08:00
|
|
|
if (!CodeGenOpts.SampleProfileFile.empty())
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
addSampleProfileLoaderPass);
|
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
// In ObjC ARC mode, add the main ARC optimization passes.
|
|
|
|
if (LangOpts.ObjCAutoRefCount) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
addObjCARCExpandPass);
|
2012-01-18 04:54:51 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
|
|
|
|
addObjCARCAPElimPass);
|
2011-07-06 06:02:36 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
|
|
|
|
addObjCARCOptPass);
|
|
|
|
}
|
2011-11-17 01:34:26 +08:00
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
|
2012-05-23 01:19:45 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
|
|
|
|
addBoundsCheckingPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addBoundsCheckingPass);
|
|
|
|
}
|
|
|
|
|
2014-11-12 06:15:07 +08:00
|
|
|
if (CodeGenOpts.SanitizeCoverage) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addSanitizerCoveragePass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addSanitizerCoveragePass);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
|
2012-10-15 22:22:56 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
2012-11-30 06:36:21 +08:00
|
|
|
addAddressSanitizerPasses);
|
2011-12-01 06:20:21 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
2012-11-30 06:36:21 +08:00
|
|
|
addAddressSanitizerPasses);
|
2011-11-17 01:34:26 +08:00
|
|
|
}
|
2012-03-02 06:27:08 +08:00
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
|
2012-12-03 21:20:43 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addMemorySanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addMemorySanitizerPass);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
|
2012-03-24 07:25:23 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
2012-03-02 06:27:08 +08:00
|
|
|
addThreadSanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addThreadSanitizerPass);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
|
2013-08-08 06:47:34 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addDataFlowSanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addDataFlowSanitizerPass);
|
|
|
|
}
|
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Figure out TargetLibraryInfo.
|
2011-05-18 07:06:23 +08:00
|
|
|
Triple TargetTriple(TheModule->getTargetTriple());
|
2015-01-24 10:25:21 +08:00
|
|
|
PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts);
|
2014-03-13 00:30:34 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
switch (Inlining) {
|
|
|
|
case CodeGenOptions::NoInlining: break;
|
|
|
|
case CodeGenOptions::NormalInlining: {
|
2014-03-13 00:30:34 +08:00
|
|
|
PMBuilder.Inliner =
|
|
|
|
createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize);
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CodeGenOptions::OnlyAlwaysInlining:
|
2011-05-22 04:40:11 +08:00
|
|
|
// Respect always_inline.
|
2012-02-25 10:56:13 +08:00
|
|
|
if (OptLevel == 0)
|
|
|
|
// Do not insert lifetime intrinsics at -O0.
|
|
|
|
PMBuilder.Inliner = createAlwaysInlinerPass(false);
|
|
|
|
else
|
|
|
|
PMBuilder.Inliner = createAlwaysInlinerPass();
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Set up the per-function pass manager.
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::FunctionPassManager *FPM = getPerFunctionPasses();
|
2011-05-22 04:40:11 +08:00
|
|
|
if (CodeGenOpts.VerifyModule)
|
|
|
|
FPM->add(createVerifierPass());
|
|
|
|
PMBuilder.populateFunctionPassManager(*FPM);
|
2011-04-06 02:49:32 +08:00
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Set up the per-module pass manager.
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManager *MPM = getPerModulePasses();
|
2015-01-09 13:10:20 +08:00
|
|
|
if (!CodeGenOpts.RewriteMapFiles.empty())
|
|
|
|
addSymbolRewriterPass(CodeGenOpts, MPM);
|
2014-04-16 00:27:43 +08:00
|
|
|
if (CodeGenOpts.VerifyModule)
|
|
|
|
MPM->add(createDebugInfoVerifierPass());
|
2011-02-19 06:20:38 +08:00
|
|
|
|
2013-03-20 09:38:16 +08:00
|
|
|
if (!CodeGenOpts.DisableGCov &&
|
|
|
|
(CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
|
2013-03-14 13:14:01 +08:00
|
|
|
// Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
|
|
|
|
// LLVM's -default-gcov-version flag is set to something invalid.
|
|
|
|
GCOVOptions Options;
|
|
|
|
Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
|
|
|
|
Options.EmitData = CodeGenOpts.EmitGcovArcs;
|
|
|
|
memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
|
|
|
|
Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
|
|
|
Options.FunctionNamesInData =
|
2013-03-20 10:14:38 +08:00
|
|
|
!CodeGenOpts.CoverageNoFunctionNamesInData;
|
2013-03-14 13:14:01 +08:00
|
|
|
MPM->add(createGCOVProfilerPass(Options));
|
2012-10-24 04:05:01 +08:00
|
|
|
if (CodeGenOpts.getDebugInfo() == CodeGenOptions::NoDebugInfo)
|
2011-04-22 07:44:07 +08:00
|
|
|
MPM->add(createStripSymbolsPass(true));
|
|
|
|
}
|
2012-10-24 11:52:31 +08:00
|
|
|
|
2014-12-09 03:04:51 +08:00
|
|
|
if (CodeGenOpts.ProfileInstrGenerate) {
|
|
|
|
InstrProfOptions Options;
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
|
|
|
MPM->add(createInstrProfilingPass(Options));
|
|
|
|
}
|
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
PMBuilder.populateModulePassManager(*MPM);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2012-10-24 11:52:31 +08:00
|
|
|
TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
|
2010-06-08 07:20:08 +08:00
|
|
|
// Create the TargetMachine for generating code.
|
|
|
|
std::string Error;
|
|
|
|
std::string Triple = TheModule->getTargetTriple();
|
|
|
|
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
|
|
|
|
if (!TheTarget) {
|
2012-10-24 11:52:31 +08:00
|
|
|
if (MustCreateTM)
|
2013-03-27 08:14:35 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_create_target) << Error;
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 10:28:32 +08:00
|
|
|
unsigned CodeModel =
|
|
|
|
llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
|
|
|
|
.Case("small", llvm::CodeModel::Small)
|
|
|
|
.Case("kernel", llvm::CodeModel::Kernel)
|
|
|
|
.Case("medium", llvm::CodeModel::Medium)
|
2014-05-09 00:28:48 +08:00
|
|
|
.Case("large", llvm::CodeModel::Large)
|
2014-05-08 10:28:32 +08:00
|
|
|
.Case("default", llvm::CodeModel::Default)
|
|
|
|
.Default(~0u);
|
|
|
|
assert(CodeModel != ~0u && "invalid code model!");
|
|
|
|
llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2012-02-08 03:36:38 +08:00
|
|
|
SmallVector<const char *, 16> BackendArgs;
|
2010-06-08 07:20:08 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
if (llvm::TimePassesIsEnabled)
|
|
|
|
BackendArgs.push_back("-time-passes");
|
2011-03-23 00:48:17 +08:00
|
|
|
for (unsigned i = 0, e = CodeGenOpts.BackendOptions.size(); i != e; ++i)
|
|
|
|
BackendArgs.push_back(CodeGenOpts.BackendOptions[i].c_str());
|
2011-08-26 08:26:29 +08:00
|
|
|
if (CodeGenOpts.NoGlobalMerge)
|
2014-07-26 08:37:28 +08:00
|
|
|
BackendArgs.push_back("-enable-global-merge=false");
|
2014-05-21 13:09:00 +08:00
|
|
|
BackendArgs.push_back(nullptr);
|
2010-06-08 07:20:08 +08:00
|
|
|
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
|
2012-02-08 03:36:38 +08:00
|
|
|
BackendArgs.data());
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
std::string FeaturesStr;
|
2015-01-23 23:36:10 +08:00
|
|
|
if (!TargetOpts.Features.empty()) {
|
2010-06-08 07:20:08 +08:00
|
|
|
SubtargetFeatures Features;
|
|
|
|
for (std::vector<std::string>::const_iterator
|
|
|
|
it = TargetOpts.Features.begin(),
|
|
|
|
ie = TargetOpts.Features.end(); it != ie; ++it)
|
|
|
|
Features.AddFeature(*it);
|
|
|
|
FeaturesStr = Features.getString();
|
|
|
|
}
|
2011-07-19 14:37:41 +08:00
|
|
|
|
|
|
|
llvm::Reloc::Model RM = llvm::Reloc::Default;
|
|
|
|
if (CodeGenOpts.RelocationModel == "static") {
|
|
|
|
RM = llvm::Reloc::Static;
|
|
|
|
} else if (CodeGenOpts.RelocationModel == "pic") {
|
|
|
|
RM = llvm::Reloc::PIC_;
|
|
|
|
} else {
|
|
|
|
assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
|
|
|
|
"Invalid PIC model!");
|
|
|
|
RM = llvm::Reloc::DynamicNoPIC;
|
|
|
|
}
|
|
|
|
|
2011-11-16 16:38:55 +08:00
|
|
|
CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
|
|
|
|
switch (CodeGenOpts.OptimizationLevel) {
|
|
|
|
default: break;
|
|
|
|
case 0: OptLevel = CodeGenOpt::None; break;
|
|
|
|
case 3: OptLevel = CodeGenOpt::Aggressive; break;
|
|
|
|
}
|
|
|
|
|
2011-12-03 06:17:00 +08:00
|
|
|
llvm::TargetOptions Options;
|
|
|
|
|
2014-10-04 05:57:44 +08:00
|
|
|
Options.ThreadModel =
|
|
|
|
llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
|
|
|
|
.Case("posix", llvm::ThreadModel::POSIX)
|
|
|
|
.Case("single", llvm::ThreadModel::Single);
|
|
|
|
|
2014-02-21 11:14:07 +08:00
|
|
|
if (CodeGenOpts.DisableIntegratedAS)
|
|
|
|
Options.DisableIntegratedAS = true;
|
|
|
|
|
2014-03-28 04:47:30 +08:00
|
|
|
if (CodeGenOpts.CompressDebugSections)
|
|
|
|
Options.CompressDebugSections = true;
|
|
|
|
|
2011-12-03 06:17:00 +08:00
|
|
|
// Set frame pointer elimination mode.
|
|
|
|
if (!CodeGenOpts.DisableFPElim) {
|
|
|
|
Options.NoFramePointerElim = false;
|
|
|
|
} else if (CodeGenOpts.OmitLeafFramePointer) {
|
|
|
|
Options.NoFramePointerElim = false;
|
|
|
|
} else {
|
|
|
|
Options.NoFramePointerElim = true;
|
|
|
|
}
|
|
|
|
|
2012-06-19 09:26:10 +08:00
|
|
|
if (CodeGenOpts.UseInitArray)
|
|
|
|
Options.UseInitArray = true;
|
|
|
|
|
2011-12-03 06:17:00 +08:00
|
|
|
// Set float ABI type.
|
|
|
|
if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp")
|
|
|
|
Options.FloatABIType = llvm::FloatABI::Soft;
|
|
|
|
else if (CodeGenOpts.FloatABI == "hard")
|
|
|
|
Options.FloatABIType = llvm::FloatABI::Hard;
|
|
|
|
else {
|
|
|
|
assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
|
|
|
|
Options.FloatABIType = llvm::FloatABI::Default;
|
|
|
|
}
|
|
|
|
|
2012-07-06 08:59:19 +08:00
|
|
|
// Set FP fusion mode.
|
2012-11-15 15:51:26 +08:00
|
|
|
switch (CodeGenOpts.getFPContractMode()) {
|
|
|
|
case CodeGenOptions::FPC_Off:
|
2012-07-06 08:59:19 +08:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
|
|
|
|
break;
|
2012-11-15 15:51:26 +08:00
|
|
|
case CodeGenOptions::FPC_On:
|
2012-07-06 08:59:19 +08:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
|
|
|
|
break;
|
2012-11-15 15:51:26 +08:00
|
|
|
case CodeGenOptions::FPC_Fast:
|
2012-07-06 08:59:19 +08:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
|
2012-10-19 12:15:32 +08:00
|
|
|
break;
|
2012-07-06 08:59:19 +08:00
|
|
|
}
|
|
|
|
|
2011-12-03 06:17:00 +08:00
|
|
|
Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
|
|
|
|
Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
|
|
|
|
Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
|
|
|
|
Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
|
|
|
|
Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
|
|
|
|
Options.UseSoftFloat = CodeGenOpts.SoftFloat;
|
2011-12-06 11:33:03 +08:00
|
|
|
Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
|
2012-01-23 16:29:12 +08:00
|
|
|
Options.DisableTailCalls = CodeGenOpts.DisableTailCalls;
|
2012-02-03 14:27:22 +08:00
|
|
|
Options.TrapFuncName = CodeGenOpts.TrapFuncName;
|
2012-04-09 05:09:51 +08:00
|
|
|
Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
|
2014-05-21 05:25:41 +08:00
|
|
|
Options.FunctionSections = CodeGenOpts.FunctionSections;
|
|
|
|
Options.DataSections = CodeGenOpts.DataSections;
|
2015-02-21 02:08:57 +08:00
|
|
|
Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
|
2011-12-03 06:17:00 +08:00
|
|
|
|
2014-05-15 09:21:56 +08:00
|
|
|
Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
|
|
|
|
Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
|
2014-05-17 04:46:14 +08:00
|
|
|
Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
|
2014-05-15 09:21:56 +08:00
|
|
|
Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
|
2014-08-27 02:40:25 +08:00
|
|
|
Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
|
2014-05-21 08:00:03 +08:00
|
|
|
Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
|
2015-01-14 08:50:32 +08:00
|
|
|
Options.MCOptions.ABIName = TargetOpts.ABI;
|
2014-05-15 09:21:56 +08:00
|
|
|
|
2011-06-30 10:06:32 +08:00
|
|
|
TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
|
2011-12-03 06:17:00 +08:00
|
|
|
FeaturesStr, Options,
|
|
|
|
RM, CM, OptLevel);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2012-10-24 08:53:38 +08:00
|
|
|
return TM;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
|
2013-12-21 04:26:53 +08:00
|
|
|
formatted_raw_ostream &OS) {
|
2012-10-24 08:53:38 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Create the code generator passes.
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManager *PM = getCodeGenPasses();
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2012-03-01 04:14:59 +08:00
|
|
|
// Add LibraryInfo.
|
2012-10-20 04:10:10 +08:00
|
|
|
llvm::Triple TargetTriple(TheModule->getTargetTriple());
|
2015-01-24 10:25:21 +08:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
|
|
|
createTLII(TargetTriple, CodeGenOpts));
|
|
|
|
PM->add(new TargetLibraryInfoWrapperPass(*TLII));
|
2012-03-01 04:14:59 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Normal mode, emit a .s or .o file by running the code generator. Note,
|
|
|
|
// this also adds codegenerator level optimization passes.
|
|
|
|
TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
|
|
|
|
if (Action == Backend_EmitObj)
|
|
|
|
CGFT = TargetMachine::CGFT_ObjectFile;
|
|
|
|
else if (Action == Backend_EmitMCNull)
|
|
|
|
CGFT = TargetMachine::CGFT_Null;
|
|
|
|
else
|
|
|
|
assert(Action == Backend_EmitAssembly && "Invalid action!");
|
2011-07-06 06:02:36 +08:00
|
|
|
|
|
|
|
// Add ObjC ARC final-cleanup optimizations. This is done as part of the
|
|
|
|
// "codegen" passes so that it isn't run multiple times when there is
|
|
|
|
// inlining happening.
|
2012-04-05 05:04:56 +08:00
|
|
|
if (LangOpts.ObjCAutoRefCount &&
|
|
|
|
CodeGenOpts.OptimizationLevel > 0)
|
2011-07-06 06:02:36 +08:00
|
|
|
PM->add(createObjCARCContractPass());
|
|
|
|
|
2011-11-16 16:38:55 +08:00
|
|
|
if (TM->addPassesToEmitFile(*PM, OS, CGFT,
|
2010-06-08 07:20:08 +08:00
|
|
|
/*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_interface_with_target);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) {
|
2014-05-21 13:09:00 +08:00
|
|
|
TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
|
2010-06-08 07:20:08 +08:00
|
|
|
llvm::formatted_raw_ostream FormattedOS;
|
|
|
|
|
2012-10-24 11:52:31 +08:00
|
|
|
bool UsesCodeGen = (Action != Backend_EmitNothing &&
|
|
|
|
Action != Backend_EmitBC &&
|
|
|
|
Action != Backend_EmitLL);
|
2013-12-21 04:26:53 +08:00
|
|
|
if (!TM)
|
|
|
|
TM.reset(CreateTargetMachine(UsesCodeGen));
|
|
|
|
|
2013-03-27 08:14:35 +08:00
|
|
|
if (UsesCodeGen && !TM) return;
|
2013-12-21 04:26:53 +08:00
|
|
|
CreatePasses();
|
2012-10-24 08:53:38 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
switch (Action) {
|
|
|
|
case Backend_EmitNothing:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitBC:
|
2013-12-21 04:26:53 +08:00
|
|
|
getPerModulePasses()->add(createBitcodeWriterPass(*OS));
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitLL:
|
|
|
|
FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
|
2014-01-12 19:31:22 +08:00
|
|
|
getPerModulePasses()->add(createPrintModulePass(FormattedOS));
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
|
2013-12-21 04:26:53 +08:00
|
|
|
if (!AddEmitPasses(Action, FormattedOS))
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-06 02:56:55 +08:00
|
|
|
// Before executing passes, print the final values of the LLVM options.
|
|
|
|
cl::PrintOptionValues();
|
|
|
|
|
2010-06-08 07:20:08 +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) {
|
|
|
|
PrettyStackTraceString CrashInfo("Per-function optimization");
|
|
|
|
|
|
|
|
PerFunctionPasses->doInitialization();
|
|
|
|
for (Module::iterator I = TheModule->begin(),
|
|
|
|
E = TheModule->end(); I != E; ++I)
|
|
|
|
if (!I->isDeclaration())
|
|
|
|
PerFunctionPasses->run(*I);
|
|
|
|
PerFunctionPasses->doFinalization();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PerModulePasses) {
|
|
|
|
PrettyStackTraceString CrashInfo("Per-module optimization passes");
|
|
|
|
PerModulePasses->run(*TheModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CodeGenPasses) {
|
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
2010-09-17 15:35:16 +08:00
|
|
|
CodeGenPasses->run(*TheModule);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
|
|
|
|
const CodeGenOptions &CGOpts,
|
2011-12-03 06:17:00 +08:00
|
|
|
const clang::TargetOptions &TOpts,
|
2014-01-02 23:08:04 +08:00
|
|
|
const LangOptions &LOpts, StringRef TDesc,
|
|
|
|
Module *M, BackendAction Action,
|
|
|
|
raw_ostream *OS) {
|
2011-07-06 06:02:36 +08:00
|
|
|
EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
AsmHelper.EmitAssembly(Action, OS);
|
2014-01-02 23:08:04 +08:00
|
|
|
|
|
|
|
// If an optional clang TargetInfo description string was passed in, use it to
|
|
|
|
// verify the LLVM TargetMachine's DataLayout.
|
|
|
|
if (AsmHelper.TM && !TDesc.empty()) {
|
2015-01-27 03:03:30 +08:00
|
|
|
std::string DLDesc =
|
|
|
|
AsmHelper.TM->getDataLayout()->getStringRepresentation();
|
2014-01-02 23:08:04 +08:00
|
|
|
if (DLDesc != TDesc) {
|
|
|
|
unsigned DiagID = Diags.getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error, "backend data layout '%0' does not match "
|
|
|
|
"expected target description '%1'");
|
|
|
|
Diags.Report(DiagID) << DLDesc << TDesc;
|
|
|
|
}
|
|
|
|
}
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|