|
| 1 | +//===- BasicBlcok.cpp - The BasicBlock class of Sandbox IR ----------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/SandboxIR/BasicBlock.h" |
| 10 | +#include "llvm/SandboxIR/Context.h" |
| 11 | +#include "llvm/SandboxIR/SandboxIR.h" // TODO: remove this |
| 12 | + |
| 13 | +namespace llvm::sandboxir { |
| 14 | + |
| 15 | +BBIterator &BBIterator::operator++() { |
| 16 | + auto ItE = BB->end(); |
| 17 | + assert(It != ItE && "Already at end!"); |
| 18 | + ++It; |
| 19 | + if (It == ItE) |
| 20 | + return *this; |
| 21 | + Instruction &NextI = *cast<sandboxir::Instruction>(Ctx->getValue(&*It)); |
| 22 | + unsigned Num = NextI.getNumOfIRInstrs(); |
| 23 | + assert(Num > 0 && "Bad getNumOfIRInstrs()"); |
| 24 | + It = std::next(It, Num - 1); |
| 25 | + return *this; |
| 26 | +} |
| 27 | + |
| 28 | +BBIterator &BBIterator::operator--() { |
| 29 | + assert(It != BB->begin() && "Already at begin!"); |
| 30 | + if (It == BB->end()) { |
| 31 | + --It; |
| 32 | + return *this; |
| 33 | + } |
| 34 | + Instruction &CurrI = **this; |
| 35 | + unsigned Num = CurrI.getNumOfIRInstrs(); |
| 36 | + assert(Num > 0 && "Bad getNumOfIRInstrs()"); |
| 37 | + assert(std::prev(It, Num - 1) != BB->begin() && "Already at begin!"); |
| 38 | + It = std::prev(It, Num); |
| 39 | + return *this; |
| 40 | +} |
| 41 | + |
| 42 | +BasicBlock *BBIterator::getNodeParent() const { |
| 43 | + llvm::BasicBlock *Parent = const_cast<BBIterator *>(this)->It.getNodeParent(); |
| 44 | + return cast<BasicBlock>(Ctx->getValue(Parent)); |
| 45 | +} |
| 46 | + |
| 47 | +BasicBlock::iterator::pointer |
| 48 | +BasicBlock::iterator::getInstr(llvm::BasicBlock::iterator It) const { |
| 49 | + return cast_or_null<Instruction>(Ctx->getValue(&*It)); |
| 50 | +} |
| 51 | + |
| 52 | +Function *BasicBlock::getParent() const { |
| 53 | + auto *BB = cast<llvm::BasicBlock>(Val); |
| 54 | + auto *F = BB->getParent(); |
| 55 | + if (F == nullptr) |
| 56 | + // Detached |
| 57 | + return nullptr; |
| 58 | + return cast_or_null<Function>(Ctx.getValue(F)); |
| 59 | +} |
| 60 | + |
| 61 | +void BasicBlock::buildBasicBlockFromLLVMIR(llvm::BasicBlock *LLVMBB) { |
| 62 | + for (llvm::Instruction &IRef : reverse(*LLVMBB)) { |
| 63 | + llvm::Instruction *I = &IRef; |
| 64 | + Ctx.getOrCreateValue(I); |
| 65 | + for (auto [OpIdx, Op] : enumerate(I->operands())) { |
| 66 | + // Skip instruction's label operands |
| 67 | + if (isa<llvm::BasicBlock>(Op)) |
| 68 | + continue; |
| 69 | + // Skip metadata |
| 70 | + if (isa<llvm::MetadataAsValue>(Op)) |
| 71 | + continue; |
| 72 | + // Skip asm |
| 73 | + if (isa<llvm::InlineAsm>(Op)) |
| 74 | + continue; |
| 75 | + Ctx.getOrCreateValue(Op); |
| 76 | + } |
| 77 | + } |
| 78 | +#if !defined(NDEBUG) |
| 79 | + verify(); |
| 80 | +#endif |
| 81 | +} |
| 82 | + |
| 83 | +BasicBlock::iterator BasicBlock::begin() const { |
| 84 | + llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val); |
| 85 | + llvm::BasicBlock::iterator It = BB->begin(); |
| 86 | + if (!BB->empty()) { |
| 87 | + auto *V = Ctx.getValue(&*BB->begin()); |
| 88 | + assert(V != nullptr && "No SandboxIR for BB->begin()!"); |
| 89 | + auto *I = cast<Instruction>(V); |
| 90 | + unsigned Num = I->getNumOfIRInstrs(); |
| 91 | + assert(Num >= 1u && "Bad getNumOfIRInstrs()"); |
| 92 | + It = std::next(It, Num - 1); |
| 93 | + } |
| 94 | + return iterator(BB, It, &Ctx); |
| 95 | +} |
| 96 | + |
| 97 | +Instruction *BasicBlock::getTerminator() const { |
| 98 | + auto *TerminatorV = |
| 99 | + Ctx.getValue(cast<llvm::BasicBlock>(Val)->getTerminator()); |
| 100 | + return cast_or_null<Instruction>(TerminatorV); |
| 101 | +} |
| 102 | + |
| 103 | +Instruction &BasicBlock::front() const { |
| 104 | + auto *BB = cast<llvm::BasicBlock>(Val); |
| 105 | + assert(!BB->empty() && "Empty block!"); |
| 106 | + auto *SBI = cast<Instruction>(getContext().getValue(&*BB->begin())); |
| 107 | + assert(SBI != nullptr && "Expected Instr!"); |
| 108 | + return *SBI; |
| 109 | +} |
| 110 | + |
| 111 | +Instruction &BasicBlock::back() const { |
| 112 | + auto *BB = cast<llvm::BasicBlock>(Val); |
| 113 | + assert(!BB->empty() && "Empty block!"); |
| 114 | + auto *SBI = cast<Instruction>(getContext().getValue(&*BB->rbegin())); |
| 115 | + assert(SBI != nullptr && "Expected Instr!"); |
| 116 | + return *SBI; |
| 117 | +} |
| 118 | + |
| 119 | +#ifndef NDEBUG |
| 120 | +void BasicBlock::dumpOS(raw_ostream &OS) const { |
| 121 | + llvm::BasicBlock *BB = cast<llvm::BasicBlock>(Val); |
| 122 | + const auto &Name = BB->getName(); |
| 123 | + OS << Name; |
| 124 | + if (!Name.empty()) |
| 125 | + OS << ":\n"; |
| 126 | + // If there are Instructions in the BB that are not mapped to SandboxIR, then |
| 127 | + // use a crash-proof dump. |
| 128 | + if (any_of(*BB, [this](llvm::Instruction &I) { |
| 129 | + return Ctx.getValue(&I) == nullptr; |
| 130 | + })) { |
| 131 | + OS << "<Crash-proof mode!>\n"; |
| 132 | + DenseSet<Instruction *> Visited; |
| 133 | + for (llvm::Instruction &IRef : *BB) { |
| 134 | + Value *SBV = Ctx.getValue(&IRef); |
| 135 | + if (SBV == nullptr) |
| 136 | + OS << IRef << " *** No SandboxIR ***\n"; |
| 137 | + else { |
| 138 | + auto *SBI = dyn_cast<Instruction>(SBV); |
| 139 | + if (SBI == nullptr) { |
| 140 | + OS << IRef << " *** Not a SBInstruction!!! ***\n"; |
| 141 | + } else { |
| 142 | + if (Visited.insert(SBI).second) |
| 143 | + OS << *SBI << "\n"; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } else { |
| 148 | + for (auto &SBI : *this) { |
| 149 | + SBI.dumpOS(OS); |
| 150 | + OS << "\n"; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +void BasicBlock::verify() const { |
| 156 | + assert(isa<llvm::BasicBlock>(Val) && "Expected BasicBlock!"); |
| 157 | + for (const auto &I : *this) { |
| 158 | + I.verify(); |
| 159 | + } |
| 160 | +} |
| 161 | +#endif // NDEBUG |
| 162 | + |
| 163 | +} // namespace llvm::sandboxir |
0 commit comments