예제 #1
0
    @SuppressWarnings("try")
    public void run(
        StructuredGraph graph, SchedulingStrategy selectedStrategy, boolean immutableGraph) {
      // assert GraphOrder.assertNonCyclicGraph(graph);
      cfg = ControlFlowGraph.compute(graph, true, true, true, false);

      NodeMap<Block> currentNodeMap = graph.createNodeMap();
      NodeBitMap visited = graph.createNodeBitMap();
      BlockMap<List<Node>> earliestBlockToNodesMap = new BlockMap<>(cfg);
      this.nodeToBlockMap = currentNodeMap;
      this.blockToNodesMap = earliestBlockToNodesMap;

      scheduleEarliestIterative(
          earliestBlockToNodesMap, currentNodeMap, visited, graph, immutableGraph);

      if (selectedStrategy != SchedulingStrategy.EARLIEST) {
        // For non-earliest schedules, we need to do a second pass.
        BlockMap<List<Node>> latestBlockToNodesMap = new BlockMap<>(cfg);
        for (Block b : cfg.getBlocks()) {
          latestBlockToNodesMap.put(b, new ArrayList<Node>());
        }

        BlockMap<ArrayList<FloatingReadNode>> watchListMap =
            calcLatestBlocks(
                selectedStrategy,
                currentNodeMap,
                earliestBlockToNodesMap,
                visited,
                latestBlockToNodesMap,
                immutableGraph);
        sortNodesLatestWithinBlock(
            cfg,
            earliestBlockToNodesMap,
            latestBlockToNodesMap,
            currentNodeMap,
            watchListMap,
            visited);

        assert verifySchedule(cfg, latestBlockToNodesMap, currentNodeMap);
        assert MemoryScheduleVerification.check(cfg.getStartBlock(), latestBlockToNodesMap);

        this.blockToNodesMap = latestBlockToNodesMap;

        cfg.setNodeToBlock(currentNodeMap);
      }

      graph.setLastSchedule(
          new ScheduleResult(this.cfg, this.nodeToBlockMap, this.blockToNodesMap));
    }
예제 #2
0
    private void scheduleEarliestIterative(
        BlockMap<List<Node>> blockToNodes,
        NodeMap<Block> nodeToBlock,
        NodeBitMap visited,
        StructuredGraph graph,
        boolean immutableGraph) {

      BitSet floatingReads = new BitSet(cfg.getBlocks().length);

      // Add begin nodes as the first entry and set the block for phi nodes.
      for (Block b : cfg.getBlocks()) {
        AbstractBeginNode beginNode = b.getBeginNode();
        ArrayList<Node> nodes = new ArrayList<>();
        nodeToBlock.set(beginNode, b);
        nodes.add(beginNode);
        blockToNodes.put(b, nodes);

        if (beginNode instanceof AbstractMergeNode) {
          AbstractMergeNode mergeNode = (AbstractMergeNode) beginNode;
          for (PhiNode phi : mergeNode.phis()) {
            nodeToBlock.set(phi, b);
          }
        } else if (beginNode instanceof LoopExitNode) {
          LoopExitNode loopExitNode = (LoopExitNode) beginNode;
          for (ProxyNode proxy : loopExitNode.proxies()) {
            nodeToBlock.set(proxy, b);
          }
        }
      }

      NodeStack stack = new NodeStack();

      // Start analysis with control flow ends.
      Block[] reversePostOrder = cfg.reversePostOrder();
      for (int j = reversePostOrder.length - 1; j >= 0; --j) {
        Block b = reversePostOrder[j];
        FixedNode endNode = b.getEndNode();
        if (isFixedEnd(endNode)) {
          stack.push(endNode);
          nodeToBlock.set(endNode, b);
        }
      }

      processStack(cfg, blockToNodes, nodeToBlock, visited, floatingReads, stack);

      // Visit back input edges of loop phis.
      boolean changed;
      boolean unmarkedPhi;
      do {
        changed = false;
        unmarkedPhi = false;
        for (LoopBeginNode loopBegin : graph.getNodes(LoopBeginNode.TYPE)) {
          for (PhiNode phi : loopBegin.phis()) {
            if (visited.isMarked(phi)) {
              for (int i = 0; i < loopBegin.getLoopEndCount(); ++i) {
                Node node = phi.valueAt(i + loopBegin.forwardEndCount());
                if (node != null && !visited.isMarked(node)) {
                  changed = true;
                  stack.push(node);
                  processStack(cfg, blockToNodes, nodeToBlock, visited, floatingReads, stack);
                }
              }
            } else {
              unmarkedPhi = true;
            }
          }
        }

        /*
         * the processing of one loop phi could have marked a previously checked loop phi,
         * therefore this needs to be iterative.
         */
      } while (unmarkedPhi && changed);

      // Check for dead nodes.
      if (!immutableGraph && visited.getCounter() < graph.getNodeCount()) {
        for (Node n : graph.getNodes()) {
          if (!visited.isMarked(n)) {
            n.clearInputs();
            n.markDeleted();
          }
        }
      }

      // Add end nodes as the last nodes in each block.
      for (Block b : cfg.getBlocks()) {
        FixedNode endNode = b.getEndNode();
        if (isFixedEnd(endNode)) {
          if (endNode != b.getBeginNode()) {
            addNode(blockToNodes, b, endNode);
          }
        }
      }

      if (!floatingReads.isEmpty()) {
        for (Block b : cfg.getBlocks()) {
          if (floatingReads.get(b.getId())) {
            resortEarliestWithinBlock(b, blockToNodes, nodeToBlock, visited);
          }
        }
      }

      assert MemoryScheduleVerification.check(cfg.getStartBlock(), blockToNodes);
    }