Skip to content

Commit dc2f228

Browse files
committed
[NFC] Add a new Intrinsics.cpp file for intrinsic code
Add new file Intrinsics.cpp and move `lookupLLVMIntrinsicByName` to that file.
1 parent a96876f commit dc2f228

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

llvm/include/llvm/IR/Intrinsics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// This file defines a set of enums which allow processing of intrinsic
10-
// functions. Values of these enum types are returned by
10+
// functions. Values of these enum types are returned by
1111
// Function::getIntrinsicID.
1212
//
1313
//===----------------------------------------------------------------------===//

llvm/lib/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_llvm_component_library(LLVMCore
3232
GCStrategy.cpp
3333
GVMaterializer.cpp
3434
Globals.cpp
35+
Intrinsics.cpp
3536
IRBuilder.cpp
3637
IRPrintingPasses.cpp
3738
SSAContext.cpp

llvm/lib/IR/IntrinsicInst.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -236,48 +236,6 @@ void DbgAssignIntrinsic::setValue(Value *V) {
236236
MetadataAsValue::get(getContext(), ValueAsMetadata::get(V)));
237237
}
238238

239-
int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
240-
StringRef Name,
241-
StringRef Target) {
242-
assert(Name.starts_with("llvm.") && "Unexpected intrinsic prefix");
243-
assert(Name.drop_front(5).starts_with(Target) && "Unexpected target");
244-
245-
// Do successive binary searches of the dotted name components. For
246-
// "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
247-
// intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
248-
// "llvm.gc.experimental.statepoint", and then we will stop as the range is
249-
// size 1. During the search, we can skip the prefix that we already know is
250-
// identical. By using strncmp we consider names with differing suffixes to
251-
// be part of the equal range.
252-
size_t CmpEnd = 4; // Skip the "llvm" component.
253-
if (!Target.empty())
254-
CmpEnd += 1 + Target.size(); // skip the .target component.
255-
256-
const char *const *Low = NameTable.begin();
257-
const char *const *High = NameTable.end();
258-
const char *const *LastLow = Low;
259-
while (CmpEnd < Name.size() && High - Low > 0) {
260-
size_t CmpStart = CmpEnd;
261-
CmpEnd = Name.find('.', CmpStart + 1);
262-
CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
263-
auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
264-
return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
265-
};
266-
LastLow = Low;
267-
std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
268-
}
269-
if (High - Low > 0)
270-
LastLow = Low;
271-
272-
if (LastLow == NameTable.end())
273-
return -1;
274-
StringRef NameFound = *LastLow;
275-
if (Name == NameFound ||
276-
(Name.starts_with(NameFound) && Name[NameFound.size()] == '.'))
277-
return LastLow - NameTable.begin();
278-
return -1;
279-
}
280-
281239
ConstantInt *InstrProfCntrInstBase::getNumCounters() const {
282240
if (InstrProfValueProfileInst::classof(this))
283241
llvm_unreachable("InstrProfValueProfileInst does not have counters!");

llvm/lib/IR/Intrinsics.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)