reduce indentation and generally simplify code, no functionality change.

llvm-svn: 121667
This commit is contained in:
Chris Lattner 2010-12-13 01:47:07 +00:00
parent 73ce4a6fd8
commit 4088e2b8e4
1 changed files with 314 additions and 338 deletions

View File

@ -301,13 +301,16 @@ ConstantInt *SimplifyCFGOpt::GetConstantInt(Value *V) {
/// value being compared, and stick the constant into the Values vector.
Value *SimplifyCFGOpt::
GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values) {
if (Instruction *Inst = dyn_cast<Instruction>(V)) {
Instruction *Inst = dyn_cast<Instruction>(V);
if (Inst == 0) return 0;
if (Inst->getOpcode() == Instruction::ICmp &&
cast<ICmpInst>(Inst)->getPredicate() == ICmpInst::ICMP_EQ) {
if (ConstantInt *C = GetConstantInt(Inst->getOperand(1))) {
Values.push_back(C);
return Inst->getOperand(0);
} else if (ConstantInt *C = GetConstantInt(Inst->getOperand(0))) {
}
if (ConstantInt *C = GetConstantInt(Inst->getOperand(0))) {
Values.push_back(C);
return Inst->getOperand(1);
}
@ -317,7 +320,6 @@ GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values) {
if (LHS == RHS)
return LHS;
}
}
return 0;
}
@ -326,13 +328,16 @@ GatherConstantSetEQs(Value *V, std::vector<ConstantInt*> &Values) {
/// being compared, and stick the constant into the Values vector.
Value *SimplifyCFGOpt::
GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values) {
if (Instruction *Inst = dyn_cast<Instruction>(V)) {
Instruction *Inst = dyn_cast<Instruction>(V);
if (Inst == 0) return 0;
if (Inst->getOpcode() == Instruction::ICmp &&
cast<ICmpInst>(Inst)->getPredicate() == ICmpInst::ICMP_NE) {
if (ConstantInt *C = GetConstantInt(Inst->getOperand(1))) {
Values.push_back(C);
return Inst->getOperand(0);
} else if (ConstantInt *C = GetConstantInt(Inst->getOperand(0))) {
}
if (ConstantInt *C = GetConstantInt(Inst->getOperand(0))) {
Values.push_back(C);
return Inst->getOperand(1);
}
@ -342,7 +347,6 @@ GatherConstantSetNEs(Value *V, std::vector<ConstantInt*> &Values) {
if (LHS == RHS)
return LHS;
}
}
return 0;
}
@ -357,7 +361,8 @@ bool SimplifyCFGOpt::GatherValueComparisons(Instruction *Cond, Value *&CompVal,
// Return true to indicate that the condition is true if the CompVal is
// equal to one of the constants.
return true;
} else if (Cond->getOpcode() == Instruction::And) {
}
if (Cond->getOpcode() == Instruction::And) {
CompVal = GatherConstantSetNEs(Cond, Values);
// Return false to indicate that the condition is false if the CompVal is
@ -508,7 +513,9 @@ SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
// If we are here, we know that the value is none of those cases listed in
// PredCases. If there are any cases in ThisCases that are in PredCases, we
// can simplify TI.
if (ValuesOverlap(PredCases, ThisCases)) {
if (!ValuesOverlap(PredCases, ThisCases))
return false;
if (isa<BranchInst>(TI)) {
// Okay, one of the successors of this condbr is dead. Convert it to a
// uncond br.
@ -525,8 +532,8 @@ SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
EraseTerminatorInstAndDCECond(TI);
return true;
}
} else {
SwitchInst *SI = cast<SwitchInst>(TI);
// Okay, TI has cases that are statically dead, prune them away.
SmallPtrSet<Constant*, 16> DeadCases;
@ -545,19 +552,16 @@ SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
DEBUG(dbgs() << "Leaving: " << *TI << "\n");
return true;
}
}
} else {
// Otherwise, TI's block must correspond to some matched value. Find out
// which value (or set of values) this is.
ConstantInt *TIV = 0;
BasicBlock *TIBB = TI->getParent();
for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
if (PredCases[i].second == TIBB) {
if (TIV == 0)
TIV = PredCases[i].first;
else
if (TIV != 0)
return false; // Cannot handle multiple values coming to this block.
TIV = PredCases[i].first;
}
assert(TIV && "No edge from pred to succ?");
@ -591,8 +595,6 @@ SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
EraseTerminatorInstAndDCECond(TI);
return true;
}
return false;
}
namespace {
/// ConstantIntOrdering - This class implements a stable ordering of constant
@ -838,7 +840,8 @@ HoistTerminator:
(PN = dyn_cast<PHINode>(BBI)); ++BBI) {
Value *BB1V = PN->getIncomingValueForBlock(BB1);
Value *BB2V = PN->getIncomingValueForBlock(BB2);
if (BB1V != BB2V) {
if (BB1V == BB2V) continue;
// These values do not agree. Insert a select instruction before NT
// that determines the right value.
SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
@ -851,7 +854,6 @@ HoistTerminator:
PN->setIncomingValue(i, SI);
}
}
}
// Update any PHI nodes in our new successors.
for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
@ -877,18 +879,16 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
if (isa<DbgInfoIntrinsic>(I)) continue;
if (I == Term) break;
if (!HInst)
HInst = I;
else
if (HInst)
return false;
HInst = I;
}
if (!HInst)
return false;
// Be conservative for now. FP select instruction can often be expensive.
Value *BrCond = BI->getCondition();
if (isa<Instruction>(BrCond) &&
cast<Instruction>(BrCond)->getOpcode() == Instruction::FCmp)
if (isa<FCmpInst>(BrCond))
return false;
// If BB1 is actually on the false edge of the conditional branch, remember
@ -992,13 +992,13 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
for(Value::use_iterator UI = BrCond->use_begin(), UE = BrCond->use_end();
UI != UE; ++UI) {
Instruction *Use = cast<Instruction>(*UI);
if (BB1Insns.count(Use)) {
if (!BB1Insns.count(Use)) continue;
// If BrCond uses the instruction that place it just before
// branch instruction.
InsertPos = BI;
break;
}
}
} else
InsertPos = BI;
BIParent->getInstList().splice(InsertPos, BB1->getInstList(), HInst);
@ -1018,8 +1018,7 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
for (unsigned i = 0, e = PHIUses.size(); i != e; ++i) {
PHINode *PN = PHIUses[i];
for (unsigned j = 0, ee = PN->getNumIncomingValues(); j != ee; ++j)
if (PN->getIncomingBlock(j) == BB1 ||
PN->getIncomingBlock(j) == BIParent)
if (PN->getIncomingBlock(j) == BB1 || PN->getIncomingBlock(j) == BIParent)
PN->setIncomingValue(j, SI);
}
@ -1077,9 +1076,9 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
// Okay, this is a simple enough basic block. See if any phi values are
// constants.
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
ConstantInt *CB;
if ((CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i))) &&
CB->getType()->isIntegerTy(1)) {
ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
if (CB == 0 || !CB->getType()->isIntegerTy(1)) continue;
// Okay, we now know that all edges from PredBB should be revectored to
// branch to RealDest.
BasicBlock *PredBB = PN->getIncomingBlock(i);
@ -1106,11 +1105,12 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
// instructions into EdgeBB. We know that there will be no uses of the
// cloned instructions outside of EdgeBB.
BasicBlock::iterator InsertPt = EdgeBB->begin();
std::map<Value*, Value*> TranslateMap; // Track translated values.
DenseMap<Value*, Value*> TranslateMap; // Track translated values.
for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
} else {
continue;
}
// Clone the instruction.
Instruction *N = BBI->clone();
if (BBI->hasName()) N->setName(BBI->getName()+".c");
@ -1118,8 +1118,7 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
// Update operands due to translation.
for (User::op_iterator i = N->op_begin(), e = N->op_end();
i != e; ++i) {
std::map<Value*, Value*>::iterator PI =
TranslateMap.find(*i);
DenseMap<Value*, Value*>::iterator PI = TranslateMap.find(*i);
if (PI != TranslateMap.end())
*i = PI->second;
}
@ -1135,7 +1134,6 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
TranslateMap[BBI] = N;
}
}
}
// Loop over all of the edges from PredBB to BB, changing them to branch
// to EdgeBB instead.
@ -1149,7 +1147,6 @@ static bool FoldCondBranchOnPHI(BranchInst *BI) {
// Recurse, simplifying any other constants.
return FoldCondBranchOnPHI(BI) | true;
}
}
return false;
}
@ -1242,25 +1239,19 @@ static bool FoldTwoEntryPHINode(PHINode *PN) {
// Move all 'aggressive' instructions, which are defined in the
// conditional parts of the if's up to the dominating block.
if (IfBlock1) {
if (IfBlock1)
DomBlock->getInstList().splice(DomBlock->getTerminator(),
IfBlock1->getInstList(),
IfBlock1->begin(),
IfBlock1->getInstList(), IfBlock1->begin(),
IfBlock1->getTerminator());
}
if (IfBlock2) {
if (IfBlock2)
DomBlock->getInstList().splice(DomBlock->getTerminator(),
IfBlock2->getInstList(),
IfBlock2->begin(),
IfBlock2->getInstList(), IfBlock2->begin(),
IfBlock2->getTerminator());
}
while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
// Change the PHI node into a select instruction.
Value *TrueVal =
PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
Value *FalseVal =
PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
Value *NV = SelectInst::Create(IfCond, TrueVal, FalseVal, "", AfterPHIIt);
PN->replaceAllUsesWith(NV);
@ -1271,21 +1262,6 @@ static bool FoldTwoEntryPHINode(PHINode *PN) {
return true;
}
/// isTerminatorFirstRelevantInsn - Return true if Term is very first
/// instruction ignoring Phi nodes and dbg intrinsics.
static bool isTerminatorFirstRelevantInsn(BasicBlock *BB, Instruction *Term) {
BasicBlock::iterator BBI = Term;
while (BBI != BB->begin()) {
--BBI;
if (!isa<DbgInfoIntrinsic>(BBI))
break;
}
if (isa<PHINode>(BBI) || &*BBI == Term || isa<DbgInfoIntrinsic>(BBI))
return true;
return false;
}
/// SimplifyCondBranchToTwoReturns - If we found a conditional branch that goes
/// to two returning blocks, try to merge them together into one return,
/// introducing a select if the return values disagree.
@ -1299,9 +1275,9 @@ static bool SimplifyCondBranchToTwoReturns(BranchInst *BI) {
// Check to ensure both blocks are empty (just a return) or optionally empty
// with PHI nodes. If there are other instructions, merging would cause extra
// computation on one path or the other.
if (!isTerminatorFirstRelevantInsn(TrueSucc, TrueRet))
if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
return false;
if (!isTerminatorFirstRelevantInsn(FalseSucc, FalseRet))
if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
return false;
// Okay, we found a branch that is going to two return nodes. If
@ -1821,7 +1797,7 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
// different return values, fold the replace the branch/return with a select
// and return.
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
if (isTerminatorFirstRelevantInsn(BB, BB->getTerminator())) {
if (BB->getFirstNonPHIOrDbg()->isTerminator()) {
// Find predecessors that end with branches.
SmallVector<BasicBlock*, 8> UncondBranchPreds;
SmallVector<BranchInst*, 8> CondBranchPreds;
@ -1890,8 +1866,8 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
SmallVector<BasicBlock*, 8> Preds(pred_begin(BB), pred_end(BB));
while (!Preds.empty()) {
BasicBlock *Pred = Preds.back();
if (InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator()))
if (II->getUnwindDest() == BB) {
InvokeInst *II = dyn_cast<InvokeInst>(Pred->getTerminator());
if (II && II->getUnwindDest() == BB) {
// Insert a new branch instruction before the invoke, because this
// is now a fall through.
BranchInst *BI = BranchInst::Create(II->getNormalDest(), II);
@ -1969,12 +1945,10 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
// Ignore dbg intrinsics.
while (isa<DbgInfoIntrinsic>(I))
++I;
if(&*I == BI) {
if (FoldValueComparisonIntoPredecessors(BI))
if (&*I == BI && FoldValueComparisonIntoPredecessors(BI))
return SimplifyCFG(BB) | true;
}
}
}
// If this is a branch on a phi node in the current block, thread control
// through this block if any PHI node entries are constants.
@ -2169,9 +2143,9 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
}
}
if (OnlyPred)
if (BranchInst *BI = dyn_cast<BranchInst>(OnlyPred->getTerminator()))
if (BI->isConditional()) {
if (OnlyPred) {
BranchInst *BI = dyn_cast<BranchInst>(OnlyPred->getTerminator());
if (BI && BI->isConditional()) {
// Get the other block.
BasicBlock *OtherBB = BI->getSuccessor(BI->getSuccessor(0) == BB);
PI = pred_begin(OtherBB);
@ -2203,11 +2177,12 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
}
}
}
}
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
if (BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator()))
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
BranchInst *BI = dyn_cast<BranchInst>((*PI)->getTerminator());
// Change br (X == 0 | X == 1), T, F into a switch instruction.
if (BI->isConditional() && isa<Instruction>(BI->getCondition())) {
if (BI && BI->isConditional() && isa<Instruction>(BI->getCondition())) {
Instruction *Cond = cast<Instruction>(BI->getCondition());
// If this is a bunch of seteq's or'd together, or if it's a bunch of
// 'setne's and'ed together, collect them.
@ -2257,6 +2232,7 @@ bool SimplifyCFGOpt::run(BasicBlock *BB) {
return true;
}
}
}
return Changed;
}