001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.baseline;
014
015/**
016 * Profile data for a branch instruction.
017 */
018public final class ConditionalBranchProfile extends BranchProfile {
019  /** Probability of being taken */
020  private final float taken;
021  /** Backward branch */
022  private final boolean backwards;
023
024  /**
025   * @param bci the bytecode index of the source branch instruction
026   * @param taken the number of times the branch was taken
027   * @param notTaken the number of times the branch was not taken
028   * @param bw is this a backwards branch?
029   */
030  ConditionalBranchProfile(int bci, int taken, int notTaken, boolean bw) {
031    super(bci, countToFloat(taken) + countToFloat(notTaken));
032    this.taken = countToFloat(taken);
033    backwards = bw;
034  }
035
036  public float getTakenProbability() {
037    if (freq > 0) {
038      return taken / freq;
039    } else if (backwards) {
040      return 0.9f;
041    } else {
042      return 0.5f;
043    }
044  }
045
046  @Override
047  public String toString() {
048    String ans = bci + (backwards ? "\tbackbranch" : "\tforwbranch");
049    ans += " < " + (int) taken + ", " + (int) (freq - taken) + " > ";
050    if (freq > 0) {
051      ans += (100.0f * taken / freq) + "% taken";
052    } else {
053      ans += "Never Executed";
054    }
055    return ans;
056  }
057
058}