Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 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,10 @@ LLVM_ABI void InvertBranch(BranchInst *PBI, IRBuilderBase &Builder);
// br/brcond/unreachable/ret
LLVM_ABI bool hasOnlySimpleTerminator(const Function &F);

/// Print BasicBlock \p BB as an operand or print "<nullptr>" if \p BB is a
/// nullptr.
LLVM_ABI Printable printBasicBlock(const BasicBlock *BB);

} // end namespace llvm

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

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

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

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

Function *F = M->getFunction("foo");
for (const BasicBlock &BB : *F) {
OS << printBasicBlock(&BB);
BB.printAsOperand(OSCheck);
EXPECT_EQ(OS.str(), OSCheck.str());
S.clear();
SCheck.clear();
}
OS << printBasicBlock(nullptr);
EXPECT_EQ(OS.str(), "<nullptr>");
}