001/*
002 * Copyright (c) 2009, 2015, 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.nodes.java;
024
025import java.util.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.graph.spi.*;
030import com.oracle.graal.nodeinfo.*;
031import com.oracle.graal.nodes.*;
032import com.oracle.graal.nodes.extended.*;
033import com.oracle.graal.nodes.memory.*;
034import com.oracle.graal.nodes.memory.address.*;
035import com.oracle.graal.nodes.spi.*;
036
037/**
038 * The {@code AbstractNewObjectNode} is the base class for the new instance and new array nodes.
039 */
040@NodeInfo
041public abstract class AbstractNewObjectNode extends DeoptimizingFixedWithNextNode implements Simplifiable, Lowerable {
042
043    public static final NodeClass<AbstractNewObjectNode> TYPE = NodeClass.create(AbstractNewObjectNode.class);
044    protected final boolean fillContents;
045
046    protected AbstractNewObjectNode(NodeClass<? extends AbstractNewObjectNode> c, Stamp stamp, boolean fillContents, FrameState stateBefore) {
047        super(c, stamp, stateBefore);
048        this.fillContents = fillContents;
049    }
050
051    /**
052     * @return <code>true</code> if the object's contents should be initialized to zero/null.
053     */
054    public boolean fillContents() {
055        return fillContents;
056    }
057
058    @Override
059    public void simplify(SimplifierTool tool) {
060        // poor man's escape analysis: check if the object can be trivially removed
061        for (Node usage : usages()) {
062            if (usage instanceof FixedValueAnchorNode) {
063                if (((FixedValueAnchorNode) usage).usages().isNotEmpty()) {
064                    return;
065                }
066            } else if (usage instanceof OffsetAddressNode) {
067                if (((OffsetAddressNode) usage).getBase() != this) {
068                    return;
069                }
070                for (Node access : usage.usages()) {
071                    if (access instanceof WriteNode) {
072                        if (access.usages().isNotEmpty()) {
073                            // we would need to fix up the memory graph if the write has usages
074                            return;
075                        }
076                    } else {
077                        return;
078                    }
079                }
080            } else {
081                return;
082            }
083        }
084        for (Node usage : usages().distinct().snapshot()) {
085            if (usage instanceof OffsetAddressNode) {
086                for (Node access : usage.usages().snapshot()) {
087                    removeUsage(tool, (FixedWithNextNode) access);
088                }
089            } else {
090                removeUsage(tool, (FixedWithNextNode) usage);
091            }
092        }
093        List<Node> snapshot = inputs().snapshot();
094        graph().removeFixed(this);
095        for (Node input : snapshot) {
096            tool.removeIfUnused(input);
097        }
098    }
099
100    private void removeUsage(SimplifierTool tool, FixedWithNextNode usage) {
101        List<Node> snapshot = usage.inputs().snapshot();
102        graph().removeFixed(usage);
103        for (Node input : snapshot) {
104            tool.removeIfUnused(input);
105        }
106    }
107
108    @Override
109    public void lower(LoweringTool tool) {
110        tool.getLowerer().lower(this, tool);
111    }
112
113    @Override
114    public boolean canDeoptimize() {
115        return true;
116    }
117}