Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit b89c2fc

Browse files
committed
Revert "[SimplifyCFG] Rewrite SinkThenElseCodeToEnd"
This reverts commit r279229. It breaks intrinsic function calls in diamonds. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279313 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 118c004 commit b89c2fc

File tree

5 files changed

+153
-416
lines changed

5 files changed

+153
-416
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 150 additions & 210 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,232 +1319,172 @@ static bool HoistThenElseCodeToIf(BranchInst *BI,
13191319
return true;
13201320
}
13211321

1322-
// Return true if V0 and V1 are equivalent. This handles the obvious cases
1323-
// where V0 == V1 and V0 and V1 are both identical instructions, but also
1324-
// handles loads and stores with identical operands.
1325-
//
1326-
// Because determining if two memory instructions are equivalent
1327-
// depends on control flow, the \c At0 and \c At1 parameters specify a
1328-
// location for the query. This function is essentially answering the
1329-
// query "If V0 were moved to At0, and V1 were moved to At1, are V0 and V1
1330-
// equivalent?". In practice this means checking that moving V0 to At0
1331-
// doesn't cross any other memory instructions.
1332-
static bool areValuesTriviallySame(Value *V0, BasicBlock::const_iterator At0,
1333-
Value *V1, BasicBlock::const_iterator At1) {
1334-
if (V0 == V1)
1335-
return true;
1336-
1337-
// Also check for instructions that are identical but not pointer-identical.
1338-
// This can include load instructions that haven't been CSE'd.
1339-
if (!isa<Instruction>(V0) || !isa<Instruction>(V1))
1340-
return false;
1341-
const auto *I0 = cast<Instruction>(V0);
1342-
const auto *I1 = cast<Instruction>(V1);
1343-
if (!I0->isIdenticalToWhenDefined(I1))
1344-
return false;
1345-
1346-
if (!I0->mayReadOrWriteMemory())
1347-
return true;
1322+
/// Given an unconditional branch that goes to BBEnd,
1323+
/// check whether BBEnd has only two predecessors and the other predecessor
1324+
/// ends with an unconditional branch. If it is true, sink any common code
1325+
/// in the two predecessors to BBEnd.
1326+
static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
1327+
assert(BI1->isUnconditional());
1328+
BasicBlock *BB1 = BI1->getParent();
1329+
BasicBlock *BBEnd = BI1->getSuccessor(0);
13481330

1349-
// Instructions that may read or write memory have extra restrictions. We
1350-
// must ensure we don't treat %a and %b as equivalent in code such as:
1351-
//
1352-
// %a = load %x
1353-
// store %x, 1
1354-
// if (%c) {
1355-
// %b = load %x
1356-
// %d = add %b, 1
1357-
// } else {
1358-
// %d = add %a, 1
1359-
// }
1360-
1361-
// Be conservative. We don't want to search the entire CFG between def
1362-
// and use; if the def isn't in the same block as the use just bail.
1363-
if (I0->getParent() != At0->getParent() ||
1364-
I1->getParent() != At1->getParent())
1331+
// Check that BBEnd has two predecessors and the other predecessor ends with
1332+
// an unconditional branch.
1333+
pred_iterator PI = pred_begin(BBEnd), PE = pred_end(BBEnd);
1334+
BasicBlock *Pred0 = *PI++;
1335+
if (PI == PE) // Only one predecessor.
13651336
return false;
1366-
1367-
// Again, be super conservative. Ideally we'd be able to query AliasAnalysis
1368-
// but we currently don't have that available.
1369-
auto WritesMemory = [](const Instruction &I) {
1370-
return I.mayReadOrWriteMemory();
1371-
};
1372-
if (std::any_of(std::next(I0->getIterator()), At0, WritesMemory))
1337+
BasicBlock *Pred1 = *PI++;
1338+
if (PI != PE) // More than two predecessors.
13731339
return false;
1374-
if (std::any_of(std::next(I1->getIterator()), At1, WritesMemory))
1340+
BasicBlock *BB2 = (Pred0 == BB1) ? Pred1 : Pred0;
1341+
BranchInst *BI2 = dyn_cast<BranchInst>(BB2->getTerminator());
1342+
if (!BI2 || !BI2->isUnconditional())
13751343
return false;
1376-
return true;
1377-
}
1378-
1379-
// Is it legal to replace the operand \c OpIdx of \c GEP with a PHI node?
1380-
static bool canReplaceGEPOperandWithPHI(const Instruction *GEP,
1381-
unsigned OpIdx) {
1382-
if (OpIdx == 0)
1383-
return true;
1384-
gep_type_iterator It = std::next(gep_type_begin(GEP), OpIdx - 1);
1385-
return !It->isStructTy();
1386-
}
13871344

