opnsense-src/lib/CodeGen/LLVMTargetMachine.cpp

429 lines
16 KiB
C++
Raw Normal View History

2009-06-02 13:52:33 -04:00
//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the LLVMTargetMachine class.
//
//===----------------------------------------------------------------------===//
#include "llvm/Target/TargetMachine.h"
#include "llvm/PassManager.h"
2010-02-16 04:30:23 -05:00
#include "llvm/Analysis/Verifier.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Assembly/PrintModulePass.h"
2009-10-14 13:57:32 -04:00
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
2010-03-16 12:51:38 -04:00
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/GCStrategy.h"
#include "llvm/CodeGen/Passes.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Target/TargetOptions.h"
2009-10-14 13:57:32 -04:00
#include "llvm/MC/MCAsmInfo.h"
2010-02-16 04:30:23 -05:00
#include "llvm/MC/MCStreamer.h"
#include "llvm/Target/TargetData.h"
2009-10-14 13:57:32 -04:00
#include "llvm/Target/TargetRegistry.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Transforms/Scalar.h"
2010-02-16 04:30:23 -05:00
#include "llvm/ADT/OwningPtr.h"
2009-06-02 13:52:33 -04:00
#include "llvm/Support/CommandLine.h"
2010-01-15 10:37:28 -05:00
#include "llvm/Support/Debug.h"
2009-10-14 13:57:32 -04:00
#include "llvm/Support/FormattedStream.h"
2009-06-02 13:52:33 -04:00
using namespace llvm;
namespace llvm {
bool EnableFastISel;
}
2009-11-05 12:17:44 -05: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-12-01 06:07:05 -05:00
static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
cl::desc("Disable tail duplication"));
2010-01-23 06:09:33 -05:00
static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
cl::desc("Disable pre-register allocation tail duplication"));
2009-11-05 12:17:44 -05: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-05-04 12:11:02 -04:00
static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
cl::Hidden,
cl::desc("Disable Machine LICM"));
2009-11-05 12:17:44 -05: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"));
2009-06-02 13:52:33 -04: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"));
static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
cl::desc("Dump garbage collector data"));
2010-05-27 11:15:58 -04: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"));
static cl::opt<bool> EnableMCLogging("enable-mc-api-logging", cl::Hidden,
cl::desc("Enable MC API logging"));
2009-06-02 13:52:33 -04:00
static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
cl::desc("Verify generated machine code"),
cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
2010-02-16 04:30:23 -05:00
static cl::opt<cl::boolOrDefault>
AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
cl::init(cl::BOU_UNSET));
static bool getVerboseAsm() {
switch (AsmVerbose) {
default:
case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
case cl::BOU_TRUE: return true;
case cl::BOU_FALSE: return false;
}
}
2010-01-15 10:37:28 -05:00
2009-06-02 13:52:33 -04:00
// Enable or disable FastISel. Both options are needed, because
// FastISel is enabled by default with -fast, and we wish to be
2009-10-14 13:57:32 -04:00
// able to enable or disable fast-isel independently from -O0.
2009-06-02 13:52:33 -04:00
static cl::opt<cl::boolOrDefault>
EnableFastISelOption("fast-isel", cl::Hidden,
2009-10-14 13:57:32 -04:00
cl::desc("Enable the \"fast\" instruction selector"));
2009-12-01 06:07:05 -05:00
// Enable or disable an experimental optimization to split GEPs
// and run a special GVN pass which does not examine loads, in
// an effort to factor out redundancy implicit in complex GEPs.
static cl::opt<bool> EnableSplitGEPGVN("split-gep-gvn", cl::Hidden,
cl::desc("Split GEPs and run no-load GVN"));
2009-10-14 13:57:32 -04:00
LLVMTargetMachine::LLVMTargetMachine(const Target &T,
2010-03-16 12:51:38 -04:00
const std::string &Triple)
: TargetMachine(T), TargetTriple(Triple) {
2009-10-14 13:57:32 -04:00
AsmInfo = T.createAsmInfo(TargetTriple);
}
2010-01-01 05:31:22 -05:00
// Set the default code model for the JIT for a generic target.
// FIXME: Is small right here? or .is64Bit() ? Large : Small?
2010-03-16 12:51:38 -04:00
void LLVMTargetMachine::setCodeModelForJIT() {
2010-01-01 05:31:22 -05:00
setCodeModel(CodeModel::Small);
}
2009-10-14 13:57:32 -04:00
2010-01-01 05:31:22 -05:00
// Set the default code model for static compilation for a generic target.
2010-03-16 12:51:38 -04:00
void LLVMTargetMachine::setCodeModelForStatic() {
2010-01-01 05:31:22 -05:00
setCodeModel(CodeModel::Small);
}
2009-06-02 13:52:33 -04:00
2010-02-16 04:30:23 -05:00
bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
formatted_raw_ostream &Out,
CodeGenFileType FileType,
2010-03-03 12:27:15 -05:00
CodeGenOpt::Level OptLevel,
bool DisableVerify) {
2009-06-02 13:52:33 -04:00
// Add common CodeGen passes.
2010-03-16 12:51:38 -04:00
MCContext *Context = 0;
if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify, Context))
2010-02-16 04:30:23 -05:00
return true;
2010-03-16 12:51:38 -04:00
assert(Context != 0 && "Failed to get MCContext");
2009-06-02 13:52:33 -04:00
2010-03-16 12:51:38 -04:00
const MCAsmInfo &MAI = *getMCAsmInfo();
2010-02-16 04:30:23 -05:00
OwningPtr<MCStreamer> AsmStreamer;
2009-06-02 13:52:33 -04:00
switch (FileType) {
2010-02-16 04:30:23 -05:00
default: return true;
case CGFT_AssemblyFile: {
MCInstPrinter *InstPrinter =
2010-04-06 11:52:58 -04:00
getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI);
2010-05-27 11:15:58 -04:00
// Create a code emitter if asked to show the encoding.
//
// FIXME: These are currently leaked.
MCCodeEmitter *MCE = 0;
if (ShowMCEncoding)
MCE = getTarget().createCodeEmitter(*this, *Context);
2010-03-16 12:51:38 -04:00
AsmStreamer.reset(createAsmStreamer(*Context, Out,
2010-02-16 04:30:23 -05:00
getTargetData()->isLittleEndian(),
getVerboseAsm(), InstPrinter,
2010-05-27 11:15:58 -04:00
MCE, ShowMCInst));
2009-06-02 13:52:33 -04:00
break;
}
2010-02-16 04:30:23 -05:00
case CGFT_ObjectFile: {
// Create the code emitter for the target if it exists. If not, .o file
// emission fails.
2010-05-27 11:15:58 -04:00
//
// FIXME: These are currently leaked.
2010-02-16 04:30:23 -05:00
MCCodeEmitter *MCE = getTarget().createCodeEmitter(*this, *Context);
2010-03-16 12:51:38 -04:00
TargetAsmBackend *TAB = getTarget().createAsmBackend(TargetTriple);
if (MCE == 0 || TAB == 0)
2010-02-16 04:30:23 -05:00
return true;
2010-05-27 11:15:58 -04:00
AsmStreamer.reset(getTarget().createObjectStreamer(TargetTriple, *Context,
*TAB, Out, MCE,
hasMCRelaxAll()));
2010-02-16 04:30:23 -05:00
break;
}
case CGFT_Null:
// The Null output is intended for use for performance analysis and testing,
// not real users.
AsmStreamer.reset(createNullStreamer(*Context));
break;
}
2010-05-27 11:15:58 -04:00
if (EnableMCLogging)
AsmStreamer.reset(createLoggingStreamer(AsmStreamer.take(), errs()));
2010-03-16 12:51:38 -04:00
// Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
2010-04-06 11:52:58 -04:00
FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
2010-02-16 04:30:23 -05:00
if (Printer == 0)
2010-01-23 06:09:33 -05:00
return true;
2010-03-16 12:51:38 -04:00
// If successful, createAsmPrinter took ownership of AsmStreamer.
AsmStreamer.take();
2010-01-01 05:31:22 -05:00
2010-02-16 04:30:23 -05:00
PM.add(Printer);
2010-01-01 05:31:22 -05:00
// Make sure the code model is set.
setCodeModelForStatic();
2009-06-02 13:52:33 -04:00
PM.add(createGCInfoDeleter());
2010-02-16 04:30:23 -05:00
return false;
2009-06-02 13:52:33 -04:00
}
/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
2010-02-16 04:30:23 -05:00
/// get machine code emitted. This uses a JITCodeEmitter object to handle
2009-06-02 13:52:33 -04: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-03-03 12:27:15 -05:00
CodeGenOpt::Level OptLevel,
bool DisableVerify) {
2010-01-01 05:31:22 -05:00
// Make sure the code model is set.
setCodeModelForJIT();
2009-06-02 13:52:33 -04:00
// Add common CodeGen passes.
2010-03-16 12:51:38 -04:00
MCContext *Ctx = 0;
if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify, Ctx))
2009-06-02 13:52:33 -04:00
return true;
2009-10-14 13:57:32 -04:00
addCodeEmitter(PM, OptLevel, JCE);
2009-06-02 13:52:33 -04:00
PM.add(createGCInfoDeleter());
return false; // success!
}
2010-03-16 12:51:38 -04:00
static void printNoVerify(PassManagerBase &PM, const char *Banner) {
2010-03-06 04:22:29 -05:00
if (PrintMachineCode)
PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
}
2009-06-02 13:52:33 -04:00
static void printAndVerify(PassManagerBase &PM,
2009-11-04 09:58:56 -05:00
const char *Banner,
2009-06-02 13:52:33 -04:00
bool allowDoubleDefs = false) {
if (PrintMachineCode)
2010-01-15 10:37:28 -05:00
PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
2009-06-02 13:52:33 -04:00
if (VerifyMachineCode)
PM.add(createMachineVerifierPass(allowDoubleDefs));
}
/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
/// emitting to assembly files or machine code output.
///
bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
2010-03-03 12:27:15 -05:00
CodeGenOpt::Level OptLevel,
2010-03-16 12:51:38 -04:00
bool DisableVerify,
MCContext *&OutContext) {
2009-06-02 13:52:33 -04:00
// Standard LLVM-Level Passes.
2010-03-03 12:27:15 -05: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());
2009-12-01 06:07:05 -05:00
// Optionally, tun split-GEPs and no-load GVN.
if (EnableSplitGEPGVN) {
PM.add(createGEPSplitterPass());
2010-03-03 12:27:15 -05:00
PM.add(createGVNPass(/*NoLoads=*/true));
2009-12-01 06:07:05 -05:00
}
2009-06-02 13:52:33 -04:00
// Run loop strength reduction before anything else.
2009-11-05 12:17:44 -05:00
if (OptLevel != CodeGenOpt::None && !DisableLSR) {
2009-06-02 13:52:33 -04:00
PM.add(createLoopStrengthReducePass(getTargetLowering()));
if (PrintLSR)
2010-01-15 10:37:28 -05:00
PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
2009-06-02 13:52:33 -04:00
}
// Turn exception handling constructs into something the code generators can
// handle.
2010-03-16 12:51:38 -04:00
switch (getMCAsmInfo()->getExceptionHandlingType()) {
2009-10-14 13:57:32 -04:00
case ExceptionHandling::SjLj:
// SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
2010-01-15 10:37:28 -05: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-10-14 13:57:32 -04:00
PM.add(createSjLjEHPass(getTargetLowering()));
2010-05-04 12:11:02 -04:00
PM.add(createDwarfEHPass(this, OptLevel==CodeGenOpt::None));
2009-10-14 13:57:32 -04:00
break;
case ExceptionHandling::Dwarf:
2010-05-04 12:11:02 -04:00
PM.add(createDwarfEHPass(this, OptLevel==CodeGenOpt::None));
2009-10-14 13:57:32 -04:00
break;
case ExceptionHandling::None:
PM.add(createLowerInvokePass(getTargetLowering()));
break;
}
2009-06-02 13:52:33 -04:00
PM.add(createGCLoweringPass());
// Make sure that no unreachable blocks are instruction selected.
PM.add(createUnreachableBlockEliminationPass());
2009-11-05 12:17:44 -05:00
if (OptLevel != CodeGenOpt::None && !DisableCGP)
2009-06-02 13:52:33 -04:00
PM.add(createCodeGenPreparePass(getTargetLowering()));
PM.add(createStackProtectorPass(getTargetLowering()));
if (PrintISelInput)
PM.add(createPrintFunctionPass("\n\n"
"*** Final LLVM Code input to ISel ***\n",
2010-01-15 10:37:28 -05:00
&dbgs()));
2009-06-02 13:52:33 -04:00
2010-03-03 12:27:15 -05: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());
2009-06-02 13:52:33 -04:00
// Standard Lower-Level Passes.
2010-03-16 12:51:38 -04:00
// Install a MachineModuleInfo class, which is an immutable pass that holds
// all the per-module stuff we're generating, including MCContext.
MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo());
PM.add(MMI);
OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
2009-06-02 13:52:33 -04:00
2009-10-14 13:57:32 -04:00
// Set up a MachineFunction for the rest of CodeGen to work on.
PM.add(new MachineFunctionAnalysis(*this, OptLevel));
2009-06-02 13:52:33 -04:00
// Enable FastISel with -fast, but allow that to be overridden.
if (EnableFastISelOption == cl::BOU_TRUE ||
(OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
EnableFastISel = true;
// Ask the target for an isel.
if (addInstSelector(PM, OptLevel))
return true;
// Print the instruction selected machine code...
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After Instruction Selection",
/* allowDoubleDefs= */ true);
2009-06-02 13:52:33 -04:00
2010-02-16 04:30:23 -05:00
// Optimize PHIs before DCE: removing dead PHI cycles may make more
// instructions dead.
if (OptLevel != CodeGenOpt::None)
PM.add(createOptimizePHIsPass());
2009-06-02 13:52:33 -04:00
if (OptLevel != CodeGenOpt::None) {
2010-07-13 13:19:57 -04: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());
printAndVerify(PM, "After codegen DCE pass",
/* allowDoubleDefs= */ true);
2010-01-15 10:37:28 -05:00
PM.add(createOptimizeExtsPass());
2009-11-05 12:17:44 -05:00
if (!DisableMachineLICM)
PM.add(createMachineLICMPass());
2010-03-10 12:45:15 -05:00
PM.add(createMachineCSEPass());
2009-11-05 12:17:44 -05:00
if (!DisableMachineSink)
PM.add(createMachineSinkingPass());
2010-03-10 12:45:15 -05:00
printAndVerify(PM, "After Machine LICM, CSE and Sinking passes",
2009-11-04 09:58:56 -05:00
/* allowDoubleDefs= */ true);
2009-06-02 13:52:33 -04:00
}
2009-12-15 13:09:07 -05:00
// Pre-ra tail duplication.
2010-01-23 06:09:33 -05:00
if (OptLevel != CodeGenOpt::None && !DisableEarlyTailDup) {
2009-12-15 13:09:07 -05:00
PM.add(createTailDuplicatePass(true));
2010-01-15 10:37:28 -05:00
printAndVerify(PM, "After Pre-RegAlloc TailDuplicate",
/* allowDoubleDefs= */ true);
2009-12-15 13:09:07 -05:00
}
2009-06-02 13:52:33 -04:00
// Run pre-ra passes.
if (addPreRegAlloc(PM, OptLevel))
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After PreRegAlloc passes",
/* allowDoubleDefs= */ true);
2009-06-02 13:52:33 -04:00
// Perform register allocation.
2010-07-13 13:19:57 -04:00
PM.add(createRegisterAllocator(OptLevel));
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After Register Allocation");
2009-06-02 13:52:33 -04:00
2010-05-04 12:11:02 -04:00
// Perform stack slot coloring and post-ra machine LICM.
if (OptLevel != CodeGenOpt::None) {
2009-10-14 13:57:32 -04:00
// FIXME: Re-enable coloring with register when it's capable of adding
// kill markers.
2010-05-04 12:11:02 -04:00
if (!DisableSSC)
PM.add(createStackSlotColoringPass(false));
// Run post-ra machine LICM to hoist reloads / remats.
if (!DisablePostRAMachineLICM)
PM.add(createMachineLICMPass(false));
printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
2009-11-04 09:58:56 -05:00
}
2009-06-02 13:52:33 -04:00
// Run post-ra passes.
if (addPostRegAlloc(PM, OptLevel))
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After PostRegAlloc passes");
2009-06-02 13:52:33 -04:00
PM.add(createLowerSubregsPass());
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After LowerSubregs");
2009-06-02 13:52:33 -04:00
// Insert prolog/epilog code. Eliminate abstract frame index references...
PM.add(createPrologEpilogCodeInserter());
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After PrologEpilogCodeInserter");
2009-06-02 13:52:33 -04:00
2009-10-14 13:57:32 -04:00
// Run pre-sched2 passes.
if (addPreSched2(PM, OptLevel))
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After PreSched2 passes");
2009-10-14 13:57:32 -04:00
2009-06-02 13:52:33 -04:00
// Second pass scheduler.
2009-11-05 12:17:44 -05:00
if (OptLevel != CodeGenOpt::None && !DisablePostRA) {
2009-10-23 10:19:52 -04:00
PM.add(createPostRAScheduler(OptLevel));
2009-11-04 09:58:56 -05:00
printAndVerify(PM, "After PostRAScheduler");
2009-06-02 13:52:33 -04:00
}
// Branch folding must be run after regalloc and prolog/epilog insertion.
2009-11-05 12:17:44 -05:00
if (OptLevel != CodeGenOpt::None && !DisableBranchFold) {
2009-06-02 13:52:33 -04:00
PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
2010-03-06 04:22:29 -05:00
printNoVerify(PM, "After BranchFolding");
2009-06-02 13:52:33 -04:00
}
2009-12-01 06:07:05 -05:00
// Tail duplication.
if (OptLevel != CodeGenOpt::None && !DisableTailDuplicate) {
2009-12-15 13:09:07 -05:00
PM.add(createTailDuplicatePass(false));
2010-03-06 04:22:29 -05:00
printNoVerify(PM, "After TailDuplicate");
2009-12-01 06:07:05 -05:00
}
2009-06-02 13:52:33 -04:00
PM.add(createGCMachineCodeAnalysisPass());
if (PrintGCInfo)
2010-01-15 10:37:28 -05:00
PM.add(createGCInfoPrinter(dbgs()));
2009-06-02 13:52:33 -04:00
2009-11-05 12:17:44 -05:00
if (OptLevel != CodeGenOpt::None && !DisableCodePlace) {
2009-11-04 09:58:56 -05:00
PM.add(createCodePlacementOptPass());
2010-03-06 04:22:29 -05:00
printNoVerify(PM, "After CodePlacementOpt");
2009-11-04 09:58:56 -05:00
}
2009-11-05 12:17:44 -05:00
if (addPreEmitPass(PM, OptLevel))
2010-03-06 04:22:29 -05:00
printNoVerify(PM, "After PreEmit passes");
2009-11-05 12:17:44 -05:00
2009-06-02 13:52:33 -04:00
return false;
}