aboutsummaryrefslogtreecommitdiff
path: root/src/share/vm/shark/sharkBuilder.cpp
blob: f361276b89922e86e61154790c9fd96a85c29a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2008, 2009, 2010 Red Hat, Inc.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "ci/ciMethod.hpp"
#include "memory/resourceArea.hpp"
#include "oops/methodOop.hpp"
#include "runtime/os.hpp"
#include "runtime/synchronizer.hpp"
#include "runtime/thread.hpp"
#include "shark/llvmHeaders.hpp"
#include "shark/llvmValue.hpp"
#include "shark/sharkBuilder.hpp"
#include "shark/sharkContext.hpp"
#include "shark/sharkRuntime.hpp"
#include "utilities/debug.hpp"

using namespace llvm;

SharkBuilder::SharkBuilder(SharkCodeBuffer* code_buffer)
  : IRBuilder<>(SharkContext::current()),
    _code_buffer(code_buffer) {
}

// Helpers for accessing structures
Value* SharkBuilder::CreateAddressOfStructEntry(Value*      base,
                                                ByteSize    offset,
                                                const Type* type,
                                                const char* name) {
  return CreateBitCast(CreateStructGEP(base, in_bytes(offset)), type, name);
}

LoadInst* SharkBuilder::CreateValueOfStructEntry(Value*      base,
                                                 ByteSize    offset,
                                                 const Type* type,
                                                 const char* name) {
  return CreateLoad(
    CreateAddressOfStructEntry(
      base, offset, PointerType::getUnqual(type)),
    name);
}

// Helpers for accessing arrays

LoadInst* SharkBuilder::CreateArrayLength(Value* arrayoop) {
  return CreateValueOfStructEntry(
    arrayoop, in_ByteSize(arrayOopDesc::length_offset_in_bytes()),
    SharkType::jint_type(), "length");
}

Value* SharkBuilder::CreateArrayAddress(Value*      arrayoop,
                                        const Type* element_type,
                                        int         element_bytes,
                                        ByteSize    base_offset,
                                        Value*      index,
                                        const char* name) {
  Value* offset = CreateIntCast(index, SharkType::intptr_type(), false);
  if (element_bytes != 1)
    offset = CreateShl(
      offset,
      LLVMValue::intptr_constant(exact_log2(element_bytes)));
  offset = CreateAdd(
    LLVMValue::intptr_constant(in_bytes(base_offset)), offset);

  return CreateIntToPtr(
    CreateAdd(CreatePtrToInt(arrayoop, SharkType::intptr_type()), offset),
    PointerType::getUnqual(element_type),
    name);
}

Value* SharkBuilder::CreateArrayAddress(Value*      arrayoop,
                                        BasicType   basic_type,
                                        ByteSize    base_offset,
                                        Value*      index,
                                        const char* name) {
  return CreateArrayAddress(
    arrayoop,
    SharkType::to_arrayType(basic_type),
    type2aelembytes(basic_type),
    base_offset, index, name);
}

Value* SharkBuilder::CreateArrayAddress(Value*      arrayoop,
                                        BasicType   basic_type,
                                        Value*      index,
                                        const char* name) {
  return CreateArrayAddress(
    arrayoop, basic_type,
    in_ByteSize(arrayOopDesc::base_offset_in_bytes(basic_type)),
    index, name);
}

// Helpers for creating intrinsics and external functions.

const Type* SharkBuilder::make_type(char type, bool void_ok) {
  switch (type) {
    // Primitive types
  case 'c':
    return SharkType::jbyte_type();
  case 'i':
    return SharkType::jint_type();
  case 'l':
    return SharkType::jlong_type();
  case 'x':
    return SharkType::intptr_type();
  case 'f':
    return SharkType::jfloat_type();
  case 'd':
    return SharkType::jdouble_type();

    // Pointers to primitive types
  case 'C':
  case 'I':
  case 'L':
  case 'X':
  case 'F':
  case 'D':
    return PointerType::getUnqual(make_type(tolower(type), false));

    // VM objects
  case 'T':
    return SharkType::thread_type();
  case 'M':
    return PointerType::getUnqual(SharkType::monitor_type());
  case 'O':
    return SharkType::oop_type();

    // Miscellaneous
  case 'v':
    assert(void_ok, "should be");
    return SharkType::void_type();
  case '1':
    return SharkType::bit_type();

  default:
    ShouldNotReachHere();
  }
}

const FunctionType* SharkBuilder::make_ftype(const char* params,
                                             const char* ret) {
  std::vector<const Type*> param_types;
  for (const char* c = params; *c; c++)
    param_types.push_back(make_type(*c, false));

  assert(strlen(ret) == 1, "should be");
  const Type *return_type = make_type(*ret, true);

  return FunctionType::get(return_type, param_types, false);
}

