1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2019, Red Hat Inc. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 
26 package org.graalvm.compiler.hotspot.gc.shared;
27 
28 import org.graalvm.compiler.debug.GraalError;
29 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
30 import org.graalvm.compiler.nodes.StructuredGraph;
31 import org.graalvm.compiler.nodes.ValueNode;
32 import org.graalvm.compiler.nodes.extended.ArrayRangeWrite;
33 import org.graalvm.compiler.nodes.java.AbstractCompareAndSwapNode;
34 import org.graalvm.compiler.nodes.java.LoweredAtomicReadAndWriteNode;
35 import org.graalvm.compiler.nodes.memory.FixedAccessNode;
36 import org.graalvm.compiler.nodes.memory.HeapAccess;
37 import org.graalvm.compiler.nodes.memory.ReadNode;
38 import org.graalvm.compiler.nodes.memory.WriteNode;
39 import org.graalvm.compiler.nodes.memory.address.AddressNode;
40 import org.graalvm.compiler.nodes.type.StampTool;
41 
42 public class CardTableBarrierSet extends BarrierSet {
43 
CardTableBarrierSet(GraalHotSpotVMConfig vmConfig)44     public CardTableBarrierSet(GraalHotSpotVMConfig vmConfig) {
45         super(vmConfig);
46     }
47 
48     @Override
addReadNodeBarriers(ReadNode node, StructuredGraph graph)49     public void addReadNodeBarriers(ReadNode node, StructuredGraph graph) {
50         // Nothing to do here.
51     }
52 
53     @Override
addWriteNodeBarriers(WriteNode node, StructuredGraph graph)54     public void addWriteNodeBarriers(WriteNode node, StructuredGraph graph) {
55         HeapAccess.BarrierType barrierType = node.getBarrierType();
56         switch (barrierType) {
57             case NONE:
58                 // nothing to do
59                 break;
60             case FIELD:
61             case ARRAY:
62             case UNKNOWN:
63                 boolean precise = barrierType != HeapAccess.BarrierType.FIELD;
64                 boolean init = node.getLocationIdentity().isInit();
65                 if (!init || !getVMConfig().useDeferredInitBarriers) {
66                     addSerialPostWriteBarrier(node, node.getAddress(), node.value(), precise, graph);
67                 }
68                 break;
69             default:
70                 throw new GraalError("unexpected barrier type: " + barrierType);
71         }
72     }
73 
74     @Override
addAtomicReadWriteNodeBarriers(LoweredAtomicReadAndWriteNode node, StructuredGraph graph)75     public void addAtomicReadWriteNodeBarriers(LoweredAtomicReadAndWriteNode node, StructuredGraph graph) {
76         HeapAccess.BarrierType barrierType = node.getBarrierType();
77         switch (barrierType) {
78             case NONE:
79                 // nothing to do
80                 break;
81             case FIELD:
82             case ARRAY:
83             case UNKNOWN:
84                 boolean precise = barrierType != HeapAccess.BarrierType.FIELD;
85                 addSerialPostWriteBarrier(node, node.getAddress(), node.getNewValue(), precise, graph);
86                 break;
87             default:
88                 throw new GraalError("unexpected barrier type: " + barrierType);
89         }
90     }
91 
92     @Override
addCASBarriers(AbstractCompareAndSwapNode node, StructuredGraph graph)93     public void addCASBarriers(AbstractCompareAndSwapNode node, StructuredGraph graph) {
94         HeapAccess.BarrierType barrierType = node.getBarrierType();
95         switch (barrierType) {
96             case NONE:
97                 // nothing to do
98                 break;
99             case FIELD:
100             case ARRAY:
101             case UNKNOWN:
102                 boolean precise = barrierType != HeapAccess.BarrierType.FIELD;
103                 addSerialPostWriteBarrier(node, node.getAddress(), node.getNewValue(), precise, graph);
104                 break;
105             default:
106                 throw new GraalError("unexpected barrier type: " + barrierType);
107         }
108     }
109 
110     @Override
addArrayRangeBarriers(ArrayRangeWrite write, StructuredGraph graph)111     public void addArrayRangeBarriers(ArrayRangeWrite write, StructuredGraph graph) {
112         SerialArrayRangeWriteBarrier serialArrayRangeWriteBarrier = graph.add(new SerialArrayRangeWriteBarrier(write.getAddress(), write.getLength(), write.getElementStride()));
113         graph.addAfterFixed(write.asNode(), serialArrayRangeWriteBarrier);
114     }
115 
addSerialPostWriteBarrier(FixedAccessNode node, AddressNode address, ValueNode value, boolean precise, StructuredGraph graph)116     protected void addSerialPostWriteBarrier(FixedAccessNode node, AddressNode address, ValueNode value, boolean precise, StructuredGraph graph) {
117         final boolean alwaysNull = StampTool.isPointerAlwaysNull(value);
118         if (alwaysNull) {
119             // Serial barrier isn't needed for null value
120             return;
121         }
122         graph.addAfterFixed(node, graph.add(new SerialWriteBarrier(address, precise)));
123     }
124 }
125