blob: 4b7642dde47b40eb639419253ba4de6cd642c469 [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
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#ifndef ART_SRC_COMPILER_LLVM_IR_BUILDER_H_
18#define ART_SRC_COMPILER_LLVM_IR_BUILDER_H_
19
20#include "backend_types.h"
TDYa127d668a062012-04-13 12:36:57 -070021#include "runtime_support_builder.h"
Logan Chien42e0e152012-01-13 15:42:36 +080022#include "runtime_support_func.h"
TDYa127aba61122012-05-04 18:28:36 -070023#include "tbaa_info.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024
25#include <llvm/Constants.h>
26#include <llvm/DerivedTypes.h>
TDYa127aba61122012-05-04 18:28:36 -070027#include <llvm/LLVMContext.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080028#include <llvm/Support/IRBuilder.h>
29#include <llvm/Type.h>
30
31#include <stdint.h>
32
33
34namespace art {
35namespace compiler_llvm {
36
37
38typedef llvm::IRBuilder<> LLVMIRBuilder;
39// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
40// switch "preserveNames" template parameter easily.
41
42
43class IRBuilder : public LLVMIRBuilder {
44 public:
45 //--------------------------------------------------------------------------
46 // General
47 //--------------------------------------------------------------------------
48
49 IRBuilder(llvm::LLVMContext& context, llvm::Module& module);
50
51
52 //--------------------------------------------------------------------------
TDYa127aba61122012-05-04 18:28:36 -070053 // Extend load & store for TBAA
54 //--------------------------------------------------------------------------
55
56 llvm::LoadInst* CreateLoad(llvm::Value* ptr, llvm::MDNode* tbaa_info) {
57 llvm::LoadInst* inst = LLVMIRBuilder::CreateLoad(ptr);
58 inst->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_info);
59 return inst;
60 }
61
62 llvm::StoreInst* CreateStore(llvm::Value* val, llvm::Value* ptr, llvm::MDNode* tbaa_info) {
63 llvm::StoreInst* inst = LLVMIRBuilder::CreateStore(val, ptr);
64 inst->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_info);
65 return inst;
66 }
67
68
69 //--------------------------------------------------------------------------
70 // TBAA
71 //--------------------------------------------------------------------------
72
73 // TODO: After we design the non-special TBAA info, re-design the TBAA interface.
74 llvm::LoadInst* CreateLoad(llvm::Value* ptr, TBAASpecialType special_ty) {
75 return CreateLoad(ptr, tbaa_.GetSpecialType(special_ty));
76 }
77
78 llvm::StoreInst* CreateStore(llvm::Value* val, llvm::Value* ptr, TBAASpecialType special_ty) {
79 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
80 return CreateStore(val, ptr, tbaa_.GetSpecialType(special_ty));
81 }
82
TDYa127706e7db2012-05-06 00:05:33 -070083 llvm::LoadInst* CreateLoad(llvm::Value* ptr, TBAASpecialType special_ty, JType j_ty) {
84 return CreateLoad(ptr, tbaa_.GetMemoryJType(special_ty, j_ty));
85 }
86
87 llvm::StoreInst* CreateStore(llvm::Value* val, llvm::Value* ptr,
88 TBAASpecialType special_ty, JType j_ty) {
89 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
90 return CreateStore(val, ptr, tbaa_.GetMemoryJType(special_ty, j_ty));
91 }
92
TDYa127aba61122012-05-04 18:28:36 -070093 llvm::Value* LoadFromObjectOffset(llvm::Value* object_addr,
94 int64_t offset,
95 llvm::Type* type,
96 TBAASpecialType special_ty) {
97 return LoadFromObjectOffset(object_addr, offset, type, tbaa_.GetSpecialType(special_ty));
98 }
99
100 void StoreToObjectOffset(llvm::Value* object_addr,
101 int64_t offset,
102 llvm::Value* new_value,
103 TBAASpecialType special_ty) {
104 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
105 StoreToObjectOffset(object_addr, offset, new_value, tbaa_.GetSpecialType(special_ty));
106 }
107
108
109 //--------------------------------------------------------------------------
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800110 // Pointer Arithmetic Helper Function
111 //--------------------------------------------------------------------------
112
113 llvm::IntegerType* getPtrEquivIntTy() {
114 return getInt32Ty();
115 }
116
117 size_t getSizeOfPtrEquivInt() {
118 return 4;
119 }
120
121 llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
122 return getPtrEquivInt(getSizeOfPtrEquivInt());
123 }
124
TDYa127ee1f59b2012-04-25 00:56:40 -0700125 llvm::ConstantInt* getPtrEquivInt(int64_t i) {
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800126 return llvm::ConstantInt::get(getPtrEquivIntTy(), i);
127 }
128
129 llvm::Value* CreatePtrDisp(llvm::Value* base,
130 llvm::Value* offset,
131 llvm::PointerType* ret_ty) {
132
133 llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
134 llvm::Value* result_int = CreateAdd(base_int, offset);
135 llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
136
137 return result;
138 }
139
140 llvm::Value* CreatePtrDisp(llvm::Value* base,
141 llvm::Value* bs,
142 llvm::Value* count,
143 llvm::Value* offset,
144 llvm::PointerType* ret_ty) {
145
146 llvm::Value* block_offset = CreateMul(bs, count);
147 llvm::Value* total_offset = CreateAdd(block_offset, offset);
148
149 return CreatePtrDisp(base, total_offset, ret_ty);
150 }
151
TDYa127aba61122012-05-04 18:28:36 -0700152 llvm::Value* LoadFromObjectOffset(llvm::Value* object_addr,
153 int64_t offset,
154 llvm::Type* type,
155 llvm::MDNode* tbaa_info) {
TDYa1275bb86012012-04-11 05:57:28 -0700156 // Convert offset to llvm::value
157 llvm::Value* llvm_offset = getPtrEquivInt(offset);
158 // Calculate the value's address
159 llvm::Value* value_addr = CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
160 // Load
TDYa127aba61122012-05-04 18:28:36 -0700161 return CreateLoad(value_addr, tbaa_info);
TDYa1275bb86012012-04-11 05:57:28 -0700162 }
163
TDYa127aba61122012-05-04 18:28:36 -0700164 void StoreToObjectOffset(llvm::Value* object_addr,
165 int64_t offset,
166 llvm::Value* new_value,
167 llvm::MDNode* tbaa_info) {
TDYa1275bb86012012-04-11 05:57:28 -0700168 // Convert offset to llvm::value
169 llvm::Value* llvm_offset = getPtrEquivInt(offset);
170 // Calculate the value's address
171 llvm::Value* value_addr = CreatePtrDisp(object_addr,
172 llvm_offset,
173 new_value->getType()->getPointerTo());
174 // Store
TDYa127aba61122012-05-04 18:28:36 -0700175 CreateStore(new_value, value_addr, tbaa_info);
TDYa1275bb86012012-04-11 05:57:28 -0700176 }
177
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800178
179 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +0800180 // Runtime Helper Function
181 //--------------------------------------------------------------------------
182
TDYa127d668a062012-04-13 12:36:57 -0700183 llvm::Function* GetRuntime(runtime_support::RuntimeId rt) {
184 return runtime_support_->GetRuntimeSupportFunction(rt);
185 }
186
187 void SetRuntimeSupport(RuntimeSupportBuilder* runtime_support) {
188 // Can only set once. We can't do this on constructor, because RuntimeSupportBuilder needs
189 // IRBuilder.
190 if (runtime_support_ == NULL && runtime_support != NULL) {
191 runtime_support_ = runtime_support;
192 }
193 }
Logan Chien42e0e152012-01-13 15:42:36 +0800194
195
196 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800197 // Type Helper Function
198 //--------------------------------------------------------------------------
199
200 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
201 return getJType(GetJTypeFromShorty(shorty_jty), space);
202 }
203
204 llvm::Type* getJType(JType jty, JTypeSpace space) {
205 switch (space) {
206 case kAccurate:
207 return getJTypeInAccurateSpace(jty);
208
209 case kReg:
210 case kField: // Currently field space is equivalent to register space.
211 return getJTypeInRegSpace(jty);
212
213 case kArray:
214 return getJTypeInArraySpace(jty);
215 }
216
Logan Chien83426162011-12-09 09:29:50 +0800217 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800218 return NULL;
219 }
220
221 llvm::Type* getJVoidTy() {
222 return getVoidTy();
223 }
224
225 llvm::IntegerType* getJBooleanTy() {
226 return getInt1Ty();
227 }
228
229 llvm::IntegerType* getJByteTy() {
230 return getInt8Ty();
231 }
232
233 llvm::IntegerType* getJCharTy() {
234 return getInt16Ty();
235 }
236
237 llvm::IntegerType* getJShortTy() {
238 return getInt16Ty();
239 }
240
241 llvm::IntegerType* getJIntTy() {
242 return getInt32Ty();
243 }
244
245 llvm::IntegerType* getJLongTy() {
246 return getInt64Ty();
247 }
248
249 llvm::Type* getJFloatTy() {
250 return getFloatTy();
251 }
252
253 llvm::Type* getJDoubleTy() {
254 return getDoubleTy();
255 }
256
257 llvm::PointerType* getJObjectTy() {
258 return jobject_type_;
259 }
260
Logan Chienf04364f2012-02-10 12:01:39 +0800261 llvm::PointerType* getJEnvTy() {
262 return jenv_type_;
263 }
264
265 llvm::Type* getJValueTy() {
266 // NOTE: JValue is an union type, which may contains boolean, byte, char,
267 // short, int, long, float, double, Object. However, LLVM itself does
268 // not support union type, so we have to return a type with biggest size,
269 // then bitcast it before we use it.
270 return getJLongTy();
271 }
272
Logan Chien8dfcbea2012-02-17 18:50:32 +0800273 llvm::StructType* getShadowFrameTy(uint32_t sirt_size);
274
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800275
276 //--------------------------------------------------------------------------
277 // Constant Value Helper Function
278 //--------------------------------------------------------------------------
279
280 llvm::ConstantInt* getJBoolean(bool is_true) {
281 return (is_true) ? getTrue() : getFalse();
282 }
283
284 llvm::ConstantInt* getJByte(int8_t i) {
285 return llvm::ConstantInt::getSigned(getJByteTy(), i);
286 }
287
288 llvm::ConstantInt* getJChar(int16_t i) {
289 return llvm::ConstantInt::getSigned(getJCharTy(), i);
290 }
291
292 llvm::ConstantInt* getJShort(int16_t i) {
293 return llvm::ConstantInt::getSigned(getJShortTy(), i);
294 }
295
296 llvm::ConstantInt* getJInt(int32_t i) {
297 return llvm::ConstantInt::getSigned(getJIntTy(), i);
298 }
299
300 llvm::ConstantInt* getJLong(int64_t i) {
301 return llvm::ConstantInt::getSigned(getJLongTy(), i);
302 }
303
304 llvm::Constant* getJFloat(float f) {
305 return llvm::ConstantFP::get(getJFloatTy(), f);
306 }
307
308 llvm::Constant* getJDouble(double d) {
309 return llvm::ConstantFP::get(getJDoubleTy(), d);
310 }
311
312 llvm::ConstantPointerNull* getJNull() {
313 return llvm::ConstantPointerNull::get(getJObjectTy());
314 }
315
316 llvm::Constant* getJZero(char shorty_jty) {
317 return getJZero(GetJTypeFromShorty(shorty_jty));
318 }
319
320 llvm::Constant* getJZero(JType jty) {
321 switch (jty) {
322 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800323 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800324 return NULL;
325
326 case kBoolean:
327 return getJBoolean(false);
328
329 case kByte:
330 return getJByte(0);
331
332 case kChar:
333 return getJChar(0);
334
335 case kShort:
336 return getJShort(0);
337
338 case kInt:
339 return getJInt(0);
340
341 case kLong:
342 return getJLong(0);
343
344 case kFloat:
345 return getJFloat(0.0f);
346
347 case kDouble:
348 return getJDouble(0.0);
349
350 case kObject:
351 return getJNull();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800352
TDYa127706e7db2012-05-06 00:05:33 -0700353 default:
354 LOG(FATAL) << "Unknown java type: " << jty;
355 return NULL;
356 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800357 }
358
359
360 private:
361 //--------------------------------------------------------------------------
362 // Type Helper Function (Private)
363 //--------------------------------------------------------------------------
364
365 llvm::Type* getJTypeInAccurateSpace(JType jty);
366 llvm::Type* getJTypeInRegSpace(JType jty);
367 llvm::Type* getJTypeInArraySpace(JType jty);
368
369
370 private:
Logan Chien6a917992012-02-17 18:43:48 +0800371 llvm::Module* module_;
372
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800373 llvm::PointerType* jobject_type_;
374
Logan Chienf04364f2012-02-10 12:01:39 +0800375 llvm::PointerType* jenv_type_;
376
Logan Chien8dfcbea2012-02-17 18:50:32 +0800377 llvm::StructType* art_frame_type_;
378
TDYa127aba61122012-05-04 18:28:36 -0700379 TBAAInfo tbaa_;
380
TDYa127d668a062012-04-13 12:36:57 -0700381 RuntimeSupportBuilder* runtime_support_;
Logan Chien42e0e152012-01-13 15:42:36 +0800382
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800383};
384
385
386} // namespace compiler_llvm
387} // namespace art
388
389#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_