blob: f21cfaf1d383932dd78e224891d7136f0eef2650 [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
TDYa1278fe384d2012-05-06 20:14:17 -070093 llvm::LoadInst* LoadFromObjectOffset(llvm::Value* object_addr,
94 int64_t offset,
95 llvm::Type* type,
96 TBAASpecialType special_ty) {
TDYa127aba61122012-05-04 18:28:36 -070097 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
TDYa12729c0cd12012-05-17 04:51:08 -0700108 llvm::LoadInst* LoadFromObjectOffset(llvm::Value* object_addr,
109 int64_t offset,
110 llvm::Type* type,
111 TBAASpecialType special_ty, JType j_ty) {
112 return LoadFromObjectOffset(object_addr, offset, type, tbaa_.GetMemoryJType(special_ty, j_ty));
113 }
114
115 void StoreToObjectOffset(llvm::Value* object_addr,
116 int64_t offset,
117 llvm::Value* new_value,
118 TBAASpecialType special_ty, JType j_ty) {
119 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
120 StoreToObjectOffset(object_addr, offset, new_value, tbaa_.GetMemoryJType(special_ty, j_ty));
121 }
122
TDYa127de479be2012-05-31 08:03:26 -0700123 void SetTBAA(llvm::Instruction* inst, TBAASpecialType special_ty) {
124 inst->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_.GetSpecialType(special_ty));
TDYa127145d4912012-05-06 21:44:45 -0700125 }
126
TDYa127aba61122012-05-04 18:28:36 -0700127
128 //--------------------------------------------------------------------------
TDYa127ac7b5bb2012-05-11 13:17:49 -0700129 // Static Branch Prediction
130 //--------------------------------------------------------------------------
131
132 // Import the orignal conditional branch
133 using LLVMIRBuilder::CreateCondBr;
134 llvm::BranchInst* CreateCondBr(llvm::Value *cond,
135 llvm::BasicBlock* true_bb,
136 llvm::BasicBlock* false_bb,
137 ExpectCond expect) {
Shih-wei Liao35876772012-05-15 12:02:12 -0700138 DCHECK_LT(expect, MAX_EXPECT) << "MAX_EXPECT is not for branch weight";
TDYa127ac7b5bb2012-05-11 13:17:49 -0700139
140 llvm::BranchInst* branch_inst = LLVMIRBuilder::CreateCondBr(cond, true_bb, false_bb);
141 branch_inst->setMetadata(llvm::LLVMContext::MD_prof, expect_cond_[expect]);
142 return branch_inst;
143 }
144
145
146 //--------------------------------------------------------------------------
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800147 // Pointer Arithmetic Helper Function
148 //--------------------------------------------------------------------------
149
150 llvm::IntegerType* getPtrEquivIntTy() {
151 return getInt32Ty();
152 }
153
154 size_t getSizeOfPtrEquivInt() {
155 return 4;
156 }
157
158 llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
159 return getPtrEquivInt(getSizeOfPtrEquivInt());
160 }
161
TDYa127ee1f59b2012-04-25 00:56:40 -0700162 llvm::ConstantInt* getPtrEquivInt(int64_t i) {
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800163 return llvm::ConstantInt::get(getPtrEquivIntTy(), i);
164 }
165
166 llvm::Value* CreatePtrDisp(llvm::Value* base,
167 llvm::Value* offset,
168 llvm::PointerType* ret_ty) {
169
170 llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
171 llvm::Value* result_int = CreateAdd(base_int, offset);
172 llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
173
174 return result;
175 }
176
177 llvm::Value* CreatePtrDisp(llvm::Value* base,
178 llvm::Value* bs,
179 llvm::Value* count,
180 llvm::Value* offset,
181 llvm::PointerType* ret_ty) {
182
183 llvm::Value* block_offset = CreateMul(bs, count);
184 llvm::Value* total_offset = CreateAdd(block_offset, offset);
185
186 return CreatePtrDisp(base, total_offset, ret_ty);
187 }
188
TDYa1278fe384d2012-05-06 20:14:17 -0700189 llvm::LoadInst* LoadFromObjectOffset(llvm::Value* object_addr,
190 int64_t offset,
191 llvm::Type* type,
192 llvm::MDNode* tbaa_info) {
TDYa1275bb86012012-04-11 05:57:28 -0700193 // Convert offset to llvm::value
194 llvm::Value* llvm_offset = getPtrEquivInt(offset);
195 // Calculate the value's address
196 llvm::Value* value_addr = CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
197 // Load
TDYa127aba61122012-05-04 18:28:36 -0700198 return CreateLoad(value_addr, tbaa_info);
TDYa1275bb86012012-04-11 05:57:28 -0700199 }
200
TDYa127aba61122012-05-04 18:28:36 -0700201 void StoreToObjectOffset(llvm::Value* object_addr,
202 int64_t offset,
203 llvm::Value* new_value,
204 llvm::MDNode* tbaa_info) {
TDYa1275bb86012012-04-11 05:57:28 -0700205 // Convert offset to llvm::value
206 llvm::Value* llvm_offset = getPtrEquivInt(offset);
207 // Calculate the value's address
208 llvm::Value* value_addr = CreatePtrDisp(object_addr,
209 llvm_offset,
210 new_value->getType()->getPointerTo());
211 // Store
TDYa127aba61122012-05-04 18:28:36 -0700212 CreateStore(new_value, value_addr, tbaa_info);
TDYa1275bb86012012-04-11 05:57:28 -0700213 }
214
Shih-wei Liao4c1f4252012-02-13 09:57:20 -0800215
216 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +0800217 // Runtime Helper Function
218 //--------------------------------------------------------------------------
219
TDYa127de479be2012-05-31 08:03:26 -0700220 RuntimeSupportBuilder& Runtime() {
221 return *runtime_support_;
222 }
223
224 // TODO: Deprecate
TDYa127d668a062012-04-13 12:36:57 -0700225 llvm::Function* GetRuntime(runtime_support::RuntimeId rt) {
226 return runtime_support_->GetRuntimeSupportFunction(rt);
227 }
228
TDYa127de479be2012-05-31 08:03:26 -0700229 // TODO: Deprecate
TDYa127d668a062012-04-13 12:36:57 -0700230 void SetRuntimeSupport(RuntimeSupportBuilder* runtime_support) {
231 // Can only set once. We can't do this on constructor, because RuntimeSupportBuilder needs
232 // IRBuilder.
233 if (runtime_support_ == NULL && runtime_support != NULL) {
234 runtime_support_ = runtime_support;
235 }
236 }
Logan Chien42e0e152012-01-13 15:42:36 +0800237
238
239 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800240 // Type Helper Function
241 //--------------------------------------------------------------------------
242
243 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
244 return getJType(GetJTypeFromShorty(shorty_jty), space);
245 }
246
247 llvm::Type* getJType(JType jty, JTypeSpace space) {
248 switch (space) {
249 case kAccurate:
250 return getJTypeInAccurateSpace(jty);
251
252 case kReg:
253 case kField: // Currently field space is equivalent to register space.
254 return getJTypeInRegSpace(jty);
255
256 case kArray:
257 return getJTypeInArraySpace(jty);
258 }
259
Logan Chien83426162011-12-09 09:29:50 +0800260 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800261 return NULL;
262 }
263
264 llvm::Type* getJVoidTy() {
265 return getVoidTy();
266 }
267
268 llvm::IntegerType* getJBooleanTy() {
269 return getInt1Ty();
270 }
271
272 llvm::IntegerType* getJByteTy() {
273 return getInt8Ty();
274 }
275
276 llvm::IntegerType* getJCharTy() {
277 return getInt16Ty();
278 }
279
280 llvm::IntegerType* getJShortTy() {
281 return getInt16Ty();
282 }
283
284 llvm::IntegerType* getJIntTy() {
285 return getInt32Ty();
286 }
287
288 llvm::IntegerType* getJLongTy() {
289 return getInt64Ty();
290 }
291
292 llvm::Type* getJFloatTy() {
293 return getFloatTy();
294 }
295
296 llvm::Type* getJDoubleTy() {
297 return getDoubleTy();
298 }
299
300 llvm::PointerType* getJObjectTy() {
301 return jobject_type_;
302 }
303
TDYa1270de52be2012-05-27 20:49:31 -0700304 llvm::Type* getArtFrameTy() {
305 return art_frame_type_;
306 }
307
Logan Chienf04364f2012-02-10 12:01:39 +0800308 llvm::PointerType* getJEnvTy() {
309 return jenv_type_;
310 }
311
312 llvm::Type* getJValueTy() {
313 // NOTE: JValue is an union type, which may contains boolean, byte, char,
314 // short, int, long, float, double, Object. However, LLVM itself does
315 // not support union type, so we have to return a type with biggest size,
316 // then bitcast it before we use it.
317 return getJLongTy();
318 }
319
Logan Chien8dfcbea2012-02-17 18:50:32 +0800320 llvm::StructType* getShadowFrameTy(uint32_t sirt_size);
321
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800322
323 //--------------------------------------------------------------------------
324 // Constant Value Helper Function
325 //--------------------------------------------------------------------------
326
327 llvm::ConstantInt* getJBoolean(bool is_true) {
328 return (is_true) ? getTrue() : getFalse();
329 }
330
331 llvm::ConstantInt* getJByte(int8_t i) {
332 return llvm::ConstantInt::getSigned(getJByteTy(), i);
333 }
334
335 llvm::ConstantInt* getJChar(int16_t i) {
336 return llvm::ConstantInt::getSigned(getJCharTy(), i);
337 }
338
339 llvm::ConstantInt* getJShort(int16_t i) {
340 return llvm::ConstantInt::getSigned(getJShortTy(), i);
341 }
342
343 llvm::ConstantInt* getJInt(int32_t i) {
344 return llvm::ConstantInt::getSigned(getJIntTy(), i);
345 }
346
347 llvm::ConstantInt* getJLong(int64_t i) {
348 return llvm::ConstantInt::getSigned(getJLongTy(), i);
349 }
350
351 llvm::Constant* getJFloat(float f) {
352 return llvm::ConstantFP::get(getJFloatTy(), f);
353 }
354
355 llvm::Constant* getJDouble(double d) {
356 return llvm::ConstantFP::get(getJDoubleTy(), d);
357 }
358
359 llvm::ConstantPointerNull* getJNull() {
360 return llvm::ConstantPointerNull::get(getJObjectTy());
361 }
362
363 llvm::Constant* getJZero(char shorty_jty) {
364 return getJZero(GetJTypeFromShorty(shorty_jty));
365 }
366
367 llvm::Constant* getJZero(JType jty) {
368 switch (jty) {
369 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800370 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800371 return NULL;
372
373 case kBoolean:
374 return getJBoolean(false);
375
376 case kByte:
377 return getJByte(0);
378
379 case kChar:
380 return getJChar(0);
381
382 case kShort:
383 return getJShort(0);
384
385 case kInt:
386 return getJInt(0);
387
388 case kLong:
389 return getJLong(0);
390
391 case kFloat:
392 return getJFloat(0.0f);
393
394 case kDouble:
395 return getJDouble(0.0);
396
397 case kObject:
398 return getJNull();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800399
TDYa127706e7db2012-05-06 00:05:33 -0700400 default:
401 LOG(FATAL) << "Unknown java type: " << jty;
402 return NULL;
403 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800404 }
405
406
407 private:
408 //--------------------------------------------------------------------------
409 // Type Helper Function (Private)
410 //--------------------------------------------------------------------------
411
412 llvm::Type* getJTypeInAccurateSpace(JType jty);
413 llvm::Type* getJTypeInRegSpace(JType jty);
414 llvm::Type* getJTypeInArraySpace(JType jty);
415
416
417 private:
Logan Chien6a917992012-02-17 18:43:48 +0800418 llvm::Module* module_;
419
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800420 llvm::PointerType* jobject_type_;
421
Logan Chienf04364f2012-02-10 12:01:39 +0800422 llvm::PointerType* jenv_type_;
423
Logan Chien8dfcbea2012-02-17 18:50:32 +0800424 llvm::StructType* art_frame_type_;
425
TDYa127aba61122012-05-04 18:28:36 -0700426 TBAAInfo tbaa_;
427
TDYa127d668a062012-04-13 12:36:57 -0700428 RuntimeSupportBuilder* runtime_support_;
Logan Chien42e0e152012-01-13 15:42:36 +0800429
TDYa127ac7b5bb2012-05-11 13:17:49 -0700430 llvm::MDNode* expect_cond_[MAX_EXPECT];
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800431};
432
433
434} // namespace compiler_llvm
435} // namespace art
436
437#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_