1 //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Block Frequency class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/BranchProbability.h"
15 #include "llvm/Support/BlockFrequency.h"
16 #include "llvm/Support/raw_ostream.h"
21 /// Multiply FREQ by N and store result in W array.
22 static void mult96bit(uint64_t freq, uint32_t N, uint64_t W[2]) {
23 uint64_t u0 = freq & UINT32_MAX;
24 uint64_t u1 = freq >> 32;
26 // Represent 96-bit value as w[2]:w[1]:w[0];
27 uint32_t w[3] = { 0, 0, 0 };
36 // W[1] - higher bits.
38 W[0] = w[0] + ((uint64_t) w[1] << 32);
43 /// Divide 96-bit value stored in W array by D.
44 /// Return 64-bit quotient, saturated to UINT64_MAX on overflow.
45 static uint64_t div96bit(uint64_t W[2], uint32_t D) {
50 assert(x != 0 && "This is really a 64-bit division");
52 // This long division algorithm automatically saturates on overflow.
53 for (i = 0; i < 64 && x; ++i) {
54 uint32_t t = -((x >> 31) & 1); // Splat bit 31 to bits 0-31.
55 x = (x << 1) | (y >> 63);
67 void BlockFrequency::scale(uint32_t N, uint32_t D) {
68 assert(D != 0 && "Division by zero");
70 // Calculate Frequency * N.
71 uint64_t MulLo = (Frequency & UINT32_MAX) * N;
72 uint64_t MulHi = (Frequency >> 32) * N;
73 uint64_t MulRes = (MulHi << 32) + MulLo;
75 // If the product fits in 64 bits, just use built-in division.
76 if (MulHi <= UINT32_MAX && MulRes >= MulLo) {
77 Frequency = MulRes / D;
81 // Product overflowed, use 96-bit operations.
82 // 96-bit value represented as W[1]:W[0].
84 mult96bit(Frequency, N, W);
85 Frequency = div96bit(W, D);
89 BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
90 scale(Prob.getNumerator(), Prob.getDenominator());
95 BlockFrequency::operator*(const BranchProbability &Prob) const {
96 BlockFrequency Freq(Frequency);
101 BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
102 scale(Prob.getDenominator(), Prob.getNumerator());
106 BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
107 BlockFrequency Freq(Frequency);
112 BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
113 uint64_t Before = Freq.Frequency;
114 Frequency += Freq.Frequency;
116 // If overflow, set frequency to the maximum value.
117 if (Frequency < Before)
118 Frequency = UINT64_MAX;
124 BlockFrequency::operator+(const BlockFrequency &Prob) const {
125 BlockFrequency Freq(Frequency);
130 void BlockFrequency::print(raw_ostream &OS) const {
131 // Convert fixed-point number to decimal.
132 OS << Frequency / getEntryFrequency() << ".";
133 uint64_t Rem = Frequency % getEntryFrequency();
138 OS << Rem / getEntryFrequency();
139 Rem = Rem % getEntryFrequency();
140 } while (Rem >= Eps/2);
145 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq) {