Yang Ni | a4ded13 | 2014-11-17 17:44:08 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "bcc/Renderscript/RSMetadata.h" |
| 18 | |
| 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/IR/Module.h" |
| 21 | |
| 22 | // Name of metadata node where pragma info resides (should be synced with |
| 23 | // slang.cpp) |
| 24 | const llvm::StringRef pragma_metadata_name("#pragma"); |
| 25 | |
| 26 | /* |
| 27 | * The following names should be synced with the one appeared in |
| 28 | * slang_rs_metadata.h. |
| 29 | */ |
| 30 | |
| 31 | // Name of metadata node where exported variable names reside |
| 32 | static const llvm::StringRef |
| 33 | export_var_metadata_name("#rs_export_var"); |
| 34 | |
| 35 | // Name of metadata node where exported function names reside |
| 36 | static const llvm::StringRef |
| 37 | export_func_metadata_name("#rs_export_func"); |
| 38 | |
| 39 | // Name of metadata node where exported ForEach name information resides |
| 40 | static const llvm::StringRef |
| 41 | export_foreach_name_metadata_name("#rs_export_foreach_name"); |
| 42 | |
| 43 | // Name of metadata node where exported ForEach signature information resides |
| 44 | static const llvm::StringRef |
| 45 | export_foreach_metadata_name("#rs_export_foreach"); |
| 46 | |
| 47 | // Name of metadata node where RS object slot info resides (should be |
| 48 | static const llvm::StringRef |
| 49 | object_slot_metadata_name("#rs_object_slots"); |
| 50 | |
| 51 | bcc::RSMetadata::RSMetadata(llvm::Module &Module) : Module(Module) {} |
| 52 | |
| 53 | void bcc::RSMetadata::deleteAll() { |
| 54 | std::vector<llvm::StringRef> MDNames; |
| 55 | MDNames.push_back(pragma_metadata_name); |
| 56 | MDNames.push_back(export_var_metadata_name); |
| 57 | MDNames.push_back(export_func_metadata_name); |
| 58 | MDNames.push_back(export_foreach_name_metadata_name); |
| 59 | MDNames.push_back(export_foreach_metadata_name); |
| 60 | MDNames.push_back(object_slot_metadata_name); |
| 61 | |
| 62 | for (std::vector<llvm::StringRef>::iterator MI = MDNames.begin(), |
| 63 | ME = MDNames.end(); |
| 64 | MI != ME; ++MI) { |
| 65 | llvm::NamedMDNode *MDNode = Module.getNamedMetadata(*MI); |
| 66 | if (MDNode) { |
| 67 | MDNode->eraseFromParent(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void bcc::RSMetadata::markForEachFunction(llvm::Function &Function, |
| 73 | uint32_t Signature) { |
| 74 | llvm::NamedMDNode *ExportForEachNameMD; |
| 75 | llvm::NamedMDNode *ExportForEachMD; |
| 76 | |
| 77 | llvm::MDString *MDString; |
| 78 | llvm::MDNode *MDNode; |
| 79 | |
| 80 | ExportForEachNameMD = |
| 81 | Module.getOrInsertNamedMetadata(export_foreach_name_metadata_name); |
| 82 | MDString = llvm::MDString::get(Module.getContext(), Function.getName()); |
| 83 | MDNode = llvm::MDNode::get(Module.getContext(), MDString); |
| 84 | ExportForEachNameMD->addOperand(MDNode); |
| 85 | |
| 86 | ExportForEachMD = |
| 87 | Module.getOrInsertNamedMetadata(export_foreach_metadata_name); |
| 88 | MDString = llvm::MDString::get(Module.getContext(), |
| 89 | llvm::utostr_32(Signature)); |
| 90 | MDNode = llvm::MDNode::get(Module.getContext(), MDString); |
| 91 | ExportForEachMD->addOperand(MDNode); |
| 92 | } |