move some generally useful functions out of jump threading

into libanalysis and transformutils.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86735 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2009-11-10 22:26:15 +00:00
parent 5b37fbac1a
commit 40d8c28b27
5 changed files with 106 additions and 85 deletions

View File

@ -24,6 +24,7 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/DebugInfo.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/ProfileInfo.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Support/CFG.h"
@ -239,7 +240,7 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB) {
//===----------------------------------------------------------------------===//
// Local dead code elimination...
// Local dead code elimination.
//
/// isInstructionTriviallyDead - Return true if the result produced by the
@ -326,9 +327,53 @@ llvm::RecursivelyDeleteDeadPHINode(PHINode *PN) {
}
//===----------------------------------------------------------------------===//
// Control Flow Graph Restructuring...
// Control Flow Graph Restructuring.
//
/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
/// method is called when we're about to delete Pred as a predecessor of BB. If
/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
///
/// Unlike the removePredecessor method, this attempts to simplify uses of PHI
/// nodes that collapse into identity values. For example, if we have:
/// x = phi(1, 0, 0, 0)
/// y = and x, z
///
/// .. and delete the predecessor corresponding to the '1', this will attempt to
/// recursively fold the and to 0.
void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
TargetData *TD) {
// This only adjusts blocks with PHI nodes.
if (!isa<PHINode>(BB->begin()))
return;
// Remove the entries for Pred from the PHI nodes in BB, but do not simplify
// them down. This will leave us with single entry phi nodes and other phis
// that can be removed.
BB->removePredecessor(Pred, true);
WeakVH PhiIt = &BB->front();
while (PHINode *PN = dyn_cast<PHINode>(PhiIt)) {
PhiIt = &*++BasicBlock::iterator(cast<Instruction>(PhiIt));
Value *PNV = PN->hasConstantValue();
if (PNV == 0) continue;
// If we're able to simplify the phi to a single value, substitute the new
// value into all of its uses.
assert(PNV != PN && "hasConstantValue broken");
ReplaceAndSimplifyAllUses(PN, PNV, TD);
// If recursive simplification ended up deleting the next PHI node we would
// iterate to, then our iterator is invalid, restart scanning from the top
// of the block.
if (PhiIt == 0) PhiIt = &BB->front();
}
}
/// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
/// predecessor is known to have one successor (DestBB!). Eliminate the edge
/// between them, moving the instructions in the predecessor into DestBB and