2012-05-05 04:18:50 +08: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-04 00:50:05 +08:00
|
|
|
#include "NVPTX.h"
|
2012-05-05 04:18:50 +08:00
|
|
|
#include "NVPTXAllocaHoisting.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "NVPTXLowerAggrCopies.h"
|
2014-11-13 17:26:31 +08:00
|
|
|
#include "NVPTXTargetObjectFile.h"
|
2015-01-31 19:17:59 +08:00
|
|
|
#include "NVPTXTargetTransformInfo.h"
|
2012-05-05 04:18:50 +08: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 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-12 19:10:32 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2012-05-05 04:18:50 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2012-05-05 04:18:50 +08: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 22:29:25 +08:00
|
|
|
namespace llvm {
|
|
|
|
void initializeNVVMReflectPass(PassRegistry&);
|
2013-05-20 20:13:32 +08:00
|
|
|
void initializeGenericToNVVMPass(PassRegistry&);
|
2015-03-11 03:20:52 +08:00
|
|
|
void initializeNVPTXAllocaHoistingPass(PassRegistry &);
|
2014-03-31 23:56:26 +08:00
|
|
|
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry&);
|
2014-04-04 05:18:25 +08:00
|
|
|
void initializeNVPTXFavorNonGenericAddrSpacesPass(PassRegistry &);
|
2015-06-05 05:28:26 +08:00
|
|
|
void initializeNVPTXLowerKernelArgsPass(PassRegistry &);
|
2015-06-18 06:31:02 +08:00
|
|
|
void initializeNVPTXLowerAllocaPass(PassRegistry &);
|
2013-03-30 22:29:25 +08:00
|
|
|
}
|
|
|
|
|
2012-05-05 04:18:50 +08:00
|
|
|
extern "C" void LLVMInitializeNVPTXTarget() {
|
|
|
|
// Register the target.
|
|
|
|
RegisterTargetMachine<NVPTXTargetMachine32> X(TheNVPTXTarget32);
|
|
|
|
RegisterTargetMachine<NVPTXTargetMachine64> Y(TheNVPTXTarget64);
|
|
|
|
|
2013-03-30 22:29:25 +08: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 20:13:32 +08:00
|
|
|
initializeGenericToNVVMPass(*PassRegistry::getPassRegistry());
|
2015-03-11 03:20:52 +08:00
|
|
|
initializeNVPTXAllocaHoistingPass(*PassRegistry::getPassRegistry());
|
2014-03-31 23:56:26 +08:00
|
|
|
initializeNVPTXAssignValidGlobalNamesPass(*PassRegistry::getPassRegistry());
|
2014-04-04 05:18:25 +08:00
|
|
|
initializeNVPTXFavorNonGenericAddrSpacesPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
2015-06-05 05:28:26 +08:00
|
|
|
initializeNVPTXLowerKernelArgsPass(*PassRegistry::getPassRegistry());
|
2015-06-18 06:31:02 +08:00
|
|
|
initializeNVPTXLowerAllocaPass(*PassRegistry::getPassRegistry());
|
2012-05-05 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
2015-01-27 03:03:15 +08: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;
|
|
|
|
}
|
|
|
|
|
2015-06-12 03:41:26 +08:00
|
|
|
NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
|
2014-06-27 09:27:06 +08:00
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options,
|
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL, bool is64bit)
|
2015-03-12 08:07:24 +08:00
|
|
|
: LLVMTargetMachine(T, computeDataLayout(is64bit), TT, CPU, FS, Options, RM,
|
|
|
|
CM, OL),
|
|
|
|
is64bit(is64bit), TLOF(make_unique<NVPTXTargetObjectFile>()),
|
2015-06-12 03:41:26 +08:00
|
|
|
Subtarget(TT, CPU, FS, *this) {
|
|
|
|
if (TT.getOS() == Triple::NVCL)
|
2015-02-19 08:08:14 +08:00
|
|
|
drvInterface = NVPTX::NVCL;
|
|
|
|
else
|
|
|
|
drvInterface = NVPTX::CUDA;
|
2013-05-13 09:16:13 +08:00
|
|
|
initAsmInfo();
|
|
|
|
}
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2014-11-21 07:37:18 +08:00
|
|
|
NVPTXTargetMachine::~NVPTXTargetMachine() {}
|
|
|
|
|
2012-05-05 04:18:50 +08:00
|
|
|
void NVPTXTargetMachine32::anchor() {}
|
|
|
|
|
2015-06-12 03:41:26 +08:00
|
|
|
NVPTXTargetMachine32::NVPTXTargetMachine32(const Target &T, const Triple &TT,
|
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options,
|
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL)
|
2013-03-30 22:29:21 +08:00
|
|
|
: NVPTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
void NVPTXTargetMachine64::anchor() {}
|
|
|
|
|
2015-06-12 03:41:26 +08:00
|
|
|
NVPTXTargetMachine64::NVPTXTargetMachine64(const Target &T, const Triple &TT,
|
|
|
|
StringRef CPU, StringRef FS,
|
|
|
|
const TargetOptions &Options,
|
|
|
|
Reloc::Model RM, CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL)
|
2013-03-30 22:29:21 +08:00
|
|
|
: NVPTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
|
2012-05-05 04:18:50 +08:00
|
|
|
|
2013-05-24 01:10:37 +08:00
|
|
|
namespace {
|
2012-05-05 04:18:50 +08:00
|
|
|
class NVPTXPassConfig : public TargetPassConfig {
|
|
|
|
public:
|
|
|
|
NVPTXPassConfig(NVPTXTargetMachine *TM, PassManagerBase &PM)
|
2013-03-30 22:29:21 +08:00
|
|
|
: TargetPassConfig(TM, PM) {}
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
NVPTXTargetMachine &getNVPTXTargetMachine() const {
|
|
|
|
return getTM<NVPTXTargetMachine>();
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:57:44 +08:00
|
|
|
void addIRPasses() override;
|
|
|
|
bool addInstSelector() override;
|
2014-12-12 05:26:47 +08:00
|
|
|
void addPostRegAlloc() override;
|
2014-06-28 02:35:14 +08:00
|
|
|
void addMachineSSAOptimization() override;
|
2014-04-29 15:57:44 +08:00
|
|
|
|
|
|
|
FunctionPass *createTargetRegisterAllocator(bool) override;
|
|
|
|
void addFastRegAlloc(FunctionPass *RegAllocPass) override;
|
|
|
|
void addOptimizedRegAlloc(FunctionPass *RegAllocPass) override;
|
2012-05-05 04:18:50 +08:00
|
|
|
};
|
2013-05-24 01:10:37 +08:00
|
|
|
} // end anonymous namespace
|
2012-05-05 04:18:50 +08:00
|
|
|
|
|
|
|
TargetPassConfig *NVPTXTargetMachine::createPassConfig(PassManagerBase &PM) {
|
|
|
|
NVPTXPassConfig *PassConfig = new NVPTXPassConfig(this, PM);
|
|
|
|
return PassConfig;
|
|
|
|
}
|
|
|
|
|
2015-02-01 21:20:00 +08:00
|
|
|
TargetIRAnalysis NVPTXTargetMachine::getTargetIRAnalysis() {
|
|
|
|
return TargetIRAnalysis(
|
|
|
|
[this](Function &) { return TargetTransformInfo(NVPTXTTIImpl(this)); });
|
2014-11-11 02:38:25 +08:00
|
|
|
}
|
|
|
|
|
2013-05-20 20:13:32 +08:00
|
|
|
void NVPTXPassConfig::addIRPasses() {
|
2013-05-31 20:14:49 +08: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 20:58:14 +08:00
|
|
|
disablePass(&TailDuplicateID);
|
2013-05-31 20:14:49 +08:00
|
|
|
|
2014-04-09 23:39:15 +08:00
|
|
|
addPass(createNVPTXImageOptimizerPass());
|
2013-05-20 20:13:32 +08:00
|
|
|
TargetPassConfig::addIRPasses();
|
2014-03-31 23:56:26 +08:00
|
|
|
addPass(createNVPTXAssignValidGlobalNamesPass());
|
2013-05-20 20:13:32 +08:00
|
|
|
addPass(createGenericToNVVMPass());
|
2015-06-05 05:28:26 +08:00
|
|
|
addPass(createNVPTXLowerKernelArgsPass(&getNVPTXTargetMachine()));
|
2015-06-09 08:05:56 +08:00
|
|
|
// NVPTXLowerKernelArgs emits alloca for byval parameters which can often
|
2015-06-18 06:31:02 +08:00
|
|
|
// be eliminated by SROA.
|
2015-06-09 08:05:56 +08:00
|
|
|
addPass(createSROAPass());
|
2015-06-18 06:31:02 +08:00
|
|
|
addPass(createNVPTXLowerAllocaPass());
|
|
|
|
addPass(createNVPTXFavorNonGenericAddrSpacesPass());
|
2015-04-22 04:47:15 +08:00
|
|
|
// FavorNonGenericAddrSpaces shortcuts unnecessary addrspacecasts, and leave
|
|
|
|
// them unused. We could remove dead code in an ad-hoc manner, but that
|
|
|
|
// requires manual work and might be error-prone.
|
|
|
|
addPass(createDeadCodeEliminationPass());
|
2014-05-02 02:38:36 +08:00
|
|
|
addPass(createSeparateConstOffsetFromGEPPass());
|
2015-04-24 04:00:04 +08:00
|
|
|
// ReassociateGEPs exposes more opportunites for SLSR. See
|
|
|
|
// the example in reassociate-geps-and-slsr.ll.
|
|
|
|
addPass(createStraightLineStrengthReducePass());
|
|
|
|
// SeparateConstOffsetFromGEP and SLSR creates common expressions which GVN or
|
|
|
|
// EarlyCSE can reuse. GVN generates significantly better code than EarlyCSE
|
|
|
|
// for some of our benchmarks.
|
2014-05-02 02:38:36 +08:00
|
|
|
if (getOptLevel() == CodeGenOpt::Aggressive)
|
|
|
|
addPass(createGVNPass());
|
|
|
|
else
|
|
|
|
addPass(createEarlyCSEPass());
|
2015-04-24 12:22:39 +08:00
|
|
|
// Run NaryReassociate after EarlyCSE/GVN to be more effective.
|
|
|
|
addPass(createNaryReassociatePass());
|
2015-05-28 12:56:52 +08:00
|
|
|
// NaryReassociate on GEPs creates redundant common expressions, so run
|
|
|
|
// EarlyCSE after it.
|
|
|
|
addPass(createEarlyCSEPass());
|
2013-05-20 20:13:32 +08:00
|
|
|
}
|
|
|
|
|
2012-05-05 04:18:50 +08:00
|
|
|
bool NVPTXPassConfig::addInstSelector() {
|
2015-03-21 11:13:03 +08:00
|
|
|
const NVPTXSubtarget &ST = *getTM<NVPTXTargetMachine>().getSubtargetImpl();
|
2014-04-09 23:39:15 +08:00
|
|
|
|
2012-07-03 03:48:31 +08:00
|
|
|
addPass(createLowerAggrCopies());
|
|
|
|
addPass(createAllocaHoisting());
|
|
|
|
addPass(createNVPTXISelDag(getNVPTXTargetMachine(), getOptLevel()));
|
2014-04-09 23:39:15 +08:00
|
|
|
|
|
|
|
if (!ST.hasImageHandles())
|
|
|
|
addPass(createNVPTXReplaceImageHandlesPass());
|
|
|
|
|
2015-06-25 04:20:16 +08:00
|
|
|
addPass(createNVPTXPeephole());
|
|
|
|
|
2012-05-05 04:18:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:26:47 +08:00
|
|
|
void NVPTXPassConfig::addPostRegAlloc() {
|
|
|
|
addPass(createNVPTXPrologEpilogPass(), false);
|
2013-05-31 20:14:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-01 03:21:58 +08:00
|
|
|
FunctionPass *NVPTXPassConfig::createTargetRegisterAllocator(bool) {
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr; // No reg alloc
|
2013-06-01 03:21:58 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 20:14:49 +08:00
|
|
|
void NVPTXPassConfig::addFastRegAlloc(FunctionPass *RegAllocPass) {
|
2013-06-01 03:21:58 +08:00
|
|
|
assert(!RegAllocPass && "NVPTX uses no regalloc!");
|
2013-10-11 20:39:39 +08:00
|
|
|
addPass(&PHIEliminationID);
|
|
|
|
addPass(&TwoAddressInstructionPassID);
|
2013-05-31 20:14:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVPTXPassConfig::addOptimizedRegAlloc(FunctionPass *RegAllocPass) {
|
2013-06-01 03:21:58 +08:00
|
|
|
assert(!RegAllocPass && "NVPTX uses no regalloc!");
|
2013-10-11 20:39:39 +08: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 20:14:49 +08:00
|
|
|
}
|
2014-06-28 02:35:14 +08: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");
|
|
|
|
}
|