1 /*
   2  * Copyright (c) 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.factories;
  25 
  26 import jdk.test.lib.jittester.BinaryOperator;
  27 import jdk.test.lib.jittester.IRNode;
  28 import jdk.test.lib.jittester.Literal;
  29 import jdk.test.lib.jittester.LocalVariable;
  30 import jdk.test.lib.jittester.OperatorKind;
  31 import jdk.test.lib.jittester.ProductionFailedException;
  32 import jdk.test.lib.jittester.loops.LoopingCondition;
  33 import jdk.test.lib.jittester.types.TypeKlass;
  34 import jdk.test.lib.jittester.types.TypeBoolean;
  35 import jdk.test.lib.jittester.utils.PseudoRandom;
  36 
  37 class LoopingConditionFactory extends Factory {
  38     private final LocalVariable counter;
  39     private final Literal limiter;
  40     private final int operatorLimit;
  41     private final long complexityLimit;
  42     private final TypeKlass ownerClass;
  43 
  44     LoopingConditionFactory(long complexityLimit, int operatorLimit, TypeKlass ownerClass,
  45             LocalVariable counter, Literal limiter) {
  46         this.complexityLimit = complexityLimit;
  47         this.operatorLimit = operatorLimit;
  48         this.counter = counter;
  49         this.limiter = limiter;
  50         this.ownerClass = ownerClass;
  51     }
  52 
  53     @Override
  54     public IRNode produce() throws ProductionFailedException {
  55         IRNode leftExpression = null;
  56         IRNode rightExpression = null;
  57         LimitedExpressionFactory exprFactory = new IRNodeBuilder()
  58                 .setResultType(new TypeBoolean())
  59                 .setComplexityLimit((complexityLimit - 1) / 2)
  60                 .setOperatorLimit((operatorLimit - 1) / 2)
  61                 .setOwnerKlass(ownerClass)
  62                 .setExceptionSafe(false)
  63                 .setNoConsts(false)
  64                 .getLimitedExpressionFactory();
  65         if (PseudoRandom.randomBoolean()) {
  66             leftExpression = exprFactory.produce();
  67         }
  68         if (PseudoRandom.randomBoolean()) {
  69             rightExpression = exprFactory.produce();
  70         }
  71         // Depending on loop counter direction, we should synthesize limiting condition.
  72         // Example: If the counter is counting forward. Then the looping condition can be:
  73         // counter < n, counter <= n, n > counter, n >= counter, n - counter > 0, etc..
  74 
  75         // Just as a temporary solution we'll assume that the counter is monotonically increasing.
  76         // And use counter < n condition to limit the loop.
  77         // In future we may introduce other equivalent relations as well.
  78         IRNode condition = new BinaryOperator(OperatorKind.LT, counter, limiter);
  79         condition = (rightExpression != null) ? new BinaryOperator(OperatorKind.AND, condition,
  80                 rightExpression) : condition;
  81         condition = (leftExpression != null) ? new BinaryOperator(OperatorKind.AND, leftExpression,
  82                 condition) : condition;
  83         return new LoopingCondition(condition);
  84     }
  85 }