Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 6cf7a91

Browse files
committed
Give the basic block variables here names based on the if-then-end
structure being analyzed. No functionality changed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@173334 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9e62095 commit 6cf7a91

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,17 +1369,29 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
13691369
/// \endcode
13701370
///
13711371
/// \returns true if the conditional block is removed.
1372-
static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
1372+
static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB) {
13731373
// Be conservative for now. FP select instruction can often be expensive.
13741374
Value *BrCond = BI->getCondition();
13751375
if (isa<FCmpInst>(BrCond))
13761376
return false;
13771377

1378+
BasicBlock *BB = BI->getParent();
1379+
BasicBlock *EndBB = ThenBB->getTerminator()->getSuccessor(0);
1380+
1381+
// If ThenBB is actually on the false edge of the conditional branch, remember
1382+
// to swap the select operands later.
1383+
bool Invert = false;
1384+
if (ThenBB != BI->getSuccessor(0)) {
1385+
assert(ThenBB == BI->getSuccessor(1) && "No edge from 'if' block?");
1386+
Invert = true;
1387+
}
1388+
assert(EndBB == BI->getSuccessor(!Invert) && "No edge from to end block");
1389+
13781390
// Only speculatively execution a single instruction (not counting the
13791391
// terminator) for now.
13801392
Instruction *HInst = NULL;
1381-
Instruction *Term = BB1->getTerminator();
1382-
for (BasicBlock::iterator BBI = BB1->begin(), BBE = BB1->end();
1393+
Instruction *Term = ThenBB->getTerminator();
1394+
for (BasicBlock::iterator BBI = ThenBB->begin(), BBE = ThenBB->end();
13831395
BBI != BBE; ++BBI) {
13841396
Instruction *I = BBI;
13851397
// Skip debug info.
@@ -1391,8 +1403,6 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
13911403
HInst = I;
13921404
}
13931405

1394-
BasicBlock *BIParent = BI->getParent();
1395-
13961406
// Check the instruction to be hoisted, if there is one.
13971407
if (HInst) {
13981408
// Don't hoist the instruction if it's unsafe or expensive.
@@ -1407,35 +1417,26 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
14071417
for (User::op_iterator i = HInst->op_begin(), e = HInst->op_end();
14081418
i != e; ++i) {
14091419
Instruction *OpI = dyn_cast<Instruction>(*i);
1410-
if (OpI && OpI->getParent() == BIParent &&
1420+
if (OpI && OpI->getParent() == BB &&
14111421
!OpI->mayHaveSideEffects() &&
1412-
!OpI->isUsedInBasicBlock(BIParent))
1422+
!OpI->isUsedInBasicBlock(BB))
14131423
return false;
14141424
}
14151425
}
14161426

1417-
// If BB1 is actually on the false edge of the conditional branch, remember
1418-
// to swap the select operands later.
1419-
bool Invert = false;
1420-
if (BB1 != BI->getSuccessor(0)) {
1421-
assert(BB1 == BI->getSuccessor(1) && "No edge from 'if' block?");
1422-
Invert = true;
1423-
}
1424-
14251427
// Collect interesting PHIs, and scan for hazards.
14261428
SmallSetVector<std::pair<Value *, Value *>, 4> PHIs;
1427-
BasicBlock *BB2 = BB1->getTerminator()->getSuccessor(0);
1428-
for (BasicBlock::iterator I = BB2->begin();
1429+
for (BasicBlock::iterator I = EndBB->begin();
14291430
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1430-
Value *BB1V = PN->getIncomingValueForBlock(BB1);
1431-
Value *BIParentV = PN->getIncomingValueForBlock(BIParent);
1431+
Value *OrigV = PN->getIncomingValueForBlock(BB);
1432+
Value *ThenV = PN->getIncomingValueForBlock(ThenBB);
14321433

14331434
// Skip PHIs which are trivial.
1434-
if (BB1V == BIParentV)
1435+
if (ThenV == OrigV)
14351436
continue;
14361437

14371438
// Check for safety.
1438-
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BB1V)) {
1439+
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(ThenV)) {
14391440
// An unfolded ConstantExpr could end up getting expanded into
14401441
// Instructions. Don't speculate this and another instruction at
14411442
// the same time.
@@ -1448,7 +1449,7 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
14481449
}
14491450

14501451
// Ok, we may insert a select for this PHI.
1451-
PHIs.insert(std::make_pair(BB1V, BIParentV));
1452+
PHIs.insert(std::make_pair(ThenV, OrigV));
14521453
}
14531454

14541455
// If there are no PHIs to process, bail early. This helps ensure idempotence
@@ -1457,11 +1458,11 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
14571458
return false;
14581459

14591460
// If we get here, we can hoist the instruction and if-convert.
1460-
DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *BB1 << "\n";);
1461+
DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *ThenBB << "\n";);
14611462

14621463
// Hoist the instruction.
14631464
if (HInst)
1464-
BIParent->getInstList().splice(BI, BB1->getInstList(), HInst);
1465+
BB->getInstList().splice(BI, ThenBB->getInstList(), HInst);
14651466

14661467
// Insert selects and rewrite the PHI operands.
14671468
IRBuilder<true, NoFolder> Builder(BI);
@@ -1483,15 +1484,15 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
14831484

14841485
// Make the PHI node use the select for all incoming values for "then" and
14851486
// "if" blocks.
1486-
for (BasicBlock::iterator I = BB2->begin();
1487+
for (BasicBlock::iterator I = EndBB->begin();
14871488
PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1488-
unsigned BB1I = PN->getBasicBlockIndex(BB1);
1489-
unsigned BIParentI = PN->getBasicBlockIndex(BIParent);
1490-
Value *BB1V = PN->getIncomingValue(BB1I);
1491-
Value *BIParentV = PN->getIncomingValue(BIParentI);
1492-
if (TrueV == BB1V && FalseV == BIParentV) {
1493-
PN->setIncomingValue(BB1I, SI);
1494-
PN->setIncomingValue(BIParentI, SI);
1489+
unsigned ThenI = PN->getBasicBlockIndex(ThenBB);
1490+
unsigned OrigI = PN->getBasicBlockIndex(BB);
1491+
Value *ThenV = PN->getIncomingValue(ThenI);
1492+
Value *OrigV = PN->getIncomingValue(OrigI);
1493+
if (TrueV == ThenV && FalseV == OrigV) {
1494+
PN->setIncomingValue(ThenI, SI);
1495+
PN->setIncomingValue(OrigI, SI);
14951496
}
14961497
}
14971498
}

0 commit comments

Comments
 (0)