Add support for enum bounds
[hiphop-php.git] / hphp / runtime / vm / jit / incref-profile.h
blob1087e4d538368651194f6eaf74adf1f93959a51a
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #pragma once
19 #include <folly/json/dynamic.h>
20 #include <folly/Format.h>
22 #include "hphp/runtime/vm/jit/target-profile.h"
24 namespace HPHP { namespace jit {
26 ///////////////////////////////////////////////////////////////////////////////
29 * Profile the frequency of the 3 possible different behaviors for an IncRef
30 * instruction. Each execution must fall into exactly one of these categories:
32 * 1) Uncounted:
33 * the type was uncounted
34 * 2) Persistent:
35 * the type was refcounted and the value was persistent (so no inc happened)
36 * 3) Incremented:
37 * the count for the value was incremented.
40 struct IncRefProfile {
41 uint32_t uncounted() const {
42 return total - refcounted;
45 uint32_t persistent() const {
46 return refcounted - incremented;
49 float percent(uint32_t value) const {
50 return total ? 100.0 * value / total : 0.0;
53 std::string toString() const {
54 return folly::sformat(
55 "total: {:4}\n uncounted: {:4} ({:.1f}%),\n persistent: {:4} ({:.1f}%),\n"
56 " incremented: {:4} ({:.1f}%)",
57 total,
58 uncounted(), percent(uncounted()),
59 persistent(), percent(persistent()),
60 incremented, percent(incremented)
64 folly::dynamic toDynamic() const {
65 return folly::dynamic::object("total", total)
66 ("uncounted", uncounted())
67 ("percentUncounted", percent(uncounted()))
68 ("persistent", persistent())
69 ("percentPersistent", percent(persistent()))
70 ("incremented", incremented)
71 ("percentIncremented", percent(incremented))
72 ("profileType", "IncRefProfile");
75 // overflow handling isn't statistically correct; but its better
76 // than overflowing.
77 static void reduce(IncRefProfile& a, const IncRefProfile& b) {
78 auto const total = static_cast<uint64_t>(a.total + b.total);
79 auto constexpr limit = std::numeric_limits<decltype(a.total)>::max();
80 if (total > limit) {
81 auto scale = [&] (uint32_t& x, uint64_t y) {
82 x = ((x + y) * limit + total - 1) / total;
84 a.total = limit;
85 scale(a.refcounted, b.refcounted);
86 scale(a.incremented, b.incremented);
87 } else {
88 a.total += b.total;
89 a.refcounted += b.refcounted;
90 a.incremented += b.incremented;
95 * The total number of times this IncRef was executed.
97 uint32_t total;
99 * The number of times this IncRef made it past the refcounted check.
101 uint32_t refcounted;
103 * The number of times this IncRef actually incremented the refcount.
105 uint32_t incremented;
108 ///////////////////////////////////////////////////////////////////////////////