2006-09-04 12:16:09 +08:00
|
|
|
//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-09-04 12:16:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVMTargetMachine class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/PassManager.h"
|
2010-02-12 18:34:29 +08:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2007-03-31 08:24:43 +08:00
|
|
|
#include "llvm/Assembly/PrintModulePass.h"
|
2009-08-14 07:48:47 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2009-08-01 02:16:33 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
|
2010-03-14 04:55:24 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/GCStrategy.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2010-11-19 10:26:16 +08:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2006-09-04 12:16:09 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2009-08-23 04:48:53 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2010-02-03 07:37:42 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2010-12-10 15:39:47 +08:00
|
|
|
#include "llvm/Target/TargetAsmInfo.h"
|
2010-02-03 07:37:42 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-07-16 07:48:37 +08:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2006-09-04 12:16:09 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2010-02-03 07:45:17 +08:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2007-03-31 08:24:43 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-01-05 06:33:16 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-15 04:18:05 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2010-10-19 02:50:27 +08:00
|
|
|
#include "llvm/Support/StandardPasses.h"
|
2006-09-04 12:16:09 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-09-25 09:14:49 +08:00
|
|
|
namespace llvm {
|
|
|
|
bool EnableFastISel;
|
|
|
|
}
|
|
|
|
|
2009-11-05 03:57:50 +08:00
|
|
|
static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
|
|
|
|
cl::desc("Disable Post Regalloc"));
|
|
|
|
static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
|
|
|
|
cl::desc("Disable branch folding"));
|
2009-11-26 08:32:21 +08:00
|
|
|
static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
|
|
|
|
cl::desc("Disable tail duplication"));
|
2010-01-16 08:29:50 +08:00
|
|
|
static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
|
|
|
|
cl::desc("Disable pre-register allocation tail duplication"));
|
2009-11-05 03:57:50 +08:00
|
|
|
static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden,
|
|
|
|
cl::desc("Disable code placement"));
|
|
|
|
static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
|
|
|
|
cl::desc("Disable Stack Slot Coloring"));
|
|
|
|
static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
|
|
|
|
cl::desc("Disable Machine LICM"));
|
2010-04-17 14:47:47 +08:00
|
|
|
static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
|
|
|
|
cl::Hidden,
|
|
|
|
cl::desc("Disable Machine LICM"));
|
2009-11-05 03:57:50 +08:00
|
|
|
static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
|
|
|
|
cl::desc("Disable Machine Sinking"));
|
|
|
|
static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
|
|
|
|
cl::desc("Disable Loop Strength Reduction Pass"));
|
|
|
|
static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
|
|
|
|
cl::desc("Disable Codegen Prepare"));
|
2007-06-19 13:47:49 +08:00
|
|
|
static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
|
|
|
|
cl::desc("Print LLVM IR produced by the loop-reduce pass"));
|
|
|
|
static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
|
|
|
|
cl::desc("Print LLVM IR input to isel pass"));
|
2008-01-07 09:33:09 +08:00
|
|
|
static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
|
|
|
|
cl::desc("Dump garbage collector data"));
|
2010-05-19 01:22:19 +08:00
|
|
|
static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
|
|
|
|
cl::desc("Show encoding in .s output"));
|
|
|
|
static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
|
|
|
|
cl::desc("Show instruction structure in .s output"));
|
2010-05-24 01:44:06 +08:00
|
|
|
static cl::opt<bool> EnableMCLogging("enable-mc-api-logging", cl::Hidden,
|
|
|
|
cl::desc("Enable MC API logging"));
|
2009-05-16 08:33:53 +08:00
|
|
|
static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
|
|
|
|
cl::desc("Verify generated machine code"),
|
|
|
|
cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
|
2007-06-19 13:47:49 +08:00
|
|
|
|
2010-02-03 06:54:51 +08:00
|
|
|
static cl::opt<cl::boolOrDefault>
|
|
|
|
AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
|
|
|
|
cl::init(cl::BOU_UNSET));
|
|
|
|
|
2010-02-03 06:58:13 +08:00
|
|
|
static bool getVerboseAsm() {
|
2010-02-03 06:54:51 +08:00
|
|
|
switch (AsmVerbose) {
|
2010-02-03 06:58:13 +08:00
|
|
|
default:
|
|
|
|
case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
|
|
|
|
case cl::BOU_TRUE: return true;
|
|
|
|
case cl::BOU_FALSE: return false;
|
2010-08-14 00:55:08 +08:00
|
|
|
}
|
2010-02-03 06:54:51 +08:00
|
|
|
}
|
2010-01-13 08:30:23 +08:00
|
|
|
|
2008-10-02 04:39:19 +08:00
|
|
|
// Enable or disable FastISel. Both options are needed, because
|
|
|
|
// FastISel is enabled by default with -fast, and we wish to be
|
2009-08-26 23:57:57 +08:00
|
|
|
// able to enable or disable fast-isel independently from -O0.
|
2008-10-08 07:00:56 +08:00
|
|
|
static cl::opt<cl::boolOrDefault>
|
2008-10-02 04:39:19 +08:00
|
|
|
EnableFastISelOption("fast-isel", cl::Hidden,
|
2009-08-26 23:57:57 +08:00
|
|
|
cl::desc("Enable the \"fast\" instruction selector"));
|
2008-09-25 09:14:49 +08:00
|
|
|
|
2009-08-12 15:22:17 +08:00
|
|
|
LLVMTargetMachine::LLVMTargetMachine(const Target &T,
|
2010-03-11 09:34:27 +08:00
|
|
|
const std::string &Triple)
|
|
|
|
: TargetMachine(T), TargetTriple(Triple) {
|
2009-08-12 15:22:17 +08:00
|
|
|
AsmInfo = T.createAsmInfo(TargetTriple);
|
|
|
|
}
|
|
|
|
|
2009-12-21 16:15:29 +08:00
|
|
|
// Set the default code model for the JIT for a generic target.
|
|
|
|
// FIXME: Is small right here? or .is64Bit() ? Large : Small?
|
2010-03-13 02:44:54 +08:00
|
|
|
void LLVMTargetMachine::setCodeModelForJIT() {
|
2009-12-21 16:15:29 +08:00
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
}
|
2009-08-12 15:22:17 +08:00
|
|
|
|
2009-12-21 16:15:29 +08:00
|
|
|
// Set the default code model for static compilation for a generic target.
|
2010-03-13 02:44:54 +08:00
|
|
|
void LLVMTargetMachine::setCodeModelForStatic() {
|
2009-12-21 16:15:29 +08:00
|
|
|
setCodeModel(CodeModel::Small);
|
|
|
|
}
|
2009-08-12 15:22:17 +08:00
|
|
|
|
2010-02-03 13:55:08 +08:00
|
|
|
bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
|
|
|
|
formatted_raw_ostream &Out,
|
|
|
|
CodeGenFileType FileType,
|
2010-02-28 08:41:59 +08:00
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
bool DisableVerify) {
|
2008-09-25 08:37:07 +08:00
|
|
|
// Add common CodeGen passes.
|
2010-03-14 04:55:24 +08:00
|
|
|
MCContext *Context = 0;
|
|
|
|
if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify, Context))
|
2010-02-03 13:55:08 +08:00
|
|
|
return true;
|
2010-03-14 04:55:24 +08:00
|
|
|
assert(Context != 0 && "Failed to get MCContext");
|
2007-02-08 09:36:53 +08:00
|
|
|
|
2010-03-12 06:53:35 +08:00
|
|
|
const MCAsmInfo &MAI = *getMCAsmInfo();
|
2010-02-03 07:45:17 +08:00
|
|
|
OwningPtr<MCStreamer> AsmStreamer;
|
|
|
|
|
2006-09-04 12:16:09 +08:00
|
|
|
switch (FileType) {
|
2010-02-03 13:55:08 +08:00
|
|
|
default: return true;
|
2010-02-03 08:29:55 +08:00
|
|
|
case CGFT_AssemblyFile: {
|
|
|
|
MCInstPrinter *InstPrinter =
|
2010-04-04 13:04:31 +08:00
|
|
|
getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI);
|
2010-05-19 01:22:19 +08:00
|
|
|
|
|
|
|
// Create a code emitter if asked to show the encoding.
|
|
|
|
MCCodeEmitter *MCE = 0;
|
2010-12-16 11:05:59 +08:00
|
|
|
TargetAsmBackend *TAB = 0;
|
|
|
|
if (ShowMCEncoding) {
|
2010-05-19 01:22:19 +08:00
|
|
|
MCE = getTarget().createCodeEmitter(*this, *Context);
|
2010-12-16 11:05:59 +08:00
|
|
|
TAB = getTarget().createAsmBackend(TargetTriple);
|
|
|
|
}
|
2010-05-19 01:22:19 +08:00
|
|
|
|
2010-12-10 15:39:47 +08:00
|
|
|
MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
|
|
|
|
getVerboseAsm(),
|
|
|
|
hasMCUseLoc(),
|
|
|
|
InstPrinter,
|
2010-12-16 11:05:59 +08:00
|
|
|
MCE, TAB,
|
2010-12-10 15:39:47 +08:00
|
|
|
ShowMCInst);
|
2010-11-19 10:26:16 +08:00
|
|
|
AsmStreamer.reset(S);
|
2010-02-03 03:14:27 +08:00
|
|
|
break;
|
2010-02-03 08:29:55 +08:00
|
|
|
}
|
2010-02-03 07:57:42 +08:00
|
|
|
case CGFT_ObjectFile: {
|
|
|
|
// Create the code emitter for the target if it exists. If not, .o file
|
|
|
|
// emission fails.
|
2010-02-13 07:12:47 +08:00
|
|
|
MCCodeEmitter *MCE = getTarget().createCodeEmitter(*this, *Context);
|
2010-03-11 09:34:27 +08:00
|
|
|
TargetAsmBackend *TAB = getTarget().createAsmBackend(TargetTriple);
|
|
|
|
if (MCE == 0 || TAB == 0)
|
2010-02-03 13:55:08 +08:00
|
|
|
return true;
|
2010-05-27 05:48:55 +08:00
|
|
|
|
|
|
|
AsmStreamer.reset(getTarget().createObjectStreamer(TargetTriple, *Context,
|
|
|
|
*TAB, Out, MCE,
|
2011-01-24 01:55:27 +08:00
|
|
|
hasMCRelaxAll(),
|
|
|
|
hasMCNoExecStack()));
|
2010-10-09 03:37:38 +08:00
|
|
|
AsmStreamer.get()->InitSections();
|
2010-02-03 07:57:42 +08:00
|
|
|
break;
|
2010-02-03 02:44:12 +08:00
|
|
|
}
|
2010-02-03 13:55:08 +08:00
|
|
|
case CGFT_Null:
|
|
|
|
// The Null output is intended for use for performance analysis and testing,
|
|
|
|
// not real users.
|
|
|
|
AsmStreamer.reset(createNullStreamer(*Context));
|
|
|
|
break;
|
2006-09-04 12:16:09 +08:00
|
|
|
}
|
2010-05-24 01:44:06 +08:00
|
|
|
|
|
|
|
if (EnableMCLogging)
|
|
|
|
AsmStreamer.reset(createLoggingStreamer(AsmStreamer.take(), errs()));
|
|
|
|
|
2010-03-14 04:55:24 +08:00
|
|
|
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
|
2010-04-04 16:18:47 +08:00
|
|
|
FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
|
2010-02-03 07:45:17 +08:00
|
|
|
if (Printer == 0)
|
2010-02-03 13:55:08 +08:00
|
|
|
return true;
|
2010-08-14 00:55:08 +08:00
|
|
|
|
2010-03-14 04:55:24 +08:00
|
|
|
// If successful, createAsmPrinter took ownership of AsmStreamer.
|
|
|
|
AsmStreamer.take();
|
2010-08-14 00:55:08 +08:00
|
|
|
|
2010-02-03 07:45:17 +08:00
|
|
|
PM.add(Printer);
|
2010-08-14 00:55:08 +08:00
|
|
|
|
2009-12-21 16:15:29 +08:00
|
|
|
// Make sure the code model is set.
|
|
|
|
setCodeModelForStatic();
|
2009-07-06 13:09:34 +08:00
|
|
|
PM.add(createGCInfoDeleter());
|
2010-02-03 13:55:08 +08:00
|
|
|
return false;
|
2008-09-25 08:37:07 +08:00
|
|
|
}
|
|
|
|
|
2009-05-31 04:51:52 +08:00
|
|
|
/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
|
2010-02-03 06:31:11 +08:00
|
|
|
/// get machine code emitted. This uses a JITCodeEmitter object to handle
|
2009-05-31 04:51:52 +08:00
|
|
|
/// actually outputting the machine code and resolving things like the address
|
|
|
|
/// of functions. This method should returns true if machine code emission is
|
|
|
|
/// not supported.
|
|
|
|
///
|
|
|
|
bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
|
|
|
|
JITCodeEmitter &JCE,
|
2010-02-28 08:41:59 +08:00
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
bool DisableVerify) {
|
2009-12-21 16:15:29 +08:00
|
|
|
// Make sure the code model is set.
|
|
|
|
setCodeModelForJIT();
|
2010-08-14 00:55:08 +08:00
|
|
|
|
2009-05-31 04:51:52 +08:00
|
|
|
// Add common CodeGen passes.
|
2010-03-14 04:55:24 +08:00
|
|
|
MCContext *Ctx = 0;
|
|
|
|
if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify, Ctx))
|
2009-05-31 04:51:52 +08:00
|
|
|
return true;
|
|
|
|
|
2009-07-16 06:33:19 +08:00
|
|
|
addCodeEmitter(PM, OptLevel, JCE);
|
2009-05-31 04:51:52 +08:00
|
|
|
PM.add(createGCInfoDeleter());
|
|
|
|
|
|
|
|
return false; // success!
|
|
|
|
}
|
|
|
|
|
2010-07-22 13:58:53 +08:00
|
|
|
/// addPassesToEmitMC - Add passes to the specified pass manager to get
|
|
|
|
/// machine code emitted with the MCJIT. This method returns true if machine
|
|
|
|
/// code is not supported. It fills the MCContext Ctx pointer which can be
|
|
|
|
/// used to build custom MCStreamer.
|
|
|
|
///
|
|
|
|
bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
|
|
|
|
MCContext *&Ctx,
|
|
|
|
CodeGenOpt::Level OptLevel,
|
|
|
|
bool DisableVerify) {
|
|
|
|
// Add common CodeGen passes.
|
|
|
|
if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify, Ctx))
|
|
|
|
return true;
|
|
|
|
// Make sure the code model is set.
|
|
|
|
setCodeModelForJIT();
|
|
|
|
|
|
|
|
return false; // success!
|
|
|
|
}
|
|
|
|
|
2010-03-14 04:55:24 +08:00
|
|
|
static void printNoVerify(PassManagerBase &PM, const char *Banner) {
|
2010-03-06 05:49:13 +08:00
|
|
|
if (PrintMachineCode)
|
|
|
|
PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
|
|
|
|
}
|
|
|
|
|
2009-05-16 08:33:53 +08:00
|
|
|
static void printAndVerify(PassManagerBase &PM,
|
2010-08-06 02:59:59 +08:00
|
|
|
const char *Banner) {
|
2009-05-16 08:33:53 +08:00
|
|
|
if (PrintMachineCode)
|
2010-01-05 06:33:16 +08:00
|
|
|
PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
|
2009-05-16 08:33:53 +08:00
|
|
|
|
|
|
|
if (VerifyMachineCode)
|
2010-12-18 08:06:56 +08:00
|
|
|
PM.add(createMachineVerifierPass(Banner));
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
|
|
|
|
2009-04-30 07:29:43 +08:00
|
|
|
/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
|
|
|
|
/// emitting to assembly files or machine code output.
|
2008-09-25 08:37:07 +08:00
|
|
|
///
|
2009-04-29 08:15:41 +08:00
|
|
|
bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
|
2010-02-28 08:41:59 +08:00
|
|
|
CodeGenOpt::Level OptLevel,
|
2010-03-14 04:55:24 +08:00
|
|
|
bool DisableVerify,
|
|
|
|
MCContext *&OutContext) {
|
2006-09-04 12:16:09 +08:00
|
|
|
// Standard LLVM-Level Passes.
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2010-10-19 02:04:47 +08:00
|
|
|
// Basic AliasAnalysis support.
|
2010-10-19 02:50:27 +08:00
|
|
|
createStandardAliasAnalysisPasses(&PM);
|
2010-10-19 02:04:47 +08:00
|
|
|
|
2010-02-28 08:41:59 +08:00
|
|
|
// Before running any passes, run the verifier to determine if the input
|
|
|
|
// coming from the front-end and/or optimizer is valid.
|
|
|
|
if (!DisableVerify)
|
|
|
|
PM.add(createVerifierPass());
|
|
|
|
|
2006-09-04 12:16:09 +08:00
|
|
|
// Run loop strength reduction before anything else.
|
2009-11-05 03:57:50 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && !DisableLSR) {
|
2007-03-31 12:18:03 +08:00
|
|
|
PM.add(createLoopStrengthReducePass(getTargetLowering()));
|
|
|
|
if (PrintLSR)
|
2010-01-05 06:33:16 +08:00
|
|
|
PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
|
2007-03-31 12:18:03 +08:00
|
|
|
}
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2010-08-05 05:44:13 +08:00
|
|
|
PM.add(createGCLoweringPass());
|
|
|
|
|
|
|
|
// Make sure that no unreachable blocks are instruction selected.
|
|
|
|
PM.add(createUnreachableBlockEliminationPass());
|
|
|
|
|
2009-05-23 04:36:31 +08:00
|
|
|
// Turn exception handling constructs into something the code generators can
|
|
|
|
// handle.
|
2010-03-14 04:55:24 +08:00
|
|
|
switch (getMCAsmInfo()->getExceptionHandlingType()) {
|
2009-08-11 08:09:57 +08:00
|
|
|
case ExceptionHandling::SjLj:
|
2009-08-18 00:41:22 +08:00
|
|
|
// SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
|
2010-01-15 05:38:31 +08:00
|
|
|
// Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
|
|
|
|
// catch info can get misplaced when a selector ends up more than one block
|
|
|
|
// removed from the parent invoke(s). This could happen when a landing
|
|
|
|
// pad is shared by multiple invokes and is also a target of a normal
|
|
|
|
// edge from elsewhere.
|
2009-08-18 00:41:22 +08:00
|
|
|
PM.add(createSjLjEHPass(getTargetLowering()));
|
2010-08-05 07:36:02 +08:00
|
|
|
// FALLTHROUGH
|
2011-01-15 05:58:08 +08:00
|
|
|
case ExceptionHandling::DwarfCFI:
|
|
|
|
case ExceptionHandling::DwarfTable:
|
2010-08-31 17:05:06 +08:00
|
|
|
PM.add(createDwarfEHPass(this));
|
2009-08-11 08:09:57 +08:00
|
|
|
break;
|
|
|
|
case ExceptionHandling::None:
|
|
|
|
PM.add(createLowerInvokePass(getTargetLowering()));
|
2010-08-05 07:36:02 +08:00
|
|
|
|
|
|
|
// The lower invoke pass may create unreachable code. Remove it.
|
|
|
|
PM.add(createUnreachableBlockEliminationPass());
|
2009-08-11 08:09:57 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-05-23 04:36:31 +08:00
|
|
|
|
2009-11-05 03:57:50 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && !DisableCGP)
|
2007-03-31 12:18:03 +08:00
|
|
|
PM.add(createCodeGenPreparePass(getTargetLowering()));
|
|
|
|
|
2008-11-13 09:02:14 +08:00
|
|
|
PM.add(createStackProtectorPass(getTargetLowering()));
|
2008-11-04 10:10:20 +08:00
|
|
|
|
2010-07-25 04:48:54 +08:00
|
|
|
addPreISel(PM, OptLevel);
|
|
|
|
|
2007-03-31 12:18:03 +08:00
|
|
|
if (PrintISelInput)
|
2008-10-22 07:33:38 +08:00
|
|
|
PM.add(createPrintFunctionPass("\n\n"
|
|
|
|
"*** Final LLVM Code input to ISel ***\n",
|
2010-01-05 06:33:16 +08:00
|
|
|
&dbgs()));
|
2007-03-31 12:18:03 +08:00
|
|
|
|
2010-02-28 08:41:59 +08:00
|
|
|
// All passes which modify the LLVM IR are now complete; run the verifier
|
|
|
|
// to ensure that the IR is valid.
|
|
|
|
if (!DisableVerify)
|
|
|
|
PM.add(createVerifierPass());
|
|
|
|
|
2008-09-25 08:37:07 +08:00
|
|
|
// Standard Lower-Level Passes.
|
2010-08-14 00:55:08 +08:00
|
|
|
|
2010-03-14 04:55:24 +08:00
|
|
|
// Install a MachineModuleInfo class, which is an immutable pass that holds
|
|
|
|
// all the per-module stuff we're generating, including MCContext.
|
2010-12-10 15:39:47 +08:00
|
|
|
TargetAsmInfo *TAI = new TargetAsmInfo(*this);
|
|
|
|
MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(), TAI);
|
2010-03-14 04:55:24 +08:00
|
|
|
PM.add(MMI);
|
|
|
|
OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2009-08-01 02:16:33 +08:00
|
|
|
// Set up a MachineFunction for the rest of CodeGen to work on.
|
|
|
|
PM.add(new MachineFunctionAnalysis(*this, OptLevel));
|
|
|
|
|
2008-10-02 04:39:19 +08:00
|
|
|
// Enable FastISel with -fast, but allow that to be overridden.
|
2008-10-08 07:00:56 +08:00
|
|
|
if (EnableFastISelOption == cl::BOU_TRUE ||
|
2009-04-30 07:29:43 +08:00
|
|
|
(OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
|
2008-10-02 04:39:19 +08:00
|
|
|
EnableFastISel = true;
|
|
|
|
|
2006-09-04 12:16:09 +08:00
|
|
|
// Ask the target for an isel.
|
2009-04-29 08:15:41 +08:00
|
|
|
if (addInstSelector(PM, OptLevel))
|
2006-09-04 12:16:09 +08:00
|
|
|
return true;
|
2007-02-08 09:36:53 +08:00
|
|
|
|
2006-09-04 12:16:09 +08:00
|
|
|
// Print the instruction selected machine code...
|
2010-08-06 02:59:59 +08:00
|
|
|
printAndVerify(PM, "After Instruction Selection");
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2010-11-19 02:48:28 +08:00
|
|
|
// Expand pseudo-instructions emitted by ISel.
|
|
|
|
PM.add(createExpandISelPseudosPass());
|
2010-11-17 05:02:37 +08:00
|
|
|
|
2010-02-13 08:31:44 +08:00
|
|
|
// Optimize PHIs before DCE: removing dead PHI cycles may make more
|
|
|
|
// instructions dead.
|
|
|
|
if (OptLevel != CodeGenOpt::None)
|
|
|
|
PM.add(createOptimizePHIsPass());
|
2010-02-06 17:07:11 +08:00
|
|
|
|
2010-08-25 03:05:43 +08:00
|
|
|
// If the target requests it, assign local variables to stack slots relative
|
|
|
|
// to one another and simplify frame index references where possible.
|
|
|
|
PM.add(createLocalStackSlotAllocationPass());
|
2010-08-14 08:15:52 +08:00
|
|
|
|
2010-07-10 00:37:18 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None) {
|
2010-07-10 17:00:22 +08:00
|
|
|
// With optimization, dead code should already be eliminated. However
|
|
|
|
// there is one known exception: lowered code for arguments that are only
|
|
|
|
// used by tail calls, where the tail calls reuse the incoming stack
|
|
|
|
// arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
|
|
|
|
PM.add(createDeadMachineInstructionElimPass());
|
2010-08-06 02:59:59 +08:00
|
|
|
printAndVerify(PM, "After codegen DCE pass");
|
2010-07-10 17:00:22 +08:00
|
|
|
|
2009-11-05 03:57:50 +08:00
|
|
|
if (!DisableMachineLICM)
|
|
|
|
PM.add(createMachineLICMPass());
|
2010-03-10 11:07:41 +08:00
|
|
|
PM.add(createMachineCSEPass());
|
2009-11-05 03:57:50 +08:00
|
|
|
if (!DisableMachineSink)
|
|
|
|
PM.add(createMachineSinkingPass());
|
2010-08-06 02:59:59 +08:00
|
|
|
printAndVerify(PM, "After Machine LICM, CSE and Sinking passes");
|
2010-11-18 04:13:28 +08:00
|
|
|
|
|
|
|
PM.add(createPeepholeOptimizerPass());
|
|
|
|
printAndVerify(PM, "After codegen peephole optimization pass");
|
2009-02-09 16:45:39 +08:00
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2009-12-04 17:42:45 +08:00
|
|
|
// Pre-ra tail duplication.
|
2010-01-16 08:29:50 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && !DisableEarlyTailDup) {
|
2009-12-04 17:42:45 +08:00
|
|
|
PM.add(createTailDuplicatePass(true));
|
2010-08-06 02:59:59 +08:00
|
|
|
printAndVerify(PM, "After Pre-RegAlloc TailDuplicate");
|
2009-12-04 17:42:45 +08:00
|
|
|
}
|
|
|
|
|
2008-04-24 02:26:03 +08:00
|
|
|
// Run pre-ra passes.
|
2009-05-16 08:33:53 +08:00
|
|
|
if (addPreRegAlloc(PM, OptLevel))
|
2010-08-06 02:59:59 +08:00
|
|
|
printAndVerify(PM, "After PreRegAlloc passes");
|
2008-04-24 02:26:03 +08:00
|
|
|
|
2008-06-04 17:18:41 +08:00
|
|
|
// Perform register allocation.
|
2010-05-28 07:57:25 +08:00
|
|
|
PM.add(createRegisterAllocator(OptLevel));
|
2009-11-01 04:17:39 +08:00
|
|
|
printAndVerify(PM, "After Register Allocation");
|
2008-06-04 17:18:41 +08:00
|
|
|
|
2010-04-17 14:47:47 +08:00
|
|
|
// Perform stack slot coloring and post-ra machine LICM.
|
|
|
|
if (OptLevel != CodeGenOpt::None) {
|
2009-08-05 15:26:17 +08:00
|
|
|
// FIXME: Re-enable coloring with register when it's capable of adding
|
|
|
|
// kill markers.
|
2010-04-17 14:47:47 +08:00
|
|
|
if (!DisableSSC)
|
|
|
|
PM.add(createStackSlotColoringPass(false));
|
2010-04-07 08:41:17 +08:00
|
|
|
|
|
|
|
// Run post-ra machine LICM to hoist reloads / remats.
|
2010-04-17 14:47:47 +08:00
|
|
|
if (!DisablePostRAMachineLICM)
|
|
|
|
PM.add(createMachineLICMPass(false));
|
|
|
|
|
|
|
|
printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
|
2009-11-01 04:17:39 +08:00
|
|
|
}
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2008-06-04 17:18:41 +08:00
|
|
|
// Run post-ra passes.
|
2009-05-16 08:33:53 +08:00
|
|
|
if (addPostRegAlloc(PM, OptLevel))
|
2009-11-01 04:17:39 +08:00
|
|
|
printAndVerify(PM, "After PostRegAlloc passes");
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2007-07-27 15:36:14 +08:00
|
|
|
PM.add(createLowerSubregsPass());
|
2009-11-01 04:17:39 +08:00
|
|
|
printAndVerify(PM, "After LowerSubregs");
|
2007-02-08 09:36:53 +08:00
|
|
|
|
2006-09-04 12:16:09 +08:00
|
|
|
// Insert prolog/epilog code. Eliminate abstract frame index references...
|
|
|
|
PM.add(createPrologEpilogCodeInserter());
|
2009-11-01 04:17:39 +08:00
|
|
|
printAndVerify(PM, "After PrologEpilogCodeInserter");
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2009-09-30 16:49:50 +08:00
|
|
|
// Run pre-sched2 passes.
|
|
|
|
if (addPreSched2(PM, OptLevel))
|
2009-11-01 04:17:39 +08:00
|
|
|
printAndVerify(PM, "After PreSched2 passes");
|
2009-09-30 16:49:50 +08:00
|
|
|
|
2007-07-14 01:13:54 +08:00
|
|
|
// Second pass scheduler.
|
2009-11-05 03:57:50 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && !DisablePostRA) {
|
2009-10-17 05:06:15 +08:00
|
|
|
PM.add(createPostRAScheduler(OptLevel));
|
2009-11-01 04:17:39 +08:00
|
|
|
printAndVerify(PM, "After PostRAScheduler");
|
2008-11-21 03:54:21 +08:00
|
|
|
}
|
|
|
|
|
2008-12-18 09:36:42 +08:00
|
|
|
// Branch folding must be run after regalloc and prolog/epilog insertion.
|
2009-11-05 03:57:50 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && !DisableBranchFold) {
|
2009-10-29 04:46:46 +08:00
|
|
|
PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
|
2010-03-06 05:49:13 +08:00
|
|
|
printNoVerify(PM, "After BranchFolding");
|
2009-05-16 08:33:53 +08:00
|
|
|
}
|
2008-12-18 09:36:42 +08:00
|
|
|
|
2009-11-26 08:32:21 +08:00
|
|
|
// Tail duplication.
|
|
|
|
if (OptLevel != CodeGenOpt::None && !DisableTailDuplicate) {
|
2009-12-04 17:42:45 +08:00
|
|
|
PM.add(createTailDuplicatePass(false));
|
2010-03-06 05:49:13 +08:00
|
|
|
printNoVerify(PM, "After TailDuplicate");
|
2009-11-26 08:32:21 +08:00
|
|
|
}
|
|
|
|
|
2008-01-07 09:33:09 +08:00
|
|
|
PM.add(createGCMachineCodeAnalysisPass());
|
2008-09-25 08:37:07 +08:00
|
|
|
|
2008-01-07 09:33:09 +08:00
|
|
|
if (PrintGCInfo)
|
2010-01-05 06:33:16 +08:00
|
|
|
PM.add(createGCInfoPrinter(dbgs()));
|
2007-02-08 09:36:53 +08:00
|
|
|
|
2009-11-05 03:57:50 +08:00
|
|
|
if (OptLevel != CodeGenOpt::None && !DisableCodePlace) {
|
2009-11-01 04:17:39 +08:00
|
|
|
PM.add(createCodePlacementOptPass());
|
2010-03-06 05:49:13 +08:00
|
|
|
printNoVerify(PM, "After CodePlacementOpt");
|
2009-11-01 04:17:39 +08:00
|
|
|
}
|
|
|
|
|
2009-11-05 09:16:59 +08:00
|
|
|
if (addPreEmitPass(PM, OptLevel))
|
2010-03-06 05:49:13 +08:00
|
|
|
printNoVerify(PM, "After PreEmit passes");
|
2009-11-05 09:16:59 +08:00
|
|
|
|
2008-09-25 08:37:07 +08:00
|
|
|
return false;
|
2006-09-04 12:16:09 +08:00
|
|
|
}
|