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