#include "llvm/BasicBlock.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Type.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/LeakDetector.h"
#include "SymbolTableListTraitsImpl.h"
#include <algorithm>
using namespace llvm;
ValueSymbolTable *BasicBlock::getValueSymbolTable() {
if (Function *F = getParent())
return &F->getValueSymbolTable();
return 0;
}
LLVMContext &BasicBlock::getContext() const {
return getType()->getContext();
}
template class llvm::SymbolTableListTraits<Instruction, BasicBlock>;
BasicBlock::BasicBlock(LLVMContext &C, const Twine &Name, Function *NewParent,
BasicBlock *InsertBefore)
: Value(Type::getLabelTy(C), Value::BasicBlockVal), Parent(0) {
LeakDetector::addGarbageObject(this);
if (InsertBefore) {
assert(NewParent &&
"Cannot insert block before another block with no function!");
NewParent->getBasicBlockList().insert(InsertBefore, this);
} else if (NewParent) {
NewParent->getBasicBlockList().push_back(this);
}
setName(Name);
}
BasicBlock::~BasicBlock() {
if (hasAddressTaken()) {
assert(!use_empty() && "There should be at least one blockaddress!");
Constant *Replacement =
ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
while (!use_empty()) {
BlockAddress *BA = cast<BlockAddress>(use_back());
BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
BA->getType()));
BA->destroyConstant();
}
}
assert(getParent() == 0 && "BasicBlock still linked into the program!");
dropAllReferences();
InstList.clear();
}
void BasicBlock::setParent(Function *parent) {
if (getParent())
LeakDetector::addGarbageObject(this);
InstList.setSymTabObject(&Parent, parent);
if (getParent())
LeakDetector::removeGarbageObject(this);
}
void BasicBlock::removeFromParent() {
getParent()->getBasicBlockList().remove(this);
}
void BasicBlock::eraseFromParent() {
getParent()->getBasicBlockList().erase(this);
}
void BasicBlock::moveBefore(BasicBlock *MovePos) {
MovePos->getParent()->getBasicBlockList().splice(MovePos,
getParent()->getBasicBlockList(), this);
}
void BasicBlock::moveAfter(BasicBlock *MovePos) {
Function::iterator I = MovePos;
MovePos->getParent()->getBasicBlockList().splice(++I,
getParent()->getBasicBlockList(), this);
}
TerminatorInst *BasicBlock::getTerminator() {
if (InstList.empty()) return 0;
return dyn_cast<TerminatorInst>(&InstList.back());
}
const TerminatorInst *BasicBlock::getTerminator() const {
if (InstList.empty()) return 0;
return dyn_cast<TerminatorInst>(&InstList.back());
}
Instruction* BasicBlock::getFirstNonPHI() {
BasicBlock::iterator i = begin();
while (isa<PHINode>(i)) ++i;
return &*i;
}
Instruction* BasicBlock::getFirstNonPHIOrDbg() {
BasicBlock::iterator i = begin();
while (isa<PHINode>(i) || isa<DbgInfoIntrinsic>(i)) ++i;
return &*i;
}
void BasicBlock::dropAllReferences() {
for(iterator I = begin(), E = end(); I != E; ++I)
I->dropAllReferences();
}
BasicBlock *BasicBlock::getSinglePredecessor() {
pred_iterator PI = pred_begin(this), E = pred_end(this);
if (PI == E) return 0; BasicBlock *ThePred = *PI;
++PI;
return (PI == E) ? ThePred : 0 ;
}
BasicBlock *BasicBlock::getUniquePredecessor() {
pred_iterator PI = pred_begin(this), E = pred_end(this);
if (PI == E) return 0; BasicBlock *PredBB = *PI;
++PI;
for (;PI != E; ++PI) {
if (*PI != PredBB)
return 0;
}
return PredBB;
}
void BasicBlock::removePredecessor(BasicBlock *Pred,
bool DontDeleteUselessPHIs) {
assert((hasNUsesOrMore(16)|| find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) &&
"removePredecessor: BB is not a predecessor!");
if (InstList.empty()) return;
PHINode *APN = dyn_cast<PHINode>(&front());
if (!APN) return;
unsigned max_idx = APN->getNumIncomingValues();
assert(max_idx != 0 && "PHI Node in block with 0 predecessors!?!?!");
if (max_idx == 2) {
BasicBlock *Other = APN->getIncomingBlock(APN->getIncomingBlock(0) == Pred);
if (this == Other) max_idx = 3;
}
if (max_idx <= 2 && !DontDeleteUselessPHIs) {
while (PHINode *PN = dyn_cast<PHINode>(&front())) {
PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs);
if (max_idx == 2) {
if (PN->getOperand(0) != PN)
PN->replaceAllUsesWith(PN->getOperand(0));
else
PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
getInstList().pop_front(); }
}
} else {
PHINode *PN;
for (iterator II = begin(); (PN = dyn_cast<PHINode>(II)); ) {
++II;
PN->removeIncomingValue(Pred, false);
Value* PNV = 0;
if (!DontDeleteUselessPHIs && (PNV = PN->hasConstantValue()))
if (PNV != PN) {
PN->replaceAllUsesWith(PNV);
PN->eraseFromParent();
}
}
}
}
BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName) {
assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
assert(I != InstList.end() &&
"Trying to get me to create degenerate basic block!");
BasicBlock *InsertBefore = llvm::next(Function::iterator(this))
.getNodePtrUnchecked();
BasicBlock *New = BasicBlock::Create(getContext(), BBName,
getParent(), InsertBefore);
New->getInstList().splice(New->end(), this->getInstList(), I, end());
BranchInst::Create(New, this);
for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
BasicBlock *Successor = *I;
PHINode *PN;
for (BasicBlock::iterator II = Successor->begin();
(PN = dyn_cast<PHINode>(II)); ++II) {
int IDX = PN->getBasicBlockIndex(this);
while (IDX != -1) {
PN->setIncomingBlock((unsigned)IDX, New);
IDX = PN->getBasicBlockIndex(this);
}
}
}
return New;
}