1 /*
   2  * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.lir.constopt;
  26 
  27 import java.util.ArrayDeque;
  28 import java.util.ArrayList;
  29 import java.util.BitSet;
  30 import java.util.Deque;
  31 import java.util.List;
  32 
  33 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  34 import org.graalvm.compiler.debug.DebugContext;
  35 import org.graalvm.compiler.debug.Indent;
  36 import org.graalvm.compiler.lir.constopt.ConstantTree.Flags;
  37 import org.graalvm.compiler.lir.constopt.ConstantTree.NodeCost;
  38 
  39 /**
  40  * Analyzes a {@link ConstantTree} and marks potential materialization positions.
  41  */
  42 public final class ConstantTreeAnalyzer {
  43     private final ConstantTree tree;
  44     private final BitSet visited;
  45 
  46     @SuppressWarnings("try")
  47     public static NodeCost analyze(DebugContext debug, ConstantTree tree, AbstractBlockBase<?> startBlock) {
  48         try (DebugContext.Scope s = debug.scope("ConstantTreeAnalyzer")) {
  49             ConstantTreeAnalyzer analyzer = new ConstantTreeAnalyzer(tree);
  50             analyzer.analyzeBlocks(debug, startBlock);
  51             return tree.getCost(startBlock);
  52         } catch (Throwable e) {
  53             throw debug.handle(e);
  54         }
  55     }
  56 
  57     private ConstantTreeAnalyzer(ConstantTree tree) {
  58         this.tree = tree;
  59         this.visited = new BitSet(tree.size());
  60     }
  61 
  62     /**
  63      * Queues all relevant blocks for {@linkplain #process processing}.
  64      *
  65      * This is a worklist-style algorithm because a (more elegant) recursive implementation may
  66      * cause {@linkplain StackOverflowError stack overflows} on larger graphs.
  67      *
  68      * @param startBlock The start block of the dominator subtree.
  69      */
  70     @SuppressWarnings("try")
  71     private void analyzeBlocks(DebugContext debug, AbstractBlockBase<?> startBlock) {
  72         Deque<AbstractBlockBase<?>> worklist = new ArrayDeque<>();
  73         worklist.offerLast(startBlock);
  74         while (!worklist.isEmpty()) {
  75             AbstractBlockBase<?> block = worklist.pollLast();
  76             try (Indent i = debug.logAndIndent(DebugContext.VERBOSE_LEVEL, "analyze: %s", block)) {
  77                 assert block != null : "worklist is empty!";
  78                 assert isMarked(block) : "Block not part of the dominator tree: " + block;
  79 
  80                 if (isLeafBlock(block)) {
  81                     debug.log(DebugContext.VERBOSE_LEVEL, "leaf block");
  82                     leafCost(block);
  83                     continue;
  84                 }
  85 
  86                 if (!visited.get(block.getId())) {
  87                     // if not yet visited (and not a leaf block) process all children first!
  88                     debug.log(DebugContext.VERBOSE_LEVEL, "not marked");
  89                     worklist.offerLast(block);
  90                     AbstractBlockBase<?> dominated = block.getFirstDominated();
  91                     while (dominated != null) {
  92                         filteredPush(debug, worklist, dominated);
  93                         dominated = dominated.getDominatedSibling();
  94                     }
  95                     visited.set(block.getId());
  96                 } else {
  97                     debug.log(DebugContext.VERBOSE_LEVEL, "marked");
  98                     // otherwise, process block
  99                     process(block);
 100                 }
 101             }
 102         }
 103     }
 104 
 105     /**
 106      * Calculates the cost of a {@code block}. It is assumed that all {@code children} have already
 107      * been {@linkplain #process processed}
 108      *
 109      * @param block The block to be processed.
 110      */
 111     private void process(AbstractBlockBase<?> block) {
 112         List<UseEntry> usages = new ArrayList<>();
 113         double bestCost = 0;
 114         int numMat = 0;
 115 
 116         // collect children costs
 117         AbstractBlockBase<?> child = block.getFirstDominated();
 118         while (child != null) {
 119             if (isMarked(child)) {
 120                 NodeCost childCost = tree.getCost(child);
 121                 assert childCost != null : "Child with null cost? block: " + child;
 122                 usages.addAll(childCost.getUsages());
 123                 numMat += childCost.getNumMaterializations();
 124                 bestCost += childCost.getBestCost();
 125             }
 126             child = child.getDominatedSibling();
 127         }
 128         assert numMat > 0 : "No materialization? " + numMat;
 129 
 130         // choose block
 131         List<UseEntry> usagesBlock = tree.getUsages(block);
 132         double probabilityBlock = block.getRelativeFrequency();
 133 
 134         if (!usagesBlock.isEmpty() || shouldMaterializerInCurrentBlock(probabilityBlock, bestCost, numMat)) {
 135             // mark current block as potential materialization position
 136             usages.addAll(usagesBlock);
 137             bestCost = probabilityBlock;
 138             numMat = 1;
 139             tree.set(Flags.CANDIDATE, block);
 140         } else {
 141             // stick with the current solution
 142         }
 143 
 144         NodeCost nodeCost = new NodeCost(bestCost, usages, numMat);
 145         tree.setCost(block, nodeCost);
 146     }
 147 
 148     /**
 149      * This is the cost function that decides whether a materialization should be inserted in the
 150      * current block.
 151      * <p>
 152      * Note that this function does not take into account if a materialization is required despite
 153      * the probabilities (e.g. there are usages in the current block).
 154      *
 155      * @param probabilityBlock Probability of the current block.
 156      * @param probabilityChildren Accumulated probability of the children.
 157      * @param numMat Number of materializations along the subtrees. We use {@code numMat - 1} to
 158      *            insert materializations as late as possible if the probabilities are the same.
 159      */
 160     private static boolean shouldMaterializerInCurrentBlock(double probabilityBlock, double probabilityChildren, int numMat) {
 161         return probabilityBlock * Math.pow(0.9, numMat - 1) < probabilityChildren;
 162     }
 163 
 164     private void filteredPush(DebugContext debug, Deque<AbstractBlockBase<?>> worklist, AbstractBlockBase<?> block) {
 165         if (isMarked(block)) {
 166             debug.log(DebugContext.VERBOSE_LEVEL, "adding %s to the worklist", block);
 167             worklist.offerLast(block);
 168         }
 169     }
 170 
 171     private void leafCost(AbstractBlockBase<?> block) {
 172         tree.set(Flags.CANDIDATE, block);
 173         tree.getOrInitCost(block);
 174     }
 175 
 176     private boolean isMarked(AbstractBlockBase<?> block) {
 177         return tree.isMarked(block);
 178     }
 179 
 180     private boolean isLeafBlock(AbstractBlockBase<?> block) {
 181         return tree.isLeafBlock(block);
 182     }
 183 
 184 }