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-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"
|
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"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/PassManager.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"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.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"
|
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;
|
|
|
|
|
2010-09-17 15:35:16 +08:00
|
|
|
mutable PassManager *CodeGenPasses;
|
2010-06-08 07:20:08 +08:00
|
|
|
mutable PassManager *PerModulePasses;
|
|
|
|
mutable FunctionPassManager *PerFunctionPasses;
|
|
|
|
|
|
|
|
private:
|
2013-12-21 04:26:53 +08:00
|
|
|
PassManager *getCodeGenPasses() const {
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!CodeGenPasses) {
|
2010-09-17 15:35:16 +08:00
|
|
|
CodeGenPasses = new PassManager();
|
2014-02-26 01:30:40 +08:00
|
|
|
CodeGenPasses->add(new DataLayoutPass(TheModule));
|
2013-01-07 09:38:01 +08:00
|
|
|
if (TM)
|
|
|
|
TM->addAnalysisPasses(*CodeGenPasses);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
return CodeGenPasses;
|
|
|
|
}
|
|
|
|
|
2013-12-21 04:26:53 +08:00
|
|
|
PassManager *getPerModulePasses() const {
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!PerModulePasses) {
|
|
|
|
PerModulePasses = new PassManager();
|
2014-02-26 01:30:40 +08:00
|
|
|
PerModulePasses->add(new DataLayoutPass(TheModule));
|
2013-01-07 09:38:01 +08:00
|
|
|
if (TM)
|
|
|
|
TM->addAnalysisPasses(*PerModulePasses);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
return PerModulePasses;
|
|
|
|
}
|
|
|
|
|
2013-12-21 04:26:53 +08:00
|
|
|
FunctionPassManager *getPerFunctionPasses() const {
|
2010-06-08 07:20:08 +08:00
|
|
|
if (!PerFunctionPasses) {
|
|
|
|
PerFunctionPasses = new FunctionPassManager(TheModule);
|
2014-02-26 01:30:40 +08:00
|
|
|
PerFunctionPasses->add(new DataLayoutPass(TheModule));
|
2013-01-07 09:38:01 +08:00
|
|
|
if (TM)
|
|
|
|
TM->addAnalysisPasses(*PerFunctionPasses);
|
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"),
|
2010-06-08 07:20:08 +08:00
|
|
|
CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {}
|
|
|
|
|
|
|
|
~EmitAssemblyHelper() {
|
|
|
|
delete CodeGenPasses;
|
|
|
|
delete PerModulePasses;
|
|
|
|
delete PerFunctionPasses;
|
2013-12-21 04:26:53 +08:00
|
|
|
if (CodeGenOpts.DisableFree)
|
2014-03-08 03:33:25 +08:00
|
|
|
BuryPointer(TM.release());
|
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,
|
|
|
|
PassManagerBase &PM) {
|
|
|
|
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,
|
|
|
|
PassManagerBase &PM) {
|
|
|
|
PM.add(createAddDiscriminatorsPass());
|
|
|
|
}
|
|
|
|
|
2012-05-23 01:19:45 +08:00
|
|
|
static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
|
|
|
|
PassManagerBase &PM) {
|
2012-11-23 18:39:49 +08:00
|
|
|
PM.add(createBoundsCheckingPass());
|
2012-05-23 01:19:45 +08:00
|
|
|
}
|
|
|
|
|
2012-11-30 06:36:21 +08:00
|
|
|
static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
|
|
|
PassManagerBase &PM) {
|
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
2012-12-04 03:12:58 +08:00
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
2012-11-30 06:36:21 +08:00
|
|
|
const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
|
2013-01-20 21:12:12 +08:00
|
|
|
PM.add(createAddressSanitizerFunctionPass(
|
|
|
|
LangOpts.Sanitize.InitOrder,
|
|
|
|
LangOpts.Sanitize.UseAfterReturn,
|
|
|
|
LangOpts.Sanitize.UseAfterScope,
|
2014-01-16 18:19:31 +08:00
|
|
|
CGOpts.SanitizerBlacklistFile));
|
2013-01-20 21:12:12 +08:00
|
|
|
PM.add(createAddressSanitizerModulePass(
|
|
|
|
LangOpts.Sanitize.InitOrder,
|
2014-01-16 18:19:31 +08:00
|
|
|
CGOpts.SanitizerBlacklistFile));
|
2011-11-17 01:34:26 +08:00
|
|
|
}
|
|
|
|
|
2012-12-03 21:20:43 +08:00
|
|
|
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
|
|
|
|
PassManagerBase &PM) {
|
2012-12-24 16:42:34 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
2013-01-20 21:12:12 +08:00
|
|
|
PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins,
|
2012-12-28 17:31:34 +08:00
|
|
|
CGOpts.SanitizerBlacklistFile));
|
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,
|
|
|
|
PassManagerBase &PM) {
|
2012-12-28 17:31:34 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
PM.add(createThreadSanitizerPass(CGOpts.SanitizerBlacklistFile));
|
2012-03-02 06:27:08 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 06:47:34 +08:00
|
|
|
static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
|
|
|
|
PassManagerBase &PM) {
|
2013-08-15 02:54:18 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
PM.add(createDataFlowSanitizerPass(CGOpts.SanitizerBlacklistFile));
|
2013-08-08 06:47:34 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
|
|
|
|
PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
|
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
|
|
|
|
2013-10-23 06:51:04 +08:00
|
|
|
if (LangOpts.Sanitize.LocalBounds) {
|
2012-05-23 01:19:45 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
|
|
|
|
addBoundsCheckingPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addBoundsCheckingPass);
|
|
|
|
}
|
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (LangOpts.Sanitize.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
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (LangOpts.Sanitize.Memory) {
|
2012-12-03 21:20:43 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addMemorySanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addMemorySanitizerPass);
|
|
|
|
}
|
|
|
|
|
2013-01-18 19:30:38 +08:00
|
|
|
if (LangOpts.Sanitize.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);
|
|
|
|
}
|
|
|
|
|
2013-08-08 06:47:34 +08:00
|
|
|
if (LangOpts.Sanitize.DataFlow) {
|
|
|
|
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());
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.LibraryInfo = new TargetLibraryInfo(TargetTriple);
|
2011-02-19 06:34:47 +08:00
|
|
|
if (!CodeGenOpts.SimplifyLibCalls)
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.LibraryInfo->disableAllFunctions();
|
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.
|
2013-12-21 04:26:53 +08:00
|
|
|
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.
|
2013-12-21 04:26:53 +08:00
|
|
|
PassManager *MPM = getPerModulePasses();
|
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
|
|
|
|
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;
|
2012-10-24 08:53:38 +08:00
|
|
|
return 0;
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Expose these capabilities via actual APIs!!!! Aside from just
|
|
|
|
// being gross, this is also totally broken if we ever care about
|
|
|
|
// concurrency.
|
2010-07-01 09:31:45 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose);
|
|
|
|
|
|
|
|
TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections);
|
|
|
|
TargetMachine::setDataSections (CodeGenOpts.DataSections);
|
|
|
|
|
|
|
|
// FIXME: Parse this earlier.
|
2011-07-20 22:43:06 +08:00
|
|
|
llvm::CodeModel::Model CM;
|
2010-06-08 07:20:08 +08:00
|
|
|
if (CodeGenOpts.CodeModel == "small") {
|
2011-07-20 22:43:06 +08:00
|
|
|
CM = llvm::CodeModel::Small;
|
2010-06-08 07:20:08 +08:00
|
|
|
} else if (CodeGenOpts.CodeModel == "kernel") {
|
2011-07-20 22:43:06 +08:00
|
|
|
CM = llvm::CodeModel::Kernel;
|
2010-06-08 07:20:08 +08:00
|
|
|
} else if (CodeGenOpts.CodeModel == "medium") {
|
2011-07-20 22:43:06 +08:00
|
|
|
CM = llvm::CodeModel::Medium;
|
2010-06-08 07:20:08 +08:00
|
|
|
} else if (CodeGenOpts.CodeModel == "large") {
|
2011-07-20 22:43:06 +08:00
|
|
|
CM = llvm::CodeModel::Large;
|
2010-06-08 07:20:08 +08:00
|
|
|
} else {
|
|
|
|
assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");
|
2011-07-20 22:43:06 +08:00
|
|
|
CM = llvm::CodeModel::Default;
|
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)
|
|
|
|
BackendArgs.push_back("-global-merge=false");
|
2010-06-08 07:20:08 +08:00
|
|
|
BackendArgs.push_back(0);
|
|
|
|
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;
|
2011-06-30 10:06:32 +08:00
|
|
|
if (TargetOpts.Features.size()) {
|
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-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;
|
2013-04-04 14:29:47 +08:00
|
|
|
Options.EnableSegmentedStacks = CodeGenOpts.EnableSegmentedStacks;
|
2011-12-03 06:17:00 +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
|
|
|
|
|
|
|
if (CodeGenOpts.RelaxAll)
|
|
|
|
TM->setMCRelaxAll(true);
|
2011-03-29 06:49:28 +08:00
|
|
|
if (CodeGenOpts.SaveTempLabels)
|
|
|
|
TM->setMCSaveTempLabels(true);
|
2011-05-01 02:35:43 +08:00
|
|
|
if (CodeGenOpts.NoDwarf2CFIAsm)
|
|
|
|
TM->setMCUseCFI(false);
|
2011-10-31 09:06:42 +08:00
|
|
|
if (!CodeGenOpts.NoDwarfDirectoryAsm)
|
|
|
|
TM->setMCUseDwarfDirectory(true);
|
2011-06-21 08:14:18 +08:00
|
|
|
if (CodeGenOpts.NoExecStack)
|
|
|
|
TM->setMCNoExecStack(true);
|
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.
|
2013-12-21 04:26:53 +08:00
|
|
|
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());
|
|
|
|
TargetLibraryInfo *TLI = new TargetLibraryInfo(TargetTriple);
|
2012-03-01 04:14:59 +08:00
|
|
|
if (!CodeGenOpts.SimplifyLibCalls)
|
|
|
|
TLI->disableAllFunctions();
|
|
|
|
PM->add(TLI);
|
|
|
|
|
2013-01-07 09:38:01 +08:00
|
|
|
// Add Target specific analysis passes.
|
|
|
|
TM->addAnalysisPasses(*PM);
|
2012-10-19 12:15:32 +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) {
|
|
|
|
TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
|
|
|
|
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()) {
|
|
|
|
std::string DLDesc =
|
|
|
|
AsmHelper.TM->getDataLayout()->getStringRepresentation();
|
|
|
|
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
|
|
|
}
|