001/*
002 * Copyright (c) 2013, 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.hotspot.phases;
024
025import jdk.internal.jvmci.common.*;
026import jdk.internal.jvmci.hotspot.*;
027
028import com.oracle.graal.graph.*;
029import com.oracle.graal.hotspot.nodes.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.extended.*;
032import com.oracle.graal.nodes.java.*;
033import com.oracle.graal.nodes.memory.*;
034import com.oracle.graal.nodes.memory.HeapAccess.BarrierType;
035import com.oracle.graal.nodes.memory.address.*;
036import com.oracle.graal.nodes.type.*;
037import com.oracle.graal.phases.*;
038
039public class WriteBarrierAdditionPhase extends Phase {
040
041    private HotSpotVMConfig config;
042
043    public WriteBarrierAdditionPhase(HotSpotVMConfig config) {
044        this.config = config;
045    }
046
047    @Override
048    protected void run(StructuredGraph graph) {
049        for (Node n : graph.getNodes()) {
050            if (n instanceof ReadNode) {
051                addReadNodeBarriers((ReadNode) n, graph);
052            } else if (n instanceof WriteNode) {
053                addWriteNodeBarriers((WriteNode) n, graph);
054            } else if (n instanceof LoweredAtomicReadAndWriteNode) {
055                LoweredAtomicReadAndWriteNode loweredAtomicReadAndWriteNode = (LoweredAtomicReadAndWriteNode) n;
056                addAtomicReadWriteNodeBarriers(loweredAtomicReadAndWriteNode, graph);
057            } else if (n instanceof LoweredCompareAndSwapNode) {
058                addCASBarriers((LoweredCompareAndSwapNode) n, graph);
059            } else if (n instanceof ArrayRangeWriteNode) {
060                ArrayRangeWriteNode node = (ArrayRangeWriteNode) n;
061                if (node.isObjectArray()) {
062                    addArrayRangeBarriers(node, graph);
063                }
064            }
065        }
066    }
067
068    private void addReadNodeBarriers(ReadNode node, StructuredGraph graph) {
069        if (node.getBarrierType() == BarrierType.PRECISE) {
070            assert config.useG1GC;
071            G1ReferentFieldReadBarrier barrier = graph.add(new G1ReferentFieldReadBarrier(node.getAddress(), node, false));
072            graph.addAfterFixed(node, barrier);
073        } else {
074            assert node.getBarrierType() == BarrierType.NONE : "Non precise read barrier has been attached to read node.";
075        }
076    }
077
078    protected static void addG1PreWriteBarrier(FixedAccessNode node, AddressNode address, ValueNode value, boolean doLoad, boolean nullCheck, StructuredGraph graph) {
079        G1PreWriteBarrier preBarrier = graph.add(new G1PreWriteBarrier(address, value, doLoad, nullCheck));
080        preBarrier.setStateBefore(node.stateBefore());
081        node.setNullCheck(false);
082        node.setStateBefore(null);
083        graph.addBeforeFixed(node, preBarrier);
084    }
085
086    protected void addG1PostWriteBarrier(FixedAccessNode node, AddressNode address, ValueNode value, boolean precise, StructuredGraph graph) {
087        final boolean alwaysNull = StampTool.isPointerAlwaysNull(value);
088        graph.addAfterFixed(node, graph.add(new G1PostWriteBarrier(address, value, precise, alwaysNull)));
089    }
090
091    protected void addSerialPostWriteBarrier(FixedAccessNode node, AddressNode address, ValueNode value, boolean precise, StructuredGraph graph) {
092        final boolean alwaysNull = StampTool.isPointerAlwaysNull(value);
093        if (alwaysNull) {
094            // Serial barrier isn't needed for null value
095            return;
096        }
097        graph.addAfterFixed(node, graph.add(new SerialWriteBarrier(address, precise)));
098    }
099
100    private void addWriteNodeBarriers(WriteNode node, StructuredGraph graph) {
101        BarrierType barrierType = node.getBarrierType();
102        switch (barrierType) {
103            case NONE:
104                // nothing to do
105                break;
106            case IMPRECISE:
107            case PRECISE:
108                boolean precise = barrierType == BarrierType.PRECISE;
109                if (config.useG1GC) {
110                    if (!node.isInitialization()) {
111                        addG1PreWriteBarrier(node, node.getAddress(), null, true, node.getNullCheck(), graph);
112                    }
113                    addG1PostWriteBarrier(node, node.getAddress(), node.value(), precise, graph);
114                } else {
115                    addSerialPostWriteBarrier(node, node.getAddress(), node.value(), precise, graph);
116                }
117                break;
118            default:
119                throw new JVMCIError("unexpected barrier type: " + barrierType);
120        }
121    }
122
123    private void addAtomicReadWriteNodeBarriers(LoweredAtomicReadAndWriteNode node, StructuredGraph graph) {
124        BarrierType barrierType = node.getBarrierType();
125        switch (barrierType) {
126            case NONE:
127                // nothing to do
128                break;
129            case IMPRECISE:
130            case PRECISE:
131                boolean precise = barrierType == BarrierType.PRECISE;
132                if (config.useG1GC) {
133                    addG1PreWriteBarrier(node, node.getAddress(), null, true, node.getNullCheck(), graph);
134                    addG1PostWriteBarrier(node, node.getAddress(), node.getNewValue(), precise, graph);
135                } else {
136                    addSerialPostWriteBarrier(node, node.getAddress(), node.getNewValue(), precise, graph);
137                }
138                break;
139            default:
140                throw new JVMCIError("unexpected barrier type: " + barrierType);
141        }
142    }
143
144    private void addCASBarriers(LoweredCompareAndSwapNode node, StructuredGraph graph) {
145        BarrierType barrierType = node.getBarrierType();
146        switch (barrierType) {
147            case NONE:
148                // nothing to do
149                break;
150            case IMPRECISE:
151            case PRECISE:
152                boolean precise = barrierType == BarrierType.PRECISE;
153                if (config.useG1GC) {
154                    addG1PreWriteBarrier(node, node.getAddress(), node.getExpectedValue(), false, false, graph);
155                    addG1PostWriteBarrier(node, node.getAddress(), node.getNewValue(), precise, graph);
156                } else {
157                    addSerialPostWriteBarrier(node, node.getAddress(), node.getNewValue(), precise, graph);
158                }
159                break;
160            default:
161                throw new JVMCIError("unexpected barrier type: " + barrierType);
162        }
163    }
164
165    private void addArrayRangeBarriers(ArrayRangeWriteNode node, StructuredGraph graph) {
166        if (config.useG1GC) {
167            if (!node.isInitialization()) {
168                G1ArrayRangePreWriteBarrier g1ArrayRangePreWriteBarrier = graph.add(new G1ArrayRangePreWriteBarrier(node.getArray(), node.getIndex(), node.getLength()));
169                graph.addBeforeFixed(node, g1ArrayRangePreWriteBarrier);
170            }
171            G1ArrayRangePostWriteBarrier g1ArrayRangePostWriteBarrier = graph.add(new G1ArrayRangePostWriteBarrier(node.getArray(), node.getIndex(), node.getLength()));
172            graph.addAfterFixed(node, g1ArrayRangePostWriteBarrier);
173        } else {
174            SerialArrayRangeWriteBarrier serialArrayRangeWriteBarrier = graph.add(new SerialArrayRangeWriteBarrier(node.getArray(), node.getIndex(), node.getLength()));
175            graph.addAfterFixed(node, serialArrayRangeWriteBarrier);
176        }
177    }
178}