aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-11-10 22:26:15 +0000
committerChris Lattner <sabre@nondot.org>2009-11-10 22:26:15 +0000
commit40d8c28b27377199b7465ba2c5a2c59c6fd12fa9 (patch)
tree236e0fd587325e3ea4c3eedf68afa6e8e1470dd9
parent5b37fbac1aece6bb970656a4f847a6af71dfff23 (diff)
downloadexternal_llvm-40d8c28b27377199b7465ba2c5a2c59c6fd12fa9.zip
external_llvm-40d8c28b27377199b7465ba2c5a2c59c6fd12fa9.tar.gz
external_llvm-40d8c28b27377199b7465ba2c5a2c59c6fd12fa9.tar.bz2
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
-rw-r--r--include/llvm/Analysis/InstructionSimplify.h9
-rw-r--r--include/llvm/Transforms/Utils/Local.h15
-rw-r--r--lib/Analysis/InstructionSimplify.cpp35
-rw-r--r--lib/Transforms/Scalar/JumpThreading.cpp83
-rw-r--r--lib/Transforms/Utils/Local.cpp49
5 files changed, 106 insertions, 85 deletions
diff --git a/include/llvm/Analysis/InstructionSimplify.h b/include/llvm/Analysis/InstructionSimplify.h
index bb5c811..aa5c0f5 100644
--- a/include/llvm/Analysis/InstructionSimplify.h
+++ b/include/llvm/Analysis/InstructionSimplify.h
@@ -59,6 +59,15 @@ namespace llvm {
/// instruction. If not, this returns null.
Value *SimplifyInstruction(Instruction *I, const TargetData *TD = 0);
+
+ /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
+ /// delete the From instruction. In addition to a basic RAUW, this does a
+ /// recursive simplification of the updated instructions. This catches
+ /// things where one simplification exposes other opportunities. This only
+ /// simplifies and deletes scalar operations, it does not change the CFG.
+ ///
+ void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
+ const TargetData *TD = 0);
} // end namespace llvm
#endif
diff --git a/include/llvm/Transforms/Utils/Local.h b/include/llvm/Transforms/Utils/Local.h
index 24be83c..292af1d 100644
--- a/include/llvm/Transforms/Utils/Local.h
+++ b/include/llvm/Transforms/Utils/Local.h
@@ -78,6 +78,21 @@ void RecursivelyDeleteDeadPHINode(PHINode *PN);
// 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 RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
+ TargetData *TD = 0);
+
+
/// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
/// predecessor is known to have one successor (BB!). Eliminate the edge
/// between them, moving the instructions in the predecessor into BB. This
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp
index 6953f16..f9953e3 100644
--- a/lib/Analysis/InstructionSimplify.cpp
+++ b/lib/Analysis/InstructionSimplify.cpp
@@ -15,6 +15,7 @@
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/ConstantFolding.h"
+#include "llvm/Support/ValueHandle.h"
#include "llvm/Instructions.h"
#include "llvm/Support/PatternMatch.h"
using namespace llvm;
@@ -311,3 +312,37 @@ Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) {
}
}
+/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
+/// delete the From instruction. In addition to a basic RAUW, this does a
+/// recursive simplification of the newly formed instructions. This catches
+/// things where one simplification exposes other opportunities. This only
+/// simplifies and deletes scalar operations, it does not change the CFG.
+///
+void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
+ const TargetData *TD) {
+ assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
+
+ // FromHandle - This keeps a weakvh on the from value so that we can know if
+ // it gets deleted out from under us in a recursive simplification.
+ WeakVH FromHandle(From);
+
+ while (!From->use_empty()) {
+ // Update the instruction to use the new value.
+ Use &U = From->use_begin().getUse();
+ Instruction *User = cast<Instruction>(U.getUser());
+ U = To;
+
+ // See if we can simplify it.
+ if (Value *V = SimplifyInstruction(User, TD)) {
+ // Recursively simplify this.
+ ReplaceAndSimplifyAllUses(User, V, TD);
+
+ // If the recursive simplification ended up revisiting and deleting 'From'
+ // then we're done.
+ if (FromHandle == 0)
+ return;
+ }
+ }
+ From->eraseFromParent();
+}
+
diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp
index 2d8309d..36cc1fa 100644
--- a/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/lib/Transforms/Scalar/JumpThreading.cpp
@@ -203,89 +203,6 @@ static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
return Size;
}
-
-//===----------------------------------------------------------------------===//
-
-/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
-/// delete the From instruction. In addition to a basic RAUW, this does a
-/// recursive simplification of the newly formed instructions. This catches
-/// things where one simplification exposes other opportunities. This only
-/// simplifies and deletes scalar operations, it does not change the CFG.
-///
-static void ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
- const TargetData *TD) {
- assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
-
- // FromHandle - This keeps a weakvh on the from value so that we can know if
- // it gets deleted out from under us in a recursive simplification.
- WeakVH FromHandle(From);
-
- while (!From->use_empty()) {
- // Update the instruction to use the new value.
- Use &U = From->use_begin().getUse();
- Instruction *User = cast<Instruction>(U.getUser());
- U = To;
-
- // See if we can simplify it.
- if (Value *V = SimplifyInstruction(User, TD)) {
- // Recursively simplify this.
- ReplaceAndSimplifyAllUses(User, V, TD);
-
- // If the recursive simplification ended up revisiting and deleting 'From'
- // then we're done.
- if (FromHandle == 0)
- return;
- }
- }
- From->eraseFromParent();
-}
-
-
-/// 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.
-static void 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();
- }
-}
-
-//===----------------------------------------------------------------------===//
-
-
/// FindLoopHeaders - We do not want jump threading to turn proper loop
/// structures into irreducible loops. Doing this breaks up the loop nesting
/// hierarchy and pessimizes later transformations. To prevent this from
diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp
index 70afce9..0167ea0 100644
--- a/lib/Transforms/Utils/Local.cpp
+++ b/lib/Transforms/Utils/Local.cpp
@@ -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