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 class BasicBlock;
25 
26 constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
27 
28 enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
29 
30 // The saturated distrution factor representing 100% for block probes.
31 constexpr static uint64_t PseudoProbeFullDistributionFactor =
32     std::numeric_limits<uint64_t>::max();
33 
34 struct PseudoProbeDwarfDiscriminator {
35 public:
36   // The following APIs encodes/decodes per-probe information to/from a
37   // 32-bit integer which is organized as:
38   //  [2:0] - 0x7, this is reserved for regular discriminator,
39   //          see DWARF discriminator encoding rule
40   //  [18:3] - probe id
41   //  [25:19] - probe distribution factor
42   //  [28:26] - probe type, see PseudoProbeType
43   //  [31:29] - reserved for probe attributes
packProbeDataPseudoProbeDwarfDiscriminator44   static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
45                                 uint32_t Factor) {
46     assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
47     assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
48     assert(Flags <= 0x7);
49     assert(Factor <= 100 &&
50            "Probe distribution factor too big to encode, exceeding 100");
51     return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
52   }
53 
extractProbeIndexPseudoProbeDwarfDiscriminator54   static uint32_t extractProbeIndex(uint32_t Value) {
55     return (Value >> 3) & 0xFFFF;
56   }
57 
extractProbeTypePseudoProbeDwarfDiscriminator58   static uint32_t extractProbeType(uint32_t Value) {
59     return (Value >> 26) & 0x7;
60   }
61 
extractProbeAttributesPseudoProbeDwarfDiscriminator62   static uint32_t extractProbeAttributes(uint32_t Value) {
63     return (Value >> 29) & 0x7;
64   }
65 
extractProbeFactorPseudoProbeDwarfDiscriminator66   static uint32_t extractProbeFactor(uint32_t Value) {
67     return (Value >> 19) & 0x7F;
68   }
69 
70   // The saturated distrution factor representing 100% for callsites.
71   constexpr static uint8_t FullDistributionFactor = 100;
72 };
73 
74 struct PseudoProbe {
75   uint32_t Id;
76   uint32_t Type;
77   uint32_t Attr;
78   float Factor;
79 };
80 
81 Optional<PseudoProbe> extractProbe(const Instruction &Inst);
82 
83 void setProbeDistributionFactor(Instruction &Inst, float Factor);
84 
85 } // end namespace llvm
86 
87 #endif // LLVM_IR_PSEUDOPROBE_H
88