1 //===- PseudoProbe.h - Pseudo Probe IR Helpers ------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Pseudo probe IR intrinsic and dwarf discriminator manipulation routines.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_IR_PSEUDOPROBE_H
14 #define LLVM_IR_PSEUDOPROBE_H
16 #include "llvm/ADT/Optional.h"
26 constexpr const char *PseudoProbeDescMetadataName = "llvm.pseudo_probe_desc";
28 enum class PseudoProbeType { Block = 0, IndirectCall, DirectCall };
30 enum class PseudoProbeAttributes {
31 Reserved = 0x1, // Reserved for future use.
34 // The saturated distrution factor representing 100% for block probes.
35 constexpr static uint64_t PseudoProbeFullDistributionFactor =
36 std::numeric_limits<uint64_t>::max();
38 struct PseudoProbeDwarfDiscriminator {
40 // The following APIs encodes/decodes per-probe information to/from a
41 // 32-bit integer which is organized as:
42 // [2:0] - 0x7, this is reserved for regular discriminator,
43 // see DWARF discriminator encoding rule
45 // [25:19] - probe distribution factor
46 // [28:26] - probe type, see PseudoProbeType
47 // [31:29] - reserved for probe attributes
48 static uint32_t packProbeData(uint32_t Index, uint32_t Type, uint32_t Flags,
50 assert(Index <= 0xFFFF && "Probe index too big to encode, exceeding 2^16");
51 assert(Type <= 0x7 && "Probe type too big to encode, exceeding 7");
53 assert(Factor <= 100 &&
54 "Probe distribution factor too big to encode, exceeding 100");
55 return (Index << 3) | (Factor << 19) | (Type << 26) | 0x7;
58 static uint32_t extractProbeIndex(uint32_t Value) {
59 return (Value >> 3) & 0xFFFF;
62 static uint32_t extractProbeType(uint32_t Value) {
63 return (Value >> 26) & 0x7;
66 static uint32_t extractProbeAttributes(uint32_t Value) {
67 return (Value >> 29) & 0x7;
70 static uint32_t extractProbeFactor(uint32_t Value) {
71 return (Value >> 19) & 0x7F;
74 // The saturated distrution factor representing 100% for callsites.
75 constexpr static uint8_t FullDistributionFactor = 100;
82 // Distribution factor that estimates the portion of the real execution count.
83 // A saturated distribution factor stands for 1.0 or 100%. A pesudo probe has
84 // a factor with the value ranged from 0.0 to 1.0.
88 Optional<PseudoProbe> extractProbe(const Instruction &Inst);
90 void setProbeDistributionFactor(Instruction &Inst, float Factor);
91 } // end namespace llvm
93 #endif // LLVM_IR_PSEUDOPROBE_H