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/Debug.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/Support/NoFolder.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <set>
@ -908,6 +909,7 @@ HoistTerminator:
NT->takeName(I1);
}
IRBuilder<true, NoFolder> Builder(NT);
// 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
// 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
// that determines the right value.
SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
if (SI == 0) {
SI = SelectInst::Create(BI->getCondition(), BB1V, BB2V,
BB1V->getName()+"."+BB2V->getName(), NT);
SI->setDebugLoc(BI->getDebugLoc());
}
if (SI == 0)
SI = cast<SelectInst>
(Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
BB1V->getName()+"."+BB2V->getName()));
// Make the PHI node use the select for all incoming values for BB1/BB2
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
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
// false value is the previously determined FalseV.
IRBuilder<true, NoFolder> Builder(BI);
SelectInst *SI;
if (Invert)
SI = SelectInst::Create(BrCond, FalseV, HInst,
FalseV->getName() + "." + HInst->getName(), BI);
SI = cast<SelectInst>
(Builder.CreateSelect(BrCond, FalseV, HInst,
FalseV->getName() + "." + HInst->getName()));
else
SI = SelectInst::Create(BrCond, HInst, FalseV,
HInst->getName() + "." + FalseV->getName(), BI);
SI->setDebugLoc(BI->getDebugLoc());
SI = cast<SelectInst>
(Builder.CreateSelect(BrCond, HInst, FalseV,
HInst->getName() + "." + FalseV->getName()));
// Make the PHI node use the select for all incoming values for "then" and
// "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
/// PHI node, see if we can eliminate it.
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD,
IRBuilder<> &Builder) {
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
// 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
// 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,
// do all of the PHI's now.
Instruction *InsertPt = DomBlock->getTerminator();
Builder.SetInsertPoint(InsertPt);
IRBuilder<true, NoFolder> Builder(InsertPt);
// Move all 'aggressive' instructions, which are defined in the
// 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.
bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
BasicBlock *BB = BI->getParent();
Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
if (Cond == 0 || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
Cond->getParent() != BB || !Cond->hasOneUse())
@ -1580,7 +1584,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
}
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 (InvertPredCond) {
Value *NewCond = PBI->getCondition();
@ -1589,8 +1594,8 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
CmpInst *CI = cast<CmpInst>(NewCond);
CI->setPredicate(CI->getInversePredicate());
} else {
NewCond = BinaryOperator::CreateNot(NewCond,
PBI->getCondition()->getName()+".not", PBI);
NewCond = Builder.CreateNot(NewCond,
PBI->getCondition()->getName()+".not");
}
PBI->setCondition(NewCond);
@ -1617,9 +1622,9 @@ bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
New->takeName(Cond);
Cond->setName(New->getName()+".old");
Instruction *NewCond = BinaryOperator::Create(Opc, PBI->getCondition(),
New, "or.cond", PBI);
NewCond->setDebugLoc(PBI->getDebugLoc());
Instruction *NewCond =
cast<Instruction>(Builder.CreateBinOp(Opc, PBI->getCondition(),
New, "or.cond"));
PBI->setCondition(NewCond);
if (PBI->getSuccessor(0) == BB) {
AddPredecessorToBlock(TrueDest, PredBlock, BB);
@ -1762,23 +1767,22 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
}
DEBUG(dbgs() << *PBI->getParent()->getParent());
// BI may have other predecessors. Because of this, we leave
// it alone, but modify PBI.
// Make sure we get to CommonDest on True&True directions.
Value *PBICond = PBI->getCondition();
IRBuilder<true, NoFolder> Builder(PBI);
if (PBIOp)
PBICond = BinaryOperator::CreateNot(PBICond,
PBICond->getName()+".not",
PBI);
PBICond = Builder.CreateNot(PBICond, PBICond->getName()+".not");
Value *BICond = BI->getCondition();
if (BIOp)
BICond = BinaryOperator::CreateNot(BICond,
BICond->getName()+".not",
PBI);
BICond = Builder.CreateNot(BICond, BICond->getName()+".not");
// 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.
PBI->setCondition(Cond);
@ -1801,8 +1805,8 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
Value *PBIV = PN->getIncomingValue(PBBIdx);
if (BIV != PBIV) {
// Insert a select in PBI to pick the right value.
Value *NV = SelectInst::Create(PBICond, PBIV, BIV,
PBIV->getName()+".mux", PBI);
Value *NV = cast<SelectInst>
(Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName()+".mux"));
PN->setIncomingValue(PBBIdx, NV);
}
}
@ -2665,7 +2669,7 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
// eliminate it, do so now.
if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
if (PN->getNumIncomingValues() == 2)
Changed |= FoldTwoEntryPHINode(PN, TD, Builder);
Changed |= FoldTwoEntryPHINode(PN, TD);
Builder.SetInsertPoint(BB->getTerminator());
if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {