#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Constants.h"
#include "llvm/GlobalVariable.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/DebugInfo.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/MathExtras.h"
using namespace llvm;
bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
TerminatorInst *T = BB->getTerminator();
if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
if (BI->isUnconditional()) return false; BasicBlock *Dest1 = BI->getSuccessor(0);
BasicBlock *Dest2 = BI->getSuccessor(1);
if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;
assert(BI->getParent() && "Terminator not inserted in block!");
OldDest->removePredecessor(BI->getParent());
BI->setUnconditionalDest(Destination);
return true;
} else if (Dest2 == Dest1) {
assert(BI->getParent() && "Terminator not inserted in block!");
Dest1->removePredecessor(BI->getParent());
BI->setUnconditionalDest(Dest1);
return true;
}
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
BasicBlock *TheOnlyDest = SI->getSuccessor(0); BasicBlock *DefaultDest = TheOnlyDest;
assert(TheOnlyDest == SI->getDefaultDest() &&
"Default destination is not successor #0?");
for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
if (SI->getSuccessorValue(i) == CI) {
TheOnlyDest = SI->getSuccessor(i);
break;
}
if (SI->getSuccessor(i) == DefaultDest) {
DefaultDest->removePredecessor(SI->getParent());
SI->removeCase(i);
--i; --e; continue;
}
if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
}
if (CI && !TheOnlyDest) {
TheOnlyDest = SI->getDefaultDest();
}
if (TheOnlyDest) {
BranchInst::Create(TheOnlyDest, SI);
BasicBlock *BB = SI->getParent();
for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
BasicBlock *Succ = SI->getSuccessor(i);
if (Succ == TheOnlyDest)
TheOnlyDest = 0; else
Succ->removePredecessor(BB);
}
BB->getInstList().erase(SI);
return true;
} else if (SI->getNumSuccessors() == 2) {
Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(),
SI->getSuccessorValue(1), "cond", SI);
BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
SI->eraseFromParent();
return true;
}
}
return false;
}
bool llvm::isInstructionTriviallyDead(Instruction *I) {
if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
if (isa<DbgInfoIntrinsic>(I)) return false;
if (!I->mayWriteToMemory())
return true;
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
if (II->getIntrinsicID() == Intrinsic::stacksave)
return true;
return false;
}
void llvm::RecursivelyDeleteTriviallyDeadInstructions(Value *V,
SmallVectorImpl<Instruction*> *DeadInst) {
Instruction *I = dyn_cast<Instruction>(V);
if (!I || !I->use_empty() || !isInstructionTriviallyDead(I))
return;
SmallVector<Instruction*, 16> DeadInsts;
DeadInsts.push_back(I);
while (!DeadInsts.empty()) {
I = DeadInsts.back();
DeadInsts.pop_back();
if (DeadInst)
DeadInst->push_back(I);
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Value *OpV = I->getOperand(i);
I->setOperand(i, 0);
if (!OpV->use_empty()) continue;
if (Instruction *OpI = dyn_cast<Instruction>(OpV))
if (isInstructionTriviallyDead(OpI))
DeadInsts.push_back(OpI);
}
I->eraseFromParent();
}
}
void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB) {
while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
Value *NewVal = PN->getIncomingValue(0);
if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
PN->replaceAllUsesWith(NewVal);
PN->eraseFromParent();
}
BasicBlock *PredBB = DestBB->getSinglePredecessor();
assert(PredBB && "Block doesn't have a single predecessor!");
PredBB->getTerminator()->eraseFromParent();
DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
PredBB->replaceAllUsesWith(DestBB);
PredBB->eraseFromParent();
}
bool llvm::OnlyUsedByDbgInfoIntrinsics(Instruction *I,
SmallVectorImpl<DbgInfoIntrinsic *> *DbgInUses) {
if (DbgInUses)
DbgInUses->clear();
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE;
++UI) {
if (DbgInfoIntrinsic *DI = dyn_cast<DbgInfoIntrinsic>(*UI)) {
if (DbgInUses)
DbgInUses->push_back(DI);
} else {
if (DbgInUses)
DbgInUses->clear();
return false;
}
}
return true;
}
bool llvm::UserIsDebugInfo(User *U) {
ConstantExpr *CE = dyn_cast<ConstantExpr>(U);
if (!CE || CE->getNumUses() != 1)
return false;
Constant *Init = dyn_cast<Constant>(CE->use_back());
if (!Init || Init->getNumUses() != 1)
return false;
GlobalVariable *GV = dyn_cast<GlobalVariable>(Init->use_back());
if (!GV || !GV->hasInitializer() || GV->getInitializer() != Init)
return false;
DIVariable DV(GV);
if (!DV.isNull())
return true;
DIGlobalVariable DGV(GV);
if (!DGV.isNull())
return true;
return false;
}
void llvm::RemoveDbgInfoUser(User *U) {
assert (UserIsDebugInfo(U) && "Unexpected User!");
ConstantExpr *CE = cast<ConstantExpr>(U);
while (!CE->use_empty()) {
Constant *C = cast<Constant>(CE->use_back());
while (!C->use_empty()) {
GlobalVariable *GV = cast<GlobalVariable>(C->use_back());
GV->eraseFromParent();
}
C->destroyConstant();
}
CE->destroyConstant();
}