blob: 8f67e111a780d34425ef7d5dfed8eff3e0e2136b [file] [log] [blame]
Chris Lattner4d1e46e2002-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//
Chris Lattner16da4942002-05-26 20:18:18 +000019bool doConstantPropogation(BasicBlock::iterator &II) {
Chris Lattner18961502002-06-25 16:12:52 +000020 if (Constant *C = ConstantFoldInstruction(II)) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +000021 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattner18961502002-06-25 16:12:52 +000022 II->replaceAllUsesWith(C);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000023
24 // Remove the instruction from the basic block...
Chris Lattner18961502002-06-25 16:12:52 +000025 II = II->getParent()->getInstList().erase(II);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000026 return true;
27 }
28
29 return false;
30}
31
32// ConstantFoldTerminator - If a terminator instruction is predicated on a
33// constant value, convert it into an unconditional branch to the constant
34// destination.
35//
Chris Lattner76ae3442002-05-21 20:04:50 +000036bool ConstantFoldTerminator(BasicBlock *BB) {
37 TerminatorInst *T = BB->getTerminator();
38
Chris Lattner4d1e46e2002-05-07 18:07:59 +000039 // 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);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000063 return true;
64 }
65#if 0
66 // FIXME: TODO: This doesn't work if the destination has PHI nodes with
67 // different incoming values on each branch!
68 //
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
73
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());
77
78 // Change a conditional branch to unconditional.
79 BI->setUnconditionalDest(Dest1);
80 return true;
81 }
82#endif
83 }
84 return false;
85}
86
87
88
89//===----------------------------------------------------------------------===//
90// Local dead code elimination...
91//
92
93bool isInstructionTriviallyDead(Instruction *I) {
Chris Lattnerf0a93ed2003-02-24 20:48:32 +000094 return I->use_empty() && !I->mayWriteToMemory() && !isa<TerminatorInst>(I);
Chris Lattner4d1e46e2002-05-07 18:07:59 +000095}
96
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
100// instruction.
101//
Chris Lattner16da4942002-05-26 20:18:18 +0000102bool dceInstruction(BasicBlock::iterator &BBI) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000103 // Look for un"used" definitions...
Chris Lattner18961502002-06-25 16:12:52 +0000104 if (isInstructionTriviallyDead(BBI)) {
105 BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000106 return true;
107 }
108 return false;
109}