1388-
// All blocks in Blocks unconditionally jump to a common successor. Analyze
1389-
// the last non-terminator instruction in each block and return true if it would
1390-
// be possible to sink them into their successor, creating one common
1391-
// instruction instead. Set NumPHIsRequired to the number of PHI nodes that
1392-
// would need to be created during sinking.
1393-
static bool canSinkLastInstruction(ArrayRef<BasicBlock*> Blocks,
1394-
unsigned &NumPHIsRequired) {
1395-
SmallVector<Instruction*,4> Insts;
1396-
for (auto *BB : Blocks) {
1397-
if (BB->getTerminator() == &BB->front())
1398-
// Block was empty.
1399-
return false;
1400-
Insts.push_back(BB->getTerminator()->getPrevNode());
1345+
// Gather the PHI nodes in BBEnd.
1346+
SmallDenseMap<std::pair<Value *, Value *>, PHINode *> JointValueMap;
1347+
Instruction *FirstNonPhiInBBEnd = nullptr;
1348+
for (BasicBlock::iterator I = BBEnd->begin(), E = BBEnd->end(); I != E; ++I) {
1349+
if (PHINode *PN = dyn_cast<PHINode>(I)) {
1350+
Value *BB1V = PN->getIncomingValueForBlock(BB1);
1351+
Value *BB2V = PN->getIncomingValueForBlock(BB2);
1352+
JointValueMap[std::make_pair(BB1V, BB2V)] = PN;
1353+
} else {
1354+
FirstNonPhiInBBEnd = &*I;
1355+
break;
1356+
}
14011357
}
1358+
if (!FirstNonPhiInBBEnd)
1359+
return false;
14021360

1403-
// Prune out obviously bad instructions to move. Any non-store instruction
1404-
// must have exactly one use, and we check later that use is by a single,
1405-
// common PHI instruction in the successor.
1406-
for (auto *I : Insts) {
1407-
// These instructions may change or break semantics if moved.
1408-
if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(I) ||
1409-
I->getType()->isTokenTy())
1410-
return false;
1411-
// Apart from loads and stores, we won't move anything that could
1412-
// change memory or have sideeffects.
1413-
if (!isa<StoreInst>(I) && !isa<LoadInst>(I) &&
1414-
(I->mayHaveSideEffects() || I->mayHaveSideEffects()))
1415-
return false;
1416-
// Everything must have only one use too, apart from stores which
1417-
// have no uses.
1418-
if (!isa<StoreInst>(I) && !I->hasOneUse())
1419-
return false;
1420-
}
1361+
// This does very trivial matching, with limited scanning, to find identical
1362+
// instructions in the two blocks. We scan backward for obviously identical
1363+
// instructions in an identical order.
1364+
BasicBlock::InstListType::reverse_iterator RI1 = BB1->getInstList().rbegin(),
1365+
RE1 = BB1->getInstList().rend(),
1366+
RI2 = BB2->getInstList().rbegin(),
1367+
RE2 = BB2->getInstList().rend();
1368+
// Skip debug info.
1369+
while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1))
1370+
++RI1;
1371+
if (RI1 == RE1)
1372+
return false;
1373+
while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2))
1374+
++RI2;
1375+
if (RI2 == RE2)
1376+
return false;
1377+
// Skip the unconditional branches.
1378+
++RI1;
1379+
++RI2;
14211380

