blob: 4811dd2d49fa989ef4342f9772219aaf4b188abc [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,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000049 bool useCFI,
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";
Justin Holewinski40466cc2011-09-22 16:45:37 +000070
71 // Copied from LLVMTargetMachine.cpp
72 void printNoVerify(PassManagerBase &PM, const char *Banner) {
73 if (PrintMachineCode)
74 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
75 }
76
77 void printAndVerify(PassManagerBase &PM,
78 const char *Banner) {
79 if (PrintMachineCode)
80 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
81
82 //if (VerifyMachineCode)
83 // PM.add(createMachineVerifierPass(Banner));
84 }
Che-Liang Chiouf48817c2011-03-02 07:36:48 +000085}
86
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000087// DataLayout and FrameLowering are filled with dummy data
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000088PTXTargetMachine::PTXTargetMachine(const Target &T,
Evan Cheng34ad6db2011-07-20 07:51:56 +000089 StringRef TT, StringRef CPU, StringRef FS,
90 Reloc::Model RM, CodeModel::Model CM,
91 bool is64Bit)
92 : LLVMTargetMachine(T, TT, CPU, FS, RM, CM),
Justin Holewinskie1fee482011-04-20 15:37:17 +000093 DataLayout(is64Bit ? DataLayout64 : DataLayout32),
Evan Cheng276365d2011-06-30 01:53:36 +000094 Subtarget(TT, CPU, FS, is64Bit),
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000095 FrameLowering(Subtarget),
Che-Liang Chiou31c488c2011-03-02 07:58:46 +000096 InstrInfo(*this),
97 TLInfo(*this) {
Eric Christopher50880d02010-09-18 18:52:28 +000098}
99
Evan Cheng43966132011-07-19 06:37:02 +0000100PTX32TargetMachine::PTX32TargetMachine(const Target &T, StringRef TT,
101 StringRef CPU, StringRef FS,
Evan Cheng34ad6db2011-07-20 07:51:56 +0000102 Reloc::Model RM, CodeModel::Model CM)
103 : PTXTargetMachine(T, TT, CPU, FS, RM, CM, false) {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000104}
105
Evan Cheng43966132011-07-19 06:37:02 +0000106PTX64TargetMachine::PTX64TargetMachine(const Target &T, StringRef TT,
107 StringRef CPU, StringRef FS,
Evan Cheng34ad6db2011-07-20 07:51:56 +0000108 Reloc::Model RM, CodeModel::Model CM)
109 : PTXTargetMachine(T, TT, CPU, FS, RM, CM, true) {
Justin Holewinskie1fee482011-04-20 15:37:17 +0000110}
111
Eric Christopher50880d02010-09-18 18:52:28 +0000112bool PTXTargetMachine::addInstSelector(PassManagerBase &PM,
113 CodeGenOpt::Level OptLevel) {
114 PM.add(createPTXISelDag(*this, OptLevel));
Che-Liang Chiouad83c1d2011-01-01 10:50:37 +0000115 return false;
116}
117
118bool PTXTargetMachine::addPostRegAlloc(PassManagerBase &PM,
119 CodeGenOpt::Level OptLevel) {
120 // PTXMFInfoExtract must after register allocation!
Che-Liang Chiou3278c422010-11-08 03:00:52 +0000121 PM.add(createPTXMFInfoExtract(*this, OptLevel));
Eric Christopher50880d02010-09-18 18:52:28 +0000122 return false;
Nick Lewyckyf7a3c502010-09-07 18:14:24 +0000123}
Justin Holewinski40466cc2011-09-22 16:45:37 +0000124
125bool PTXTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
126 formatted_raw_ostream &Out,
127 CodeGenFileType FileType,
128 CodeGenOpt::Level OptLevel,
129 bool DisableVerify) {
130 // This is mostly based on LLVMTargetMachine::addPassesToEmitFile
131
132 // Add common CodeGen passes.
133 MCContext *Context = 0;
134 if (addCommonCodeGenPasses(PM, OptLevel, DisableVerify, Context))
135 return true;
136 assert(Context != 0 && "Failed to get MCContext");
137
138 if (hasMCSaveTempLabels())
139 Context->setAllowTemporaryLabels(false);
140
141 const MCAsmInfo &MAI = *getMCAsmInfo();
142 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
143 OwningPtr<MCStreamer> AsmStreamer;
144
145 switch (FileType) {
146 default: return true;
147 case CGFT_AssemblyFile: {
148 MCInstPrinter *InstPrinter =
149 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
150
151 // Create a code emitter if asked to show the encoding.
152 MCCodeEmitter *MCE = 0;
153 MCAsmBackend *MAB = 0;
154
155 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
156 true, /* verbose asm */
157 hasMCUseLoc(),
158 hasMCUseCFI(),
159 InstPrinter,
160 MCE, MAB,
161 false /* show MC encoding */);
162 AsmStreamer.reset(S);
163 break;
164 }
165 case CGFT_ObjectFile: {
166 llvm_unreachable("Object file emission is not supported with PTX");
167 }
168 case CGFT_Null:
169 // The Null output is intended for use for performance analysis and testing,
170 // not real users.
171 AsmStreamer.reset(createNullStreamer(*Context));
172 break;
173 }
174
175 // MC Logging
176 //AsmStreamer.reset(createLoggingStreamer(AsmStreamer.take(), errs()));
177
178 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
179 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
180 if (Printer == 0)
181 return true;
182
183 // If successful, createAsmPrinter took ownership of AsmStreamer.
184 AsmStreamer.take();
185
186 PM.add(Printer);
187
188 PM.add(createGCInfoDeleter());
189 return false;
190}
191
192bool PTXTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
193 CodeGenOpt::Level OptLevel,
194 bool DisableVerify,
195 MCContext *&OutContext) {
196 // Add standard LLVM codegen passes.
197 // This is derived from LLVMTargetMachine::addCommonCodeGenPasses, with some
198 // modifications for the PTX target.
199
200 // Standard LLVM-Level Passes.
201
202 // Basic AliasAnalysis support.
203 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
204 // BasicAliasAnalysis wins if they disagree. This is intended to help
205 // support "obvious" type-punning idioms.
206 PM.add(createTypeBasedAliasAnalysisPass());
207 PM.add(createBasicAliasAnalysisPass());
208
209 // Before running any passes, run the verifier to determine if the input
210 // coming from the front-end and/or optimizer is valid.
211 if (!DisableVerify)
212 PM.add(createVerifierPass());
213
214 // Run loop strength reduction before anything else.
215 if (OptLevel != CodeGenOpt::None) {
216 PM.add(createLoopStrengthReducePass(getTargetLowering()));
217 //PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
218 }
219
220 PM.add(createGCLoweringPass());
221
222 // Make sure that no unreachable blocks are instruction selected.
223 PM.add(createUnreachableBlockEliminationPass());
224
225 PM.add(createLowerInvokePass(getTargetLowering()));
226 // The lower invoke pass may create unreachable code. Remove it.
227 PM.add(createUnreachableBlockEliminationPass());
228
229 if (OptLevel != CodeGenOpt::None)
230 PM.add(createCodeGenPreparePass(getTargetLowering()));
231
232 PM.add(createStackProtectorPass(getTargetLowering()));
233
234 addPreISel(PM, OptLevel);
235
236 //PM.add(createPrintFunctionPass("\n\n"
237 // "*** Final LLVM Code input to ISel ***\n",
238 // &dbgs()));
239
240 // All passes which modify the LLVM IR are now complete; run the verifier
241 // to ensure that the IR is valid.
242 if (!DisableVerify)
243 PM.add(createVerifierPass());
244
245 // Standard Lower-Level Passes.
246
247 // Install a MachineModuleInfo class, which is an immutable pass that holds
248 // all the per-module stuff we're generating, including MCContext.
249 MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
250 *getRegisterInfo(),
Justin Holewinski05591be2011-09-22 16:45:43 +0000251 &getTargetLowering()->getObjFileLowering());
Justin Holewinski40466cc2011-09-22 16:45:37 +0000252 PM.add(MMI);
253 OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
254
255 // Set up a MachineFunction for the rest of CodeGen to work on.
256 PM.add(new MachineFunctionAnalysis(*this, OptLevel));
257
258 // Ask the target for an isel.
259 if (addInstSelector(PM, OptLevel))
260 return true;
261
262 // Print the instruction selected machine code...
263 printAndVerify(PM, "After Instruction Selection");
264
265 // Expand pseudo-instructions emitted by ISel.
266 PM.add(createExpandISelPseudosPass());
267
268 // Pre-ra tail duplication.
269 if (OptLevel != CodeGenOpt::None) {
270 PM.add(createTailDuplicatePass(true));
271 printAndVerify(PM, "After Pre-RegAlloc TailDuplicate");
272 }
273
274 // Optimize PHIs before DCE: removing dead PHI cycles may make more
275 // instructions dead.
276 if (OptLevel != CodeGenOpt::None)
277 PM.add(createOptimizePHIsPass());
278
279 // If the target requests it, assign local variables to stack slots relative
280 // to one another and simplify frame index references where possible.
281 PM.add(createLocalStackSlotAllocationPass());
282
283 if (OptLevel != CodeGenOpt::None) {
284 // With optimization, dead code should already be eliminated. However
285 // there is one known exception: lowered code for arguments that are only
286 // used by tail calls, where the tail calls reuse the incoming stack
287 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
288 PM.add(createDeadMachineInstructionElimPass());
289 printAndVerify(PM, "After codegen DCE pass");
290
291 PM.add(createMachineLICMPass());
292 PM.add(createMachineCSEPass());
293 PM.add(createMachineSinkingPass());
294 printAndVerify(PM, "After Machine LICM, CSE and Sinking passes");
295
296 PM.add(createPeepholeOptimizerPass());
297 printAndVerify(PM, "After codegen peephole optimization pass");
298 }
299
300 // Run pre-ra passes.
301 if (addPreRegAlloc(PM, OptLevel))
302 printAndVerify(PM, "After PreRegAlloc passes");
303
304 // Perform register allocation.
305 PM.add(createPTXRegisterAllocator());
306 printAndVerify(PM, "After Register Allocation");
307
308 // Perform stack slot coloring and post-ra machine LICM.
309 if (OptLevel != CodeGenOpt::None) {
310 // FIXME: Re-enable coloring with register when it's capable of adding
311 // kill markers.
312 PM.add(createStackSlotColoringPass(false));
313
314 // FIXME: Post-RA LICM has asserts that fire on virtual registers.
315 // Run post-ra machine LICM to hoist reloads / remats.
316 //if (!DisablePostRAMachineLICM)
317 // PM.add(createMachineLICMPass(false));
318
319 printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
320 }
321
322 // Run post-ra passes.
323 if (addPostRegAlloc(PM, OptLevel))
324 printAndVerify(PM, "After PostRegAlloc passes");
325
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000326 PM.add(createExpandPostRAPseudosPass());
327 printAndVerify(PM, "After ExpandPostRAPseudos");
Justin Holewinski40466cc2011-09-22 16:45:37 +0000328
329 // Insert prolog/epilog code. Eliminate abstract frame index references...
330 PM.add(createPrologEpilogCodeInserter());
331 printAndVerify(PM, "After PrologEpilogCodeInserter");
332
333 // Run pre-sched2 passes.
334 if (addPreSched2(PM, OptLevel))
335 printAndVerify(PM, "After PreSched2 passes");
336
337 // Second pass scheduler.
338 if (OptLevel != CodeGenOpt::None) {
339 PM.add(createPostRAScheduler(OptLevel));
340 printAndVerify(PM, "After PostRAScheduler");
341 }
342
343 // Branch folding must be run after regalloc and prolog/epilog insertion.
344 if (OptLevel != CodeGenOpt::None) {
345 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
346 printNoVerify(PM, "After BranchFolding");
347 }
348
349 // Tail duplication.
350 if (OptLevel != CodeGenOpt::None) {
351 PM.add(createTailDuplicatePass(false));
352 printNoVerify(PM, "After TailDuplicate");
353 }
354
355 PM.add(createGCMachineCodeAnalysisPass());
356
357 //if (PrintGCInfo)
358 // PM.add(createGCInfoPrinter(dbgs()));
359
360 if (OptLevel != CodeGenOpt::None) {
361 PM.add(createCodePlacementOptPass());
362 printNoVerify(PM, "After CodePlacementOpt");
363 }
364
365 if (addPreEmitPass(PM, OptLevel))
366 printNoVerify(PM, "After PreEmit passes");
367
368 return false;
369}