// Create an object representing an intrinsic or external function by
// referencing the symbol by name.  This is the LLVM-style approach,
// but it cannot be used on functions within libjvm.so its symbols
// are not exported.  Note that you cannot make this work simply by
// exporting the symbols, as some symbols have the same names as
// symbols in the standard libraries (eg, atan2, fabs) and would
// obscure them were they visible.
Value* SharkBuilder::make_function(const char* name,
                                   const char* params,
                                   const char* ret) {
  return SharkContext::current().get_external(name, make_ftype(params, ret));
}

// Create an object representing an external function by inlining a
// function pointer in the code.  This is not the LLVM way, but it's
// the only way to access functions in libjvm.so and functions like
// __kernel_dmb on ARM which is accessed via an absolute address.
Value* SharkBuilder::make_function(address     func,
                                   const char* params,
                                   const char* ret) {
  return CreateIntToPtr(
    LLVMValue::intptr_constant((intptr_t) func),
    PointerType::getUnqual(make_ftype(params, ret)));
}

// VM calls

Value* SharkBuilder::find_exception_handler() {
  return make_function(
    (address) SharkRuntime::find_exception_handler, "TIi", "i");
}

Value* SharkBuilder::monitorenter() {
  return make_function((address) SharkRuntime::monitorenter, "TM", "v");
}

Value* SharkBuilder::monitorexit() {
  return make_function((address) SharkRuntime::monitorexit, "TM", "v");
}

Value* SharkBuilder::new_instance() {
  return make_function((address) SharkRuntime::new_instance, "Ti", "v");
}

Value* SharkBuilder::newarray() {
  return make_function((address) SharkRuntime::newarray, "Tii", "v");
}

Value* SharkBuilder::anewarray() {
  return make_function((address) SharkRuntime::anewarray, "Tii", "v");
}

Value* SharkBuilder::multianewarray() {
  return make_function((address) SharkRuntime::multianewarray, "TiiI", "v");
}

Value* SharkBuilder::register_finalizer() {
  return make_function((address) SharkRuntime::register_finalizer, "TO", "v");
}

Value* SharkBuilder::safepoint() {
  return make_function((address) SafepointSynchronize::block, "T", "v");
}

Value* SharkBuilder::throw_ArithmeticException() {
  return make_function(
    (address) SharkRuntime::throw_ArithmeticException, "TCi", "v");
}

Value* SharkBuilder::throw_ArrayIndexOutOfBoundsException() {
  return make_function(
    (address) SharkRuntime::throw_ArrayIndexOutOfBoundsException, "TCii", "v");
}

Value* SharkBuilder::throw_ClassCastException() {
  return make_function(
    (address) SharkRuntime::throw_ClassCastException, "TCi", "v");
}

Value* SharkBuilder::throw_NullPointerException() {
  return make_function(
    (address) SharkRuntime::throw_NullPointerException, "TCi", "v");
}

// High-level non-VM calls

Value* SharkBuilder::f2i() {
  return make_function((address) SharedRuntime::f2i, "f", "i");
}

Value* SharkBuilder::f2l() {
  return make_function((address) SharedRuntime::f2l, "f", "l");
}

Value* SharkBuilder::d2i() {
  return make_function((address) SharedRuntime::d2i, "d", "i");
}

Value* SharkBuilder::d2l() {
  return make_function((address) SharedRuntime::d2l, "d", "l");
}

Value* SharkBuilder::is_subtype_of() {
  return make_function((address) SharkRuntime::is_subtype_of, "OO", "c");
}

Value* SharkBuilder::current_time_millis() {
  return make_function((address) os::javaTimeMillis, "", "l");
}

Value* SharkBuilder::sin() {
  return make_function("llvm.sin.f64", "d", "d");
}

Value* SharkBuilder::cos() {
  return make_function("llvm.cos.f64", "d", "d");
}

Value* SharkBuilder::tan() {
  return make_function((address) ::tan, "d", "d");
}

Value* SharkBuilder::atan2() {
  return make_function((address) ::atan2, "dd", "d");
}

Value* SharkBuilder::sqrt() {
  return make_function("llvm.sqrt.f64", "d", "d");
}

Value* SharkBuilder::log() {
  return make_function("llvm.log.f64", "d", "d");
}

Value* SharkBuilder::log10() {
  return make_function("llvm.log10.f64", "d", "d");
}

Value* SharkBuilder::pow() {
  return make_function("llvm.pow.f64", "dd", "d");
}

Value* SharkBuilder::exp() {
  return make_function("llvm.exp.f64", "d", "d");
}

