Local.cpp revision 18961504fc2b299578dba817900a0696cf3ccc4d
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) {
2018961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (Constant *C = ConstantFoldInstruction(II)) {
214d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // Replaces all of the uses of a variable with uses of the constant.
2218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    II->replaceAllUsesWith(C);
234d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
244d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // Remove the instruction from the basic block...
2518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    II = II->getParent()->getInstList().erase(II);
264d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    return true;
274d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
284d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
294d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
304d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
314d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
324d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// ConstantFoldTerminator - If a terminator instruction is predicated on a
334d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// constant value, convert it into an unconditional branch to the constant
344d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// destination.
354d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
3676ae3445f81164aaff9f95123426109c119f27c0Chris Lattnerbool ConstantFoldTerminator(BasicBlock *BB) {
3776ae3445f81164aaff9f95123426109c119f27c0Chris Lattner  TerminatorInst *T = BB->getTerminator();
3876ae3445f81164aaff9f95123426109c119f27c0Chris Lattner
394d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Branch - See if we are conditional jumping on constant
404d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
414d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
424d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
434d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
444d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
454d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
464d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Are we branching on constant?
474d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // YES.  Change to unconditional branch...
484d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
494d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BasicBlock *OldDest     = Cond->getValue() ? Dest2 : Dest1;
504d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
514d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //cerr << "Function: " << T->getParent()->getParent()
524d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     << "\nRemoving branch from " << T->getParent()
534d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     << "\n\nTo: " << OldDest << endl;
544d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
554d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of it.  Based on this,
564d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // it will adjust it's PHI nodes.
574d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
584d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      OldDest->removePredecessor(BI->getParent());
594d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
604d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Set the unconditional destination, and change the insn to be an
614d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // unconditional branch.
624d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BI->setUnconditionalDest(Destination);
634d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
644d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    }
654d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#if 0
664d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // FIXME: TODO: This doesn't work if the destination has PHI nodes with
674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // different incoming values on each branch!
684d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    //
694d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    else if (Dest2 == Dest1) {       // Conditional branch to same location?
704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // This branch matches something like this:
714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     br bool %cond, label %Dest, label %Dest
724d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // and changes it into:  br label %Dest
734d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
744d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of one copy of it.
754d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
764d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      Dest1->removePredecessor(BI->getParent());
774d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
784d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Change a conditional branch to unconditional.
794d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BI->setUnconditionalDest(Dest1);
804d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
814d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    }
824d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#endif
834d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
844d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
854d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
864d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
874d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
884d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
894d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
904d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//  Local dead code elimination...
914d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
934d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattnerbool isInstructionTriviallyDead(Instruction *I) {
944d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return I->use_empty() && !I->hasSideEffects() && !isa<TerminatorInst>(I);
954d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
964d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
974d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// dceInstruction - Inspect the instruction at *BBI and figure out if it's
984d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// [trivially] dead.  If so, remove the instruction and update the iterator
994d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// to point to the instruction that immediately succeeded the original
1004d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// instruction.
1014d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
10216da494c81a09ea4273e7f3e7ad12eec68b05468Chris Lattnerbool dceInstruction(BasicBlock::iterator &BBI) {
1034d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Look for un"used" definitions...
10418961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (isInstructionTriviallyDead(BBI)) {
10518961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    BBI = BBI->getParent()->getInstList().erase(BBI);   // Bye bye
1064d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    return true;
1074d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
1084d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
1094d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
110