Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 1 | //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements Block Frequency class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 13 | #include "llvm/Support/BlockFrequency.h" |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 14 | #include <cassert> |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 18 | BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) { |
Duncan P. N. Exon Smith | 547183b | 2014-04-29 16:31:29 +0000 | [diff] [blame] | 19 | Frequency = Prob.scale(Frequency); |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 20 | return *this; |
| 21 | } |
| 22 | |
Cong Hou | 90c6cf8 | 2015-10-12 18:14:15 +0000 | [diff] [blame] | 23 | BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const { |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 24 | BlockFrequency Freq(Frequency); |
| 25 | Freq *= Prob; |
| 26 | return Freq; |
| 27 | } |
| 28 | |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 29 | BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) { |
Duncan P. N. Exon Smith | 547183b | 2014-04-29 16:31:29 +0000 | [diff] [blame] | 30 | Frequency = Prob.scaleByInverse(Frequency); |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 31 | return *this; |
| 32 | } |
| 33 | |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 34 | BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const { |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 35 | BlockFrequency Freq(Frequency); |
| 36 | Freq /= Prob; |
| 37 | return Freq; |
| 38 | } |
| 39 | |
Cong Hou | 90c6cf8 | 2015-10-12 18:14:15 +0000 | [diff] [blame] | 40 | BlockFrequency &BlockFrequency::operator+=(BlockFrequency Freq) { |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 41 | uint64_t Before = Freq.Frequency; |
| 42 | Frequency += Freq.Frequency; |
| 43 | |
| 44 | // If overflow, set frequency to the maximum value. |
| 45 | if (Frequency < Before) |
| 46 | Frequency = UINT64_MAX; |
| 47 | |
| 48 | return *this; |
| 49 | } |
| 50 | |
Cong Hou | 90c6cf8 | 2015-10-12 18:14:15 +0000 | [diff] [blame] | 51 | BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const { |
| 52 | BlockFrequency NewFreq(Frequency); |
| 53 | NewFreq += Freq; |
| 54 | return NewFreq; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Cong Hou | 61e13de | 2015-10-12 18:34:00 +0000 | [diff] [blame] | 57 | BlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) { |
| 58 | // If underflow, set frequency to 0. |
| 59 | if (Frequency <= Freq.Frequency) |
| 60 | Frequency = 0; |
| 61 | else |
| 62 | Frequency -= Freq.Frequency; |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const { |
| 67 | BlockFrequency NewFreq(Frequency); |
| 68 | NewFreq -= Freq; |
| 69 | return NewFreq; |
| 70 | } |
| 71 | |
Michael Gottesman | 8f17dcc | 2013-12-14 02:24:22 +0000 | [diff] [blame] | 72 | BlockFrequency &BlockFrequency::operator>>=(const unsigned count) { |
| 73 | // Frequency can never be 0 by design. |
| 74 | assert(Frequency != 0); |
| 75 | |
| 76 | // Shift right by count. |
| 77 | Frequency >>= count; |
| 78 | |
| 79 | // Saturate to 1 if we are 0. |
| 80 | Frequency |= Frequency == 0; |
| 81 | return *this; |
| 82 | } |