OLD | NEW |
1 //===-- NVPTXAsmPrinter.cpp - NVPTX LLVM assembly writer ------------------===// | 1 //===-- NVPTXAsmPrinter.cpp - NVPTX LLVM assembly writer ------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file contains a printer that converts from our internal representation | 10 // This file contains a printer that converts from our internal representation |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 namespace llvm { bool InterleaveSrcInPtx = false; } | 61 namespace llvm { bool InterleaveSrcInPtx = false; } |
62 | 62 |
63 static cl::opt<bool, true> | 63 static cl::opt<bool, true> |
64 InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore, | 64 InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore, |
65 cl::desc("NVPTX Specific: Emit source line in ptx file"), | 65 cl::desc("NVPTX Specific: Emit source line in ptx file"), |
66 cl::location(llvm::InterleaveSrcInPtx)); | 66 cl::location(llvm::InterleaveSrcInPtx)); |
67 | 67 |
68 namespace { | 68 namespace { |
69 /// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V | 69 /// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V |
70 /// depends. | 70 /// depends. |
71 void DiscoverDependentGlobals(const Value *V, | 71 void DiscoverDependentGlobals(Value *V, DenseSet<GlobalVariable *> &Globals) { |
72 DenseSet<const GlobalVariable *> &Globals) { | 72 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
73 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) | |
74 Globals.insert(GV); | 73 Globals.insert(GV); |
75 else { | 74 else { |
76 if (const User *U = dyn_cast<User>(V)) { | 75 if (User *U = dyn_cast<User>(V)) { |
77 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) { | 76 for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) { |
78 DiscoverDependentGlobals(U->getOperand(i), Globals); | 77 DiscoverDependentGlobals(U->getOperand(i), Globals); |
79 } | 78 } |
80 } | 79 } |
81 } | 80 } |
82 } | 81 } |
83 | 82 |
84 /// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable | 83 /// VisitGlobalVariableForEmission - Add \p GV to the list of GlobalVariable |
85 /// instances to be emitted, but only after any dependents have been added | 84 /// instances to be emitted, but only after any dependents have been added |
86 /// first. | 85 /// first. |
87 void VisitGlobalVariableForEmission( | 86 void VisitGlobalVariableForEmission( |
88 const GlobalVariable *GV, SmallVectorImpl<const GlobalVariable *> &Order, | 87 GlobalVariable *GV, SmallVectorImpl<GlobalVariable *> &Order, |
89 DenseSet<const GlobalVariable *> &Visited, | 88 DenseSet<GlobalVariable *> &Visited, DenseSet<GlobalVariable *> &Visiting) { |
90 DenseSet<const GlobalVariable *> &Visiting) { | |
91 // Have we already visited this one? | 89 // Have we already visited this one? |
92 if (Visited.count(GV)) | 90 if (Visited.count(GV)) |
93 return; | 91 return; |
94 | 92 |
95 // Do we have a circular dependency? | 93 // Do we have a circular dependency? |
96 if (Visiting.count(GV)) | 94 if (Visiting.count(GV)) |
97 report_fatal_error("Circular dependency found in global variable set"); | 95 report_fatal_error("Circular dependency found in global variable set"); |
98 | 96 |
99 // Start visiting this global | 97 // Start visiting this global |
100 Visiting.insert(GV); | 98 Visiting.insert(GV); |
101 | 99 |
102 // Make sure we visit all dependents first | 100 // Make sure we visit all dependents first |
103 DenseSet<const GlobalVariable *> Others; | 101 DenseSet<GlobalVariable *> Others; |
104 for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i) | 102 for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i) |
105 DiscoverDependentGlobals(GV->getOperand(i), Others); | 103 DiscoverDependentGlobals(GV->getOperand(i), Others); |
106 | 104 |
107 for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(), | 105 for (DenseSet<GlobalVariable *>::iterator I = Others.begin(), |
108 E = Others.end(); | 106 E = Others.end(); |
109 I != E; ++I) | 107 I != E; ++I) |
110 VisitGlobalVariableForEmission(*I, Order, Visited, Visiting); | 108 VisitGlobalVariableForEmission(*I, Order, Visited, Visiting); |
111 | 109 |
112 // Now we can visit ourself | 110 // Now we can visit ourself |
113 Order.push_back(GV); | 111 Order.push_back(GV); |
114 Visited.insert(GV); | 112 Visited.insert(GV); |
115 Visiting.erase(GV); | 113 Visiting.erase(GV); |
116 } | 114 } |
117 } | 115 } |
118 | 116 |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF, | 398 void NVPTXAsmPrinter::printReturnValStr(const MachineFunction &MF, |
401 raw_ostream &O) { | 399 raw_ostream &O) { |
402 const Function *F = MF.getFunction(); | 400 const Function *F = MF.getFunction(); |
403 printReturnValStr(F, O); | 401 printReturnValStr(F, O); |
404 } | 402 } |
405 | 403 |
406 void NVPTXAsmPrinter::EmitFunctionEntryLabel() { | 404 void NVPTXAsmPrinter::EmitFunctionEntryLabel() { |
407 SmallString<128> Str; | 405 SmallString<128> Str; |
408 raw_svector_ostream O(Str); | 406 raw_svector_ostream O(Str); |
409 | 407 |
410 if (!GlobalsEmitted) { | |
411 emitGlobals(*MF->getFunction()->getParent()); | |
412 GlobalsEmitted = true; | |
413 } | |
414 | |
415 // Set up | 408 // Set up |
416 MRI = &MF->getRegInfo(); | 409 MRI = &MF->getRegInfo(); |
417 F = MF->getFunction(); | 410 F = MF->getFunction(); |
418 emitLinkageDirective(F, O); | 411 emitLinkageDirective(F, O); |
419 if (llvm::isKernelFunction(*F)) | 412 if (llvm::isKernelFunction(*F)) |
420 O << ".entry "; | 413 O << ".entry "; |
421 else { | 414 else { |
422 O << ".func "; | 415 O << ".func "; |
423 printReturnValStr(*MF, O); | 416 printReturnValStr(*MF, O); |
424 } | 417 } |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
695 } | 688 } |
696 | 689 |
697 void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) { | 690 void NVPTXAsmPrinter::emitDeclaration(const Function *F, raw_ostream &O) { |
698 | 691 |
699 emitLinkageDirective(F, O); | 692 emitLinkageDirective(F, O); |
700 if (llvm::isKernelFunction(*F)) | 693 if (llvm::isKernelFunction(*F)) |
701 O << ".entry "; | 694 O << ".entry "; |
702 else | 695 else |
703 O << ".func "; | 696 O << ".func "; |
704 printReturnValStr(F, O); | 697 printReturnValStr(F, O); |
705 O << *Mang->getSymbol(F) << "\n"; | 698 O << *CurrentFnSym << "\n"; |
706 emitFunctionParamList(F, O); | 699 emitFunctionParamList(F, O); |
707 O << ";\n"; | 700 O << ";\n"; |
708 } | 701 } |
709 | 702 |
710 static bool usedInGlobalVarDef(const Constant *C) { | 703 static bool usedInGlobalVarDef(const Constant *C) { |
711 if (!C) | 704 if (!C) |
712 return false; | 705 return false; |
713 | 706 |
714 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { | 707 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
715 if (GV->getName().str() == "llvm.used") | 708 if (GV->getName().str() == "llvm.used") |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 const Function *caller = bb->getParent(); | 788 const Function *caller = bb->getParent(); |
796 if (!caller) | 789 if (!caller) |
797 continue; | 790 continue; |
798 if (seenMap.find(caller) != seenMap.end()) | 791 if (seenMap.find(caller) != seenMap.end()) |
799 return true; | 792 return true; |
800 } | 793 } |
801 } | 794 } |
802 return false; | 795 return false; |
803 } | 796 } |
804 | 797 |
805 void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) { | 798 void NVPTXAsmPrinter::emitDeclarations(Module &M, raw_ostream &O) { |
806 llvm::DenseMap<const Function *, bool> seenMap; | 799 llvm::DenseMap<const Function *, bool> seenMap; |
807 for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { | 800 for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
808 const Function *F = FI; | 801 const Function *F = FI; |
809 | 802 |
810 if (F->isDeclaration()) { | 803 if (F->isDeclaration()) { |
811 if (F->use_empty()) | 804 if (F->use_empty()) |
812 continue; | 805 continue; |
813 if (F->getIntrinsicID()) | 806 if (F->getIntrinsicID()) |
814 continue; | 807 continue; |
| 808 CurrentFnSym = Mang->getSymbol(F); |
815 emitDeclaration(F, O); | 809 emitDeclaration(F, O); |
816 continue; | 810 continue; |
817 } | 811 } |
818 for (Value::const_use_iterator iter = F->use_begin(), | 812 for (Value::const_use_iterator iter = F->use_begin(), |
819 iterEnd = F->use_end(); | 813 iterEnd = F->use_end(); |
820 iter != iterEnd; ++iter) { | 814 iter != iterEnd; ++iter) { |
821 if (const Constant *C = dyn_cast<Constant>(*iter)) { | 815 if (const Constant *C = dyn_cast<Constant>(*iter)) { |
822 if (usedInGlobalVarDef(C)) { | 816 if (usedInGlobalVarDef(C)) { |
823 // The use is in the initialization of a global variable | 817 // The use is in the initialization of a global variable |
824 // that is a function pointer, so print a declaration | 818 // that is a function pointer, so print a declaration |
825 // for the original function | 819 // for the original function |
| 820 CurrentFnSym = Mang->getSymbol(F); |
826 emitDeclaration(F, O); | 821 emitDeclaration(F, O); |
827 break; | 822 break; |
828 } | 823 } |
829 // Emit a declaration of this function if the function that | 824 // Emit a declaration of this function if the function that |
830 // uses this constant expr has already been seen. | 825 // uses this constant expr has already been seen. |
831 if (useFuncSeen(C, seenMap)) { | 826 if (useFuncSeen(C, seenMap)) { |
| 827 CurrentFnSym = Mang->getSymbol(F); |
832 emitDeclaration(F, O); | 828 emitDeclaration(F, O); |
833 break; | 829 break; |
834 } | 830 } |
835 } | 831 } |
836 | 832 |
837 if (!isa<Instruction>(*iter)) | 833 if (!isa<Instruction>(*iter)) |
838 continue; | 834 continue; |
839 const Instruction *instr = cast<Instruction>(*iter); | 835 const Instruction *instr = cast<Instruction>(*iter); |
840 const BasicBlock *bb = instr->getParent(); | 836 const BasicBlock *bb = instr->getParent(); |
841 if (!bb) | 837 if (!bb) |
842 continue; | 838 continue; |
843 const Function *caller = bb->getParent(); | 839 const Function *caller = bb->getParent(); |
844 if (!caller) | 840 if (!caller) |
845 continue; | 841 continue; |
846 | 842 |
847 // If a caller has already been seen, then the caller is | 843 // If a caller has already been seen, then the caller is |
848 // appearing in the module before the callee. so print out | 844 // appearing in the module before the callee. so print out |
849 // a declaration for the callee. | 845 // a declaration for the callee. |
850 if (seenMap.find(caller) != seenMap.end()) { | 846 if (seenMap.find(caller) != seenMap.end()) { |
| 847 CurrentFnSym = Mang->getSymbol(F); |
851 emitDeclaration(F, O); | 848 emitDeclaration(F, O); |
852 break; | 849 break; |
853 } | 850 } |
854 } | 851 } |
855 seenMap[F] = true; | 852 seenMap[F] = true; |
856 } | 853 } |
857 } | 854 } |
858 | 855 |
859 void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) { | 856 void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) { |
860 DebugInfoFinder DbgFinder; | 857 DebugInfoFinder DbgFinder; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 // Emit header before any dwarf directives are emitted below. | 914 // Emit header before any dwarf directives are emitted below. |
918 emitHeader(M, OS1); | 915 emitHeader(M, OS1); |
919 OutStreamer.EmitRawText(OS1.str()); | 916 OutStreamer.EmitRawText(OS1.str()); |
920 | 917 |
921 // Already commented out | 918 // Already commented out |
922 //bool Result = AsmPrinter::doInitialization(M); | 919 //bool Result = AsmPrinter::doInitialization(M); |
923 | 920 |
924 if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA) | 921 if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA) |
925 recordAndEmitFilenames(M); | 922 recordAndEmitFilenames(M); |
926 | 923 |
927 GlobalsEmitted = false; | |
928 | |
929 return false; // success | |
930 } | |
931 | |
932 void NVPTXAsmPrinter::emitGlobals(const Module &M) { | |
933 SmallString<128> Str2; | 924 SmallString<128> Str2; |
934 raw_svector_ostream OS2(Str2); | 925 raw_svector_ostream OS2(Str2); |
935 | 926 |
936 emitDeclarations(M, OS2); | 927 emitDeclarations(M, OS2); |
937 | 928 |
938 // As ptxas does not support forward references of globals, we need to first | 929 // As ptxas does not support forward references of globals, we need to first |
939 // sort the list of module-level globals in def-use order. We visit each | 930 // sort the list of module-level globals in def-use order. We visit each |
940 // global variable in order, and ensure that we emit it *after* its dependent | 931 // global variable in order, and ensure that we emit it *after* its dependent |
941 // globals. We use a little extra memory maintaining both a set and a list to | 932 // globals. We use a little extra memory maintaining both a set and a list to |
942 // have fast searches while maintaining a strict ordering. | 933 // have fast searches while maintaining a strict ordering. |
943 SmallVector<const GlobalVariable *, 8> Globals; | 934 SmallVector<GlobalVariable *, 8> Globals; |
944 DenseSet<const GlobalVariable *> GVVisited; | 935 DenseSet<GlobalVariable *> GVVisited; |
945 DenseSet<const GlobalVariable *> GVVisiting; | 936 DenseSet<GlobalVariable *> GVVisiting; |
946 | 937 |
947 // Visit each global variable, in order | 938 // Visit each global variable, in order |
948 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); | 939 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; |
949 I != E; ++I) | 940 ++I) |
950 VisitGlobalVariableForEmission(I, Globals, GVVisited, GVVisiting); | 941 VisitGlobalVariableForEmission(I, Globals, GVVisited, GVVisiting); |
951 | 942 |
952 assert(GVVisited.size() == M.getGlobalList().size() && | 943 assert(GVVisited.size() == M.getGlobalList().size() && |
953 "Missed a global variable"); | 944 "Missed a global variable"); |
954 assert(GVVisiting.size() == 0 && "Did not fully process a global variable"); | 945 assert(GVVisiting.size() == 0 && "Did not fully process a global variable"); |
955 | 946 |
956 // Print out module-level global variables in proper order | 947 // Print out module-level global variables in proper order |
957 for (unsigned i = 0, e = Globals.size(); i != e; ++i) | 948 for (unsigned i = 0, e = Globals.size(); i != e; ++i) |
958 printModuleLevelGV(Globals[i], OS2); | 949 printModuleLevelGV(Globals[i], OS2); |
959 | 950 |
960 OS2 << '\n'; | 951 OS2 << '\n'; |
961 | 952 |
962 OutStreamer.EmitRawText(OS2.str()); | 953 OutStreamer.EmitRawText(OS2.str()); |
| 954 return false; // success |
963 } | 955 } |
964 | 956 |
965 void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O) { | 957 void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O) { |
966 O << "//\n"; | 958 O << "//\n"; |
967 O << "// Generated by LLVM NVPTX Back-End\n"; | 959 O << "// Generated by LLVM NVPTX Back-End\n"; |
968 O << "//\n"; | 960 O << "//\n"; |
969 O << "\n"; | 961 O << "\n"; |
970 | 962 |
971 unsigned PTXVersion = nvptxSubtarget.getPTXVersion(); | 963 unsigned PTXVersion = nvptxSubtarget.getPTXVersion(); |
972 O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n"; | 964 O << ".version " << (PTXVersion / 10) << "." << (PTXVersion % 10) << "\n"; |
(...skipping 17 matching lines...) Expand all Loading... |
990 if (nvptxSubtarget.is64Bit()) | 982 if (nvptxSubtarget.is64Bit()) |
991 O << "64"; | 983 O << "64"; |
992 else | 984 else |
993 O << "32"; | 985 O << "32"; |
994 O << "\n"; | 986 O << "\n"; |
995 | 987 |
996 O << "\n"; | 988 O << "\n"; |
997 } | 989 } |
998 | 990 |
999 bool NVPTXAsmPrinter::doFinalization(Module &M) { | 991 bool NVPTXAsmPrinter::doFinalization(Module &M) { |
1000 | |
1001 // If we did not emit any functions, then the global declarations have not | |
1002 // yet been emitted. | |
1003 if (!GlobalsEmitted) { | |
1004 emitGlobals(M); | |
1005 GlobalsEmitted = true; | |
1006 } | |
1007 | |
1008 // XXX Temproarily remove global variables so that doFinalization() will not | 992 // XXX Temproarily remove global variables so that doFinalization() will not |
1009 // emit them again (global variables are emitted at beginning). | 993 // emit them again (global variables are emitted at beginning). |
1010 | 994 |
1011 Module::GlobalListType &global_list = M.getGlobalList(); | 995 Module::GlobalListType &global_list = M.getGlobalList(); |
1012 int i, n = global_list.size(); | 996 int i, n = global_list.size(); |
1013 GlobalVariable **gv_array = new GlobalVariable *[n]; | 997 GlobalVariable **gv_array = new GlobalVariable *[n]; |
1014 | 998 |
1015 // first, back-up GlobalVariable in gv_array | 999 // first, back-up GlobalVariable in gv_array |
1016 i = 0; | 1000 i = 0; |
1017 for (Module::global_iterator I = global_list.begin(), E = global_list.end(); | 1001 for (Module::global_iterator I = global_list.begin(), E = global_list.end(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1072 msg.append("Error: "); | 1056 msg.append("Error: "); |
1073 msg.append("Symbol "); | 1057 msg.append("Symbol "); |
1074 if (V->hasName()) | 1058 if (V->hasName()) |
1075 msg.append(V->getName().str()); | 1059 msg.append(V->getName().str()); |
1076 msg.append("has unsupported appending linkage type"); | 1060 msg.append("has unsupported appending linkage type"); |
1077 llvm_unreachable(msg.c_str()); | 1061 llvm_unreachable(msg.c_str()); |
1078 } | 1062 } |
1079 } | 1063 } |
1080 } | 1064 } |
1081 | 1065 |
1082 void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar, | 1066 void NVPTXAsmPrinter::printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O, |
1083 raw_ostream &O, | |
1084 bool processDemoted) { | 1067 bool processDemoted) { |
1085 | 1068 |
1086 // Skip meta data | 1069 // Skip meta data |
1087 if (GVar->hasSection()) { | 1070 if (GVar->hasSection()) { |
1088 if (GVar->getSection() == "llvm.metadata") | 1071 if (GVar->getSection() == "llvm.metadata") |
1089 return; | 1072 return; |
1090 } | 1073 } |
1091 | 1074 |
1092 const DataLayout *TD = TM.getDataLayout(); | 1075 const DataLayout *TD = TM.getDataLayout(); |
1093 | 1076 |
(...skipping 23 matching lines...) Expand all Loading... |
1117 // Currently the only known declaration is for an automatic __local | 1100 // Currently the only known declaration is for an automatic __local |
1118 // (.shared) promoted to global. | 1101 // (.shared) promoted to global. |
1119 emitPTXGlobalVariable(GVar, O); | 1102 emitPTXGlobalVariable(GVar, O); |
1120 O << ";\n"; | 1103 O << ";\n"; |
1121 return; | 1104 return; |
1122 } | 1105 } |
1123 | 1106 |
1124 if (llvm::isSampler(*GVar)) { | 1107 if (llvm::isSampler(*GVar)) { |
1125 O << ".global .samplerref " << llvm::getSamplerName(*GVar); | 1108 O << ".global .samplerref " << llvm::getSamplerName(*GVar); |
1126 | 1109 |
1127 const Constant *Initializer = NULL; | 1110 Constant *Initializer = NULL; |
1128 if (GVar->hasInitializer()) | 1111 if (GVar->hasInitializer()) |
1129 Initializer = GVar->getInitializer(); | 1112 Initializer = GVar->getInitializer(); |
1130 const ConstantInt *CI = NULL; | 1113 ConstantInt *CI = NULL; |
1131 if (Initializer) | 1114 if (Initializer) |
1132 CI = dyn_cast<ConstantInt>(Initializer); | 1115 CI = dyn_cast<ConstantInt>(Initializer); |
1133 if (CI) { | 1116 if (CI) { |
1134 unsigned sample = CI->getZExtValue(); | 1117 unsigned sample = CI->getZExtValue(); |
1135 | 1118 |
1136 O << " = { "; | 1119 O << " = { "; |
1137 | 1120 |
1138 for (int i = 0, | 1121 for (int i = 0, |
1139 addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE); | 1122 addr = ((sample & __CLK_ADDRESS_MASK) >> __CLK_ADDRESS_BASE); |
1140 i < 3; i++) { | 1123 i < 3; i++) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1193 if (GVar->use_empty()) | 1176 if (GVar->use_empty()) |
1194 return; | 1177 return; |
1195 } | 1178 } |
1196 | 1179 |
1197 const Function *demotedFunc = 0; | 1180 const Function *demotedFunc = 0; |
1198 if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) { | 1181 if (!processDemoted && canDemoteGlobalVar(GVar, demotedFunc)) { |
1199 O << "// " << GVar->getName().str() << " has been demoted\n"; | 1182 O << "// " << GVar->getName().str() << " has been demoted\n"; |
1200 if (localDecls.find(demotedFunc) != localDecls.end()) | 1183 if (localDecls.find(demotedFunc) != localDecls.end()) |
1201 localDecls[demotedFunc].push_back(GVar); | 1184 localDecls[demotedFunc].push_back(GVar); |
1202 else { | 1185 else { |
1203 std::vector<const GlobalVariable *> temp; | 1186 std::vector<GlobalVariable *> temp; |
1204 temp.push_back(GVar); | 1187 temp.push_back(GVar); |
1205 localDecls[demotedFunc] = temp; | 1188 localDecls[demotedFunc] = temp; |
1206 } | 1189 } |
1207 return; | 1190 return; |
1208 } | 1191 } |
1209 | 1192 |
1210 O << "."; | 1193 O << "."; |
1211 emitPTXAddressSpace(PTy->getAddressSpace(), O); | 1194 emitPTXAddressSpace(PTy->getAddressSpace(), O); |
1212 if (GVar->getAlignment() == 0) | 1195 if (GVar->getAlignment() == 0) |
1213 O << " .align " << (int) TD->getPrefTypeAlignment(ETy); | 1196 O << " .align " << (int) TD->getPrefTypeAlignment(ETy); |
1214 else | 1197 else |
1215 O << " .align " << GVar->getAlignment(); | 1198 O << " .align " << GVar->getAlignment(); |
1216 | 1199 |
1217 if (ETy->isPrimitiveType() || ETy->isIntegerTy() || isa<PointerType>(ETy)) { | 1200 if (ETy->isPrimitiveType() || ETy->isIntegerTy() || isa<PointerType>(ETy)) { |
1218 O << " ."; | 1201 O << " ."; |
1219 // Special case: ABI requires that we use .u8 for predicates | 1202 O << getPTXFundamentalTypeStr(ETy, false); |
1220 if (ETy->isIntegerTy(1)) | |
1221 O << "u8"; | |
1222 else | |
1223 O << getPTXFundamentalTypeStr(ETy, false); | |
1224 O << " "; | 1203 O << " "; |
1225 O << *Mang->getSymbol(GVar); | 1204 O << *Mang->getSymbol(GVar); |
1226 | 1205 |
1227 // Ptx allows variable initilization only for constant and global state | 1206 // Ptx allows variable initilization only for constant and global state |
1228 // spaces. | 1207 // spaces. |
1229 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) || | 1208 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) || |
1230 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST_NOT_GEN) || | 1209 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST_NOT_GEN) || |
1231 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) && | 1210 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) && |
1232 GVar->hasInitializer()) { | 1211 GVar->hasInitializer()) { |
1233 const Constant *Initializer = GVar->getInitializer(); | 1212 Constant *Initializer = GVar->getInitializer(); |
1234 if (!Initializer->isNullValue()) { | 1213 if (!Initializer->isNullValue()) { |
1235 O << " = "; | 1214 O << " = "; |
1236 printScalarConstant(Initializer, O); | 1215 printScalarConstant(Initializer, O); |
1237 } | 1216 } |
1238 } | 1217 } |
1239 } else { | 1218 } else { |
1240 unsigned int ElementSize = 0; | 1219 unsigned int ElementSize = 0; |
1241 | 1220 |
1242 // Although PTX has direct support for struct type and array type and | 1221 // Although PTX has direct support for struct type and array type and |
1243 // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for | 1222 // LLVM IR is very similar to PTX, the LLVM CodeGen does not support for |
1244 // targets that support these high level field accesses. Structs, arrays | 1223 // targets that support these high level field accesses. Structs, arrays |
1245 // and vectors are lowered into arrays of bytes. | 1224 // and vectors are lowered into arrays of bytes. |
1246 switch (ETy->getTypeID()) { | 1225 switch (ETy->getTypeID()) { |
1247 case Type::StructTyID: | 1226 case Type::StructTyID: |
1248 case Type::ArrayTyID: | 1227 case Type::ArrayTyID: |
1249 case Type::VectorTyID: | 1228 case Type::VectorTyID: |
1250 ElementSize = TD->getTypeStoreSize(ETy); | 1229 ElementSize = TD->getTypeStoreSize(ETy); |
1251 // Ptx allows variable initilization only for constant and | 1230 // Ptx allows variable initilization only for constant and |
1252 // global state spaces. | 1231 // global state spaces. |
1253 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) || | 1232 if (((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) || |
1254 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST_NOT_GEN) || | 1233 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST_NOT_GEN) || |
1255 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) && | 1234 (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) && |
1256 GVar->hasInitializer()) { | 1235 GVar->hasInitializer()) { |
1257 const Constant *Initializer = GVar->getInitializer(); | 1236 Constant *Initializer = GVar->getInitializer(); |
1258 if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) { | 1237 if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) { |
1259 AggBuffer aggBuffer(ElementSize, O, *this); | 1238 AggBuffer aggBuffer(ElementSize, O, *this); |
1260 bufferAggregateConstant(Initializer, &aggBuffer); | 1239 bufferAggregateConstant(Initializer, &aggBuffer); |
1261 if (aggBuffer.numSymbols) { | 1240 if (aggBuffer.numSymbols) { |
1262 if (nvptxSubtarget.is64Bit()) { | 1241 if (nvptxSubtarget.is64Bit()) { |
1263 O << " .u64 " << *Mang->getSymbol(GVar) << "["; | 1242 O << " .u64 " << *Mang->getSymbol(GVar) << "["; |
1264 O << ElementSize / 8; | 1243 O << ElementSize / 8; |
1265 } else { | 1244 } else { |
1266 O << " .u32 " << *Mang->getSymbol(GVar) << "["; | 1245 O << " .u32 " << *Mang->getSymbol(GVar) << "["; |
1267 O << ElementSize / 4; | 1246 O << ElementSize / 4; |
(...skipping 29 matching lines...) Expand all Loading... |
1297 } | 1276 } |
1298 | 1277 |
1299 } | 1278 } |
1300 O << ";\n"; | 1279 O << ";\n"; |
1301 } | 1280 } |
1302 | 1281 |
1303 void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) { | 1282 void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) { |
1304 if (localDecls.find(f) == localDecls.end()) | 1283 if (localDecls.find(f) == localDecls.end()) |
1305 return; | 1284 return; |
1306 | 1285 |
1307 std::vector<const GlobalVariable *> &gvars = localDecls[f]; | 1286 std::vector<GlobalVariable *> &gvars = localDecls[f]; |
1308 | 1287 |
1309 for (unsigned i = 0, e = gvars.size(); i != e; ++i) { | 1288 for (unsigned i = 0, e = gvars.size(); i != e; ++i) { |
1310 O << "\t// demoted variable\n\t"; | 1289 O << "\t// demoted variable\n\t"; |
1311 printModuleLevelGV(gvars[i], O, true); | 1290 printModuleLevelGV(gvars[i], O, true); |
1312 } | 1291 } |
1313 } | 1292 } |
1314 | 1293 |
1315 void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace, | 1294 void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace, |
1316 raw_ostream &O) const { | 1295 raw_ostream &O) const { |
1317 switch (AddressSpace) { | 1296 switch (AddressSpace) { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1462 const FunctionType *FTy = dyn_cast<FunctionType>(Ty); | 1441 const FunctionType *FTy = dyn_cast<FunctionType>(Ty); |
1463 if (FTy) | 1442 if (FTy) |
1464 return TD->getPointerPrefAlignment(); | 1443 return TD->getPointerPrefAlignment(); |
1465 return TD->getPrefTypeAlignment(Ty); | 1444 return TD->getPrefTypeAlignment(Ty); |
1466 } | 1445 } |
1467 | 1446 |
1468 void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I, | 1447 void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I, |
1469 int paramIndex, raw_ostream &O) { | 1448 int paramIndex, raw_ostream &O) { |
1470 if ((nvptxSubtarget.getDrvInterface() == NVPTX::NVCL) || | 1449 if ((nvptxSubtarget.getDrvInterface() == NVPTX::NVCL) || |
1471 (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)) | 1450 (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)) |
1472 O << *Mang->getSymbol(I->getParent()) << "_param_" << paramIndex; | 1451 O << *CurrentFnSym << "_param_" << paramIndex; |
1473 else { | 1452 else { |
1474 std::string argName = I->getName(); | 1453 std::string argName = I->getName(); |
1475 const char *p = argName.c_str(); | 1454 const char *p = argName.c_str(); |
1476 while (*p) { | 1455 while (*p) { |
1477 if (*p == '.') | 1456 if (*p == '.') |
1478 O << "_"; | 1457 O << "_"; |
1479 else | 1458 else |
1480 O << *p; | 1459 O << *p; |
1481 p++; | 1460 p++; |
1482 } | 1461 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1521 if (!first) | 1500 if (!first) |
1522 O << ",\n"; | 1501 O << ",\n"; |
1523 | 1502 |
1524 first = false; | 1503 first = false; |
1525 | 1504 |
1526 // Handle image/sampler parameters | 1505 // Handle image/sampler parameters |
1527 if (llvm::isSampler(*I) || llvm::isImage(*I)) { | 1506 if (llvm::isSampler(*I) || llvm::isImage(*I)) { |
1528 if (llvm::isImage(*I)) { | 1507 if (llvm::isImage(*I)) { |
1529 std::string sname = I->getName(); | 1508 std::string sname = I->getName(); |
1530 if (llvm::isImageWriteOnly(*I)) | 1509 if (llvm::isImageWriteOnly(*I)) |
1531 O << "\t.param .surfref " << *Mang->getSymbol(F) << "_param_" | 1510 O << "\t.param .surfref " << *CurrentFnSym << "_param_" << paramIndex; |
1532 << paramIndex; | |
1533 else // Default image is read_only | 1511 else // Default image is read_only |
1534 O << "\t.param .texref " << *Mang->getSymbol(F) << "_param_" | 1512 O << "\t.param .texref " << *CurrentFnSym << "_param_" << paramIndex; |
1535 << paramIndex; | |
1536 } else // Should be llvm::isSampler(*I) | 1513 } else // Should be llvm::isSampler(*I) |
1537 O << "\t.param .samplerref " << *Mang->getSymbol(F) << "_param_" | 1514 O << "\t.param .samplerref " << *CurrentFnSym << "_param_" |
1538 << paramIndex; | 1515 << paramIndex; |
1539 continue; | 1516 continue; |
1540 } | 1517 } |
1541 | 1518 |
1542 if (PAL.hasAttribute(paramIndex + 1, Attribute::ByVal) == false) { | 1519 if (PAL.hasAttribute(paramIndex + 1, Attribute::ByVal) == false) { |
1543 if (Ty->isVectorTy()) { | 1520 if (Ty->isVectorTy()) { |
1544 // Just print .param .b8 .align <a> .param[size]; | 1521 // Just print .param .b8 .align <a> .param[size]; |
1545 // <a> = PAL.getparamalignment | 1522 // <a> = PAL.getparamalignment |
1546 // size = typeallocsize of element type | 1523 // size = typeallocsize of element type |
1547 unsigned align = PAL.getParamAlignment(paramIndex + 1); | 1524 unsigned align = PAL.getParamAlignment(paramIndex + 1); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1580 O << ".ptr .global "; | 1557 O << ".ptr .global "; |
1581 break; | 1558 break; |
1582 } | 1559 } |
1583 O << ".align " << (int) getOpenCLAlignment(TD, ETy) << " "; | 1560 O << ".align " << (int) getOpenCLAlignment(TD, ETy) << " "; |
1584 } | 1561 } |
1585 printParamName(I, paramIndex, O); | 1562 printParamName(I, paramIndex, O); |
1586 continue; | 1563 continue; |
1587 } | 1564 } |
1588 | 1565 |
1589 // non-pointer scalar to kernel func | 1566 // non-pointer scalar to kernel func |
1590 O << "\t.param ."; | 1567 O << "\t.param ." << getPTXFundamentalTypeStr(Ty) << " "; |
1591 // Special case: predicate operands become .u8 types | |
1592 if (Ty->isIntegerTy(1)) | |
1593 O << "u8"; | |
1594 else | |
1595 O << getPTXFundamentalTypeStr(Ty); | |
1596 O << " "; | |
1597 printParamName(I, paramIndex, O); | 1568 printParamName(I, paramIndex, O); |
1598 continue; | 1569 continue; |
1599 } | 1570 } |
1600 // Non-kernel function, just print .param .b<size> for ABI | 1571 // Non-kernel function, just print .param .b<size> for ABI |
1601 // and .reg .b<size> for non ABY | 1572 // and .reg .b<size> for non ABY |
1602 unsigned sz = 0; | 1573 unsigned sz = 0; |
1603 if (isa<IntegerType>(Ty)) { | 1574 if (isa<IntegerType>(Ty)) { |
1604 sz = cast<IntegerType>(Ty)->getBitWidth(); | 1575 sz = cast<IntegerType>(Ty)->getBitWidth(); |
1605 if (sz < 32) | 1576 if (sz < 32) |
1606 sz = 32; | 1577 sz = 32; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1773 llvm_unreachable("unsupported fp type"); | 1744 llvm_unreachable("unsupported fp type"); |
1774 | 1745 |
1775 APInt API = APF.bitcastToAPInt(); | 1746 APInt API = APF.bitcastToAPInt(); |
1776 std::string hexstr(utohexstr(API.getZExtValue())); | 1747 std::string hexstr(utohexstr(API.getZExtValue())); |
1777 O << lead; | 1748 O << lead; |
1778 if (hexstr.length() < numHex) | 1749 if (hexstr.length() < numHex) |
1779 O << std::string(numHex - hexstr.length(), '0'); | 1750 O << std::string(numHex - hexstr.length(), '0'); |
1780 O << utohexstr(API.getZExtValue()); | 1751 O << utohexstr(API.getZExtValue()); |
1781 } | 1752 } |
1782 | 1753 |
1783 void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) { | 1754 void NVPTXAsmPrinter::printScalarConstant(Constant *CPV, raw_ostream &O) { |
1784 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { | 1755 if (ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) { |
1785 O << CI->getValue(); | 1756 O << CI->getValue(); |
1786 return; | 1757 return; |
1787 } | 1758 } |
1788 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { | 1759 if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) { |
1789 printFPConstant(CFP, O); | 1760 printFPConstant(CFP, O); |
1790 return; | 1761 return; |
1791 } | 1762 } |
1792 if (isa<ConstantPointerNull>(CPV)) { | 1763 if (isa<ConstantPointerNull>(CPV)) { |
1793 O << "0"; | 1764 O << "0"; |
1794 return; | 1765 return; |
1795 } | 1766 } |
1796 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { | 1767 if (GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
1797 O << *Mang->getSymbol(GVar); | 1768 O << *Mang->getSymbol(GVar); |
1798 return; | 1769 return; |
1799 } | 1770 } |
1800 if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { | 1771 if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1801 const Value *v = Cexpr->stripPointerCasts(); | 1772 Value *v = Cexpr->stripPointerCasts(); |
1802 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { | 1773 if (GlobalValue *GVar = dyn_cast<GlobalValue>(v)) { |
1803 O << *Mang->getSymbol(GVar); | 1774 O << *Mang->getSymbol(GVar); |
1804 return; | 1775 return; |
1805 } else { | 1776 } else { |
1806 O << *LowerConstant(CPV, *this); | 1777 O << *LowerConstant(CPV, *this); |
1807 return; | 1778 return; |
1808 } | 1779 } |
1809 } | 1780 } |
1810 llvm_unreachable("Not scalar type found in printScalarConstant()"); | 1781 llvm_unreachable("Not scalar type found in printScalarConstant()"); |
1811 } | 1782 } |
1812 | 1783 |
1813 void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, | 1784 void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes, |
1814 AggBuffer *aggBuffer) { | 1785 AggBuffer *aggBuffer) { |
1815 | 1786 |
1816 const DataLayout *TD = TM.getDataLayout(); | 1787 const DataLayout *TD = TM.getDataLayout(); |
1817 | 1788 |
1818 if (isa<UndefValue>(CPV) || CPV->isNullValue()) { | 1789 if (isa<UndefValue>(CPV) || CPV->isNullValue()) { |
1819 int s = TD->getTypeAllocSize(CPV->getType()); | 1790 int s = TD->getTypeAllocSize(CPV->getType()); |
1820 if (s < Bytes) | 1791 if (s < Bytes) |
1821 s = Bytes; | 1792 s = Bytes; |
1822 aggBuffer->addZeros(s); | 1793 aggBuffer->addZeros(s); |
1823 return; | 1794 return; |
1824 } | 1795 } |
1825 | 1796 |
1826 unsigned char *ptr; | 1797 unsigned char *ptr; |
1827 switch (CPV->getType()->getTypeID()) { | 1798 switch (CPV->getType()->getTypeID()) { |
1828 | 1799 |
1829 case Type::IntegerTyID: { | 1800 case Type::IntegerTyID: { |
1830 const Type *ETy = CPV->getType(); | 1801 const Type *ETy = CPV->getType(); |
1831 if (ETy == Type::getInt8Ty(CPV->getContext())) { | 1802 if (ETy == Type::getInt8Ty(CPV->getContext())) { |
1832 unsigned char c = | 1803 unsigned char c = |
1833 (unsigned char)(dyn_cast<ConstantInt>(CPV))->getZExtValue(); | 1804 (unsigned char)(dyn_cast<ConstantInt>(CPV))->getZExtValue(); |
1834 ptr = &c; | 1805 ptr = &c; |
1835 aggBuffer->addBytes(ptr, 1, Bytes); | 1806 aggBuffer->addBytes(ptr, 1, Bytes); |
1836 } else if (ETy == Type::getInt16Ty(CPV->getContext())) { | 1807 } else if (ETy == Type::getInt16Ty(CPV->getContext())) { |
1837 short int16 = (short)(dyn_cast<ConstantInt>(CPV))->getZExtValue(); | 1808 short int16 = (short)(dyn_cast<ConstantInt>(CPV))->getZExtValue(); |
1838 ptr = (unsigned char *)&int16; | 1809 ptr = (unsigned char *)&int16; |
1839 aggBuffer->addBytes(ptr, 2, Bytes); | 1810 aggBuffer->addBytes(ptr, 2, Bytes); |
1840 } else if (ETy == Type::getInt32Ty(CPV->getContext())) { | 1811 } else if (ETy == Type::getInt32Ty(CPV->getContext())) { |
1841 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { | 1812 if (ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
1842 int int32 = (int)(constInt->getZExtValue()); | 1813 int int32 = (int)(constInt->getZExtValue()); |
1843 ptr = (unsigned char *)&int32; | 1814 ptr = (unsigned char *)&int32; |
1844 aggBuffer->addBytes(ptr, 4, Bytes); | 1815 aggBuffer->addBytes(ptr, 4, Bytes); |
1845 break; | 1816 break; |
1846 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { | 1817 } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1847 if (const ConstantInt *constInt = dyn_cast<ConstantInt>( | 1818 if (ConstantInt *constInt = dyn_cast<ConstantInt>( |
1848 ConstantFoldConstantExpression(Cexpr, TD))) { | 1819 ConstantFoldConstantExpression(Cexpr, TD))) { |
1849 int int32 = (int)(constInt->getZExtValue()); | 1820 int int32 = (int)(constInt->getZExtValue()); |
1850 ptr = (unsigned char *)&int32; | 1821 ptr = (unsigned char *)&int32; |
1851 aggBuffer->addBytes(ptr, 4, Bytes); | 1822 aggBuffer->addBytes(ptr, 4, Bytes); |
1852 break; | 1823 break; |
1853 } | 1824 } |
1854 if (Cexpr->getOpcode() == Instruction::PtrToInt) { | 1825 if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
1855 Value *v = Cexpr->getOperand(0)->stripPointerCasts(); | 1826 Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
1856 aggBuffer->addSymbol(v); | 1827 aggBuffer->addSymbol(v); |
1857 aggBuffer->addZeros(4); | 1828 aggBuffer->addZeros(4); |
1858 break; | 1829 break; |
1859 } | 1830 } |
1860 } | 1831 } |
1861 llvm_unreachable("unsupported integer const type"); | 1832 llvm_unreachable("unsupported integer const type"); |
1862 } else if (ETy == Type::getInt64Ty(CPV->getContext())) { | 1833 } else if (ETy == Type::getInt64Ty(CPV->getContext())) { |
1863 if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { | 1834 if (ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) { |
1864 long long int64 = (long long)(constInt->getZExtValue()); | 1835 long long int64 = (long long)(constInt->getZExtValue()); |
1865 ptr = (unsigned char *)&int64; | 1836 ptr = (unsigned char *)&int64; |
1866 aggBuffer->addBytes(ptr, 8, Bytes); | 1837 aggBuffer->addBytes(ptr, 8, Bytes); |
1867 break; | 1838 break; |
1868 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { | 1839 } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1869 if (const ConstantInt *constInt = dyn_cast<ConstantInt>( | 1840 if (ConstantInt *constInt = dyn_cast<ConstantInt>( |
1870 ConstantFoldConstantExpression(Cexpr, TD))) { | 1841 ConstantFoldConstantExpression(Cexpr, TD))) { |
1871 long long int64 = (long long)(constInt->getZExtValue()); | 1842 long long int64 = (long long)(constInt->getZExtValue()); |
1872 ptr = (unsigned char *)&int64; | 1843 ptr = (unsigned char *)&int64; |
1873 aggBuffer->addBytes(ptr, 8, Bytes); | 1844 aggBuffer->addBytes(ptr, 8, Bytes); |
1874 break; | 1845 break; |
1875 } | 1846 } |
1876 if (Cexpr->getOpcode() == Instruction::PtrToInt) { | 1847 if (Cexpr->getOpcode() == Instruction::PtrToInt) { |
1877 Value *v = Cexpr->getOperand(0)->stripPointerCasts(); | 1848 Value *v = Cexpr->getOperand(0)->stripPointerCasts(); |
1878 aggBuffer->addSymbol(v); | 1849 aggBuffer->addSymbol(v); |
1879 aggBuffer->addZeros(8); | 1850 aggBuffer->addZeros(8); |
1880 break; | 1851 break; |
1881 } | 1852 } |
1882 } | 1853 } |
1883 llvm_unreachable("unsupported integer const type"); | 1854 llvm_unreachable("unsupported integer const type"); |
1884 } else | 1855 } else |
1885 llvm_unreachable("unsupported integer const type"); | 1856 llvm_unreachable("unsupported integer const type"); |
1886 break; | 1857 break; |
1887 } | 1858 } |
1888 case Type::FloatTyID: | 1859 case Type::FloatTyID: |
1889 case Type::DoubleTyID: { | 1860 case Type::DoubleTyID: { |
1890 const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV); | 1861 ConstantFP *CFP = dyn_cast<ConstantFP>(CPV); |
1891 const Type *Ty = CFP->getType(); | 1862 const Type *Ty = CFP->getType(); |
1892 if (Ty == Type::getFloatTy(CPV->getContext())) { | 1863 if (Ty == Type::getFloatTy(CPV->getContext())) { |
1893 float float32 = (float) CFP->getValueAPF().convertToFloat(); | 1864 float float32 = (float) CFP->getValueAPF().convertToFloat(); |
1894 ptr = (unsigned char *)&float32; | 1865 ptr = (unsigned char *)&float32; |
1895 aggBuffer->addBytes(ptr, 4, Bytes); | 1866 aggBuffer->addBytes(ptr, 4, Bytes); |
1896 } else if (Ty == Type::getDoubleTy(CPV->getContext())) { | 1867 } else if (Ty == Type::getDoubleTy(CPV->getContext())) { |
1897 double float64 = CFP->getValueAPF().convertToDouble(); | 1868 double float64 = CFP->getValueAPF().convertToDouble(); |
1898 ptr = (unsigned char *)&float64; | 1869 ptr = (unsigned char *)&float64; |
1899 aggBuffer->addBytes(ptr, 8, Bytes); | 1870 aggBuffer->addBytes(ptr, 8, Bytes); |
1900 } else { | 1871 } else { |
1901 llvm_unreachable("unsupported fp const type"); | 1872 llvm_unreachable("unsupported fp const type"); |
1902 } | 1873 } |
1903 break; | 1874 break; |
1904 } | 1875 } |
1905 case Type::PointerTyID: { | 1876 case Type::PointerTyID: { |
1906 if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { | 1877 if (GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) { |
1907 aggBuffer->addSymbol(GVar); | 1878 aggBuffer->addSymbol(GVar); |
1908 } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { | 1879 } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) { |
1909 const Value *v = Cexpr->stripPointerCasts(); | 1880 Value *v = Cexpr->stripPointerCasts(); |
1910 aggBuffer->addSymbol(v); | 1881 aggBuffer->addSymbol(v); |
1911 } | 1882 } |
1912 unsigned int s = TD->getTypeAllocSize(CPV->getType()); | 1883 unsigned int s = TD->getTypeAllocSize(CPV->getType()); |
1913 aggBuffer->addZeros(s); | 1884 aggBuffer->addZeros(s); |
1914 break; | 1885 break; |
1915 } | 1886 } |
1916 | 1887 |
1917 case Type::ArrayTyID: | 1888 case Type::ArrayTyID: |
1918 case Type::VectorTyID: | 1889 case Type::VectorTyID: |
1919 case Type::StructTyID: { | 1890 case Type::StructTyID: { |
1920 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV) || | 1891 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV) || |
1921 isa<ConstantStruct>(CPV)) { | 1892 isa<ConstantStruct>(CPV)) { |
1922 int ElementSize = TD->getTypeAllocSize(CPV->getType()); | 1893 int ElementSize = TD->getTypeAllocSize(CPV->getType()); |
1923 bufferAggregateConstant(CPV, aggBuffer); | 1894 bufferAggregateConstant(CPV, aggBuffer); |
1924 if (Bytes > ElementSize) | 1895 if (Bytes > ElementSize) |
1925 aggBuffer->addZeros(Bytes - ElementSize); | 1896 aggBuffer->addZeros(Bytes - ElementSize); |
1926 } else if (isa<ConstantAggregateZero>(CPV)) | 1897 } else if (isa<ConstantAggregateZero>(CPV)) |
1927 aggBuffer->addZeros(Bytes); | 1898 aggBuffer->addZeros(Bytes); |
1928 else | 1899 else |
1929 llvm_unreachable("Unexpected Constant type"); | 1900 llvm_unreachable("Unexpected Constant type"); |
1930 break; | 1901 break; |
1931 } | 1902 } |
1932 | 1903 |
1933 default: | 1904 default: |
1934 llvm_unreachable("unsupported type"); | 1905 llvm_unreachable("unsupported type"); |
1935 } | 1906 } |
1936 } | 1907 } |
1937 | 1908 |
1938 void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV, | 1909 void NVPTXAsmPrinter::bufferAggregateConstant(Constant *CPV, |
1939 AggBuffer *aggBuffer) { | 1910 AggBuffer *aggBuffer) { |
1940 const DataLayout *TD = TM.getDataLayout(); | 1911 const DataLayout *TD = TM.getDataLayout(); |
1941 int Bytes; | 1912 int Bytes; |
1942 | 1913 |
1943 // Old constants | 1914 // Old constants |
1944 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) { | 1915 if (isa<ConstantArray>(CPV) || isa<ConstantVector>(CPV)) { |
1945 if (CPV->getNumOperands()) | 1916 if (CPV->getNumOperands()) |
1946 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) | 1917 for (unsigned i = 0, e = CPV->getNumOperands(); i != e; ++i) |
1947 bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer); | 1918 bufferLEByte(cast<Constant>(CPV->getOperand(i)), 0, aggBuffer); |
1948 return; | 1919 return; |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2137 theCurLine++; | 2108 theCurLine++; |
2138 } | 2109 } |
2139 return buff; | 2110 return buff; |
2140 } | 2111 } |
2141 | 2112 |
2142 // Force static initialization. | 2113 // Force static initialization. |
2143 extern "C" void LLVMInitializeNVPTXAsmPrinter() { | 2114 extern "C" void LLVMInitializeNVPTXAsmPrinter() { |
2144 RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32); | 2115 RegisterAsmPrinter<NVPTXAsmPrinter> X(TheNVPTXTarget32); |
2145 RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64); | 2116 RegisterAsmPrinter<NVPTXAsmPrinter> Y(TheNVPTXTarget64); |
2146 } | 2117 } |
OLD | NEW |