1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 2009 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciTypeFlow.hpp"
  28 #include "memory/allocation.hpp"
  29 #include "shark/llvmHeaders.hpp"
  30 #include "shark/llvmValue.hpp"
  31 #include "shark/sharkBuilder.hpp"
  32 #include "shark/sharkEntry.hpp"
  33 #include "shark/sharkFunction.hpp"
  34 #include "shark/sharkState.hpp"
  35 #include "shark/sharkTopLevelBlock.hpp"
  36 #include "shark/shark_globals.hpp"
  37 #include "utilities/debug.hpp"
  38 
  39 using namespace llvm;
  40 
  41 void SharkFunction::initialize(const char *name) {
  42   // Create the function
  43   _function = Function::Create(
  44     entry_point_type(),
  45     GlobalVariable::InternalLinkage,
  46     name);
  47 
  48   // Get our arguments
  49   Function::arg_iterator ai = function()->arg_begin();
  50   Argument *method = ai++;
  51   method->setName("method");
  52   Argument *osr_buf = NULL;
  53   if (is_osr()) {
  54     osr_buf = ai++;
  55     osr_buf->setName("osr_buf");
  56   }
  57   Argument *base_pc = ai++;
  58   base_pc->setName("base_pc");
  59   code_buffer()->set_base_pc(base_pc);
  60   Argument *thread = ai++;
  61   thread->setName("thread");
  62   set_thread(thread);
  63 
  64   // Create the list of blocks
  65   set_block_insertion_point(NULL);
  66   _blocks = NEW_RESOURCE_ARRAY(SharkTopLevelBlock*, block_count());
  67   for (int i = 0; i < block_count(); i++) {
  68     ciTypeFlow::Block *b = flow()->pre_order_at(i);
  69 
  70     // Work around a bug in pre_order_at() that does not return
  71     // the correct pre-ordering.  If pre_order_at() were correct
  72     // this line could simply be:
  73     // _blocks[i] = new SharkTopLevelBlock(this, b);
  74     _blocks[b->pre_order()] = new SharkTopLevelBlock(this, b);
  75   }
  76 
  77   // Walk the tree from the start block to determine which
  78   // blocks are entered and which blocks require phis
  79   SharkTopLevelBlock *start_block = block(flow()->start_block_num());
  80   assert(start_block->start() == flow()->start_bci(), "blocks out of order");
  81   start_block->enter();
  82 
  83   // Initialize all entered blocks
  84   for (int i = 0; i < block_count(); i++) {
  85     if (block(i)->entered())
  86       block(i)->initialize();
  87   }
  88 
  89   // Create and push our stack frame
  90   set_block_insertion_point(&function()->front());
  91   builder()->SetInsertPoint(CreateBlock());
  92   _stack = SharkStack::CreateBuildAndPushFrame(this, method);
  93 
  94   // Create the entry state
  95   SharkState *entry_state;
  96   if (is_osr()) {
  97     entry_state = new SharkOSREntryState(start_block, method, osr_buf);
  98 
  99     // Free the OSR buffer
 100     builder()->CreateCall(builder()->osr_migration_end(), osr_buf);
 101   }
 102   else {
 103     entry_state = new SharkNormalEntryState(start_block, method);
 104 
 105     // Lock if necessary
 106     if (is_synchronized()) {
 107       SharkTopLevelBlock *locker =
 108         new SharkTopLevelBlock(this, start_block->ciblock());
 109       locker->add_incoming(entry_state);
 110 
 111       set_block_insertion_point(start_block->entry_block());
 112       locker->acquire_method_lock();
 113 
 114       entry_state = locker->current_state();
 115     }
 116   }
 117 
 118   // Transition into the method proper
 119   start_block->add_incoming(entry_state);
 120   builder()->CreateBr(start_block->entry_block());
 121 
 122   // Parse the blocks
 123   for (int i = 0; i < block_count(); i++) {
 124     if (!block(i)->entered())
 125       continue;
 126 
 127     if (i + 1 < block_count())
 128       set_block_insertion_point(block(i + 1)->entry_block());
 129     else
 130       set_block_insertion_point(NULL);
 131 
 132     block(i)->emit_IR();
 133   }
 134   do_deferred_zero_checks();
 135 }
 136 
 137 class DeferredZeroCheck : public SharkTargetInvariants {
 138  public:
 139   DeferredZeroCheck(SharkTopLevelBlock* block, SharkValue* value)
 140     : SharkTargetInvariants(block),
 141       _block(block),
 142       _value(value),
 143       _bci(block->bci()),
 144       _state(block->current_state()->copy()),
 145       _check_block(builder()->GetInsertBlock()),
 146       _continue_block(function()->CreateBlock("not_zero")) {
 147     builder()->SetInsertPoint(continue_block());
 148   }
 149 
 150  private:
 151   SharkTopLevelBlock* _block;
 152   SharkValue*         _value;
 153   int                 _bci;
 154   SharkState*         _state;
 155   BasicBlock*         _check_block;
 156   BasicBlock*         _continue_block;
 157 
 158  public:
 159   SharkTopLevelBlock* block() const {
 160     return _block;
 161   }
 162   SharkValue* value() const {
 163     return _value;
 164   }
 165   int bci() const {
 166     return _bci;
 167   }
 168   SharkState* state() const {
 169     return _state;
 170   }
 171   BasicBlock* check_block() const {
 172     return _check_block;
 173   }
 174   BasicBlock* continue_block() const {
 175     return _continue_block;
 176   }
 177 
 178  public:
 179   SharkFunction* function() const {
 180     return block()->function();
 181   }
 182 
 183  public:
 184   void process() const {
 185     builder()->SetInsertPoint(check_block());
 186     block()->do_deferred_zero_check(value(), bci(), state(), continue_block());
 187   }
 188 };
 189 
 190 void SharkFunction::add_deferred_zero_check(SharkTopLevelBlock* block,
 191                                             SharkValue*         value) {
 192   deferred_zero_checks()->append(new DeferredZeroCheck(block, value));
 193 }
 194 
 195 void SharkFunction::do_deferred_zero_checks() {
 196   for (int i = 0; i < deferred_zero_checks()->length(); i++)
 197     deferred_zero_checks()->at(i)->process();
 198 }