- Timestamp:
- Apr 19, 2011, 11:12:07 PM (14 years ago)
- Location:
- clamav/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
clamav/trunk ¶
-
Property svn:mergeinfo
set to
/clamav/vendor/0.97 merged eligible
-
Property svn:mergeinfo
set to
-
TabularUnified clamav/trunk/libclamav/c++/llvm/lib/Transforms/Utils/SimplifyCFG.cpp ¶
r189 r319 225 225 if (!AggressiveInsts) return false; 226 226 // Okay, it looks like the instruction IS in the "condition". Check to 227 // see if it s a cheap instruction to unconditionally compute, and if it227 // see if it's a cheap instruction to unconditionally compute, and if it 228 228 // only uses stuff defined outside of the condition. If so, hoist it out. 229 229 if (!I->isSafeToSpeculativelyExecute()) … … 950 950 // Ignore any user that is not a PHI node in BB2. These can only occur in 951 951 // unreachable blocks, because they would not be dominated by the instr. 952 PHINode *PN = dyn_cast<PHINode>( UI);952 PHINode *PN = dyn_cast<PHINode>(*UI); 953 953 if (!PN || PN->getParent() != BB2) 954 954 return false; … … 1378 1378 BasicBlock *BB = BI->getParent(); 1379 1379 Instruction *Cond = dyn_cast<Instruction>(BI->getCondition()); 1380 if (Cond == 0) return false; 1381 1380 if (Cond == 0 || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) || 1381 Cond->getParent() != BB || !Cond->hasOneUse()) 1382 return false; 1382 1383 1383 1384 // Only allow this if the condition is a simple instruction that can be … … 1388 1389 while(isa<DbgInfoIntrinsic>(FrontIt)) 1389 1390 ++FrontIt; 1390 if ((!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) || 1391 Cond->getParent() != BB || &*FrontIt != Cond || !Cond->hasOneUse()) { 1391 1392 // Allow a single instruction to be hoisted in addition to the compare 1393 // that feeds the branch. We later ensure that any values that _it_ uses 1394 // were also live in the predecessor, so that we don't unnecessarily create 1395 // register pressure or inhibit out-of-order execution. 1396 Instruction *BonusInst = 0; 1397 if (&*FrontIt != Cond && 1398 FrontIt->hasOneUse() && *FrontIt->use_begin() == Cond && 1399 FrontIt->isSafeToSpeculativelyExecute()) { 1400 BonusInst = &*FrontIt; 1401 ++FrontIt; 1402 } 1403 1404 // Only a single bonus inst is allowed. 1405 if (&*FrontIt != Cond) 1392 1406 return false; 1393 }1394 1407 1395 1408 // Make sure the instruction after the condition is the cond branch. … … 1429 1442 !SafeToMergeTerminators(BI, PBI)) 1430 1443 continue; 1444 1445 // Ensure that any values used in the bonus instruction are also used 1446 // by the terminator of the predecessor. This means that those values 1447 // must already have been resolved, so we won't be inhibiting the 1448 // out-of-order core by speculating them earlier. 1449 if (BonusInst) { 1450 // Collect the values used by the bonus inst 1451 SmallPtrSet<Value*, 4> UsedValues; 1452 for (Instruction::op_iterator OI = BonusInst->op_begin(), 1453 OE = BonusInst->op_end(); OI != OE; ++OI) { 1454 Value* V = *OI; 1455 if (!isa<Constant>(V)) 1456 UsedValues.insert(V); 1457 } 1458 1459 SmallVector<std::pair<Value*, unsigned>, 4> Worklist; 1460 Worklist.push_back(std::make_pair(PBI->getOperand(0), 0)); 1461 1462 // Walk up to four levels back up the use-def chain of the predecessor's 1463 // terminator to see if all those values were used. The choice of four 1464 // levels is arbitrary, to provide a compile-time-cost bound. 1465 while (!Worklist.empty()) { 1466 std::pair<Value*, unsigned> Pair = Worklist.back(); 1467 Worklist.pop_back(); 1468 1469 if (Pair.second >= 4) continue; 1470 UsedValues.erase(Pair.first); 1471 if (UsedValues.empty()) break; 1472 1473 if (Instruction* I = dyn_cast<Instruction>(Pair.first)) { 1474 for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end(); 1475 OI != OE; ++OI) 1476 Worklist.push_back(std::make_pair(OI->get(), Pair.second+1)); 1477 } 1478 } 1479 1480 if (!UsedValues.empty()) return false; 1481 } 1431 1482 1432 1483 Instruction::BinaryOps Opc; … … 1458 1509 } 1459 1510 1511 // If we have a bonus inst, clone it into the predecessor block. 1512 Instruction *NewBonus = 0; 1513 if (BonusInst) { 1514 NewBonus = BonusInst->clone(); 1515 PredBlock->getInstList().insert(PBI, NewBonus); 1516 NewBonus->takeName(BonusInst); 1517 BonusInst->setName(BonusInst->getName()+".old"); 1518 } 1519 1460 1520 // Clone Cond into the predecessor basic block, and or/and the 1461 1521 // two conditions together. 1462 1522 Instruction *New = Cond->clone(); 1523 if (BonusInst) New->replaceUsesOfWith(BonusInst, NewBonus); 1463 1524 PredBlock->getInstList().insert(PBI, New); 1464 1525 New->takeName(Cond); … … 1514 1575 // predecessor, compute the PHI'd conditional value for all of the preds. 1515 1576 // Any predecessor where the condition is not computable we keep symbolic. 1516 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) 1517 if ((PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) && 1577 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 1578 BasicBlock *P = *PI; 1579 if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) && 1518 1580 PBI != BI && PBI->isConditional() && 1519 1581 PBI->getCondition() == BI->getCondition() && … … 1521 1583 bool CondIsTrue = PBI->getSuccessor(0) == BB; 1522 1584 NewPN->addIncoming(ConstantInt::get(Type::getInt1Ty(BB->getContext()), 1523 CondIsTrue), *PI);1585 CondIsTrue), P); 1524 1586 } else { 1525 NewPN->addIncoming(BI->getCondition(), *PI);1587 NewPN->addIncoming(BI->getCondition(), P); 1526 1588 } 1589 } 1527 1590 1528 1591 BI->setCondition(NewPN); … … 1662 1725 assert(BB && BB->getParent() && "Block not embedded in function!"); 1663 1726 assert(BB->getTerminator() && "Degenerate basic block encountered!"); 1664 assert(&BB->getParent()->getEntryBlock() != BB && 1665 "Can't Simplify entry block!");1666 1667 // Remove basic blocks that have no predecessors... or that just have themself1668 // as a predecessor. These are unreachable.1669 if (pred_begin(BB) == pred_end(BB) ||BB->getSinglePredecessor() == BB) {1727 1728 // Remove basic blocks that have no predecessors (except the entry block)... 1729 // or that just have themself as a predecessor. These are unreachable. 1730 if ((pred_begin(BB) == pred_end(BB) && 1731 &BB->getParent()->getEntryBlock() != BB) || 1732 BB->getSinglePredecessor() == BB) { 1670 1733 DEBUG(dbgs() << "Removing BB: \n" << *BB); 1671 1734 DeleteDeadBlock(BB); … … 1698 1761 SmallVector<BranchInst*, 8> CondBranchPreds; 1699 1762 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { 1700 TerminatorInst *PTI = (*PI)->getTerminator(); 1763 BasicBlock *P = *PI; 1764 TerminatorInst *PTI = P->getTerminator(); 1701 1765 if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) { 1702 1766 if (BI->isUnconditional()) 1703 UncondBranchPreds.push_back( *PI);1767 UncondBranchPreds.push_back(P); 1704 1768 else 1705 1769 CondBranchPreds.push_back(BI); … … 1769 1833 1770 1834 // Insert the call now. 1771 SmallVector<Value*,8> Args(II->op_begin() +3, II->op_end());1835 SmallVector<Value*,8> Args(II->op_begin(), II->op_end()-3); 1772 1836 CallInst *CI = CallInst::Create(II->getCalledValue(), 1773 1837 Args.begin(), Args.end(), … … 1817 1881 ++BBI; 1818 1882 if (BBI->isTerminator()) // Terminator is the only non-phi instruction! 1819 if (TryToSimplifyUncondBranchFromEmptyBlock(BB)) 1820 return true; 1883 if (BB != &BB->getParent()->getEntryBlock()) 1884 if (TryToSimplifyUncondBranchFromEmptyBlock(BB)) 1885 return true; 1821 1886 1822 1887 } else { // Conditional branch … … 1827 1892 if (BasicBlock *OnlyPred = BB->getSinglePredecessor()) 1828 1893 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred)) 1829 return SimplifyCFG(BB) | | 1;1894 return SimplifyCFG(BB) | true; 1830 1895 1831 1896 // This block must be empty, except for the setcond inst, if it exists. … … 1861 1926 // predecessor and use logical operations to pick the right destination. 1862 1927 if (FoldBranchToCommonDest(BI)) 1863 return SimplifyCFG(BB) | 1;1928 return SimplifyCFG(BB) | true; 1864 1929 1865 1930 … … 1971 2036 1972 2037 // Insert the call now... 1973 SmallVector<Value*, 8> Args(II->op_begin() +3, II->op_end());2038 SmallVector<Value*, 8> Args(II->op_begin(), II->op_end()-3); 1974 2039 CallInst *CI = CallInst::Create(II->getCalledValue(), 1975 2040 Args.begin(), Args.end(), … … 1977 2042 CI->setCallingConv(II->getCallingConv()); 1978 2043 CI->setAttributes(II->getAttributes()); 1979 // If the invoke produced a value, the Call does now instead.2044 // If the invoke produced a value, the call does now instead. 1980 2045 II->replaceAllUsesWith(CI); 1981 2046 delete II; … … 1986 2051 1987 2052 // If this block is now dead, remove it. 1988 if (pred_begin(BB) == pred_end(BB)) { 2053 if (pred_begin(BB) == pred_end(BB) && 2054 BB != &BB->getParent()->getEntryBlock()) { 1989 2055 // We know there are no successors, so just nuke the block. 1990 2056 M->getBasicBlockList().erase(BB); 1991 2057 return true; 1992 2058 } 2059 } 2060 } else if (IndirectBrInst *IBI = 2061 dyn_cast<IndirectBrInst>(BB->getTerminator())) { 2062 // Eliminate redundant destinations. 2063 SmallPtrSet<Value *, 8> Succs; 2064 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) { 2065 BasicBlock *Dest = IBI->getDestination(i); 2066 if (!Dest->hasAddressTaken() || !Succs.insert(Dest)) { 2067 Dest->removePredecessor(BB); 2068 IBI->removeDestination(i); 2069 --i; --e; 2070 Changed = true; 2071 } 2072 } 2073 2074 if (IBI->getNumDestinations() == 0) { 2075 // If the indirectbr has no successors, change it to unreachable. 2076 new UnreachableInst(IBI->getContext(), IBI); 2077 IBI->eraseFromParent(); 2078 Changed = true; 2079 } else if (IBI->getNumDestinations() == 1) { 2080 // If the indirectbr has one successor, change it to a direct branch. 2081 BranchInst::Create(IBI->getDestination(0), IBI); 2082 IBI->eraseFromParent(); 2083 Changed = true; 1993 2084 } 1994 2085 } … … 2005 2096 // into our predecessor. 2006 2097 pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); 2007 BasicBlock *OnlyPred = *PI++; 2008 for (; PI != PE; ++PI) // Search all predecessors, see if they are all same 2009 if (*PI != OnlyPred) { 2098 BasicBlock *OnlyPred = 0; 2099 for (; PI != PE; ++PI) { // Search all predecessors, see if they are all same 2100 if (!OnlyPred) 2101 OnlyPred = *PI; 2102 else if (*PI != OnlyPred) { 2010 2103 OnlyPred = 0; // There are multiple different predecessors... 2011 2104 break; 2012 2105 } 2106 } 2013 2107 2014 2108 if (OnlyPred) … … 2109 2203 /// of the CFG. It returns true if a modification was made. 2110 2204 /// 2111 /// WARNING: The entry node of a function may not be simplified.2112 ///2113 2205 bool llvm::SimplifyCFG(BasicBlock *BB, const TargetData *TD) { 2114 2206 return SimplifyCFGOpt(TD).run(BB);
Note:
See TracChangeset
for help on using the changeset viewer.