2002-05-07 18:07:59 +00:00
|
|
|
//===-- Local.cpp - Functions to perform local transformations ------------===//
|
|
|
|
//
|
|
|
|
// This family of functions perform various local transformations to the
|
|
|
|
// program.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
#include "llvm/iTerminators.h"
|
|
|
|
#include "llvm/ConstantHandling.h"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-05-20 21:01:22 +00:00
|
|
|
// Local constant propagation...
|
2002-05-07 18:07:59 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
// ConstantFoldInstruction - If an instruction references constants, try to fold
|
|
|
|
// them together...
|
|
|
|
//
|
2003-05-20 21:01:22 +00:00
|
|
|
bool doConstantPropagation(BasicBlock::iterator &II) {
|
2002-06-25 16:12:52 +00:00
|
|
|
if (Constant *C = ConstantFoldInstruction(II)) {
|
2002-05-07 18:07:59 +00:00
|
|
|
// Replaces all of the uses of a variable with uses of the constant.
|
2002-06-25 16:12:52 +00:00
|
|
|
II->replaceAllUsesWith(C);
|
2002-05-07 18:07:59 +00:00
|
|
|
|
|
|
|
// Remove the instruction from the basic block...
|
2002-06-25 16:12:52 +00:00
|
|
|
II = II->getParent()->getInstList().erase(II);
|
2002-05-07 18:07:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConstantFoldTerminator - If a terminator instruction is predicated on a
|
|
|
|
// constant value, convert it into an unconditional branch to the constant
|
|
|
|
// destination.
|
|
|
|
//
|
2002-05-21 20:04:50 +00:00
|
|
|
bool ConstantFoldTerminator(BasicBlock *BB) {
|
|
|
|
TerminatorInst *T = BB->getTerminator();
|
|
|
|
|
2002-05-07 18:07:59 +00:00
|
|
|
// Branch - See if we are conditional jumping on constant
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
|
|
|
|
if (BI->isUnconditional()) return false; // Can't optimize uncond branch
|
|
|
|
BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
|
|
|
|
BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
|
|
|
|
|
|
|
|
if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
|
|
|
|
// Are we branching on constant?
|
|
|
|
// YES. Change to unconditional branch...
|
|
|
|
BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
|
|
|
|
BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
|
|
|
|
|
|
|
|
//cerr << "Function: " << T->getParent()->getParent()
|
|
|
|
// << "\nRemoving branch from " << T->getParent()
|
|
|
|
// << "\n\nTo: " << OldDest << endl;
|
|
|
|
|
|
|
|
// Let the basic block know that we are letting go of it. Based on this,
|
|
|
|
// it will adjust it's PHI nodes.
|
|
|
|
assert(BI->getParent() && "Terminator not inserted in block!");
|
|
|
|
OldDest->removePredecessor(BI->getParent());
|
|
|
|
|
|
|
|
// Set the unconditional destination, and change the insn to be an
|
|
|
|
// unconditional branch.
|
|
|
|
BI->setUnconditionalDest(Destination);
|
|
|
|
return true;
|
2003-08-17 19:34:55 +00:00
|
|
|
} else if (Dest2 == Dest1) { // Conditional branch to same location?
|
2002-05-07 18:07:59 +00:00
|
|
|
// This branch matches something like this:
|
|
|
|
// br bool %cond, label %Dest, label %Dest
|
|
|
|
// and changes it into: br label %Dest
|
|
|
|
|
|
|
|
// Let the basic block know that we are letting go of one copy of it.
|
|
|
|
assert(BI->getParent() && "Terminator not inserted in block!");
|
|
|
|
Dest1->removePredecessor(BI->getParent());
|
|
|
|
|
|
|
|
// Change a conditional branch to unconditional.
|
|
|
|
BI->setUnconditionalDest(Dest1);
|
|
|
|
return true;
|
|
|
|
}
|
2003-08-17 19:41:53 +00:00
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition())) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-05-07 18:07:59 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Local dead code elimination...
|
|
|
|
//
|
|
|
|
|
|
|
|
bool isInstructionTriviallyDead(Instruction *I) {
|
2003-02-24 20:48:32 +00:00
|
|
|
return I->use_empty() && !I->mayWriteToMemory() && !isa<TerminatorInst>(I);
|
2002-05-07 18:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dceInstruction - Inspect the instruction at *BBI and figure out if it's
|
|
|
|
// [trivially] dead. If so, remove the instruction and update the iterator
|
|
|
|
// to point to the instruction that immediately succeeded the original
|
|
|
|
// instruction.
|
|
|
|
//
|
2002-05-26 20:18:18 +00:00
|
|
|
bool dceInstruction(BasicBlock::iterator &BBI) {
|
2002-05-07 18:07:59 +00:00
|
|
|
// Look for un"used" definitions...
|
2002-06-25 16:12:52 +00:00
|
|
|
if (isInstructionTriviallyDead(BBI)) {
|
|
|
|
BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye
|
2002-05-07 18:07:59 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|