xref: /freebsd-src/contrib/llvm-project/llvm/lib/IR/PseudoProbe.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===- PseudoProbe.cpp - Pseudo Probe Helpers -----------------------------===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric //
9*e8d8bef9SDimitry Andric // This file implements the helpers to manipulate pseudo probe IR intrinsic
10*e8d8bef9SDimitry Andric // calls.
11*e8d8bef9SDimitry Andric //
12*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13*e8d8bef9SDimitry Andric 
14*e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h"
15*e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
16*e8d8bef9SDimitry Andric #include "llvm/IR/IRBuilder.h"
17*e8d8bef9SDimitry Andric #include "llvm/IR/Instruction.h"
18*e8d8bef9SDimitry Andric 
19*e8d8bef9SDimitry Andric using namespace llvm;
20*e8d8bef9SDimitry Andric 
21*e8d8bef9SDimitry Andric namespace llvm {
22*e8d8bef9SDimitry Andric 
23*e8d8bef9SDimitry Andric Optional<PseudoProbe> extractProbeFromDiscriminator(const Instruction &Inst) {
24*e8d8bef9SDimitry Andric   assert(isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst) &&
25*e8d8bef9SDimitry Andric          "Only call instructions should have pseudo probe encodes as their "
26*e8d8bef9SDimitry Andric          "Dwarf discriminators");
27*e8d8bef9SDimitry Andric   if (const DebugLoc &DLoc = Inst.getDebugLoc()) {
28*e8d8bef9SDimitry Andric     const DILocation *DIL = DLoc;
29*e8d8bef9SDimitry Andric     auto Discriminator = DIL->getDiscriminator();
30*e8d8bef9SDimitry Andric     if (DILocation::isPseudoProbeDiscriminator(Discriminator)) {
31*e8d8bef9SDimitry Andric       PseudoProbe Probe;
32*e8d8bef9SDimitry Andric       Probe.Id =
33*e8d8bef9SDimitry Andric           PseudoProbeDwarfDiscriminator::extractProbeIndex(Discriminator);
34*e8d8bef9SDimitry Andric       Probe.Type =
35*e8d8bef9SDimitry Andric           PseudoProbeDwarfDiscriminator::extractProbeType(Discriminator);
36*e8d8bef9SDimitry Andric       Probe.Attr =
37*e8d8bef9SDimitry Andric           PseudoProbeDwarfDiscriminator::extractProbeAttributes(Discriminator);
38*e8d8bef9SDimitry Andric       return Probe;
39*e8d8bef9SDimitry Andric     }
40*e8d8bef9SDimitry Andric   }
41*e8d8bef9SDimitry Andric   return None;
42*e8d8bef9SDimitry Andric }
43*e8d8bef9SDimitry Andric 
44*e8d8bef9SDimitry Andric Optional<PseudoProbe> extractProbe(const Instruction &Inst) {
45*e8d8bef9SDimitry Andric   if (const auto *II = dyn_cast<PseudoProbeInst>(&Inst)) {
46*e8d8bef9SDimitry Andric     PseudoProbe Probe;
47*e8d8bef9SDimitry Andric     Probe.Id = II->getIndex()->getZExtValue();
48*e8d8bef9SDimitry Andric     Probe.Type = (uint32_t)PseudoProbeType::Block;
49*e8d8bef9SDimitry Andric     Probe.Attr = II->getAttributes()->getZExtValue();
50*e8d8bef9SDimitry Andric     return Probe;
51*e8d8bef9SDimitry Andric   }
52*e8d8bef9SDimitry Andric 
53*e8d8bef9SDimitry Andric   if (isa<CallBase>(&Inst) && !isa<IntrinsicInst>(&Inst))
54*e8d8bef9SDimitry Andric     return extractProbeFromDiscriminator(Inst);
55*e8d8bef9SDimitry Andric 
56*e8d8bef9SDimitry Andric   return None;
57*e8d8bef9SDimitry Andric }
58*e8d8bef9SDimitry Andric } // namespace llvm
59