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 } | 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.TypeList; 33 import jdk.test.lib.jittester.loops.LoopingCondition; 34 import jdk.test.lib.jittester.types.TypeKlass; 35 import jdk.test.lib.jittester.utils.PseudoRandom; 36 37 class LoopingConditionFactory extends Factory<LoopingCondition> { 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 LoopingCondition produce() throws ProductionFailedException { 55 IRNode leftExpression = null; 56 IRNode rightExpression = null; 57 Factory<IRNode> exprFactory = new IRNodeBuilder() 58 .setResultType(TypeList.BOOLEAN) 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 BinaryOperator condition = new BinaryOperator(OperatorKind.LT, TypeList.BOOLEAN, counter, limiter); 79 condition = (rightExpression != null) ? new BinaryOperator(OperatorKind.AND, TypeList.BOOLEAN, condition, 80 rightExpression) : condition; 81 condition = (leftExpression != null) ? new BinaryOperator(OperatorKind.AND, TypeList.BOOLEAN, leftExpression, 82 condition) : condition; 83 return new LoopingCondition(condition); 84 } 85 } |