blob: fb9dc606aed9f16a8b21c0d5c35fc019571c53c3 [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) {
139 default: return true;
140 case CGFT_AssemblyFile: {
141 MCInstPrinter *InstPrinter =
142 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
143
144 // Create a code emitter if asked to show the encoding.
145 MCCodeEmitter *MCE = 0;
146 MCAsmBackend *MAB = 0;
147
148 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
149 true, /* verbose asm */
150 hasMCUseLoc(),
151 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000152 hasMCUseDwarfDirectory(),
Justin Holewinski40466cc2011-09-22 16:45:37 +0000153 InstPrinter,
154 MCE, MAB,
155 false /* show MC encoding */);
156 AsmStreamer.reset(S);
157 break;
158 }
159 case CGFT_ObjectFile: {
160 llvm_unreachable("Object file emission is not supported with PTX");
161 }
162 case CGFT_Null:
163 // The Null output is intended for use for performance analysis and testing,
164 // not real users.
165 AsmStreamer.reset(createNullStreamer(*Context));
166 break;
167 }
168
169 // MC Logging
170 //AsmStreamer.reset(createLoggingStreamer(AsmStreamer.take(), errs()));
171
172 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
173 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
174 if (Printer == 0)
175 return true;
176
177 // If successful, createAsmPrinter took ownership of AsmStreamer.
178 AsmStreamer.take();
179
180 PM.add(Printer);
181
182 PM.add(createGCInfoDeleter());
183 return false;
184}
185
186bool PTXTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Justin Holewinski40466cc2011-09-22 16:45:37 +0000187 bool DisableVerify,
188 MCContext *&OutContext) {
189 // Add standard LLVM codegen passes.
190 // This is derived from LLVMTargetMachine::addCommonCodeGenPasses, with some
191 // modifications for the PTX target.
192
193 // Standard LLVM-Level Passes.
194
195 // Basic AliasAnalysis support.
196 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
197 // BasicAliasAnalysis wins if they disagree. This is intended to help
198 // support "obvious" type-punning idioms.
199 PM.add(createTypeBasedAliasAnalysisPass());
200 PM.add(createBasicAliasAnalysisPass());
201
202 // Before running any passes, run the verifier to determine if the input
203 // coming from the front-end and/or optimizer is valid.
204 if (!DisableVerify)
205 PM.add(createVerifierPass());
206
207 // Run loop strength reduction before anything else.
Evan Chengb95fc312011-11-16 08:38:26 +0000208 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000209 PM.add(createLoopStrengthReducePass(getTargetLowering()));
210 //PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
211 }
212
213 PM.add(createGCLoweringPass());
214
215 // Make sure that no unreachable blocks are instruction selected.
216 PM.add(createUnreachableBlockEliminationPass());
217
218 PM.add(createLowerInvokePass(getTargetLowering()));
219 // The lower invoke pass may create unreachable code. Remove it.
220 PM.add(createUnreachableBlockEliminationPass());
221
Evan Chengb95fc312011-11-16 08:38:26 +0000222 if (getOptLevel() != CodeGenOpt::None)
Justin Holewinski40466cc2011-09-22 16:45:37 +0000223 PM.add(createCodeGenPreparePass(getTargetLowering()));
224
225 PM.add(createStackProtectorPass(getTargetLowering()));
226
Evan Chengb95fc312011-11-16 08:38:26 +0000227 addPreISel(PM);
Justin Holewinski40466cc2011-09-22 16:45:37 +0000228
229 //PM.add(createPrintFunctionPass("\n\n"
230 // "*** Final LLVM Code input to ISel ***\n",
231 // &dbgs()));
232
233 // All passes which modify the LLVM IR are now complete; run the verifier
234 // to ensure that the IR is valid.
235 if (!DisableVerify)
236 PM.add(createVerifierPass());
237
238 // Standard Lower-Level Passes.
239
240 // Install a MachineModuleInfo class, which is an immutable pass that holds
241 // all the per-module stuff we're generating, including MCContext.
242 MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
243 *getRegisterInfo(),
Justin Holewinski05591be2011-09-22 16:45:43 +0000244 &getTargetLowering()->getObjFileLowering());
Justin Holewinski40466cc2011-09-22 16:45:37 +0000245 PM.add(MMI);
246 OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
247
248 // Set up a MachineFunction for the rest of CodeGen to work on.
Evan Chengb95fc312011-11-16 08:38:26 +0000249 PM.add(new MachineFunctionAnalysis(*this));
Justin Holewinski40466cc2011-09-22 16:45:37 +0000250
251 // Ask the target for an isel.
Evan Chengb95fc312011-11-16 08:38:26 +0000252 if (addInstSelector(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000253 return true;
254
255 // Print the instruction selected machine code...
256 printAndVerify(PM, "After Instruction Selection");
257
258 // Expand pseudo-instructions emitted by ISel.
259 PM.add(createExpandISelPseudosPass());
260
261 // Pre-ra tail duplication.
Evan Chengb95fc312011-11-16 08:38:26 +0000262 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000263 PM.add(createTailDuplicatePass(true));
264 printAndVerify(PM, "After Pre-RegAlloc TailDuplicate");
265 }
266
267 // Optimize PHIs before DCE: removing dead PHI cycles may make more
268 // instructions dead.
Evan Chengb95fc312011-11-16 08:38:26 +0000269 if (getOptLevel() != CodeGenOpt::None)
Justin Holewinski40466cc2011-09-22 16:45:37 +0000270 PM.add(createOptimizePHIsPass());
271
272 // If the target requests it, assign local variables to stack slots relative
273 // to one another and simplify frame index references where possible.
274 PM.add(createLocalStackSlotAllocationPass());
275
Evan Chengb95fc312011-11-16 08:38:26 +0000276 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000277 // With optimization, dead code should already be eliminated. However
278 // there is one known exception: lowered code for arguments that are only
279 // used by tail calls, where the tail calls reuse the incoming stack
280 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
281 PM.add(createDeadMachineInstructionElimPass());
282 printAndVerify(PM, "After codegen DCE pass");
283
284 PM.add(createMachineLICMPass());
285 PM.add(createMachineCSEPass());
286 PM.add(createMachineSinkingPass());
287 printAndVerify(PM, "After Machine LICM, CSE and Sinking passes");
288
289 PM.add(createPeepholeOptimizerPass());
290 printAndVerify(PM, "After codegen peephole optimization pass");
291 }
292
293 // Run pre-ra passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000294 if (addPreRegAlloc(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000295 printAndVerify(PM, "After PreRegAlloc passes");
296
297 // Perform register allocation.
298 PM.add(createPTXRegisterAllocator());
299 printAndVerify(PM, "After Register Allocation");
300
301 // Perform stack slot coloring and post-ra machine LICM.
Evan Chengb95fc312011-11-16 08:38:26 +0000302 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000303 // FIXME: Re-enable coloring with register when it's capable of adding
304 // kill markers.
305 PM.add(createStackSlotColoringPass(false));
306
307 // FIXME: Post-RA LICM has asserts that fire on virtual registers.
308 // Run post-ra machine LICM to hoist reloads / remats.
309 //if (!DisablePostRAMachineLICM)
310 // PM.add(createMachineLICMPass(false));
311
312 printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
313 }
314
315 // Run post-ra passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000316 if (addPostRegAlloc(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000317 printAndVerify(PM, "After PostRegAlloc passes");
318
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000319 PM.add(createExpandPostRAPseudosPass());
320 printAndVerify(PM, "After ExpandPostRAPseudos");
Justin Holewinski40466cc2011-09-22 16:45:37 +0000321
322 // Insert prolog/epilog code. Eliminate abstract frame index references...
323 PM.add(createPrologEpilogCodeInserter());
324 printAndVerify(PM, "After PrologEpilogCodeInserter");
325
326 // Run pre-sched2 passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000327 if (addPreSched2(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000328 printAndVerify(PM, "After PreSched2 passes");
329
330 // Second pass scheduler.
Evan Chengb95fc312011-11-16 08:38:26 +0000331 if (getOptLevel() != CodeGenOpt::None) {
332 PM.add(createPostRAScheduler(getOptLevel()));
Justin Holewinski40466cc2011-09-22 16:45:37 +0000333 printAndVerify(PM, "After PostRAScheduler");
334 }
335
336 // Branch folding must be run after regalloc and prolog/epilog insertion.
Evan Chengb95fc312011-11-16 08:38:26 +0000337 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000338 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
339 printNoVerify(PM, "After BranchFolding");
340 }
341
342 // Tail duplication.
Evan Chengb95fc312011-11-16 08:38:26 +0000343 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000344 PM.add(createTailDuplicatePass(false));
345 printNoVerify(PM, "After TailDuplicate");
346 }
347
348 PM.add(createGCMachineCodeAnalysisPass());
349
350 //if (PrintGCInfo)
351 // PM.add(createGCInfoPrinter(dbgs()));
352
Evan Chengb95fc312011-11-16 08:38:26 +0000353 if (getOptLevel() != CodeGenOpt::None) {
Justin Holewinski40466cc2011-09-22 16:45:37 +0000354 PM.add(createCodePlacementOptPass());
355 printNoVerify(PM, "After CodePlacementOpt");
356 }
357
Evan Chengb95fc312011-11-16 08:38:26 +0000358 if (addPreEmitPass(PM))
Justin Holewinski40466cc2011-09-22 16:45:37 +0000359 printNoVerify(PM, "After PreEmit passes");
360
Evan Chengb95fc312011-11-16 08:38:26 +0000361 PM.add(createPTXMFInfoExtract(*this, getOptLevel()));
362 PM.add(createPTXFPRoundingModePass(*this, getOptLevel()));
Justin Holewinski6b8990d2011-09-26 16:20:25 +0000363
Justin Holewinski40466cc2011-09-22 16:45:37 +0000364 return false;
365}