blob: 36622642bfa3cdf9e392f858b22cda28d8477bf8 [file] [log] [blame]
Chris Lattner28537df2002-05-07 18:07:59 +00001//===-- Local.cpp - Functions to perform local transformations ------------===//
2//
3// This family of functions perform various local transformations to the
4// program.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Transforms/Utils/Local.h"
9#include "llvm/iTerminators.h"
10#include "llvm/ConstantHandling.h"
11
12//===----------------------------------------------------------------------===//
13// Local constant propogation...
14//
15
16// ConstantFoldInstruction - If an instruction references constants, try to fold
17// them together...
18//
19bool doConstantPropogation(BasicBlock *BB, BasicBlock::iterator &II) {
20 Instruction *Inst = *II;
21 if (Constant *C = ConstantFoldInstruction(Inst)) {
22 // Replaces all of the uses of a variable with uses of the constant.
23 Inst->replaceAllUsesWith(C);
24
25 // Remove the instruction from the basic block...
26 delete BB->getInstList().remove(II);
27 return true;
28 }
29
30 return false;
31}
32
33// ConstantFoldTerminator - If a terminator instruction is predicated on a
34// constant value, convert it into an unconditional branch to the constant
35// destination.
36//
37bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &II,
38 TerminatorInst *T) {
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));
44
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;
50
51 //cerr << "Function: " << T->getParent()->getParent()
52 // << "\nRemoving branch from " << T->getParent()
53 // << "\n\nTo: " << OldDest << endl;
54
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());
59
60 // Set the unconditional destination, and change the insn to be an
61 // unconditional branch.
62 BI->setUnconditionalDest(Destination);
63 II = BB->end()-1; // Update instruction iterator!
64 return true;
65 }
66#if 0
67 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
68 // different incoming values on each branch!
69 //
70 else if (Dest2 == Dest1) { // Conditional branch to same location?
71 // This branch matches something like this:
72 // br bool %cond, label %Dest, label %Dest
73 // and changes it into: br label %Dest
74
75 // Let the basic block know that we are letting go of one copy of it.
76 assert(BI->getParent() && "Terminator not inserted in block!");
77 Dest1->removePredecessor(BI->getParent());
78
79 // Change a conditional branch to unconditional.
80 BI->setUnconditionalDest(Dest1);
81 return true;
82 }
83#endif
84 }
85 return false;
86}
87
88
89
90//===----------------------------------------------------------------------===//
91// Local dead code elimination...
92//
93
94bool isInstructionTriviallyDead(Instruction *I) {
95 return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
96}
97
98// dceInstruction - Inspect the instruction at *BBI and figure out if it's
99// [trivially] dead. If so, remove the instruction and update the iterator
100// to point to the instruction that immediately succeeded the original
101// instruction.
102//
103bool dceInstruction(BasicBlock::InstListType &BBIL,
104 BasicBlock::iterator &BBI) {
105 // Look for un"used" definitions...
106 if (isInstructionTriviallyDead(*BBI)) {
107 delete BBIL.remove(BBI); // Bye bye
108 return true;
109 }
110 return false;
111}