Reapply r131605. This time with a fix, which is to use NoFolder.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@131673 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel
2011-05-19 20:52:46 +00:00
parent a3bff99f0a
commit d3a1788da1

View File

@@ -33,6 +33,7 @@
#include "llvm/Support/ConstantRange.h" #include "llvm/Support/ConstantRange.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"
#include "llvm/Support/NoFolder.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <algorithm> #include <algorithm>
#include <set> #include <set>
@@ -908,6 +909,7 @@ HoistTerminator:
NT->takeName(I1); NT->takeName(I1);
} }
IRBuilder<true, NoFolder> Builder(NT);
// Hoisting one of the terminators from our successor is a great thing. // Hoisting one of the terminators from our successor is a great thing.
// Unfortunately, the successors of the if/else blocks may have PHI nodes in // Unfortunately, the successors of the if/else blocks may have PHI nodes in
// them. If they do, all PHI entries for BB1/BB2 must agree for all PHI // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
@@ -924,11 +926,11 @@ HoistTerminator:
// These values do not agree. Insert a select instruction before NT // These values do not agree. Insert a select instruction before NT
// that determines the right value. // that determines the right value.
SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)]; SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
if (SI == 0) { if (SI == 0)
SI = SelectInst::Create(BI->getCondition(), BB1V, BB2V, SI = cast<SelectInst>
BB1V->getName()+"."+BB2V->getName(), NT); (Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
SI->setDebugLoc(BI->getDebugLoc()); BB1V->getName()+"."+BB2V->getName()));
}
// Make the PHI node use the select for all incoming values for BB1/BB2 // Make the PHI node use the select for all incoming values for BB1/BB2
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2) if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
@@ -1086,14 +1088,16 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
// Create a select whose true value is the speculatively executed value and // Create a select whose true value is the speculatively executed value and
// false value is the previously determined FalseV. // false value is the previously determined FalseV.
IRBuilder<true, NoFolder> Builder(BI);
SelectInst *SI; SelectInst *SI;
if (Invert) if (Invert)
SI = SelectInst::Create(BrCond, FalseV, HInst, SI = cast<SelectInst>
FalseV->getName() + "." + HInst->getName(), BI); (Builder.CreateSelect(BrCond, FalseV, HInst,
FalseV->getName() + "." + HInst->getName()));
else else
SI = SelectInst::Create(BrCond, HInst, FalseV, SI = cast<SelectInst>
HInst->getName() + "." + FalseV->getName(), BI); (Builder.CreateSelect(BrCond, HInst, FalseV,
SI->setDebugLoc(BI->getDebugLoc()); HInst->getName() + "." + FalseV->getName()));
// Make the PHI node use the select for all incoming values for "then" and // Make the PHI node use the select for all incoming values for "then" and
// "if" blocks. // "if" blocks.
@@ -1232,8 +1236,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI, const TargetData *TD) {
/// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry /// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
/// PHI node, see if we can eliminate it. /// PHI node, see if we can eliminate it.
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD, static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
IRBuilder<> &Builder) {
// Ok, this is a two entry PHI node. Check to see if this is a simple "if // Ok, this is a two entry PHI node. Check to see if this is a simple "if
// statement", which has a very simple dominance structure. Basically, we // statement", which has a very simple dominance structure. Basically, we
// are trying to find the condition that is being branched on, which // are trying to find the condition that is being branched on, which
@@ -1332,7 +1335,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD,
// If we can still promote the PHI nodes after this gauntlet of tests, // If we can still promote the PHI nodes after this gauntlet of tests,
// do all of the PHI's now. // do all of the PHI's now.
Instruction *InsertPt = DomBlock->getTerminator(); Instruction *InsertPt = DomBlock->getTerminator();
Builder.SetInsertPoint(InsertPt); IRBuilder<true, NoFolder> Builder(InsertPt);
// Move all 'aggressive' instructions, which are defined in the // Move all 'aggressive' instructions, which are defined in the
// conditional parts of the if's up to the dominating block. // conditional parts of the if's up to the dominating block.
@@ -1460,6 +1463,7 @@ static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
/// the predecessor and use logical operations to pick the right destination. /// the predecessor and use logical operations to pick the right destination.
bool llvm::FoldBranchToCommonDest(BranchInst *BI) { bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
BasicBlock *BB = BI->getParent(); BasicBlock *BB = BI->getParent();
Instruction *Cond = dyn_cast<Instruction>(BI->getCondition()); Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
if (Cond == 0 || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) || if (Cond == 0 || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
Cond->getParent() != BB || !Cond->hasOneUse()) Cond->getParent() != BB || !Cond->hasOneUse())
@@ -1580,7 +1584,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
} }
DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB); DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
IRBuilder<> Builder(PBI);
// If we need to invert the condition in the pred block to match, do so now. // If we need to invert the condition in the pred block to match, do so now.
if (InvertPredCond) { if (InvertPredCond) {
Value *NewCond = PBI->getCondition(); Value *NewCond = PBI->getCondition();
@@ -1589,8 +1594,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
CmpInst *CI = cast<CmpInst>(NewCond); CmpInst *CI = cast<CmpInst>(NewCond);
CI->setPredicate(CI->getInversePredicate()); CI->setPredicate(CI->getInversePredicate());
} else { } else {
NewCond = BinaryOperator::CreateNot(NewCond, NewCond = Builder.CreateNot(NewCond,
PBI->getCondition()->getName()+".not", PBI); PBI->getCondition()->getName()+".not");
} }
PBI->setCondition(NewCond); PBI->setCondition(NewCond);
@@ -1617,9 +1622,9 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
New->takeName(Cond); New->takeName(Cond);
Cond->setName(New->getName()+".old"); Cond->setName(New->getName()+".old");
Instruction *NewCond = BinaryOperator::Create(Opc, PBI->getCondition(), Instruction *NewCond =
New, "or.cond", PBI); cast<Instruction>(Builder.CreateBinOp(Opc, PBI->getCondition(),
NewCond->setDebugLoc(PBI->getDebugLoc()); New, "or.cond"));
PBI->setCondition(NewCond); PBI->setCondition(NewCond);
if (PBI->getSuccessor(0) == BB) { if (PBI->getSuccessor(0) == BB) {
AddPredecessorToBlock(TrueDest, PredBlock, BB); AddPredecessorToBlock(TrueDest, PredBlock, BB);
@@ -1762,23 +1767,22 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
} }
DEBUG(dbgs() << *PBI->getParent()->getParent()); DEBUG(dbgs() << *PBI->getParent()->getParent());
// BI may have other predecessors. Because of this, we leave // BI may have other predecessors. Because of this, we leave
// it alone, but modify PBI. // it alone, but modify PBI.
// Make sure we get to CommonDest on True&True directions. // Make sure we get to CommonDest on True&True directions.
Value *PBICond = PBI->getCondition(); Value *PBICond = PBI->getCondition();
IRBuilder<true, NoFolder> Builder(PBI);
if (PBIOp) if (PBIOp)
PBICond = BinaryOperator::CreateNot(PBICond, PBICond = Builder.CreateNot(PBICond, PBICond->getName()+".not");
PBICond->getName()+".not",
PBI);
Value *BICond = BI->getCondition(); Value *BICond = BI->getCondition();
if (BIOp) if (BIOp)
BICond = BinaryOperator::CreateNot(BICond, BICond = Builder.CreateNot(BICond, BICond->getName()+".not");
BICond->getName()+".not",
PBI);
// Merge the conditions. // Merge the conditions.
Value *Cond = BinaryOperator::CreateOr(PBICond, BICond, "brmerge", PBI); Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
// Modify PBI to branch on the new condition to the new dests. // Modify PBI to branch on the new condition to the new dests.
PBI->setCondition(Cond); PBI->setCondition(Cond);
@@ -1801,8 +1805,8 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
Value *PBIV = PN->getIncomingValue(PBBIdx); Value *PBIV = PN->getIncomingValue(PBBIdx);
if (BIV != PBIV) { if (BIV != PBIV) {
// Insert a select in PBI to pick the right value. // Insert a select in PBI to pick the right value.
Value *NV = SelectInst::Create(PBICond, PBIV, BIV, Value *NV = cast<SelectInst>
PBIV->getName()+".mux", PBI); (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName()+".mux"));
PN->setIncomingValue(PBBIdx, NV); PN->setIncomingValue(PBBIdx, NV);
} }
} }
@@ -2665,7 +2669,7 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
// eliminate it, do so now. // eliminate it, do so now.
if (PHINode *PN = dyn_cast<PHINode>(BB->begin())) if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
if (PN->getNumIncomingValues() == 2) if (PN->getNumIncomingValues() == 2)
Changed |= FoldTwoEntryPHINode(PN, TD, Builder); Changed |= FoldTwoEntryPHINode(PN, TD);
Builder.SetInsertPoint(BB->getTerminator()); Builder.SetInsertPoint(BB->getTerminator());
if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {