blob: 46c37f18adc54b59f1676cd861981f864b6d3b1a [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
TDYa127145d4912012-05-06 21:44:45 -0700123 void SetTBAACall(llvm::CallInst* call_inst, TBAASpecialType special_ty) {
124 call_inst->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_.GetSpecialType(special_ty));
125 }
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
TDYa127d668a062012-04-13 12:36:57 -0700220 llvm::Function* GetRuntime(runtime_support::RuntimeId rt) {
221 return runtime_support_->GetRuntimeSupportFunction(rt);
222 }
223
224 void SetRuntimeSupport(RuntimeSupportBuilder* runtime_support) {
225 // Can only set once. We can't do this on constructor, because RuntimeSupportBuilder needs
226 // IRBuilder.
227 if (runtime_support_ == NULL && runtime_support != NULL) {
228 runtime_support_ = runtime_support;
229 }
230 }
Logan Chien42e0e152012-01-13 15:42:36 +0800231
232
233 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800234 // Type Helper Function
235 //--------------------------------------------------------------------------
236
237 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
238 return getJType(GetJTypeFromShorty(shorty_jty), space);
239 }
240
241 llvm::Type* getJType(JType jty, JTypeSpace space) {
242 switch (space) {
243 case kAccurate:
244 return getJTypeInAccurateSpace(jty);
245
246 case kReg:
247 case kField: // Currently field space is equivalent to register space.
248 return getJTypeInRegSpace(jty);
249
250 case kArray:
251 return getJTypeInArraySpace(jty);
252 }
253
Logan Chien83426162011-12-09 09:29:50 +0800254 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800255 return NULL;
256 }
257
258 llvm::Type* getJVoidTy() {
259 return getVoidTy();
260 }
261
262 llvm::IntegerType* getJBooleanTy() {
263 return getInt1Ty();
264 }
265
266 llvm::IntegerType* getJByteTy() {
267 return getInt8Ty();
268 }
269
270 llvm::IntegerType* getJCharTy() {
271 return getInt16Ty();
272 }
273
274 llvm::IntegerType* getJShortTy() {
275 return getInt16Ty();
276 }
277
278 llvm::IntegerType* getJIntTy() {
279 return getInt32Ty();
280 }
281
282 llvm::IntegerType* getJLongTy() {
283 return getInt64Ty();
284 }
285
286 llvm::Type* getJFloatTy() {
287 return getFloatTy();
288 }
289
290 llvm::Type* getJDoubleTy() {
291 return getDoubleTy();
292 }
293
294 llvm::PointerType* getJObjectTy() {
295 return jobject_type_;
296 }
297
TDYa1270de52be2012-05-27 20:49:31 -0700298 llvm::Type* getArtFrameTy() {
299 return art_frame_type_;
300 }
301
Logan Chienf04364f2012-02-10 12:01:39 +0800302 llvm::PointerType* getJEnvTy() {
303 return jenv_type_;
304 }
305
306 llvm::Type* getJValueTy() {
307 // NOTE: JValue is an union type, which may contains boolean, byte, char,
308 // short, int, long, float, double, Object. However, LLVM itself does
309 // not support union type, so we have to return a type with biggest size,
310 // then bitcast it before we use it.
311 return getJLongTy();
312 }
313
Logan Chien8dfcbea2012-02-17 18:50:32 +0800314 llvm::StructType* getShadowFrameTy(uint32_t sirt_size);
315
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800316
317 //--------------------------------------------------------------------------
318 // Constant Value Helper Function
319 //--------------------------------------------------------------------------
320
321 llvm::ConstantInt* getJBoolean(bool is_true) {
322 return (is_true) ? getTrue() : getFalse();
323 }
324
325 llvm::ConstantInt* getJByte(int8_t i) {
326 return llvm::ConstantInt::getSigned(getJByteTy(), i);
327 }
328
329 llvm::ConstantInt* getJChar(int16_t i) {
330 return llvm::ConstantInt::getSigned(getJCharTy(), i);
331 }
332
333 llvm::ConstantInt* getJShort(int16_t i) {
334 return llvm::ConstantInt::getSigned(getJShortTy(), i);
335 }
336
337 llvm::ConstantInt* getJInt(int32_t i) {
338 return llvm::ConstantInt::getSigned(getJIntTy(), i);
339 }
340
341 llvm::ConstantInt* getJLong(int64_t i) {
342 return llvm::ConstantInt::getSigned(getJLongTy(), i);
343 }
344
345 llvm::Constant* getJFloat(float f) {
346 return llvm::ConstantFP::get(getJFloatTy(), f);
347 }
348
349 llvm::Constant* getJDouble(double d) {
350 return llvm::ConstantFP::get(getJDoubleTy(), d);
351 }
352
353 llvm::ConstantPointerNull* getJNull() {
354 return llvm::ConstantPointerNull::get(getJObjectTy());
355 }
356
357 llvm::Constant* getJZero(char shorty_jty) {
358 return getJZero(GetJTypeFromShorty(shorty_jty));
359 }
360
361 llvm::Constant* getJZero(JType jty) {
362 switch (jty) {
363 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800364 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800365 return NULL;
366
367 case kBoolean:
368 return getJBoolean(false);
369
370 case kByte:
371 return getJByte(0);
372
373 case kChar:
374 return getJChar(0);
375
376 case kShort:
377 return getJShort(0);
378
379 case kInt:
380 return getJInt(0);
381
382 case kLong:
383 return getJLong(0);
384
385 case kFloat:
386 return getJFloat(0.0f);
387
388 case kDouble:
389 return getJDouble(0.0);
390
391 case kObject:
392 return getJNull();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800393
TDYa127706e7db2012-05-06 00:05:33 -0700394 default:
395 LOG(FATAL) << "Unknown java type: " << jty;
396 return NULL;
397 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800398 }
399
400
401 private:
402 //--------------------------------------------------------------------------
403 // Type Helper Function (Private)
404 //--------------------------------------------------------------------------
405
406 llvm::Type* getJTypeInAccurateSpace(JType jty);
407 llvm::Type* getJTypeInRegSpace(JType jty);
408 llvm::Type* getJTypeInArraySpace(JType jty);
409
410
411 private:
Logan Chien6a917992012-02-17 18:43:48 +0800412 llvm::Module* module_;
413
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800414 llvm::PointerType* jobject_type_;
415
Logan Chienf04364f2012-02-10 12:01:39 +0800416 llvm::PointerType* jenv_type_;
417
Logan Chien8dfcbea2012-02-17 18:50:32 +0800418 llvm::StructType* art_frame_type_;
419
TDYa127aba61122012-05-04 18:28:36 -0700420 TBAAInfo tbaa_;
421
TDYa127d668a062012-04-13 12:36:57 -0700422 RuntimeSupportBuilder* runtime_support_;
Logan Chien42e0e152012-01-13 15:42:36 +0800423
TDYa127ac7b5bb2012-05-11 13:17:49 -0700424 llvm::MDNode* expect_cond_[MAX_EXPECT];
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800425};
426
427
428} // namespace compiler_llvm
429} // namespace art
430
431#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_