OSDN Git Service

radeonsi: initial WIP SI code
[android-x86/external-mesa.git] / src / gallium / drivers / radeon / AMDILAsmPrinter7XX.cpp
1 //===-- AMDILAsmPrinter7XX.cpp - TODO: Add brief description -------===//
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 #include "AMDIL7XXAsmPrinter.h"
10
11 #include "AMDILAlgorithms.tpp"
12 #include "AMDIL7XXAsmPrinter.h"
13 #include "AMDILDevices.h"
14 #include "AMDILGlobalManager.h"
15 #include "AMDILKernelManager.h"
16 #include "AMDILMachineFunctionInfo.h"
17 #include "AMDILUtilityFunctions.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/Analysis/DebugInfo.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/Constants.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/Metadata.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/DebugLoc.h"
32 #include "llvm/Support/InstIterator.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Type.h"
35
36 using namespace llvm;
37
38 // TODO: Add support for verbose.
39   AMDIL7XXAsmPrinter::AMDIL7XXAsmPrinter(TargetMachine& TM, MCStreamer &Streamer)
40 : AMDILAsmPrinter(TM, Streamer)
41 {
42 }
43
44 AMDIL7XXAsmPrinter::~AMDIL7XXAsmPrinter()
45 {
46 }
47 ///
48 /// @param name
49 /// @brief strips KERNEL_PREFIX and KERNEL_SUFFIX from the name
50 /// and returns that name if both of the tokens are present.
51 ///
52   static
53 std::string Strip(const std::string &name)
54 {
55   size_t start = name.find("__OpenCL_");
56   size_t end = name.find("_kernel");
57   if (start == std::string::npos
58       || end == std::string::npos
59       || (start == end)) {
60     return name;
61   } else {
62     return name.substr(9, name.length()-16);
63   }
64 }
65   void
66 AMDIL7XXAsmPrinter::emitMacroFunc(const MachineInstr *MI,
67     llvm::raw_ostream &O)
68 {
69   const AMDILSubtarget *curTarget = mTM->getSubtargetImpl();
70   const char *name = "unknown";
71   llvm::StringRef nameRef;
72   if (MI->getOperand(0).isGlobal()) {
73     nameRef = MI->getOperand(0).getGlobal()->getName();
74     name = nameRef.data();
75     if (curTarget->device()->usesHardware(
76           AMDILDeviceInfo::DoubleOps)
77         && !::strncmp(name, "__sqrt_f64", 10) ) {
78       name = "__sqrt_f64_7xx";
79     }
80   }
81   emitMCallInst(MI, O, name);
82 }
83
84   bool
85 AMDIL7XXAsmPrinter::runOnMachineFunction(MachineFunction &lMF)
86 {
87   this->MF = &lMF;
88   mMeta->setMF(&lMF);
89   mMFI = lMF.getInfo<AMDILMachineFunctionInfo>();
90   SetupMachineFunction(lMF);
91   std::string kernelName = MF->getFunction()->getName();
92   mName = Strip(kernelName);
93
94   mKernelName = kernelName;
95   EmitFunctionHeader();
96   EmitFunctionBody();
97   return false;
98 }
99
100   void
101 AMDIL7XXAsmPrinter::EmitInstruction(const MachineInstr *II)
102 {
103   std::string FunStr;
104   raw_string_ostream OFunStr(FunStr);
105   formatted_raw_ostream O(OFunStr);
106   const AMDILSubtarget *curTarget = mTM->getSubtargetImpl();
107   if (mDebugMode) {
108     O << ";" ;
109     II->print(O);
110   }
111    if (isMacroFunc(II)) {
112     emitMacroFunc(II, O);
113     O.flush();
114     OutStreamer.EmitRawText(StringRef(FunStr));
115     return;
116   }
117   if (isMacroCall(II)) {
118     const char *name;
119     name = mTM->getInstrInfo()->getName(II->getOpcode()) + 5;
120     int macronum = amd::MacroDBFindMacro(name);
121     O << "\t;"<< name<<"\n";
122     O << "\tmcall("<<macronum<<")";
123     if (curTarget->device()->isSupported(
124           AMDILDeviceInfo::MacroDB)) {
125       mMacroIDs.insert(macronum);
126     } else {
127       mMFI->addCalledIntr(macronum);
128     }
129   }
130
131   // Print the assembly for the instruction.
132   // We want to make sure that we do HW constants
133   // before we do arena segment
134   if (mMeta->useCompilerWrite(II)) {
135     // TODO: This is a hack to get around some
136     // conformance failures. 
137     O << "\tif_logicalz cb0[0].x\n";
138     O << "\tuav_raw_store_id("
139       << curTarget->device()->getResourceID(AMDILDevice::RAW_UAV_ID)
140       << ") ";
141     O << "mem0.x___, cb0[3].x, r0.0\n";
142     O << "\tendif\n";
143     mMFI->addMetadata(";memory:compilerwrite");
144   } else {
145     printInstruction(II, O);
146   }
147   O.flush();
148   OutStreamer.EmitRawText(StringRef(FunStr));
149 }