blob: 734b22f791696e58d3c19713e768fe0080cd7c38 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -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#ifndef ART_SRC_COMPILER_LLVM_IR_BUILDER_H_
18#define ART_SRC_COMPILER_LLVM_IR_BUILDER_H_
19
20#include "backend_types.h"
21#include "dex/compiler_enums.h"
22#include "intrinsic_helper.h"
23#include "md_builder.h"
24#include "runtime_support_builder.h"
25#include "runtime_support_llvm_func.h"
26
27#include <llvm/IR/Constants.h>
28#include <llvm/IR/DerivedTypes.h>
29#include <llvm/IR/IRBuilder.h>
30#include <llvm/IR/LLVMContext.h>
31#include <llvm/IR/Type.h>
32#include <llvm/Support/NoFolder.h>
33
34#include <stdint.h>
35
36
37namespace art {
38namespace llvm {
39
40class InserterWithDexOffset : public ::llvm::IRBuilderDefaultInserter<true> {
41 public:
42 InserterWithDexOffset() : node_(NULL) {}
43
44 void InsertHelper(::llvm::Instruction *I, const ::llvm::Twine &Name,
45 ::llvm::BasicBlock *BB,
46 ::llvm::BasicBlock::iterator InsertPt) const {
47 ::llvm::IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
48 if (node_ != NULL) {
49 I->setMetadata("DexOff", node_);
50 }
51 }
52
53 void SetDexOffset(::llvm::MDNode* node) {
54 node_ = node;
55 }
56 private:
57 ::llvm::MDNode* node_;
58};
59
60typedef ::llvm::IRBuilder<true, ::llvm::ConstantFolder, InserterWithDexOffset> LLVMIRBuilder;
61// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
62// switch "preserveNames" template parameter easily.
63
64
65class IRBuilder : public LLVMIRBuilder {
66 public:
67 //--------------------------------------------------------------------------
68 // General
69 //--------------------------------------------------------------------------
70
71 IRBuilder(::llvm::LLVMContext& context, ::llvm::Module& module,
72 IntrinsicHelper& intrinsic_helper);
73
74
75 //--------------------------------------------------------------------------
76 // Extend load & store for TBAA
77 //--------------------------------------------------------------------------
78
79 ::llvm::LoadInst* CreateLoad(::llvm::Value* ptr, ::llvm::MDNode* tbaa_info) {
80 ::llvm::LoadInst* inst = LLVMIRBuilder::CreateLoad(ptr);
81 inst->setMetadata(::llvm::LLVMContext::MD_tbaa, tbaa_info);
82 return inst;
83 }
84
85 ::llvm::StoreInst* CreateStore(::llvm::Value* val, ::llvm::Value* ptr, ::llvm::MDNode* tbaa_info) {
86 ::llvm::StoreInst* inst = LLVMIRBuilder::CreateStore(val, ptr);
87 inst->setMetadata(::llvm::LLVMContext::MD_tbaa, tbaa_info);
88 return inst;
89 }
90
91 ::llvm::AtomicCmpXchgInst*
92 CreateAtomicCmpXchgInst(::llvm::Value* ptr, ::llvm::Value* cmp, ::llvm::Value* val,
93 ::llvm::MDNode* tbaa_info) {
94 ::llvm::AtomicCmpXchgInst* inst =
95 LLVMIRBuilder::CreateAtomicCmpXchg(ptr, cmp, val, ::llvm::Acquire);
96 inst->setMetadata(::llvm::LLVMContext::MD_tbaa, tbaa_info);
97 return inst;
98 }
99
100 //--------------------------------------------------------------------------
101 // Extend memory barrier
102 //--------------------------------------------------------------------------
103 void CreateMemoryBarrier(MemBarrierKind barrier_kind) {
104#if ANDROID_SMP
105 // TODO: select atomic ordering according to given barrier kind.
106 CreateFence(::llvm::SequentiallyConsistent);
107#endif
108 }
109
110 //--------------------------------------------------------------------------
111 // TBAA
112 //--------------------------------------------------------------------------
113
114 // TODO: After we design the non-special TBAA info, re-design the TBAA interface.
115 ::llvm::LoadInst* CreateLoad(::llvm::Value* ptr, TBAASpecialType special_ty) {
116 return CreateLoad(ptr, mdb_.GetTBAASpecialType(special_ty));
117 }
118
119 ::llvm::StoreInst* CreateStore(::llvm::Value* val, ::llvm::Value* ptr, TBAASpecialType special_ty) {
120 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
121 return CreateStore(val, ptr, mdb_.GetTBAASpecialType(special_ty));
122 }
123
124 ::llvm::LoadInst* CreateLoad(::llvm::Value* ptr, TBAASpecialType special_ty, JType j_ty) {
125 return CreateLoad(ptr, mdb_.GetTBAAMemoryJType(special_ty, j_ty));
126 }
127
128 ::llvm::StoreInst* CreateStore(::llvm::Value* val, ::llvm::Value* ptr,
129 TBAASpecialType special_ty, JType j_ty) {
130 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
131 return CreateStore(val, ptr, mdb_.GetTBAAMemoryJType(special_ty, j_ty));
132 }
133
134 ::llvm::LoadInst* LoadFromObjectOffset(::llvm::Value* object_addr,
135 int64_t offset,
136 ::llvm::Type* type,
137 TBAASpecialType special_ty) {
138 return LoadFromObjectOffset(object_addr, offset, type, mdb_.GetTBAASpecialType(special_ty));
139 }
140
141 void StoreToObjectOffset(::llvm::Value* object_addr,
142 int64_t offset,
143 ::llvm::Value* new_value,
144 TBAASpecialType special_ty) {
145 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
146 StoreToObjectOffset(object_addr, offset, new_value, mdb_.GetTBAASpecialType(special_ty));
147 }
148
149 ::llvm::LoadInst* LoadFromObjectOffset(::llvm::Value* object_addr,
150 int64_t offset,
151 ::llvm::Type* type,
152 TBAASpecialType special_ty, JType j_ty) {
153 return LoadFromObjectOffset(object_addr, offset, type, mdb_.GetTBAAMemoryJType(special_ty, j_ty));
154 }
155
156 void StoreToObjectOffset(::llvm::Value* object_addr,
157 int64_t offset,
158 ::llvm::Value* new_value,
159 TBAASpecialType special_ty, JType j_ty) {
160 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
161 StoreToObjectOffset(object_addr, offset, new_value, mdb_.GetTBAAMemoryJType(special_ty, j_ty));
162 }
163
164 ::llvm::AtomicCmpXchgInst*
165 CompareExchangeObjectOffset(::llvm::Value* object_addr,
166 int64_t offset,
167 ::llvm::Value* cmp_value,
168 ::llvm::Value* new_value,
169 TBAASpecialType special_ty) {
170 DCHECK_NE(special_ty, kTBAAConstJObject) << "ConstJObject is read only!";
171 return CompareExchangeObjectOffset(object_addr, offset, cmp_value, new_value,
172 mdb_.GetTBAASpecialType(special_ty));
173 }
174
175 void SetTBAA(::llvm::Instruction* inst, TBAASpecialType special_ty) {
176 inst->setMetadata(::llvm::LLVMContext::MD_tbaa, mdb_.GetTBAASpecialType(special_ty));
177 }
178
179
180 //--------------------------------------------------------------------------
181 // Static Branch Prediction
182 //--------------------------------------------------------------------------
183
184 // Import the orignal conditional branch
185 using LLVMIRBuilder::CreateCondBr;
186 ::llvm::BranchInst* CreateCondBr(::llvm::Value *cond,
187 ::llvm::BasicBlock* true_bb,
188 ::llvm::BasicBlock* false_bb,
189 ExpectCond expect) {
190 ::llvm::BranchInst* branch_inst = CreateCondBr(cond, true_bb, false_bb);
191 if (false) {
192 // TODO: http://b/8511695 Restore branch weight metadata
193 branch_inst->setMetadata(::llvm::LLVMContext::MD_prof, mdb_.GetBranchWeights(expect));
194 }
195 return branch_inst;
196 }
197
198
199 //--------------------------------------------------------------------------
200 // Pointer Arithmetic Helper Function
201 //--------------------------------------------------------------------------
202
203 ::llvm::IntegerType* getPtrEquivIntTy() {
204 return getInt32Ty();
205 }
206
207 size_t getSizeOfPtrEquivInt() {
208 return 4;
209 }
210
211 ::llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
212 return getPtrEquivInt(getSizeOfPtrEquivInt());
213 }
214
215 ::llvm::ConstantInt* getPtrEquivInt(int64_t i) {
216 return ::llvm::ConstantInt::get(getPtrEquivIntTy(), i);
217 }
218
219 ::llvm::Value* CreatePtrDisp(::llvm::Value* base,
220 ::llvm::Value* offset,
221 ::llvm::PointerType* ret_ty) {
222
223 ::llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
224 ::llvm::Value* result_int = CreateAdd(base_int, offset);
225 ::llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
226
227 return result;
228 }
229
230 ::llvm::Value* CreatePtrDisp(::llvm::Value* base,
231 ::llvm::Value* bs,
232 ::llvm::Value* count,
233 ::llvm::Value* offset,
234 ::llvm::PointerType* ret_ty) {
235
236 ::llvm::Value* block_offset = CreateMul(bs, count);
237 ::llvm::Value* total_offset = CreateAdd(block_offset, offset);
238
239 return CreatePtrDisp(base, total_offset, ret_ty);
240 }
241
242 ::llvm::LoadInst* LoadFromObjectOffset(::llvm::Value* object_addr,
243 int64_t offset,
244 ::llvm::Type* type,
245 ::llvm::MDNode* tbaa_info) {
246 // Convert offset to ::llvm::value
247 ::llvm::Value* llvm_offset = getPtrEquivInt(offset);
248 // Calculate the value's address
249 ::llvm::Value* value_addr = CreatePtrDisp(object_addr, llvm_offset, type->getPointerTo());
250 // Load
251 return CreateLoad(value_addr, tbaa_info);
252 }
253
254 void StoreToObjectOffset(::llvm::Value* object_addr,
255 int64_t offset,
256 ::llvm::Value* new_value,
257 ::llvm::MDNode* tbaa_info) {
258 // Convert offset to ::llvm::value
259 ::llvm::Value* llvm_offset = getPtrEquivInt(offset);
260 // Calculate the value's address
261 ::llvm::Value* value_addr = CreatePtrDisp(object_addr,
262 llvm_offset,
263 new_value->getType()->getPointerTo());
264 // Store
265 CreateStore(new_value, value_addr, tbaa_info);
266 }
267
268 ::llvm::AtomicCmpXchgInst* CompareExchangeObjectOffset(::llvm::Value* object_addr,
269 int64_t offset,
270 ::llvm::Value* cmp_value,
271 ::llvm::Value* new_value,
272 ::llvm::MDNode* tbaa_info) {
273 // Convert offset to ::llvm::value
274 ::llvm::Value* llvm_offset = getPtrEquivInt(offset);
275 // Calculate the value's address
276 ::llvm::Value* value_addr = CreatePtrDisp(object_addr,
277 llvm_offset,
278 new_value->getType()->getPointerTo());
279 // Atomic compare and exchange
280 return CreateAtomicCmpXchgInst(value_addr, cmp_value, new_value, tbaa_info);
281 }
282
283
284 //--------------------------------------------------------------------------
285 // Runtime Helper Function
286 //--------------------------------------------------------------------------
287
288 RuntimeSupportBuilder& Runtime() {
289 return *runtime_support_;
290 }
291
292 // TODO: Deprecate
293 ::llvm::Function* GetRuntime(runtime_support::RuntimeId rt) {
294 return runtime_support_->GetRuntimeSupportFunction(rt);
295 }
296
297 // TODO: Deprecate
298 void SetRuntimeSupport(RuntimeSupportBuilder* runtime_support) {
299 // Can only set once. We can't do this on constructor, because RuntimeSupportBuilder needs
300 // IRBuilder.
301 if (runtime_support_ == NULL && runtime_support != NULL) {
302 runtime_support_ = runtime_support;
303 }
304 }
305
306
307 //--------------------------------------------------------------------------
308 // Type Helper Function
309 //--------------------------------------------------------------------------
310
311 ::llvm::Type* getJType(char shorty_jty) {
312 return getJType(GetJTypeFromShorty(shorty_jty));
313 }
314
315 ::llvm::Type* getJType(JType jty);
316
317 ::llvm::Type* getJVoidTy() {
318 return getVoidTy();
319 }
320
321 ::llvm::IntegerType* getJBooleanTy() {
322 return getInt8Ty();
323 }
324
325 ::llvm::IntegerType* getJByteTy() {
326 return getInt8Ty();
327 }
328
329 ::llvm::IntegerType* getJCharTy() {
330 return getInt16Ty();
331 }
332
333 ::llvm::IntegerType* getJShortTy() {
334 return getInt16Ty();
335 }
336
337 ::llvm::IntegerType* getJIntTy() {
338 return getInt32Ty();
339 }
340
341 ::llvm::IntegerType* getJLongTy() {
342 return getInt64Ty();
343 }
344
345 ::llvm::Type* getJFloatTy() {
346 return getFloatTy();
347 }
348
349 ::llvm::Type* getJDoubleTy() {
350 return getDoubleTy();
351 }
352
353 ::llvm::PointerType* getJObjectTy() {
354 return java_object_type_;
355 }
356
357 ::llvm::PointerType* getJMethodTy() {
358 return java_method_type_;
359 }
360
361 ::llvm::PointerType* getJThreadTy() {
362 return java_thread_type_;
363 }
364
365 ::llvm::Type* getArtFrameTy() {
366 return art_frame_type_;
367 }
368
369 ::llvm::PointerType* getJEnvTy() {
370 return jenv_type_;
371 }
372
373 ::llvm::Type* getJValueTy() {
374 // NOTE: JValue is an union type, which may contains boolean, byte, char,
375 // short, int, long, float, double, Object. However, LLVM itself does
376 // not support union type, so we have to return a type with biggest size,
377 // then bitcast it before we use it.
378 return getJLongTy();
379 }
380
381 ::llvm::StructType* getShadowFrameTy(uint32_t vreg_size);
382
383
384 //--------------------------------------------------------------------------
385 // Constant Value Helper Function
386 //--------------------------------------------------------------------------
387
388 ::llvm::ConstantInt* getJBoolean(bool is_true) {
389 return (is_true) ? getTrue() : getFalse();
390 }
391
392 ::llvm::ConstantInt* getJByte(int8_t i) {
393 return ::llvm::ConstantInt::getSigned(getJByteTy(), i);
394 }
395
396 ::llvm::ConstantInt* getJChar(int16_t i) {
397 return ::llvm::ConstantInt::getSigned(getJCharTy(), i);
398 }
399
400 ::llvm::ConstantInt* getJShort(int16_t i) {
401 return ::llvm::ConstantInt::getSigned(getJShortTy(), i);
402 }
403
404 ::llvm::ConstantInt* getJInt(int32_t i) {
405 return ::llvm::ConstantInt::getSigned(getJIntTy(), i);
406 }
407
408 ::llvm::ConstantInt* getJLong(int64_t i) {
409 return ::llvm::ConstantInt::getSigned(getJLongTy(), i);
410 }
411
412 ::llvm::Constant* getJFloat(float f) {
413 return ::llvm::ConstantFP::get(getJFloatTy(), f);
414 }
415
416 ::llvm::Constant* getJDouble(double d) {
417 return ::llvm::ConstantFP::get(getJDoubleTy(), d);
418 }
419
420 ::llvm::ConstantPointerNull* getJNull() {
421 return ::llvm::ConstantPointerNull::get(getJObjectTy());
422 }
423
424 ::llvm::Constant* getJZero(char shorty_jty) {
425 return getJZero(GetJTypeFromShorty(shorty_jty));
426 }
427
428 ::llvm::Constant* getJZero(JType jty) {
429 switch (jty) {
430 case kVoid:
431 LOG(FATAL) << "Zero is not a value of void type";
432 return NULL;
433
434 case kBoolean:
435 return getJBoolean(false);
436
437 case kByte:
438 return getJByte(0);
439
440 case kChar:
441 return getJChar(0);
442
443 case kShort:
444 return getJShort(0);
445
446 case kInt:
447 return getJInt(0);
448
449 case kLong:
450 return getJLong(0);
451
452 case kFloat:
453 return getJFloat(0.0f);
454
455 case kDouble:
456 return getJDouble(0.0);
457
458 case kObject:
459 return getJNull();
460
461 default:
462 LOG(FATAL) << "Unknown java type: " << jty;
463 return NULL;
464 }
465 }
466
467
468 private:
469 ::llvm::Module* module_;
470
471 MDBuilder mdb_;
472
473 ::llvm::PointerType* java_object_type_;
474 ::llvm::PointerType* java_method_type_;
475 ::llvm::PointerType* java_thread_type_;
476
477 ::llvm::PointerType* jenv_type_;
478
479 ::llvm::StructType* art_frame_type_;
480
481 RuntimeSupportBuilder* runtime_support_;
482
483 IntrinsicHelper& intrinsic_helper_;
484};
485
486
487} // namespace llvm
488} // namespace art
489
490#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_