1422-
const Instruction *I0 = Insts.front();
1423-
for (auto *I : Insts)
1424-
if (!I->isSameOperationAs(I0))
1425-
return false;
1381+
bool Changed = false;
1382+
while (RI1 != RE1 && RI2 != RE2) {
1383+
// Skip debug info.
1384+
while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1))
1385+
++RI1;
1386+
if (RI1 == RE1)
1387+
return Changed;
1388+
while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2))
1389+
++RI2;
1390+
if (RI2 == RE2)
1391+
return Changed;
14261392

1427-
// If this isn't a store, check the only user is a single PHI.
1428-
if (!isa<StoreInst>(I0)) {
1429-
auto *PNUse = dyn_cast<PHINode>(*I0->user_begin());
1430-
if (!PNUse ||
1431-
!all_of(Insts, [&PNUse](const Instruction *I) {
1432-
return *I->user_begin() == PNUse;
1433-
}))
1434-
return false;
1435-
}
1393+
Instruction *I1 = &*RI1, *I2 = &*RI2;
1394+
auto InstPair = std::make_pair(I1, I2);
1395+
// I1 and I2 should have a single use in the same PHI node, and they
1396+
// perform the same operation.
1397+
// Cannot move control-flow-involving, volatile loads, vaarg, etc.
1398+
if (isa<PHINode>(I1) || isa<PHINode>(I2) || isa<TerminatorInst>(I1) ||
1399+
isa<TerminatorInst>(I2) || I1->isEHPad() || I2->isEHPad() ||
1400+
isa<AllocaInst>(I1) || isa<AllocaInst>(I2) ||
1401+
I1->mayHaveSideEffects() || I2->mayHaveSideEffects() ||
1402+
I1->mayReadOrWriteMemory() || I2->mayReadOrWriteMemory() ||
1403+
!I1->hasOneUse() || !I2->hasOneUse() || !JointValueMap.count(InstPair))
1404+
return Changed;
14361405

