Skip to content

Commit 3337875

Browse files
committed
[BasicBlockUtils] Add BasicBlock printer
1 parent 6b5c38d commit 3337875

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/IR/BasicBlock.h"
2222
#include "llvm/IR/Dominators.h"
2323
#include "llvm/Support/Compiler.h"
24+
#include "llvm/Support/Printable.h"
2425
#include <cassert>
2526

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

615+
LLVM_ABI Printable printBasicBlock(const BasicBlock *BB);
616+
614617
} // end namespace llvm
615618

616619
#endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H

llvm/lib/Transforms/Utils/BasicBlockUtils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,3 +1775,10 @@ bool llvm::hasOnlySimpleTerminator(const Function &F) {
17751775
}
17761776
return true;
17771777
}
1778+
1779+
Printable llvm::printBasicBlock(const BasicBlock *BB) {
1780+
return Printable([BB](raw_ostream &OS) {
1781+
if (BB)
1782+
return BB->printAsOperand(OS);
1783+
});
1784+
}

llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,27 @@ attributes #0 = { presplitcoroutine }
716716
EXPECT_FALSE(llvm::isPresplitCoroSuspendExitEdge(
717717
*ExitN.getSinglePredecessor(), ExitN));
718718
}
719+
720+
TEST(BasicBlockUtils, BasicBlockPrintable) {
721+
std::string S;
722+
llvm::raw_string_ostream OS{S};
723+
724+
LLVMContext C;
725+
std::unique_ptr<Module> M = parseIR(C, R"IR(
726+
define void @foo() {
727+
.entry:
728+
br label %bb0
729+
bb0:
730+
ret void
731+
}
732+
)IR");
733+
734+
Function *F = M->getFunction("foo");
735+
for (BasicBlock &BB : *F) {
736+
OS << printBasicBlock(&BB);
737+
EXPECT_EQ(OS.str(), "label %" + BB.getName().str());
738+
S.clear();
739+
}
740+
OS << printBasicBlock(nullptr);
741+
EXPECT_EQ(OS.str(), "");
742+
}

0 commit comments

Comments
 (0)