From c2d4209f85f46cc91163bc47cc43db252c94acf6 Mon Sep 17 00:00:00 2001 From: Stefan Weigl-Bosker Date: Thu, 19 Feb 2026 18:51:48 -0500 Subject: [willow]: more cleanup, tests (#12) --- willow/lib/IR/BasicBlock.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 willow/lib/IR/BasicBlock.cpp (limited to 'willow/lib/IR/BasicBlock.cpp') diff --git a/willow/lib/IR/BasicBlock.cpp b/willow/lib/IR/BasicBlock.cpp new file mode 100644 index 0000000..ceb4653 --- /dev/null +++ b/willow/lib/IR/BasicBlock.cpp @@ -0,0 +1,54 @@ +#include + +namespace willow { + +void BasicBlock::push_back(Instruction &inst) { + assert(!inst.hasParent() && "Instruction is already parented"); + body.push_back(inst); + inst.setParent(this); +} + +void BasicBlock::push_front(Instruction &inst) { + assert(!inst.hasParent() && "Instruction is already parented"); + body.push_front(inst); + inst.setParent(this); +} + +BasicBlock::Iterator BasicBlock::insert(Iterator pos, Instruction &inst) { + assert(!inst.hasParent() && "Instruction is already parented"); + auto it = body.insert(pos, inst); + inst.setParent(this); + return it; +} + +BasicBlock::Iterator BasicBlock::erase(BasicBlock::Iterator pos) { + Instruction &I = *pos; + I.setParent(nullptr); + return body.erase(pos); +} + +BasicBlock::Iterator BasicBlock::eraseAndDelete(BasicBlock::Iterator pos) { + Instruction &inst = *pos; + pos->setParent(nullptr); + auto it = body.erase(pos); + delete &inst; + return it; +} + +void BasicBlock::addPred(BasicBlock *bb) { + auto [it, inserted] = predecessors.try_emplace(bb, 1); + + if (!inserted) + it->second += 1; +} + +void BasicBlock::delPred(BasicBlock *bb) { + auto it = preds().find(bb); + + it->second -= 1; + + if (it->second <= 0) + preds().erase(it); +} + +} // namespace willow -- cgit v1.2.3