1437-
NumPHIsRequired = 0;
1438-
for (unsigned OI = 0, OE = I0->getNumOperands(); OI != OE; ++OI) {
1439-
if (I0->getOperand(OI)->getType()->isTokenTy())
1440-
// Don't touch any operand of token type.
1441-
return false;
1442-
auto SameAsI0 = [&I0, OI](const Instruction *I) {
1443-
return areValuesTriviallySame(I->getOperand(OI), I->getIterator(),
1444-
I0->getOperand(OI), I0->getIterator());
1445-
};
1446-
if (!all_of(Insts, SameAsI0)) {
1447-
if (isa<GetElementPtrInst>(I0) && !canReplaceGEPOperandWithPHI(I0, OI))
1448-
// We can't create a PHI from this GEP.
1449-
return false;
1450-
if (isa<ShuffleVectorInst>(I0) && OI == 2)
1451-
// We can't create a PHI for a shufflevector mask.
1452-
return false;
1453-
++NumPHIsRequired;
1406+
// Check whether we should swap the operands of ICmpInst.
1407+
// TODO: Add support of communativity.
1408+
ICmpInst *ICmp1 = dyn_cast<ICmpInst>(I1), *ICmp2 = dyn_cast<ICmpInst>(I2);
1409+
bool SwapOpnds = false;
1410+
if (ICmp1 && ICmp2 && ICmp1->getOperand(0) != ICmp2->getOperand(0) &&
1411+
ICmp1->getOperand(1) != ICmp2->getOperand(1) &&
1412+
(ICmp1->getOperand(0) == ICmp2->getOperand(1) ||
1413+
ICmp1->getOperand(1) == ICmp2->getOperand(0))) {
1414+
ICmp2->swapOperands();
1415+
SwapOpnds = true;
14541416
}
1455-
}
1456-
return true;
1457-
}
1458-
1459-
// Assuming canSinkLastInstruction(Blocks) has returned true, sink the last
1460-
// instruction of every block in Blocks to their common successor, commoning
1461-
// into one instruction.
1462-
static void sinkLastInstruction(ArrayRef<BasicBlock*> Blocks) {
1463-
unsigned Dummy;
1464-
(void)Dummy;
1465-
assert(canSinkLastInstruction(Blocks, Dummy) &&
1466-
"Must analyze before transforming!");
1467-
auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0);
1468-
1469-
// canSinkLastInstruction returning true guarantees that every block has at
1470-
// least one non-terminator instruction.
1471-
SmallVector<Instruction*,4> Insts;
1472-
for (auto *BB : Blocks)
1473-
Insts.push_back(BB->getTerminator()->getPrevNode());
1474-
1475-
// We don't need to do any checking here; canSinkLastInstruction should have
1476-
// done it all for us.
1477-
Instruction *I0 = Insts.front();
1478-
SmallVector<Value*, 4> NewOperands;
1479-
for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O) {
1480-
// This check is different to that in canSinkLastInstruction. There, we
1481-
// cared about the global view once simplifycfg (and instcombine) have
1482-
// completed - it takes into account PHIs that become trivially
1483-
// simplifiable. However here we need a more local view; if an operand
1484-
// differs we create a PHI and rely on instcombine to clean up the very
1485-
// small mess we may make.
1486-
bool NeedPHI = any_of(Insts, [&I0, O](const Instruction *I) {
1487-
return I->getOperand(O) != I0->getOperand(O);
1488-
});
1489-
if (!NeedPHI) {
1490-
NewOperands.push_back(I0->getOperand(O));
1491-
continue;
1417+
if (!I1->isSameOperationAs(I2)) {
1418+
if (SwapOpnds)
1419+
ICmp2->swapOperands();
1420+
return Changed;
14921421
}
14931422

1494-
// Create a new PHI in the successor block and populate it.
1495-
auto *Op = I0->getOperand(O);
1496-
assert(!Op->getType()->isTokenTy() && "Can't PHI tokens!");
1497-
auto *PN = PHINode::Create(Op->getType(), Insts.size(),
1498-
Op->getName() + ".sink", &BBEnd->front());
1499-
for (auto *I : Insts)
1500-
PN->addIncoming(I->getOperand(O), I->getParent());
1501-
NewOperands.push_back(PN);
1502-
}
1503-
1504-
// Arbitrarily use I0 as the new "common" instruction; remap its operands
1505-
// and move it to the start of the successor block.
1506-
for (unsigned O = 0, E = I0->getNumOperands(); O != E; ++O)
1507-
I0->getOperandUse(O).set(NewOperands[O]);
1508-
I0->moveBefore(&*BBEnd->getFirstInsertionPt());
1509-
1510-
if (!isa<StoreInst>(I0)) {
1511-
// canSinkLastInstruction checked that all instructions were used by
1512-
// one and only one PHI node. Find that now, RAUW it to our common
1513-
// instruction and nuke it.
1514-
assert(I0->hasOneUse());
1515-
auto *PN = cast<PHINode>(*I0->user_begin());
1516-
PN->replaceAllUsesWith(I0);
1517-
PN->eraseFromParent();
1518-
}
1519-
1520-
// Finally nuke all instructions apart from the common instruction.
1521-
for (auto *I : Insts)
1522-
if (I != I0)
1523-
I->eraseFromParent();
1524-
}
1423+
// The operands should be either the same or they need to be generated
1424+
// with a PHI node after sinking. We only handle the case where there is
1425+
// a single pair of different operands.
1426+
Value *DifferentOp1 = nullptr, *DifferentOp2 = nullptr;
1427+
unsigned Op1Idx = ~0U;
1428+
for (unsigned I = 0, E = I1->getNumOperands(); I != E; ++I) {
1429+
if (I1->getOperand(I) == I2->getOperand(I))
1430+
continue;
1431+
// Early exit if we have more-than one pair of different operands or if
1432+
// we need a PHI node to replace a constant.
1433+
if (Op1Idx != ~0U || isa<Constant>(I1->getOperand(I)) ||
1434+
isa<Constant>(I2->getOperand(I))) {
1435+
// If we can't sink the instructions, undo the swapping.
1436+
if (SwapOpnds)
1437+
ICmp2->swapOperands();
1438+
return Changed;
1439+
}
1440+
DifferentOp1 = I1->getOperand(I);
1441+
Op1Idx = I;
1442+
DifferentOp2 = I2->getOperand(I);
1443+
}
15251444

