001/*
002 * Copyright (c) 2011, 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.loop.phases;
024
025import com.oracle.graal.loop.*;
026import com.oracle.graal.nodes.*;
027import com.oracle.graal.nodes.cfg.*;
028import com.oracle.graal.nodes.extended.*;
029import com.oracle.graal.phases.*;
030import com.oracle.graal.phases.tiers.*;
031
032public class LoopSafepointEliminationPhase extends BasePhase<MidTierContext> {
033
034    @Override
035    protected void run(StructuredGraph graph, MidTierContext context) {
036        LoopsData loops = new LoopsData(graph);
037        if (context.getOptimisticOptimizations().useLoopLimitChecks()) {
038            loops.detectedCountedLoops();
039            for (LoopEx loop : loops.countedLoops()) {
040                if (loop.loop().getChildren().isEmpty() && loop.counted().getStamp().getBits() <= 32) {
041                    boolean hasSafepoint = false;
042                    for (LoopEndNode loopEnd : loop.loopBegin().loopEnds()) {
043                        hasSafepoint |= loopEnd.canSafepoint();
044                    }
045                    if (hasSafepoint) {
046                        loop.counted().createOverFlowGuard();
047                        for (LoopEndNode loopEnd : loop.loopBegin().loopEnds()) {
048                            loopEnd.disableSafepoint();
049                        }
050                    }
051                }
052            }
053        }
054        for (LoopEx loop : loops.countedLoops()) {
055            for (LoopEndNode loopEnd : loop.loopBegin().loopEnds()) {
056                Block b = loops.getCFG().blockFor(loopEnd);
057                blocks: while (b != loop.loop().getHeader()) {
058                    assert b != null;
059                    for (FixedNode node : b.getNodes()) {
060                        if (node instanceof Invoke || node instanceof ForeignCallNode) {
061                            loopEnd.disableSafepoint();
062                            break blocks;
063                        }
064                    }
065                    b = b.getDominator();
066                }
067            }
068        }
069        loops.deleteUnusedNodes();
070    }
071}