001/* 002 * Copyright (c) 2015, 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.phases.schedule; 024 025import java.util.*; 026 027import jdk.internal.jvmci.meta.*; 028 029import com.oracle.graal.compiler.common.*; 030import com.oracle.graal.compiler.common.cfg.*; 031import com.oracle.graal.graph.*; 032import com.oracle.graal.nodes.*; 033import com.oracle.graal.nodes.cfg.*; 034import com.oracle.graal.nodes.memory.*; 035import com.oracle.graal.phases.graph.*; 036import com.oracle.graal.phases.graph.ReentrantBlockIterator.BlockIteratorClosure; 037 038public final class MemoryScheduleVerification extends BlockIteratorClosure<Set<FloatingReadNode>> { 039 040 private final BlockMap<List<Node>> blockToNodesMap; 041 042 public static boolean check(Block startBlock, BlockMap<List<Node>> blockToNodesMap) { 043 ReentrantBlockIterator.apply(new MemoryScheduleVerification(blockToNodesMap), startBlock); 044 return true; 045 } 046 047 private MemoryScheduleVerification(BlockMap<List<Node>> blockToNodesMap) { 048 this.blockToNodesMap = blockToNodesMap; 049 } 050 051 @Override 052 protected Set<FloatingReadNode> getInitialState() { 053 return CollectionsFactory.newSet(); 054 } 055 056 @Override 057 protected Set<FloatingReadNode> processBlock(Block block, Set<FloatingReadNode> currentState) { 058 AbstractBeginNode beginNode = block.getBeginNode(); 059 if (beginNode instanceof AbstractMergeNode) { 060 AbstractMergeNode abstractMergeNode = (AbstractMergeNode) beginNode; 061 for (PhiNode phi : abstractMergeNode.phis()) { 062 if (phi instanceof MemoryPhiNode) { 063 MemoryPhiNode memoryPhiNode = (MemoryPhiNode) phi; 064 addFloatingReadUsages(currentState, memoryPhiNode); 065 } 066 } 067 } 068 for (Node n : blockToNodesMap.get(block)) { 069 if (n instanceof MemoryCheckpoint) { 070 if (n instanceof MemoryCheckpoint.Single) { 071 MemoryCheckpoint.Single single = (MemoryCheckpoint.Single) n; 072 processLocation(n, single.getLocationIdentity(), currentState); 073 } else if (n instanceof MemoryCheckpoint.Multi) { 074 MemoryCheckpoint.Multi multi = (MemoryCheckpoint.Multi) n; 075 for (LocationIdentity location : multi.getLocationIdentities()) { 076 processLocation(n, location, currentState); 077 } 078 } 079 080 addFloatingReadUsages(currentState, n); 081 } else if (n instanceof MemoryNode) { 082 addFloatingReadUsages(currentState, n); 083 } else if (n instanceof FloatingReadNode) { 084 FloatingReadNode floatingReadNode = (FloatingReadNode) n; 085 if (floatingReadNode.getLastLocationAccess() != null && floatingReadNode.getLocationIdentity().isMutable()) { 086 if (currentState.contains(floatingReadNode)) { 087 // Floating read was found in the state. 088 currentState.remove(floatingReadNode); 089 } else { 090 throw new RuntimeException("Floating read node " + n + " was not found in the state, i.e., it was killed by a memory check point before its place in the schedule. Block=" + 091 block + ", block begin: " + block.getBeginNode() + " block loop: " + block.getLoop() + ", " + blockToNodesMap.get(block).get(0)); 092 } 093 } 094 095 } 096 } 097 return currentState; 098 } 099 100 private static void addFloatingReadUsages(Set<FloatingReadNode> currentState, Node n) { 101 for (FloatingReadNode read : n.usages().filter(FloatingReadNode.class)) { 102 if (read.getLastLocationAccess() == n && read.getLocationIdentity().isMutable()) { 103 currentState.add(read); 104 } 105 } 106 } 107 108 private void processLocation(Node n, LocationIdentity location, Set<FloatingReadNode> currentState) { 109 assert n != null; 110 if (location.isImmutable()) { 111 return; 112 } 113 114 for (FloatingReadNode r : cloneState(currentState)) { 115 if (r.getLocationIdentity().overlaps(location)) { 116 // This read is killed by this location. 117 currentState.remove(r); 118 } 119 } 120 } 121 122 @Override 123 protected Set<FloatingReadNode> merge(Block merge, List<Set<FloatingReadNode>> states) { 124 Set<FloatingReadNode> result = states.get(0); 125 for (int i = 1; i < states.size(); ++i) { 126 result.retainAll(states.get(i)); 127 } 128 return result; 129 } 130 131 @Override 132 protected Set<FloatingReadNode> cloneState(Set<FloatingReadNode> oldState) { 133 Set<FloatingReadNode> result = CollectionsFactory.newSet(); 134 result.addAll(oldState); 135 return result; 136 } 137 138 @Override 139 protected List<Set<FloatingReadNode>> processLoop(Loop<Block> loop, Set<FloatingReadNode> initialState) { 140 HIRLoop l = (HIRLoop) loop; 141 for (MemoryPhiNode memoryPhi : ((LoopBeginNode) l.getHeader().getBeginNode()).phis().filter(MemoryPhiNode.class)) { 142 for (FloatingReadNode r : cloneState(initialState)) { 143 if (r.getLocationIdentity().overlaps(memoryPhi.getLocationIdentity())) { 144 initialState.remove(r); 145 } 146 } 147 } 148 return ReentrantBlockIterator.processLoop(this, loop, initialState).exitStates; 149 } 150}