-inline static bool
-ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II,
- BinaryOperator *Op,
- Constant *D1, Constant *D2) {
- Constant *ReplaceWith = ConstantFoldBinaryInstruction(Op->getOpcode(), D1,D2);
- if (!ReplaceWith) return false; // Nothing new to change...
-
- // Replaces all of the uses of a variable with uses of the constant.
- Op->replaceAllUsesWith(ReplaceWith);
-
- // Remove the operator from the list of definitions...
- Op->getParent()->getInstList().remove(II);
-
- // The new constant inherits the old name of the operator...
- if (Op->hasName())
- ReplaceWith->setName(Op->getName(), BB->getParent()->getSymbolTableSure());
-
- // Delete the operator now...
- delete Op;
- return true;
-}
-
-// ConstantFoldTerminator - If a terminator instruction is predicated on a
-// constant value, convert it into an unconditional branch to the constant
-// destination.
-//
-bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &II,
- TerminatorInst *T) {
- // Branch - See if we are conditional jumping on constant
- if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
- if (BI->isUnconditional()) return false; // Can't optimize uncond branch
- BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
- BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
-
- if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) {
- // Are we branching on constant?
- // YES. Change to unconditional branch...
- BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
- BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1;
-
- //cerr << "Method: " << T->getParent()->getParent()
- // << "\nRemoving branch from " << T->getParent()
- // << "\n\nTo: " << OldDest << endl;
-
- // Let the basic block know that we are letting go of it. Based on this,
- // it will adjust it's PHI nodes.
- assert(BI->getParent() && "Terminator not inserted in block!");
- OldDest->removePredecessor(BI->getParent());
-
- // Set the unconditional destination, and change the insn to be an
- // unconditional branch.
- BI->setUnconditionalDest(Destination);
- II = BB->end()-1; // Update instruction iterator!
- return true;