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     */
013    package org.jikesrvm.compilers.opt.ir.operand;
014    
015    
016    /**
017     *
018     * @see Operand
019     */
020    public 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      public BranchProfileOperand flip() {
066        takenProbability = 1f - takenProbability;
067        return this;
068      }
069    
070      @Override
071      public boolean similar(Operand op) {
072        return (op instanceof BranchProfileOperand) &&
073               (takenProbability == ((BranchProfileOperand) op).takenProbability);
074      }
075    
076      /**
077       * Returns the string representation of this operand.
078       *
079       * @return a string representation of this operand.
080       */
081      @Override
082      public String toString() {
083        return "Probability: " + takenProbability;
084      }
085    
086    }
087    
088    
089    
090