2011-07-25 20:08:58 +00:00
|
|
|
//===- BranchProbability.h - Branch Probability Wrapper ---------*- C++ -*-===//
|
2011-06-11 01:05:22 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Definition of BranchProbability shared by IR and Machine Instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_BRANCHPROBABILITY_H
|
|
|
|
#define LLVM_SUPPORT_BRANCHPROBABILITY_H
|
|
|
|
|
|
|
|
#include "llvm/Support/DataTypes.h"
|
2011-10-23 11:19:14 +00:00
|
|
|
#include <cassert>
|
2011-06-11 01:05:22 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2011-06-23 21:45:20 +00:00
|
|
|
class raw_ostream;
|
2011-06-11 01:05:22 +00:00
|
|
|
|
|
|
|
// This class represents Branch Probability as a non-negative fraction.
|
|
|
|
class BranchProbability {
|
|
|
|
// Numerator
|
|
|
|
uint32_t N;
|
|
|
|
|
|
|
|
// Denominator
|
|
|
|
uint32_t D;
|
|
|
|
|
|
|
|
public:
|
2011-10-23 11:19:14 +00:00
|
|
|
BranchProbability(uint32_t n, uint32_t d) : N(n), D(d) {
|
|
|
|
assert(d > 0 && "Denomiator cannot be 0!");
|
|
|
|
assert(n <= d && "Probability cannot be bigger than 1!");
|
|
|
|
}
|
2011-06-23 21:45:20 +00:00
|
|
|
|
2011-10-23 20:10:34 +00:00
|
|
|
static BranchProbability getZero() { return BranchProbability(0, 1); }
|
|
|
|
static BranchProbability getOne() { return BranchProbability(1, 1); }
|
|
|
|
|
2011-06-23 21:45:20 +00:00
|
|
|
uint32_t getNumerator() const { return N; }
|
|
|
|
uint32_t getDenominator() const { return D; }
|
2011-07-25 20:08:58 +00:00
|
|
|
|
2011-07-10 02:12:39 +00:00
|
|
|
// Return (1 - Probability).
|
2011-11-20 11:22:03 +00:00
|
|
|
BranchProbability getCompl() const {
|
2011-07-10 02:12:39 +00:00
|
|
|
return BranchProbability(D - N, D);
|
|
|
|
}
|
2011-06-23 21:45:20 +00:00
|
|
|
|
2011-07-25 22:27:42 +00:00
|
|
|
void print(raw_ostream &OS) const;
|
2011-06-11 01:05:22 +00:00
|
|
|
|
|
|
|
void dump() const;
|
2011-10-23 11:19:14 +00:00
|
|
|
|
2011-10-24 13:50:56 +00:00
|
|
|
bool operator==(BranchProbability RHS) const {
|
|
|
|
return (uint64_t)N * RHS.D == (uint64_t)D * RHS.N;
|
|
|
|
}
|
|
|
|
bool operator!=(BranchProbability RHS) const {
|
|
|
|
return !(*this == RHS);
|
|
|
|
}
|
|
|
|
bool operator<(BranchProbability RHS) const {
|
|
|
|
return (uint64_t)N * RHS.D < (uint64_t)D * RHS.N;
|
|
|
|
}
|
|
|
|
bool operator>(BranchProbability RHS) const {
|
|
|
|
return RHS < *this;
|
|
|
|
}
|
|
|
|
bool operator<=(BranchProbability RHS) const {
|
|
|
|
return (uint64_t)N * RHS.D <= (uint64_t)D * RHS.N;
|
|
|
|
}
|
|
|
|
bool operator>=(BranchProbability RHS) const {
|
|
|
|
return RHS <= *this;
|
|
|
|
}
|
2011-06-11 01:05:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const BranchProbability &Prob);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|