blob: f8787a7cd5bc39758034b9f9d1d55f4807969e4a [file] [log] [blame]
Nick Lewyckyf7a3c502010-09-07 18:14:24 +00001//===-- PTXTargetMachine.cpp - Define TargetMachine for PTX ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Top-level implementation for the PTX target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PTX.h"
15#include "PTXTargetMachine.h"
Eric Christopher50880d02010-09-18 18:52:28 +000016#include "llvm/PassManager.h"
Justin Holewinski40466cc2011-09-22 16:45:37 +000017#include "llvm/Analysis/Passes.h"
18#include "llvm/Analysis/Verifier.h"
19#include "llvm/Assembly/PrintModulePass.h"
20#include "llvm/ADT/OwningPtr.h"
21#include "llvm/CodeGen/AsmPrinter.h"
22#include "llvm/CodeGen/MachineFunctionAnalysis.h"
23#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSubtargetInfo.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Che-Liang Chiouf48817c2011-03-02 07:36:48 +000030#include "llvm/Support/raw_ostream.h"
Justin Holewinski40466cc2011-09-22 16:45:37 +000031#include "llvm/Target/TargetData.h"
32#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetLowering.h"
34#include "llvm/Target/TargetLoweringObjectFile.h"
35#include "llvm/Target/TargetMachine.h"
36#include "llvm/Target/TargetOptions.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38#include "llvm/Target/TargetSubtargetInfo.h"
39#include "llvm/Transforms/Scalar.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/TargetRegistry.h"
42
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000043
44using namespace llvm;
45
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000046namespace llvm {
47 MCStreamer *createPTXAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
Rafael Espindola89b93722010-12-10 07:39:47 +000048 bool isVerboseAsm, bool useLoc,
Nick Lewycky44d798d2011-10-17 23:05:28 +000049 bool useCFI, bool useDwarfDirectory,
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000050 MCInstPrinter *InstPrint,
51 MCCodeEmitter *CE,
Evan Cheng78c10ee2011-07-25 23:24:55 +000052 MCAsmBackend *MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +000053 bool ShowInst);
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000054}
55
Eric Christopher50880d02010-09-18 18:52:28 +000056extern "C" void LLVMInitializePTXTarget() {
Justin Holewinskie1fee482011-04-20 15:37:17 +000057
58 RegisterTargetMachine<PTX32TargetMachine> X(ThePTX32Target);
59 RegisterTargetMachine<PTX64TargetMachine> Y(ThePTX64Target);
60
Justin Holewinskie1fee482011-04-20 15:37:17 +000061 TargetRegistry::RegisterAsmStreamer(ThePTX32Target, createPTXAsmStreamer);
62 TargetRegistry::RegisterAsmStreamer(ThePTX64Target, createPTXAsmStreamer);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000063}
64
Che-Liang Chiouf48817c2011-03-02 07:36:48 +000065namespace {
Che-Liang Chiou31c488c2011-03-02 07:58:46 +000066 const char* DataLayout32 =
67 "e-p:32:32-i64:32:32-f64:32:32-v128:32:128-v64:32:64-n32:64";
68 const char* DataLayout64 =
69 "e-p:64:64-i64:32:32-f64:32:32-v128:32:128-v64:32:64-n32:64";
Che-Liang Chiouf48817c2011-03-02 07:36:48 +000070}
71
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000072// DataLayout and FrameLowering are filled with dummy data
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000073PTXTargetMachine::PTXTargetMachine(const Target &T,
Evan Cheng34ad6db2011-07-20 07:51:56 +000074 StringRef TT, StringRef CPU, StringRef FS,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000075 const TargetOptions &Options,
Evan Cheng34ad6db2011-07-20 07:51:56 +000076 Reloc::Model RM, CodeModel::Model CM,
Evan Chengb95fc312011-11-16 08:38:26 +000077 CodeGenOpt::Level OL,
Evan Cheng34ad6db2011-07-20 07:51:56 +000078 bool is64Bit)
Nick Lewycky8a8d4792011-12-02 22:16:29 +000079 : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
Justin Holewinskie1fee482011-04-20 15:37:17 +000080 DataLayout(is64Bit ? DataLayout64 : DataLayout32),
Evan Cheng276365d2011-06-30 01:53:36 +000081 Subtarget(TT, CPU, FS, is64Bit),
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000082 FrameLowering(Subtarget),
Che-Liang Chiou31c488c2011-03-02 07:58:46 +000083 InstrInfo(*this),
Justin Holewinskibc97f442011-09-26 18:57:27 +000084 TSInfo(*this),
Che-Liang Chiou31c488c2011-03-02 07:58:46 +000085 TLInfo(*this) {
Eric Christopher50880d02010-09-18 18:52:28 +000086}
87
David Blaikie2d24e2a2011-12-20 02:50:00 +000088void PTX32TargetMachine::anchor() { }
89
Evan Cheng43966132011-07-19 06:37:02 +000090PTX32TargetMachine::PTX32TargetMachine(const Target &T, StringRef TT,
91 StringRef CPU, StringRef FS,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000092 const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +000093 Reloc::Model RM, CodeModel::Model CM,
94 CodeGenOpt::Level OL)
Nick Lewycky8a8d4792011-12-02 22:16:29 +000095 : PTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {
Justin Holewinskie1fee482011-04-20 15:37:17 +000096}
97
David Blaikie2d24e2a2011-12-20 02:50:00 +000098void PTX64TargetMachine::anchor() { }
99
Evan Cheng43966132011-07-19 06:37:02 +0000100PTX64TargetMachine::PTX64TargetMachine(const Target &T, StringRef TT,
101 StringRef CPU, StringRef FS,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000102 const TargetOptions &Options,
Evan Chengb95fc312011-11-16 08:38:26 +0000103 Reloc::Model RM, CodeModel::Model CM,
104 CodeGenOpt::Level OL)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000105 : PTXTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000106}
107
Evan Chengb95fc312011-11-16 08:38:26 +0000108bool PTXTargetMachine::addInstSelector(PassManagerBase &PM) {
109 PM.add(createPTXISelDag(*this, getOptLevel()));
Che-Liang Chiouad83c1d2011-01-01 10:50:37 +0000110 return false;
111}
112
Evan Chengb95fc312011-11-16 08:38:26 +0000113bool PTXTargetMachine::addPostRegAlloc(PassManagerBase &PM) {
Che-Liang Chiouad83c1d2011-01-01 10:50:37 +0000114 // PTXMFInfoExtract must after register allocation!
Evan Chengb95fc312011-11-16 08:38:26 +0000115 //PM.add(createPTXMFInfoExtract(*this));
Eric Christopher50880d02010-09-18 18:52:28 +0000116 return false;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000117}
Justin Holewinski40466cc2011-09-22 16:45:37 +0000118
119bool PTXTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
120 formatted_raw_ostream &Out,
121 CodeGenFileType FileType,
Justin Holewinski40466cc2011-09-22 16:45:37 +0000122 bool DisableVerify) {
123 // This is mostly based on LLVMTargetMachine::addPassesToEmitFile
124
125 // Add common CodeGen passes.
126 MCContext *Context = 0;
Evan Chengb95fc312011-11-16 08:38:26 +0000127 if (addCommonCodeGenPasses(PM, DisableVerify, Context))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000128 return true;
129 assert(Context != 0 && "Failed to get MCContext");
130
131 if (hasMCSaveTempLabels())
132 Context->setAllowTemporaryLabels(false);
133
134 const MCAsmInfo &MAI = *getMCAsmInfo();
135 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
136 OwningPtr<MCStreamer> AsmStreamer;
137
138 switch (FileType) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000139 case CGFT_AssemblyFile: {
140 MCInstPrinter *InstPrinter =
141 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
142
143 // Create a code emitter if asked to show the encoding.
144 MCCodeEmitter *MCE = 0;
145 MCAsmBackend *MAB = 0;
146
147 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
148 true, /* verbose asm */
149 hasMCUseLoc(),
150 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000151 hasMCUseDwarfDirectory(),
Justin Holewinski40466cc2011-09-22 16:45:37 +0000152 InstPrinter,
153 MCE, MAB,
154 false /* show MC encoding */);
155 AsmStreamer.reset(S);
156 break;
157 }
158 case CGFT_ObjectFile: {
159 llvm_unreachable("Object file emission is not supported with PTX");
160 }
161 case CGFT_Null:
162 // The Null output is intended for use for performance analysis and testing,
163 // not real users.
164 AsmStreamer.reset(createNullStreamer(*Context));
165 break;
166 }
167
Justin Holewinski40466cc2011-09-22 16:45:37 +0000168 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
169 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
170 if (Printer == 0)
171 return true;
172
173 // If successful, createAsmPrinter took ownership of AsmStreamer.
174 AsmStreamer.take();
175
176 PM.add(Printer);
177
178 PM.add(createGCInfoDeleter());
179 return false;
180}
181
182bool PTXTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Justin Holewinski40466cc2011-09-22 16:45:37 +0000183 bool DisableVerify,
184 MCContext *&OutContext) {
185 // Add standard LLVM codegen passes.
186 // This is derived from LLVMTargetMachine::addCommonCodeGenPasses, with some
187 // modifications for the PTX target.
188
189 // Standard LLVM-Level Passes.
190
191 // Basic AliasAnalysis support.
192 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
193 // BasicAliasAnalysis wins if they disagree. This is intended to help
194 // support "obvious" type-punning idioms.
195 PM.add(createTypeBasedAliasAnalysisPass());
196 PM.add(createBasicAliasAnalysisPass());
197
198 // Before running any passes, run the verifier to determine if the input
199 // coming from the front-end and/or optimizer is valid.
200 if (!DisableVerify)
201 PM.add(createVerifierPass());
202
203 // Run loop strength reduction before anything else.
Evan Chengb95fc312011-11-16 08:38:26 +0000204 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000205 PM.add(createLoopStrengthReducePass(getTargetLowering()));
206 //PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
207 }
208
209 PM.add(createGCLoweringPass());
210
211 // Make sure that no unreachable blocks are instruction selected.
212 PM.add(createUnreachableBlockEliminationPass());
213
214 PM.add(createLowerInvokePass(getTargetLowering()));
215 // The lower invoke pass may create unreachable code. Remove it.
216 PM.add(createUnreachableBlockEliminationPass());
217
Evan Chengb95fc312011-11-16 08:38:26 +0000218 if (getOptLevel() != CodeGenOpt::None)
Justin Holewinski40466cc2011-09-22 16:45:37 +0000219 PM.add(createCodeGenPreparePass(getTargetLowering()));
220
221 PM.add(createStackProtectorPass(getTargetLowering()));
222
Evan Chengb95fc312011-11-16 08:38:26 +0000223 addPreISel(PM);
Justin Holewinski40466cc2011-09-22 16:45:37 +0000224
225 //PM.add(createPrintFunctionPass("\n\n"
226 // "*** Final LLVM Code input to ISel ***\n",
227 // &dbgs()));
228
229 // All passes which modify the LLVM IR are now complete; run the verifier
230 // to ensure that the IR is valid.
231 if (!DisableVerify)
232 PM.add(createVerifierPass());
233
234 // Standard Lower-Level Passes.
235
236 // Install a MachineModuleInfo class, which is an immutable pass that holds
237 // all the per-module stuff we're generating, including MCContext.
238 MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
239 *getRegisterInfo(),
Justin Holewinski05591be2011-09-22 16:45:43 +0000240 &getTargetLowering()->getObjFileLowering());
Justin Holewinski40466cc2011-09-22 16:45:37 +0000241 PM.add(MMI);
242 OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
243
244 // Set up a MachineFunction for the rest of CodeGen to work on.
Evan Chengb95fc312011-11-16 08:38:26 +0000245 PM.add(new MachineFunctionAnalysis(*this));
Justin Holewinski40466cc2011-09-22 16:45:37 +0000246
247 // Ask the target for an isel.
Evan Chengb95fc312011-11-16 08:38:26 +0000248 if (addInstSelector(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000249 return true;
250
251 // Print the instruction selected machine code...
252 printAndVerify(PM, "After Instruction Selection");
253
254 // Expand pseudo-instructions emitted by ISel.
255 PM.add(createExpandISelPseudosPass());
256
257 // Pre-ra tail duplication.
Evan Chengb95fc312011-11-16 08:38:26 +0000258 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000259 PM.add(createTailDuplicatePass(true));
260 printAndVerify(PM, "After Pre-RegAlloc TailDuplicate");
261 }
262
263 // Optimize PHIs before DCE: removing dead PHI cycles may make more
264 // instructions dead.
Evan Chengb95fc312011-11-16 08:38:26 +0000265 if (getOptLevel() != CodeGenOpt::None)
Justin Holewinski40466cc2011-09-22 16:45:37 +0000266 PM.add(createOptimizePHIsPass());
267
268 // If the target requests it, assign local variables to stack slots relative
269 // to one another and simplify frame index references where possible.
270 PM.add(createLocalStackSlotAllocationPass());
271
Evan Chengb95fc312011-11-16 08:38:26 +0000272 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000273 // With optimization, dead code should already be eliminated. However
274 // there is one known exception: lowered code for arguments that are only
275 // used by tail calls, where the tail calls reuse the incoming stack
276 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
277 PM.add(createDeadMachineInstructionElimPass());
278 printAndVerify(PM, "After codegen DCE pass");
279
280 PM.add(createMachineLICMPass());
281 PM.add(createMachineCSEPass());
282 PM.add(createMachineSinkingPass());
283 printAndVerify(PM, "After Machine LICM, CSE and Sinking passes");
284
285 PM.add(createPeepholeOptimizerPass());
286 printAndVerify(PM, "After codegen peephole optimization pass");
287 }
288
289 // Run pre-ra passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000290 if (addPreRegAlloc(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000291 printAndVerify(PM, "After PreRegAlloc passes");
292
293 // Perform register allocation.
294 PM.add(createPTXRegisterAllocator());
295 printAndVerify(PM, "After Register Allocation");
296
297 // Perform stack slot coloring and post-ra machine LICM.
Evan Chengb95fc312011-11-16 08:38:26 +0000298 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000299 // FIXME: Re-enable coloring with register when it's capable of adding
300 // kill markers.
301 PM.add(createStackSlotColoringPass(false));
302
303 // FIXME: Post-RA LICM has asserts that fire on virtual registers.
304 // Run post-ra machine LICM to hoist reloads / remats.
305 //if (!DisablePostRAMachineLICM)
306 // PM.add(createMachineLICMPass(false));
307
308 printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
309 }
310
311 // Run post-ra passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000312 if (addPostRegAlloc(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000313 printAndVerify(PM, "After PostRegAlloc passes");
314
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000315 PM.add(createExpandPostRAPseudosPass());
316 printAndVerify(PM, "After ExpandPostRAPseudos");
Justin Holewinski40466cc2011-09-22 16:45:37 +0000317
318 // Insert prolog/epilog code. Eliminate abstract frame index references...
319 PM.add(createPrologEpilogCodeInserter());
320 printAndVerify(PM, "After PrologEpilogCodeInserter");
321
322 // Run pre-sched2 passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000323 if (addPreSched2(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000324 printAndVerify(PM, "After PreSched2 passes");
325
326 // Second pass scheduler.
Evan Chengb95fc312011-11-16 08:38:26 +0000327 if (getOptLevel() != CodeGenOpt::None) {
328 PM.add(createPostRAScheduler(getOptLevel()));
Justin Holewinski40466cc2011-09-22 16:45:37 +0000329 printAndVerify(PM, "After PostRAScheduler");
330 }
331
332 // Branch folding must be run after regalloc and prolog/epilog insertion.
Evan Chengb95fc312011-11-16 08:38:26 +0000333 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000334 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
335 printNoVerify(PM, "After BranchFolding");
336 }
337
338 // Tail duplication.
Evan Chengb95fc312011-11-16 08:38:26 +0000339 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000340 PM.add(createTailDuplicatePass(false));
341 printNoVerify(PM, "After TailDuplicate");
342 }
343
344 PM.add(createGCMachineCodeAnalysisPass());
345
346 //if (PrintGCInfo)
347 // PM.add(createGCInfoPrinter(dbgs()));
348
Evan Chengb95fc312011-11-16 08:38:26 +0000349 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000350 PM.add(createCodePlacementOptPass());
351 printNoVerify(PM, "After CodePlacementOpt");
352 }
353
Evan Chengb95fc312011-11-16 08:38:26 +0000354 if (addPreEmitPass(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000355 printNoVerify(PM, "After PreEmit passes");
356
Evan Chengb95fc312011-11-16 08:38:26 +0000357 PM.add(createPTXMFInfoExtract(*this, getOptLevel()));
358 PM.add(createPTXFPRoundingModePass(*this, getOptLevel()));
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000359
Justin Holewinski40466cc2011-09-22 16:45:37 +0000360 return false;
361}