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 DoWhile 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 DoWhilePart {
  42         HEADER,
  43         BODY1,
  44         BODY2,
  45     }
  46     private final Loop loop;
  47     // header;                  [subblock]
  48     // do {
  49     //      body1;              [subblock with breaks]
  50     //      mutate(counter);
  51     //      body2;              [subblock with breaks]
  52     // } while(condition);
  53     private long thisLoopIterLimit = 0;
  54 
  55     public DoWhile(int level, Loop loop, long thisLoopIterLimit, Block header,
  56                    Block body1, Block body2) {
  57         super(body1.getResultType());
  58         this.level = level;
  59         this.loop = loop;
  60         this.thisLoopIterLimit = thisLoopIterLimit;
  61         addChild(header);
  62         addChild(body1);
  63         addChild(body2);
  64     }
  65 
  66     @Override
  67     public long complexity() {
  68         IRNode header = getChild(DoWhilePart.HEADER.ordinal());
  69         IRNode body1 = getChild(DoWhilePart.BODY1.ordinal());
  70         IRNode body2 = getChild(DoWhilePart.BODY2.ordinal());
  71         return loop.initialization.complexity()
  72                 + header.complexity()
  73                 + thisLoopIterLimit * (body1.complexity()
  74                 + loop.manipulator.complexity()
  75                 + body2.complexity()
  76                 + loop.condition.complexity());
  77     }
  78 
  79     @Override
  80     public long countDepth() {
  81         return Long.max(level, super.countDepth());
  82     }
  83 
  84     @Override
  85     public boolean removeSelf() {
  86         IRNode header = getChildren().get(DoWhilePart.HEADER.ordinal());
  87         List<IRNode> siblings = getParent().getChildren();
  88         int index = siblings.indexOf(this);
  89         siblings.set(index++, loop.initialization);
  90         if (header instanceof Block) {
  91             siblings.addAll(index, header.getChildren());
  92         } else {
  93             siblings.add(index, header);
  94         }
  95         return true;
  96     }
  97 }