30 #include "runtime/os.hpp"
31 #include "runtime/synchronizer.hpp"
32 #include "runtime/thread.hpp"
33 #include "shark/llvmHeaders.hpp"
34 #include "shark/llvmValue.hpp"
35 #include "shark/sharkBuilder.hpp"
36 #include "shark/sharkContext.hpp"
37 #include "shark/sharkRuntime.hpp"
38 #include "utilities/debug.hpp"
39
40 using namespace llvm;
41
42 SharkBuilder::SharkBuilder(SharkCodeBuffer* code_buffer)
43 : IRBuilder<>(SharkContext::current()),
44 _code_buffer(code_buffer) {
45 }
46
47 // Helpers for accessing structures
48 Value* SharkBuilder::CreateAddressOfStructEntry(Value* base,
49 ByteSize offset,
50 const Type* type,
51 const char* name) {
52 return CreateBitCast(CreateStructGEP(base, in_bytes(offset)), type, name);
53 }
54
55 LoadInst* SharkBuilder::CreateValueOfStructEntry(Value* base,
56 ByteSize offset,
57 const Type* type,
58 const char* name) {
59 return CreateLoad(
60 CreateAddressOfStructEntry(
61 base, offset, PointerType::getUnqual(type)),
62 name);
63 }
64
65 // Helpers for accessing arrays
66
67 LoadInst* SharkBuilder::CreateArrayLength(Value* arrayoop) {
68 return CreateValueOfStructEntry(
69 arrayoop, in_ByteSize(arrayOopDesc::length_offset_in_bytes()),
70 SharkType::jint_type(), "length");
71 }
72
73 Value* SharkBuilder::CreateArrayAddress(Value* arrayoop,
74 const Type* element_type,
75 int element_bytes,
76 ByteSize base_offset,
77 Value* index,
78 const char* name) {
79 Value* offset = CreateIntCast(index, SharkType::intptr_type(), false);
80 if (element_bytes != 1)
81 offset = CreateShl(
82 offset,
83 LLVMValue::intptr_constant(exact_log2(element_bytes)));
84 offset = CreateAdd(
85 LLVMValue::intptr_constant(in_bytes(base_offset)), offset);
86
87 return CreateIntToPtr(
88 CreateAdd(CreatePtrToInt(arrayoop, SharkType::intptr_type()), offset),
89 PointerType::getUnqual(element_type),
90 name);
91 }
92
93 Value* SharkBuilder::CreateArrayAddress(Value* arrayoop,
94 BasicType basic_type,
97 const char* name) {
98 return CreateArrayAddress(
99 arrayoop,
100 SharkType::to_arrayType(basic_type),
101 type2aelembytes(basic_type),
102 base_offset, index, name);
103 }
104
105 Value* SharkBuilder::CreateArrayAddress(Value* arrayoop,
106 BasicType basic_type,
107 Value* index,
108 const char* name) {
109 return CreateArrayAddress(
110 arrayoop, basic_type,
111 in_ByteSize(arrayOopDesc::base_offset_in_bytes(basic_type)),
112 index, name);
113 }
114
115 // Helpers for creating intrinsics and external functions.
116
117 const Type* SharkBuilder::make_type(char type, bool void_ok) {
118 switch (type) {
119 // Primitive types
120 case 'c':
121 return SharkType::jbyte_type();
122 case 'i':
123 return SharkType::jint_type();
124 case 'l':
125 return SharkType::jlong_type();
126 case 'x':
127 return SharkType::intptr_type();
128 case 'f':
129 return SharkType::jfloat_type();
130 case 'd':
131 return SharkType::jdouble_type();
132
133 // Pointers to primitive types
134 case 'C':
135 case 'I':
136 case 'L':
137 case 'X':
138 case 'F':
139 case 'D':
140 return PointerType::getUnqual(make_type(tolower(type), false));
141
142 // VM objects
143 case 'T':
144 return SharkType::thread_type();
145 case 'M':
146 return PointerType::getUnqual(SharkType::monitor_type());
147 case 'O':
148 return SharkType::oop_type();
149
150 // Miscellaneous
151 case 'v':
152 assert(void_ok, "should be");
153 return SharkType::void_type();
154 case '1':
155 return SharkType::bit_type();
156
157 default:
158 ShouldNotReachHere();
159 }
160 }
161
162 const FunctionType* SharkBuilder::make_ftype(const char* params,
163 const char* ret) {
164 std::vector<const Type*> param_types;
165 for (const char* c = params; *c; c++)
166 param_types.push_back(make_type(*c, false));
167
168 assert(strlen(ret) == 1, "should be");
169 const Type *return_type = make_type(*ret, true);
170
171 return FunctionType::get(return_type, param_types, false);
172 }
173
174 // Create an object representing an intrinsic or external function by
175 // referencing the symbol by name. This is the LLVM-style approach,
176 // but it cannot be used on functions within libjvm.so its symbols
177 // are not exported. Note that you cannot make this work simply by
178 // exporting the symbols, as some symbols have the same names as
179 // symbols in the standard libraries (eg, atan2, fabs) and would
180 // obscure them were they visible.
181 Value* SharkBuilder::make_function(const char* name,
182 const char* params,
183 const char* ret) {
184 return SharkContext::current().get_external(name, make_ftype(params, ret));
185 }
186
187 // Create an object representing an external function by inlining a
188 // function pointer in the code. This is not the LLVM way, but it's
189 // the only way to access functions in libjvm.so and functions like
257
258 // High-level non-VM calls
259
260 Value* SharkBuilder::f2i() {
261 return make_function((address) SharedRuntime::f2i, "f", "i");
262 }
263
264 Value* SharkBuilder::f2l() {
265 return make_function((address) SharedRuntime::f2l, "f", "l");
266 }
267
268 Value* SharkBuilder::d2i() {
269 return make_function((address) SharedRuntime::d2i, "d", "i");
270 }
271
272 Value* SharkBuilder::d2l() {
273 return make_function((address) SharedRuntime::d2l, "d", "l");
274 }
275
276 Value* SharkBuilder::is_subtype_of() {
277 return make_function((address) SharkRuntime::is_subtype_of, "OO", "c");
278 }
279
280 Value* SharkBuilder::current_time_millis() {
281 return make_function((address) os::javaTimeMillis, "", "l");
282 }
283
284 Value* SharkBuilder::sin() {
285 return make_function("llvm.sin.f64", "d", "d");
286 }
287
288 Value* SharkBuilder::cos() {
289 return make_function("llvm.cos.f64", "d", "d");
290 }
291
292 Value* SharkBuilder::tan() {
293 return make_function((address) ::tan, "d", "d");
294 }
295
296 Value* SharkBuilder::atan2() {
297 return make_function((address) ::atan2, "dd", "d");
335 Value* SharkBuilder::throw_StackOverflowError() {
336 return make_function((address) ZeroStack::handle_overflow, "T", "v");
337 }
338
339 Value* SharkBuilder::uncommon_trap() {
340 return make_function((address) SharkRuntime::uncommon_trap, "Ti", "i");
341 }
342
343 Value* SharkBuilder::deoptimized_entry_point() {
344 return make_function((address) CppInterpreter::main_loop, "iT", "v");
345 }
346
347 // Native-Java transition
348
349 Value* SharkBuilder::check_special_condition_for_native_trans() {
350 return make_function(
351 (address) JavaThread::check_special_condition_for_native_trans,
352 "T", "v");
353 }
354
355 // Low-level non-VM calls
356
357 // The ARM-specific code here is to work around unimplemented
358 // atomic exchange and memory barrier intrinsics in LLVM.
359 //
360 // Delegating to external functions for these would normally
361 // incur a speed penalty, but Linux on ARM is a special case
362 // in that atomic operations on that platform are handled by
363 // external functions anyway. It would be *preferable* for
364 // the calls to be hidden away in LLVM, but it's not hurting
365 // performance so having the calls here is acceptable.
366 //
367 // If you are building Shark on a platform without atomic
368 // exchange and/or memory barrier intrinsics then it is only
369 // acceptable to mimic this approach if your platform cannot
370 // perform these operations without delegating to a function.
371
372 #ifdef ARM
373 static jint zero_cmpxchg_int(volatile jint *ptr, jint oldval, jint newval) {
374 return Atomic::cmpxchg(newval, ptr, oldval);
375 }
376 #endif // ARM
377
378 Value* SharkBuilder::cmpxchg_int() {
379 return make_function(
380 #ifdef ARM
381 (address) zero_cmpxchg_int,
382 #else
383 "llvm.atomic.cmp.swap.i32.p0i32",
384 #endif // ARM
385 "Iii", "i");
386 }
387
388 #ifdef ARM
389 static intptr_t zero_cmpxchg_ptr(volatile intptr_t* ptr,
390 intptr_t oldval,
391 intptr_t newval) {
392 return Atomic::cmpxchg_ptr(newval, ptr, oldval);
393 }
394 #endif // ARM
395
396 Value* SharkBuilder::cmpxchg_ptr() {
397 return make_function(
398 #ifdef ARM
399 (address) zero_cmpxchg_ptr,
400 #else
401 "llvm.atomic.cmp.swap.i" LP64_ONLY("64") NOT_LP64("32") ".p0i" LP64_ONLY("64") NOT_LP64("32"),
402 #endif // ARM
403 "Xxx", "x");
404 }
405
406 Value* SharkBuilder::frame_address() {
407 return make_function("llvm.frameaddress", "i", "C");
408 }
409
410 Value* SharkBuilder::memory_barrier() {
411 return make_function(
412 #ifdef ARM
413 (address) 0xffff0fa0, // __kernel_dmb
414 #else
415 "llvm.memory.barrier",
416 #endif // ARM
417 "11111", "v");
418 }
419
420 Value* SharkBuilder::memset() {
421 #if SHARK_LLVM_VERSION >= 28
422 // LLVM 2.8 added a fifth isVolatile field for memset
423 // introduced with LLVM r100304
424 return make_function("llvm.memset.i32", "Cciii", "v");
425 #else
426 return make_function("llvm.memset.i32", "Ccii", "v");
427 #endif
428 }
429
430 Value* SharkBuilder::unimplemented() {
431 return make_function((address) report_unimplemented, "Ci", "v");
432 }
433
434 Value* SharkBuilder::should_not_reach_here() {
435 return make_function((address) report_should_not_reach_here, "Ci", "v");
436 }
437
438 Value* SharkBuilder::dump() {
439 return make_function((address) SharkRuntime::dump, "Cx", "v");
440 }
441
442 // Public interface to low-level non-VM calls
443
444 CallInst* SharkBuilder::CreateCmpxchgInt(Value* exchange_value,
445 Value* dst,
446 Value* compare_value) {
447 return CreateCall3(cmpxchg_int(), dst, compare_value, exchange_value);
448 }
449
450 CallInst* SharkBuilder::CreateCmpxchgPtr(Value* exchange_value,
451 Value* dst,
452 Value* compare_value) {
453 return CreateCall3(cmpxchg_ptr(), dst, compare_value, exchange_value);
454 }
455
456 CallInst* SharkBuilder::CreateGetFrameAddress() {
457 return CreateCall(frame_address(), LLVMValue::jint_constant(0));
458 }
459
460 CallInst *SharkBuilder::CreateMemoryBarrier(int flags) {
461 Value *args[] = {
462 LLVMValue::bit_constant((flags & BARRIER_LOADLOAD) ? 1 : 0),
463 LLVMValue::bit_constant((flags & BARRIER_LOADSTORE) ? 1 : 0),
464 LLVMValue::bit_constant((flags & BARRIER_STORELOAD) ? 1 : 0),
465 LLVMValue::bit_constant((flags & BARRIER_STORESTORE) ? 1 : 0),
466 LLVMValue::bit_constant(1)};
467
468 return CreateCall(memory_barrier(), args, args + 5);
469 }
470
471 CallInst* SharkBuilder::CreateMemset(Value* dst,
472 Value* value,
473 Value* len,
474 Value* align) {
475 #if SHARK_LLVM_VERSION >= 28
476 return CreateCall5(memset(), dst, value, len, align,
477 LLVMValue::jint_constant(0));
478 #else
479 return CreateCall4(memset(), dst, value, len, align);
480 #endif
481 }
482
483 CallInst* SharkBuilder::CreateUnimplemented(const char* file, int line) {
484 return CreateCall2(
485 unimplemented(),
486 CreateIntToPtr(
487 LLVMValue::intptr_constant((intptr_t) file),
488 PointerType::getUnqual(SharkType::jbyte_type())),
489 LLVMValue::jint_constant(line));
490 }
491
492 CallInst* SharkBuilder::CreateShouldNotReachHere(const char* file, int line) {
493 return CreateCall2(
494 should_not_reach_here(),
495 CreateIntToPtr(
496 LLVMValue::intptr_constant((intptr_t) file),
497 PointerType::getUnqual(SharkType::jbyte_type())),
498 LLVMValue::jint_constant(line));
499 }
500
501 #ifndef PRODUCT
502 CallInst* SharkBuilder::CreateDump(Value* value) {
503 const char *name;
504 if (value->hasName())
505 // XXX this leaks, but it's only debug code
506 name = strdup(value->getName().str().c_str());
507 else
508 name = "unnamed_value";
509
510 if (isa<PointerType>(value->getType()))
511 value = CreatePtrToInt(value, SharkType::intptr_type());
512 else if (value->getType()->
513 #if SHARK_LLVM_VERSION >= 27
514 isIntegerTy()
515 #else
516 isInteger()
517 #endif
518 )
519 value = CreateIntCast(value, SharkType::intptr_type(), false);
520 else
521 Unimplemented();
522
523 return CreateCall2(
524 dump(),
525 CreateIntToPtr(
526 LLVMValue::intptr_constant((intptr_t) name),
527 PointerType::getUnqual(SharkType::jbyte_type())),
528 value);
529 }
530 #endif // PRODUCT
531
532 // HotSpot memory barriers
533
534 void SharkBuilder::CreateUpdateBarrierSet(BarrierSet* bs, Value* field) {
535 if (bs->kind() != BarrierSet::CardTableModRef)
536 Unimplemented();
537
546 LLVMValue::intptr_constant(CardTableModRefBS::card_shift))),
547 PointerType::getUnqual(SharkType::jbyte_type())));
548 }
549
550 // Helpers for accessing the code buffer
551
552 Value* SharkBuilder::code_buffer_address(int offset) {
553 return CreateAdd(
554 code_buffer()->base_pc(),
555 LLVMValue::intptr_constant(offset));
556 }
557
558 Value* SharkBuilder::CreateInlineOop(jobject object, const char* name) {
559 return CreateLoad(
560 CreateIntToPtr(
561 code_buffer_address(code_buffer()->inline_oop(object)),
562 PointerType::getUnqual(SharkType::oop_type())),
563 name);
564 }
565
566 Value* SharkBuilder::CreateInlineData(void* data,
567 size_t size,
568 const Type* type,
569 const char* name) {
570 return CreateIntToPtr(
571 code_buffer_address(code_buffer()->inline_data(data, size)),
572 type,
573 name);
574 }
575
576 // Helpers for creating basic blocks.
577
578 BasicBlock* SharkBuilder::GetBlockInsertionPoint() const {
579 BasicBlock *cur = GetInsertBlock();
580
581 // BasicBlock::Create takes an insertBefore argument, so
582 // we need to find the block _after_ the current block
583 Function::iterator iter = cur->getParent()->begin();
584 Function::iterator end = cur->getParent()->end();
585 while (iter != end) {
586 iter++;
587 if (&*iter == cur) {
588 iter++;
589 break;
590 }
591 }
592
593 if (iter == end)
594 return NULL;
595 else
596 return iter;
597 }
598
599 BasicBlock* SharkBuilder::CreateBlock(BasicBlock* ip, const char* name) const {
600 return BasicBlock::Create(
601 SharkContext::current(), name, GetInsertBlock()->getParent(), ip);
602 }
|
30 #include "runtime/os.hpp"
31 #include "runtime/synchronizer.hpp"
32 #include "runtime/thread.hpp"
33 #include "shark/llvmHeaders.hpp"
34 #include "shark/llvmValue.hpp"
35 #include "shark/sharkBuilder.hpp"
36 #include "shark/sharkContext.hpp"
37 #include "shark/sharkRuntime.hpp"
38 #include "utilities/debug.hpp"
39
40 using namespace llvm;
41
42 SharkBuilder::SharkBuilder(SharkCodeBuffer* code_buffer)
43 : IRBuilder<>(SharkContext::current()),
44 _code_buffer(code_buffer) {
45 }
46
47 // Helpers for accessing structures
48 Value* SharkBuilder::CreateAddressOfStructEntry(Value* base,
49 ByteSize offset,
50 Type* type,
51 const char* name) {
52 return CreateBitCast(CreateStructGEP(base, in_bytes(offset)), type, name);
53 }
54
55 LoadInst* SharkBuilder::CreateValueOfStructEntry(Value* base,
56 ByteSize offset,
57 Type* type,
58 const char* name) {
59 return CreateLoad(
60 CreateAddressOfStructEntry(
61 base, offset, PointerType::getUnqual(type)),
62 name);
63 }
64
65 // Helpers for accessing arrays
66
67 LoadInst* SharkBuilder::CreateArrayLength(Value* arrayoop) {
68 return CreateValueOfStructEntry(
69 arrayoop, in_ByteSize(arrayOopDesc::length_offset_in_bytes()),
70 SharkType::jint_type(), "length");
71 }
72
73 Value* SharkBuilder::CreateArrayAddress(Value* arrayoop,
74 Type* element_type,
75 int element_bytes,
76 ByteSize base_offset,
77 Value* index,
78 const char* name) {
79 Value* offset = CreateIntCast(index, SharkType::intptr_type(), false);
80 if (element_bytes != 1)
81 offset = CreateShl(
82 offset,
83 LLVMValue::intptr_constant(exact_log2(element_bytes)));
84 offset = CreateAdd(
85 LLVMValue::intptr_constant(in_bytes(base_offset)), offset);
86
87 return CreateIntToPtr(
88 CreateAdd(CreatePtrToInt(arrayoop, SharkType::intptr_type()), offset),
89 PointerType::getUnqual(element_type),
90 name);
91 }
92
93 Value* SharkBuilder::CreateArrayAddress(Value* arrayoop,
94 BasicType basic_type,
97 const char* name) {
98 return CreateArrayAddress(
99 arrayoop,
100 SharkType::to_arrayType(basic_type),
101 type2aelembytes(basic_type),
102 base_offset, index, name);
103 }
104
105 Value* SharkBuilder::CreateArrayAddress(Value* arrayoop,
106 BasicType basic_type,
107 Value* index,
108 const char* name) {
109 return CreateArrayAddress(
110 arrayoop, basic_type,
111 in_ByteSize(arrayOopDesc::base_offset_in_bytes(basic_type)),
112 index, name);
113 }
114
115 // Helpers for creating intrinsics and external functions.
116
117 Type* SharkBuilder::make_type(char type, bool void_ok) {
118 switch (type) {
119 // Primitive types
120 case 'c':
121 return SharkType::jbyte_type();
122 case 'i':
123 return SharkType::jint_type();
124 case 'l':
125 return SharkType::jlong_type();
126 case 'x':
127 return SharkType::intptr_type();
128 case 'f':
129 return SharkType::jfloat_type();
130 case 'd':
131 return SharkType::jdouble_type();
132
133 // Pointers to primitive types
134 case 'C':
135 case 'I':
136 case 'L':
137 case 'X':
138 case 'F':
139 case 'D':
140 return PointerType::getUnqual(make_type(tolower(type), false));
141
142 // VM objects
143 case 'T':
144 return SharkType::thread_type();
145 case 'M':
146 return PointerType::getUnqual(SharkType::monitor_type());
147 case 'O':
148 return SharkType::oop_type();
149 case 'K':
150 return SharkType::klass_type();
151
152 // Miscellaneous
153 case 'v':
154 assert(void_ok, "should be");
155 return SharkType::void_type();
156 case '1':
157 return SharkType::bit_type();
158
159 default:
160 ShouldNotReachHere();
161 }
162 }
163
164 FunctionType* SharkBuilder::make_ftype(const char* params,
165 const char* ret) {
166 std::vector<Type*> param_types;
167 for (const char* c = params; *c; c++)
168 param_types.push_back(make_type(*c, false));
169
170 assert(strlen(ret) == 1, "should be");
171 Type *return_type = make_type(*ret, true);
172
173 return FunctionType::get(return_type, param_types, false);
174 }
175
176 // Create an object representing an intrinsic or external function by
177 // referencing the symbol by name. This is the LLVM-style approach,
178 // but it cannot be used on functions within libjvm.so its symbols
179 // are not exported. Note that you cannot make this work simply by
180 // exporting the symbols, as some symbols have the same names as
181 // symbols in the standard libraries (eg, atan2, fabs) and would
182 // obscure them were they visible.
183 Value* SharkBuilder::make_function(const char* name,
184 const char* params,
185 const char* ret) {
186 return SharkContext::current().get_external(name, make_ftype(params, ret));
187 }
188
189 // Create an object representing an external function by inlining a
190 // function pointer in the code. This is not the LLVM way, but it's
191 // the only way to access functions in libjvm.so and functions like
259
260 // High-level non-VM calls
261
262 Value* SharkBuilder::f2i() {
263 return make_function((address) SharedRuntime::f2i, "f", "i");
264 }
265
266 Value* SharkBuilder::f2l() {
267 return make_function((address) SharedRuntime::f2l, "f", "l");
268 }
269
270 Value* SharkBuilder::d2i() {
271 return make_function((address) SharedRuntime::d2i, "d", "i");
272 }
273
274 Value* SharkBuilder::d2l() {
275 return make_function((address) SharedRuntime::d2l, "d", "l");
276 }
277
278 Value* SharkBuilder::is_subtype_of() {
279 return make_function((address) SharkRuntime::is_subtype_of, "KK", "c");
280 }
281
282 Value* SharkBuilder::current_time_millis() {
283 return make_function((address) os::javaTimeMillis, "", "l");
284 }
285
286 Value* SharkBuilder::sin() {
287 return make_function("llvm.sin.f64", "d", "d");
288 }
289
290 Value* SharkBuilder::cos() {
291 return make_function("llvm.cos.f64", "d", "d");
292 }
293
294 Value* SharkBuilder::tan() {
295 return make_function((address) ::tan, "d", "d");
296 }
297
298 Value* SharkBuilder::atan2() {
299 return make_function((address) ::atan2, "dd", "d");
337 Value* SharkBuilder::throw_StackOverflowError() {
338 return make_function((address) ZeroStack::handle_overflow, "T", "v");
339 }
340
341 Value* SharkBuilder::uncommon_trap() {
342 return make_function((address) SharkRuntime::uncommon_trap, "Ti", "i");
343 }
344
345 Value* SharkBuilder::deoptimized_entry_point() {
346 return make_function((address) CppInterpreter::main_loop, "iT", "v");
347 }
348
349 // Native-Java transition
350
351 Value* SharkBuilder::check_special_condition_for_native_trans() {
352 return make_function(
353 (address) JavaThread::check_special_condition_for_native_trans,
354 "T", "v");
355 }
356
357 Value* SharkBuilder::frame_address() {
358 return make_function("llvm.frameaddress", "i", "C");
359 }
360
361 #if SHARK_LLVM_VERSION <= 31
362 Value* SharkBuilder::memory_barrier() {
363 return make_function(
364 #ifdef ARM
365 (address) 0xffff0fa0, // __kernel_dmb
366 #else
367 "llvm.memory.barrier",
368 #endif // ARM
369 "11111", "v");
370 }
371 #endif
372
373 Value* SharkBuilder::memset() {
374 // LLVM 2.8 added a fifth isVolatile field for memset
375 // introduced with LLVM r100304
376 return make_function("llvm.memset.p0i8.i32", "Cciii", "v");
377 }
378
379 Value* SharkBuilder::unimplemented() {
380 return make_function((address) report_unimplemented, "Ci", "v");
381 }
382
383 Value* SharkBuilder::should_not_reach_here() {
384 return make_function((address) report_should_not_reach_here, "Ci", "v");
385 }
386
387 Value* SharkBuilder::dump() {
388 return make_function((address) SharkRuntime::dump, "Cx", "v");
389 }
390
391 // Public interface to low-level non-VM calls
392
393 CallInst* SharkBuilder::CreateGetFrameAddress() {
394 return CreateCall(frame_address(), LLVMValue::jint_constant(0));
395 }
396
397 #if SHARK_LLVM_VERSION <= 31
398 CallInst* SharkBuilder::CreateMemoryBarrier(int flags) {
399 Value *args[] = {
400 LLVMValue::bit_constant((flags & BARRIER_LOADLOAD) ? 1 : 0),
401 LLVMValue::bit_constant((flags & BARRIER_LOADSTORE) ? 1 : 0),
402 LLVMValue::bit_constant((flags & BARRIER_STORELOAD) ? 1 : 0),
403 LLVMValue::bit_constant((flags & BARRIER_STORESTORE) ? 1 : 0),
404 LLVMValue::bit_constant(1)};
405
406 return CreateCall(memory_barrier(), llvm::makeArrayRef(args, args + 5));
407 }
408 #endif
409
410 CallInst* SharkBuilder::CreateMemset(Value* dst,
411 Value* value,
412 Value* len,
413 Value* align) {
414 return CreateCall5(memset(), dst, value, len, align,
415 LLVMValue::jint_constant(0));
416 }
417
418 CallInst* SharkBuilder::CreateUnimplemented(const char* file, int line) {
419 return CreateCall2(
420 unimplemented(),
421 CreateIntToPtr(
422 LLVMValue::intptr_constant((intptr_t) file),
423 PointerType::getUnqual(SharkType::jbyte_type())),
424 LLVMValue::jint_constant(line));
425 }
426
427 CallInst* SharkBuilder::CreateShouldNotReachHere(const char* file, int line) {
428 return CreateCall2(
429 should_not_reach_here(),
430 CreateIntToPtr(
431 LLVMValue::intptr_constant((intptr_t) file),
432 PointerType::getUnqual(SharkType::jbyte_type())),
433 LLVMValue::jint_constant(line));
434 }
435
436 #ifndef PRODUCT
437 CallInst* SharkBuilder::CreateDump(Value* value) {
438 const char *name;
439 if (value->hasName())
440 // XXX this leaks, but it's only debug code
441 name = strdup(value->getName().str().c_str());
442 else
443 name = "unnamed_value";
444
445 if (isa<PointerType>(value->getType()))
446 value = CreatePtrToInt(value, SharkType::intptr_type());
447 else if (value->getType()->
448 isIntegerTy()
449 )
450 value = CreateIntCast(value, SharkType::intptr_type(), false);
451 else
452 Unimplemented();
453
454 return CreateCall2(
455 dump(),
456 CreateIntToPtr(
457 LLVMValue::intptr_constant((intptr_t) name),
458 PointerType::getUnqual(SharkType::jbyte_type())),
459 value);
460 }
461 #endif // PRODUCT
462
463 // HotSpot memory barriers
464
465 void SharkBuilder::CreateUpdateBarrierSet(BarrierSet* bs, Value* field) {
466 if (bs->kind() != BarrierSet::CardTableModRef)
467 Unimplemented();
468
477 LLVMValue::intptr_constant(CardTableModRefBS::card_shift))),
478 PointerType::getUnqual(SharkType::jbyte_type())));
479 }
480
481 // Helpers for accessing the code buffer
482
483 Value* SharkBuilder::code_buffer_address(int offset) {
484 return CreateAdd(
485 code_buffer()->base_pc(),
486 LLVMValue::intptr_constant(offset));
487 }
488
489 Value* SharkBuilder::CreateInlineOop(jobject object, const char* name) {
490 return CreateLoad(
491 CreateIntToPtr(
492 code_buffer_address(code_buffer()->inline_oop(object)),
493 PointerType::getUnqual(SharkType::oop_type())),
494 name);
495 }
496
497 Value* SharkBuilder::CreateInlineMetadata(Metadata* metadata, llvm::PointerType* type, const char* name) {
498 assert(metadata != NULL, "inlined metadata must not be NULL");
499 assert(metadata->is_metadata(), "sanity check");
500 return CreateLoad(
501 CreateIntToPtr(
502 code_buffer_address(code_buffer()->inline_Metadata(metadata)),
503 PointerType::getUnqual(type)),
504 name);
505 }
506
507 Value* SharkBuilder::CreateInlineData(void* data,
508 size_t size,
509 Type* type,
510 const char* name) {
511 return CreateIntToPtr(
512 code_buffer_address(code_buffer()->inline_data(data, size)),
513 type,
514 name);
515 }
516
517 // Helpers for creating basic blocks.
518
519 BasicBlock* SharkBuilder::GetBlockInsertionPoint() const {
520 BasicBlock *cur = GetInsertBlock();
521
522 // BasicBlock::Create takes an insertBefore argument, so
523 // we need to find the block _after_ the current block
524 Function::iterator iter = cur->getParent()->begin();
525 Function::iterator end = cur->getParent()->end();
526 while (iter != end) {
527 iter++;
528 if (&*iter == cur) {
529 iter++;
530 break;
531 }
532 }
533
534 if (iter == end)
535 return NULL;
536 else
537 return iter;
538 }
539
540 BasicBlock* SharkBuilder::CreateBlock(BasicBlock* ip, const char* name) const {
541 return BasicBlock::Create(
542 SharkContext::current(), name, GetInsertBlock()->getParent(), ip);
543 }
544
545 LoadInst* SharkBuilder::CreateAtomicLoad(Value* ptr, unsigned align, AtomicOrdering ordering, SynchronizationScope synchScope, bool isVolatile, const char* name) {
546 return Insert(new LoadInst(ptr, name, isVolatile, align, ordering, synchScope), name);
547 }
548
549 StoreInst* SharkBuilder::CreateAtomicStore(Value* val, Value* ptr, unsigned align, AtomicOrdering ordering, SynchronizationScope synchScope, bool isVolatile, const char* name) {
550 return Insert(new StoreInst(val, ptr, isVolatile, align, ordering, synchScope), name);
551 }
|