Local.cpp revision 694e37f08a7c09ccc24642532106295cf7b3a1e3
1//===-- 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 propagation...
14//
15
16// ConstantFoldInstruction - If an instruction references constants, try to fold
17// them together...
18//
19bool doConstantPropagation(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);
23
24    // Remove the instruction from the basic block...
25    II = II->getParent()->getInstList().erase(II);
26    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//
36bool ConstantFoldTerminator(BasicBlock *BB) {
37  TerminatorInst *T = BB->getTerminator();
38
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      return true;
64    } else if (Dest2 == Dest1) {       // Conditional branch to same location?
65      // 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    }
77  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
78    if (ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition())) {
79
80
81    }
82
83  }
84  return false;
85}
86
87
88
89//===----------------------------------------------------------------------===//
90//  Local dead code elimination...
91//
92
93bool isInstructionTriviallyDead(Instruction *I) {
94  return I->use_empty() && !I->mayWriteToMemory() && !isa<TerminatorInst>(I);
95}
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//
102bool dceInstruction(BasicBlock::iterator &BBI) {
103  // Look for un"used" definitions...
104  if (isInstructionTriviallyDead(BBI)) {
105    BBI = BBI->getParent()->getInstList().erase(BBI);   // Bye bye
106    return true;
107  }
108  return false;
109}
110