1 //===-- AMDGPUTargetMachine.cpp - TargetMachine for hw codegen targets-----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The AMDGPU target machine contains all of the hardware specific information
11 // needed to emit code for R600 and SI GPUs.
13 //===----------------------------------------------------------------------===//
15 #include "AMDGPUTargetMachine.h"
17 #include "R600ISelLowering.h"
18 #include "R600InstrInfo.h"
19 #include "SIISelLowering.h"
20 #include "SIInstrInfo.h"
21 #include "llvm/Analysis/Passes.h"
22 #include "llvm/Analysis/Verifier.h"
23 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/PassManager.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Support/raw_os_ostream.h"
30 #include "llvm/Transforms/IPO.h"
31 #include "llvm/Transforms/Scalar.h"
32 #include <llvm/CodeGen/Passes.h>
36 extern "C" void LLVMInitializeAMDGPUTarget() {
37 // Register the target
38 RegisterTargetMachine
<AMDGPUTargetMachine
> X(TheAMDGPUTarget
);
41 AMDGPUTargetMachine::AMDGPUTargetMachine(const Target
&T
, StringRef TT
,
42 StringRef CPU
, StringRef FS
,
43 TargetOptions Options
,
44 Reloc::Model RM
, CodeModel::Model CM
,
45 CodeGenOpt::Level OptLevel
48 LLVMTargetMachine(T
, TT
, CPU
, FS
, Options
, RM
, CM
, OptLevel
),
49 Subtarget(TT
, CPU
, FS
),
50 DataLayout(Subtarget
.getDataLayout()),
51 FrameLowering(TargetFrameLowering::StackGrowsUp
,
52 Subtarget
.device()->getStackAlignment(), 0),
54 InstrItins(&Subtarget
.getInstrItineraryData()),
58 // TLInfo uses InstrInfo so it must be initialized after.
59 if (Subtarget
.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX
) {
60 InstrInfo
= new R600InstrInfo(*this);
61 TLInfo
= new R600TargetLowering(*this);
63 InstrInfo
= new SIInstrInfo(*this);
64 TLInfo
= new SITargetLowering(*this);
68 AMDGPUTargetMachine::~AMDGPUTargetMachine()
72 bool AMDGPUTargetMachine::addPassesToEmitFile(PassManagerBase
&PM
,
73 formatted_raw_ostream
&Out
,
74 CodeGenFileType FileType
,
76 // XXX: Hack here addPassesToEmitFile will fail, but this is Ok since we are
77 // only using it to access addPassesToGenerateCode()
78 bool fail
= LLVMTargetMachine::addPassesToEmitFile(PM
, Out
, FileType
,
82 const AMDGPUSubtarget
&STM
= getSubtarget
<AMDGPUSubtarget
>();
83 std::string gpu
= STM
.getDeviceName();
85 PM
.add(createSICodeEmitterPass(Out
));
86 } else if (Subtarget
.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX
) {
87 PM
.add(createR600CodeEmitterPass(Out
));
92 PM
.add(createGCInfoDeleter());
98 class AMDGPUPassConfig
: public TargetPassConfig
{
100 AMDGPUPassConfig(AMDGPUTargetMachine
*TM
, PassManagerBase
&PM
)
101 : TargetPassConfig(TM
, PM
) {}
103 AMDGPUTargetMachine
&getAMDGPUTargetMachine() const {
104 return getTM
<AMDGPUTargetMachine
>();
107 virtual bool addPreISel();
108 virtual bool addInstSelector();
109 virtual bool addPreRegAlloc();
110 virtual bool addPostRegAlloc();
111 virtual bool addPreSched2();
112 virtual bool addPreEmitPass();
114 } // End of anonymous namespace
116 TargetPassConfig
*AMDGPUTargetMachine::createPassConfig(PassManagerBase
&PM
) {
117 return new AMDGPUPassConfig(this, PM
);
121 AMDGPUPassConfig::addPreISel()
123 const AMDGPUSubtarget
&ST
= TM
->getSubtarget
<AMDGPUSubtarget
>();
124 if (ST
.device()->getGeneration() <= AMDGPUDeviceInfo::HD6XXX
) {
125 PM
->add(createR600KernelParametersPass(
126 getAMDGPUTargetMachine().getTargetData()));
131 bool AMDGPUPassConfig::addInstSelector() {
132 PM
->add(createAMDGPUPeepholeOpt(*TM
));
133 PM
->add(createAMDGPUISelDag(getAMDGPUTargetMachine()));
137 bool AMDGPUPassConfig::addPreRegAlloc() {
138 const AMDGPUSubtarget
&ST
= TM
->getSubtarget
<AMDGPUSubtarget
>();
140 if (ST
.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX
) {
141 PM
->add(createSIAssignInterpRegsPass(*TM
));
143 PM
->add(createAMDGPUConvertToISAPass(*TM
));
147 bool AMDGPUPassConfig::addPostRegAlloc() {
151 bool AMDGPUPassConfig::addPreSched2() {
153 addPass(IfConverterID
);
157 bool AMDGPUPassConfig::addPreEmitPass() {
158 PM
->add(createAMDGPUCFGPreparationPass(*TM
));
159 PM
->add(createAMDGPUCFGStructurizerPass(*TM
));