@@ -1369,17 +1369,29 @@ static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
1369
1369
// / \endcode
1370
1370
// /
1371
1371
// / \returns true if the conditional block is removed.
1372
- static bool SpeculativelyExecuteBB (BranchInst *BI, BasicBlock *BB1 ) {
1372
+ static bool SpeculativelyExecuteBB (BranchInst *BI, BasicBlock *ThenBB ) {
1373
1373
// Be conservative for now. FP select instruction can often be expensive.
1374
1374
Value *BrCond = BI->getCondition ();
1375
1375
if (isa<FCmpInst>(BrCond))
1376
1376
return false ;
1377
1377
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
+
1378
1390
// Only speculatively execution a single instruction (not counting the
1379
1391
// terminator) for now.
1380
1392
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 ();
1383
1395
BBI != BBE; ++BBI) {
1384
1396
Instruction *I = BBI;
1385
1397
// Skip debug info.
@@ -1391,8 +1403,6 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
1391
1403
HInst = I;
1392
1404
}
1393
1405
1394
- BasicBlock *BIParent = BI->getParent ();
1395
-
1396
1406
// Check the instruction to be hoisted, if there is one.
1397
1407
if (HInst) {
1398
1408
// Don't hoist the instruction if it's unsafe or expensive.
@@ -1407,35 +1417,26 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
1407
1417
for (User::op_iterator i = HInst->op_begin (), e = HInst->op_end ();
1408
1418
i != e; ++i) {
1409
1419
Instruction *OpI = dyn_cast<Instruction>(*i);
1410
- if (OpI && OpI->getParent () == BIParent &&
1420
+ if (OpI && OpI->getParent () == BB &&
1411
1421
!OpI->mayHaveSideEffects () &&
1412
- !OpI->isUsedInBasicBlock (BIParent ))
1422
+ !OpI->isUsedInBasicBlock (BB ))
1413
1423
return false ;
1414
1424
}
1415
1425
}
1416
1426
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
-
1425
1427
// Collect interesting PHIs, and scan for hazards.
1426
1428
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 ();
1429
1430
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 );
1432
1433
1433
1434
// Skip PHIs which are trivial.
1434
- if (BB1V == BIParentV )
1435
+ if (ThenV == OrigV )
1435
1436
continue ;
1436
1437
1437
1438
// Check for safety.
1438
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BB1V )) {
1439
+ if (ConstantExpr *CE = dyn_cast<ConstantExpr>(ThenV )) {
1439
1440
// An unfolded ConstantExpr could end up getting expanded into
1440
1441
// Instructions. Don't speculate this and another instruction at
1441
1442
// the same time.
@@ -1448,7 +1449,7 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
1448
1449
}
1449
1450
1450
1451
// 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 ));
1452
1453
}
1453
1454
1454
1455
// If there are no PHIs to process, bail early. This helps ensure idempotence
@@ -1457,11 +1458,11 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
1457
1458
return false ;
1458
1459
1459
1460
// 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 " ;);
1461
1462
1462
1463
// Hoist the instruction.
1463
1464
if (HInst)
1464
- BIParent ->getInstList ().splice (BI, BB1 ->getInstList (), HInst);
1465
+ BB ->getInstList ().splice (BI, ThenBB ->getInstList (), HInst);
1465
1466
1466
1467
// Insert selects and rewrite the PHI operands.
1467
1468
IRBuilder<true , NoFolder> Builder (BI);
@@ -1483,15 +1484,15 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
1483
1484
1484
1485
// Make the PHI node use the select for all incoming values for "then" and
1485
1486
// "if" blocks.
1486
- for (BasicBlock::iterator I = BB2 ->begin ();
1487
+ for (BasicBlock::iterator I = EndBB ->begin ();
1487
1488
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);
1495
1496
}
1496
1497
}
1497
1498
}
0 commit comments