1 /*
   2  * Copyright (c) 2005, 2015, 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 package jdk.test.lib.jittester.loops;
  25 
  26 import java.util.List;
  27 import jdk.test.lib.jittester.Block;
  28 import jdk.test.lib.jittester.IRNode;
  29 import jdk.test.lib.jittester.visitors.Visitor;
  30 
  31 public class For extends IRNode {
  32 
  33     @Override
  34     public<T> T accept(Visitor<T> v) {
  35         return v.visit(this);
  36     }
  37 
  38     public Loop getLoop() {
  39         return loop;
  40     }
  41     public enum ForPart {
  42         HEADER,
  43         STATEMENT1,
  44         STATEMENT2,
  45         BODY1,
  46         BODY2,
  47         BODY3,
  48     };
  49 
  50     private final Loop loop;
  51     // header;                       // [subblock]
  52     // statement1, statement2;       // for (statement; condition; statement) {
  53     // body1;                        //    [subblock with breaks]
  54     //    mutate(x);
  55     // body2;                        //    [subblock with breaks and continues]
  56     // body3;                        //    [subblock with breaks]
  57     // }
  58     private long thisLoopIterLimit = 0;
  59     public For(int level, Loop loop, long thisLoopIterLimit,
  60             IRNode header, IRNode statement1,
  61             IRNode statement2, IRNode body1, IRNode body2, IRNode body3) {
  62         this.level = level;
  63         this.loop = loop;
  64         this.thisLoopIterLimit = thisLoopIterLimit;
  65         resizeUpChildren(ForPart.values().length);
  66         getChildren().set(ForPart.HEADER.ordinal(), header);
  67         getChildren().set(ForPart.STATEMENT1.ordinal(), statement1);
  68         getChildren().set(ForPart.STATEMENT2.ordinal(), statement2);
  69         getChildren().set(ForPart.BODY1.ordinal(), body1);
  70         getChildren().set(ForPart.BODY2.ordinal(), body2);
  71         getChildren().set(ForPart.BODY3.ordinal(), body3);
  72     }
  73 
  74     @Override
  75     public long complexity() {
  76         IRNode header = getChild(ForPart.HEADER.ordinal());
  77         IRNode statement1 = getChild(ForPart.STATEMENT1.ordinal());
  78         IRNode statement2 = getChild(ForPart.STATEMENT2.ordinal());
  79         IRNode body1 = getChild(ForPart.BODY1.ordinal());
  80         IRNode body2 = getChild(ForPart.BODY2.ordinal());
  81         IRNode body3 = getChild(ForPart.BODY3.ordinal());
  82         return loop.initialization.complexity()
  83                 + header.complexity()
  84                 + statement1.complexity()
  85                 + thisLoopIterLimit * (loop.condition.complexity()
  86                 + statement2.complexity()
  87                 + body1.complexity()
  88                 + loop.manipulator.complexity()
  89                 + body2.complexity()
  90                 + body3.complexity());
  91     }
  92 
  93     @Override
  94     public long countDepth() {
  95         return Long.max(level, super.countDepth());
  96     }
  97 
  98     @Override
  99     public boolean removeSelf() {
 100         IRNode header = getChildren().get(ForPart.HEADER.ordinal());
 101         List<IRNode> siblings = getParent().getChildren();
 102         int index = siblings.indexOf(this);
 103         if (header instanceof Block) {
 104             siblings.remove(this);
 105             siblings.addAll(index, header.getChildren());
 106         } else {
 107             siblings.set(index, header);
 108         }
 109         siblings.add(index + header.getChildren().size(), loop.initialization);
 110         return true;
 111     }
 112 }