xref: /llvm-project/llvm/lib/Support/BlockFrequency.cpp (revision a9e8aa04822cb18c3ebd0ab49ce3237d7f7699ad)
1 //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements Block Frequency class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/BranchProbability.h"
15 #include "llvm/Support/BlockFrequency.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cassert>
18 
19 using namespace llvm;
20 
21 namespace {
22 
23 /// mult96bit - Multiply FREQ by N and store result in W array.
24 void mult96bit(uint64_t freq, uint32_t N, uint64_t W[2]) {
25   uint64_t u0 = freq & UINT32_MAX;
26   uint64_t u1 = freq >> 32;
27 
28   // Represent 96-bit value as w[2]:w[1]:w[0];
29   uint32_t w[3] = { 0, 0, 0 };
30 
31   uint64_t t = u0 * N;
32   uint64_t k = t >> 32;
33   w[0] = t;
34   t = u1 * N + k;
35   w[1] = t;
36   w[2] = t >> 32;
37 
38   // W[1] - higher bits.
39   // W[0] - lower bits.
40   W[0] = w[0] + ((uint64_t) w[1] << 32);
41   W[1] = w[2];
42 }
43 
44 
45 /// div96bit - Divide 96-bit value stored in W array by D. Return 64-bit frequency.
46 uint64_t div96bit(uint64_t W[2], uint32_t D) {
47   uint64_t y = W[0];
48   uint64_t x = W[1];
49 
50   for (int i = 1; i <= 64; ++i) {
51     uint32_t t = (int)x >> 31;
52     x = (x << 1) | (y >> 63);
53     y = y << 1;
54     if ((x | t) >= D) {
55       x -= D;
56       ++y;
57     }
58   }
59 
60   return y;
61 }
62 
63 }
64 
65 
66 BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
67   uint32_t n = Prob.getNumerator();
68   uint32_t d = Prob.getDenominator();
69 
70   assert(n <= d && "Probability must be less or equal to 1.");
71 
72   // If we can overflow use 96-bit operations.
73   if (n > 0 && Frequency > UINT64_MAX / n) {
74     // 96-bit value represented as W[1]:W[0].
75     uint64_t W[2];
76 
77     // Probability is less or equal to 1 which means that results must fit
78     // 64-bit.
79     mult96bit(Frequency, n, W);
80     Frequency = div96bit(W, d);
81     return *this;
82   }
83 
84   Frequency *= n;
85   Frequency /= d;
86   return *this;
87 }
88 
89 const BlockFrequency
90 BlockFrequency::operator*(const BranchProbability &Prob) const {
91   BlockFrequency Freq(Frequency);
92   Freq *= Prob;
93   return Freq;
94 }
95 
96 BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
97   uint64_t Before = Freq.Frequency;
98   Frequency += Freq.Frequency;
99 
100   // If overflow, set frequency to the maximum value.
101   if (Frequency < Before)
102     Frequency = UINT64_MAX;
103 
104   return *this;
105 }
106 
107 const BlockFrequency
108 BlockFrequency::operator+(const BlockFrequency &Prob) const {
109   BlockFrequency Freq(Frequency);
110   Freq += Prob;
111   return Freq;
112 }
113 
114 void BlockFrequency::print(raw_ostream &OS) const {
115   OS << Frequency;
116 }
117 
118 namespace llvm {
119 
120 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq) {
121   Freq.print(OS);
122   return OS;
123 }
124 
125 }
126