src/share/vm/shark/sharkBuilder.cpp

Print this page
rev 3850 : [mq]: shark.patch


  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 Value* SharkBuilder::memset() {

 362   // LLVM 2.8 added a fifth isVolatile field for memset
 363   // introduced with LLVM r100304
 364   return make_function("llvm.memset.p0i8.i32", "Cciii", "v");



 365 }
 366 
 367 Value* SharkBuilder::unimplemented() {
 368   return make_function((address) report_unimplemented, "Ci", "v");
 369 }
 370 
 371 Value* SharkBuilder::should_not_reach_here() {
 372   return make_function((address) report_should_not_reach_here, "Ci", "v");
 373 }
 374 
 375 Value* SharkBuilder::dump() {
 376   return make_function((address) SharkRuntime::dump, "Cx", "v");
 377 }
 378 
 379 // Public interface to low-level non-VM calls
 380 












 381 CallInst* SharkBuilder::CreateGetFrameAddress() {
 382   return CreateCall(frame_address(), LLVMValue::jint_constant(0));
 383 }
 384 











 385 CallInst* SharkBuilder::CreateMemset(Value* dst,
 386                                      Value* value,
 387                                      Value* len,
 388                                      Value* align) {

 389   return CreateCall5(memset(), dst, value, len, align,
 390                      LLVMValue::jint_constant(0));



 391 }
 392 
 393 CallInst* SharkBuilder::CreateUnimplemented(const char* file, int line) {
 394   return CreateCall2(
 395     unimplemented(),
 396     CreateIntToPtr(
 397       LLVMValue::intptr_constant((intptr_t) file),
 398       PointerType::getUnqual(SharkType::jbyte_type())),
 399     LLVMValue::jint_constant(line));
 400 }
 401 
 402 CallInst* SharkBuilder::CreateShouldNotReachHere(const char* file, int line) {
 403   return CreateCall2(
 404     should_not_reach_here(),
 405     CreateIntToPtr(
 406       LLVMValue::intptr_constant((intptr_t) file),
 407       PointerType::getUnqual(SharkType::jbyte_type())),
 408     LLVMValue::jint_constant(line));
 409 }
 410 
 411 #ifndef PRODUCT
 412 CallInst* SharkBuilder::CreateDump(Value* value) {
 413   const char *name;
 414   if (value->hasName())
 415     // XXX this leaks, but it's only debug code
 416     name = strdup(value->getName().str().c_str());
 417   else
 418     name = "unnamed_value";
 419 
 420   if (isa<PointerType>(value->getType()))
 421     value = CreatePtrToInt(value, SharkType::intptr_type());
 422   else if (value->getType()->

 423            isIntegerTy()



 424            )
 425     value = CreateIntCast(value, SharkType::intptr_type(), false);
 426   else
 427     Unimplemented();
 428 
 429   return CreateCall2(
 430     dump(),
 431     CreateIntToPtr(
 432       LLVMValue::intptr_constant((intptr_t) name),
 433       PointerType::getUnqual(SharkType::jbyte_type())),
 434     value);
 435 }
 436 #endif // PRODUCT
 437 
 438 // HotSpot memory barriers
 439 
 440 void SharkBuilder::CreateUpdateBarrierSet(BarrierSet* bs, Value* field) {
 441   if (bs->kind() != BarrierSet::CardTableModRef)
 442     Unimplemented();
 443 


 452           LLVMValue::intptr_constant(CardTableModRefBS::card_shift))),
 453       PointerType::getUnqual(SharkType::jbyte_type())));
 454 }
 455 
 456 // Helpers for accessing the code buffer
 457 
 458 Value* SharkBuilder::code_buffer_address(int offset) {
 459   return CreateAdd(
 460     code_buffer()->base_pc(),
 461     LLVMValue::intptr_constant(offset));
 462 }
 463 
 464 Value* SharkBuilder::CreateInlineOop(jobject object, const char* name) {
 465   return CreateLoad(
 466     CreateIntToPtr(
 467       code_buffer_address(code_buffer()->inline_oop(object)),
 468       PointerType::getUnqual(SharkType::oop_type())),
 469     name);
 470 }
 471 
 472 Value* SharkBuilder::CreateInlineMetadata(Metadata* metadata, llvm::PointerType* type, const char* name) {
 473   assert(metadata != NULL, "inlined metadata must not be NULL");
 474   assert(metadata->is_metadata(), "sanity check");
 475   return CreateLoad(
 476     CreateIntToPtr(
 477       code_buffer_address(code_buffer()->inline_Metadata(metadata)),
 478       PointerType::getUnqual(type)),
 479     name);
 480 }
 481 
 482 Value* SharkBuilder::CreateInlineData(void*       data,
 483                                       size_t      size,
 484                                       Type* type,
 485                                       const char* name) {
 486   return CreateIntToPtr(
 487     code_buffer_address(code_buffer()->inline_data(data, size)),
 488     type,
 489     name);
 490 }
 491 
 492 // Helpers for creating basic blocks.
 493 
 494 BasicBlock* SharkBuilder::GetBlockInsertionPoint() const {
 495   BasicBlock *cur = GetInsertBlock();
 496 
 497   // BasicBlock::Create takes an insertBefore argument, so
 498   // we need to find the block _after_ the current block
 499   Function::iterator iter = cur->getParent()->begin();
 500   Function::iterator end  = cur->getParent()->end();
 501   while (iter != end) {
 502     iter++;
 503     if (&*iter == cur) {
 504       iter++;
 505       break;
 506     }
 507   }
 508 
 509   if (iter == end)
 510     return NULL;
 511   else
 512     return iter;
 513 }
 514 
 515 BasicBlock* SharkBuilder::CreateBlock(BasicBlock* ip, const char* name) const {
 516   return BasicBlock::Create(
 517     SharkContext::current(), name, GetInsertBlock()->getParent(), ip);
 518 }
 519 
 520 LoadInst* SharkBuilder::CreateAtomicLoad(Value* ptr, unsigned align, AtomicOrdering ordering, SynchronizationScope synchScope, bool isVolatile, const char* name) {
 521   return Insert(new LoadInst(ptr, name, isVolatile, align, ordering, synchScope), name);
 522 }
 523 
 524 StoreInst* SharkBuilder::CreateAtomicStore(Value* val, Value* ptr, unsigned align, AtomicOrdering ordering, SynchronizationScope synchScope, bool isVolatile, const char* name) {
 525   return Insert(new StoreInst(val, ptr, isVolatile, align, ordering, synchScope), name);
 526 }