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.opt.ir.operand;
014
015
016/**
017 *
018 * @see Operand
019 */
020public final class BranchProfileOperand extends Operand {
021  public float takenProbability;
022
023  public static final float ALWAYS = 1f;
024  public static final float LIKELY = .99f;
025  public static final float UNLIKELY = 1f - LIKELY;
026  public static final float NEVER = 1f - ALWAYS;
027
028  public BranchProfileOperand(float takenProbability) {
029    this.takenProbability = takenProbability;
030  }
031
032  public BranchProfileOperand() {
033    this.takenProbability = 0.5f;
034  }
035
036  public static BranchProfileOperand always() {
037    return new BranchProfileOperand(ALWAYS);
038  }
039
040  public static BranchProfileOperand likely() {
041    return new BranchProfileOperand(LIKELY);
042  }
043
044  public static BranchProfileOperand unlikely() {
045    return new BranchProfileOperand(UNLIKELY);
046  }
047
048  public static BranchProfileOperand never() {
049    return new BranchProfileOperand(NEVER);
050  }
051
052  /**
053   * Returns a copy of this branch operand.
054   *
055   * @return a copy of this operand
056   */
057  @Override
058  public Operand copy() {
059    return new BranchProfileOperand(takenProbability);
060  }
061
062  /**
063   * Flip the probability (p = 1 - p)
064   *
065   * @return this
066   */
067  public BranchProfileOperand flip() {
068    takenProbability = 1f - takenProbability;
069    return this;
070  }
071
072  @Override
073  public boolean similar(Operand op) {
074    return (op instanceof BranchProfileOperand) &&
075           (takenProbability == ((BranchProfileOperand) op).takenProbability);
076  }
077
078  /**
079   * Returns the string representation of this operand.
080   *
081   * @return a string representation of this operand.
082   */
083  @Override
084  public String toString() {
085    return "Probability: " + takenProbability;
086  }
087
088}
089
090
091
092