blob: 03ef52541d2dc22194824337b1ae594ff9291ebd [file] [log] [blame]
TDYa127aba61122012-05-04 18:28:36 -07001/*
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
27namespace art {
28namespace compiler_llvm {
29
30
31llvm::MDNode* TBAAInfo::GetRootType() {
32 if (root_ == NULL) {
33 root_ = GenTBAANode("Art TBAA Root");
34 }
35 return root_;
36}
37
38llvm::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) {
TDYa127706e7db2012-05-06 00:05:33 -070045 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;
TDYa127d955bec2012-05-11 10:54:02 -070052 case kTBAAShadowFrame: spec_ty = GenTBAANode("ShadowFrame", GetRootType()); break;
TDYa127706e7db2012-05-06 00:05:33 -070053 case kTBAAConstJObject: spec_ty = GenTBAANode("ConstJObject", GetRootType(), true); break;
TDYa127aba61122012-05-04 18:28:36 -070054 default:
55 LOG(FATAL) << "Unknown TBAA special type: " << sty_id;
56 break;
57 }
58 }
59 return spec_ty;
60}
61
TDYa127706e7db2012-05-06 00:05:33 -070062llvm::MDNode* TBAAInfo::GetMemoryJType(TBAASpecialType sty_id, JType jty_id) {
63 DCHECK(sty_id == kTBAAHeapArray ||
64 sty_id == kTBAAHeapInstance ||
TDYa127d3e24c22012-05-05 20:54:19 -070065 sty_id == kTBAAHeapStatic) << "SpecialType must be array, instance, or static";
TDYa127706e7db2012-05-06 00:05:33 -070066
67 DCHECK_GE(jty_id, 0) << "Unknown JType: " << jty_id;
68 DCHECK_LT(jty_id, MAX_JTYPE) << "Unknown JType: " << jty_id;
69 DCHECK_NE(jty_id, kVoid) << "Can't load/store Void type!";
70
71 std::string name;
72 size_t sty_mapped_index = 0;
73 switch (sty_id) {
74 case kTBAAHeapArray: sty_mapped_index = 0; name = "HeapArray "; break;
75 case kTBAAHeapInstance: sty_mapped_index = 1; name = "HeapInstance "; break;
76 case kTBAAHeapStatic: sty_mapped_index = 2; name = "HeapStatic "; break;
77 default:
78 LOG(FATAL) << "Unknown TBAA special type: " << sty_id;
79 break;
80 }
81
82 llvm::MDNode*& spec_ty = memory_jtype_[sty_mapped_index][jty_id];
83 if (spec_ty != NULL) {
84 return spec_ty;
85 }
86
87 switch (jty_id) {
88 case kBoolean: name += "Boolean"; break;
89 case kByte: name += "Byte"; break;
90 case kChar: name += "Char"; break;
91 case kShort: name += "Short"; break;
92 case kInt: name += "Int"; break;
93 case kLong: name += "Long"; break;
94 case kFloat: name += "Float"; break;
95 case kDouble: name += "Double"; break;
96 case kObject: name += "Object"; break;
97 default:
98 LOG(FATAL) << "Unknown JType: " << jty_id;
99 break;
100 }
101
102 spec_ty = GenTBAANode(name, GetSpecialType(sty_id));
103 return spec_ty;
104}
105
TDYa127aba61122012-05-04 18:28:36 -0700106llvm::MDNode* TBAAInfo::GenTBAANode(llvm::StringRef name, llvm::MDNode* parent, bool read_only) {
107 llvm::SmallVector<llvm::Value*, 3> array_ref;
108
109 array_ref.push_back(llvm::MDString::get(context_, name));
110 if (parent != NULL) {
111 array_ref.push_back(parent);
112 }
113 if (read_only != false) {
114 array_ref.push_back(llvm::ConstantInt::get(llvm::Type::getInt1Ty(context_), read_only));
115 }
116
117 return llvm::MDNode::get(context_, array_ref);
118}
119
120
121} // namespace compiler_llvm
122} // namespace art