TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 18 | #include "tbaa_info.h" |
| 19 | |
| 20 | #include <llvm/ADT/SmallVector.h> |
| 21 | #include <llvm/ADT/StringRef.h> |
| 22 | #include <llvm/Constants.h> |
| 23 | #include <llvm/Metadata.h> |
| 24 | #include <llvm/Type.h> |
| 25 | |
| 26 | |
| 27 | namespace art { |
| 28 | namespace compiler_llvm { |
| 29 | |
| 30 | |
| 31 | llvm::MDNode* TBAAInfo::GetRootType() { |
| 32 | if (root_ == NULL) { |
| 33 | root_ = GenTBAANode("Art TBAA Root"); |
| 34 | } |
| 35 | return root_; |
| 36 | } |
| 37 | |
| 38 | llvm::MDNode* TBAAInfo::GetSpecialType(TBAASpecialType sty_id) { |
| 39 | DCHECK_GE(sty_id, 0) << "Unknown TBAA special type: " << sty_id; |
| 40 | DCHECK_LT(sty_id, MAX_TBAA_SPECIAL_TYPE) << "Unknown TBAA special type: " << sty_id; |
| 41 | |
| 42 | llvm::MDNode*& spec_ty = special_type_[sty_id]; |
| 43 | if (spec_ty == NULL) { |
| 44 | switch (sty_id) { |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame^] | 45 | case kTBAARegister: spec_ty = GenTBAANode("Register", GetRootType()); break; |
| 46 | case kTBAAStackTemp: spec_ty = GenTBAANode("StackTemp", GetRootType()); break; |
| 47 | case kTBAAHeapArray: spec_ty = GenTBAANode("HeapArray", GetRootType()); break; |
| 48 | case kTBAAHeapInstance: spec_ty = GenTBAANode("HeapInstance", GetRootType()); break; |
| 49 | case kTBAAHeapStatic: spec_ty = GenTBAANode("HeapStatic", GetRootType()); break; |
| 50 | case kTBAAJRuntime: spec_ty = GenTBAANode("JRuntime", GetRootType()); break; |
| 51 | case kTBAARuntimeInfo: spec_ty = GenTBAANode("RuntimeInfo", GetRootType()); break; |
| 52 | case kTBAAConstJObject: spec_ty = GenTBAANode("ConstJObject", GetRootType(), true); break; |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 53 | default: |
| 54 | LOG(FATAL) << "Unknown TBAA special type: " << sty_id; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | return spec_ty; |
| 59 | } |
| 60 | |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame^] | 61 | llvm::MDNode* TBAAInfo::GetMemoryJType(TBAASpecialType sty_id, JType jty_id) { |
| 62 | DCHECK(sty_id == kTBAAHeapArray || |
| 63 | sty_id == kTBAAHeapInstance || |
| 64 | sty_id == kTBAAHeapStatic) << "SpecialType must be array, identified, or static"; |
| 65 | |
| 66 | DCHECK_GE(jty_id, 0) << "Unknown JType: " << jty_id; |
| 67 | DCHECK_LT(jty_id, MAX_JTYPE) << "Unknown JType: " << jty_id; |
| 68 | DCHECK_NE(jty_id, kVoid) << "Can't load/store Void type!"; |
| 69 | |
| 70 | std::string name; |
| 71 | size_t sty_mapped_index = 0; |
| 72 | switch (sty_id) { |
| 73 | case kTBAAHeapArray: sty_mapped_index = 0; name = "HeapArray "; break; |
| 74 | case kTBAAHeapInstance: sty_mapped_index = 1; name = "HeapInstance "; break; |
| 75 | case kTBAAHeapStatic: sty_mapped_index = 2; name = "HeapStatic "; break; |
| 76 | default: |
| 77 | LOG(FATAL) << "Unknown TBAA special type: " << sty_id; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | llvm::MDNode*& spec_ty = memory_jtype_[sty_mapped_index][jty_id]; |
| 82 | if (spec_ty != NULL) { |
| 83 | return spec_ty; |
| 84 | } |
| 85 | |
| 86 | switch (jty_id) { |
| 87 | case kBoolean: name += "Boolean"; break; |
| 88 | case kByte: name += "Byte"; break; |
| 89 | case kChar: name += "Char"; break; |
| 90 | case kShort: name += "Short"; break; |
| 91 | case kInt: name += "Int"; break; |
| 92 | case kLong: name += "Long"; break; |
| 93 | case kFloat: name += "Float"; break; |
| 94 | case kDouble: name += "Double"; break; |
| 95 | case kObject: name += "Object"; break; |
| 96 | default: |
| 97 | LOG(FATAL) << "Unknown JType: " << jty_id; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | spec_ty = GenTBAANode(name, GetSpecialType(sty_id)); |
| 102 | return spec_ty; |
| 103 | } |
| 104 | |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 105 | llvm::MDNode* TBAAInfo::GenTBAANode(llvm::StringRef name, llvm::MDNode* parent, bool read_only) { |
| 106 | llvm::SmallVector<llvm::Value*, 3> array_ref; |
| 107 | |
| 108 | array_ref.push_back(llvm::MDString::get(context_, name)); |
| 109 | if (parent != NULL) { |
| 110 | array_ref.push_back(parent); |
| 111 | } |
| 112 | if (read_only != false) { |
| 113 | array_ref.push_back(llvm::ConstantInt::get(llvm::Type::getInt1Ty(context_), read_only)); |
| 114 | } |
| 115 | |
| 116 | return llvm::MDNode::get(context_, array_ref); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | } // namespace compiler_llvm |
| 121 | } // namespace art |