1526-
/// Given an unconditional branch that goes to BBEnd,
1527-
/// check whether BBEnd has only two predecessors and the other predecessor
1528-
/// ends with an unconditional branch. If it is true, sink any common code
1529-
/// in the two predecessors to BBEnd.
1530-
static bool SinkThenElseCodeToEnd(BranchInst *BI1) {
1531-
assert(BI1->isUnconditional());
1532-
BasicBlock *BBEnd = BI1->getSuccessor(0);
1445+
DEBUG(dbgs() << "SINK common instructions " << *I1 << "\n");
1446+
DEBUG(dbgs() << " " << *I2 << "\n");
1447+
1448+
// We insert the pair of different operands to JointValueMap and
1449+
// remove (I1, I2) from JointValueMap.
1450+
if (Op1Idx != ~0U) {
1451+
auto &NewPN = JointValueMap[std::make_pair(DifferentOp1, DifferentOp2)];
1452+
if (!NewPN) {
1453+
NewPN =
1454+
PHINode::Create(DifferentOp1->getType(), 2,
1455+
DifferentOp1->getName() + ".sink", &BBEnd->front());
1456+
NewPN->addIncoming(DifferentOp1, BB1);
1457+
NewPN->addIncoming(DifferentOp2, BB2);
1458+
DEBUG(dbgs() << "Create PHI node " << *NewPN << "\n";);
1459+
}
1460+
// I1 should use NewPN instead of DifferentOp1.
1461+
I1->setOperand(Op1Idx, NewPN);
1462+
}
1463+
PHINode *OldPN = JointValueMap[InstPair];
1464+
JointValueMap.erase(InstPair);
1465+
1466+
// We need to update RE1 and RE2 if we are going to sink the first
1467+
// instruction in the basic block down.
1468+
bool UpdateRE1 = (I1 == &BB1->front()), UpdateRE2 = (I2 == &BB2->front());
1469+
// Sink the instruction.
1470+
BBEnd->getInstList().splice(FirstNonPhiInBBEnd->getIterator(),
1471+
BB1->getInstList(), I1);
1472+
if (!OldPN->use_empty())
1473+
OldPN->replaceAllUsesWith(I1);
1474+
OldPN->eraseFromParent();
15331475

1534-
SmallVector<BasicBlock*,4> Blocks;
1535-
for (auto *BB : predecessors(BBEnd))
1536-
Blocks.push_back(BB);
1537-
if (Blocks.size() != 2 ||
1538-
!all_of(Blocks, [](const BasicBlock *BB) {
1539-
auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
1540-
return BI && BI->isUnconditional();
1541-
}))
1542-
return false;
1476+
if (!I2->use_empty())
1477+
I2->replaceAllUsesWith(I1);
1478+
I1->intersectOptionalDataWith(I2);
1479+
// TODO: Use combineMetadata here to preserve what metadata we can
1480+
// (analogous to the hoisting case above).
1481+
I2->eraseFromParent();
15431482

1544-
bool Changed = false;
1545-
unsigned NumPHIsToInsert;
1546-
while (canSinkLastInstruction(Blocks, NumPHIsToInsert) && NumPHIsToInsert <= 1) {
1547-
sinkLastInstruction(Blocks);
1483+
if (UpdateRE1)
1484+
RE1 = BB1->getInstList().rend();
1485+
if (UpdateRE2)
1486+
RE2 = BB2->getInstList().rend();
1487+
FirstNonPhiInBBEnd = &*I1;
15481488
NumSinkCommons++;
15491489
Changed = true;
15501490
}

test/CodeGen/ARM/avoid-cpsr-rmw.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ if.then:
106106

107107
if.else:
108108
store i32 3, i32* %p, align 4
109-
%incdec.ptr5 = getelementptr inbounds i32, i32* %p, i32 3
109+
%incdec.ptr5 = getelementptr inbounds i32, i32* %p, i32 2
110110
store i32 5, i32* %incdec.ptr1, align 4
111111
store i32 6, i32* %incdec.ptr5, align 4
112112
br label %if.end

0 commit comments

Comments
 (0)