blob: 3ea76c4ea51dae41bf86a2d75b6b7c83233f407a [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//===----------------------------------------------------------------------===//
Misha Brukman373086d2003-05-20 21:01:22 +000013// Local constant propagation...
Chris Lattner28537df2002-05-07 18:07:59 +000014//
15
16// ConstantFoldInstruction - If an instruction references constants, try to fold
17// them together...
18//
Misha Brukman373086d2003-05-20 21:01:22 +000019bool doConstantPropagation(BasicBlock::iterator &II) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000020 if (Constant *C = ConstantFoldInstruction(II)) {
Chris Lattner28537df2002-05-07 18:07:59 +000021 // Replaces all of the uses of a variable with uses of the constant.
Chris Lattnerfda72b12002-06-25 16:12:52 +000022 II->replaceAllUsesWith(C);
Chris Lattner28537df2002-05-07 18:07:59 +000023
24 // Remove the instruction from the basic block...
Chris Lattnerfda72b12002-06-25 16:12:52 +000025 II = II->getParent()->getInstList().erase(II);
Chris Lattner28537df2002-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 Lattner4b009ad2002-05-21 20:04:50 +000036bool ConstantFoldTerminator(BasicBlock *BB) {
37 TerminatorInst *T = BB->getTerminator();
38
Chris Lattner28537df2002-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 Lattner28537df2002-05-07 18:07:59 +000063 return true;
Chris Lattner4b7e3362003-08-17 19:34:55 +000064 } else if (Dest2 == Dest1) { // Conditional branch to same location?
Chris Lattner28537df2002-05-07 18:07:59 +000065 // This branch matches something like this:
66 // br bool %cond, label %Dest, label %Dest
67 // and changes it into: br label %Dest
68
69 // Let the basic block know that we are letting go of one copy of it.
70 assert(BI->getParent() && "Terminator not inserted in block!");
71 Dest1->removePredecessor(BI->getParent());
72
73 // Change a conditional branch to unconditional.
74 BI->setUnconditionalDest(Dest1);
75 return true;
76 }
Chris Lattner031340a2003-08-17 19:41:53 +000077 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
78 if (ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition())) {
79
80
81 }
82
Chris Lattner28537df2002-05-07 18:07:59 +000083 }
84 return false;
85}
86
87
88
89//===----------------------------------------------------------------------===//
90// Local dead code elimination...
91//
92
93bool isInstructionTriviallyDead(Instruction *I) {
Chris Lattner4869f372003-02-24 20:48:32 +000094 return I->use_empty() && !I->mayWriteToMemory() && !isa<TerminatorInst>(I);
Chris Lattner28537df2002-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 Lattnerab038d42002-05-26 20:18:18 +0000102bool dceInstruction(BasicBlock::iterator &BBI) {
Chris Lattner28537df2002-05-07 18:07:59 +0000103 // Look for un"used" definitions...
Chris Lattnerfda72b12002-06-25 16:12:52 +0000104 if (isInstructionTriviallyDead(BBI)) {
105 BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye
Chris Lattner28537df2002-05-07 18:07:59 +0000106 return true;
107 }
108 return false;
109}