001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.truffle.test.nodes;
024
025import com.oracle.truffle.api.*;
026import com.oracle.truffle.api.frame.*;
027import com.oracle.truffle.api.nodes.*;
028
029@NodeInfo
030public class AssumptionCutsBranchTestNode extends AbstractTestNode {
031
032    private final Assumption assumption;
033    private int counter;
034    @Child private Node childNode;
035
036    public AssumptionCutsBranchTestNode(Assumption assumption) {
037        this.assumption = assumption;
038        this.counter = 0;
039    }
040
041    @Override
042    public int execute(VirtualFrame frame) {
043        int returnVal = 0;
044        if (!assumption.isValid()) {
045            // this branch should be cut off, thus the Assertion should not trigger
046            CompilerAsserts.neverPartOfCompilation("this branch should be cut off");
047
048            // execute some complicated but otherwise meaningless code
049            double sum = 0;
050
051            if (Math.random() < 0.5) {
052                int iSum = 0;
053                for (int i = 0; i < 100; i++) {
054                    if (Math.random() > 0.5) {
055                        sum += Math.cos(Math.random());
056                    } else {
057                        sum += Math.sin(Math.random());
058                    }
059                    iSum += i * 2;
060                }
061                AssumptionCutsBranchTestNode node2 = new AssumptionCutsBranchTestNode(assumption);
062                node2.adoptChildren();
063                childNode = node2.copy();
064
065                if (iSum % 2 != 0) {
066                    // this is never executed, but introduces a potential invalidation
067                    assumption.invalidate();
068                }
069            } else {
070                sum = Math.random();
071            }
072
073            return Math.round(sum) % 100 == 0 && childNode.toString().contains("a") ? 666 : 777;
074        } else if (counter % 2 == 0) {
075            returnVal++;
076        }
077        counter++;
078        return returnVal % 2 == 0 ? returnVal : 0; // will always return 0
079    }
080
081    public Node getChildNode() {
082        return childNode;
083    }
084}