@@ -1548,52 +1548,6 @@ FindMostPopularDest(BasicBlock *BB,
1548
1548
return MostPopularDest;
1549
1549
}
1550
1550
1551
- // Try to evaluate the value of V when the control flows from PredPredBB to
1552
- // BB->getSinglePredecessor() and then on to BB.
1553
- Constant *JumpThreadingPass::EvaluateOnPredecessorEdge (BasicBlock *BB,
1554
- BasicBlock *PredPredBB,
1555
- Value *V) {
1556
- BasicBlock *PredBB = BB->getSinglePredecessor ();
1557
- assert (PredBB && " Expected a single predecessor" );
1558
-
1559
- if (Constant *Cst = dyn_cast<Constant>(V)) {
1560
- return Cst;
1561
- }
1562
-
1563
- // Consult LVI if V is not an instruction in BB or PredBB.
1564
- Instruction *I = dyn_cast<Instruction>(V);
1565
- if (!I || (I->getParent () != BB && I->getParent () != PredBB)) {
1566
- if (DTU->hasPendingDomTreeUpdates ())
1567
- LVI->disableDT ();
1568
- else
1569
- LVI->enableDT ();
1570
- return LVI->getConstantOnEdge (V, PredPredBB, PredBB, nullptr );
1571
- }
1572
-
1573
- // Look into a PHI argument.
1574
- if (PHINode *PHI = dyn_cast<PHINode>(V)) {
1575
- if (PHI->getParent () == PredBB)
1576
- return dyn_cast<Constant>(PHI->getIncomingValueForBlock (PredPredBB));
1577
- return nullptr ;
1578
- }
1579
-
1580
- // If we have a CmpInst, try to fold it for each incoming edge into PredBB.
1581
- if (CmpInst *CondCmp = dyn_cast<CmpInst>(V)) {
1582
- if (CondCmp->getParent () == BB) {
1583
- Constant *Op0 =
1584
- EvaluateOnPredecessorEdge (BB, PredPredBB, CondCmp->getOperand (0 ));
1585
- Constant *Op1 =
1586
- EvaluateOnPredecessorEdge (BB, PredPredBB, CondCmp->getOperand (1 ));
1587
- if (Op0 && Op1) {
1588
- return ConstantExpr::getCompare (CondCmp->getPredicate (), Op0, Op1);
1589
- }
1590
- }
1591
- return nullptr ;
1592
- }
1593
-
1594
- return nullptr ;
1595
- }
1596
-
1597
1551
bool JumpThreadingPass::ProcessThreadableEdges (Value *Cond, BasicBlock *BB,
1598
1552
ConstantPreference Preference,
1599
1553
Instruction *CxtI) {
@@ -1603,12 +1557,8 @@ bool JumpThreadingPass::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
1603
1557
return false ;
1604
1558
1605
1559
PredValueInfoTy PredValues;
1606
- if (!ComputeValueKnownInPredecessors (Cond, BB, PredValues, Preference,
1607
- CxtI)) {
1608
- // We don't have known values in predecessors. See if we can thread through
1609
- // BB and its sole predecessor.
1610
- return MaybeThreadThroughTwoBasicBlocks (BB, Cond);
1611
- }
1560
+ if (!ComputeValueKnownInPredecessors (Cond, BB, PredValues, Preference, CxtI))
1561
+ return false ;
1612
1562
1613
1563
assert (!PredValues.empty () &&
1614
1564
" ComputeValueKnownInPredecessors returned true with no values" );
@@ -2065,182 +2015,6 @@ JumpThreadingPass::CloneInstructions(BasicBlock::iterator BI,
2065
2015
return ValueMapping;
2066
2016
}
2067
2017
2068
- // / Attempt to thread through two successive basic blocks.
2069
- bool JumpThreadingPass::MaybeThreadThroughTwoBasicBlocks (BasicBlock *BB,
2070
- Value *Cond) {
2071
- // Consider:
2072
- //
2073
- // PredBB:
2074
- // %var = phi i32* [ null, %bb1 ], [ @a, %bb2 ]
2075
- // %tobool = icmp eq i32 %cond, 0
2076
- // br i1 %tobool, label %BB, label ...
2077
- //
2078
- // BB:
2079
- // %cmp = icmp eq i32* %var, null
2080
- // br i1 %cmp, label ..., label ...
2081
- //
2082
- // We don't know the value of %var at BB even if we know which incoming edge
2083
- // we take to BB. However, once we duplicate PredBB for each of its incoming
2084
- // edges (say, PredBB1 and PredBB2), we know the value of %var in each copy of
2085
- // PredBB. Then we can thread edges PredBB1->BB and PredBB2->BB through BB.
2086
-
2087
- // Require that BB end with a Branch for simplicity.
2088
- BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator ());
2089
- if (!CondBr)
2090
- return false ;
2091
-
2092
- // BB must have exactly one predecessor.
2093
- BasicBlock *PredBB = BB->getSinglePredecessor ();
2094
- if (!PredBB)
2095
- return false ;
2096
-
2097
- // Require that PredBB end with a Branch. If PredBB ends with an
2098
- // unconditional branch, we should be merging PredBB and BB instead. For
2099
- // simplicity, we don't deal with a switch.
2100
- BranchInst *PredBBBranch = dyn_cast<BranchInst>(PredBB->getTerminator ());
2101
- if (!PredBBBranch)
2102
- return false ;
2103
-
2104
- // If PredBB has exactly one incoming edge, we don't gain anything by copying
2105
- // PredBB.
2106
- if (PredBB->getSinglePredecessor ())
2107
- return false ;
2108
-
2109
- // Don't thread across a loop header.
2110
- if (LoopHeaders.count (PredBB))
2111
- return false ;
2112
-
2113
- // Find a predecessor that we can thread. For simplicity, we only consider a
2114
- // successor edge out of BB to which we thread exactly one incoming edge into
2115
- // PredBB.
2116
- unsigned ZeroCount = 0 ;
2117
- unsigned OneCount = 0 ;
2118
- BasicBlock *ZeroPred = nullptr ;
2119
- BasicBlock *OnePred = nullptr ;
2120
- for (BasicBlock *P : predecessors (PredBB)) {
2121
- if (Constant *Cst = EvaluateOnPredecessorEdge (BB, P, Cond)) {
2122
- if (Cst->isZeroValue ()) {
2123
- ZeroCount++;
2124
- ZeroPred = P;
2125
- } else {
2126
- OneCount++;
2127
- OnePred = P;
2128
- }
2129
- }
2130
- }
2131
-
2132
- // Disregard complicated cases where we have to thread multiple edges.
2133
- BasicBlock *PredPredBB;
2134
- if (ZeroCount == 1 ) {
2135
- PredPredBB = ZeroPred;
2136
- } else if (OneCount == 1 ) {
2137
- PredPredBB = OnePred;
2138
- } else {
2139
- return false ;
2140
- }
2141
-
2142
- BasicBlock *SuccBB = CondBr->getSuccessor (PredPredBB == ZeroPred);
2143
-
2144
- // If threading to the same block as we come from, we would infinite loop.
2145
- if (SuccBB == BB) {
2146
- LLVM_DEBUG (dbgs () << " Not threading across BB '" << BB->getName ()
2147
- << " ' - would thread to self!\n " );
2148
- return false ;
2149
- }
2150
-
2151
- // If threading this would thread across a loop header, don't thread the edge.
2152
- // See the comments above FindLoopHeaders for justifications and caveats.
2153
- if (LoopHeaders.count (BB) || LoopHeaders.count (SuccBB)) {
2154
- LLVM_DEBUG ({
2155
- bool BBIsHeader = LoopHeaders.count (BB);
2156
- bool SuccIsHeader = LoopHeaders.count (SuccBB);
2157
- dbgs () << " Not threading across "
2158
- << (BBIsHeader ? " loop header BB '" : " block BB '" )
2159
- << BB->getName () << " ' to dest "
2160
- << (SuccIsHeader ? " loop header BB '" : " block BB '" )
2161
- << SuccBB->getName ()
2162
- << " ' - it might create an irreducible loop!\n " ;
2163
- });
2164
- return false ;
2165
- }
2166
-
2167
- // Check the cost of duplicating BB and PredBB.
2168
- unsigned JumpThreadCost =
2169
- getJumpThreadDuplicationCost (BB, BB->getTerminator (), BBDupThreshold);
2170
- JumpThreadCost += getJumpThreadDuplicationCost (
2171
- PredBB, PredBB->getTerminator (), BBDupThreshold);
2172
- if (JumpThreadCost > BBDupThreshold) {
2173
- LLVM_DEBUG (dbgs () << " Not threading BB '" << BB->getName ()
2174
- << " ' - Cost is too high: " << JumpThreadCost << " \n " );
2175
- return false ;
2176
- }
2177
-
2178
- // Now we are ready to duplicate PredBB.
2179
- ThreadThroughTwoBasicBlocks (PredPredBB, PredBB, BB, SuccBB);
2180
- return true ;
2181
- }
2182
-
2183
- void JumpThreadingPass::ThreadThroughTwoBasicBlocks (BasicBlock *PredPredBB,
2184
- BasicBlock *PredBB,
2185
- BasicBlock *BB,
2186
- BasicBlock *SuccBB) {
2187
- LLVM_DEBUG (dbgs () << " Threading through '" << PredBB->getName () << " ' and '"
2188
- << BB->getName () << " '\n " );
2189
-
2190
- BranchInst *CondBr = cast<BranchInst>(BB->getTerminator ());
2191
- BranchInst *PredBBBranch = cast<BranchInst>(PredBB->getTerminator ());
2192
-
2193
- BasicBlock *NewBB =
2194
- BasicBlock::Create (PredBB->getContext (), PredBB->getName () + " .thread" ,
2195
- PredBB->getParent (), PredBB);
2196
- NewBB->moveAfter (PredBB);
2197
-
2198
- // Set the block frequency of NewBB.
2199
- if (HasProfileData) {
2200
- auto NewBBFreq = BFI->getBlockFreq (PredPredBB) *
2201
- BPI->getEdgeProbability (PredPredBB, PredBB);
2202
- BFI->setBlockFreq (NewBB, NewBBFreq.getFrequency ());
2203
- }
2204
-
2205
- // We are going to have to map operands from the original BB block to the new
2206
- // copy of the block 'NewBB'. If there are PHI nodes in PredBB, evaluate them
2207
- // to account for entry from PredPredBB.
2208
- DenseMap<Instruction *, Value *> ValueMapping =
2209
- CloneInstructions (PredBB->begin (), PredBB->end (), NewBB, PredPredBB);
2210
-
2211
- // Update the terminator of PredPredBB to jump to NewBB instead of PredBB.
2212
- // This eliminates predecessors from PredPredBB, which requires us to simplify
2213
- // any PHI nodes in PredBB.
2214
- Instruction *PredPredTerm = PredPredBB->getTerminator ();
2215
- for (unsigned i = 0 , e = PredPredTerm->getNumSuccessors (); i != e; ++i)
2216
- if (PredPredTerm->getSuccessor (i) == PredBB) {
2217
- PredBB->removePredecessor (PredPredBB, true );
2218
- PredPredTerm->setSuccessor (i, NewBB);
2219
- }
2220
-
2221
- AddPHINodeEntriesForMappedBlock (PredBBBranch->getSuccessor (0 ), PredBB, NewBB,
2222
- ValueMapping);
2223
- AddPHINodeEntriesForMappedBlock (PredBBBranch->getSuccessor (1 ), PredBB, NewBB,
2224
- ValueMapping);
2225
-
2226
- DTU->applyUpdatesPermissive (
2227
- {{DominatorTree::Insert, NewBB, CondBr->getSuccessor (0 )},
2228
- {DominatorTree::Insert, NewBB, CondBr->getSuccessor (1 )},
2229
- {DominatorTree::Insert, PredPredBB, NewBB},
2230
- {DominatorTree::Delete, PredPredBB, PredBB}});
2231
-
2232
- UpdateSSA (PredBB, NewBB, ValueMapping);
2233
-
2234
- // Clean up things like PHI nodes with single operands, dead instructions,
2235
- // etc.
2236
- SimplifyInstructionsInBlock (NewBB, TLI);
2237
- SimplifyInstructionsInBlock (PredBB, TLI);
2238
-
2239
- SmallVector<BasicBlock *, 1 > PredsToFactor;
2240
- PredsToFactor.push_back (NewBB);
2241
- ThreadEdge (BB, PredsToFactor, SuccBB);
2242
- }
2243
-
2244
2018
// / TryThreadEdge - Thread an edge if it's safe and profitable to do so.
2245
2019
bool JumpThreadingPass::TryThreadEdge (
2246
2020
BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs,
0 commit comments