xref: /aosp_15_r20/external/llvm/lib/IR/IntrinsicInst.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements methods that make it really easy to deal with intrinsic
11*9880d681SAndroid Build Coastguard Worker // functions.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // All intrinsic function calls are instances of the call instruction, so these
14*9880d681SAndroid Build Coastguard Worker // are all subclasses of the CallInst class.  Note that none of these classes
15*9880d681SAndroid Build Coastguard Worker // has state or virtual methods, which is an important part of this gross/neat
16*9880d681SAndroid Build Coastguard Worker // hack working.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker // In some cases, arguments to intrinsics need to be generic and are defined as
19*9880d681SAndroid Build Coastguard Worker // type pointer to empty struct { }*.  To access the real item of interest the
20*9880d681SAndroid Build Coastguard Worker // cast instruction needs to be stripped away.
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
23*9880d681SAndroid Build Coastguard Worker 
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalVariable.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Metadata.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
29*9880d681SAndroid Build Coastguard Worker using namespace llvm;
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
32*9880d681SAndroid Build Coastguard Worker /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
33*9880d681SAndroid Build Coastguard Worker ///
34*9880d681SAndroid Build Coastguard Worker 
getVariableLocation(bool AllowNullOp) const35*9880d681SAndroid Build Coastguard Worker Value *DbgInfoIntrinsic::getVariableLocation(bool AllowNullOp) const {
36*9880d681SAndroid Build Coastguard Worker   Value *Op = getArgOperand(0);
37*9880d681SAndroid Build Coastguard Worker   if (AllowNullOp && !Op)
38*9880d681SAndroid Build Coastguard Worker     return nullptr;
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker   auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
41*9880d681SAndroid Build Coastguard Worker   if (auto *V = dyn_cast<ValueAsMetadata>(MD))
42*9880d681SAndroid Build Coastguard Worker     return V->getValue();
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker   // When the value goes to null, it gets replaced by an empty MDNode.
45*9880d681SAndroid Build Coastguard Worker   assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
46*9880d681SAndroid Build Coastguard Worker   return nullptr;
47*9880d681SAndroid Build Coastguard Worker }
48*9880d681SAndroid Build Coastguard Worker 
lookupLLVMIntrinsicByName(ArrayRef<const char * > NameTable,StringRef Name)49*9880d681SAndroid Build Coastguard Worker int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
50*9880d681SAndroid Build Coastguard Worker                                                StringRef Name) {
51*9880d681SAndroid Build Coastguard Worker   assert(Name.startswith("llvm."));
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker   // Do successive binary searches of the dotted name components. For
54*9880d681SAndroid Build Coastguard Worker   // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
55*9880d681SAndroid Build Coastguard Worker   // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
56*9880d681SAndroid Build Coastguard Worker   // "llvm.gc.experimental.statepoint", and then we will stop as the range is
57*9880d681SAndroid Build Coastguard Worker   // size 1. During the search, we can skip the prefix that we already know is
58*9880d681SAndroid Build Coastguard Worker   // identical. By using strncmp we consider names with differing suffixes to
59*9880d681SAndroid Build Coastguard Worker   // be part of the equal range.
60*9880d681SAndroid Build Coastguard Worker   size_t CmpStart = 0;
61*9880d681SAndroid Build Coastguard Worker   size_t CmpEnd = 4; // Skip the "llvm" component.
62*9880d681SAndroid Build Coastguard Worker   const char *const *Low = NameTable.begin();
63*9880d681SAndroid Build Coastguard Worker   const char *const *High = NameTable.end();
64*9880d681SAndroid Build Coastguard Worker   const char *const *LastLow = Low;
65*9880d681SAndroid Build Coastguard Worker   while (CmpEnd < Name.size() && High - Low > 0) {
66*9880d681SAndroid Build Coastguard Worker     CmpStart = CmpEnd;
67*9880d681SAndroid Build Coastguard Worker     CmpEnd = Name.find('.', CmpStart + 1);
68*9880d681SAndroid Build Coastguard Worker     CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
69*9880d681SAndroid Build Coastguard Worker     auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
70*9880d681SAndroid Build Coastguard Worker       return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
71*9880d681SAndroid Build Coastguard Worker     };
72*9880d681SAndroid Build Coastguard Worker     LastLow = Low;
73*9880d681SAndroid Build Coastguard Worker     std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
74*9880d681SAndroid Build Coastguard Worker   }
75*9880d681SAndroid Build Coastguard Worker   if (High - Low > 0)
76*9880d681SAndroid Build Coastguard Worker     LastLow = Low;
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   if (LastLow == NameTable.end())
79*9880d681SAndroid Build Coastguard Worker     return -1;
80*9880d681SAndroid Build Coastguard Worker   StringRef NameFound = *LastLow;
81*9880d681SAndroid Build Coastguard Worker   if (Name == NameFound ||
82*9880d681SAndroid Build Coastguard Worker       (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
83*9880d681SAndroid Build Coastguard Worker     return LastLow - NameTable.begin();
84*9880d681SAndroid Build Coastguard Worker   return -1;
85*9880d681SAndroid Build Coastguard Worker }
86