Fix formatting in ssabuilder
[platform/upstream/coreclr.git] / src / jit / ssabuilder.cpp
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 // ==++==
6 //
7
8 //
9
10 //
11 // ==--==
12
13 /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
14 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15 XX                                                                           XX
16 XX                                  SSA                                      XX
17 XX                                                                           XX
18 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
19 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
20 */
21
22 #include "jitpch.h"
23 #include "ssaconfig.h"
24 #include "ssarenamestate.h"
25 #include "ssabuilder.h"
26
27 namespace
28 {
29 /**
30  * Method that finds a common IDom parent, much like least common ancestor.
31  *
32  * @param finger1 A basic block that might share IDom ancestor with finger2.
33  * @param finger2 A basic block that might share IDom ancestor with finger1.
34  *
35  * @see "A simple, fast dominance algorithm" by Keith D. Cooper, Timothy J. Harvey, Ken Kennedy.
36  *
37  * @return A basic block whose IDom is the dominator for finger1 and finger2,
38  * or else NULL.  This may be called while immediate dominators are being
39  * computed, and if the input values are members of the same loop (each reachable from the other),
40  * then one may not yet have its immediate dominator computed when we are attempting
41  * to find the immediate dominator of the other.  So a NULL return value means that the
42  * the two inputs are in a cycle, not that they don't have a common dominator ancestor.
43  */
44 static inline BasicBlock* IntersectDom(BasicBlock* finger1, BasicBlock* finger2)
45 {
46     while (finger1 != finger2)
47     {
48         if (finger1 == nullptr || finger2 == nullptr)
49         {
50             return nullptr;
51         }
52         while (finger1 != nullptr && finger1->bbPostOrderNum < finger2->bbPostOrderNum)
53         {
54             finger1 = finger1->bbIDom;
55         }
56         if (finger1 == nullptr)
57         {
58             return nullptr;
59         }
60         while (finger2 != nullptr && finger2->bbPostOrderNum < finger1->bbPostOrderNum)
61         {
62             finger2 = finger2->bbIDom;
63         }
64     }
65     return finger1;
66 }
67
68 } // end of anonymous namespace.
69
70 // =================================================================================
71 //                                      SSA
72 // =================================================================================
73
74 void Compiler::fgSsaBuild()
75 {
76     IAllocator* pIAllocator = new (this, CMK_SSA) CompAllocator(this, CMK_SSA);
77
78     // If this is not the first invocation, reset data structures for SSA.
79     if (fgSsaPassesCompleted > 0)
80     {
81         fgResetForSsa();
82     }
83
84     SsaBuilder builder(this, pIAllocator);
85     builder.Build();
86     fgSsaPassesCompleted++;
87 #ifdef DEBUG
88     JitTestCheckSSA();
89 #endif // DEBUG
90
91 #ifdef DEBUG
92     if (verbose)
93     {
94         JITDUMP("\nAfter fgSsaBuild:\n");
95         fgDispBasicBlocks(/*dumpTrees*/ true);
96     }
97 #endif // DEBUG
98 }
99
100 void Compiler::fgResetForSsa()
101 {
102     for (unsigned i = 0; i < lvaCount; ++i)
103     {
104         lvaTable[i].lvPerSsaData.Reset();
105     }
106     for (BasicBlock* blk = fgFirstBB; blk != nullptr; blk = blk->bbNext)
107     {
108         // Eliminate phis.
109         blk->bbHeapSsaPhiFunc = nullptr;
110         if (blk->bbTreeList != nullptr)
111         {
112             GenTreePtr last = blk->bbTreeList->gtPrev;
113             blk->bbTreeList = blk->FirstNonPhiDef();
114             if (blk->bbTreeList != nullptr)
115             {
116                 blk->bbTreeList->gtPrev = last;
117             }
118         }
119     }
120 }
121
122 /**
123  *  Constructor for the SSA builder.
124  *
125  *  @param pCompiler Current compiler instance.
126  *
127  *  @remarks Initializes the class and member pointers/objects that use constructors.
128  */
129 SsaBuilder::SsaBuilder(Compiler* pCompiler, IAllocator* pIAllocator)
130     : m_pCompiler(pCompiler)
131     , m_allocator(pIAllocator)
132
133 #ifdef SSA_FEATURE_DOMARR
134     , m_pDomPreOrder(NULL)
135     , m_pDomPostOrder(NULL)
136 #endif
137 #ifdef SSA_FEATURE_USEDEF
138     , m_uses(jitstd::allocator<void>(pIAllocator))
139     , m_defs(jitstd::allocator<void>(pIAllocator))
140 #endif
141 {
142 }
143
144 //------------------------------------------------------------------------
145 //  TopologicalSort: Topologically sort the graph and return the number of nodes visited.
146 //
147 //  Arguments:
148 //     postOrder - The array in which the arranged basic blocks have to be returned.
149 //     count - The size of the postOrder array.
150 //
151 //  Return Value:
152 //     The number of nodes visited while performing DFS on the graph.
153
154 int SsaBuilder::TopologicalSort(BasicBlock** postOrder, int count)
155 {
156     Compiler* comp = m_pCompiler;
157
158     BitVecTraits traits(comp->fgBBNumMax + 1, comp);
159     BitVec       BITVEC_INIT_NOCOPY(visited, BitVecOps::MakeEmpty(&traits));
160
161     // Display basic blocks.
162     DBEXEC(VERBOSE, comp->fgDispBasicBlocks());
163     DBEXEC(VERBOSE, comp->fgDispHandlerTab());
164
165     // Compute order.
166     int         postIndex = 0;
167     BasicBlock* block     = comp->fgFirstBB;
168     BitVecOps::AddElemD(&traits, visited, block->bbNum);
169
170     ArrayStack<BasicBlock*>      blocks(comp);
171     ArrayStack<AllSuccessorIter> iterators(comp);
172     ArrayStack<AllSuccessorIter> ends(comp);
173
174     // there are three stacks used here and all should be same height
175     // the first is for blocks
176     // the second is the iterator to keep track of what succ of the block we are looking at
177     // and the third is the end marker iterator
178     blocks.Push(block);
179     iterators.Push(block->GetAllSuccs(comp).begin());
180     ends.Push(block->GetAllSuccs(comp).end());
181
182     while (blocks.Height() > 0)
183     {
184         block = blocks.Top();
185
186 #ifdef DEBUG
187         if (comp->verboseSsa)
188         {
189             printf("[SsaBuilder::TopologicalSort] Visiting BB%02u: ", block->bbNum);
190             printf("[");
191             unsigned numSucc = block->NumSucc(comp);
192             for (unsigned i = 0; i < numSucc; ++i)
193             {
194                 printf("BB%02u, ", block->GetSucc(i, comp)->bbNum);
195             }
196             EHSuccessorIter end = block->GetEHSuccs(comp).end();
197             for (EHSuccessorIter ehsi = block->GetEHSuccs(comp).begin(); ehsi != end; ++ehsi)
198             {
199                 printf("[EH]BB%02u, ", (*ehsi)->bbNum);
200             }
201             printf("]\n");
202         }
203 #endif
204
205         if (iterators.TopRef() != ends.TopRef())
206         {
207             // if the block on TOS still has unreached successors, visit them
208             AllSuccessorIter& iter = iterators.TopRef();
209             BasicBlock*       succ = *iter;
210             ++iter;
211
212             // push the children
213             if (!BitVecOps::IsMember(&traits, visited, succ->bbNum))
214             {
215                 blocks.Push(succ);
216                 iterators.Push(succ->GetAllSuccs(comp).begin());
217                 ends.Push(succ->GetAllSuccs(comp).end());
218                 BitVecOps::AddElemD(&traits, visited, succ->bbNum);
219             }
220         }
221         else
222         {
223             // all successors have been visited
224             blocks.Pop();
225             iterators.Pop();
226             ends.Pop();
227
228             postOrder[postIndex]  = block;
229             block->bbPostOrderNum = postIndex;
230             postIndex += 1;
231
232             DBG_SSA_JITDUMP("postOrder[%d] = [%p] and BB%02u\n", postIndex, dspPtr(block), block->bbNum);
233         }
234     }
235
236     // In the absence of EH (because catch/finally have no preds), this should be valid.
237     // assert(postIndex == (count - 1));
238
239     return postIndex;
240 }
241
242 /**
243  * Computes the immediate dominator IDom for each block iteratively.
244  *
245  * @param postOrder The array of basic blocks arranged in postOrder.
246  * @param count The size of valid elements in the postOrder array.
247  *
248  * @see "A simple, fast dominance algorithm." paper.
249  */
250 void SsaBuilder::ComputeImmediateDom(BasicBlock** postOrder, int count)
251 {
252     JITDUMP("[SsaBuilder::ComputeImmediateDom]\n");
253
254     // TODO-Cleanup: We currently have two dominance computations happening.  We should unify them; for
255     // now, at least forget the results of the first.
256     for (BasicBlock* blk = m_pCompiler->fgFirstBB; blk != nullptr; blk = blk->bbNext)
257     {
258         blk->bbIDom = nullptr;
259     }
260
261     // Add entry point to processed as its IDom is NULL.
262     BitVecTraits traits(m_pCompiler->fgBBNumMax + 1, m_pCompiler);
263     BitVec       BITVEC_INIT_NOCOPY(processed, BitVecOps::MakeEmpty(&traits));
264
265     BitVecOps::AddElemD(&traits, processed, m_pCompiler->fgFirstBB->bbNum);
266     assert(postOrder[count - 1] == m_pCompiler->fgFirstBB);
267
268     bool changed = true;
269     while (changed)
270     {
271         changed = false;
272
273         // In reverse post order, except for the entry block (count - 1 is entry BB).
274         for (int i = count - 2; i >= 0; --i)
275         {
276             BasicBlock* block = postOrder[i];
277
278             DBG_SSA_JITDUMP("Visiting in reverse post order: BB%02u.\n", block->bbNum);
279
280             // Find the first processed predecessor block.
281             BasicBlock* predBlock = nullptr;
282             for (flowList* pred = m_pCompiler->BlockPredsWithEH(block); pred; pred = pred->flNext)
283             {
284                 if (BitVecOps::IsMember(&traits, processed, pred->flBlock->bbNum))
285                 {
286                     predBlock = pred->flBlock;
287                     break;
288                 }
289             }
290
291             // There could just be a single basic block, so just check if there were any preds.
292             if (predBlock != nullptr)
293             {
294                 DBG_SSA_JITDUMP("Pred block is BB%02u.\n", predBlock->bbNum);
295             }
296
297             // Intersect DOM, if computed, for all predecessors.
298             BasicBlock* bbIDom = predBlock;
299             for (flowList* pred = m_pCompiler->BlockPredsWithEH(block); pred; pred = pred->flNext)
300             {
301                 if (predBlock != pred->flBlock)
302                 {
303                     BasicBlock* domAncestor = IntersectDom(pred->flBlock, bbIDom);
304                     // The result may be NULL if "block" and "pred->flBlock" are part of a
305                     // cycle -- neither is guaranteed ordered wrt the other in reverse postorder,
306                     // so we may be computing the IDom of "block" before the IDom of "pred->flBlock" has
307                     // been computed.  But that's OK -- if they're in a cycle, they share the same immediate
308                     // dominator, so the contribution of "pred->flBlock" is not necessary to compute
309                     // the result.
310                     if (domAncestor != nullptr)
311                     {
312                         bbIDom = domAncestor;
313                     }
314                 }
315             }
316
317             // Did we change the bbIDom value?  If so, we go around the outer loop again.
318             if (block->bbIDom != bbIDom)
319             {
320                 changed = true;
321
322                 // IDom has changed, update it.
323                 DBG_SSA_JITDUMP("bbIDom of BB%02u becomes BB%02u.\n", block->bbNum, bbIDom ? bbIDom->bbNum : 0);
324                 block->bbIDom = bbIDom;
325             }
326
327             // Mark the current block as processed.
328             BitVecOps::AddElemD(&traits, processed, block->bbNum);
329
330             DBG_SSA_JITDUMP("Marking block BB%02u as processed.\n", block->bbNum);
331         }
332     }
333 }
334
335 #ifdef SSA_FEATURE_DOMARR
336 /**
337  * Walk the DOM tree and compute pre and post-order arrangement of the tree.
338  *
339  * @param curBlock The current block being operated on at some recursive level.
340  * @param domTree The DOM tree as a map (block -> set of child blocks.)
341  * @param preIndex The initial index given to the first block visited in pre order.
342  * @param postIndex The initial index given to the first block visited in post order.
343  *
344  * @remarks This would help us answer queries such as "a dom b?" in constant time.
345  *          For example, if a dominated b, then Pre[a] < Pre[b] but Post[a] > Post[b]
346  */
347 void SsaBuilder::DomTreeWalk(BasicBlock* curBlock, BlkToBlkSetMap* domTree, int* preIndex, int* postIndex)
348 {
349     JITDUMP("[SsaBuilder::DomTreeWalk] block [%p], BB%02u:\n", dspPtr(curBlock), curBlock->bbNum);
350
351     // Store the order number at the block number in the pre order list.
352     m_pDomPreOrder[curBlock->bbNum] = *preIndex;
353     ++(*preIndex);
354
355     BlkSet* pBlkSet;
356     if (domTree->Lookup(curBlock, &pBlkSet))
357     {
358         for (BlkSet::KeyIterator ki = pBlkSet->Begin(); !ki.Equal(pBlkSet->End()); ++ki)
359         {
360             if (curBlock != ki.Get())
361             {
362                 DomTreeWalk(ki.Get(), domTree, preIndex, postIndex);
363             }
364         }
365     }
366
367     // Store the order number at the block number in the post order list.
368     m_pDomPostOrder[curBlock->bbNum] = *postIndex;
369     ++(*postIndex);
370 }
371 #endif
372
373 /**
374  * Using IDom of each basic block, add a mapping from block->IDom -> block.
375  * @param pCompiler Compiler instance
376  * @param block The basic block that will become the child node of it's iDom.
377  * @param domTree The output domTree which will hold the mapping "block->bbIDom" -> "block"
378  *
379  */
380 /* static */
381 void SsaBuilder::ConstructDomTreeForBlock(Compiler* pCompiler, BasicBlock* block, BlkToBlkSetMap* domTree)
382 {
383     BasicBlock* bbIDom = block->bbIDom;
384
385     // bbIDom for (only) fgFirstBB will be NULL.
386     if (bbIDom == nullptr)
387     {
388         return;
389     }
390
391     // If the bbIDom map key doesn't exist, create one.
392     BlkSet* pBlkSet;
393     if (!domTree->Lookup(bbIDom, &pBlkSet))
394     {
395         pBlkSet = new (pCompiler->getAllocator()) BlkSet(pCompiler->getAllocator());
396         domTree->Set(bbIDom, pBlkSet);
397     }
398
399     DBG_SSA_JITDUMP("Inserting BB%02u as dom child of BB%02u.\n", block->bbNum, bbIDom->bbNum);
400     // Insert the block into the block's set.
401     pBlkSet->Set(block, true);
402 }
403
404 /**
405  * Using IDom of each basic block, compute the whole tree. If a block "b" has IDom "i",
406  * then, block "b" is dominated by "i". The mapping then is i -> { ..., b, ... }, in
407  * other words, "domTree" is a tree represented by nodes mapped to their children.
408  *
409  * @param pCompiler Compiler instance
410  * @param domTree The output domTree which will hold the mapping "block->bbIDom" -> "block"
411  *
412  */
413 /* static */
414 void SsaBuilder::ComputeDominators(Compiler* pCompiler, BlkToBlkSetMap* domTree)
415 {
416     JITDUMP("*************** In SsaBuilder::ComputeDominators(Compiler*, ...)\n");
417
418     // Construct the DOM tree from bbIDom
419     for (BasicBlock* block = pCompiler->fgFirstBB; block != nullptr; block = block->bbNext)
420     {
421         ConstructDomTreeForBlock(pCompiler, block, domTree);
422     }
423
424     DBEXEC(pCompiler->verboseSsa, DisplayDominators(domTree));
425 }
426
427 /**
428  * Compute the DOM tree into a map(block -> set of blocks) adjacency representation.
429  *
430  * Using IDom of each basic block, compute the whole tree. If a block "b" has IDom "i",
431  * then, block "b" is dominated by "i". The mapping then is i -> { ..., b, ... }
432  *
433  * @param postOrder The array of basic blocks arranged in postOrder.
434  * @param count The size of valid elements in the postOrder array.
435  * @param domTree A map of (block -> set of blocks) tree representation that is empty.
436  *
437  */
438 void SsaBuilder::ComputeDominators(BasicBlock** postOrder, int count, BlkToBlkSetMap* domTree)
439 {
440     JITDUMP("*************** In SsaBuilder::ComputeDominators(BasicBlock** postOrder, int count, ...)\n");
441
442     // Construct the DOM tree from bbIDom
443     for (int i = 0; i < count; ++i)
444     {
445         ConstructDomTreeForBlock(m_pCompiler, postOrder[i], domTree);
446     }
447
448     DBEXEC(m_pCompiler->verboseSsa, DisplayDominators(domTree));
449
450 #ifdef SSA_FEATURE_DOMARR
451     // Allocate space for constant time computation of (a DOM b?) query.
452     unsigned bbArrSize = m_pCompiler->fgBBNumMax + 1; // We will use 1-based bbNums as indices into these arrays, so
453                                                       // add 1.
454     m_pDomPreOrder  = jitstd::utility::allocate<int>(m_allocator, bbArrSize);
455     m_pDomPostOrder = jitstd::utility::allocate<int>(m_allocator, bbArrSize);
456
457     // Initial counters.
458     int preIndex  = 0;
459     int postIndex = 0;
460
461     // Populate the pre and post order of the tree.
462     DomTreeWalk(m_pCompiler->fgFirstBB, domTree, &preIndex, &postIndex);
463 #endif
464 }
465
466 #ifdef DEBUG
467
468 /**
469  * Display the DOM tree.
470  *
471  * @param domTree A map of (block -> set of blocks) tree representation.
472  */
473 /* static */
474 void SsaBuilder::DisplayDominators(BlkToBlkSetMap* domTree)
475 {
476     printf("After computing dominator tree: \n");
477     for (BlkToBlkSetMap::KeyIterator nodes = domTree->Begin(); !nodes.Equal(domTree->End()); ++nodes)
478     {
479         printf("BB%02u := {", nodes.Get()->bbNum);
480
481         BlkSet* pBlkSet = nodes.GetValue();
482         for (BlkSet::KeyIterator ki = pBlkSet->Begin(); !ki.Equal(pBlkSet->End()); ++ki)
483         {
484             if (!ki.Equal(pBlkSet->Begin()))
485             {
486                 printf(",");
487             }
488             printf("BB%02u", ki.Get()->bbNum);
489         }
490         printf("}\n");
491     }
492 }
493
494 #endif // DEBUG
495
496 // (Spec comment at declaration.)
497 // See "A simple, fast dominance algorithm", by Cooper, Harvey, and Kennedy.
498 // First we compute the dominance frontier for each block, then we convert these to iterated
499 // dominance frontiers by a closure operation.
500 BlkToBlkSetMap* SsaBuilder::ComputeIteratedDominanceFrontier(BasicBlock** postOrder, int count)
501 {
502     BlkToBlkSetMap* frontier = new (m_pCompiler->getAllocator()) BlkToBlkSetMap(m_pCompiler->getAllocator());
503
504     DBG_SSA_JITDUMP("Computing IDF: First computing DF.\n");
505
506     for (int i = 0; i < count; ++i)
507     {
508         BasicBlock* block = postOrder[i];
509
510         DBG_SSA_JITDUMP("Considering block BB%02u.\n", block->bbNum);
511
512         // Recall that B3 is in the dom frontier of B1 if there exists a B2
513         // such that B1 dom B2, !(B1 dom B3), and B3 is an immediate successor
514         // of B2.  (Note that B1 might be the same block as B2.)
515         // In that definition, we're considering "block" to be B3, and trying
516         // to find B1's.  To do so, first we consider the predecessors of "block",
517         // searching for candidate B2's -- "block" is obviously an immediate successor
518         // of its immediate predecessors.  If there are zero or one preds, then there
519         // is no pred, or else the single pred dominates "block", so no B2 exists.
520
521         flowList* blockPreds = m_pCompiler->BlockPredsWithEH(block);
522
523         // If block has more 0/1 predecessor, skip.
524         if (blockPreds == nullptr || blockPreds->flNext == nullptr)
525         {
526             DBG_SSA_JITDUMP("   Has %d preds; skipping.\n", blockPreds == nullptr ? 0 : 1);
527             continue;
528         }
529
530         // Otherwise, there are > 1 preds.  Each is a candidate B2 in the definition --
531         // *unless* it dominates "block"/B3.
532
533         for (flowList* pred = blockPreds; pred; pred = pred->flNext)
534         {
535             DBG_SSA_JITDUMP("   Considering predecessor BB%02u.\n", pred->flBlock->bbNum);
536
537             // If we've found a B2, then consider the possible B1's.  We start with
538             // B2, since a block dominates itself, then traverse upwards in the dominator
539             // tree, stopping when we reach the root, or the immediate dominator of "block"/B3.
540             // (Note that we are guaranteed to encounter this immediate dominator of "block"/B3:
541             // a predecessor must be dominated by B3's immediate dominator.)
542             // Along this way, make "block"/B3 part of the dom frontier of the B1.
543             // When we reach this immediate dominator, the definition no longer applies, since this
544             // potential B1 *does* dominate "block"/B3, so we stop.
545             for (BasicBlock* b1 = pred->flBlock; (b1 != nullptr) && (b1 != block->bbIDom); // !root && !loop
546                  b1             = b1->bbIDom)
547             {
548                 DBG_SSA_JITDUMP("      Adding BB%02u to dom frontier of pred dom BB%02u.\n", block->bbNum, b1->bbNum);
549                 BlkSet* pBlkSet;
550                 if (!frontier->Lookup(b1, &pBlkSet))
551                 {
552                     pBlkSet = new (m_pCompiler->getAllocator()) BlkSet(m_pCompiler->getAllocator());
553                     frontier->Set(b1, pBlkSet);
554                 }
555                 pBlkSet->Set(block, true);
556             }
557         }
558     }
559
560 #ifdef DEBUG
561     if (m_pCompiler->verboseSsa)
562     {
563         printf("\nComputed DF:\n");
564         for (int i = 0; i < count; ++i)
565         {
566             BasicBlock* block = postOrder[i];
567             printf("Block BB%02u := {", block->bbNum);
568
569             bool    first = true;
570             BlkSet* blkDf;
571             if (frontier->Lookup(block, &blkDf))
572             {
573                 for (BlkSet::KeyIterator blkDfIter = blkDf->Begin(); !blkDfIter.Equal(blkDf->End()); blkDfIter++)
574                 {
575                     if (!first)
576                     {
577                         printf(",");
578                     }
579                     printf("BB%02u", blkDfIter.Get()->bbNum);
580                     first = false;
581                 }
582             }
583             printf("}\n");
584         }
585     }
586 #endif
587
588     // Now do the closure operation to make the dominance frontier into an IDF.
589     // There's probably a better way to do this...
590     BlkToBlkSetMap* idf = new (m_pCompiler->getAllocator()) BlkToBlkSetMap(m_pCompiler->getAllocator());
591     for (BlkToBlkSetMap::KeyIterator kiFrontBlks = frontier->Begin(); !kiFrontBlks.Equal(frontier->End());
592          kiFrontBlks++)
593     {
594         // Create IDF(b)
595         BlkSet* blkIdf = new (m_pCompiler->getAllocator()) BlkSet(m_pCompiler->getAllocator());
596         idf->Set(kiFrontBlks.Get(), blkIdf);
597
598         // Keep track of what got newly added to the IDF, so we can go after their DFs.
599         BlkSet* delta = new (m_pCompiler->getAllocator()) BlkSet(m_pCompiler->getAllocator());
600         delta->Set(kiFrontBlks.Get(), true);
601
602         // Now transitively add DF+(delta) to IDF(b), each step gathering new "delta."
603         while (delta->GetCount() > 0)
604         {
605             // Extract a block x to be worked on.
606             BlkSet::KeyIterator ki     = delta->Begin();
607             BasicBlock*         curBlk = ki.Get();
608             // TODO-Cleanup: Remove(ki) doesn't work correctly in SimplerHash.
609             delta->Remove(curBlk);
610
611             // Get DF(x).
612             BlkSet* blkDf;
613             if (frontier->Lookup(curBlk, &blkDf))
614             {
615                 // Add DF(x) to IDF(b) and update "delta" i.e., new additions to IDF(b).
616                 for (BlkSet::KeyIterator ki = blkDf->Begin(); !ki.Equal(blkDf->End()); ki++)
617                 {
618                     if (!blkIdf->Lookup(ki.Get()))
619                     {
620                         delta->Set(ki.Get(), true);
621                         blkIdf->Set(ki.Get(), true);
622                     }
623                 }
624             }
625         }
626     }
627
628 #ifdef DEBUG
629     if (m_pCompiler->verboseSsa)
630     {
631         printf("\nComputed IDF:\n");
632         for (int i = 0; i < count; ++i)
633         {
634             BasicBlock* block = postOrder[i];
635             printf("Block BB%02u := {", block->bbNum);
636
637             bool    first = true;
638             BlkSet* blkIdf;
639             if (idf->Lookup(block, &blkIdf))
640             {
641                 for (BlkSet::KeyIterator ki = blkIdf->Begin(); !ki.Equal(blkIdf->End()); ki++)
642                 {
643                     if (!first)
644                     {
645                         printf(",");
646                     }
647                     printf("BB%02u", ki.Get()->bbNum);
648                     first = false;
649                 }
650             }
651             printf("}\n");
652         }
653     }
654 #endif
655
656     return idf;
657 }
658
659 /**
660  * Returns the phi GT_PHI node if the variable already has a phi node.
661  *
662  * @param block The block for which the existence of a phi node needs to be checked.
663  * @param lclNum The lclNum for which the occurrence of a phi node needs to be checked.
664  *
665  * @return If there is a phi node for the lclNum, returns the GT_PHI tree, else NULL.
666  */
667 static GenTree* GetPhiNode(BasicBlock* block, unsigned lclNum)
668 {
669     // Walk the statements for phi nodes.
670     for (GenTreePtr stmt = block->bbTreeList; stmt; stmt = stmt->gtNext)
671     {
672         // A prefix of the statements of the block are phi definition nodes. If we complete processing
673         // that prefix, exit.
674         if (!stmt->IsPhiDefnStmt())
675         {
676             break;
677         }
678
679         GenTreePtr tree = stmt->gtStmt.gtStmtExpr;
680
681         GenTreePtr phiLhs = tree->gtOp.gtOp1;
682         assert(phiLhs->OperGet() == GT_LCL_VAR);
683         if (phiLhs->gtLclVarCommon.gtLclNum == lclNum)
684         {
685             return tree->gtOp.gtOp2;
686         }
687     }
688     return nullptr;
689 }
690
691 /**
692  * Inserts phi functions at DF(b) for variables v that are live after the phi
693  * insertion point i.e., v in live-in(b).
694  *
695  * To do so, the function computes liveness, dominance frontier and inserts a phi node,
696  * if we have var v in def(b) and live-in(l) and l is in DF(b).
697  *
698  * @param postOrder The array of basic blocks arranged in postOrder.
699  * @param count The size of valid elements in the postOrder array.
700  */
701 void SsaBuilder::InsertPhiFunctions(BasicBlock** postOrder, int count)
702 {
703     JITDUMP("*************** In SsaBuilder::InsertPhiFunctions()\n");
704
705     // Compute liveness on the graph.
706     m_pCompiler->fgLocalVarLiveness();
707     EndPhase(PHASE_BUILD_SSA_LIVENESS);
708
709     // Compute dominance frontier.
710     BlkToBlkSetMap* frontier = ComputeIteratedDominanceFrontier(postOrder, count);
711     EndPhase(PHASE_BUILD_SSA_IDF);
712
713     JITDUMP("Inserting phi functions:\n");
714
715     for (int i = 0; i < count; ++i)
716     {
717         BasicBlock* block = postOrder[i];
718         DBG_SSA_JITDUMP("Considering dominance frontier of block BB%02u:\n", block->bbNum);
719
720         // If the block's dominance frontier is empty, go on to the next block.
721         BlkSet* blkIdf;
722         if (!frontier->Lookup(block, &blkIdf))
723         {
724             continue;
725         }
726
727         // For each local var number "lclNum" that "block" assigns to...
728         VARSET_ITER_INIT(m_pCompiler, defVars, block->bbVarDef, varIndex);
729         while (defVars.NextElem(m_pCompiler, &varIndex))
730         {
731             unsigned lclNum = m_pCompiler->lvaTrackedToVarNum[varIndex];
732             DBG_SSA_JITDUMP("  Considering local var V%02u:\n", lclNum);
733
734             if (m_pCompiler->fgExcludeFromSsa(lclNum))
735             {
736                 DBG_SSA_JITDUMP("  Skipping because it is excluded.\n");
737                 continue;
738             }
739
740             // For each block "bbInDomFront" that is in the dominance frontier of "block"...
741             for (BlkSet::KeyIterator iterBlk = blkIdf->Begin(); !iterBlk.Equal(blkIdf->End()); ++iterBlk)
742             {
743                 BasicBlock* bbInDomFront = iterBlk.Get();
744                 DBG_SSA_JITDUMP("     Considering BB%02u in dom frontier of BB%02u:\n", bbInDomFront->bbNum,
745                                 block->bbNum);
746
747                 // Check if variable "lclNum" is live in block "*iterBlk".
748                 if (!VarSetOps::IsMember(m_pCompiler, bbInDomFront->bbLiveIn, varIndex))
749                 {
750                     continue;
751                 }
752
753                 // Check if we've already inserted a phi node.
754                 if (GetPhiNode(bbInDomFront, lclNum) == nullptr)
755                 {
756                     // We have a variable i that is defined in block j and live at l, and l belongs to dom frontier of
757                     // j. So insert a phi node at l.
758                     JITDUMP("Inserting phi definition for V%02u at start of BB%02u.\n", lclNum, bbInDomFront->bbNum);
759
760                     GenTreePtr phiLhs = m_pCompiler->gtNewLclvNode(lclNum, m_pCompiler->lvaTable[lclNum].TypeGet());
761
762                     // Create 'phiRhs' as a GT_PHI node for 'lclNum', it will eventually hold a GT_LIST of GT_PHI_ARG
763                     // nodes. However we have to construct this list so for now the gtOp1 of 'phiRhs' is a nullptr.
764                     // It will get replaced with a GT_LIST of GT_PHI_ARG nodes in
765                     // SsaBuilder::AssignPhiNodeRhsVariables() and in SsaBuilder::AddDefToHandlerPhis()
766
767                     GenTreePtr phiRhs =
768                         m_pCompiler->gtNewOperNode(GT_PHI, m_pCompiler->lvaTable[lclNum].TypeGet(), nullptr);
769
770                     GenTreePtr phiAsg = m_pCompiler->gtNewAssignNode(phiLhs, phiRhs);
771
772                     GenTreePtr stmt = m_pCompiler->fgInsertStmtAtBeg(bbInDomFront, phiAsg);
773                     m_pCompiler->gtSetStmtInfo(stmt);
774                     m_pCompiler->fgSetStmtSeq(stmt);
775                 }
776             }
777         }
778
779         // Now make a similar phi definition if the block defines Heap.
780         if (block->bbHeapDef)
781         {
782             // For each block "bbInDomFront" that is in the dominance frontier of "block".
783             for (BlkSet::KeyIterator iterBlk = blkIdf->Begin(); !iterBlk.Equal(blkIdf->End()); ++iterBlk)
784             {
785                 BasicBlock* bbInDomFront = iterBlk.Get();
786                 DBG_SSA_JITDUMP("     Considering BB%02u in dom frontier of BB%02u for Heap phis:\n",
787                                 bbInDomFront->bbNum, block->bbNum);
788
789                 // Check if Heap is live into block "*iterBlk".
790                 if (!bbInDomFront->bbHeapLiveIn)
791                 {
792                     continue;
793                 }
794
795                 // Check if we've already inserted a phi node.
796                 if (bbInDomFront->bbHeapSsaPhiFunc == nullptr)
797                 {
798                     // We have a variable i that is defined in block j and live at l, and l belongs to dom frontier of
799                     // j. So insert a phi node at l.
800                     JITDUMP("Inserting phi definition for Heap at start of BB%02u.\n", bbInDomFront->bbNum);
801                     bbInDomFront->bbHeapSsaPhiFunc = BasicBlock::EmptyHeapPhiDef;
802                 }
803             }
804         }
805     }
806     EndPhase(PHASE_BUILD_SSA_INSERT_PHIS);
807 }
808
809 #ifdef SSA_FEATURE_USEDEF
810 /**
811  * Record a use point of a variable.
812  *
813  * The use point is just the tree that is a local variable use.
814  *
815  * @param tree Tree node where an SSA variable is used.
816  *
817  * @remarks The result is in the m_uses map :: [lclNum, ssaNum] -> tree.
818  */
819 void SsaBuilder::AddUsePoint(GenTree* tree)
820 {
821     assert(tree->IsLocal());
822     SsaVarName          key(tree->gtLclVarCommon.gtLclNum, tree->gtLclVarCommon.gtSsaNum);
823     VarToUses::iterator iter = m_uses.find(key);
824     if (iter == m_uses.end())
825     {
826         iter = m_uses.insert(key, VarToUses::mapped_type(m_uses.get_allocator()));
827     }
828     (*iter).second.push_back(tree);
829 }
830 #endif // !SSA_FEATURE_USEDEF
831
832 /**
833  * Record a def point of a variable.
834  *
835  * The def point is just the tree that is a local variable def.
836  *
837  * @param tree Tree node where an SSA variable is def'ed.
838  *
839  * @remarks The result is in the m_defs map :: [lclNum, ssaNum] -> tree.
840  */
841 void SsaBuilder::AddDefPoint(GenTree* tree, BasicBlock* blk)
842 {
843     Compiler::IndirectAssignmentAnnotation* pIndirAnnot;
844     // In the case of an "indirect assignment", where the LHS is IND of a byref to the local actually being assigned,
845     // we make the ASG tree the def point.
846     assert(tree->IsLocal() || IsIndirectAssign(tree, &pIndirAnnot));
847     unsigned lclNum;
848     unsigned defSsaNum;
849     if (tree->IsLocal())
850     {
851         lclNum    = tree->gtLclVarCommon.gtLclNum;
852         defSsaNum = m_pCompiler->GetSsaNumForLocalVarDef(tree);
853     }
854     else
855     {
856         bool b = m_pCompiler->GetIndirAssignMap()->Lookup(tree, &pIndirAnnot);
857         assert(b);
858         lclNum    = pIndirAnnot->m_lclNum;
859         defSsaNum = pIndirAnnot->m_defSsaNum;
860     }
861 #ifdef DEBUG
862     // Record that there's a new SSA def.
863     m_pCompiler->lvaTable[lclNum].lvNumSsaNames++;
864 #endif
865     // Record where the defn happens.
866     LclSsaVarDsc* ssaDef    = m_pCompiler->lvaTable[lclNum].GetPerSsaData(defSsaNum);
867     ssaDef->m_defLoc.m_blk  = blk;
868     ssaDef->m_defLoc.m_tree = tree;
869
870 #ifdef SSA_FEATURE_USEDEF
871     SsaVarName         key(lclNum, defSsaNum);
872     VarToDef::iterator iter = m_defs.find(key);
873     if (iter == m_defs.end())
874     {
875         iter = m_defs.insert(key, tree);
876         return;
877     }
878     // There can only be a single definition for an SSA var.
879     unreached();
880 #endif
881 }
882
883 bool SsaBuilder::IsIndirectAssign(GenTreePtr tree, Compiler::IndirectAssignmentAnnotation** ppIndirAssign)
884 {
885     return tree->OperGet() == GT_ASG && m_pCompiler->m_indirAssignMap != nullptr &&
886            m_pCompiler->GetIndirAssignMap()->Lookup(tree, ppIndirAssign);
887 }
888
889 /**
890  * Rename the local variable tree node.
891  *
892  * If the given tree node is a local variable, then for a def give a new count, if use,
893  * then give the count in the top of stack, i.e., current count (used for last def.)
894  *
895  * @param tree Tree node where an SSA variable is used or def'ed.
896  * @param pRenameState The incremental rename information stored during renaming process.
897  *
898  * @remarks This method has to maintain parity with TreePopStacks corresponding to pushes
899  *          it makes for defs.
900  */
901 void SsaBuilder::TreeRenameVariables(GenTree* tree, BasicBlock* block, SsaRenameState* pRenameState, bool isPhiDefn)
902 {
903     // This is perhaps temporary -- maybe should be done elsewhere.  Label GT_INDs on LHS of assignments, so we
904     // can skip these during (at least) value numbering.
905     if (tree->OperIsAssignment())
906     {
907         GenTreePtr lhs     = tree->gtOp.gtOp1->gtEffectiveVal(/*commaOnly*/ true);
908         GenTreePtr trueLhs = lhs->gtEffectiveVal(/*commaOnly*/ true);
909         if (trueLhs->OperIsIndir())
910         {
911             trueLhs->gtFlags |= GTF_IND_ASG_LHS;
912         }
913         else if (trueLhs->OperGet() == GT_CLS_VAR)
914         {
915             trueLhs->gtFlags |= GTF_CLS_VAR_ASG_LHS;
916         }
917     }
918
919     // Figure out if "tree" may make a new heap state (if we care for this block).
920     if (!block->bbHeapHavoc)
921     {
922         if (tree->OperIsAssignment() || tree->OperIsBlkOp())
923         {
924             if (m_pCompiler->ehBlockHasExnFlowDsc(block))
925             {
926                 GenTreeLclVarCommon* lclVarNode;
927                 if (!tree->DefinesLocal(m_pCompiler, &lclVarNode))
928                 {
929                     // It *may* define the heap in a non-havoc way.  Make a new SSA # -- associate with this node.
930                     unsigned count = pRenameState->CountForHeapDef();
931                     pRenameState->PushHeap(block, count);
932                     m_pCompiler->GetHeapSsaMap()->Set(tree, count);
933 #ifdef DEBUG
934                     if (JitTls::GetCompiler()->verboseSsa)
935                     {
936                         printf("Node ");
937                         Compiler::printTreeID(tree);
938                         printf(" (in try block) may define heap; ssa # = %d.\n", count);
939                     }
940 #endif // DEBUG
941
942                     // Now add this SSA # to all phis of the reachable catch blocks.
943                     AddHeapDefToHandlerPhis(block, count);
944                 }
945             }
946         }
947     }
948
949     Compiler::IndirectAssignmentAnnotation* pIndirAssign = nullptr;
950     if (!tree->IsLocal() && !IsIndirectAssign(tree, &pIndirAssign))
951     {
952         return;
953     }
954
955     if (pIndirAssign != nullptr)
956     {
957         unsigned lclNum = pIndirAssign->m_lclNum;
958         // Is this a variable we exclude from SSA?
959         if (m_pCompiler->fgExcludeFromSsa(lclNum))
960         {
961             pIndirAssign->m_defSsaNum = SsaConfig::RESERVED_SSA_NUM;
962             return;
963         }
964         // Otherwise...
965         if (!pIndirAssign->m_isEntire)
966         {
967             pIndirAssign->m_useSsaNum = pRenameState->CountForUse(lclNum);
968         }
969         unsigned count            = pRenameState->CountForDef(lclNum);
970         pIndirAssign->m_defSsaNum = count;
971         pRenameState->Push(block, lclNum, count);
972         AddDefPoint(tree, block);
973     }
974     else
975     {
976         unsigned lclNum = tree->gtLclVarCommon.gtLclNum;
977         // Is this a variable we exclude from SSA?
978         if (m_pCompiler->fgExcludeFromSsa(lclNum))
979         {
980             tree->gtLclVarCommon.SetSsaNum(SsaConfig::RESERVED_SSA_NUM);
981             return;
982         }
983
984         if (tree->gtFlags & GTF_VAR_DEF)
985         {
986             if (tree->gtFlags & GTF_VAR_USEASG)
987             {
988                 // This the "x" in something like "x op= y"; it is both a use (first), then a def.
989                 // The def will define a new SSA name, and record that in "x".  If we need the SSA
990                 // name of the use, we record it in a map reserved for that purpose.
991                 unsigned count = pRenameState->CountForUse(lclNum);
992                 tree->gtLclVarCommon.SetSsaNum(count);
993 #ifdef SSA_FEATURE_USEDEF
994                 AddUsePoint(tree);
995 #endif
996             }
997
998             // Give a count and increment.
999             unsigned count = pRenameState->CountForDef(lclNum);
1000             if (tree->gtFlags & GTF_VAR_USEASG)
1001             {
1002                 m_pCompiler->GetOpAsgnVarDefSsaNums()->Set(tree, count);
1003             }
1004             else
1005             {
1006                 tree->gtLclVarCommon.SetSsaNum(count);
1007             }
1008             pRenameState->Push(block, lclNum, count);
1009             AddDefPoint(tree, block);
1010
1011             // If necessary, add "lclNum/count" to the arg list of a phi def in any
1012             // handlers for try blocks that "block" is within.  (But only do this for "real" definitions,
1013             // not phi definitions.)
1014             if (!isPhiDefn)
1015             {
1016                 AddDefToHandlerPhis(block, lclNum, count);
1017             }
1018         }
1019         else if (!isPhiDefn) // Phi args already have ssa numbers.
1020         {
1021             // This case is obviated by the short-term "early-out" above...but it's in the right direction.
1022             // Is it a promoted struct local?
1023             if (m_pCompiler->lvaTable[lclNum].lvPromoted)
1024             {
1025                 assert(tree->TypeGet() == TYP_STRUCT);
1026                 LclVarDsc* varDsc = &m_pCompiler->lvaTable[lclNum];
1027                 // If has only a single field var, treat this as a use of that field var.
1028                 // Otherwise, we don't give SSA names to uses of promoted struct vars.
1029                 if (varDsc->lvFieldCnt == 1)
1030                 {
1031                     lclNum = varDsc->lvFieldLclStart;
1032                 }
1033                 else
1034                 {
1035                     tree->gtLclVarCommon.SetSsaNum(SsaConfig::RESERVED_SSA_NUM);
1036                     return;
1037                 }
1038             }
1039             // Give the count as top of stack.
1040             unsigned count = pRenameState->CountForUse(lclNum);
1041             tree->gtLclVarCommon.SetSsaNum(count);
1042 #ifdef SSA_FEATURE_USEDEF
1043             AddUsePoint(tree);
1044 #endif
1045         }
1046     }
1047 }
1048
1049 void SsaBuilder::AddDefToHandlerPhis(BasicBlock* block, unsigned lclNum, unsigned count)
1050 {
1051     assert(m_pCompiler->lvaTable[lclNum].lvTracked); // Precondition.
1052     unsigned lclIndex = m_pCompiler->lvaTable[lclNum].lvVarIndex;
1053
1054     EHblkDsc* tryBlk = m_pCompiler->ehGetBlockExnFlowDsc(block);
1055     if (tryBlk != nullptr)
1056     {
1057         DBG_SSA_JITDUMP(
1058             "Definition of local V%02u/d:%d in block BB%02u has exn handler; adding as phi arg to handlers.\n", lclNum,
1059             count, block->bbNum);
1060         while (true)
1061         {
1062             BasicBlock* handler = tryBlk->ExFlowBlock();
1063
1064             // Is "lclNum" live on entry to the handler?
1065             if (VarSetOps::IsMember(m_pCompiler, handler->bbLiveIn, lclIndex))
1066             {
1067 #ifdef DEBUG
1068                 bool phiFound = false;
1069 #endif
1070                 // A prefix of blocks statements will be SSA definitions.  Search those for "lclNum".
1071                 for (GenTreePtr stmt = handler->bbTreeList; stmt; stmt = stmt->gtNext)
1072                 {
1073                     // If the tree is not an SSA def, break out of the loop: we're done.
1074                     if (!stmt->IsPhiDefnStmt())
1075                     {
1076                         break;
1077                     }
1078
1079                     GenTreePtr tree = stmt->gtStmt.gtStmtExpr;
1080
1081                     assert(tree->IsPhiDefn());
1082
1083                     if (tree->gtOp.gtOp1->gtLclVar.gtLclNum == lclNum)
1084                     {
1085                         // It's the definition for the right local.  Add "count" to the RHS.
1086                         GenTreePtr      phi  = tree->gtOp.gtOp2;
1087                         GenTreeArgList* args = nullptr;
1088                         if (phi->gtOp.gtOp1 != nullptr)
1089                         {
1090                             args = phi->gtOp.gtOp1->AsArgList();
1091                         }
1092 #ifdef DEBUG
1093                         // Make sure it isn't already present: we should only add each definition once.
1094                         for (GenTreeArgList* curArgs = args; curArgs != nullptr; curArgs = curArgs->Rest())
1095                         {
1096                             GenTreePhiArg* phiArg = curArgs->Current()->AsPhiArg();
1097                             assert(phiArg->gtSsaNum != count);
1098                         }
1099 #endif
1100                         var_types      typ = m_pCompiler->lvaTable[lclNum].TypeGet();
1101                         GenTreePhiArg* newPhiArg =
1102                             new (m_pCompiler, GT_PHI_ARG) GenTreePhiArg(typ, lclNum, count, block);
1103
1104                         phi->gtOp.gtOp1 = new (m_pCompiler, GT_LIST) GenTreeArgList(newPhiArg, args);
1105                         m_pCompiler->gtSetStmtInfo(stmt);
1106                         m_pCompiler->fgSetStmtSeq(stmt);
1107 #ifdef DEBUG
1108                         phiFound = true;
1109 #endif
1110                         DBG_SSA_JITDUMP("   Added phi arg u:%d for V%02u to phi defn in handler block BB%02u.\n", count,
1111                                         lclNum, handler->bbNum);
1112                         break;
1113                     }
1114                 }
1115                 assert(phiFound);
1116             }
1117
1118             unsigned nextTryIndex = tryBlk->ebdEnclosingTryIndex;
1119             if (nextTryIndex == EHblkDsc::NO_ENCLOSING_INDEX)
1120             {
1121                 break;
1122             }
1123
1124             tryBlk = m_pCompiler->ehGetDsc(nextTryIndex);
1125         }
1126     }
1127 }
1128
1129 void SsaBuilder::AddHeapDefToHandlerPhis(BasicBlock* block, unsigned count)
1130 {
1131     if (m_pCompiler->ehBlockHasExnFlowDsc(block))
1132     {
1133         // Don't do anything for a compiler-inserted BBJ_ALWAYS that is a "leave helper".
1134         if (block->bbJumpKind == BBJ_ALWAYS && (block->bbFlags & BBF_INTERNAL) && (block->bbPrev->isBBCallAlwaysPair()))
1135         {
1136             return;
1137         }
1138
1139         // Otherwise...
1140         DBG_SSA_JITDUMP("Definition of Heap/d:%d in block BB%02u has exn handler; adding as phi arg to handlers.\n",
1141                         count, block->bbNum);
1142         EHblkDsc* tryBlk = m_pCompiler->ehGetBlockExnFlowDsc(block);
1143         while (true)
1144         {
1145             BasicBlock* handler = tryBlk->ExFlowBlock();
1146
1147             // Is Heap live on entry to the handler?
1148             if (handler->bbHeapLiveIn)
1149             {
1150                 assert(handler->bbHeapSsaPhiFunc != nullptr);
1151
1152                 // Add "count" to the phi args of Heap.
1153                 if (handler->bbHeapSsaPhiFunc == BasicBlock::EmptyHeapPhiDef)
1154                 {
1155                     handler->bbHeapSsaPhiFunc = new (m_pCompiler) BasicBlock::HeapPhiArg(count);
1156                 }
1157                 else
1158                 {
1159 #ifdef DEBUG
1160                     BasicBlock::HeapPhiArg* curArg = handler->bbHeapSsaPhiFunc;
1161                     while (curArg != nullptr)
1162                     {
1163                         assert(curArg->GetSsaNum() != count);
1164                         curArg = curArg->m_nextArg;
1165                     }
1166 #endif // DEBUG
1167                     handler->bbHeapSsaPhiFunc =
1168                         new (m_pCompiler) BasicBlock::HeapPhiArg(count, handler->bbHeapSsaPhiFunc);
1169                 }
1170
1171                 DBG_SSA_JITDUMP("   Added phi arg u:%d for Heap to phi defn in handler block BB%02u.\n", count,
1172                                 handler->bbNum);
1173             }
1174             unsigned tryInd = tryBlk->ebdEnclosingTryIndex;
1175             if (tryInd == EHblkDsc::NO_ENCLOSING_INDEX)
1176             {
1177                 break;
1178             }
1179             tryBlk = m_pCompiler->ehGetDsc(tryInd);
1180         }
1181     }
1182 }
1183
1184 /**
1185  * Walk the block's tree in the evaluation order and give var definitions and uses their
1186  * SSA names.
1187  *
1188  * @param block Block for which SSA variables have to be renamed.
1189  * @param pRenameState The incremental rename information stored during renaming process.
1190  *
1191  */
1192 void SsaBuilder::BlockRenameVariables(BasicBlock* block, SsaRenameState* pRenameState)
1193 {
1194     // Walk the statements of the block and rename the tree variables.
1195
1196     // First handle the incoming Heap state.
1197
1198     // Is there an Phi definition for heap at the start of this block?
1199     if (block->bbHeapSsaPhiFunc != nullptr)
1200     {
1201         unsigned count = pRenameState->CountForHeapDef();
1202         pRenameState->PushHeap(block, count);
1203
1204         DBG_SSA_JITDUMP("Ssa # for Heap phi on entry to BB%02u is %d.\n", block->bbNum, count);
1205     }
1206
1207     // Record the "in" Ssa # for Heap.
1208     block->bbHeapSsaNumIn = pRenameState->CountForHeapUse();
1209
1210     // We need to iterate over phi definitions, to give them SSA names, but we need
1211     // to know which are which, so we don't add phi definitions to handler phi arg lists.
1212     // Statements are phi defns until they aren't.
1213     bool       isPhiDefn   = true;
1214     GenTreePtr firstNonPhi = block->FirstNonPhiDef();
1215     for (GenTreePtr stmt = block->bbTreeList; stmt; stmt = stmt->gtNext)
1216     {
1217         if (stmt == firstNonPhi)
1218         {
1219             isPhiDefn = false;
1220         }
1221
1222         for (GenTreePtr tree = stmt->gtStmt.gtStmtList; tree; tree = tree->gtNext)
1223         {
1224             TreeRenameVariables(tree, block, pRenameState, isPhiDefn);
1225         }
1226     }
1227
1228     // Now handle the final heap state.
1229
1230     // If the block defines Heap, allocate an SSA variable for the final heap state in the block.
1231     // (This may be redundant with the last SSA var explicitly created, but there's no harm in that.)
1232     if (block->bbHeapDef)
1233     {
1234         unsigned count = pRenameState->CountForHeapDef();
1235         pRenameState->PushHeap(block, count);
1236         AddHeapDefToHandlerPhis(block, count);
1237     }
1238
1239     // Record the "out" Ssa" # for Heap.
1240     block->bbHeapSsaNumOut = pRenameState->CountForHeapUse();
1241
1242     DBG_SSA_JITDUMP("Ssa # for Heap on entry to BB%02u is %d; on exit is %d.\n", block->bbNum, block->bbHeapSsaNumIn,
1243                     block->bbHeapSsaNumOut);
1244 }
1245
1246 /**
1247  * Walk through the phi nodes of a given block and assign rhs variables to them.
1248  *
1249  * Also renumber the rhs variables from top of the stack.
1250  *
1251  * @param block Block for which phi nodes have to be assigned their rhs arguments.
1252  * @param pRenameState The incremental rename information stored during renaming process.
1253  *
1254  */
1255 void SsaBuilder::AssignPhiNodeRhsVariables(BasicBlock* block, SsaRenameState* pRenameState)
1256 {
1257     BasicBlock::AllSuccs allSuccs    = block->GetAllSuccs(m_pCompiler);
1258     AllSuccessorIter     allSuccsEnd = allSuccs.end();
1259     for (AllSuccessorIter allSuccsIter = allSuccs.begin(); allSuccsIter != allSuccsEnd; ++allSuccsIter)
1260     {
1261         BasicBlock* succ = (*allSuccsIter);
1262         // Walk the statements for phi nodes.
1263         for (GenTreePtr stmt = succ->bbTreeList; stmt != nullptr && stmt->IsPhiDefnStmt(); stmt = stmt->gtNext)
1264         {
1265             GenTreePtr tree = stmt->gtStmt.gtStmtExpr;
1266             assert(tree->IsPhiDefn());
1267
1268             // Get the phi node from GT_ASG.
1269             GenTreePtr phiNode = tree->gtOp.gtOp2;
1270             assert(phiNode->gtOp.gtOp1 == nullptr || phiNode->gtOp.gtOp1->OperGet() == GT_LIST);
1271
1272             unsigned lclNum = tree->gtOp.gtOp1->gtLclVar.gtLclNum;
1273             unsigned ssaNum = pRenameState->CountForUse(lclNum);
1274             // Search the arglist for an existing definition for ssaNum.
1275             // (Can we assert that its the head of the list?  This should only happen when we add
1276             // during renaming for a definition that occurs within a try, and then that's the last
1277             // value of the var within that basic block.)
1278             GenTreeArgList* argList = (phiNode->gtOp.gtOp1 == nullptr ? nullptr : phiNode->gtOp.gtOp1->AsArgList());
1279             bool            found   = false;
1280             while (argList != nullptr)
1281             {
1282                 if (argList->Current()->AsLclVarCommon()->GetSsaNum() == ssaNum)
1283                 {
1284                     found = true;
1285                     break;
1286                 }
1287                 argList = argList->Rest();
1288             }
1289             if (!found)
1290             {
1291                 GenTreePtr newPhiArg =
1292                     new (m_pCompiler, GT_PHI_ARG) GenTreePhiArg(tree->gtOp.gtOp1->TypeGet(), lclNum, ssaNum, block);
1293                 argList             = (phiNode->gtOp.gtOp1 == nullptr ? nullptr : phiNode->gtOp.gtOp1->AsArgList());
1294                 phiNode->gtOp.gtOp1 = new (m_pCompiler, GT_LIST) GenTreeArgList(newPhiArg, argList);
1295                 DBG_SSA_JITDUMP("  Added phi arg u:%d for V%02u from BB%02u in BB%02u.\n", ssaNum, lclNum, block->bbNum,
1296                                 succ->bbNum);
1297             }
1298
1299             m_pCompiler->gtSetStmtInfo(stmt);
1300             m_pCompiler->fgSetStmtSeq(stmt);
1301         }
1302
1303         // Now handle Heap.
1304         if (succ->bbHeapSsaPhiFunc != nullptr)
1305         {
1306             if (succ->bbHeapSsaPhiFunc == BasicBlock::EmptyHeapPhiDef)
1307             {
1308                 succ->bbHeapSsaPhiFunc = new (m_pCompiler) BasicBlock::HeapPhiArg(block);
1309             }
1310             else
1311             {
1312                 BasicBlock::HeapPhiArg* curArg = succ->bbHeapSsaPhiFunc;
1313                 bool                    found  = false;
1314                 // This is a quadratic algorithm.  We might need to consider some switch over to a hash table
1315                 // representation for the arguments of a phi node, to make this linear.
1316                 while (curArg != nullptr)
1317                 {
1318                     if (curArg->m_predBB == block)
1319                     {
1320                         found = true;
1321                         break;
1322                     }
1323                     curArg = curArg->m_nextArg;
1324                 }
1325                 if (!found)
1326                 {
1327                     succ->bbHeapSsaPhiFunc = new (m_pCompiler) BasicBlock::HeapPhiArg(block, succ->bbHeapSsaPhiFunc);
1328                 }
1329             }
1330             DBG_SSA_JITDUMP("  Added phi arg for Heap from BB%02u in BB%02u.\n", block->bbNum, succ->bbNum);
1331         }
1332
1333         // If "succ" is the first block of a try block (and "block" is not also in that try block)
1334         // then we must look at the vars that have phi defs in the corresponding handler;
1335         // the current SSA name for such vars must be included as an argument to that phi.
1336         if (m_pCompiler->bbIsTryBeg(succ))
1337         {
1338             assert(succ->hasTryIndex());
1339             unsigned tryInd = succ->getTryIndex();
1340
1341             while (tryInd != EHblkDsc::NO_ENCLOSING_INDEX)
1342             {
1343                 // Check if the predecessor "block" is within the same try block.
1344                 if (block->hasTryIndex())
1345                 {
1346                     for (unsigned blockTryInd = block->getTryIndex(); blockTryInd != EHblkDsc::NO_ENCLOSING_INDEX;
1347                          blockTryInd          = m_pCompiler->ehGetEnclosingTryIndex(blockTryInd))
1348                     {
1349                         if (blockTryInd == tryInd)
1350                         {
1351                             // It is; don't execute the loop below.
1352                             tryInd = EHblkDsc::NO_ENCLOSING_INDEX;
1353                             break;
1354                         }
1355                     }
1356
1357                     // The loop just above found that the predecessor "block" is within the same
1358                     // try block as "succ."  So we don't need to process this try, or any
1359                     // further outer try blocks here, since they would also contain both "succ"
1360                     // and "block".
1361                     if (tryInd == EHblkDsc::NO_ENCLOSING_INDEX)
1362                     {
1363                         break;
1364                     }
1365                 }
1366
1367                 EHblkDsc* succTry = m_pCompiler->ehGetDsc(tryInd);
1368                 // This is necessarily true on the first iteration, but not
1369                 // necessarily on the second and subsequent.
1370                 if (succTry->ebdTryBeg != succ)
1371                 {
1372                     break;
1373                 }
1374
1375                 // succ is the first block of this try.  Look at phi defs in the handler.
1376                 // For a filter, we consider the filter to be the "real" handler.
1377                 BasicBlock* handlerStart = succTry->ExFlowBlock();
1378
1379                 for (GenTreePtr stmt = handlerStart->bbTreeList; stmt; stmt = stmt->gtNext)
1380                 {
1381                     GenTreePtr tree = stmt->gtStmt.gtStmtExpr;
1382
1383                     // Check if the first n of the statements are phi nodes. If not, exit.
1384                     if (tree->OperGet() != GT_ASG || tree->gtOp.gtOp2 == nullptr ||
1385                         tree->gtOp.gtOp2->OperGet() != GT_PHI)
1386                     {
1387                         break;
1388                     }
1389
1390                     // Get the phi node from GT_ASG.
1391                     GenTreePtr lclVar = tree->gtOp.gtOp1;
1392                     unsigned   lclNum = lclVar->gtLclVar.gtLclNum;
1393
1394                     // If the variable is live-out of "blk", and is therefore live on entry to the try-block-start
1395                     // "succ", then we make sure the current SSA name for the
1396                     // var is one of the args of the phi node.  If not, go on.
1397                     LclVarDsc* lclVarDsc = &m_pCompiler->lvaTable[lclNum];
1398                     if (!lclVarDsc->lvTracked ||
1399                         !VarSetOps::IsMember(m_pCompiler, block->bbLiveOut, lclVarDsc->lvVarIndex))
1400                     {
1401                         continue;
1402                     }
1403
1404                     GenTreePtr phiNode = tree->gtOp.gtOp2;
1405                     assert(phiNode->gtOp.gtOp1 == nullptr || phiNode->gtOp.gtOp1->OperGet() == GT_LIST);
1406                     GenTreeArgList* argList = reinterpret_cast<GenTreeArgList*>(phiNode->gtOp.gtOp1);
1407
1408                     // What is the current SSAName from the predecessor for this local?
1409                     unsigned ssaNum = pRenameState->CountForUse(lclNum);
1410
1411                     // See if this ssaNum is already an arg to the phi.
1412                     bool alreadyArg = false;
1413                     for (GenTreeArgList* curArgs = argList; curArgs != nullptr; curArgs = curArgs->Rest())
1414                     {
1415                         if (curArgs->Current()->gtPhiArg.gtSsaNum == ssaNum)
1416                         {
1417                             alreadyArg = true;
1418                             break;
1419                         }
1420                     }
1421                     if (!alreadyArg)
1422                     {
1423                         // Add the new argument.
1424                         GenTreePtr newPhiArg =
1425                             new (m_pCompiler, GT_PHI_ARG) GenTreePhiArg(lclVar->TypeGet(), lclNum, ssaNum, block);
1426                         phiNode->gtOp.gtOp1 = new (m_pCompiler, GT_LIST) GenTreeArgList(newPhiArg, argList);
1427
1428                         DBG_SSA_JITDUMP("  Added phi arg u:%d for V%02u from BB%02u in BB%02u.\n", ssaNum, lclNum,
1429                                         block->bbNum, handlerStart->bbNum);
1430
1431                         m_pCompiler->gtSetStmtInfo(stmt);
1432                         m_pCompiler->fgSetStmtSeq(stmt);
1433                     }
1434                 }
1435
1436                 // Now handle Heap.
1437                 if (handlerStart->bbHeapSsaPhiFunc != nullptr)
1438                 {
1439                     if (handlerStart->bbHeapSsaPhiFunc == BasicBlock::EmptyHeapPhiDef)
1440                     {
1441                         handlerStart->bbHeapSsaPhiFunc = new (m_pCompiler) BasicBlock::HeapPhiArg(block);
1442                     }
1443                     else
1444                     {
1445 #ifdef DEBUG
1446                         BasicBlock::HeapPhiArg* curArg = handlerStart->bbHeapSsaPhiFunc;
1447                         while (curArg != nullptr)
1448                         {
1449                             assert(curArg->m_predBB != block);
1450                             curArg = curArg->m_nextArg;
1451                         }
1452 #endif // DEBUG
1453                         handlerStart->bbHeapSsaPhiFunc =
1454                             new (m_pCompiler) BasicBlock::HeapPhiArg(block, handlerStart->bbHeapSsaPhiFunc);
1455                     }
1456                     DBG_SSA_JITDUMP("  Added phi arg for Heap from BB%02u in BB%02u.\n", block->bbNum,
1457                                     handlerStart->bbNum);
1458                 }
1459
1460                 tryInd = succTry->ebdEnclosingTryIndex;
1461             }
1462         }
1463     }
1464 }
1465
1466 /**
1467  * Walk the block's tree in the evaluation order and reclaim rename stack for var definitions.
1468  *
1469  * @param block Block for which SSA variables have to be renamed.
1470  * @param pRenameState The incremental rename information stored during renaming process.
1471  *
1472  */
1473 void SsaBuilder::BlockPopStacks(BasicBlock* block, SsaRenameState* pRenameState)
1474 {
1475     // Pop the names given to the non-phi nodes.
1476     pRenameState->PopBlockStacks(block);
1477
1478     // And for Heap.
1479     pRenameState->PopBlockHeapStack(block);
1480 }
1481
1482 /**
1483  * Perform variable renaming.
1484  *
1485  * Walks the blocks and renames all var defs with ssa numbers and all uses with the
1486  * current count that is in the top of the stack. Assigns phi node rhs variables
1487  * (i.e., the arguments to the phi.) Then, calls the function recursively on child
1488  * nodes in the DOM tree to continue the renaming process.
1489  *
1490  * @param block Block for which SSA variables have to be renamed.
1491  * @param pRenameState The incremental rename information stored during renaming process.
1492  *
1493  * @remarks At the end of the method, m_uses and m_defs should be populated linking the
1494  *          uses and defs.
1495  *
1496  * @see Briggs, Cooper, Harvey and Simpson "Practical Improvements to the Construction
1497  *      and Destruction of Static Single Assignment Form."
1498  */
1499
1500 void SsaBuilder::RenameVariables(BlkToBlkSetMap* domTree, SsaRenameState* pRenameState)
1501 {
1502     JITDUMP("*************** In SsaBuilder::RenameVariables()\n");
1503
1504     // The first thing we do is treat parameters and must-init variables as if they have a
1505     // virtual definition before entry -- they start out at SSA name 1.
1506     for (unsigned i = 0; i < m_pCompiler->lvaCount; i++)
1507     {
1508         LclVarDsc* varDsc = &m_pCompiler->lvaTable[i];
1509
1510 #ifdef DEBUG
1511         varDsc->lvNumSsaNames = SsaConfig::UNINIT_SSA_NUM; // Start off fresh...
1512 #endif
1513
1514         if (varDsc->lvIsParam || m_pCompiler->info.compInitMem || varDsc->lvMustInit ||
1515             (varDsc->lvTracked &&
1516              VarSetOps::IsMember(m_pCompiler, m_pCompiler->fgFirstBB->bbLiveIn, varDsc->lvVarIndex)))
1517         {
1518             unsigned count = pRenameState->CountForDef(i);
1519
1520             // In ValueNum we'd assume un-inited variables get FIRST_SSA_NUM.
1521             assert(count == SsaConfig::FIRST_SSA_NUM);
1522 #ifdef DEBUG
1523             varDsc->lvNumSsaNames++;
1524 #endif
1525             pRenameState->Push(nullptr, i, count);
1526         }
1527     }
1528     // In ValueNum we'd assume un-inited heap gets FIRST_SSA_NUM.
1529     // The heap is a parameter.  Use FIRST_SSA_NUM as first SSA name.
1530     unsigned initHeapCount = pRenameState->CountForHeapDef();
1531     assert(initHeapCount == SsaConfig::FIRST_SSA_NUM);
1532     pRenameState->PushHeap(m_pCompiler->fgFirstBB, initHeapCount);
1533
1534     // Initialize the heap ssa numbers for unreachable blocks. ValueNum expects
1535     // heap ssa numbers to have some intitial value.
1536     for (BasicBlock* block = m_pCompiler->fgFirstBB; block; block = block->bbNext)
1537     {
1538         if (block->bbIDom == nullptr)
1539         {
1540             block->bbHeapSsaNumIn  = initHeapCount;
1541             block->bbHeapSsaNumOut = initHeapCount;
1542         }
1543     }
1544
1545     struct BlockWork
1546     {
1547         BasicBlock* m_blk;
1548         bool        m_processed; // Whether the this block have already been processed: its var renamed, and children
1549                                  // processed.
1550                                  // If so, awaiting only BlockPopStacks.
1551         BlockWork(BasicBlock* blk, bool processed = false) : m_blk(blk), m_processed(processed)
1552         {
1553         }
1554     };
1555     typedef jitstd::vector<BlockWork> BlockWorkStack;
1556     BlockWorkStack*                   blocksToDo =
1557         new (jitstd::utility::allocate<BlockWorkStack>(m_allocator), jitstd::placement_t()) BlockWorkStack(m_allocator);
1558
1559     blocksToDo->push_back(BlockWork(m_pCompiler->fgFirstBB)); // Probably have to include other roots of dom tree.
1560
1561     while (blocksToDo->size() != 0)
1562     {
1563         BlockWork blockWrk = blocksToDo->back();
1564         blocksToDo->pop_back();
1565         BasicBlock* block = blockWrk.m_blk;
1566
1567         DBG_SSA_JITDUMP("[SsaBuilder::RenameVariables](BB%02u, processed = %d)\n", block->bbNum, blockWrk.m_processed);
1568
1569         if (!blockWrk.m_processed)
1570         {
1571             // Push the block back on the stack with "m_processed" true, to record the fact that when its children have
1572             // been (recursively) processed, we still need to call BlockPopStacks on it.
1573             blocksToDo->push_back(BlockWork(block, true));
1574
1575             // Walk the block give counts to DEFs and give top of stack count for USEs.
1576             BlockRenameVariables(block, pRenameState);
1577
1578             // Assign arguments to the phi node of successors, corresponding to the block's index.
1579             AssignPhiNodeRhsVariables(block, pRenameState);
1580
1581             // Recurse with the block's DOM children.
1582             BlkSet* pBlkSet;
1583             if (domTree->Lookup(block, &pBlkSet))
1584             {
1585                 for (BlkSet::KeyIterator child = pBlkSet->Begin(); !child.Equal(pBlkSet->End()); ++child)
1586                 {
1587                     DBG_SSA_JITDUMP("[SsaBuilder::RenameVariables](pushing dom child BB%02u)\n", child.Get()->bbNum);
1588                     blocksToDo->push_back(BlockWork(child.Get()));
1589                 }
1590             }
1591         }
1592         else
1593         {
1594             // Done, pop all the stack count, if there is one for this block.
1595             BlockPopStacks(block, pRenameState);
1596             DBG_SSA_JITDUMP("[SsaBuilder::RenameVariables] done with BB%02u\n", block->bbNum);
1597         }
1598     }
1599
1600     // Remember the number of Heap SSA names.
1601     m_pCompiler->lvHeapNumSsaNames = pRenameState->HeapCount();
1602 }
1603
1604 #ifdef DEBUG
1605 /**
1606  * Print the blocks, the phi nodes get printed as well.
1607  * @example:
1608  * After SSA BB02:
1609  *                [0027CC0C] -----------                 stmtExpr  void  (IL 0x019...0x01B)
1610  * N001 (  1,  1)       [0027CB70] -----------                 const     int    23
1611  * N003 (  3,  3)    [0027CBD8] -A------R--                 =         int
1612  * N002 (  1,  1)       [0027CBA4] D------N---                 lclVar    int    V01 arg1         d:5
1613  *
1614  * After SSA BB04:
1615  *                [0027D530] -----------                 stmtExpr  void  (IL   ???...  ???)
1616  * N002 (  0,  0)       [0027D4C8] -----------                 phi       int
1617  *                            [0027D8CC] -----------                 lclVar    int    V01 arg1         u:5
1618  *                            [0027D844] -----------                 lclVar    int    V01 arg1         u:4
1619  * N004 (  2,  2)    [0027D4FC] -A------R--                 =         int
1620  * N003 (  1,  1)       [0027D460] D------N---                 lclVar    int    V01 arg1         d:3
1621  */
1622 void SsaBuilder::Print(BasicBlock** postOrder, int count)
1623 {
1624     for (int i = count - 1; i >= 0; --i)
1625     {
1626         printf("After SSA BB%02u:\n", postOrder[i]->bbNum);
1627         m_pCompiler->gtDispTreeList(postOrder[i]->bbTreeList);
1628     }
1629 }
1630 #endif // DEBUG
1631
1632 /**
1633  * Build SSA form.
1634  *
1635  * Sorts the graph topologically.
1636  *   - Collects them in postOrder array.
1637  *
1638  * Identifies each block's immediate dominator.
1639  *   - Computes this in bbIDom of each BasicBlock.
1640  *
1641  * Computes DOM tree relation.
1642  *   - Computes domTree as block -> set of blocks.
1643  *   - Computes pre/post order traversal of the DOM tree.
1644  *
1645  * Inserts phi nodes.
1646  *   - Computes dominance frontier as block -> set of blocks.
1647  *   - Allocates block use/def/livein/liveout and computes it.
1648  *   - Inserts phi nodes with only rhs at the beginning of the blocks.
1649  *
1650  * Renames variables.
1651  *   - Walks blocks in evaluation order and gives uses and defs names.
1652  *   - Gives empty phi nodes their rhs arguments as they become known while renaming.
1653  *
1654  * @return true if successful, for now, this must always be true.
1655  *
1656  * @see "A simple, fast dominance algorithm" by Keith D. Cooper, Timothy J. Harvey, Ken Kennedy.
1657  * @see Briggs, Cooper, Harvey and Simpson "Practical Improvements to the Construction
1658  *      and Destruction of Static Single Assignment Form."
1659  */
1660 void SsaBuilder::Build()
1661 {
1662 #ifdef DEBUG
1663     if (m_pCompiler->verbose)
1664     {
1665         printf("*************** In SsaBuilder::Build()\n");
1666     }
1667 #endif
1668
1669     // Ensure that there's a first block outside a try, so that the dominator tree has a unique root.
1670     SetupBBRoot();
1671
1672     // Just to keep block no. & index same add 1.
1673     int blockCount = m_pCompiler->fgBBNumMax + 1;
1674
1675     JITDUMP("[SsaBuilder] Max block count is %d.\n", blockCount);
1676
1677     // Allocate the postOrder array for the graph.
1678
1679     BasicBlock** postOrder;
1680
1681     if (blockCount > DEFAULT_MIN_OPTS_BB_COUNT)
1682     {
1683         postOrder = new (m_pCompiler->getAllocator()) BasicBlock*[blockCount];
1684     }
1685     else
1686     {
1687         postOrder = (BasicBlock**)alloca(blockCount * sizeof(BasicBlock*));
1688     }
1689
1690     // Topologically sort the graph.
1691     int count = TopologicalSort(postOrder, blockCount);
1692     JITDUMP("[SsaBuilder] Topologically sorted the graph.\n");
1693     EndPhase(PHASE_BUILD_SSA_TOPOSORT);
1694
1695     // Compute IDom(b).
1696     ComputeImmediateDom(postOrder, count);
1697
1698     // Compute the dominator tree.
1699     BlkToBlkSetMap* domTree = new (m_pCompiler->getAllocator()) BlkToBlkSetMap(m_pCompiler->getAllocator());
1700     ComputeDominators(postOrder, count, domTree);
1701     EndPhase(PHASE_BUILD_SSA_DOMS);
1702
1703     // Insert phi functions.
1704     InsertPhiFunctions(postOrder, count);
1705
1706     // Rename local variables and collect UD information for each ssa var.
1707     SsaRenameState* pRenameState = new (jitstd::utility::allocate<SsaRenameState>(m_allocator), jitstd::placement_t())
1708         SsaRenameState(m_allocator, m_pCompiler->lvaCount);
1709     RenameVariables(domTree, pRenameState);
1710     EndPhase(PHASE_BUILD_SSA_RENAME);
1711
1712 #ifdef DEBUG
1713     // At this point we are in SSA form. Print the SSA form.
1714     if (m_pCompiler->verboseSsa)
1715     {
1716         Print(postOrder, count);
1717     }
1718 #endif
1719 }
1720
1721 void SsaBuilder::SetupBBRoot()
1722 {
1723     // Allocate a bbroot, if necessary.
1724     // We need a unique block to be the root of the dominator tree.
1725     // This can be violated if the first block is in a try, or if it is the first block of
1726     // a loop (which would necessarily be an infinite loop) -- i.e., it has a predecessor.
1727
1728     // If neither condition holds, no reason to make a new block.
1729     if (!m_pCompiler->fgFirstBB->hasTryIndex() && m_pCompiler->fgFirstBB->bbPreds == nullptr)
1730     {
1731         return;
1732     }
1733
1734     BasicBlock* bbRoot = m_pCompiler->bbNewBasicBlock(BBJ_NONE);
1735     bbRoot->bbFlags |= BBF_INTERNAL;
1736
1737     // May need to fix up preds list, so remember the old first block.
1738     BasicBlock* oldFirst = m_pCompiler->fgFirstBB;
1739
1740     // Copy the liveness information from the first basic block.
1741     if (m_pCompiler->fgLocalVarLivenessDone)
1742     {
1743         VarSetOps::Assign(m_pCompiler, bbRoot->bbLiveIn, oldFirst->bbLiveIn);
1744         VarSetOps::Assign(m_pCompiler, bbRoot->bbLiveOut, oldFirst->bbLiveIn);
1745     }
1746
1747     // Copy the bbWeight.  (This is technically wrong, if the first block is a loop head, but
1748     // it shouldn't matter...)
1749     bbRoot->inheritWeight(oldFirst);
1750
1751     // There's an artifical incoming reference count for the first BB.  We're about to make it no longer
1752     // the first BB, so decrement that.
1753     assert(oldFirst->bbRefs > 0);
1754     oldFirst->bbRefs--;
1755
1756     m_pCompiler->fgInsertBBbefore(m_pCompiler->fgFirstBB, bbRoot);
1757
1758     assert(m_pCompiler->fgFirstBB == bbRoot);
1759     if (m_pCompiler->fgComputePredsDone)
1760     {
1761         m_pCompiler->fgAddRefPred(oldFirst, bbRoot);
1762     }
1763 }
1764
1765 #ifdef DEBUG
1766 // This method asserts that SSA name constraints specified are satisfied.
1767 void Compiler::JitTestCheckSSA()
1768 {
1769     struct SSAName
1770     {
1771         unsigned m_lvNum;
1772         unsigned m_ssaNum;
1773
1774         static unsigned GetHashCode(SSAName ssaNm)
1775         {
1776             return ssaNm.m_lvNum << 16 | ssaNm.m_ssaNum;
1777         }
1778
1779         static bool Equals(SSAName ssaNm1, SSAName ssaNm2)
1780         {
1781             return ssaNm1.m_lvNum == ssaNm2.m_lvNum && ssaNm1.m_ssaNum == ssaNm2.m_ssaNum;
1782         }
1783     };
1784
1785     typedef SimplerHashTable<ssize_t, SmallPrimitiveKeyFuncs<ssize_t>, SSAName, JitSimplerHashBehavior>
1786         LabelToSSANameMap;
1787     typedef SimplerHashTable<SSAName, SSAName, ssize_t, JitSimplerHashBehavior> SSANameToLabelMap;
1788
1789     // If we have no test data, early out.
1790     if (m_nodeTestData == nullptr)
1791     {
1792         return;
1793     }
1794
1795     NodeToTestDataMap* testData = GetNodeTestData();
1796
1797     // First we have to know which nodes in the tree are reachable.
1798     NodeToIntMap* reachable = FindReachableNodesInNodeTestData();
1799
1800     LabelToSSANameMap* labelToSSA = new (getAllocatorDebugOnly()) LabelToSSANameMap(getAllocatorDebugOnly());
1801     SSANameToLabelMap* ssaToLabel = new (getAllocatorDebugOnly()) SSANameToLabelMap(getAllocatorDebugOnly());
1802
1803     if (verbose)
1804     {
1805         printf("\nJit Testing: SSA names.\n");
1806     }
1807     for (NodeToTestDataMap::KeyIterator ki = testData->Begin(); !ki.Equal(testData->End()); ++ki)
1808     {
1809         TestLabelAndNum tlAndN;
1810         GenTreePtr      node = ki.Get();
1811         bool            b    = testData->Lookup(node, &tlAndN);
1812         assert(b);
1813         if (tlAndN.m_tl == TL_SsaName)
1814         {
1815             if (node->OperGet() != GT_LCL_VAR)
1816             {
1817                 printf("SSAName constraint put on non-lcl-var expression ");
1818                 printTreeID(node);
1819                 printf(" (of type %s).\n", varTypeName(node->TypeGet()));
1820                 unreached();
1821             }
1822             GenTreeLclVarCommon* lcl = node->AsLclVarCommon();
1823
1824             int dummy;
1825             if (!reachable->Lookup(lcl, &dummy))
1826             {
1827                 printf("Node ");
1828                 printTreeID(lcl);
1829                 printf(" had a test constraint declared, but has become unreachable at the time the constraint is "
1830                        "tested.\n"
1831                        "(This is probably as a result of some optimization -- \n"
1832                        "you may need to modify the test case to defeat this opt.)\n");
1833                 unreached();
1834             }
1835
1836             if (verbose)
1837             {
1838                 printf("  Node: ");
1839                 printTreeID(lcl);
1840                 printf(", SSA name = <%d, %d> -- SSA name class %d.\n", lcl->gtLclNum, lcl->gtSsaNum, tlAndN.m_num);
1841             }
1842             SSAName ssaNm;
1843             if (labelToSSA->Lookup(tlAndN.m_num, &ssaNm))
1844             {
1845                 if (verbose)
1846                 {
1847                     printf("      Already in hash tables.\n");
1848                 }
1849                 // The mapping(s) must be one-to-one: if the label has a mapping, then the ssaNm must, as well.
1850                 ssize_t num2;
1851                 bool    b = ssaToLabel->Lookup(ssaNm, &num2);
1852                 // And the mappings must be the same.
1853                 if (tlAndN.m_num != num2)
1854                 {
1855                     printf("Node: ");
1856                     printTreeID(lcl);
1857                     printf(", SSA name = <%d, %d> was declared in SSA name class %d,\n", lcl->gtLclNum, lcl->gtSsaNum,
1858                            tlAndN.m_num);
1859                     printf(
1860                         "but this SSA name <%d,%d> has already been associated with a different SSA name class: %d.\n",
1861                         ssaNm.m_lvNum, ssaNm.m_ssaNum, num2);
1862                     unreached();
1863                 }
1864                 // And the current node must be of the specified SSA family.
1865                 if (!(lcl->gtLclNum == ssaNm.m_lvNum && lcl->gtSsaNum == ssaNm.m_ssaNum))
1866                 {
1867                     printf("Node: ");
1868                     printTreeID(lcl);
1869                     printf(", SSA name = <%d, %d> was declared in SSA name class %d,\n", lcl->gtLclNum, lcl->gtSsaNum,
1870                            tlAndN.m_num);
1871                     printf("but that name class was previously bound to a different SSA name: <%d,%d>.\n",
1872                            ssaNm.m_lvNum, ssaNm.m_ssaNum);
1873                     unreached();
1874                 }
1875             }
1876             else
1877             {
1878                 ssaNm.m_lvNum  = lcl->gtLclNum;
1879                 ssaNm.m_ssaNum = lcl->gtSsaNum;
1880                 ssize_t num;
1881                 // The mapping(s) must be one-to-one: if the label has no mapping, then the ssaNm may not, either.
1882                 if (ssaToLabel->Lookup(ssaNm, &num))
1883                 {
1884                     printf("Node: ");
1885                     printTreeID(lcl);
1886                     printf(", SSA name = <%d, %d> was declared in SSA name class %d,\n", lcl->gtLclNum, lcl->gtSsaNum,
1887                            tlAndN.m_num);
1888                     printf("but this SSA name has already been associated with a different name class: %d.\n", num);
1889                     unreached();
1890                 }
1891                 // Add to both mappings.
1892                 labelToSSA->Set(tlAndN.m_num, ssaNm);
1893                 ssaToLabel->Set(ssaNm, tlAndN.m_num);
1894                 if (verbose)
1895                 {
1896                     printf("      added to hash tables.\n");
1897                 }
1898             }
1899         }
1900     }
1901 }
1902 #endif // DEBUG