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