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