Nick Lewycky | ea08c70 | 2014-02-26 03:10:45 +0000 | [diff] [blame] | 1 | //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===// |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jim Laskey | 864e444 | 2006-03-24 10:00:56 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements methods that make it really easy to deal with intrinsic |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 11 | // functions. |
Jim Laskey | 864e444 | 2006-03-24 10:00:56 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 23 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/IntrinsicInst.h" |
| 25 | #include "llvm/IR/Constants.h" |
| 26 | #include "llvm/IR/GlobalVariable.h" |
| 27 | #include "llvm/IR/Metadata.h" |
Reid Kleckner | c2752da | 2016-01-26 22:33:19 +0000 | [diff] [blame^] | 28 | #include "llvm/Support/raw_ostream.h" |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 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)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 37 | if (CE->isCast()) |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 38 | return CE->getOperand(0); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 39 | return nullptr; |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Value *DbgInfoIntrinsic::StripCast(Value *C) { |
| 43 | if (Value *CO = CastOperand(C)) { |
Jim Laskey | 7092888 | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 44 | C = StripCast(CO); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 45 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| 46 | if (GV->hasInitializer()) |
| 47 | if (Value *CO = CastOperand(GV->getInitializer())) |
Jim Laskey | 7092888 | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 48 | C = StripCast(CO); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 49 | } |
Jim Laskey | 7092888 | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 50 | return dyn_cast<GlobalVariable>(C); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 53 | static Value *getValueImpl(Value *Op) { |
| 54 | auto *MD = cast<MetadataAsValue>(Op)->getMetadata(); |
| 55 | if (auto *V = dyn_cast<ValueAsMetadata>(MD)) |
| 56 | return V->getValue(); |
| 57 | |
| 58 | // When the value goes to null, it gets replaced by an empty MDNode. |
| 59 | assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode"); |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 63 | //===----------------------------------------------------------------------===// |
Victor Hernandez | b324e66 | 2010-01-15 19:04:09 +0000 | [diff] [blame] | 64 | /// DbgDeclareInst - This represents the llvm.dbg.declare instruction. |
| 65 | /// |
| 66 | |
| 67 | Value *DbgDeclareInst::getAddress() const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 68 | if (!getArgOperand(0)) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 69 | return nullptr; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 70 | |
| 71 | return getValueImpl(getArgOperand(0)); |
Victor Hernandez | b324e66 | 2010-01-15 19:04:09 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | //===----------------------------------------------------------------------===// |
Chris Lattner | cc8c814 | 2009-12-31 01:32:41 +0000 | [diff] [blame] | 75 | /// DbgValueInst - This represents the llvm.dbg.value instruction. |
| 76 | /// |
| 77 | |
Victor Hernandez | 9ce5b51 | 2010-01-11 07:45:19 +0000 | [diff] [blame] | 78 | const Value *DbgValueInst::getValue() const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 79 | return const_cast<DbgValueInst *>(this)->getValue(); |
Victor Hernandez | 9ce5b51 | 2010-01-11 07:45:19 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 82 | Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); } |
Reid Kleckner | c2752da | 2016-01-26 22:33:19 +0000 | [diff] [blame^] | 83 | |
| 84 | int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable, |
| 85 | StringRef Name) { |
| 86 | assert(Name.startswith("llvm.")); |
| 87 | |
| 88 | // Do successive binary searches of the dotted name components. For |
| 89 | // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of |
| 90 | // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then |
| 91 | // "llvm.gc.experimental.statepoint", and then we will stop as the range is |
| 92 | // size 1. During the search, we can skip the prefix that we already know is |
| 93 | // identical. By using strncmp we consider names with differing suffixes to |
| 94 | // be part of the equal range. |
| 95 | size_t CmpStart = 0; |
| 96 | size_t CmpEnd = 4; // Skip the "llvm" component. |
| 97 | const char *const *Low = NameTable.begin(); |
| 98 | const char *const *High = NameTable.end(); |
| 99 | const char *const *LastLow = Low; |
| 100 | while (CmpEnd < Name.size() && High - Low > 0) { |
| 101 | CmpStart = CmpEnd; |
| 102 | CmpEnd = Name.find('.', CmpStart + 1); |
| 103 | CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd; |
| 104 | auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) { |
| 105 | return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0; |
| 106 | }; |
| 107 | LastLow = Low; |
| 108 | std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp); |
| 109 | } |
| 110 | if (High - Low > 0) |
| 111 | LastLow = Low; |
| 112 | |
| 113 | if (LastLow == NameTable.end()) |
| 114 | return -1; |
| 115 | StringRef NameFound = *LastLow; |
| 116 | if (Name == NameFound || |
| 117 | (Name.startswith(NameFound) && Name[NameFound.size()] == '.')) |
| 118 | return LastLow - NameTable.begin(); |
| 119 | return -1; |
| 120 | } |