|
| 1 | +//===-- Intrinsics.cpp - Intrinsic Function Handling ------------*- C++ -*-===// |
| 2 | +// |
| 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 |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements functions required for supporting intrinsic functions. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "llvm/IR/Intrinsics.h" |
| 14 | + |
| 15 | +using namespace llvm; |
| 16 | + |
| 17 | +int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable, |
| 18 | + StringRef Name, |
| 19 | + StringRef Target) { |
| 20 | + assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix"); |
| 21 | + assert(Name.drop_front(5).starts_with(Target) && "Unexpected target"); |
| 22 | + |
| 23 | + // Do successive binary searches of the dotted name components. For |
| 24 | + // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of |
| 25 | + // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then |
| 26 | + // "llvm.gc.experimental.statepoint", and then we will stop as the range is |
| 27 | + // size 1. During the search, we can skip the prefix that we already know is |
| 28 | + // identical. By using strncmp we consider names with differing suffixes to |
| 29 | + // be part of the equal range. |
| 30 | + size_t CmpEnd = 4; // Skip the "llvm" component. |
| 31 | + if (!Target.empty()) |
| 32 | + CmpEnd += 1 + Target.size(); // skip the .target component. |
| 33 | + |
| 34 | + const char *const *Low = NameTable.begin(); |
| 35 | + const char *const *High = NameTable.end(); |
| 36 | + const char *const *LastLow = Low; |
| 37 | + while (CmpEnd < Name.size() && High - Low > 0) { |
| 38 | + size_t CmpStart = CmpEnd; |
| 39 | + CmpEnd = Name.find('.', CmpStart + 1); |
| 40 | + CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd; |
| 41 | + auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) { |
| 42 | + return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0; |
| 43 | + }; |
| 44 | + LastLow = Low; |
| 45 | + std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp); |
| 46 | + } |
| 47 | + if (High - Low > 0) |
| 48 | + LastLow = Low; |
| 49 | + |
| 50 | + if (LastLow == NameTable.end()) |
| 51 | + return -1; |
| 52 | + StringRef NameFound = *LastLow; |
| 53 | + if (Name == NameFound || |
| 54 | + (Name.starts_with(NameFound) && Name[NameFound.size()] == '.')) |
| 55 | + return LastLow - NameTable.begin(); |
| 56 | + return -1; |
| 57 | +} |
0 commit comments