Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Dominators.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Printable.h"
#include <cassert>

namespace llvm {
Expand Down Expand Up @@ -611,6 +612,8 @@ LLVM_ABI void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder);
// br/brcond/unreachable/ret
LLVM_ABI bool hasOnlySimpleTerminator(const Function &F);

LLVM_ABI Printable printBasicBlock(const BasicBlock *BB);

} // end namespace llvm

#endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
7 changes: 7 additions & 0 deletions llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,3 +1775,10 @@ bool llvm::hasOnlySimpleTerminator(const Function &F) {
}
return true;
}

Printable llvm::printBasicBlock(const BasicBlock *BB) {
return Printable([BB](raw_ostream &OS) {
if (BB)
return BB->printAsOperand(OS);
});
}
24 changes: 24 additions & 0 deletions llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,27 @@ attributes #0 = { presplitcoroutine }
EXPECT_FALSE(llvm::isPresplitCoroSuspendExitEdge(
*ExitN.getSinglePredecessor(), ExitN));
}

TEST(BasicBlockUtils, BasicBlockPrintable) {
std::string S;
llvm::raw_string_ostream OS{S};

LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"IR(
define void @foo() {
.entry:
br label %bb0
bb0:
ret void
}
)IR");

Function *F = M->getFunction("foo");
for (BasicBlock &BB : *F) {
OS << printBasicBlock(&BB);
EXPECT_EQ(OS.str(), "label %" + BB.getName().str());
S.clear();
}
OS << printBasicBlock(nullptr);
EXPECT_EQ(OS.str(), "");
}