Skip to content

Commit 5572121

Browse files
author
Yilin Li
committed
[llvm-extract] support unnamed bb.
1 parent f46d641 commit 5572121

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

llvm/tools/llvm-extract/llvm-extract.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ static cl::list<std::string> ExtractBlocks(
9090
"Each pair will create a function.\n"
9191
"If multiple basic blocks are specified in one pair,\n"
9292
"the first block in the sequence should dominate the rest.\n"
93+
"If an unnamed basic block is to be extracted,\n"
94+
"'%' should be added before the basic block variable names.\n"
9395
"eg:\n"
9496
" --bb=f:bb1;bb2 will extract one function with both bb1 and bb2;\n"
9597
" --bb=f:bb1 --bb=f:bb2 will extract two functions, one with bb1, one "
96-
"with bb2."),
98+
"with bb2.\n"
99+
" --bb=f:%1 will extract one function with basic block 1;"),
97100
cl::value_desc("function:bb1[;bb2...]"), cl::cat(ExtractCat));
98101

99102
// ExtractAlias - The alias to extract from the module.
@@ -355,9 +358,25 @@ int main(int argc, char **argv) {
355358
for (StringRef BBName : P.second) {
356359
// The function has been materialized, so add its matching basic blocks
357360
// to the block extractor list, or fail if a name is not found.
361+
#ifndef NDEBUG
358362
auto Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
359-
return BB.getName() == BBName;
363+
return BB.getNameOrAsOperand() == BBName;
360364
});
365+
#else
366+
llvm::Function::iterator Res;
367+
if (BBName.substr(0, 1) == "%") {
368+
Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
369+
std::string tmpName;
370+
raw_string_ostream OS(tmpName);
371+
BB.printAsOperand(OS, false);
372+
return OS.str() == BBName;
373+
});
374+
} else {
375+
Res = llvm::find_if(*P.first, [&](const BasicBlock &BB) {
376+
return BB.getName() == BBName;
377+
});
378+
}
379+
#endif
361380
if (Res == P.first->end()) {
362381
errs() << argv[0] << ": function " << P.first->getName()
363382
<< " doesn't contain a basic block named '" << BBName

0 commit comments

Comments
 (0)