1 /*
   2  * Copyright (c) 2012, 2018, 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 
  24 
  25 package org.graalvm.compiler.loop;
  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.ValueNode;
  36 import org.graalvm.compiler.nodes.cfg.Block;
  37 
  38 public class LoopFragmentWhole extends LoopFragment {
  39 
  40     public LoopFragmentWhole(LoopEx loop) {
  41         super(loop);
  42     }
  43 
  44     public LoopFragmentWhole(LoopFragmentWhole original) {
  45         super(null, original);
  46     }
  47 
  48     @Override
  49     public LoopFragmentWhole duplicate() {
  50         LoopFragmentWhole loopFragmentWhole = new LoopFragmentWhole(this);
  51         loopFragmentWhole.reify();
  52         return loopFragmentWhole;
  53     }
  54 
  55     private void reify() {
  56         assert this.isDuplicate();
  57 
  58         patchNodes(null);
  59 
  60         mergeEarlyExits();
  61     }
  62 
  63     @Override
  64     public NodeBitMap nodes() {
  65         if (nodes == null) {
  66             Loop<Block> loop = loop().loop();
  67             nodes = LoopFragment.computeNodes(graph(), LoopFragment.toHirBlocks(loop.getBlocks()), LoopFragment.toHirBlocks(loop.getLoopExits()));
  68         }
  69         return nodes;
  70     }
  71 
  72     @Override
  73     protected ValueNode prim(ValueNode b) {
  74         return getDuplicatedNode(b);
  75     }
  76 
  77     @Override
  78     protected DuplicationReplacement getDuplicationReplacement() {
  79         final FixedNode entry = loop().entryPoint();
  80         final Graph graph = this.graph();
  81         return new DuplicationReplacement() {
  82 
  83             private EndNode endNode;
  84 
  85             @Override
  86             public Node replacement(Node o) {
  87                 if (o == entry) {
  88                     if (endNode == null) {
  89                         endNode = graph.add(new EndNode());
  90                     }
  91                     return endNode;
  92                 }
  93                 return o;
  94             }
  95         };
  96     }
  97 
  98     public FixedNode entryPoint() {
  99         if (isDuplicate()) {
 100             LoopBeginNode newLoopBegin = getDuplicatedNode(original().loop().loopBegin());
 101             return newLoopBegin.forwardEnd();
 102         }
 103         return loop().entryPoint();
 104     }
 105 
 106     @Override
 107     protected void beforeDuplication() {
 108         // nothing to do
 109     }
 110 
 111     @Override
 112     public void insertBefore(LoopEx loop) {
 113         // TODO Auto-generated method stub
 114 
 115     }
 116 }