Value* SharkBuilder::fabs() {
  return make_function((address) ::fabs, "d", "d");
}

Value* SharkBuilder::unsafe_field_offset_to_byte_offset() {
  extern jlong Unsafe_field_offset_to_byte_offset(jlong field_offset);
  return make_function((address) Unsafe_field_offset_to_byte_offset, "l", "l");
}

Value* SharkBuilder::osr_migration_end() {
  return make_function((address) SharedRuntime::OSR_migration_end, "C", "v");
}

// Semi-VM calls

Value* SharkBuilder::throw_StackOverflowError() {
  return make_function((address) ZeroStack::handle_overflow, "T", "v");
}

Value* SharkBuilder::uncommon_trap() {
  return make_function((address) SharkRuntime::uncommon_trap, "Ti", "i");
}

Value* SharkBuilder::deoptimized_entry_point() {
  return make_function((address) CppInterpreter::main_loop, "iT", "v");
}

// Native-Java transition

Value* SharkBuilder::check_special_condition_for_native_trans() {
  return make_function(
    (address) JavaThread::check_special_condition_for_native_trans,
    "T", "v");
}

// Low-level non-VM calls

// The ARM-specific code here is to work around unimplemented
// atomic exchange and memory barrier intrinsics in LLVM.
//
// Delegating to external functions for these would normally
// incur a speed penalty, but Linux on ARM is a special case
// in that atomic operations on that platform are handled by
// external functions anyway.  It would be *preferable* for
// the calls to be hidden away in LLVM, but it's not hurting
// performance so having the calls here is acceptable.
//
// If you are building Shark on a platform without atomic
// exchange and/or memory barrier intrinsics then it is only
// acceptable to mimic this approach if your platform cannot
// perform these operations without delegating to a function.

#ifdef ARM
static jint zero_cmpxchg_int(volatile jint *ptr, jint oldval, jint newval) {
  return Atomic::cmpxchg(newval, ptr, oldval);
}
#endif // ARM

Value* SharkBuilder::cmpxchg_int() {
  return make_function(
#ifdef ARM
    (address) zero_cmpxchg_int,
#else
    "llvm.atomic.cmp.swap.i32.p0i32",
#endif // ARM
    "Iii", "i");
}

#ifdef ARM
static intptr_t zero_cmpxchg_ptr(volatile intptr_t* ptr,
                                 intptr_t           oldval,
                                 intptr_t           newval) {
  return Atomic::cmpxchg_ptr(newval, ptr, oldval);
}
#endif // ARM

Value* SharkBuilder::cmpxchg_ptr() {
  return make_function(
#ifdef ARM
    (address) zero_cmpxchg_ptr,
#else
    "llvm.atomic.cmp.swap.i" LP64_ONLY("64") NOT_LP64("32") ".p0i" LP64_ONLY("64") NOT_LP64("32"),
#endif // ARM
    "Xxx", "x");
}

Value* SharkBuilder::frame_address() {
  return make_function("llvm.frameaddress", "i", "C");
}

Value* SharkBuilder::memory_barrier() {
  return make_function(
#ifdef ARM
    (address) 0xffff0fa0, // __kernel_dmb
#else
    "llvm.memory.barrier",
#endif // ARM
    "11111", "v");
}

Value* SharkBuilder::memset() {
#if SHARK_LLVM_VERSION >= 28
  // LLVM 2.8 added a fifth isVolatile field for memset
  // introduced with LLVM r100304
  return make_function("llvm.memset.i32", "Cciii", "v");
#else
  return make_function("llvm.memset.i32", "Ccii", "v");
#endif
}

Value* SharkBuilder::unimplemented() {
  return make_function((address) report_unimplemented, "Ci", "v");
}

Value* SharkBuilder::should_not_reach_here() {
  return make_function((address) report_should_not_reach_here, "Ci", "v");
}

Value* SharkBuilder::dump() {
  return make_function((address) SharkRuntime::dump, "Cx", "v");
}

// Public interface to low-level non-VM calls

CallInst* SharkBuilder::CreateCmpxchgInt(Value* exchange_value,
                                         Value* dst,
                                         Value* compare_value) {
  return CreateCall3(cmpxchg_int(), dst, compare_value, exchange_value);
}

CallInst* SharkBuilder::CreateCmpxchgPtr(Value* exchange_value,
                                         Value* dst,
                                         Value* compare_value) {
  return CreateCall3(cmpxchg_ptr(), dst, compare_value, exchange_value);
}

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

