1 /*
   2  * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.loop;
  24 
  25 import java.util.Collections;
  26 
  27 import org.graalvm.compiler.core.common.cfg.Loop;
  28 import org.graalvm.compiler.graph.Graph;
  29 import org.graalvm.compiler.graph.Graph.DuplicationReplacement;
  30 import org.graalvm.compiler.graph.Node;
  31 import org.graalvm.compiler.graph.NodeBitMap;
  32 import org.graalvm.compiler.nodes.EndNode;
  33 import org.graalvm.compiler.nodes.FixedNode;
  34 import org.graalvm.compiler.nodes.LoopBeginNode;
  35 import org.graalvm.compiler.nodes.StructuredGraph.GuardsStage;
  36 import org.graalvm.compiler.nodes.ValueNode;
  37 import org.graalvm.compiler.nodes.cfg.Block;
  38 
  39 public class LoopFragmentWhole extends LoopFragment {
  40 
  41     public LoopFragmentWhole(LoopEx loop) {
  42         super(loop);
  43     }
  44 
  45     public LoopFragmentWhole(LoopFragmentWhole original) {
  46         super(null, original);
  47     }
  48 
  49     @Override
  50     public LoopFragmentWhole duplicate() {
  51         LoopFragmentWhole loopFragmentWhole = new LoopFragmentWhole(this);
  52         loopFragmentWhole.reify();
  53         return loopFragmentWhole;
  54     }
  55 
  56     private void reify() {
  57         assert this.isDuplicate();
  58 
  59         patchNodes(null);
  60 
  61         mergeEarlyExits();
  62     }
  63 
  64     @Override
  65     public NodeBitMap nodes() {
  66         if (nodes == null) {
  67             Loop<Block> loop = loop().loop();
  68             if (loop.getHeader().getBeginNode().graph().getGuardsStage() == GuardsStage.AFTER_FSA) {
  69                 nodes = LoopFragment.computeNodes(graph(), LoopFragment.toHirBlocks(loop.getBlocks()), Collections.emptyList());
  70             } else {
  71                 nodes = LoopFragment.computeNodes(graph(), LoopFragment.toHirBlocks(loop.getBlocks()), LoopFragment.toHirExits(loop.getExits()));
  72             }
  73         }
  74         return nodes;
  75     }
  76 
  77     @Override
  78     protected ValueNode prim(ValueNode b) {
  79         return getDuplicatedNode(b);
  80     }
  81 
  82     @Override
  83     protected DuplicationReplacement getDuplicationReplacement() {
  84         final FixedNode entry = loop().entryPoint();
  85         final Graph graph = this.graph();
  86         return new DuplicationReplacement() {
  87 
  88             private EndNode endNode;
  89 
  90             @Override
  91             public Node replacement(Node o) {
  92                 if (o == entry) {
  93                     if (endNode == null) {
  94                         endNode = graph.add(new EndNode());
  95                     }
  96                     return endNode;
  97                 }
  98                 return o;
  99             }
 100         };
 101     }
 102 
 103     public FixedNode entryPoint() {
 104         if (isDuplicate()) {
 105             LoopBeginNode newLoopBegin = getDuplicatedNode(original().loop().loopBegin());
 106             return newLoopBegin.forwardEnd();
 107         }
 108         return loop().entryPoint();
 109     }
 110 
 111     @Override
 112     protected void finishDuplication() {
 113         // TODO (gd) ?
 114     }
 115 
 116     @Override
 117     public void insertBefore(LoopEx loop) {
 118         // TODO Auto-generated method stub
 119 
 120     }
 121 }