Local.cpp revision 16da494c81a09ea4273e7f3e7ad12eec68b05468
14d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===-- Local.cpp - Functions to perform local transformations ------------===//
24d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
34d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// This family of functions perform various local transformations to the
44d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// program.
54d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
64d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
74d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
84d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#include "llvm/Transforms/Utils/Local.h"
94d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#include "llvm/iTerminators.h"
104d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#include "llvm/ConstantHandling.h"
114d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
124d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
134d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//  Local constant propogation...
144d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
154d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
164d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// ConstantFoldInstruction - If an instruction references constants, try to fold
174d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// them together...
184d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
1916da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattnerbool doConstantPropogation(BasicBlock::iterator &II) {
204d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  Instruction *Inst = *II;
214d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  if (Constant *C = ConstantFoldInstruction(Inst)) {
224d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // Replaces all of the uses of a variable with uses of the constant.
234d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    Inst->replaceAllUsesWith(C);
244d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
254d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // Remove the instruction from the basic block...
2616da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattner    delete Inst->getParent()->getInstList().remove(II);
274d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    return true;
284d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
294d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
304d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
314d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
324d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
334d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// ConstantFoldTerminator - If a terminator instruction is predicated on a
344d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// constant value, convert it into an unconditional branch to the constant
354d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// destination.
364d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
3776ae3445f81164aaff9f95123426109c119f27c0Chris Lattnerbool ConstantFoldTerminator(BasicBlock *BB) {
3876ae3445f81164aaff9f95123426109c119f27c0Chris Lattner  TerminatorInst *T = BB->getTerminator();
3976ae3445f81164aaff9f95123426109c119f27c0Chris Lattner
404d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Branch - See if we are conditional jumping on constant
414d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
424d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
434d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
444d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
454d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
464d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
474d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Are we branching on constant?
484d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // YES.  Change to unconditional branch...
494d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
504d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BasicBlock *OldDest     = Cond->getValue() ? Dest2 : Dest1;
514d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
524d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //cerr << "Function: " << T->getParent()->getParent()
534d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     << "\nRemoving branch from " << T->getParent()
544d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     << "\n\nTo: " << OldDest << endl;
554d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
564d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of it.  Based on this,
574d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // it will adjust it's PHI nodes.
584d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
594d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      OldDest->removePredecessor(BI->getParent());
604d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
614d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Set the unconditional destination, and change the insn to be an
624d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // unconditional branch.
634d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BI->setUnconditionalDest(Destination);
644d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
654d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    }
664d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#if 0
674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // FIXME: TODO: This doesn't work if the destination has PHI nodes with
684d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // different incoming values on each branch!
694d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    //
704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    else if (Dest2 == Dest1) {       // Conditional branch to same location?
714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // This branch matches something like this:
724d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     br bool %cond, label %Dest, label %Dest
734d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // and changes it into:  br label %Dest
744d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
754d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of one copy of it.
764d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
774d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      Dest1->removePredecessor(BI->getParent());
784d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
794d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Change a conditional branch to unconditional.
804d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BI->setUnconditionalDest(Dest1);
814d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
824d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    }
834d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#endif
844d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
854d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
864d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
874d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
884d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
894d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
904d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
914d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//  Local dead code elimination...
924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
934d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
944d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattnerbool isInstructionTriviallyDead(Instruction *I) {
954d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
964d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
974d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
984d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// dceInstruction - Inspect the instruction at *BBI and figure out if it's
994d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// [trivially] dead.  If so, remove the instruction and update the iterator
1004d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// to point to the instruction that immediately succeeded the original
1014d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// instruction.
1024d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
10316da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattnerbool dceInstruction(BasicBlock::iterator &BBI) {
1044d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Look for un"used" definitions...
10516da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattner  Instruction *I = *BBI;
10616da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattner  if (isInstructionTriviallyDead(I)) {
10716da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattner    delete I->getParent()->getInstList().remove(BBI);   // Bye bye
1084d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    return true;
1094d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
1104d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
1114d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
112