1 //===- PseudoProbe.h - Pseudo Probe IR Helpers ------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Pseudo probe IR intrinsic and dwarf discriminator manipulation routines.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_PSEUDOPROBE_H
14 #define LLVM_IR_PSEUDOPROBE_H
15 
16 #include "llvm/ADT/Optional.h"
17 #include <cassert>
18 #include <cstdint>
19 #include <limits>
20 
21 namespace llvm {
22 
23 class Instruction;
24 
25 constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
26 
27 enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
28 
29 // The saturated distrution factor representing 100% for block probes.
30 constexpr static uint64_t PseudoProbeFullDistributionFactor =
31     std::numeric_limits<uint64_t>::max();
32 
33 struct PseudoProbeDwarfDiscriminator {
34 public:
35   // The following APIs encodes/decodes per-probe information to/from a
36   // 32-bit integer which is organized as:
37   //  [2:0] - 0x7, this is reserved for regular discriminator,
38   //          see DWARF discriminator encoding rule
39   //  [18:3] - probe id
40   //  [25:19] - probe distribution factor
41   //  [28:26] - probe type, see PseudoProbeType
42   //  [31:29] - reserved for probe attributes
43   static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
44                                 uint32_t Factor) {
45     assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
46     assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
47     assert(Flags <= 0x7);
48     assert(Factor <= 100 &&
49            "Probe distribution factor too big to encode, exceeding 100");
50     return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
51   }
52 
53   static uint32_t extractProbeIndex(uint32_t Value) {
54     return (Value >> 3) & 0xFFFF;
55   }
56 
57   static uint32_t extractProbeType(uint32_t Value) {
58     return (Value >> 26) & 0x7;
59   }
60 
61   static uint32_t extractProbeAttributes(uint32_t Value) {
62     return (Value >> 29) & 0x7;
63   }
64 
65   static uint32_t extractProbeFactor(uint32_t Value) {
66     return (Value >> 19) & 0x7F;
67   }
68 
69   // The saturated distrution factor representing 100% for callsites.
70   constexpr static uint8_t FullDistributionFactor = 100;
71 };
72 
73 struct PseudoProbe {
74   uint32_t Id;
75   uint32_t Type;
76   uint32_t Attr;
77   // Distribution factor that estimates the portion of the real execution count.
78   // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
79   // a factor with the value ranged from 0.0 to 1.0.
80   float Factor;
81 };
82 
83 Optional<PseudoProbe> extractProbe(const Instruction &Inst);
84 
85 void setProbeDistributionFactor(Instruction &Inst, float Factor);
86 } // end namespace llvm
87 
88 #endif // LLVM_IR_PSEUDOPROBE_H
89