CallInst *SharkBuilder::CreateMemoryBarrier(int flags) {
  Value *args[] = {
    LLVMValue::bit_constant((flags & BARRIER_LOADLOAD) ? 1 : 0),
    LLVMValue::bit_constant((flags & BARRIER_LOADSTORE) ? 1 : 0),
    LLVMValue::bit_constant((flags & BARRIER_STORELOAD) ? 1 : 0),
    LLVMValue::bit_constant((flags & BARRIER_STORESTORE) ? 1 : 0),
    LLVMValue::bit_constant(1)};

  return CreateCall(memory_barrier(), args, args + 5);
}

CallInst* SharkBuilder::CreateMemset(Value* dst,
                                     Value* value,
                                     Value* len,
                                     Value* align) {
#if SHARK_LLVM_VERSION >= 28
  return CreateCall5(memset(), dst, value, len, align,
                     LLVMValue::jint_constant(0));
#else
  return CreateCall4(memset(), dst, value, len, align);
#endif
}

CallInst* SharkBuilder::CreateUnimplemented(const char* file, int line) {
  return CreateCall2(
    unimplemented(),
    CreateIntToPtr(
      LLVMValue::intptr_constant((intptr_t) file),
      PointerType::getUnqual(SharkType::jbyte_type())),
    LLVMValue::jint_constant(line));
}

CallInst* SharkBuilder::CreateShouldNotReachHere(const char* file, int line) {
  return CreateCall2(
    should_not_reach_here(),
    CreateIntToPtr(
      LLVMValue::intptr_constant((intptr_t) file),
      PointerType::getUnqual(SharkType::jbyte_type())),
    LLVMValue::jint_constant(line));
}

#ifndef PRODUCT
CallInst* SharkBuilder::CreateDump(Value* value) {
  const char *name;
  if (value->hasName())
    // XXX this leaks, but it's only debug code
    name = strdup(value->getName().str().c_str());
  else
    name = "unnamed_value";

  if (isa<PointerType>(value->getType()))
    value = CreatePtrToInt(value, SharkType::intptr_type());
  else if (value->getType()->
#if SHARK_LLVM_VERSION >= 27
           isIntegerTy()
#else
           isInteger()
#endif
           )
    value = CreateIntCast(value, SharkType::intptr_type(), false);
  else
    Unimplemented();

  return CreateCall2(
    dump(),
    CreateIntToPtr(
      LLVMValue::intptr_constant((intptr_t) name),
      PointerType::getUnqual(SharkType::jbyte_type())),
    value);
}
#endif // PRODUCT

// HotSpot memory barriers

void SharkBuilder::CreateUpdateBarrierSet(BarrierSet* bs, Value* field) {
  if (bs->kind() != BarrierSet::CardTableModRef)
    Unimplemented();

  CreateStore(
    LLVMValue::jbyte_constant(CardTableModRefBS::dirty_card),
    CreateIntToPtr(
      CreateAdd(
        LLVMValue::intptr_constant(
          (intptr_t) ((CardTableModRefBS *) bs)->byte_map_base),
        CreateLShr(
          CreatePtrToInt(field, SharkType::intptr_type()),
          LLVMValue::intptr_constant(CardTableModRefBS::card_shift))),
      PointerType::getUnqual(SharkType::jbyte_type())));
}

// Helpers for accessing the code buffer

Value* SharkBuilder::code_buffer_address(int offset) {
  return CreateAdd(
    code_buffer()->base_pc(),
    LLVMValue::intptr_constant(offset));
}

Value* SharkBuilder::CreateInlineOop(jobject object, const char* name) {
  return CreateLoad(
    CreateIntToPtr(
      code_buffer_address(code_buffer()->inline_oop(object)),
      PointerType::getUnqual(SharkType::oop_type())),
    name);
}

Value* SharkBuilder::CreateInlineData(void*       data,
                                      size_t      size,
                                      const Type* type,
                                      const char* name) {
  return CreateIntToPtr(
    code_buffer_address(code_buffer()->inline_data(data, size)),
    type,
    name);
}

// Helpers for creating basic blocks.

BasicBlock* SharkBuilder::GetBlockInsertionPoint() const {
  BasicBlock *cur = GetInsertBlock();

  // BasicBlock::Create takes an insertBefore argument, so
  // we need to find the block _after_ the current block
  Function::iterator iter = cur->getParent()->begin();
  Function::iterator end  = cur->getParent()->end();
  while (iter != end) {
    iter++;
    if (&*iter == cur) {
      iter++;
      break;
    }
  }

  if (iter == end)
    return NULL;
  else
    return iter;
}

BasicBlock* SharkBuilder::CreateBlock(BasicBlock* ip, const char* name) const {
  return BasicBlock::Create(
    SharkContext::current(), name, GetInsertBlock()->getParent(), ip);
}