1 //===-- Local.cpp - Functions to perform local transformations ------------===//
3 // This family of functions perform various local transformations to the
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Transforms/Utils/Local.h"
9 #include "llvm/iTerminators.h"
10 #include "llvm/ConstantHandling.h"
12 //===----------------------------------------------------------------------===//
13 // Local constant propogation...
16 // ConstantFoldInstruction - If an instruction references constants, try to fold
19 bool doConstantPropogation(BasicBlock::iterator &II) {
20 if (Constant *C = ConstantFoldInstruction(II)) {
21 // Replaces all of the uses of a variable with uses of the constant.
22 II->replaceAllUsesWith(C);
24 // Remove the instruction from the basic block...
25 II = II->getParent()->getInstList().erase(II);
32 // ConstantFoldTerminator - If a terminator instruction is predicated on a
33 // constant value, convert it into an unconditional branch to the constant
36 bool ConstantFoldTerminator(BasicBlock *BB) {
37 TerminatorInst *T = BB->getTerminator();
39 // Branch - See if we are conditional jumping on constant
40 if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
41 if (BI->isUnconditional()) return false; // Can't optimize uncond branch
42 BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
43 BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
45 if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
46 // Are we branching on constant?
47 // YES. Change to unconditional branch...
48 BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
49 BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
51 //cerr << "Function: " << T->getParent()->getParent()
52 // << "\nRemoving branch from " << T->getParent()
53 // << "\n\nTo: " << OldDest << endl;
55 // Let the basic block know that we are letting go of it. Based on this,
56 // it will adjust it's PHI nodes.
57 assert(BI->getParent() && "Terminator not inserted in block!");
58 OldDest->removePredecessor(BI->getParent());
60 // Set the unconditional destination, and change the insn to be an
61 // unconditional branch.
62 BI->setUnconditionalDest(Destination);
66 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
67 // different incoming values on each branch!
69 else if (Dest2 == Dest1) { // Conditional branch to same location?
70 // This branch matches something like this:
71 // br bool %cond, label %Dest, label %Dest
72 // and changes it into: br label %Dest
74 // Let the basic block know that we are letting go of one copy of it.
75 assert(BI->getParent() && "Terminator not inserted in block!");
76 Dest1->removePredecessor(BI->getParent());
78 // Change a conditional branch to unconditional.
79 BI->setUnconditionalDest(Dest1);
89 //===----------------------------------------------------------------------===//
90 // Local dead code elimination...
93 bool isInstructionTriviallyDead(Instruction *I) {
94 return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
97 // dceInstruction - Inspect the instruction at *BBI and figure out if it's
98 // [trivially] dead. If so, remove the instruction and update the iterator
99 // to point to the instruction that immediately succeeded the original
102 bool dceInstruction(BasicBlock::iterator &BBI) {
103 // Look for un"used" definitions...
104 if (isInstructionTriviallyDead(BBI)) {
105 BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye