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"
|
2016-04-09 00:52:00 +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"
|
2016-12-15 10:19:17 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2015-09-26 09:25:08 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-05-08 10:28:32 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2016-05-12 00:26:03 +08:00
|
|
|
#include "llvm/ADT/Triple.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"
|
2016-11-11 13:35:12 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2016-12-24 04:44:01 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
|
|
|
#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"
|
2016-12-24 04:44:01 +08:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2014-01-13 17:26:48 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2016-08-13 02:12:08 +08:00
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
2011-06-29 09:14:32 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2016-03-15 08:04:44 +08:00
|
|
|
#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
|
2016-12-24 04:44:01 +08:00
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-08-13 02:12:08 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#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"
|
2016-10-04 06:44:48 +08:00
|
|
|
#include "llvm/Transforms/Coroutines.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2016-08-17 11:09:11 +08:00
|
|
|
#include "llvm/Transforms/IPO/AlwaysInliner.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#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"
|
2016-03-11 17:02:43 +08:00
|
|
|
#include "llvm/Transforms/Scalar/GVN.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;
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS;
|
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
|
|
|
}
|
|
|
|
|
2016-04-13 04:22:32 +08:00
|
|
|
/// Set LLVM command line options passed through -backend-option.
|
|
|
|
void setCommandLineOpts();
|
|
|
|
|
2016-08-13 02:12:08 +08:00
|
|
|
void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
|
2012-10-24 08:53:38 +08:00
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
/// Generates the TargetMachine.
|
2016-07-15 08:55:40 +08:00
|
|
|
/// Leaves TM unchanged if it is unable to create the target machine.
|
2012-10-24 11:52:31 +08:00
|
|
|
/// 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.
|
2016-07-15 08:55:40 +08:00
|
|
|
void CreateTargetMachine(bool MustCreateTM);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
/// Add passes necessary to emit assembly or LLVM IR.
|
2010-06-08 07:20:08 +08:00
|
|
|
///
|
|
|
|
/// \return True on success.
|
2016-07-15 08:55:40 +08:00
|
|
|
bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
|
|
|
|
raw_pwrite_stream &OS);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
public:
|
2015-12-08 03:21:34 +08:00
|
|
|
EmitAssemblyHelper(DiagnosticsEngine &_Diags, const CodeGenOptions &CGOpts,
|
2011-12-03 06:17:00 +08:00
|
|
|
const clang::TargetOptions &TOpts,
|
2016-01-09 01:04:29 +08:00
|
|
|
const LangOptions &LOpts, Module *M)
|
2015-12-08 03:21:34 +08:00
|
|
|
: Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
|
2016-11-19 03:43:25 +08:00
|
|
|
TheModule(M), CodeGenerationTime("codegen", "Code Generation Time") {}
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
~EmitAssemblyHelper() {
|
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
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
void EmitAssembly(BackendAction Action,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS);
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
void EmitAssemblyWithNewPassManager(BackendAction Action,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS);
|
2010-06-08 07:20:08 +08:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
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,
|
2016-06-02 08:24:20 +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();
|
2015-05-08 02:31:29 +08:00
|
|
|
SanitizerCoverageOptions Opts;
|
|
|
|
Opts.CoverageType =
|
|
|
|
static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
|
|
|
|
Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
|
|
|
|
Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
|
|
|
|
Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
|
2016-08-30 09:27:03 +08:00
|
|
|
Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
|
|
|
|
Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
|
2015-05-08 02:31:29 +08:00
|
|
|
Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
|
2016-02-18 05:34:43 +08:00
|
|
|
Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
|
2016-09-14 09:39:49 +08:00
|
|
|
Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
|
2015-05-08 02:31:29 +08:00
|
|
|
PM.add(createSanitizerCoverageModulePass(Opts));
|
2014-11-12 06:15:07 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-11-11 18:45:48 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
|
2016-06-02 08:24:20 +08:00
|
|
|
bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
|
|
|
|
PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
|
|
|
|
UseAfterScope));
|
2015-11-11 18:45:48 +08:00
|
|
|
PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover));
|
2015-06-19 20:19:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
2016-06-02 08:24:20 +08:00
|
|
|
PM.add(createAddressSanitizerFunctionPass(
|
|
|
|
/*CompileKernel*/ true,
|
|
|
|
/*Recover*/ true, /*UseAfterScope*/ false));
|
2015-11-11 18:45:48 +08:00
|
|
|
PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
|
|
|
|
/*Recover*/true));
|
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();
|
2016-11-08 05:02:11 +08:00
|
|
|
int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
|
|
|
|
bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
|
|
|
|
PM.add(createMemorySanitizerPass(TrackOrigins, Recover));
|
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
|
|
|
}
|
|
|
|
|
2016-04-22 05:32:04 +08:00
|
|
|
static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
|
|
|
|
EfficiencySanitizerOptions Opts;
|
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
|
|
|
|
Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
|
2016-05-25 08:41:24 +08:00
|
|
|
else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
|
|
|
|
Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
|
2016-04-22 05:32:04 +08:00
|
|
|
PM.add(createEfficiencySanitizerPass(Opts));
|
|
|
|
}
|
|
|
|
|
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();
|
2016-01-06 22:35:46 +08:00
|
|
|
else {
|
|
|
|
// Disable individual libc/libm calls in TargetLibraryInfo.
|
|
|
|
LibFunc::Func F;
|
|
|
|
for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
|
|
|
|
if (TLII->getLibFunc(FuncName, F))
|
|
|
|
TLII->setUnavailable(F);
|
|
|
|
}
|
2015-03-18 04:03:11 +08:00
|
|
|
|
|
|
|
switch (CodeGenOpts.getVecLib()) {
|
|
|
|
case CodeGenOptions::Accelerate:
|
|
|
|
TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
|
|
|
|
break;
|
2016-07-30 00:44:24 +08:00
|
|
|
case CodeGenOptions::SVML:
|
|
|
|
TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
|
|
|
|
break;
|
2015-03-18 04:03:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-01-24 10:25:21 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
|
2016-08-13 02:12:08 +08:00
|
|
|
legacy::FunctionPassManager &FPM) {
|
2016-12-23 08:23:01 +08:00
|
|
|
// Handle disabling of all LLVM passes, where we want to preserve the
|
|
|
|
// internal module before any optimization.
|
2015-07-18 04:09:56 +08:00
|
|
|
if (CodeGenOpts.DisableLLVMPasses)
|
|
|
|
return;
|
|
|
|
|
2012-12-04 03:12:58 +08:00
|
|
|
PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
|
2015-12-08 03:21:34 +08:00
|
|
|
|
2016-11-23 04:03:35 +08:00
|
|
|
// Figure out TargetLibraryInfo. This needs to be added to MPM and FPM
|
|
|
|
// manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
|
|
|
|
// are inserted before PMBuilder ones - they'd get the default-constructed
|
|
|
|
// TLI with an unknown target otherwise.
|
2015-12-08 03:21:34 +08:00
|
|
|
Triple TargetTriple(TheModule->getTargetTriple());
|
2016-11-23 04:03:35 +08:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
|
|
|
createTLII(TargetTriple, CodeGenOpts));
|
2015-12-08 03:21:34 +08:00
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
// At O0 and O1 we only run the always inliner which is more efficient. At
|
|
|
|
// higher optimization levels we run the normal inliner.
|
|
|
|
if (CodeGenOpts.OptimizationLevel <= 1) {
|
|
|
|
bool InsertLifetimeIntrinsics = CodeGenOpts.OptimizationLevel != 0;
|
|
|
|
PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
|
|
|
|
} else {
|
|
|
|
PMBuilder.Inliner = createFunctionInliningPass(
|
|
|
|
CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize);
|
2015-12-08 03:21:34 +08:00
|
|
|
}
|
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
|
2011-05-22 07:50:44 +08:00
|
|
|
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
|
|
|
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
|
2014-10-24 08:49:29 +08:00
|
|
|
PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
|
2016-03-15 08:04:44 +08:00
|
|
|
PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
|
2015-07-07 00:23:00 +08:00
|
|
|
PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
|
2013-11-18 00:03:29 +08:00
|
|
|
PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
|
2011-07-06 06:02:36 +08:00
|
|
|
|
2016-11-23 04:03:35 +08:00
|
|
|
MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
|
|
|
|
|
2016-04-28 03:12:56 +08:00
|
|
|
// Add target-specific passes that need to run as early as possible.
|
|
|
|
if (TM)
|
|
|
|
PMBuilder.addExtension(
|
|
|
|
PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
[&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
|
|
|
|
TM->addEarlyAsPossiblePasses(PM);
|
|
|
|
});
|
|
|
|
|
2014-03-04 04:06:18 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
addAddDiscriminatorsPass);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-08 06:34:06 +08:00
|
|
|
if (CodeGenOpts.SanitizeCoverageType ||
|
|
|
|
CodeGenOpts.SanitizeCoverageIndirectCalls ||
|
|
|
|
CodeGenOpts.SanitizeCoverageTraceCmp) {
|
2014-11-12 06:15:07 +08:00
|
|
|
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
|
|
|
|
2015-06-19 20:19:07 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addKernelAddressSanitizerPasses);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addKernelAddressSanitizerPasses);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-10-04 06:44:48 +08:00
|
|
|
if (LangOpts.CoroutinesTS)
|
|
|
|
addCoroutinePassesToExtensionPoints(PMBuilder);
|
|
|
|
|
2016-04-22 05:32:04 +08:00
|
|
|
if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addEfficiencySanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addEfficiencySanitizerPass);
|
|
|
|
}
|
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Set up the per-function pass manager.
|
2016-11-23 04:03:35 +08:00
|
|
|
FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
|
2011-05-22 04:40:11 +08:00
|
|
|
if (CodeGenOpts.VerifyModule)
|
2016-07-15 08:55:40 +08:00
|
|
|
FPM.add(createVerifierPass());
|
2011-04-06 02:49:32 +08:00
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Set up the per-module pass manager.
|
2015-01-09 13:10:20 +08:00
|
|
|
if (!CodeGenOpts.RewriteMapFiles.empty())
|
2016-07-15 08:55:40 +08:00
|
|
|
addSymbolRewriterPass(CodeGenOpts, &MPM);
|
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;
|
2015-03-17 07:52:21 +08:00
|
|
|
Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
|
2016-07-15 08:55:40 +08:00
|
|
|
MPM.add(createGCOVProfilerPass(Options));
|
2016-02-02 19:06:51 +08:00
|
|
|
if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
|
2016-07-15 08:55:40 +08:00
|
|
|
MPM.add(createStripSymbolsPass(true));
|
2011-04-22 07:44:07 +08:00
|
|
|
}
|
2012-10-24 11:52:31 +08:00
|
|
|
|
2016-02-05 02:39:09 +08:00
|
|
|
if (CodeGenOpts.hasProfileClangInstr()) {
|
2014-12-09 03:04:51 +08:00
|
|
|
InstrProfOptions Options;
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
2015-05-01 07:49:42 +08:00
|
|
|
Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
|
2016-07-15 08:55:40 +08:00
|
|
|
MPM.add(createInstrProfilingLegacyPass(Options));
|
2014-12-09 03:04:51 +08:00
|
|
|
}
|
2016-03-01 02:54:59 +08:00
|
|
|
if (CodeGenOpts.hasProfileIRInstr()) {
|
2016-07-23 12:28:59 +08:00
|
|
|
PMBuilder.EnablePGOInstrGen = true;
|
2016-03-01 02:54:59 +08:00
|
|
|
if (!CodeGenOpts.InstrProfileOutput.empty())
|
|
|
|
PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
|
|
|
|
else
|
2016-07-23 06:25:01 +08:00
|
|
|
PMBuilder.PGOInstrGen = "default_%m.profraw";
|
2016-03-01 02:54:59 +08:00
|
|
|
}
|
2016-03-03 04:59:36 +08:00
|
|
|
if (CodeGenOpts.hasProfileIRUse())
|
|
|
|
PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
|
2014-12-09 03:04:51 +08:00
|
|
|
|
2016-12-15 05:41:04 +08:00
|
|
|
if (!CodeGenOpts.SampleProfileFile.empty())
|
|
|
|
PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
|
2015-08-25 23:25:13 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
PMBuilder.populateFunctionPassManager(FPM);
|
|
|
|
PMBuilder.populateModulePassManager(MPM);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2016-04-13 04:22:32 +08:00
|
|
|
void EmitAssemblyHelper::setCommandLineOpts() {
|
|
|
|
SmallVector<const char *, 16> 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());
|
|
|
|
}
|
|
|
|
for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
|
|
|
|
BackendArgs.push_back(BackendOption.c_str());
|
|
|
|
BackendArgs.push_back(nullptr);
|
|
|
|
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
|
|
|
|
BackendArgs.data());
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
void 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;
|
2016-07-15 08:55:40 +08:00
|
|
|
return;
|
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
|
|
|
|
2015-09-26 09:25:08 +08:00
|
|
|
std::string FeaturesStr =
|
|
|
|
llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
|
2011-07-19 14:37:41 +08:00
|
|
|
|
2015-09-18 19:13:43 +08:00
|
|
|
// Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
|
2016-05-19 06:04:57 +08:00
|
|
|
llvm::Optional<llvm::Reloc::Model> RM;
|
2011-07-19 14:37:41 +08:00
|
|
|
if (CodeGenOpts.RelocationModel == "static") {
|
|
|
|
RM = llvm::Reloc::Static;
|
|
|
|
} else if (CodeGenOpts.RelocationModel == "pic") {
|
|
|
|
RM = llvm::Reloc::PIC_;
|
2016-08-08 23:28:40 +08:00
|
|
|
} else if (CodeGenOpts.RelocationModel == "ropi") {
|
|
|
|
RM = llvm::Reloc::ROPI;
|
|
|
|
} else if (CodeGenOpts.RelocationModel == "rwpi") {
|
|
|
|
RM = llvm::Reloc::RWPI;
|
|
|
|
} else if (CodeGenOpts.RelocationModel == "ropi-rwpi") {
|
|
|
|
RM = llvm::Reloc::ROPI_RWPI;
|
2011-07-19 14:37:41 +08:00
|
|
|
} 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);
|
|
|
|
|
2011-12-03 06:17:00 +08:00
|
|
|
// Set float ABI type.
|
2015-10-19 04:24:53 +08:00
|
|
|
assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
|
|
|
|
CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
|
|
|
|
"Invalid Floating Point ABI!");
|
|
|
|
Options.FloatABIType =
|
|
|
|
llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
|
|
|
|
.Case("soft", llvm::FloatABI::Soft)
|
|
|
|
.Case("softfp", llvm::FloatABI::Soft)
|
|
|
|
.Case("hard", llvm::FloatABI::Hard)
|
|
|
|
.Default(llvm::FloatABI::Default);
|
2011-12-03 06:17:00 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-10-19 04:24:53 +08:00
|
|
|
Options.UseInitArray = CodeGenOpts.UseInitArray;
|
|
|
|
Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
|
|
|
|
Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
|
2016-05-29 10:01:14 +08:00
|
|
|
Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
|
2015-11-09 20:40:41 +08:00
|
|
|
|
|
|
|
// Set EABI version.
|
2016-04-09 00:52:05 +08:00
|
|
|
Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion)
|
2015-11-09 20:40:41 +08:00
|
|
|
.Case("4", llvm::EABI::EABI4)
|
|
|
|
.Case("5", llvm::EABI::EABI5)
|
|
|
|
.Case("gnu", llvm::EABI::GNU)
|
|
|
|
.Default(llvm::EABI::Default);
|
|
|
|
|
2016-05-24 11:21:01 +08:00
|
|
|
if (LangOpts.SjLjExceptions)
|
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
|
|
|
|
|
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;
|
2011-12-06 11:33:03 +08:00
|
|
|
Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
|
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;
|
2015-07-29 00:27:56 +08:00
|
|
|
Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
|
2016-02-06 07:23:25 +08:00
|
|
|
Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
|
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;
|
2015-12-22 06:09:34 +08:00
|
|
|
Options.MCOptions.MCIncrementalLinkerCompatible =
|
|
|
|
CodeGenOpts.IncrementalLinkerCompatible;
|
2016-10-20 04:24:06 +08:00
|
|
|
Options.MCOptions.MCPIECopyRelocations =
|
|
|
|
CodeGenOpts.PIECopyRelocations;
|
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;
|
2016-07-28 03:57:40 +08:00
|
|
|
Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
|
2015-01-14 08:50:32 +08:00
|
|
|
Options.MCOptions.ABIName = TargetOpts.ABI;
|
2014-05-15 09:21:56 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
|
|
|
|
Options, RM, CM, OptLevel));
|
2012-10-24 08:53:38 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
|
|
|
|
BackendAction Action,
|
2015-04-14 23:15:49 +08:00
|
|
|
raw_pwrite_stream &OS) {
|
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));
|
2016-07-15 08:55:40 +08:00
|
|
|
CodeGenPasses.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.
|
2015-05-02 08:56:15 +08:00
|
|
|
if (CodeGenOpts.OptimizationLevel > 0)
|
2016-07-15 08:55:40 +08:00
|
|
|
CodeGenPasses.add(createObjCARCContractPass());
|
2011-07-06 06:02:36 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, 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;
|
|
|
|
}
|
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS) {
|
2014-05-21 13:09:00 +08:00
|
|
|
TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2016-04-13 04:22:32 +08:00
|
|
|
setCommandLineOpts();
|
|
|
|
|
2012-10-24 11:52:31 +08:00
|
|
|
bool UsesCodeGen = (Action != Backend_EmitNothing &&
|
|
|
|
Action != Backend_EmitBC &&
|
|
|
|
Action != Backend_EmitLL);
|
2016-07-15 08:55:40 +08:00
|
|
|
CreateTargetMachine(UsesCodeGen);
|
2013-12-21 04:26:53 +08:00
|
|
|
|
2015-06-23 21:59:36 +08:00
|
|
|
if (UsesCodeGen && !TM)
|
|
|
|
return;
|
|
|
|
if (TM)
|
2015-07-25 00:04:29 +08:00
|
|
|
TheModule->setDataLayout(TM->createDataLayout());
|
2016-01-09 01:04:29 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
legacy::PassManager PerModulePasses;
|
|
|
|
PerModulePasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
|
|
|
|
|
|
|
legacy::FunctionPassManager PerFunctionPasses(TheModule);
|
|
|
|
PerFunctionPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
|
|
|
|
2016-08-13 02:12:08 +08:00
|
|
|
CreatePasses(PerModulePasses, PerFunctionPasses);
|
2016-07-15 08:55:40 +08:00
|
|
|
|
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
CodeGenPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
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:
|
2016-07-15 08:55:40 +08:00
|
|
|
PerModulePasses.add(createBitcodeWriterPass(
|
2016-04-12 02:45:20 +08:00
|
|
|
*OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitSummaryIndex,
|
|
|
|
CodeGenOpts.EmitSummaryIndex));
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitLL:
|
2016-07-15 08:55:40 +08:00
|
|
|
PerModulePasses.add(
|
2015-04-15 10:45:28 +08:00
|
|
|
createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-07-15 08:55:40 +08:00
|
|
|
if (!AddEmitPasses(CodeGenPasses, Action, *OS))
|
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.
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
{
|
2010-06-08 07:20:08 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-function optimization");
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
PerFunctionPasses.doInitialization();
|
2015-06-05 17:40:53 +08:00
|
|
|
for (Function &F : *TheModule)
|
|
|
|
if (!F.isDeclaration())
|
2016-07-15 08:55:40 +08:00
|
|
|
PerFunctionPasses.run(F);
|
|
|
|
PerFunctionPasses.doFinalization();
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
{
|
2010-06-08 07:20:08 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-module optimization passes");
|
2016-07-15 08:55:40 +08:00
|
|
|
PerModulePasses.run(*TheModule);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
{
|
2010-06-08 07:20:08 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
2016-07-15 08:55:40 +08:00
|
|
|
CodeGenPasses.run(*TheModule);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-24 04:44:01 +08:00
|
|
|
static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
|
|
|
|
switch (Opts.OptimizationLevel) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid optimization level!");
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return PassBuilder::O1;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
switch (Opts.OptimizeSize) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalide optimization level for size!");
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
return PassBuilder::O2;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
return PassBuilder::Os;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
return PassBuilder::Oz;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
return PassBuilder::O3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A clean version of `EmitAssembly` that uses the new pass manager.
|
|
|
|
///
|
|
|
|
/// Not all features are currently supported in this system, but where
|
|
|
|
/// necessary it falls back to the legacy pass manager to at least provide
|
|
|
|
/// basic functionality.
|
|
|
|
///
|
|
|
|
/// This API is planned to have its functionality finished and then to replace
|
|
|
|
/// `EmitAssembly` at some point in the future when the default switches.
|
|
|
|
void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
|
|
|
|
BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
|
|
|
|
TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
|
|
|
|
setCommandLineOpts();
|
|
|
|
|
|
|
|
// The new pass manager always makes a target machine available to passes
|
|
|
|
// during construction.
|
|
|
|
CreateTargetMachine(/*MustCreateTM*/ true);
|
|
|
|
if (!TM)
|
|
|
|
// This will already be diagnosed, just bail.
|
|
|
|
return;
|
|
|
|
TheModule->setDataLayout(TM->createDataLayout());
|
|
|
|
|
|
|
|
PassBuilder PB(TM.get());
|
|
|
|
|
|
|
|
LoopAnalysisManager LAM;
|
|
|
|
FunctionAnalysisManager FAM;
|
|
|
|
CGSCCAnalysisManager CGAM;
|
|
|
|
ModuleAnalysisManager MAM;
|
|
|
|
|
|
|
|
// Register the AA manager first so that our version is the one used.
|
|
|
|
FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
|
|
|
|
|
|
|
|
// Register all the basic analyses with the managers.
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
|
|
|
ModulePassManager MPM;
|
|
|
|
|
2016-12-27 08:13:09 +08:00
|
|
|
if (!CodeGenOpts.DisableLLVMPasses) {
|
|
|
|
if (CodeGenOpts.OptimizationLevel == 0) {
|
|
|
|
// Build a minimal pipeline based on the semantics required by Clang,
|
|
|
|
// which is just that always inlining occurs.
|
|
|
|
MPM.addPass(AlwaysInlinerPass());
|
|
|
|
} else {
|
|
|
|
// Otherwise, use the default pass pipeline. We also have to map our
|
|
|
|
// optimization levels into one of the distinct levels used to configure
|
|
|
|
// the pipeline.
|
|
|
|
PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
|
|
|
|
|
|
|
|
MPM = PB.buildPerModuleDefaultPipeline(Level);
|
|
|
|
}
|
2016-12-24 04:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We still use the legacy pass manager to do code generation. We
|
|
|
|
// create that pass manager here and use it as needed below.
|
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
bool NeedCodeGen = false;
|
|
|
|
|
|
|
|
// Append any output we need to the pass manager.
|
|
|
|
switch (Action) {
|
|
|
|
case Backend_EmitNothing:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitBC:
|
|
|
|
MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
|
|
|
|
CodeGenOpts.EmitSummaryIndex,
|
|
|
|
CodeGenOpts.EmitSummaryIndex));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitLL:
|
|
|
|
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitAssembly:
|
|
|
|
case Backend_EmitMCNull:
|
|
|
|
case Backend_EmitObj:
|
|
|
|
NeedCodeGen = true;
|
|
|
|
CodeGenPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
|
|
|
if (!AddEmitPasses(CodeGenPasses, Action, *OS))
|
|
|
|
// FIXME: Should we handle this error differently?
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before executing passes, print the final values of the LLVM options.
|
|
|
|
cl::PrintOptionValues();
|
|
|
|
|
|
|
|
// Now that we have all of the passes ready, run them.
|
|
|
|
{
|
|
|
|
PrettyStackTraceString CrashInfo("Optimizer");
|
|
|
|
MPM.run(*TheModule, MAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now if needed, run the legacy PM for codegen.
|
|
|
|
if (NeedCodeGen) {
|
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
|
|
|
CodeGenPasses.run(*TheModule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 02:12:08 +08:00
|
|
|
static void runThinLTOBackend(const CodeGenOptions &CGOpts, Module *M,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS) {
|
|
|
|
// If we are performing a ThinLTO importing compile, load the function index
|
|
|
|
// into memory and pass it into thinBackend, which will run the function
|
|
|
|
// importer and invoke LTO passes.
|
2016-11-12 03:50:39 +08:00
|
|
|
Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
|
|
|
|
llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile);
|
|
|
|
if (!IndexOrErr) {
|
|
|
|
logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
|
|
|
|
"Error loading index file '" +
|
|
|
|
CGOpts.ThinLTOIndexFile + "': ");
|
2016-08-13 02:12:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
|
|
|
|
|
|
|
|
StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
|
|
|
|
ModuleToDefinedGVSummaries;
|
|
|
|
CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
|
|
|
|
|
2016-12-29 02:00:08 +08:00
|
|
|
// We can simply import the values mentioned in the combined index, since
|
|
|
|
// we should only invoke this using the individual indexes written out
|
|
|
|
// via a WriteIndexesThinBackend.
|
2016-08-13 02:12:08 +08:00
|
|
|
FunctionImporter::ImportMapTy ImportList;
|
2016-12-29 02:00:08 +08:00
|
|
|
for (auto &GlobalList : *CombinedIndex) {
|
|
|
|
auto GUID = GlobalList.first;
|
|
|
|
assert(GlobalList.second.size() == 1 &&
|
|
|
|
"Expected individual combined index to have one summary per GUID");
|
|
|
|
auto &Summary = GlobalList.second[0];
|
|
|
|
// Skip the summaries for the importing module. These are included to
|
|
|
|
// e.g. record required linkage changes.
|
|
|
|
if (Summary->modulePath() == M->getModuleIdentifier())
|
|
|
|
continue;
|
|
|
|
// Doesn't matter what value we plug in to the map, just needs an entry
|
|
|
|
// to provoke importing by thinBackend.
|
|
|
|
ImportList[Summary->modulePath()][GUID] = 1;
|
|
|
|
}
|
2016-08-13 02:12:08 +08:00
|
|
|
|
|
|
|
std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
|
2016-12-14 09:17:59 +08:00
|
|
|
MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
|
2016-08-13 02:12:08 +08:00
|
|
|
|
|
|
|
for (auto &I : ImportList) {
|
|
|
|
ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
|
|
|
|
llvm::MemoryBuffer::getFile(I.first());
|
|
|
|
if (!MBOrErr) {
|
|
|
|
errs() << "Error loading imported file '" << I.first()
|
|
|
|
<< "': " << MBOrErr.getError().message() << "\n";
|
|
|
|
return;
|
|
|
|
}
|
2016-12-14 09:17:59 +08:00
|
|
|
|
|
|
|
Expected<std::vector<BitcodeModule>> BMsOrErr =
|
|
|
|
getBitcodeModuleList(**MBOrErr);
|
|
|
|
if (!BMsOrErr) {
|
|
|
|
handleAllErrors(BMsOrErr.takeError(), [&](ErrorInfoBase &EIB) {
|
|
|
|
errs() << "Error loading imported file '" << I.first()
|
|
|
|
<< "': " << EIB.message() << '\n';
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The bitcode file may contain multiple modules, we want the one with a
|
|
|
|
// summary.
|
|
|
|
bool FoundModule = false;
|
|
|
|
for (BitcodeModule &BM : *BMsOrErr) {
|
|
|
|
Expected<bool> HasSummary = BM.hasSummary();
|
|
|
|
if (HasSummary && *HasSummary) {
|
|
|
|
ModuleMap.insert({I.first(), BM});
|
|
|
|
FoundModule = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!FoundModule) {
|
|
|
|
errs() << "Error loading imported file '" << I.first()
|
|
|
|
<< "': Could not find module summary\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-13 02:12:08 +08:00
|
|
|
OwnedImports.push_back(std::move(*MBOrErr));
|
|
|
|
}
|
2016-09-24 05:43:51 +08:00
|
|
|
auto AddStream = [&](size_t Task) {
|
|
|
|
return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
|
2016-08-17 14:23:08 +08:00
|
|
|
};
|
2016-08-13 02:12:08 +08:00
|
|
|
lto::Config Conf;
|
|
|
|
if (Error E = thinBackend(
|
2016-09-24 05:43:51 +08:00
|
|
|
Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
|
2016-08-13 02:12:08 +08:00
|
|
|
ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2016-03-05 03:00:41 +08:00
|
|
|
const LangOptions &LOpts, const llvm::DataLayout &TDesc,
|
2014-01-02 23:08:04 +08:00
|
|
|
Module *M, BackendAction Action,
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS) {
|
2016-08-13 02:12:08 +08:00
|
|
|
if (!CGOpts.ThinLTOIndexFile.empty()) {
|
|
|
|
runThinLTOBackend(CGOpts, M, std::move(OS));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-09 01:04:29 +08:00
|
|
|
EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2016-12-24 04:44:01 +08:00
|
|
|
if (CGOpts.ExperimentalNewPassManager)
|
|
|
|
AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
|
|
|
|
else
|
|
|
|
AsmHelper.EmitAssembly(Action, std::move(OS));
|
2014-01-02 23:08:04 +08:00
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
|
|
|
|
// DataLayout.
|
|
|
|
if (AsmHelper.TM) {
|
2015-07-25 00:04:29 +08:00
|
|
|
std::string DLDesc = M->getDataLayout().getStringRepresentation();
|
2016-03-05 03:00:41 +08:00
|
|
|
if (DLDesc != TDesc.getStringRepresentation()) {
|
2014-01-02 23:08:04 +08:00
|
|
|
unsigned DiagID = Diags.getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error, "backend data layout '%0' does not match "
|
|
|
|
"expected target description '%1'");
|
2016-03-05 03:00:41 +08:00
|
|
|
Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
|
2014-01-02 23:08:04 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
2016-05-12 00:26:03 +08:00
|
|
|
|
|
|
|
static const char* getSectionNameForBitcode(const Triple &T) {
|
|
|
|
switch (T.getObjectFormat()) {
|
|
|
|
case Triple::MachO:
|
|
|
|
return "__LLVM,__bitcode";
|
|
|
|
case Triple::COFF:
|
|
|
|
case Triple::ELF:
|
|
|
|
case Triple::UnknownObjectFormat:
|
|
|
|
return ".llvmbc";
|
|
|
|
}
|
2016-05-12 05:55:37 +08:00
|
|
|
llvm_unreachable("Unimplemented ObjectFormatType");
|
2016-05-12 00:26:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* getSectionNameForCommandline(const Triple &T) {
|
|
|
|
switch (T.getObjectFormat()) {
|
|
|
|
case Triple::MachO:
|
|
|
|
return "__LLVM,__cmdline";
|
|
|
|
case Triple::COFF:
|
|
|
|
case Triple::ELF:
|
|
|
|
case Triple::UnknownObjectFormat:
|
|
|
|
return ".llvmcmd";
|
|
|
|
}
|
2016-05-12 05:55:37 +08:00
|
|
|
llvm_unreachable("Unimplemented ObjectFormatType");
|
2016-05-12 00:26:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// With -fembed-bitcode, save a copy of the llvm IR as data in the
|
|
|
|
// __LLVM,__bitcode section.
|
|
|
|
void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
|
|
|
|
llvm::MemoryBufferRef Buf) {
|
|
|
|
if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
|
|
|
|
return;
|
|
|
|
|
2016-05-17 02:54:58 +08:00
|
|
|
// Save llvm.compiler.used and remote it.
|
|
|
|
SmallVector<Constant*, 2> UsedArray;
|
|
|
|
SmallSet<GlobalValue*, 4> UsedGlobals;
|
|
|
|
Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
|
|
|
|
GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
|
|
|
|
for (auto *GV : UsedGlobals) {
|
|
|
|
if (GV->getName() != "llvm.embedded.module" &&
|
|
|
|
GV->getName() != "llvm.cmdline")
|
|
|
|
UsedArray.push_back(
|
|
|
|
ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
|
|
|
|
}
|
|
|
|
if (Used)
|
|
|
|
Used->eraseFromParent();
|
|
|
|
|
2016-05-12 00:26:03 +08:00
|
|
|
// Embed the bitcode for the llvm module.
|
|
|
|
std::string Data;
|
|
|
|
ArrayRef<uint8_t> ModuleData;
|
|
|
|
Triple T(M->getTargetTriple());
|
|
|
|
// Create a constant that contains the bitcode.
|
|
|
|
// In case of embedding a marker, ignore the input Buf and use the empty
|
|
|
|
// ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
|
|
|
|
if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
|
|
|
|
if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
|
|
|
|
(const unsigned char *)Buf.getBufferEnd())) {
|
|
|
|
// If the input is LLVM Assembly, bitcode is produced by serializing
|
|
|
|
// the module. Use-lists order need to be perserved in this case.
|
|
|
|
llvm::raw_string_ostream OS(Data);
|
|
|
|
llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
|
|
|
|
ModuleData =
|
|
|
|
ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
|
|
|
|
} else
|
|
|
|
// If the input is LLVM bitcode, write the input byte stream directly.
|
|
|
|
ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
|
|
|
|
Buf.getBufferSize());
|
|
|
|
}
|
|
|
|
llvm::Constant *ModuleConstant =
|
|
|
|
llvm::ConstantDataArray::get(M->getContext(), ModuleData);
|
|
|
|
llvm::GlobalVariable *GV = new llvm::GlobalVariable(
|
2016-05-17 02:54:58 +08:00
|
|
|
*M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
|
2016-05-12 00:26:03 +08:00
|
|
|
ModuleConstant);
|
|
|
|
GV->setSection(getSectionNameForBitcode(T));
|
2016-05-17 02:54:58 +08:00
|
|
|
UsedArray.push_back(
|
|
|
|
ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
|
2016-05-12 00:26:03 +08:00
|
|
|
if (llvm::GlobalVariable *Old =
|
2016-05-17 02:54:58 +08:00
|
|
|
M->getGlobalVariable("llvm.embedded.module", true)) {
|
|
|
|
assert(Old->hasOneUse() &&
|
|
|
|
"llvm.embedded.module can only be used once in llvm.compiler.used");
|
2016-05-12 00:26:03 +08:00
|
|
|
GV->takeName(Old);
|
|
|
|
Old->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
GV->setName("llvm.embedded.module");
|
|
|
|
}
|
|
|
|
|
2016-05-17 02:54:58 +08:00
|
|
|
// Skip if only bitcode needs to be embedded.
|
|
|
|
if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
|
|
|
|
// Embed command-line options.
|
2016-05-20 11:58:12 +08:00
|
|
|
ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
|
2016-05-17 02:54:58 +08:00
|
|
|
CGOpts.CmdArgs.size());
|
|
|
|
llvm::Constant *CmdConstant =
|
|
|
|
llvm::ConstantDataArray::get(M->getContext(), CmdData);
|
|
|
|
GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
|
|
|
|
llvm::GlobalValue::PrivateLinkage,
|
|
|
|
CmdConstant);
|
|
|
|
GV->setSection(getSectionNameForCommandline(T));
|
|
|
|
UsedArray.push_back(
|
|
|
|
ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
|
|
|
|
if (llvm::GlobalVariable *Old =
|
|
|
|
M->getGlobalVariable("llvm.cmdline", true)) {
|
|
|
|
assert(Old->hasOneUse() &&
|
|
|
|
"llvm.cmdline can only be used once in llvm.compiler.used");
|
|
|
|
GV->takeName(Old);
|
|
|
|
Old->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
GV->setName("llvm.cmdline");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UsedArray.empty())
|
2016-05-12 00:26:03 +08:00
|
|
|
return;
|
|
|
|
|
2016-05-17 02:54:58 +08:00
|
|
|
// Recreate llvm.compiler.used.
|
|
|
|
ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
|
|
|
|
auto *NewUsed = new GlobalVariable(
|
|
|
|
*M, ATy, false, llvm::GlobalValue::AppendingLinkage,
|
|
|
|
llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
|
|
|
|
NewUsed->setSection("llvm.metadata");
|
2016-05-12 00:26:03 +08:00
|
|
|
}
|