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 propogation... 14 // 15 16 // ConstantFoldInstruction - If an instruction references constants, try to fold 17 // them together... 18 // 19 bool doConstantPropogation(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 // 36 bool 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 } 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 93 bool isInstructionTriviallyDead(Instruction *I) { 94 return I->use_empty() && !I->hasSideEffects() && !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 // 102 bool 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