2014-04-11 23:20:58 +00:00
|
|
|
//==- BlockFrequencyInfoImpl.h - Block Frequency Implementation -*- C++ -*-===//
|
2011-06-23 21:56:59 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-04-21 17:57:07 +00:00
|
|
|
// Shared implementation of BlockFrequency for IR and Machine Instructions.
|
2014-04-28 20:02:29 +00:00
|
|
|
// See the documentation below for BlockFrequencyInfoImpl for details.
|
2011-06-23 21:56:59 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-04-11 23:20:58 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H
|
|
|
|
#define LLVM_ANALYSIS_BLOCKFREQUENCYINFOIMPL_H
|
2011-06-23 21:56:59 +00:00
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2014-04-28 20:02:29 +00:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2014-04-25 04:38:09 +00:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2011-07-27 22:05:51 +00:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2011-06-23 21:56:59 +00:00
|
|
|
#include "llvm/Support/BranchProbability.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2011-07-16 20:23:20 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-06-23 21:56:59 +00:00
|
|
|
#include <string>
|
2012-12-03 17:02:12 +00:00
|
|
|
#include <vector>
|
2014-04-25 04:30:06 +00:00
|
|
|
#include <list>
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "block-freq"
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-04-21 18:31:58 +00:00
|
|
|
// UnsignedFloat definition.
|
2014-04-21 17:57:07 +00:00
|
|
|
//
|
|
|
|
// TODO: Make this private to BlockFrequencyInfoImpl or delete.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-06-23 21:56:59 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
class UnsignedFloatBase {
|
2014-04-21 17:57:07 +00:00
|
|
|
public:
|
|
|
|
static const int32_t MaxExponent = 16383;
|
|
|
|
static const int32_t MinExponent = -16382;
|
|
|
|
static const int DefaultPrecision = 10;
|
|
|
|
|
|
|
|
static void dump(uint64_t D, int16_t E, int Width);
|
|
|
|
static raw_ostream &print(raw_ostream &OS, uint64_t D, int16_t E, int Width,
|
|
|
|
unsigned Precision);
|
|
|
|
static std::string toString(uint64_t D, int16_t E, int Width,
|
|
|
|
unsigned Precision);
|
|
|
|
static int countLeadingZeros32(uint32_t N) { return countLeadingZeros(N); }
|
|
|
|
static int countLeadingZeros64(uint64_t N) { return countLeadingZeros(N); }
|
|
|
|
static uint64_t getHalf(uint64_t N) { return (N >> 1) + (N & 1); }
|
|
|
|
|
|
|
|
static std::pair<uint64_t, bool> splitSigned(int64_t N) {
|
|
|
|
if (N >= 0)
|
|
|
|
return std::make_pair(N, false);
|
|
|
|
uint64_t Unsigned = N == INT64_MIN ? UINT64_C(1) << 63 : uint64_t(-N);
|
|
|
|
return std::make_pair(Unsigned, true);
|
|
|
|
}
|
|
|
|
static int64_t joinSigned(uint64_t U, bool IsNeg) {
|
|
|
|
if (U > uint64_t(INT64_MAX))
|
|
|
|
return IsNeg ? INT64_MIN : INT64_MAX;
|
|
|
|
return IsNeg ? -int64_t(U) : int64_t(U);
|
|
|
|
}
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
static int32_t extractLg(const std::pair<int32_t, int> &Lg) {
|
|
|
|
return Lg.first;
|
|
|
|
}
|
|
|
|
static int32_t extractLgFloor(const std::pair<int32_t, int> &Lg) {
|
|
|
|
return Lg.first - (Lg.second > 0);
|
|
|
|
}
|
|
|
|
static int32_t extractLgCeiling(const std::pair<int32_t, int> &Lg) {
|
|
|
|
return Lg.first + (Lg.second < 0);
|
|
|
|
}
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
static std::pair<uint64_t, int16_t> divide64(uint64_t L, uint64_t R);
|
|
|
|
static std::pair<uint64_t, int16_t> multiply64(uint64_t L, uint64_t R);
|
|
|
|
|
|
|
|
static int compare(uint64_t L, uint64_t R, int Shift) {
|
|
|
|
assert(Shift >= 0);
|
|
|
|
assert(Shift < 64);
|
|
|
|
|
|
|
|
uint64_t L_adjusted = L >> Shift;
|
|
|
|
if (L_adjusted < R)
|
|
|
|
return -1;
|
|
|
|
if (L_adjusted > R)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return L > L_adjusted << Shift ? 1 : 0;
|
|
|
|
}
|
2014-04-11 23:21:02 +00:00
|
|
|
};
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
/// \brief Simple representation of an unsigned floating point.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// UnsignedFloat is a unsigned floating point number. It uses simple
|
2014-04-21 17:57:07 +00:00
|
|
|
/// saturation arithmetic, and every operation is well-defined for every value.
|
|
|
|
///
|
|
|
|
/// The number is split into a signed exponent and unsigned digits. The number
|
|
|
|
/// represented is \c getDigits()*2^getExponent(). In this way, the digits are
|
|
|
|
/// much like the mantissa in the x87 long double, but there is no canonical
|
|
|
|
/// form, so the same number can be represented by many bit representations
|
|
|
|
/// (it's always in "denormal" mode).
|
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// UnsignedFloat is templated on the underlying integer type for digits, which
|
2014-04-21 17:57:07 +00:00
|
|
|
/// is expected to be one of uint64_t, uint32_t, uint16_t or uint8_t.
|
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// Unlike builtin floating point types, UnsignedFloat is portable.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// Unlike APFloat, UnsignedFloat does not model architecture floating point
|
2014-04-21 17:57:07 +00:00
|
|
|
/// behaviour (this should make it a little faster), and implements most
|
|
|
|
/// operators (this makes it usable).
|
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// UnsignedFloat is totally ordered. However, there is no canonical form, so
|
2014-04-21 17:57:07 +00:00
|
|
|
/// there are multiple representations of most scalars. E.g.:
|
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// UnsignedFloat(8u, 0) == UnsignedFloat(4u, 1)
|
|
|
|
/// UnsignedFloat(4u, 1) == UnsignedFloat(2u, 2)
|
|
|
|
/// UnsignedFloat(2u, 2) == UnsignedFloat(1u, 3)
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// UnsignedFloat implements most arithmetic operations. Precision is kept
|
2014-04-21 17:57:07 +00:00
|
|
|
/// where possible. Uses simple saturation arithmetic, so that operations
|
|
|
|
/// saturate to 0.0 or getLargest() rather than under or overflowing. It has
|
|
|
|
/// some extra arithmetic for unit inversion. 0.0/0.0 is defined to be 0.0.
|
|
|
|
/// Any other division by 0.0 is defined to be getLargest().
|
|
|
|
///
|
|
|
|
/// As a convenience for modifying the exponent, left and right shifting are
|
|
|
|
/// both implemented, and both interpret negative shifts as positive shifts in
|
|
|
|
/// the opposite direction.
|
|
|
|
///
|
|
|
|
/// Exponents are limited to the range accepted by x87 long double. This makes
|
|
|
|
/// it trivial to add functionality to convert to APFloat (this is already
|
|
|
|
/// relied on for the implementation of printing).
|
2014-04-21 18:31:58 +00:00
|
|
|
///
|
|
|
|
/// The current plan is to gut this and make the necessary parts of it (even
|
|
|
|
/// more) private to BlockFrequencyInfo.
|
|
|
|
template <class DigitsT> class UnsignedFloat : UnsignedFloatBase {
|
2014-04-21 17:57:07 +00:00
|
|
|
public:
|
|
|
|
static_assert(!std::numeric_limits<DigitsT>::is_signed,
|
|
|
|
"only unsigned floats supported");
|
|
|
|
|
|
|
|
typedef DigitsT DigitsType;
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::numeric_limits<DigitsType> DigitsLimits;
|
|
|
|
|
|
|
|
static const int Width = sizeof(DigitsType) * 8;
|
|
|
|
static_assert(Width <= 64, "invalid integer width for digits");
|
|
|
|
|
|
|
|
private:
|
|
|
|
DigitsType Digits;
|
|
|
|
int16_t Exponent;
|
|
|
|
|
|
|
|
public:
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat() : Digits(0), Exponent(0) {}
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat(DigitsType Digits, int16_t Exponent)
|
2014-04-21 17:57:07 +00:00
|
|
|
: Digits(Digits), Exponent(Exponent) {}
|
|
|
|
|
|
|
|
private:
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat(const std::pair<uint64_t, int16_t> &X)
|
2014-04-21 17:57:07 +00:00
|
|
|
: Digits(X.first), Exponent(X.second) {}
|
|
|
|
|
|
|
|
public:
|
2014-04-21 18:31:58 +00:00
|
|
|
static UnsignedFloat getZero() { return UnsignedFloat(0, 0); }
|
|
|
|
static UnsignedFloat getOne() { return UnsignedFloat(1, 0); }
|
|
|
|
static UnsignedFloat getLargest() {
|
|
|
|
return UnsignedFloat(DigitsLimits::max(), MaxExponent);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2014-04-21 18:31:58 +00:00
|
|
|
static UnsignedFloat getFloat(uint64_t N) { return adjustToWidth(N, 0); }
|
|
|
|
static UnsignedFloat getInverseFloat(uint64_t N) {
|
2014-04-21 17:57:07 +00:00
|
|
|
return getFloat(N).invert();
|
|
|
|
}
|
2014-04-21 18:31:58 +00:00
|
|
|
static UnsignedFloat getFraction(DigitsType N, DigitsType D) {
|
2014-04-21 17:57:07 +00:00
|
|
|
return getQuotient(N, D);
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t getExponent() const { return Exponent; }
|
|
|
|
DigitsType getDigits() const { return Digits; }
|
|
|
|
|
|
|
|
/// \brief Convert to the given integer type.
|
|
|
|
///
|
|
|
|
/// Convert to \c IntT using simple saturating arithmetic, truncating if
|
|
|
|
/// necessary.
|
|
|
|
template <class IntT> IntT toInt() const;
|
|
|
|
|
|
|
|
bool isZero() const { return !Digits; }
|
|
|
|
bool isLargest() const { return *this == getLargest(); }
|
|
|
|
bool isOne() const {
|
|
|
|
if (Exponent > 0 || Exponent <= -Width)
|
|
|
|
return false;
|
|
|
|
return Digits == DigitsType(1) << -Exponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief The log base 2, rounded.
|
|
|
|
///
|
|
|
|
/// Get the lg of the scalar. lg 0 is defined to be INT32_MIN.
|
|
|
|
int32_t lg() const { return extractLg(lgImpl()); }
|
|
|
|
|
|
|
|
/// \brief The log base 2, rounded towards INT32_MIN.
|
|
|
|
///
|
|
|
|
/// Get the lg floor. lg 0 is defined to be INT32_MIN.
|
|
|
|
int32_t lgFloor() const { return extractLgFloor(lgImpl()); }
|
|
|
|
|
|
|
|
/// \brief The log base 2, rounded towards INT32_MAX.
|
|
|
|
///
|
|
|
|
/// Get the lg ceiling. lg 0 is defined to be INT32_MIN.
|
|
|
|
int32_t lgCeiling() const { return extractLgCeiling(lgImpl()); }
|
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
bool operator==(const UnsignedFloat &X) const { return compare(X) == 0; }
|
|
|
|
bool operator<(const UnsignedFloat &X) const { return compare(X) < 0; }
|
|
|
|
bool operator!=(const UnsignedFloat &X) const { return compare(X) != 0; }
|
|
|
|
bool operator>(const UnsignedFloat &X) const { return compare(X) > 0; }
|
|
|
|
bool operator<=(const UnsignedFloat &X) const { return compare(X) <= 0; }
|
|
|
|
bool operator>=(const UnsignedFloat &X) const { return compare(X) >= 0; }
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
bool operator!() const { return isZero(); }
|
|
|
|
|
|
|
|
/// \brief Convert to a decimal representation in a string.
|
|
|
|
///
|
|
|
|
/// Convert to a string. Uses scientific notation for very large/small
|
|
|
|
/// numbers. Scientific notation is used roughly for numbers outside of the
|
|
|
|
/// range 2^-64 through 2^64.
|
|
|
|
///
|
|
|
|
/// \c Precision indicates the number of decimal digits of precision to use;
|
|
|
|
/// 0 requests the maximum available.
|
|
|
|
///
|
|
|
|
/// As a special case to make debugging easier, if the number is small enough
|
|
|
|
/// to convert without scientific notation and has more than \c Precision
|
|
|
|
/// digits before the decimal place, it's printed accurately to the first
|
|
|
|
/// digit past zero. E.g., assuming 10 digits of precision:
|
|
|
|
///
|
|
|
|
/// 98765432198.7654... => 98765432198.8
|
|
|
|
/// 8765432198.7654... => 8765432198.8
|
|
|
|
/// 765432198.7654... => 765432198.8
|
|
|
|
/// 65432198.7654... => 65432198.77
|
|
|
|
/// 5432198.7654... => 5432198.765
|
|
|
|
std::string toString(unsigned Precision = DefaultPrecision) {
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloatBase::toString(Digits, Exponent, Width, Precision);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Print a decimal representation.
|
|
|
|
///
|
|
|
|
/// Print a string. See toString for documentation.
|
|
|
|
raw_ostream &print(raw_ostream &OS,
|
|
|
|
unsigned Precision = DefaultPrecision) const {
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloatBase::print(OS, Digits, Exponent, Width, Precision);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2014-04-21 18:31:58 +00:00
|
|
|
void dump() const { return UnsignedFloatBase::dump(Digits, Exponent, Width); }
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat &operator+=(const UnsignedFloat &X);
|
|
|
|
UnsignedFloat &operator-=(const UnsignedFloat &X);
|
|
|
|
UnsignedFloat &operator*=(const UnsignedFloat &X);
|
|
|
|
UnsignedFloat &operator/=(const UnsignedFloat &X);
|
|
|
|
UnsignedFloat &operator<<=(int16_t Shift) { shiftLeft(Shift); return *this; }
|
|
|
|
UnsignedFloat &operator>>=(int16_t Shift) { shiftRight(Shift); return *this; }
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void shiftLeft(int32_t Shift);
|
|
|
|
void shiftRight(int32_t Shift);
|
|
|
|
|
|
|
|
/// \brief Adjust two floats to have matching exponents.
|
|
|
|
///
|
|
|
|
/// Adjust \c this and \c X to have matching exponents. Returns the new \c X
|
|
|
|
/// by value. Does nothing if \a isZero() for either.
|
|
|
|
///
|
|
|
|
/// The value that compares smaller will lose precision, and possibly become
|
|
|
|
/// \a isZero().
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat matchExponents(UnsignedFloat X);
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
/// \brief Increase exponent to match another float.
|
|
|
|
///
|
|
|
|
/// Increases \c this to have an exponent matching \c X. May decrease the
|
|
|
|
/// exponent of \c X in the process, and \c this may possibly become \a
|
|
|
|
/// isZero().
|
2014-04-21 18:31:58 +00:00
|
|
|
void increaseExponentToMatch(UnsignedFloat &X, int32_t ExponentDiff);
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// \brief Scale a large number accurately.
|
|
|
|
///
|
|
|
|
/// Scale N (multiply it by this). Uses full precision multiplication, even
|
|
|
|
/// if Width is smaller than 64, so information is not lost.
|
|
|
|
uint64_t scale(uint64_t N) const;
|
|
|
|
uint64_t scaleByInverse(uint64_t N) const {
|
|
|
|
// TODO: implement directly, rather than relying on inverse. Inverse is
|
|
|
|
// expensive.
|
|
|
|
return inverse().scale(N);
|
|
|
|
}
|
|
|
|
int64_t scale(int64_t N) const {
|
|
|
|
std::pair<uint64_t, bool> Unsigned = splitSigned(N);
|
|
|
|
return joinSigned(scale(Unsigned.first), Unsigned.second);
|
|
|
|
}
|
|
|
|
int64_t scaleByInverse(int64_t N) const {
|
|
|
|
std::pair<uint64_t, bool> Unsigned = splitSigned(N);
|
|
|
|
return joinSigned(scaleByInverse(Unsigned.first), Unsigned.second);
|
|
|
|
}
|
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
int compare(const UnsignedFloat &X) const;
|
2014-04-21 17:57:07 +00:00
|
|
|
int compareTo(uint64_t N) const {
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat Float = getFloat(N);
|
2014-04-21 17:57:07 +00:00
|
|
|
int Compare = compare(Float);
|
|
|
|
if (Width == 64 || Compare != 0)
|
|
|
|
return Compare;
|
|
|
|
|
|
|
|
// Check for precision loss. We know *this == RoundTrip.
|
|
|
|
uint64_t RoundTrip = Float.template toInt<uint64_t>();
|
|
|
|
return N == RoundTrip ? 0 : RoundTrip < N ? -1 : 1;
|
|
|
|
}
|
|
|
|
int compareTo(int64_t N) const { return N < 0 ? 1 : compareTo(uint64_t(N)); }
|
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat &invert() { return *this = UnsignedFloat::getFloat(1) / *this; }
|
|
|
|
UnsignedFloat inverse() const { return UnsignedFloat(*this).invert(); }
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
private:
|
2014-04-21 18:31:58 +00:00
|
|
|
static UnsignedFloat getProduct(DigitsType L, DigitsType R);
|
|
|
|
static UnsignedFloat getQuotient(DigitsType Dividend, DigitsType Divisor);
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
std::pair<int32_t, int> lgImpl() const;
|
|
|
|
static int countLeadingZerosWidth(DigitsType Digits) {
|
|
|
|
if (Width == 64)
|
|
|
|
return countLeadingZeros64(Digits);
|
|
|
|
if (Width == 32)
|
|
|
|
return countLeadingZeros32(Digits);
|
|
|
|
return countLeadingZeros32(Digits) + Width - 32;
|
|
|
|
}
|
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
static UnsignedFloat adjustToWidth(uint64_t N, int32_t S) {
|
2014-04-21 17:57:07 +00:00
|
|
|
assert(S >= MinExponent);
|
|
|
|
assert(S <= MaxExponent);
|
|
|
|
if (Width == 64 || N <= DigitsLimits::max())
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloat(N, S);
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
// Shift right.
|
|
|
|
int Shift = 64 - Width - countLeadingZeros64(N);
|
|
|
|
DigitsType Shifted = N >> Shift;
|
|
|
|
|
|
|
|
// Round.
|
|
|
|
assert(S + Shift <= MaxExponent);
|
2014-04-21 18:31:58 +00:00
|
|
|
return getRounded(UnsignedFloat(Shifted, S + Shift),
|
2014-04-21 17:57:07 +00:00
|
|
|
N & UINT64_C(1) << (Shift - 1));
|
|
|
|
}
|
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
static UnsignedFloat getRounded(UnsignedFloat P, bool Round) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (!Round)
|
|
|
|
return P;
|
|
|
|
if (P.Digits == DigitsLimits::max())
|
|
|
|
// Careful of overflow in the exponent.
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloat(1, P.Exponent) <<= Width;
|
|
|
|
return UnsignedFloat(P.Digits + 1, P.Exponent);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2014-04-11 23:21:02 +00:00
|
|
|
};
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
#define UNSIGNED_FLOAT_BOP(op, base) \
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class DigitsT> \
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> operator op(const UnsignedFloat<DigitsT> &L, \
|
|
|
|
const UnsignedFloat<DigitsT> &R) { \
|
|
|
|
return UnsignedFloat<DigitsT>(L) base R; \
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2014-04-21 18:31:58 +00:00
|
|
|
UNSIGNED_FLOAT_BOP(+, += )
|
|
|
|
UNSIGNED_FLOAT_BOP(-, -= )
|
|
|
|
UNSIGNED_FLOAT_BOP(*, *= )
|
|
|
|
UNSIGNED_FLOAT_BOP(/, /= )
|
|
|
|
UNSIGNED_FLOAT_BOP(<<, <<= )
|
|
|
|
UNSIGNED_FLOAT_BOP(>>, >>= )
|
|
|
|
#undef UNSIGNED_FLOAT_BOP
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const UnsignedFloat<DigitsT> &X) {
|
2014-04-21 17:57:07 +00:00
|
|
|
return X.print(OS, 10);
|
2014-04-19 22:34:26 +00:00
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
#define UNSIGNED_FLOAT_COMPARE_TO_TYPE(op, T1, T2) \
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class DigitsT> \
|
2014-04-21 18:31:58 +00:00
|
|
|
bool operator op(const UnsignedFloat<DigitsT> &L, T1 R) { \
|
2014-04-21 17:57:07 +00:00
|
|
|
return L.compareTo(T2(R)) op 0; \
|
|
|
|
} \
|
|
|
|
template <class DigitsT> \
|
2014-04-21 18:31:58 +00:00
|
|
|
bool operator op(T1 L, const UnsignedFloat<DigitsT> &R) { \
|
2014-04-21 17:57:07 +00:00
|
|
|
return 0 op R.compareTo(T2(L)); \
|
|
|
|
}
|
2014-04-21 18:31:58 +00:00
|
|
|
#define UNSIGNED_FLOAT_COMPARE_TO(op) \
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO_TYPE(op, uint64_t, uint64_t) \
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO_TYPE(op, uint32_t, uint64_t) \
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO_TYPE(op, int64_t, int64_t) \
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO_TYPE(op, int32_t, int64_t)
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO(< )
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO(> )
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO(== )
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO(!= )
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO(<= )
|
|
|
|
UNSIGNED_FLOAT_COMPARE_TO(>= )
|
|
|
|
#undef UNSIGNED_FLOAT_COMPARE_TO
|
|
|
|
#undef UNSIGNED_FLOAT_COMPARE_TO_TYPE
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
uint64_t UnsignedFloat<DigitsT>::scale(uint64_t N) const {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (Width == 64 || N <= DigitsLimits::max())
|
|
|
|
return (getFloat(N) * *this).template toInt<uint64_t>();
|
|
|
|
|
|
|
|
// Defer to the 64-bit version.
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloat<uint64_t>(Digits, Exponent).scale(N);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> UnsignedFloat<DigitsT>::getProduct(DigitsType L,
|
2014-04-21 17:57:07 +00:00
|
|
|
DigitsType R) {
|
|
|
|
// Check for zero.
|
|
|
|
if (!L || !R)
|
|
|
|
return getZero();
|
|
|
|
|
|
|
|
// Check for numbers that we can compute with 64-bit math.
|
|
|
|
if (Width <= 32 || (L <= UINT32_MAX && R <= UINT32_MAX))
|
|
|
|
return adjustToWidth(uint64_t(L) * uint64_t(R), 0);
|
|
|
|
|
|
|
|
// Do the full thing.
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloat(multiply64(L, R));
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> UnsignedFloat<DigitsT>::getQuotient(DigitsType Dividend,
|
2014-04-21 17:57:07 +00:00
|
|
|
DigitsType Divisor) {
|
|
|
|
// Check for zero.
|
|
|
|
if (!Dividend)
|
|
|
|
return getZero();
|
|
|
|
if (!Divisor)
|
|
|
|
return getLargest();
|
|
|
|
|
|
|
|
if (Width == 64)
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloat(divide64(Dividend, Divisor));
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
// We can compute this with 64-bit math.
|
|
|
|
int Shift = countLeadingZeros64(Dividend);
|
|
|
|
uint64_t Shifted = uint64_t(Dividend) << Shift;
|
|
|
|
uint64_t Quotient = Shifted / Divisor;
|
|
|
|
|
|
|
|
// If Quotient needs to be shifted, then adjustToWidth will round.
|
|
|
|
if (Quotient > DigitsLimits::max())
|
|
|
|
return adjustToWidth(Quotient, -Shift);
|
|
|
|
|
|
|
|
// Round based on the value of the next bit.
|
2014-04-21 18:31:58 +00:00
|
|
|
return getRounded(UnsignedFloat(Quotient, -Shift),
|
2014-04-21 17:57:07 +00:00
|
|
|
Shifted % Divisor >= getHalf(Divisor));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class DigitsT>
|
|
|
|
template <class IntT>
|
2014-04-21 18:31:58 +00:00
|
|
|
IntT UnsignedFloat<DigitsT>::toInt() const {
|
2014-04-21 17:57:07 +00:00
|
|
|
typedef std::numeric_limits<IntT> Limits;
|
|
|
|
if (*this < 1)
|
|
|
|
return 0;
|
|
|
|
if (*this >= Limits::max())
|
|
|
|
return Limits::max();
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
IntT N = Digits;
|
|
|
|
if (Exponent > 0) {
|
|
|
|
assert(size_t(Exponent) < sizeof(IntT) * 8);
|
|
|
|
return N << Exponent;
|
|
|
|
}
|
|
|
|
if (Exponent < 0) {
|
|
|
|
assert(size_t(-Exponent) < sizeof(IntT) * 8);
|
|
|
|
return N >> -Exponent;
|
|
|
|
}
|
|
|
|
return N;
|
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
std::pair<int32_t, int> UnsignedFloat<DigitsT>::lgImpl() const {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (isZero())
|
|
|
|
return std::make_pair(INT32_MIN, 0);
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Get the floor of the lg of Digits.
|
|
|
|
int32_t LocalFloor = Width - countLeadingZerosWidth(Digits) - 1;
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Get the floor of the lg of this.
|
|
|
|
int32_t Floor = Exponent + LocalFloor;
|
|
|
|
if (Digits == UINT64_C(1) << LocalFloor)
|
|
|
|
return std::make_pair(Floor, 0);
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Round based on the next digit.
|
|
|
|
assert(LocalFloor >= 1);
|
|
|
|
bool Round = Digits & UINT64_C(1) << (LocalFloor - 1);
|
|
|
|
return std::make_pair(Floor + Round, Round ? 1 : -1);
|
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> UnsignedFloat<DigitsT>::matchExponents(UnsignedFloat X) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (isZero() || X.isZero() || Exponent == X.Exponent)
|
|
|
|
return X;
|
|
|
|
|
|
|
|
int32_t Diff = int32_t(X.Exponent) - int32_t(Exponent);
|
|
|
|
if (Diff > 0)
|
|
|
|
increaseExponentToMatch(X, Diff);
|
|
|
|
else
|
|
|
|
X.increaseExponentToMatch(*this, -Diff);
|
|
|
|
return X;
|
|
|
|
}
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
void UnsignedFloat<DigitsT>::increaseExponentToMatch(UnsignedFloat &X,
|
2014-04-21 17:57:07 +00:00
|
|
|
int32_t ExponentDiff) {
|
|
|
|
assert(ExponentDiff > 0);
|
|
|
|
if (ExponentDiff >= 2 * Width) {
|
|
|
|
*this = getZero();
|
|
|
|
return;
|
2014-04-19 22:34:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Use up any leading zeros on X, and then shift this.
|
|
|
|
int32_t ShiftX = std::min(countLeadingZerosWidth(X.Digits), ExponentDiff);
|
|
|
|
assert(ShiftX < Width);
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
int32_t ShiftThis = ExponentDiff - ShiftX;
|
|
|
|
if (ShiftThis >= Width) {
|
|
|
|
*this = getZero();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
X.Digits <<= ShiftX;
|
|
|
|
X.Exponent -= ShiftX;
|
|
|
|
Digits >>= ShiftThis;
|
|
|
|
Exponent += ShiftThis;
|
|
|
|
return;
|
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> &UnsignedFloat<DigitsT>::
|
|
|
|
operator+=(const UnsignedFloat &X) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (isLargest() || X.isZero())
|
|
|
|
return *this;
|
|
|
|
if (isZero() || X.isLargest())
|
|
|
|
return *this = X;
|
|
|
|
|
|
|
|
// Normalize exponents.
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat Scaled = matchExponents(X);
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
// Check for zero again.
|
|
|
|
if (isZero())
|
|
|
|
return *this = Scaled;
|
|
|
|
if (Scaled.isZero())
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// Compute sum.
|
|
|
|
DigitsType Sum = Digits + Scaled.Digits;
|
|
|
|
bool DidOverflow = Sum < Digits;
|
|
|
|
Digits = Sum;
|
|
|
|
if (!DidOverflow)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
if (Exponent == MaxExponent)
|
|
|
|
return *this = getLargest();
|
|
|
|
|
|
|
|
++Exponent;
|
|
|
|
Digits = UINT64_C(1) << (Width - 1) | Digits >> 1;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> &UnsignedFloat<DigitsT>::
|
|
|
|
operator-=(const UnsignedFloat &X) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (X.isZero())
|
|
|
|
return *this;
|
|
|
|
if (*this <= X)
|
|
|
|
return *this = getZero();
|
|
|
|
|
|
|
|
// Normalize exponents.
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat Scaled = matchExponents(X);
|
2014-04-21 17:57:07 +00:00
|
|
|
assert(Digits >= Scaled.Digits);
|
|
|
|
|
|
|
|
// Compute difference.
|
|
|
|
if (!Scaled.isZero()) {
|
|
|
|
Digits -= Scaled.Digits;
|
|
|
|
return *this;
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Check if X just barely lost its last bit. E.g., for 32-bit:
|
|
|
|
//
|
|
|
|
// 1*2^32 - 1*2^0 == 0xffffffff != 1*2^32
|
2014-04-21 18:31:58 +00:00
|
|
|
if (*this == UnsignedFloat(1, X.lgFloor() + Width)) {
|
2014-04-21 17:57:07 +00:00
|
|
|
Digits = DigitsType(0) - 1;
|
|
|
|
--Exponent;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> &UnsignedFloat<DigitsT>::
|
|
|
|
operator*=(const UnsignedFloat &X) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (isZero())
|
|
|
|
return *this;
|
|
|
|
if (X.isZero())
|
|
|
|
return *this = X;
|
|
|
|
|
|
|
|
// Save the exponents.
|
|
|
|
int32_t Exponents = int32_t(Exponent) + int32_t(X.Exponent);
|
|
|
|
|
|
|
|
// Get the raw product.
|
|
|
|
*this = getProduct(Digits, X.Digits);
|
|
|
|
|
|
|
|
// Combine with exponents.
|
|
|
|
return *this <<= Exponents;
|
|
|
|
}
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<DigitsT> &UnsignedFloat<DigitsT>::
|
|
|
|
operator/=(const UnsignedFloat &X) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (isZero())
|
|
|
|
return *this;
|
|
|
|
if (X.isZero())
|
|
|
|
return *this = getLargest();
|
|
|
|
|
|
|
|
// Save the exponents.
|
|
|
|
int32_t Exponents = int32_t(Exponent) - int32_t(X.Exponent);
|
|
|
|
|
|
|
|
// Get the raw quotient.
|
|
|
|
*this = getQuotient(Digits, X.Digits);
|
|
|
|
|
|
|
|
// Combine with exponents.
|
|
|
|
return *this <<= Exponents;
|
|
|
|
}
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
void UnsignedFloat<DigitsT>::shiftLeft(int32_t Shift) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (!Shift || isZero())
|
|
|
|
return;
|
|
|
|
assert(Shift != INT32_MIN);
|
|
|
|
if (Shift < 0) {
|
|
|
|
shiftRight(-Shift);
|
|
|
|
return;
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Shift as much as we can in the exponent.
|
|
|
|
int32_t ExponentShift = std::min(Shift, MaxExponent - Exponent);
|
|
|
|
Exponent += ExponentShift;
|
|
|
|
if (ExponentShift == Shift)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check this late, since it's rare.
|
|
|
|
if (isLargest())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Shift the digits themselves.
|
|
|
|
Shift -= ExponentShift;
|
|
|
|
if (Shift > countLeadingZerosWidth(Digits)) {
|
|
|
|
// Saturate.
|
|
|
|
*this = getLargest();
|
|
|
|
return;
|
2014-04-19 22:34:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
Digits <<= Shift;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
void UnsignedFloat<DigitsT>::shiftRight(int32_t Shift) {
|
2014-04-21 17:57:07 +00:00
|
|
|
if (!Shift || isZero())
|
|
|
|
return;
|
|
|
|
assert(Shift != INT32_MIN);
|
|
|
|
if (Shift < 0) {
|
|
|
|
shiftLeft(-Shift);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift as much as we can in the exponent.
|
|
|
|
int32_t ExponentShift = std::min(Shift, Exponent - MinExponent);
|
|
|
|
Exponent -= ExponentShift;
|
|
|
|
if (ExponentShift == Shift)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Shift the digits themselves.
|
|
|
|
Shift -= ExponentShift;
|
|
|
|
if (Shift >= Width) {
|
|
|
|
// Saturate.
|
|
|
|
*this = getZero();
|
|
|
|
return;
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
Digits >>= Shift;
|
|
|
|
return;
|
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class DigitsT>
|
2014-04-21 18:31:58 +00:00
|
|
|
int UnsignedFloat<DigitsT>::compare(const UnsignedFloat &X) const {
|
2014-04-21 17:57:07 +00:00
|
|
|
// Check for zero.
|
|
|
|
if (isZero())
|
|
|
|
return X.isZero() ? 0 : -1;
|
|
|
|
if (X.isZero())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// Check for the scale. Use lgFloor to be sure that the exponent difference
|
|
|
|
// is always lower than 64.
|
|
|
|
int32_t lgL = lgFloor(), lgR = X.lgFloor();
|
|
|
|
if (lgL != lgR)
|
|
|
|
return lgL < lgR ? -1 : 1;
|
|
|
|
|
|
|
|
// Compare digits.
|
|
|
|
if (Exponent < X.Exponent)
|
2014-04-21 18:31:58 +00:00
|
|
|
return UnsignedFloatBase::compare(Digits, X.Digits, X.Exponent - Exponent);
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
return -UnsignedFloatBase::compare(X.Digits, Digits, Exponent - X.Exponent);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 18:31:58 +00:00
|
|
|
template <class T> struct isPodLike<UnsignedFloat<T>> {
|
2014-04-21 17:57:07 +00:00
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// BlockMass definition.
|
|
|
|
//
|
|
|
|
// TODO: Make this private to BlockFrequencyInfoImpl or delete.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// \brief Mass of a block.
|
|
|
|
///
|
|
|
|
/// This class implements a sort of fixed-point fraction always between 0.0 and
|
|
|
|
/// 1.0. getMass() == UINT64_MAX indicates a value of 1.0.
|
|
|
|
///
|
|
|
|
/// Masses can be added and subtracted. Simple saturation arithmetic is used,
|
|
|
|
/// so arithmetic operations never overflow or underflow.
|
|
|
|
///
|
|
|
|
/// Masses can be multiplied. Multiplication treats full mass as 1.0 and uses
|
|
|
|
/// an inexpensive floating-point algorithm that's off-by-one (almost, but not
|
|
|
|
/// quite, maximum precision).
|
|
|
|
///
|
|
|
|
/// Masses can be scaled by \a BranchProbability at maximum precision.
|
|
|
|
class BlockMass {
|
|
|
|
uint64_t Mass;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BlockMass() : Mass(0) {}
|
|
|
|
explicit BlockMass(uint64_t Mass) : Mass(Mass) {}
|
|
|
|
|
|
|
|
static BlockMass getEmpty() { return BlockMass(); }
|
|
|
|
static BlockMass getFull() { return BlockMass(UINT64_MAX); }
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
uint64_t getMass() const { return Mass; }
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
bool isFull() const { return Mass == UINT64_MAX; }
|
|
|
|
bool isEmpty() const { return !Mass; }
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
bool operator!() const { return isEmpty(); }
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Add another mass.
|
|
|
|
///
|
|
|
|
/// Adds another mass, saturating at \a isFull() rather than overflowing.
|
|
|
|
BlockMass &operator+=(const BlockMass &X) {
|
|
|
|
uint64_t Sum = Mass + X.Mass;
|
|
|
|
Mass = Sum < Mass ? UINT64_MAX : Sum;
|
|
|
|
return *this;
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Subtract another mass.
|
2011-06-23 21:56:59 +00:00
|
|
|
///
|
2014-04-21 17:57:07 +00:00
|
|
|
/// Subtracts another mass, saturating at \a isEmpty() rather than
|
|
|
|
/// undeflowing.
|
|
|
|
BlockMass &operator-=(const BlockMass &X) {
|
|
|
|
uint64_t Diff = Mass - X.Mass;
|
|
|
|
Mass = Diff > Mass ? 0 : Diff;
|
|
|
|
return *this;
|
2014-04-19 22:34:26 +00:00
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-29 16:20:05 +00:00
|
|
|
BlockMass &operator*=(const BranchProbability &P) {
|
|
|
|
Mass = P.scale(Mass);
|
|
|
|
return *this;
|
|
|
|
}
|
2014-04-18 02:17:43 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
bool operator==(const BlockMass &X) const { return Mass == X.Mass; }
|
|
|
|
bool operator!=(const BlockMass &X) const { return Mass != X.Mass; }
|
|
|
|
bool operator<=(const BlockMass &X) const { return Mass <= X.Mass; }
|
|
|
|
bool operator>=(const BlockMass &X) const { return Mass >= X.Mass; }
|
|
|
|
bool operator<(const BlockMass &X) const { return Mass < X.Mass; }
|
|
|
|
bool operator>(const BlockMass &X) const { return Mass > X.Mass; }
|
2014-04-18 17:56:08 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Convert to floating point.
|
|
|
|
///
|
|
|
|
/// Convert to a float. \a isFull() gives 1.0, while \a isEmpty() gives
|
|
|
|
/// slightly above 0.0.
|
2014-04-21 18:31:58 +00:00
|
|
|
UnsignedFloat<uint64_t> toFloat() const;
|
2014-04-18 22:30:03 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
void dump() const;
|
|
|
|
raw_ostream &print(raw_ostream &OS) const;
|
|
|
|
};
|
2014-04-19 00:42:46 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
inline BlockMass operator+(const BlockMass &L, const BlockMass &R) {
|
|
|
|
return BlockMass(L) += R;
|
|
|
|
}
|
|
|
|
inline BlockMass operator-(const BlockMass &L, const BlockMass &R) {
|
|
|
|
return BlockMass(L) -= R;
|
|
|
|
}
|
|
|
|
inline BlockMass operator*(const BlockMass &L, const BranchProbability &R) {
|
|
|
|
return BlockMass(L) *= R;
|
|
|
|
}
|
|
|
|
inline BlockMass operator*(const BranchProbability &L, const BlockMass &R) {
|
|
|
|
return BlockMass(R) *= L;
|
|
|
|
}
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const BlockMass &X) {
|
|
|
|
return X.print(OS);
|
|
|
|
}
|
2014-04-19 00:42:46 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <> struct isPodLike<BlockMass> {
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
}
|
2014-04-19 22:46:00 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// BlockFrequencyInfoImpl definition.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class BasicBlock;
|
|
|
|
class BranchProbabilityInfo;
|
|
|
|
class Function;
|
|
|
|
class Loop;
|
|
|
|
class LoopInfo;
|
|
|
|
class MachineBasicBlock;
|
|
|
|
class MachineBranchProbabilityInfo;
|
|
|
|
class MachineFunction;
|
|
|
|
class MachineLoop;
|
|
|
|
class MachineLoopInfo;
|
|
|
|
|
2014-04-28 20:02:29 +00:00
|
|
|
namespace bfi_detail {
|
|
|
|
struct IrreducibleGraph;
|
|
|
|
|
|
|
|
// This is part of a workaround for a GCC 4.7 crash on lambdas.
|
|
|
|
template <class BT> struct BlockEdgesAdder;
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Base class for BlockFrequencyInfoImpl
|
|
|
|
///
|
|
|
|
/// BlockFrequencyInfoImplBase has supporting data structures and some
|
|
|
|
/// algorithms for BlockFrequencyInfoImplBase. Only algorithms that depend on
|
|
|
|
/// the block type (or that call such algorithms) are skipped here.
|
|
|
|
///
|
|
|
|
/// Nevertheless, the majority of the overall algorithm documention lives with
|
|
|
|
/// BlockFrequencyInfoImpl. See there for details.
|
|
|
|
class BlockFrequencyInfoImplBase {
|
|
|
|
public:
|
2014-04-21 18:31:58 +00:00
|
|
|
typedef UnsignedFloat<uint64_t> Float;
|
2014-04-19 22:46:00 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Representative of a block.
|
|
|
|
///
|
|
|
|
/// This is a simple wrapper around an index into the reverse-post-order
|
|
|
|
/// traversal of the blocks.
|
|
|
|
///
|
|
|
|
/// Unlike a block pointer, its order has meaning (location in the
|
|
|
|
/// topological sort) and it's class is the same regardless of block type.
|
|
|
|
struct BlockNode {
|
|
|
|
typedef uint32_t IndexType;
|
|
|
|
IndexType Index;
|
|
|
|
|
|
|
|
bool operator==(const BlockNode &X) const { return Index == X.Index; }
|
|
|
|
bool operator!=(const BlockNode &X) const { return Index != X.Index; }
|
|
|
|
bool operator<=(const BlockNode &X) const { return Index <= X.Index; }
|
|
|
|
bool operator>=(const BlockNode &X) const { return Index >= X.Index; }
|
|
|
|
bool operator<(const BlockNode &X) const { return Index < X.Index; }
|
|
|
|
bool operator>(const BlockNode &X) const { return Index > X.Index; }
|
|
|
|
|
|
|
|
BlockNode() : Index(UINT32_MAX) {}
|
|
|
|
BlockNode(IndexType Index) : Index(Index) {}
|
|
|
|
|
|
|
|
bool isValid() const { return Index <= getMaxIndex(); }
|
|
|
|
static size_t getMaxIndex() { return UINT32_MAX - 1; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Stats about a block itself.
|
|
|
|
struct FrequencyData {
|
|
|
|
Float Floating;
|
|
|
|
uint64_t Integer;
|
|
|
|
};
|
|
|
|
|
2014-04-22 03:31:31 +00:00
|
|
|
/// \brief Data about a loop.
|
2014-04-22 03:31:25 +00:00
|
|
|
///
|
2014-04-22 03:31:31 +00:00
|
|
|
/// Contains the data necessary to represent represent a loop as a
|
|
|
|
/// pseudo-node once it's packaged.
|
|
|
|
struct LoopData {
|
2014-04-22 03:31:25 +00:00
|
|
|
typedef SmallVector<std::pair<BlockNode, BlockMass>, 4> ExitMap;
|
2014-04-25 04:38:15 +00:00
|
|
|
typedef SmallVector<BlockNode, 4> NodeList;
|
2014-04-25 04:38:03 +00:00
|
|
|
LoopData *Parent; ///< The parent loop.
|
2014-04-22 03:31:37 +00:00
|
|
|
bool IsPackaged; ///< Whether this has been packaged.
|
2014-04-28 20:02:29 +00:00
|
|
|
uint32_t NumHeaders; ///< Number of headers.
|
2014-04-22 03:31:25 +00:00
|
|
|
ExitMap Exits; ///< Successor edges (and weights).
|
2014-04-25 04:38:15 +00:00
|
|
|
NodeList Nodes; ///< Header and the members of the loop.
|
2014-04-22 03:31:25 +00:00
|
|
|
BlockMass BackedgeMass; ///< Mass returned to loop header.
|
|
|
|
BlockMass Mass;
|
|
|
|
Float Scale;
|
|
|
|
|
2014-04-25 04:38:03 +00:00
|
|
|
LoopData(LoopData *Parent, const BlockNode &Header)
|
2014-04-28 20:02:29 +00:00
|
|
|
: Parent(Parent), IsPackaged(false), NumHeaders(1), Nodes(1, Header) {}
|
|
|
|
template <class It1, class It2>
|
|
|
|
LoopData(LoopData *Parent, It1 FirstHeader, It1 LastHeader, It2 FirstOther,
|
|
|
|
It2 LastOther)
|
|
|
|
: Parent(Parent), IsPackaged(false), Nodes(FirstHeader, LastHeader) {
|
|
|
|
NumHeaders = Nodes.size();
|
|
|
|
Nodes.insert(Nodes.end(), FirstOther, LastOther);
|
|
|
|
}
|
|
|
|
bool isHeader(const BlockNode &Node) const {
|
|
|
|
if (isIrreducible())
|
|
|
|
return std::binary_search(Nodes.begin(), Nodes.begin() + NumHeaders,
|
|
|
|
Node);
|
|
|
|
return Node == Nodes[0];
|
|
|
|
}
|
2014-04-25 04:38:09 +00:00
|
|
|
BlockNode getHeader() const { return Nodes[0]; }
|
2014-04-28 20:02:29 +00:00
|
|
|
bool isIrreducible() const { return NumHeaders > 1; }
|
2014-04-25 04:38:09 +00:00
|
|
|
|
2014-04-28 20:02:29 +00:00
|
|
|
NodeList::const_iterator members_begin() const {
|
|
|
|
return Nodes.begin() + NumHeaders;
|
|
|
|
}
|
2014-04-25 04:38:15 +00:00
|
|
|
NodeList::const_iterator members_end() const { return Nodes.end(); }
|
|
|
|
iterator_range<NodeList::const_iterator> members() const {
|
2014-04-25 04:38:09 +00:00
|
|
|
return make_range(members_begin(), members_end());
|
|
|
|
}
|
2014-04-22 03:31:25 +00:00
|
|
|
};
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Index of loop information.
|
|
|
|
struct WorkingData {
|
2014-04-25 04:38:03 +00:00
|
|
|
BlockNode Node; ///< This node.
|
|
|
|
LoopData *Loop; ///< The loop this block is inside.
|
|
|
|
BlockMass Mass; ///< Mass distribution from the entry block.
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-25 04:38:03 +00:00
|
|
|
WorkingData(const BlockNode &Node) : Node(Node), Loop(nullptr) {}
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-25 04:38:06 +00:00
|
|
|
bool isLoopHeader() const { return Loop && Loop->isHeader(Node); }
|
2014-04-28 20:02:29 +00:00
|
|
|
bool isDoubleLoopHeader() const {
|
|
|
|
return isLoopHeader() && Loop->Parent && Loop->Parent->isIrreducible() &&
|
|
|
|
Loop->Parent->isHeader(Node);
|
|
|
|
}
|
2014-04-22 03:31:37 +00:00
|
|
|
|
2014-04-25 04:38:03 +00:00
|
|
|
LoopData *getContainingLoop() const {
|
2014-04-28 20:02:29 +00:00
|
|
|
if (!isLoopHeader())
|
|
|
|
return Loop;
|
|
|
|
if (!isDoubleLoopHeader())
|
|
|
|
return Loop->Parent;
|
|
|
|
return Loop->Parent->Parent;
|
2014-04-25 04:38:03 +00:00
|
|
|
}
|
2014-04-25 18:47:04 +00:00
|
|
|
|
|
|
|
/// \brief Resolve a node to its representative.
|
|
|
|
///
|
|
|
|
/// Get the node currently representing Node, which could be a containing
|
|
|
|
/// loop.
|
|
|
|
///
|
|
|
|
/// This function should only be called when distributing mass. As long as
|
|
|
|
/// there are no irreducilbe edges to Node, then it will have complexity
|
|
|
|
/// O(1) in this context.
|
|
|
|
///
|
|
|
|
/// In general, the complexity is O(L), where L is the number of loop
|
|
|
|
/// headers Node has been packaged into. Since this method is called in
|
|
|
|
/// the context of distributing mass, L will be the number of loop headers
|
|
|
|
/// an early exit edge jumps out of.
|
|
|
|
BlockNode getResolvedNode() const {
|
|
|
|
auto L = getPackagedLoop();
|
|
|
|
return L ? L->getHeader() : Node;
|
|
|
|
}
|
|
|
|
LoopData *getPackagedLoop() const {
|
|
|
|
if (!Loop || !Loop->IsPackaged)
|
|
|
|
return nullptr;
|
|
|
|
auto L = Loop;
|
|
|
|
while (L->Parent && L->Parent->IsPackaged)
|
|
|
|
L = L->Parent;
|
|
|
|
return L;
|
2014-04-22 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 18:47:04 +00:00
|
|
|
/// \brief Get the appropriate mass for a node.
|
|
|
|
///
|
|
|
|
/// Get appropriate mass for Node. If Node is a loop-header (whose loop
|
|
|
|
/// has been packaged), returns the mass of its pseudo-node. If it's a
|
|
|
|
/// node inside a packaged loop, it returns the loop's mass.
|
2014-04-28 20:02:29 +00:00
|
|
|
BlockMass &getMass() {
|
|
|
|
if (!isAPackage())
|
|
|
|
return Mass;
|
|
|
|
if (!isADoublePackage())
|
|
|
|
return Loop->Mass;
|
|
|
|
return Loop->Parent->Mass;
|
|
|
|
}
|
2014-04-25 18:47:04 +00:00
|
|
|
|
2014-04-22 03:31:44 +00:00
|
|
|
/// \brief Has ContainingLoop been packaged up?
|
2014-04-25 18:47:04 +00:00
|
|
|
bool isPackaged() const { return getResolvedNode() != Node; }
|
2014-04-22 03:31:44 +00:00
|
|
|
/// \brief Has Loop been packaged up?
|
2014-04-25 04:38:03 +00:00
|
|
|
bool isAPackage() const { return isLoopHeader() && Loop->IsPackaged; }
|
2014-04-28 20:02:29 +00:00
|
|
|
/// \brief Has Loop been packaged up twice?
|
|
|
|
bool isADoublePackage() const {
|
|
|
|
return isDoubleLoopHeader() && Loop->Parent->IsPackaged;
|
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Unscaled probability weight.
|
|
|
|
///
|
|
|
|
/// Probability weight for an edge in the graph (including the
|
|
|
|
/// successor/target node).
|
|
|
|
///
|
|
|
|
/// All edges in the original function are 32-bit. However, exit edges from
|
|
|
|
/// loop packages are taken from 64-bit exit masses, so we need 64-bits of
|
|
|
|
/// space in general.
|
|
|
|
///
|
|
|
|
/// In addition to the raw weight amount, Weight stores the type of the edge
|
|
|
|
/// in the current context (i.e., the context of the loop being processed).
|
|
|
|
/// Is this a local edge within the loop, an exit from the loop, or a
|
|
|
|
/// backedge to the loop header?
|
|
|
|
struct Weight {
|
|
|
|
enum DistType { Local, Exit, Backedge };
|
|
|
|
DistType Type;
|
|
|
|
BlockNode TargetNode;
|
|
|
|
uint64_t Amount;
|
|
|
|
Weight() : Type(Local), Amount(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Distribution of unscaled probability weight.
|
|
|
|
///
|
|
|
|
/// Distribution of unscaled probability weight to a set of successors.
|
|
|
|
///
|
|
|
|
/// This class collates the successor edge weights for later processing.
|
|
|
|
///
|
|
|
|
/// \a DidOverflow indicates whether \a Total did overflow while adding to
|
2014-04-25 04:38:43 +00:00
|
|
|
/// the distribution. It should never overflow twice.
|
2014-04-21 17:57:07 +00:00
|
|
|
struct Distribution {
|
|
|
|
typedef SmallVector<Weight, 4> WeightList;
|
|
|
|
WeightList Weights; ///< Individual successor weights.
|
|
|
|
uint64_t Total; ///< Sum of all weights.
|
|
|
|
bool DidOverflow; ///< Whether \a Total did overflow.
|
|
|
|
|
2014-04-25 04:38:43 +00:00
|
|
|
Distribution() : Total(0), DidOverflow(false) {}
|
2014-04-21 17:57:07 +00:00
|
|
|
void addLocal(const BlockNode &Node, uint64_t Amount) {
|
|
|
|
add(Node, Amount, Weight::Local);
|
|
|
|
}
|
|
|
|
void addExit(const BlockNode &Node, uint64_t Amount) {
|
|
|
|
add(Node, Amount, Weight::Exit);
|
|
|
|
}
|
|
|
|
void addBackedge(const BlockNode &Node, uint64_t Amount) {
|
|
|
|
add(Node, Amount, Weight::Backedge);
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Normalize the distribution.
|
|
|
|
///
|
|
|
|
/// Combines multiple edges to the same \a Weight::TargetNode and scales
|
|
|
|
/// down so that \a Total fits into 32-bits.
|
|
|
|
///
|
|
|
|
/// This is linear in the size of \a Weights. For the vast majority of
|
|
|
|
/// cases, adjacent edge weights are combined by sorting WeightList and
|
|
|
|
/// combining adjacent weights. However, for very large edge lists an
|
|
|
|
/// auxiliary hash table is used.
|
|
|
|
void normalize();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void add(const BlockNode &Node, uint64_t Amount, Weight::DistType Type);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Data about each block. This is used downstream.
|
|
|
|
std::vector<FrequencyData> Freqs;
|
|
|
|
|
|
|
|
/// \brief Loop data: see initializeLoops().
|
|
|
|
std::vector<WorkingData> Working;
|
|
|
|
|
2014-04-22 03:31:44 +00:00
|
|
|
/// \brief Indexed information about loops.
|
2014-04-25 04:30:06 +00:00
|
|
|
std::list<LoopData> Loops;
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
/// \brief Add all edges out of a packaged loop to the distribution.
|
|
|
|
///
|
|
|
|
/// Adds all edges from LocalLoopHead to Dist. Calls addToDist() to add each
|
|
|
|
/// successor edge.
|
2014-04-28 20:02:29 +00:00
|
|
|
///
|
|
|
|
/// \return \c true unless there's an irreducible backedge.
|
|
|
|
bool addLoopSuccessorsToDist(const LoopData *OuterLoop, LoopData &Loop,
|
2014-04-21 17:57:07 +00:00
|
|
|
Distribution &Dist);
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Add an edge to the distribution.
|
|
|
|
///
|
|
|
|
/// Adds an edge to Succ to Dist. If \c LoopHead.isValid(), then whether the
|
2014-04-25 04:38:43 +00:00
|
|
|
/// edge is local/exit/backedge is in the context of LoopHead. Otherwise,
|
|
|
|
/// every edge should be a local edge (since all the loops are packaged up).
|
2014-04-28 20:02:29 +00:00
|
|
|
///
|
|
|
|
/// \return \c true unless aborted due to an irreducible backedge.
|
|
|
|
bool addToDist(Distribution &Dist, const LoopData *OuterLoop,
|
2014-04-21 17:57:07 +00:00
|
|
|
const BlockNode &Pred, const BlockNode &Succ, uint64_t Weight);
|
|
|
|
|
2014-04-22 03:31:31 +00:00
|
|
|
LoopData &getLoopPackage(const BlockNode &Head) {
|
2014-04-21 17:57:07 +00:00
|
|
|
assert(Head.Index < Working.size());
|
2014-04-25 04:38:03 +00:00
|
|
|
assert(Working[Head.Index].isLoopHeader());
|
2014-04-22 03:31:37 +00:00
|
|
|
return *Working[Head.Index].Loop;
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 20:02:29 +00:00
|
|
|
/// \brief Analyze irreducible SCCs.
|
|
|
|
///
|
|
|
|
/// Separate irreducible SCCs from \c G, which is an explict graph of \c
|
|
|
|
/// OuterLoop (or the top-level function, if \c OuterLoop is \c nullptr).
|
|
|
|
/// Insert them into \a Loops before \c Insert.
|
|
|
|
///
|
|
|
|
/// \return the \c LoopData nodes representing the irreducible SCCs.
|
|
|
|
iterator_range<std::list<LoopData>::iterator>
|
|
|
|
analyzeIrreducible(const bfi_detail::IrreducibleGraph &G, LoopData *OuterLoop,
|
|
|
|
std::list<LoopData>::iterator Insert);
|
|
|
|
|
|
|
|
/// \brief Update a loop after packaging irreducible SCCs inside of it.
|
|
|
|
///
|
|
|
|
/// Update \c OuterLoop. Before finding irreducible control flow, it was
|
|
|
|
/// partway through \a computeMassInLoop(), so \a LoopData::Exits and \a
|
|
|
|
/// LoopData::BackedgeMass need to be reset. Also, nodes that were packaged
|
|
|
|
/// up need to be removed from \a OuterLoop::Nodes.
|
|
|
|
void updateLoopWithIrreducible(LoopData &OuterLoop);
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Distribute mass according to a distribution.
|
|
|
|
///
|
|
|
|
/// Distributes the mass in Source according to Dist. If LoopHead.isValid(),
|
2014-04-22 03:31:50 +00:00
|
|
|
/// backedges and exits are stored in its entry in Loops.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
|
|
|
/// Mass is distributed in parallel from two copies of the source mass.
|
2014-04-25 04:38:01 +00:00
|
|
|
void distributeMass(const BlockNode &Source, LoopData *OuterLoop,
|
2014-04-21 17:57:07 +00:00
|
|
|
Distribution &Dist);
|
2014-04-18 17:22:25 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Compute the loop scale for a loop.
|
2014-04-25 04:38:01 +00:00
|
|
|
void computeLoopScale(LoopData &Loop);
|
2014-04-18 17:22:25 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Package up a loop.
|
2014-04-25 04:38:01 +00:00
|
|
|
void packageLoop(LoopData &Loop);
|
2014-04-18 22:30:03 +00:00
|
|
|
|
2014-04-25 04:38:17 +00:00
|
|
|
/// \brief Unwrap loops.
|
|
|
|
void unwrapLoops();
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Finalize frequency metrics.
|
|
|
|
///
|
2014-04-25 04:38:17 +00:00
|
|
|
/// Calculates final frequencies and cleans up no-longer-needed data
|
|
|
|
/// structures.
|
2014-04-21 17:57:07 +00:00
|
|
|
void finalizeMetrics();
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Clear all memory.
|
|
|
|
void clear();
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
virtual std::string getBlockName(const BlockNode &Node) const;
|
2014-04-28 20:02:29 +00:00
|
|
|
std::string getLoopName(const LoopData &Loop) const;
|
2014-04-21 17:57:07 +00:00
|
|
|
|
|
|
|
virtual raw_ostream &print(raw_ostream &OS) const { return OS; }
|
|
|
|
void dump() const { print(dbgs()); }
|
|
|
|
|
|
|
|
Float getFloatingBlockFreq(const BlockNode &Node) const;
|
|
|
|
|
|
|
|
BlockFrequency getBlockFreq(const BlockNode &Node) const;
|
|
|
|
|
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockNode &Node) const;
|
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS,
|
|
|
|
const BlockFrequency &Freq) const;
|
|
|
|
|
|
|
|
uint64_t getEntryFreq() const {
|
|
|
|
assert(!Freqs.empty());
|
|
|
|
return Freqs[0].Integer;
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Virtual destructor.
|
|
|
|
///
|
|
|
|
/// Need a virtual destructor to mask the compiler warning about
|
|
|
|
/// getBlockName().
|
|
|
|
virtual ~BlockFrequencyInfoImplBase() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace bfi_detail {
|
|
|
|
template <class BlockT> struct TypeMap {};
|
|
|
|
template <> struct TypeMap<BasicBlock> {
|
|
|
|
typedef BasicBlock BlockT;
|
|
|
|
typedef Function FunctionT;
|
|
|
|
typedef BranchProbabilityInfo BranchProbabilityInfoT;
|
|
|
|
typedef Loop LoopT;
|
|
|
|
typedef LoopInfo LoopInfoT;
|
|
|
|
};
|
|
|
|
template <> struct TypeMap<MachineBasicBlock> {
|
|
|
|
typedef MachineBasicBlock BlockT;
|
|
|
|
typedef MachineFunction FunctionT;
|
|
|
|
typedef MachineBranchProbabilityInfo BranchProbabilityInfoT;
|
|
|
|
typedef MachineLoop LoopT;
|
|
|
|
typedef MachineLoopInfo LoopInfoT;
|
|
|
|
};
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Get the name of a MachineBasicBlock.
|
|
|
|
///
|
|
|
|
/// Get the name of a MachineBasicBlock. It's templated so that including from
|
|
|
|
/// CodeGen is unnecessary (that would be a layering issue).
|
|
|
|
///
|
|
|
|
/// This is used mainly for debug output. The name is similar to
|
|
|
|
/// MachineBasicBlock::getFullName(), but skips the name of the function.
|
|
|
|
template <class BlockT> std::string getBlockName(const BlockT *BB) {
|
|
|
|
assert(BB && "Unexpected nullptr");
|
|
|
|
auto MachineName = "BB" + Twine(BB->getNumber());
|
|
|
|
if (BB->getBasicBlock())
|
|
|
|
return (MachineName + "[" + BB->getName() + "]").str();
|
|
|
|
return MachineName.str();
|
|
|
|
}
|
|
|
|
/// \brief Get the name of a BasicBlock.
|
|
|
|
template <> inline std::string getBlockName(const BasicBlock *BB) {
|
|
|
|
assert(BB && "Unexpected nullptr");
|
|
|
|
return BB->getName().str();
|
|
|
|
}
|
2014-04-28 20:02:29 +00:00
|
|
|
|
|
|
|
/// \brief Graph of irreducible control flow.
|
|
|
|
///
|
|
|
|
/// This graph is used for determining the SCCs in a loop (or top-level
|
|
|
|
/// function) that has irreducible control flow.
|
|
|
|
///
|
|
|
|
/// During the block frequency algorithm, the local graphs are defined in a
|
|
|
|
/// light-weight way, deferring to the \a BasicBlock or \a MachineBasicBlock
|
|
|
|
/// graphs for most edges, but getting others from \a LoopData::ExitMap. The
|
|
|
|
/// latter only has successor information.
|
|
|
|
///
|
|
|
|
/// \a IrreducibleGraph makes this graph explicit. It's in a form that can use
|
|
|
|
/// \a GraphTraits (so that \a analyzeIrreducible() can use \a scc_iterator),
|
|
|
|
/// and it explicitly lists predecessors and successors. The initialization
|
|
|
|
/// that relies on \c MachineBasicBlock is defined in the header.
|
|
|
|
struct IrreducibleGraph {
|
|
|
|
typedef BlockFrequencyInfoImplBase BFIBase;
|
|
|
|
|
|
|
|
BFIBase &BFI;
|
|
|
|
|
|
|
|
typedef BFIBase::BlockNode BlockNode;
|
|
|
|
struct IrrNode {
|
|
|
|
BlockNode Node;
|
|
|
|
unsigned NumIn;
|
|
|
|
std::deque<const IrrNode *> Edges;
|
|
|
|
IrrNode(const BlockNode &Node) : Node(Node), NumIn(0) {}
|
|
|
|
|
2014-04-28 20:08:23 +00:00
|
|
|
typedef std::deque<const IrrNode *>::const_iterator iterator;
|
2014-04-28 20:02:29 +00:00
|
|
|
iterator pred_begin() const { return Edges.begin(); }
|
|
|
|
iterator succ_begin() const { return Edges.begin() + NumIn; }
|
|
|
|
iterator pred_end() const { return succ_begin(); }
|
|
|
|
iterator succ_end() const { return Edges.end(); }
|
|
|
|
};
|
|
|
|
BlockNode Start;
|
|
|
|
const IrrNode *StartIrr;
|
|
|
|
std::vector<IrrNode> Nodes;
|
|
|
|
SmallDenseMap<uint32_t, IrrNode *, 4> Lookup;
|
|
|
|
|
|
|
|
/// \brief Construct an explicit graph containing irreducible control flow.
|
|
|
|
///
|
|
|
|
/// Construct an explicit graph of the control flow in \c OuterLoop (or the
|
|
|
|
/// top-level function, if \c OuterLoop is \c nullptr). Uses \c
|
|
|
|
/// addBlockEdges to add block successors that have not been packaged into
|
|
|
|
/// loops.
|
|
|
|
///
|
|
|
|
/// \a BlockFrequencyInfoImpl::computeIrreducibleMass() is the only expected
|
|
|
|
/// user of this.
|
|
|
|
template <class BlockEdgesAdder>
|
|
|
|
IrreducibleGraph(BFIBase &BFI, const BFIBase::LoopData *OuterLoop,
|
|
|
|
BlockEdgesAdder addBlockEdges)
|
|
|
|
: BFI(BFI), StartIrr(nullptr) {
|
|
|
|
initialize(OuterLoop, addBlockEdges);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BlockEdgesAdder>
|
|
|
|
void initialize(const BFIBase::LoopData *OuterLoop,
|
|
|
|
BlockEdgesAdder addBlockEdges);
|
|
|
|
void addNodesInLoop(const BFIBase::LoopData &OuterLoop);
|
|
|
|
void addNodesInFunction();
|
|
|
|
void addNode(const BlockNode &Node) {
|
|
|
|
Nodes.emplace_back(Node);
|
|
|
|
BFI.Working[Node.Index].getMass() = BlockMass::getEmpty();
|
|
|
|
}
|
|
|
|
void indexNodes();
|
|
|
|
template <class BlockEdgesAdder>
|
|
|
|
void addEdges(const BlockNode &Node, const BFIBase::LoopData *OuterLoop,
|
|
|
|
BlockEdgesAdder addBlockEdges);
|
|
|
|
void addEdge(IrrNode &Irr, const BlockNode &Succ,
|
|
|
|
const BFIBase::LoopData *OuterLoop);
|
|
|
|
};
|
|
|
|
template <class BlockEdgesAdder>
|
|
|
|
void IrreducibleGraph::initialize(const BFIBase::LoopData *OuterLoop,
|
|
|
|
BlockEdgesAdder addBlockEdges) {
|
|
|
|
if (OuterLoop) {
|
|
|
|
addNodesInLoop(*OuterLoop);
|
|
|
|
for (auto N : OuterLoop->Nodes)
|
|
|
|
addEdges(N, OuterLoop, addBlockEdges);
|
|
|
|
} else {
|
|
|
|
addNodesInFunction();
|
|
|
|
for (uint32_t Index = 0; Index < BFI.Working.size(); ++Index)
|
|
|
|
addEdges(Index, OuterLoop, addBlockEdges);
|
|
|
|
}
|
|
|
|
StartIrr = Lookup[Start.Index];
|
|
|
|
}
|
|
|
|
template <class BlockEdgesAdder>
|
|
|
|
void IrreducibleGraph::addEdges(const BlockNode &Node,
|
|
|
|
const BFIBase::LoopData *OuterLoop,
|
|
|
|
BlockEdgesAdder addBlockEdges) {
|
|
|
|
auto L = Lookup.find(Node.Index);
|
|
|
|
if (L == Lookup.end())
|
|
|
|
return;
|
|
|
|
IrrNode &Irr = *L->second;
|
|
|
|
const auto &Working = BFI.Working[Node.Index];
|
|
|
|
|
|
|
|
if (Working.isAPackage())
|
|
|
|
for (const auto &I : Working.Loop->Exits)
|
|
|
|
addEdge(Irr, I.first, OuterLoop);
|
|
|
|
else
|
|
|
|
addBlockEdges(*this, Irr, OuterLoop);
|
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Shared implementation for block frequency analysis.
|
|
|
|
///
|
|
|
|
/// This is a shared implementation of BlockFrequencyInfo and
|
|
|
|
/// MachineBlockFrequencyInfo, and calculates the relative frequencies of
|
|
|
|
/// blocks.
|
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// LoopInfo defines a loop as a "non-trivial" SCC dominated by a single block,
|
|
|
|
/// which is called the header. A given loop, L, can have sub-loops, which are
|
|
|
|
/// loops within the subgraph of L that exclude its header. (A "trivial" SCC
|
|
|
|
/// consists of a single block that does not have a self-edge.)
|
|
|
|
///
|
|
|
|
/// In addition to loops, this algorithm has limited support for irreducible
|
|
|
|
/// SCCs, which are SCCs with multiple entry blocks. Irreducible SCCs are
|
|
|
|
/// discovered on they fly, and modelled as loops with multiple headers.
|
|
|
|
///
|
|
|
|
/// The headers of irreducible sub-SCCs consist of its entry blocks and all
|
|
|
|
/// nodes that are targets of a backedge within it (excluding backedges within
|
|
|
|
/// true sub-loops). Block frequency calculations act as if a block is
|
|
|
|
/// inserted that intercepts all the edges to the headers. All backedges and
|
|
|
|
/// entries point to this block. Its successors are the headers, which split
|
|
|
|
/// the frequency evenly.
|
|
|
|
///
|
2014-04-21 18:31:58 +00:00
|
|
|
/// This algorithm leverages BlockMass and UnsignedFloat to maintain precision,
|
2014-04-21 17:57:07 +00:00
|
|
|
/// separates mass distribution from loop scaling, and dithers to eliminate
|
|
|
|
/// probability mass loss.
|
|
|
|
///
|
|
|
|
/// The implementation is split between BlockFrequencyInfoImpl, which knows the
|
|
|
|
/// type of graph being modelled (BasicBlock vs. MachineBasicBlock), and
|
|
|
|
/// BlockFrequencyInfoImplBase, which doesn't. The base class uses \a
|
|
|
|
/// BlockNode, a wrapper around a uint32_t. BlockNode is numbered from 0 in
|
|
|
|
/// reverse-post order. This gives two advantages: it's easy to compare the
|
|
|
|
/// relative ordering of two nodes, and maps keyed on BlockT can be represented
|
|
|
|
/// by vectors.
|
|
|
|
///
|
|
|
|
/// This algorithm is O(V+E), unless there is irreducible control flow, in
|
|
|
|
/// which case it's O(V*E) in the worst case.
|
|
|
|
///
|
|
|
|
/// These are the main stages:
|
|
|
|
///
|
|
|
|
/// 0. Reverse post-order traversal (\a initializeRPOT()).
|
|
|
|
///
|
|
|
|
/// Run a single post-order traversal and save it (in reverse) in RPOT.
|
|
|
|
/// All other stages make use of this ordering. Save a lookup from BlockT
|
|
|
|
/// to BlockNode (the index into RPOT) in Nodes.
|
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// 1. Loop initialization (\a initializeLoops()).
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
|
|
|
/// Translate LoopInfo/MachineLoopInfo into a form suitable for the rest of
|
|
|
|
/// the algorithm. In particular, store the immediate members of each loop
|
|
|
|
/// in reverse post-order.
|
|
|
|
///
|
|
|
|
/// 2. Calculate mass and scale in loops (\a computeMassInLoops()).
|
|
|
|
///
|
|
|
|
/// For each loop (bottom-up), distribute mass through the DAG resulting
|
|
|
|
/// from ignoring backedges and treating sub-loops as a single pseudo-node.
|
|
|
|
/// Track the backedge mass distributed to the loop header, and use it to
|
2014-04-28 20:02:29 +00:00
|
|
|
/// calculate the loop scale (number of loop iterations). Immediate
|
|
|
|
/// members that represent sub-loops will already have been visited and
|
|
|
|
/// packaged into a pseudo-node.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
|
|
|
/// Distributing mass in a loop is a reverse-post-order traversal through
|
|
|
|
/// the loop. Start by assigning full mass to the Loop header. For each
|
|
|
|
/// node in the loop:
|
|
|
|
///
|
|
|
|
/// - Fetch and categorize the weight distribution for its successors.
|
|
|
|
/// If this is a packaged-subloop, the weight distribution is stored
|
2014-04-22 03:31:31 +00:00
|
|
|
/// in \a LoopData::Exits. Otherwise, fetch it from
|
2014-04-21 17:57:07 +00:00
|
|
|
/// BranchProbabilityInfo.
|
|
|
|
///
|
2014-04-25 04:38:43 +00:00
|
|
|
/// - Each successor is categorized as \a Weight::Local, a local edge
|
|
|
|
/// within the current loop, \a Weight::Backedge, a backedge to the
|
|
|
|
/// loop header, or \a Weight::Exit, any successor outside the loop.
|
|
|
|
/// The weight, the successor, and its category are stored in \a
|
|
|
|
/// Distribution. There can be multiple edges to each successor.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// - If there's a backedge to a non-header, there's an irreducible SCC.
|
|
|
|
/// The usual flow is temporarily aborted. \a
|
|
|
|
/// computeIrreducibleMass() finds the irreducible SCCs within the
|
|
|
|
/// loop, packages them up, and restarts the flow.
|
|
|
|
///
|
2014-04-21 17:57:07 +00:00
|
|
|
/// - Normalize the distribution: scale weights down so that their sum
|
|
|
|
/// is 32-bits, and coalesce multiple edges to the same node.
|
|
|
|
///
|
|
|
|
/// - Distribute the mass accordingly, dithering to minimize mass loss,
|
2014-04-25 04:38:43 +00:00
|
|
|
/// as described in \a distributeMass().
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
|
|
|
/// Finally, calculate the loop scale from the accumulated backedge mass.
|
|
|
|
///
|
|
|
|
/// 3. Distribute mass in the function (\a computeMassInFunction()).
|
|
|
|
///
|
|
|
|
/// Finally, distribute mass through the DAG resulting from packaging all
|
|
|
|
/// loops in the function. This uses the same algorithm as distributing
|
|
|
|
/// mass in a loop, except that there are no exit or backedge edges.
|
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// 4. Unpackage loops (\a unwrapLoops()).
|
|
|
|
///
|
|
|
|
/// Initialize each block's frequency to a floating point representation of
|
|
|
|
/// its mass.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// Visit loops top-down, scaling the frequencies of its immediate members
|
|
|
|
/// by the loop's pseudo-node's frequency.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// 5. Convert frequencies to a 64-bit range (\a finalizeMetrics()).
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
|
|
|
/// Using the min and max frequencies as a guide, translate floating point
|
|
|
|
/// frequencies to an appropriate range in uint64_t.
|
|
|
|
///
|
|
|
|
/// It has some known flaws.
|
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// - Loop scale is limited to 4096 per loop (2^12) to avoid exhausting
|
|
|
|
/// BlockFrequency's 64-bit integer precision.
|
|
|
|
///
|
|
|
|
/// - The model of irreducible control flow is a rough approximation.
|
2014-04-21 17:57:07 +00:00
|
|
|
///
|
|
|
|
/// Modelling irreducible control flow exactly involves setting up and
|
|
|
|
/// solving a group of infinite geometric series. Such precision is
|
|
|
|
/// unlikely to be worthwhile, since most of our algorithms give up on
|
|
|
|
/// irreducible control flow anyway.
|
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// Nevertheless, we might find that we need to get closer. Here's a sort
|
|
|
|
/// of TODO list for the model with diminishing returns, to be completed as
|
|
|
|
/// necessary.
|
2014-04-25 23:08:57 +00:00
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// - The headers for the \a LoopData representing an irreducible SCC
|
|
|
|
/// include non-entry blocks. When these extra blocks exist, they
|
|
|
|
/// indicate a self-contained irreducible sub-SCC. We could treat them
|
|
|
|
/// as sub-loops, rather than arbitrarily shoving the problematic
|
|
|
|
/// blocks into the headers of the main irreducible SCC.
|
|
|
|
///
|
|
|
|
/// - Backedge frequencies are assumed to be evenly split between the
|
|
|
|
/// headers of a given irreducible SCC. Instead, we could track the
|
|
|
|
/// backedge mass separately for each header, and adjust their relative
|
|
|
|
/// frequencies.
|
|
|
|
///
|
|
|
|
/// - Entry frequencies are assumed to be evenly split between the
|
|
|
|
/// headers of a given irreducible SCC, which is the only option if we
|
|
|
|
/// need to compute mass in the SCC before its parent loop. Instead,
|
|
|
|
/// we could partially compute mass in the parent loop, and stop when
|
|
|
|
/// we get to the SCC. Here, we have the correct ratio of entry
|
|
|
|
/// masses, which we can use to adjust their relative frequencies.
|
|
|
|
/// Compute mass in the SCC, and then continue propagation in the
|
|
|
|
/// parent.
|
|
|
|
///
|
|
|
|
/// - We can propagate mass iteratively through the SCC, for some fixed
|
|
|
|
/// number of iterations. Each iteration starts by assigning the entry
|
|
|
|
/// blocks their backedge mass from the prior iteration. The final
|
|
|
|
/// mass for each block (and each exit, and the total backedge mass
|
|
|
|
/// used for computing loop scale) is the sum of all iterations.
|
|
|
|
/// (Running this until fixed point would "solve" the geometric
|
|
|
|
/// series by simulation.)
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class BT> class BlockFrequencyInfoImpl : BlockFrequencyInfoImplBase {
|
|
|
|
typedef typename bfi_detail::TypeMap<BT>::BlockT BlockT;
|
|
|
|
typedef typename bfi_detail::TypeMap<BT>::FunctionT FunctionT;
|
|
|
|
typedef typename bfi_detail::TypeMap<BT>::BranchProbabilityInfoT
|
|
|
|
BranchProbabilityInfoT;
|
|
|
|
typedef typename bfi_detail::TypeMap<BT>::LoopT LoopT;
|
|
|
|
typedef typename bfi_detail::TypeMap<BT>::LoopInfoT LoopInfoT;
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-28 20:02:29 +00:00
|
|
|
// This is part of a workaround for a GCC 4.7 crash on lambdas.
|
|
|
|
friend struct bfi_detail::BlockEdgesAdder<BT>;
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
typedef GraphTraits<const BlockT *> Successor;
|
|
|
|
typedef GraphTraits<Inverse<const BlockT *>> Predecessor;
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
const BranchProbabilityInfoT *BPI;
|
|
|
|
const LoopInfoT *LI;
|
|
|
|
const FunctionT *F;
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// All blocks in reverse postorder.
|
|
|
|
std::vector<const BlockT *> RPOT;
|
|
|
|
DenseMap<const BlockT *, BlockNode> Nodes;
|
blockfreq: Rewrite BlockFrequencyInfoImpl
Rewrite the shared implementation of BlockFrequencyInfo and
MachineBlockFrequencyInfo entirely.
The old implementation had a fundamental flaw: precision losses from
nested loops (or very wide branches) compounded past loop exits (and
convergence points).
The @nested_loops testcase at the end of
test/Analysis/BlockFrequencyAnalysis/basic.ll is motivating. This
function has three nested loops, with branch weights in the loop headers
of 1:4000 (exit:continue). The old analysis gives non-sensical results:
Printing analysis 'Block Frequency Analysis' for function 'nested_loops':
---- Block Freqs ----
entry = 1.0
for.cond1.preheader = 1.00103
for.cond4.preheader = 5.5222
for.body6 = 18095.19995
for.inc8 = 4.52264
for.inc11 = 0.00109
for.end13 = 0.0
The new analysis gives correct results:
Printing analysis 'Block Frequency Analysis' for function 'nested_loops':
block-frequency-info: nested_loops
- entry: float = 1.0, int = 8
- for.cond1.preheader: float = 4001.0, int = 32007
- for.cond4.preheader: float = 16008001.0, int = 128064007
- for.body6: float = 64048012001.0, int = 512384096007
- for.inc8: float = 16008001.0, int = 128064007
- for.inc11: float = 4001.0, int = 32007
- for.end13: float = 1.0, int = 8
Most importantly, the frequency leaving each loop matches the frequency
entering it.
The new algorithm leverages BlockMass and PositiveFloat to maintain
precision, separates "probability mass distribution" from "loop
scaling", and uses dithering to eliminate probability mass loss. I have
unit tests for these types out of tree, but it was decided in the review
to make the classes private to BlockFrequencyInfoImpl, and try to shrink
them (or remove them entirely) in follow-up commits.
The new algorithm should generally have a complexity advantage over the
old. The previous algorithm was quadratic in the worst case. The new
algorithm is still worst-case quadratic in the presence of irreducible
control flow, but it's linear without it.
The key difference between the old algorithm and the new is that control
flow within a loop is evaluated separately from control flow outside,
limiting propagation of precision problems and allowing loop scale to be
calculated independently of mass distribution. Loops are visited
bottom-up, their loop scales are calculated, and they are replaced by
pseudo-nodes. Mass is then distributed through the function, which is
now a DAG. Finally, loops are revisited top-down to multiply through
the loop scales and the masses distributed to pseudo nodes.
There are some remaining flaws.
- Irreducible control flow isn't modelled correctly. LoopInfo and
MachineLoopInfo ignore irreducible edges, so this algorithm will
fail to scale accordingly. There's a note in the class
documentation about how to get closer. See also the comments in
test/Analysis/BlockFrequencyInfo/irreducible.ll.
- Loop scale is limited to 4096 per loop (2^12) to avoid exhausting
the 64-bit integer precision used downstream.
- The "bias" calculation proposed on llvmdev is *not* incorporated
here. This will be added in a follow-up commit, once comments from
this review have been handled.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206548 91177308-0d34-0410-b5e6-96231b3b80d8
2014-04-18 01:57:45 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
typedef typename std::vector<const BlockT *>::const_iterator rpot_iterator;
|
2011-06-23 21:56:59 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
rpot_iterator rpot_begin() const { return RPOT.begin(); }
|
|
|
|
rpot_iterator rpot_end() const { return RPOT.end(); }
|
2014-04-18 17:22:25 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
size_t getIndex(const rpot_iterator &I) const { return I - rpot_begin(); }
|
blockfreq: Rewrite BlockFrequencyInfoImpl
Rewrite the shared implementation of BlockFrequencyInfo and
MachineBlockFrequencyInfo entirely.
The old implementation had a fundamental flaw: precision losses from
nested loops (or very wide branches) compounded past loop exits (and
convergence points).
The @nested_loops testcase at the end of
test/Analysis/BlockFrequencyAnalysis/basic.ll is motivating. This
function has three nested loops, with branch weights in the loop headers
of 1:4000 (exit:continue). The old analysis gives non-sensical results:
Printing analysis 'Block Frequency Analysis' for function 'nested_loops':
---- Block Freqs ----
entry = 1.0
for.cond1.preheader = 1.00103
for.cond4.preheader = 5.5222
for.body6 = 18095.19995
for.inc8 = 4.52264
for.inc11 = 0.00109
for.end13 = 0.0
The new analysis gives correct results:
Printing analysis 'Block Frequency Analysis' for function 'nested_loops':
block-frequency-info: nested_loops
- entry: float = 1.0, int = 8
- for.cond1.preheader: float = 4001.0, int = 32007
- for.cond4.preheader: float = 16008001.0, int = 128064007
- for.body6: float = 64048012001.0, int = 512384096007
- for.inc8: float = 16008001.0, int = 128064007
- for.inc11: float = 4001.0, int = 32007
- for.end13: float = 1.0, int = 8
Most importantly, the frequency leaving each loop matches the frequency
entering it.
The new algorithm leverages BlockMass and PositiveFloat to maintain
precision, separates "probability mass distribution" from "loop
scaling", and uses dithering to eliminate probability mass loss. I have
unit tests for these types out of tree, but it was decided in the review
to make the classes private to BlockFrequencyInfoImpl, and try to shrink
them (or remove them entirely) in follow-up commits.
The new algorithm should generally have a complexity advantage over the
old. The previous algorithm was quadratic in the worst case. The new
algorithm is still worst-case quadratic in the presence of irreducible
control flow, but it's linear without it.
The key difference between the old algorithm and the new is that control
flow within a loop is evaluated separately from control flow outside,
limiting propagation of precision problems and allowing loop scale to be
calculated independently of mass distribution. Loops are visited
bottom-up, their loop scales are calculated, and they are replaced by
pseudo-nodes. Mass is then distributed through the function, which is
now a DAG. Finally, loops are revisited top-down to multiply through
the loop scales and the masses distributed to pseudo nodes.
There are some remaining flaws.
- Irreducible control flow isn't modelled correctly. LoopInfo and
MachineLoopInfo ignore irreducible edges, so this algorithm will
fail to scale accordingly. There's a note in the class
documentation about how to get closer. See also the comments in
test/Analysis/BlockFrequencyInfo/irreducible.ll.
- Loop scale is limited to 4096 per loop (2^12) to avoid exhausting
the 64-bit integer precision used downstream.
- The "bias" calculation proposed on llvmdev is *not* incorporated
here. This will be added in a follow-up commit, once comments from
this review have been handled.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206548 91177308-0d34-0410-b5e6-96231b3b80d8
2014-04-18 01:57:45 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
BlockNode getNode(const rpot_iterator &I) const {
|
|
|
|
return BlockNode(getIndex(I));
|
|
|
|
}
|
|
|
|
BlockNode getNode(const BlockT *BB) const { return Nodes.lookup(BB); }
|
2014-04-18 22:30:03 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
const BlockT *getBlock(const BlockNode &Node) const {
|
|
|
|
assert(Node.Index < RPOT.size());
|
|
|
|
return RPOT[Node.Index];
|
|
|
|
}
|
2014-04-18 22:30:03 +00:00
|
|
|
|
2014-04-25 04:38:32 +00:00
|
|
|
/// \brief Run (and save) a post-order traversal.
|
|
|
|
///
|
|
|
|
/// Saves a reverse post-order traversal of all the nodes in \a F.
|
2014-04-21 17:57:07 +00:00
|
|
|
void initializeRPOT();
|
2014-04-25 04:38:32 +00:00
|
|
|
|
|
|
|
/// \brief Initialize loop data.
|
|
|
|
///
|
|
|
|
/// Build up \a Loops using \a LoopInfo. \a LoopInfo gives us a mapping from
|
|
|
|
/// each block to the deepest loop it's in, but we need the inverse. For each
|
|
|
|
/// loop, we store in reverse post-order its "immediate" members, defined as
|
|
|
|
/// the header, the headers of immediate sub-loops, and all other blocks in
|
|
|
|
/// the loop that are not in sub-loops.
|
2014-04-21 17:57:07 +00:00
|
|
|
void initializeLoops();
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-25 04:38:32 +00:00
|
|
|
/// \brief Propagate to a block's successors.
|
|
|
|
///
|
|
|
|
/// In the context of distributing mass through \c OuterLoop, divide the mass
|
|
|
|
/// currently assigned to \c Node between its successors.
|
2014-04-28 20:02:29 +00:00
|
|
|
///
|
|
|
|
/// \return \c true unless there's an irreducible backedge.
|
|
|
|
bool propagateMassToSuccessors(LoopData *OuterLoop, const BlockNode &Node);
|
2014-04-25 04:38:32 +00:00
|
|
|
|
|
|
|
/// \brief Compute mass in a particular loop.
|
|
|
|
///
|
|
|
|
/// Assign mass to \c Loop's header, and then for each block in \c Loop in
|
|
|
|
/// reverse post-order, distribute mass to its successors. Only visits nodes
|
|
|
|
/// that have not been packaged into sub-loops.
|
|
|
|
///
|
|
|
|
/// \pre \a computeMassInLoop() has been called for each subloop of \c Loop.
|
2014-04-28 20:02:29 +00:00
|
|
|
/// \return \c true unless there's an irreducible backedge.
|
|
|
|
bool computeMassInLoop(LoopData &Loop);
|
|
|
|
|
|
|
|
/// \brief Try to compute mass in the top-level function.
|
|
|
|
///
|
|
|
|
/// Assign mass to the entry block, and then for each block in reverse
|
|
|
|
/// post-order, distribute mass to its successors. Skips nodes that have
|
|
|
|
/// been packaged into loops.
|
|
|
|
///
|
|
|
|
/// \pre \a computeMassInLoops() has been called.
|
|
|
|
/// \return \c true unless there's an irreducible backedge.
|
|
|
|
bool tryToComputeMassInFunction();
|
|
|
|
|
|
|
|
/// \brief Compute mass in (and package up) irreducible SCCs.
|
|
|
|
///
|
|
|
|
/// Find the irreducible SCCs in \c OuterLoop, add them to \a Loops (in front
|
|
|
|
/// of \c Insert), and call \a computeMassInLoop() on each of them.
|
|
|
|
///
|
|
|
|
/// If \c OuterLoop is \c nullptr, it refers to the top-level function.
|
|
|
|
///
|
|
|
|
/// \pre \a computeMassInLoop() has been called for each subloop of \c
|
|
|
|
/// OuterLoop.
|
|
|
|
/// \pre \c Insert points at the the last loop successfully processed by \a
|
|
|
|
/// computeMassInLoop().
|
|
|
|
/// \pre \c OuterLoop has irreducible SCCs.
|
|
|
|
void computeIrreducibleMass(LoopData *OuterLoop,
|
|
|
|
std::list<LoopData>::iterator Insert);
|
2014-04-25 04:38:32 +00:00
|
|
|
|
|
|
|
/// \brief Compute mass in all loops.
|
|
|
|
///
|
|
|
|
/// For each loop bottom-up, call \a computeMassInLoop().
|
2014-04-28 20:02:29 +00:00
|
|
|
///
|
|
|
|
/// \a computeMassInLoop() aborts (and returns \c false) on loops that
|
|
|
|
/// contain a irreducible sub-SCCs. Use \a computeIrreducibleMass() and then
|
|
|
|
/// re-enter \a computeMassInLoop().
|
|
|
|
///
|
|
|
|
/// \post \a computeMassInLoop() has returned \c true for every loop.
|
2014-04-25 04:38:32 +00:00
|
|
|
void computeMassInLoops();
|
|
|
|
|
|
|
|
/// \brief Compute mass in the top-level function.
|
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// Uses \a tryToComputeMassInFunction() and \a computeIrreducibleMass() to
|
|
|
|
/// compute mass in the top-level function.
|
2014-04-25 04:38:32 +00:00
|
|
|
///
|
2014-04-28 20:02:29 +00:00
|
|
|
/// \post \a tryToComputeMassInFunction() has returned \c true.
|
2014-04-21 17:57:07 +00:00
|
|
|
void computeMassInFunction();
|
|
|
|
|
|
|
|
std::string getBlockName(const BlockNode &Node) const override {
|
|
|
|
return bfi_detail::getBlockName(getBlock(Node));
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-04-21 17:57:07 +00:00
|
|
|
const FunctionT *getFunction() const { return F; }
|
2013-12-13 23:44:36 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
void doFunction(const FunctionT *F, const BranchProbabilityInfoT *BPI,
|
|
|
|
const LoopInfoT *LI);
|
2014-04-28 04:05:08 +00:00
|
|
|
BlockFrequencyInfoImpl() : BPI(nullptr), LI(nullptr), F(nullptr) {}
|
2013-12-13 23:44:36 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
using BlockFrequencyInfoImplBase::getEntryFreq;
|
2011-12-20 20:03:10 +00:00
|
|
|
BlockFrequency getBlockFreq(const BlockT *BB) const {
|
2014-04-21 17:57:07 +00:00
|
|
|
return BlockFrequencyInfoImplBase::getBlockFreq(getNode(BB));
|
|
|
|
}
|
|
|
|
Float getFloatingBlockFreq(const BlockT *BB) const {
|
|
|
|
return BlockFrequencyInfoImplBase::getFloatingBlockFreq(getNode(BB));
|
2014-04-18 17:22:25 +00:00
|
|
|
}
|
2014-04-18 22:30:03 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
/// \brief Print the frequencies for the current function.
|
|
|
|
///
|
|
|
|
/// Prints the frequencies for the blocks in the current function.
|
|
|
|
///
|
|
|
|
/// Blocks are printed in the natural iteration order of the function, rather
|
|
|
|
/// than reverse post-order. This provides two advantages: writing -analyze
|
|
|
|
/// tests is easier (since blocks come out in source order), and even
|
|
|
|
/// unreachable blocks are printed.
|
|
|
|
///
|
|
|
|
/// \a BlockFrequencyInfoImplBase::print() only knows reverse post-order, so
|
|
|
|
/// we need to override it here.
|
|
|
|
raw_ostream &print(raw_ostream &OS) const override;
|
|
|
|
using BlockFrequencyInfoImplBase::dump;
|
|
|
|
|
|
|
|
using BlockFrequencyInfoImplBase::printBlockFreq;
|
|
|
|
raw_ostream &printBlockFreq(raw_ostream &OS, const BlockT *BB) const {
|
|
|
|
return BlockFrequencyInfoImplBase::printBlockFreq(OS, getNode(BB));
|
2014-04-18 22:30:03 +00:00
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class BT>
|
|
|
|
void BlockFrequencyInfoImpl<BT>::doFunction(const FunctionT *F,
|
|
|
|
const BranchProbabilityInfoT *BPI,
|
|
|
|
const LoopInfoT *LI) {
|
|
|
|
// Save the parameters.
|
|
|
|
this->BPI = BPI;
|
|
|
|
this->LI = LI;
|
|
|
|
this->F = F;
|
|
|
|
|
|
|
|
// Clean up left-over data structures.
|
|
|
|
BlockFrequencyInfoImplBase::clear();
|
|
|
|
RPOT.clear();
|
|
|
|
Nodes.clear();
|
|
|
|
|
|
|
|
// Initialize.
|
|
|
|
DEBUG(dbgs() << "\nblock-frequency: " << F->getName() << "\n================="
|
|
|
|
<< std::string(F->getName().size(), '=') << "\n");
|
|
|
|
initializeRPOT();
|
|
|
|
initializeLoops();
|
|
|
|
|
|
|
|
// Visit loops in post-order to find thelocal mass distribution, and then do
|
|
|
|
// the full function.
|
|
|
|
computeMassInLoops();
|
|
|
|
computeMassInFunction();
|
2014-04-25 04:38:17 +00:00
|
|
|
unwrapLoops();
|
2014-04-21 17:57:07 +00:00
|
|
|
finalizeMetrics();
|
|
|
|
}
|
2014-04-18 22:30:03 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class BT> void BlockFrequencyInfoImpl<BT>::initializeRPOT() {
|
|
|
|
const BlockT *Entry = F->begin();
|
|
|
|
RPOT.reserve(F->size());
|
|
|
|
std::copy(po_begin(Entry), po_end(Entry), std::back_inserter(RPOT));
|
|
|
|
std::reverse(RPOT.begin(), RPOT.end());
|
|
|
|
|
|
|
|
assert(RPOT.size() - 1 <= BlockNode::getMaxIndex() &&
|
|
|
|
"More nodes in function than Block Frequency Info supports");
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "reverse-post-order-traversal\n");
|
|
|
|
for (rpot_iterator I = rpot_begin(), E = rpot_end(); I != E; ++I) {
|
|
|
|
BlockNode Node = getNode(I);
|
|
|
|
DEBUG(dbgs() << " - " << getIndex(I) << ": " << getBlockName(Node) << "\n");
|
|
|
|
Nodes[*I] = Node;
|
2014-04-18 22:30:03 +00:00
|
|
|
}
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-25 04:38:03 +00:00
|
|
|
Working.reserve(RPOT.size());
|
|
|
|
for (size_t Index = 0; Index < RPOT.size(); ++Index)
|
|
|
|
Working.emplace_back(Index);
|
2014-04-21 17:57:07 +00:00
|
|
|
Freqs.resize(RPOT.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BT> void BlockFrequencyInfoImpl<BT>::initializeLoops() {
|
|
|
|
DEBUG(dbgs() << "loop-detection\n");
|
|
|
|
if (LI->empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Visit loops top down and assign them an index.
|
2014-04-25 04:38:03 +00:00
|
|
|
std::deque<std::pair<const LoopT *, LoopData *>> Q;
|
|
|
|
for (const LoopT *L : *LI)
|
|
|
|
Q.emplace_back(L, nullptr);
|
2014-04-21 17:57:07 +00:00
|
|
|
while (!Q.empty()) {
|
2014-04-25 04:38:03 +00:00
|
|
|
const LoopT *Loop = Q.front().first;
|
|
|
|
LoopData *Parent = Q.front().second;
|
2014-04-21 17:57:07 +00:00
|
|
|
Q.pop_front();
|
|
|
|
|
|
|
|
BlockNode Header = getNode(Loop->getHeader());
|
|
|
|
assert(Header.isValid());
|
|
|
|
|
2014-04-25 04:38:03 +00:00
|
|
|
Loops.emplace_back(Parent, Header);
|
2014-04-25 04:30:06 +00:00
|
|
|
Working[Header.Index].Loop = &Loops.back();
|
2014-04-21 17:57:07 +00:00
|
|
|
DEBUG(dbgs() << " - loop = " << getBlockName(Header) << "\n");
|
2014-04-25 04:38:03 +00:00
|
|
|
|
|
|
|
for (const LoopT *L : *Loop)
|
|
|
|
Q.emplace_back(L, &Loops.back());
|
2014-04-19 22:34:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
// Visit nodes in reverse post-order and add them to their deepest containing
|
|
|
|
// loop.
|
|
|
|
for (size_t Index = 0; Index < RPOT.size(); ++Index) {
|
2014-04-25 04:38:03 +00:00
|
|
|
// Loop headers have already been mostly mapped.
|
|
|
|
if (Working[Index].isLoopHeader()) {
|
|
|
|
LoopData *ContainingLoop = Working[Index].getContainingLoop();
|
|
|
|
if (ContainingLoop)
|
2014-04-25 04:38:09 +00:00
|
|
|
ContainingLoop->Nodes.push_back(Index);
|
2014-04-25 04:38:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
const LoopT *Loop = LI->getLoopFor(RPOT[Index]);
|
|
|
|
if (!Loop)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add this node to its containing loop's member list.
|
|
|
|
BlockNode Header = getNode(Loop->getHeader());
|
|
|
|
assert(Header.isValid());
|
|
|
|
const auto &HeaderData = Working[Header.Index];
|
|
|
|
assert(HeaderData.isLoopHeader());
|
|
|
|
|
2014-04-25 04:38:03 +00:00
|
|
|
Working[Index].Loop = HeaderData.Loop;
|
2014-04-25 04:38:09 +00:00
|
|
|
HeaderData.Loop->Nodes.push_back(Index);
|
2014-04-21 17:57:07 +00:00
|
|
|
DEBUG(dbgs() << " - loop = " << getBlockName(Header)
|
|
|
|
<< ": member = " << getBlockName(Index) << "\n");
|
2014-04-19 22:34:26 +00:00
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2014-04-19 22:34:26 +00:00
|
|
|
|
2014-04-21 17:57:07 +00:00
|
|
|
template <class BT> void BlockFrequencyInfoImpl<BT>::computeMassInLoops() {
|
|
|
|
// Visit loops with the deepest first, and the top-level loops last.
|
2014-04-28 20:02:29 +00:00
|
|
|
for (auto L = Loops.rbegin(), E = Loops.rend(); L != E; ++L) {
|
|
|
|
if (computeMassInLoop(*L))
|
|
|
|
continue;
|
|
|
|
auto Next = std::next(L);
|
|
|
|
computeIrreducibleMass(&*L, L.base());
|
|
|
|
L = std::prev(Next);
|
|
|
|
if (computeMassInLoop(*L))
|
|
|
|
continue;
|
|
|
|
llvm_unreachable("unhandled irreducible control flow");
|
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BT>
|
2014-04-28 20:02:29 +00:00
|
|
|
bool BlockFrequencyInfoImpl<BT>::computeMassInLoop(LoopData &Loop) {
|
2014-04-21 17:57:07 +00:00
|
|
|
// Compute mass in loop.
|
2014-04-28 20:02:29 +00:00
|
|
|
DEBUG(dbgs() << "compute-mass-in-loop: " << getLoopName(Loop) << "\n");
|
|
|
|
|
|
|
|
if (Loop.isIrreducible()) {
|
|
|
|
BlockMass Remaining = BlockMass::getFull();
|
|
|
|
for (uint32_t H = 0; H < Loop.NumHeaders; ++H) {
|
|
|
|
auto &Mass = Working[Loop.Nodes[H].Index].getMass();
|
|
|
|
Mass = Remaining * BranchProbability(1, Loop.NumHeaders - H);
|
|
|
|
Remaining -= Mass;
|
|
|
|
}
|
|
|
|
for (const BlockNode &M : Loop.Nodes)
|
|
|
|
if (!propagateMassToSuccessors(&Loop, M))
|
|
|
|
llvm_unreachable("unhandled irreducible control flow");
|
|
|
|
} else {
|
|
|
|
Working[Loop.getHeader().Index].getMass() = BlockMass::getFull();
|
|
|
|
if (!propagateMassToSuccessors(&Loop, Loop.getHeader()))
|
|
|
|
llvm_unreachable("irreducible control flow to loop header!?");
|
|
|
|
for (const BlockNode &M : Loop.members())
|
|
|
|
if (!propagateMassToSuccessors(&Loop, M))
|
|
|
|
// Irreducible backedge.
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-21 17:57:07 +00:00
|
|
|
|
2014-04-25 04:38:01 +00:00
|
|
|
computeLoopScale(Loop);
|
|
|
|
packageLoop(Loop);
|
2014-04-28 20:02:29 +00:00
|
|
|
return true;
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 20:02:29 +00:00
|
|
|
template <class BT>
|
|
|
|
bool BlockFrequencyInfoImpl<BT>::tryToComputeMassInFunction() {
|
2014-04-21 17:57:07 +00:00
|
|
|
// Compute mass in function.
|
|
|
|
DEBUG(dbgs() << "compute-mass-in-function\n");
|
|
|
|
assert(!Working.empty() && "no blocks in function");
|
|
|
|
assert(!Working[0].isLoopHeader() && "entry block is a loop header");
|
|
|
|
|
2014-04-25 18:47:04 +00:00
|
|
|
Working[0].getMass() = BlockMass::getFull();
|
2014-04-21 17:57:07 +00:00
|
|
|
for (rpot_iterator I = rpot_begin(), IE = rpot_end(); I != IE; ++I) {
|
|
|
|
// Check for nodes that have been packaged.
|
|
|
|
BlockNode Node = getNode(I);
|
2014-04-25 18:47:04 +00:00
|
|
|
if (Working[Node.Index].isPackaged())
|
2014-04-21 17:57:07 +00:00
|
|
|
continue;
|
|
|
|
|
2014-04-28 20:02:29 +00:00
|
|
|
if (!propagateMassToSuccessors(nullptr, Node))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BT> void BlockFrequencyInfoImpl<BT>::computeMassInFunction() {
|
|
|
|
if (tryToComputeMassInFunction())
|
|
|
|
return;
|
|
|
|
computeIrreducibleMass(nullptr, Loops.begin());
|
|
|
|
if (tryToComputeMassInFunction())
|
|
|
|
return;
|
|
|
|
llvm_unreachable("unhandled irreducible control flow");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \note This should be a lambda, but that crashes GCC 4.7.
|
|
|
|
namespace bfi_detail {
|
|
|
|
template <class BT> struct BlockEdgesAdder {
|
|
|
|
typedef BT BlockT;
|
|
|
|
typedef BlockFrequencyInfoImplBase::LoopData LoopData;
|
|
|
|
typedef GraphTraits<const BlockT *> Successor;
|
|
|
|
|
|
|
|
const BlockFrequencyInfoImpl<BT> &BFI;
|
|
|
|
explicit BlockEdgesAdder(const BlockFrequencyInfoImpl<BT> &BFI)
|
|
|
|
: BFI(BFI) {}
|
|
|
|
void operator()(IrreducibleGraph &G, IrreducibleGraph::IrrNode &Irr,
|
|
|
|
const LoopData *OuterLoop) {
|
|
|
|
const BlockT *BB = BFI.RPOT[Irr.Node.Index];
|
|
|
|
for (auto I = Successor::child_begin(BB), E = Successor::child_end(BB);
|
|
|
|
I != E; ++I)
|
|
|
|
G.addEdge(Irr, BFI.getNode(*I), OuterLoop);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
2014-04-28 20:02:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
template <class BT>
|
|
|
|
void BlockFrequencyInfoImpl<BT>::computeIrreducibleMass(
|
|
|
|
LoopData *OuterLoop, std::list<LoopData>::iterator Insert) {
|
|
|
|
DEBUG(dbgs() << "analyze-irreducible-in-";
|
|
|
|
if (OuterLoop) dbgs() << "loop: " << getLoopName(*OuterLoop) << "\n";
|
|
|
|
else dbgs() << "function\n");
|
|
|
|
|
|
|
|
using namespace bfi_detail;
|
|
|
|
// Ideally, addBlockEdges() would be declared here as a lambda, but that
|
|
|
|
// crashes GCC 4.7.
|
|
|
|
BlockEdgesAdder<BT> addBlockEdges(*this);
|
|
|
|
IrreducibleGraph G(*this, OuterLoop, addBlockEdges);
|
|
|
|
|
|
|
|
for (auto &L : analyzeIrreducible(G, OuterLoop, Insert))
|
|
|
|
computeMassInLoop(L);
|
|
|
|
|
|
|
|
if (!OuterLoop)
|
|
|
|
return;
|
|
|
|
updateLoopWithIrreducible(*OuterLoop);
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BT>
|
2014-04-28 20:02:29 +00:00
|
|
|
bool
|
2014-04-25 04:38:01 +00:00
|
|
|
BlockFrequencyInfoImpl<BT>::propagateMassToSuccessors(LoopData *OuterLoop,
|
2014-04-21 17:57:07 +00:00
|
|
|
const BlockNode &Node) {
|
|
|
|
DEBUG(dbgs() << " - node: " << getBlockName(Node) << "\n");
|
|
|
|
// Calculate probability for successors.
|
|
|
|
Distribution Dist;
|
2014-04-25 18:47:04 +00:00
|
|
|
if (auto *Loop = Working[Node.Index].getPackagedLoop()) {
|
|
|
|
assert(Loop != OuterLoop && "Cannot propagate mass in a packaged loop");
|
2014-04-28 20:02:29 +00:00
|
|
|
if (!addLoopSuccessorsToDist(OuterLoop, *Loop, Dist))
|
|
|
|
// Irreducible backedge.
|
|
|
|
return false;
|
2014-04-25 18:47:04 +00:00
|
|
|
} else {
|
2014-04-21 17:57:07 +00:00
|
|
|
const BlockT *BB = getBlock(Node);
|
|
|
|
for (auto SI = Successor::child_begin(BB), SE = Successor::child_end(BB);
|
|
|
|
SI != SE; ++SI)
|
|
|
|
// Do not dereference SI, or getEdgeWeight() is linear in the number of
|
|
|
|
// successors.
|
2014-04-28 20:02:29 +00:00
|
|
|
if (!addToDist(Dist, OuterLoop, Node, getNode(*SI),
|
|
|
|
BPI->getEdgeWeight(BB, SI)))
|
|
|
|
// Irreducible backedge.
|
|
|
|
return false;
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Distribute mass to successors, saving exit and backedge data in the
|
|
|
|
// loop header.
|
2014-04-25 04:38:01 +00:00
|
|
|
distributeMass(Node, OuterLoop, Dist);
|
2014-04-28 20:02:29 +00:00
|
|
|
return true;
|
2014-04-21 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class BT>
|
|
|
|
raw_ostream &BlockFrequencyInfoImpl<BT>::print(raw_ostream &OS) const {
|
|
|
|
if (!F)
|
|
|
|
return OS;
|
|
|
|
OS << "block-frequency-info: " << F->getName() << "\n";
|
|
|
|
for (const BlockT &BB : *F)
|
|
|
|
OS << " - " << bfi_detail::getBlockName(&BB)
|
|
|
|
<< ": float = " << getFloatingBlockFreq(&BB)
|
|
|
|
<< ", int = " << getBlockFreq(&BB).getFrequency() << "\n";
|
|
|
|
|
|
|
|
// Add an extra newline for readability.
|
|
|
|
OS << "\n";
|
|
|
|
return OS;
|
|
|
|
}
|
2011-06-23 21:56:59 +00:00
|
|
|
}
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
2011-06-23 21:56:59 +00:00
|
|
|
#endif
|