2012-05-04 20:18:50 +00:00
|
|
|
//===-- NVPTXTargetMachine.cpp - Define TargetMachine for NVPTX -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Top-level implementation for the NVPTX target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "NVPTXTargetMachine.h"
|
|
|
|
#include "MCTargetDesc/NVPTXMCAsmInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "NVPTX.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "NVPTXAllocaHoisting.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "NVPTXLowerAggrCopies.h"
|
2014-11-13 09:26:31 +00:00
|
|
|
#include "NVPTXTargetObjectFile.h"
|
2015-01-31 11:17:59 +00:00
|
|
|
#include "NVPTXTargetTransformInfo.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-12 11:10:32 +00:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/PassManager.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2012-05-04 20:18:50 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-03-30 14:29:25 +00:00
|
|
|
namespace llvm {
|
|
|
|
void initializeNVVMReflectPass(PassRegistry&);
|
2013-05-20 12:13:32 +00:00
|
|
|
void initializeGenericToNVVMPass(PassRegistry&);
|
2014-03-31 15:56:26 +00:00
|
|
|
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry&);
|
2014-04-03 21:18:25 +00:00
|
|
|
void initializeNVPTXFavorNonGenericAddrSpacesPass(PassRegistry &);
|
2014-11-05 18:19:30 +00:00
|
|
|
void initializeNVPTXLowerStructArgsPass(PassRegistry &);
|
2013-03-30 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 20:18:50 +00:00
|
|
|
extern "C" void LLVMInitializeNVPTXTarget() {
|
|
|
|
// Register the target.
|
|
|
|
RegisterTargetMachine<NVPTXTargetMachine32> X(TheNVPTXTarget32);
|
|
|
|
RegisterTargetMachine<NVPTXTargetMachine64> Y(TheNVPTXTarget64);
|
|
|
|
|
2013-03-30 14:29:25 +00:00
|
|
|
// FIXME: This pass is really intended to be invoked during IR optimization,
|
|
|
|
// but it's very NVPTX-specific.
|
|
|
|
initializeNVVMReflectPass(*PassRegistry::getPassRegistry());
|
2013-05-20 12:13:32 +00:00
|
|
|
initializeGenericToNVVMPass(*PassRegistry::getPassRegistry());
|
2014-03-31 15:56:26 +00:00
|
|
|
initializeNVPTXAssignValidGlobalNamesPass(*PassRegistry::getPassRegistry());
|
2014-04-03 21:18:25 +00:00
|
|
|
initializeNVPTXFavorNonGenericAddrSpacesPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
2014-11-05 18:19:30 +00:00
|
|
|
initializeNVPTXLowerStructArgsPass(*PassRegistry::getPassRegistry());
|
2012-05-04 20:18:50 +00:00
|
|
|
}
|
|
|
|
|
2015-01-26 19:03:15 +00:00
|
|
|
static std::string computeDataLayout(bool is64Bit) {
|
|
|
|
std::string Ret = "e";
|
|
|
|
|
|
|
|
if (!is64Bit)
|
|
|
|
Ret += "-p:32:32";
|
|
|
|
|
|
|
|
Ret += "-i64:64-v16:16-v32:32-n16:32:64";
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2014-06-27 01:27:06 +00:00
|
|
|
NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, StringRef TT,
|
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options,
|
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL, bool is64bit)
|
2013-03-30 14:29:21 +00:00
|
|
|
: LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
|
2014-11-13 09:26:31 +00:00
|
|
|
TLOF(make_unique<NVPTXTargetObjectFile>()),
|
2015-01-26 19:03:15 +00:00
|
|
|
DL(computeDataLayout(is64bit)),
|
2014-06-27 04:33:14 +00:00
|
|
|
Subtarget(TT, CPU, FS, *this, is64bit) {
|
2013-05-13 01:16:13 +00:00
|
|
|
initAsmInfo();
|
|
|
|
}
|
2012-05-04 20:18:50 +00:00
|
|
|
|
2014-11-20 23:37:18 +00:00
|
|
|
NVPTXTargetMachine::~NVPTXTargetMachine() {}
|
|
|
|
|
2012-05-04 20:18:50 +00:00
|
|
|
void NVPTXTargetMachine32::anchor() {}
|
|
|
|
|
2013-03-30 14:29:21 +00:00
|
|
|
NVPTXTargetMachine32::NVPTXTargetMachine32(
|
|
|
|
const Target &T, StringRef TT, StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL)
|
|
|
|
: NVPTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
void NVPTXTargetMachine64::anchor() {}
|
|
|
|
|
2013-03-30 14:29:21 +00:00
|
|
|
NVPTXTargetMachine64::NVPTXTargetMachine64(
|
|
|
|
const Target &T, StringRef TT, StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL)
|
|
|
|
: NVPTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
|
2012-05-04 20:18:50 +00:00
|
|
|
|
2013-05-23 17:10:37 +00:00
|
|
|
namespace {
|
2012-05-04 20:18:50 +00:00
|
|
|
class NVPTXPassConfig : public TargetPassConfig {
|
|
|
|
public:
|
|
|
|
NVPTXPassConfig(NVPTXTargetMachine *TM, PassManagerBase &PM)
|
2013-03-30 14:29:21 +00:00
|
|
|
: TargetPassConfig(TM, PM) {}
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
NVPTXTargetMachine &getNVPTXTargetMachine() const {
|
|
|
|
return getTM<NVPTXTargetMachine>();
|
|
|
|
}
|
|
|
|
|
2014-04-29 07:57:44 +00:00
|
|
|
void addIRPasses() override;
|
|
|
|
bool addInstSelector() override;
|
2014-12-11 21:26:47 +00:00
|
|
|
void addPostRegAlloc() override;
|
2014-06-27 18:35:14 +00:00
|
|
|
void addMachineSSAOptimization() override;
|
2014-04-29 07:57:44 +00:00
|
|
|
|
|
|
|
FunctionPass *createTargetRegisterAllocator(bool) override;
|
|
|
|
void addFastRegAlloc(FunctionPass *RegAllocPass) override;
|
|
|
|
void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override;
|
2012-05-04 20:18:50 +00:00
|
|
|
};
|
2013-05-23 17:10:37 +00:00
|
|
|
} // end anonymous namespace
|
2012-05-04 20:18:50 +00:00
|
|
|
|
|
|
|
TargetPassConfig *NVPTXTargetMachine::createPassConfig(PassManagerBase &PM) {
|
|
|
|
NVPTXPassConfig *PassConfig = new NVPTXPassConfig(this, PM);
|
|
|
|
return PassConfig;
|
|
|
|
}
|
|
|
|
|
2015-02-01 13:20:00 +00:00
|
|
|
TargetIRAnalysis NVPTXTargetMachine::getTargetIRAnalysis() {
|
|
|
|
return TargetIRAnalysis(
|
|
|
|
[this](Function &) { return TargetTransformInfo(NVPTXTTIImpl(this)); });
|
2014-11-10 18:38:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 12:13:32 +00:00
|
|
|
void NVPTXPassConfig::addIRPasses() {
|
2013-05-31 12:14:49 +00:00
|
|
|
// The following passes are known to not play well with virtual regs hanging
|
|
|
|
// around after register allocation (which in our case, is *all* registers).
|
|
|
|
// We explicitly disable them here. We do, however, need some functionality
|
|
|
|
// of the PrologEpilogCodeInserter pass, so we emulate that behavior in the
|
|
|
|
// NVPTXPrologEpilog pass (see NVPTXPrologEpilogPass.cpp).
|
|
|
|
disablePass(&PrologEpilogCodeInserterID);
|
|
|
|
disablePass(&MachineCopyPropagationID);
|
|
|
|
disablePass(&BranchFolderPassID);
|
2013-11-11 12:58:14 +00:00
|
|
|
disablePass(&TailDuplicateID);
|
2013-05-31 12:14:49 +00:00
|
|
|
|
2014-04-09 15:39:15 +00:00
|
|
|
addPass(createNVPTXImageOptimizerPass());
|
2013-05-20 12:13:32 +00:00
|
|
|
TargetPassConfig::addIRPasses();
|
2014-03-31 15:56:26 +00:00
|
|
|
addPass(createNVPTXAssignValidGlobalNamesPass());
|
2013-05-20 12:13:32 +00:00
|
|
|
addPass(createGenericToNVVMPass());
|
2014-04-03 21:18:25 +00:00
|
|
|
addPass(createNVPTXFavorNonGenericAddrSpacesPass());
|
2014-05-01 18:38:36 +00:00
|
|
|
addPass(createSeparateConstOffsetFromGEPPass());
|
|
|
|
// The SeparateConstOffsetFromGEP pass creates variadic bases that can be used
|
|
|
|
// by multiple GEPs. Run GVN or EarlyCSE to really reuse them. GVN generates
|
|
|
|
// significantly better code than EarlyCSE for some of our benchmarks.
|
|
|
|
if (getOptLevel() == CodeGenOpt::Aggressive)
|
|
|
|
addPass(createGVNPass());
|
|
|
|
else
|
|
|
|
addPass(createEarlyCSEPass());
|
|
|
|
// Both FavorNonGenericAddrSpaces and SeparateConstOffsetFromGEP may leave
|
|
|
|
// some dead code. We could remove dead code in an ad-hoc manner, but that
|
|
|
|
// requires manual work and might be error-prone.
|
|
|
|
//
|
|
|
|
// The FavorNonGenericAddrSpaces pass shortcuts unnecessary addrspacecasts,
|
|
|
|
// and leave them unused.
|
|
|
|
//
|
|
|
|
// SeparateConstOffsetFromGEP rebuilds a new index from the old index, and the
|
|
|
|
// old index and some of its intermediate results may become unused.
|
2014-04-03 21:18:25 +00:00
|
|
|
addPass(createDeadCodeEliminationPass());
|
2013-05-20 12:13:32 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 20:18:50 +00:00
|
|
|
bool NVPTXPassConfig::addInstSelector() {
|
2014-04-09 15:39:15 +00:00
|
|
|
const NVPTXSubtarget &ST =
|
|
|
|
getTM<NVPTXTargetMachine>().getSubtarget<NVPTXSubtarget>();
|
|
|
|
|
2012-07-02 19:48:31 +00:00
|
|
|
addPass(createLowerAggrCopies());
|
|
|
|
addPass(createAllocaHoisting());
|
|
|
|
addPass(createNVPTXISelDag(getNVPTXTargetMachine(), getOptLevel()));
|
2014-04-09 15:39:15 +00:00
|
|
|
|
|
|
|
if (!ST.hasImageHandles())
|
|
|
|
addPass(createNVPTXReplaceImageHandlesPass());
|
|
|
|
|
2012-05-04 20:18:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 21:26:47 +00:00
|
|
|
void NVPTXPassConfig::addPostRegAlloc() {
|
|
|
|
addPass(createNVPTXPrologEpilogPass(), false);
|
2013-05-31 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 19:21:58 +00:00
|
|
|
FunctionPass *NVPTXPassConfig::createTargetRegisterAllocator(bool) {
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr; // No reg alloc
|
2013-05-31 19:21:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 12:14:49 +00:00
|
|
|
void NVPTXPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
|
2013-05-31 19:21:58 +00:00
|
|
|
assert(!RegAllocPass && "NVPTX uses no regalloc!");
|
2013-10-11 12:39:39 +00:00
|
|
|
addPass(&PHIEliminationID);
|
|
|
|
addPass(&TwoAddressInstructionPassID);
|
2013-05-31 12:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVPTXPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
|
2013-05-31 19:21:58 +00:00
|
|
|
assert(!RegAllocPass && "NVPTX uses no regalloc!");
|
2013-10-11 12:39:39 +00:00
|
|
|
|
|
|
|
addPass(&ProcessImplicitDefsID);
|
|
|
|
addPass(&LiveVariablesID);
|
|
|
|
addPass(&MachineLoopInfoID);
|
|
|
|
addPass(&PHIEliminationID);
|
|
|
|
|
|
|
|
addPass(&TwoAddressInstructionPassID);
|
|
|
|
addPass(&RegisterCoalescerID);
|
|
|
|
|
|
|
|
// PreRA instruction scheduling.
|
|
|
|
if (addPass(&MachineSchedulerID))
|
|
|
|
printAndVerify("After Machine Scheduling");
|
|
|
|
|
|
|
|
|
|
|
|
addPass(&StackSlotColoringID);
|
|
|
|
|
|
|
|
// FIXME: Needs physical registers
|
|
|
|
//addPass(&PostRAMachineLICMID);
|
|
|
|
|
|
|
|
printAndVerify("After StackSlotColoring");
|
2013-05-31 12:14:49 +00:00
|
|
|
}
|
2014-06-27 18:35:14 +00:00
|
|
|
|
|
|
|
void NVPTXPassConfig::addMachineSSAOptimization() {
|
|
|
|
// Pre-ra tail duplication.
|
|
|
|
if (addPass(&EarlyTailDuplicateID))
|
|
|
|
printAndVerify("After Pre-RegAlloc TailDuplicate");
|
|
|
|
|
|
|
|
// Optimize PHIs before DCE: removing dead PHI cycles may make more
|
|
|
|
// instructions dead.
|
|
|
|
addPass(&OptimizePHIsID);
|
|
|
|
|
|
|
|
// This pass merges large allocas. StackSlotColoring is a different pass
|
|
|
|
// which merges spill slots.
|
|
|
|
addPass(&StackColoringID);
|
|
|
|
|
|
|
|
// If the target requests it, assign local variables to stack slots relative
|
|
|
|
// to one another and simplify frame index references where possible.
|
|
|
|
addPass(&LocalStackSlotAllocationID);
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
addPass(&DeadMachineInstructionElimID);
|
|
|
|
printAndVerify("After codegen DCE pass");
|
|
|
|
|
|
|
|
// Allow targets to insert passes that improve instruction level parallelism,
|
|
|
|
// like if-conversion. Such passes will typically need dominator trees and
|
|
|
|
// loop info, just like LICM and CSE below.
|
|
|
|
if (addILPOpts())
|
|
|
|
printAndVerify("After ILP optimizations");
|
|
|
|
|
|
|
|
addPass(&MachineLICMID);
|
|
|
|
addPass(&MachineCSEID);
|
|
|
|
|
|
|
|
addPass(&MachineSinkingID);
|
|
|
|
printAndVerify("After Machine LICM, CSE and Sinking passes");
|
|
|
|
|
|
|
|
addPass(&PeepholeOptimizerID);
|
|
|
|
printAndVerify("After codegen peephole optimization pass");
|
|
|
|
}
|