File: LiveVars.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (790 lines) | stat: -rw-r--r-- 26,484 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

/*========================== begin_copyright_notice ============================

This file is distributed under the University of Illinois Open Source License.
See LICENSE.TXT for details.

============================= end_copyright_notice ===========================*/

//===------------ LiveVars.cpp - Live Variable Analysis ---------*- C++ -*-===//
//
// Intel extension to LLVM core
//
//===----------------------------------------------------------------------===//
//
// This file implements the LiveVariables analysis pass. It originates from
// the same function in llvm3.0/codegen, however works on llvm-ir instead of
// the code-gen-level ir.
//
// This class computes live variables using a sparse implementation based on
// the llvm-ir SSA form.  This class uses the dominance properties of SSA form
// to efficiently compute live variables for virtual registers. Also Note that
// there is no physical register at llvm-ir level.
//
//===----------------------------------------------------------------------===//

#include "Compiler/CISACodeGen/LiveVars.hpp"
#include "Compiler/IGCPassSupport.h"
#include "common/debug/Debug.hpp"
#include "common/LLVMWarningsPush.hpp"
#include "llvmWrapper/IR/CFG.h"
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Debug.h>
#include <llvm/ADT/DepthFirstIterator.h>
#include <llvm/ADT/STLExtras.h>
#include "common/LLVMWarningsPop.hpp"
#include <algorithm>
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;
using namespace IGC::Debug;

Instruction*
LiveVars::LVInfo::findKill(const BasicBlock* MBB) const {
    for (unsigned i = 0, e = Kills.size(); i != e; ++i)
        if (Kills[i]->getParent() == MBB)
            return Kills[i];
    return NULL;
}

void LiveVars::LVInfo::print(raw_ostream& OS) const {
    OS << "  Alive in blocks: ";
    for (auto I = AliveBlocks.begin(), E = AliveBlocks.end(); I != E; ++I) {
        (*I)->print(OS);
        OS << ", ";
    }
    OS << "\n  Killed by:";
    if (Kills.empty())
        OS << " No instructions.\n";
    else {
        for (unsigned i = 0, e = Kills.size(); i != e; ++i) {
            OS << "\n    " << ": " << *Kills[i];
        }
        OS << "\n";
    }
}

void LiveVars::print(raw_ostream& OS, const Module*) const {
    for (DenseMap<Value*, LiveVars::LVInfo*>::const_iterator I = VirtRegInfo.begin(),
        E = VirtRegInfo.end(); I != E; ++I) {
        OS << "\n{";
        I->first->print(OS);
        I->second->print(OS);
        OS << "}";
    }
}

/// Destructor needs to release the allocated memory
LiveVars::~LiveVars()
{
    releaseMemory();
}

void LiveVars::releaseMemory() {
    // Free the LVInfo table
    VirtRegInfo.clear();
    PHIVarInfo.clear();
    DistanceMap.clear();
}

void LiveVars::preAllocMemory(Function& F)
{
    // Pre-allocate enough memory to avoid many allocations
    // Use the number of values (arg_size + the number of insts)
    // as the amount for pre-allocation. This size is super set
    // for both VirtRegInfo and DistanceMap. (This amount is about
    // 3%-5% more than the real size of VirtRegInfo.)
    size_t nVals = F.arg_size();
    for (auto& BB : F) {
        nVals += BB.size();
    }

    // Allocate a bit more than we need in case it needs grow.
    // Note that as DenseMap will automatically allocate when
    // the map is 3/4 full, so we take this into account.
    uint32_t mapCap1 = int_cast<uint32_t>((size_t)(nVals * 1.40f));
    // For PHIVarInfo, increase 10% only.
    uint32_t mapCap2 = int_cast<uint32_t>((size_t)(F.size() * 1.10f));
    DistanceMap.grow(mapCap1);
    VirtRegInfo.grow(mapCap1);
    PHIVarInfo.grow(mapCap2);
}

void LiveVars::dump() const {
    print(ods());
}

#if  defined( _DEBUG )
void LiveVars::dump(Function* F)
{
    if (F->size() == 0)
    {
        return;
    }

    if (BBIds.size() == 0)
    {
        setupBBIds(F);
    }

    raw_ostream& OS = errs();
    for (DenseMap<Value*, LiveVars::LVInfo*>::const_iterator I = VirtRegInfo.begin(),
        E = VirtRegInfo.end(); I != E; ++I) {
        Value* V = I->first;
        LVInfo* LVI = I->second;
        OS << "\n{";
        V->print(OS);
        OS << "\n  Alive in blocks: ";
        for (auto I = LVI->AliveBlocks.begin(), E = LVI->AliveBlocks.end();
            I != E; ++I) {
            BasicBlock* BB = *I;
            if (BBIds.count(BB) > 0)
            {
                int id = BBIds[BB];
                OS << id << ", ";
            }
            else
            {
                OS << "<unknown>, ";
            }
        }
        OS << "\n  Killed by:";
        if (LVI->Kills.empty())
            OS << " No instructions.\n";
        else {
            for (unsigned i = 0, e = LVI->Kills.size(); i != e; ++i) {
                OS << "\n    " << ": " << *LVI->Kills[i];
            }
            OS << "\n";
        }
        OS << "}";
    }
}

void LiveVars::dump(int BBId)
{
    BasicBlock* BB = IdToBBs[BBId];
    if (BB)
    {
        BB->print(ods());
    }
}

void LiveVars::setupBBIds(Function* F)
{
    if (F)
    {
        int ix = 0;
        for (Function::iterator I = F->begin(), E = F->end();
            I != E; ++I)
        {
            BasicBlock* BB = &*I;
            BBIds.insert(std::make_pair(BB, ix));
            IdToBBs.insert(std::make_pair(ix, BB));
            ++ix;
        }
    }
}
#endif

/// getLVInfo - Get (possibly creating) a LVInfo object for the given vreg.
LiveVars::LVInfo& LiveVars::getLVInfo(Value* LV) {
    IGC_ASSERT(isa<Instruction>(LV) || isa<Argument>(LV));
    DenseMap<Value*, LiveVars::LVInfo*>::const_iterator it = VirtRegInfo.find(LV);
    if (it == VirtRegInfo.end()) {
        LiveVars::LVInfo* lvInfo = new (Allocator.Allocate()) LiveVars::LVInfo();
        lvInfo->uniform = (WIA && WIA->isUniform(LV));
        VirtRegInfo.insert(std::pair<Value*, LVInfo*>(LV, lvInfo));
        if (Instruction * inst = dyn_cast<Instruction>(LV)) {
            // if the value is an instruction, default it to dead
            lvInfo->Kills.push_back(inst);
        }
        return *(lvInfo);
    }
    return *(it->second);
}

void LiveVars::MarkVirtRegAliveInBlock(LiveVars::LVInfo& VRInfo,
    BasicBlock* DefBlock,
    BasicBlock* MBB,
    std::vector<BasicBlock*>& WorkList) {

    if (VRInfo.AliveBlocks.count(MBB))
        return;  // We already know the block is live

    // Check to see if this basic block is one of the killing blocks.  If so,
    // remove it.
    for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
        if (VRInfo.Kills[i]->getParent() == MBB) {
            VRInfo.Kills.erase(VRInfo.Kills.begin() + i);  // Erase entry
            break;
        }

    if (MBB == &(MF->getEntryBlock()) && DefBlock != nullptr)
        return;  // Only Arguments can be live-through entry block
    if (MBB == DefBlock)
        return;  // Terminate recursion

      // Mark the variable known alive in this bb
    VRInfo.AliveBlocks.insert(MBB);

    // Skip the simdPred if WIA is not available
    if (!WIA)
    {
        return;
    }

    bool hasNonUniformBranch = false;
    bool hasLayoutPred = true;
    for (pred_iterator PI = pred_begin(MBB), E = pred_end(MBB);
        PI != E; ++PI) {
        BasicBlock* PredBlk = *PI;
        //Before pushing check if the predecessor has already been marked
        if (VRInfo.AliveBlocks.count(PredBlk))
            continue;  // We already know the block is live

        WorkList.push_back(PredBlk);
        // Check if we need to update liveness for uniform variable
        // inside divergent control-flow
        if (PredBlk == MBB->getPrevNode())
            hasLayoutPred = false;
        if (hasLayoutPred && VRInfo.uniform) {
            Instruction* cbr = PredBlk->getTerminator();
            if (cbr && !WIA->isUniform(cbr))
                hasNonUniformBranch = true;
        }
    }
    if (hasLayoutPred && hasNonUniformBranch && VRInfo.uniform) {
        BasicBlock* simdPred = MBB->getPrevNode();
        while (simdPred && simdPred != DefBlock) {
            //check if it is marked live
            if (!VRInfo.AliveBlocks.count(simdPred))
                WorkList.push_back(simdPred);
            simdPred = simdPred->getPrevNode();
        }
    }
}

void LiveVars::MarkVirtRegAliveInBlock(LiveVars::LVInfo& VRInfo,
    BasicBlock* DefBlock,
    BasicBlock* MBB) {
    std::vector<BasicBlock*> WorkList;
    MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
    while (!WorkList.empty()) {
        BasicBlock* Pred = WorkList.back();
        WorkList.pop_back();
        MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
    }
}

void LiveVars::HandleVirtRegUse(Value* VL, BasicBlock* MBB,
    Instruction* MI, bool ScanAllKills,
    bool ScanBBTopDown)
{
    LiveVars::LVInfo& VRInfo = getLVInfo(VL);
    VRInfo.NumUses++;

    // Check to see if this basic block is already a kill block.
    // - When we establish live-info for the first time, the uses are scanned in bottom-up fashion
    //   for every basic block, the kill for the current block is appended to
    //   the end of the kill-list, therefore we only need to check the last kill on the kill-list.
    // - When we update the live-info later on in top-down fashion, for example, when coalescing InsertElements
    //   in DeSSA, the existing kill for this block may be anywhere on the list, then we need to
    //   scan all the kills in order to replace the right one.
    if (ScanAllKills) {
        for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i) {
            if (VRInfo.Kills[i]->getParent() == MBB) {
                Instruction* killInst = VRInfo.Kills[i];
                IGC_ASSERT_MESSAGE(DistanceMap.count(killInst), "DistanceMap not set up yet.");
                IGC_ASSERT_MESSAGE(DistanceMap.count(MI), "DistanceMap not set up yet.");
                if (DistanceMap[killInst] < DistanceMap[MI]) {
                    VRInfo.Kills[i] = MI;
                }
                return;
            }
        }
    }
    else {
        if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
            if (ScanBBTopDown)
            {
                VRInfo.Kills.back() = MI;
            }
            else
            {
                // Initially the kill is the def itself.
                // replace it with the kill, otherwise, just ignore the new use because it is not the last.
                if (VRInfo.Kills.back() == VL) {
                    VRInfo.Kills.back() = MI;
                }
            }
            return;
        }

        for(size_t i = 0, e = VRInfo.Kills.size(); i < e; ++i)
        {
            IGC_ASSERT(nullptr != VRInfo.Kills[i]);
            IGC_ASSERT_MESSAGE((VRInfo.Kills[i]->getParent() != MBB), "entry should be at end!");
        }
    }

    // This situation can occur:
    //
    //     ,------.
    //     |      |
    //     |      v
    //     |   t2 = phi ... t1 ...
    //     |      |
    //     |      v
    //     |   t1 = ...
    //     |  ... = ... t1 ...
    //     |      |
    //     `------'
    //
    // where there is a use in a PHI node that's a predecessor to the defining
    // block. We don't want to mark all predecessors as having the value "alive"
    // in this case.
    if (isa<Instruction>(VL)) {
        if (MBB == cast<Instruction>(VL)->getParent())
            return;
    }
    // Add a new kill entry for this basic block. If this virtual register is
    // already marked as alive in this basic block, that means it is alive in at
    // least one of the successor blocks, it's not a kill.
    if (!VRInfo.AliveBlocks.count(MBB))
        VRInfo.Kills.push_back(MI);

    if (MBB == &(MF->getEntryBlock()))
        return;

    if (!WIA)
        return;

    // Update all dominating blocks to mark them as "known live".
    bool hasNonUniformBranch = false;
    bool hasLayoutPred = true;
    for (pred_iterator PI = pred_begin(MBB), E = pred_end(MBB);
        PI != E; ++PI) {
        BasicBlock* DefBlk = (isa<Instruction>(VL)) ?
            cast<Instruction>(VL)->getParent() : NULL;
        BasicBlock* PredBlk = *PI;
        MarkVirtRegAliveInBlock(VRInfo, DefBlk, PredBlk);
        Instruction* cbr = PredBlk->getTerminator();
        if (cbr && !WIA->isUniform(cbr))
            hasNonUniformBranch = true;
        if (PredBlk == MBB->getPrevNode())
            hasLayoutPred = false;
    }
    if (hasLayoutPred && hasNonUniformBranch && WIA->isUniform(VL)) {
        BasicBlock* simdPred = MBB->getPrevNode();
        BasicBlock* DefBlk = (isa<Instruction>(VL)) ?
            cast<Instruction>(VL)->getParent() : NULL;
        while (simdPred && simdPred != DefBlk) {
            MarkVirtRegAliveInBlock(VRInfo, DefBlk, simdPred);
            simdPred = simdPred->getPrevNode();
        }
    }
}

void LiveVars::HandleVirtRegDef(Instruction* MI)
{
    LiveVars::LVInfo& VRInfo = getLVInfo(MI);
    if (VRInfo.AliveBlocks.empty())
        // If vr is not alive in any block, then defaults to dead.
        VRInfo.Kills.push_back(MI);
}

void LiveVars::initDistance(Function& F)
{
    DistanceMap.clear();

    for (auto& BB : F)
    {
        unsigned Dist = 0;
        for (auto& II : BB) {
            Instruction* MI = &II;
            DistanceMap.insert(std::make_pair(MI, Dist++));
        }
    }
}

void LiveVars::ComputeLiveness(Function* mf, WIAnalysis* wia)
{
    releaseMemory();
    MF = mf;
    WIA = wia;

    preAllocMemory(*MF);

    // First, set up DistanceMap
    // save distance map
    initDistance(*MF);

    analyzePHINodes(*mf);
    BasicBlock* Entry = &(*MF->begin());
    typedef df_iterator_default_set<BasicBlock*, 16> VisitedTy;
    VisitedTy Visited;

    for (df_ext_iterator<BasicBlock*, VisitedTy>
        DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
        DFI != E; ++DFI) {
        BasicBlock* MBB = *DFI;
        // Handle any virtual assignments from PHI nodes which might be at the
        // bottom of this basic block.  We check all of our successor blocks to see
        // if they have PHI nodes, and if so, we simulate an assignment at the end
        // of the current block.
        auto it = PHIVarInfo.find(MBB);
        if (it != PHIVarInfo.end()) {
            SmallVector<Value*, 4> & VarInfoVec = it->second;

            for (SmallVector<Value*, 4>::iterator I = VarInfoVec.begin(),
                E = VarInfoVec.end(); I != E; ++I) {
                // Mark it alive only in the block we are representing.
                Value* DefV = *I;
                BasicBlock* DefBlk = (isa<Instruction>(DefV)) ?
                    cast<Instruction>(DefV)->getParent() : NULL;
                MarkVirtRegAliveInBlock(getLVInfo(DefV), DefBlk, MBB);
            }
        }
    }
}

/// analyzePHINodes - Gather information about the PHI nodes in here. In
/// particular, we want to map the variable information of a virtual register
/// which is used in a PHI node. We map that to the BB the vreg is coming from.
///
void LiveVars::analyzePHINodes(const Function& Fn) {
    for (Function::const_iterator I = Fn.begin(), E = Fn.end();
        I != E; ++I) {
        for (BasicBlock::const_iterator BBI = I->begin(), BBE = I->end();
            BBI != BBE; ++BBI) {
            const PHINode* phi = dyn_cast<PHINode>(BBI);
            if (!phi)
                break;
            for (unsigned i = 0, e = phi->getNumOperands(); i != e; ++i) {
                BasicBlock* PBB = phi->getIncomingBlock(i);
                Value* VL = phi->getOperand(i);
                if (isa<Instruction>(VL) || isa<Argument>(VL)) {
                    SmallVector<Value*, 4> & VV = PHIVarInfo[PBB];
                    VV.push_back(phi->getOperand(i));
                }
            }
        }
    }
}

bool LiveVars::LVInfo::isLiveIn(const BasicBlock& MBB, Value* VL) {

    // Reg is live-through.
    if (AliveBlocks.count((BasicBlock*)& MBB))
        return true;

    // Registers defined in MBB cannot be live in.
    const Instruction* Def = dyn_cast<Instruction>(VL);
    if (Def && Def->getParent() == &MBB)
        return false;

    // Reg was not defined in MBB, was it killed here?
    if (findKill(&MBB))
        return true;
    return false;
}

bool LiveVars::isLiveAt(Value* VL, Instruction* MI) {
    BasicBlock* MBB = MI->getParent();
    LVInfo& info = getLVInfo(VL);
    // Reg is live-through.
    if (info.AliveBlocks.count(MBB))
        return true;

    // Registers defined in MBB cannot be live in.
    const Instruction* Def = dyn_cast<Instruction>(VL);
    if (Def && Def->getParent() == MBB) {
        if (getDistance(Def) > getDistance(MI)) {
            return false;
        }
        else if (info.AliveBlocks.empty() && info.Kills.empty()) {
            // handle that special case: def is the current block
            // and phi in the immed-successor block is the last use
            return true;
        }
    }

    // Reg was not defined in MBB, was it killed here?
    Instruction* kill = info.findKill(MBB);
    if (kill) {
        return getDistance(kill) > getDistance(MI);
    }

    if (isLiveOut(VL, *MBB)) {
        return true;
    }

    return false;
}

bool LiveVars::isLiveOut(Value* VL, const BasicBlock& MBB) {
    LiveVars::LVInfo& VI = getLVInfo(VL);

    if (isa<Instruction>(VL) && VI.AliveBlocks.empty())
    {
        // a local value does not live out of any BB.
        // (Originally, this function might be for checking non-local
        //  value. Adding this code to make it work for any value.)
        Instruction* I = cast<Instruction>(VL);
        if (VI.Kills.size() == 1) {
            BasicBlock* killBB = VI.Kills[0]->getParent();
            if (killBB == I->getParent()) {
                return false;
            }
        }
    }

    // Loop over all of the successors of the basic block, checking to see if
    // the value is either live in the block, or if it is killed in the block.
    SmallVector<const BasicBlock*, 8> OpSuccBlocks;
    for (IGCLLVM::const_succ_iterator SI = succ_begin(&MBB), E = succ_end(&MBB); SI != E; ++SI) {
        const BasicBlock* SuccMBB = *SI;
        // Is it alive in this successor?
        if (VI.AliveBlocks.count((BasicBlock*)SuccMBB))
            return true;
        OpSuccBlocks.push_back(SuccMBB);
    }

    // Check to see if this value is live because there is a use in a successor
    // that kills it.
    switch (OpSuccBlocks.size()) {
    case 1: {
        const BasicBlock* SuccMBB = OpSuccBlocks[0];
        for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
            if (VI.Kills[i]->getParent() == SuccMBB)
                return true;
        break;
    }
    case 2: {
        const BasicBlock* SuccMBB1 = OpSuccBlocks[0], * SuccMBB2 = OpSuccBlocks[1];
        for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
            if (VI.Kills[i]->getParent() == SuccMBB1 ||
                VI.Kills[i]->getParent() == SuccMBB2)
                return true;
        break;
    }
    default:
        std::sort(OpSuccBlocks.begin(), OpSuccBlocks.end());
        for (unsigned i = 0, e = VI.Kills.size(); i != e; ++i)
            if (std::binary_search(OpSuccBlocks.begin(), OpSuccBlocks.end(),
                VI.Kills[i]->getParent()))
                return true;
    }
    return false;
}

void LiveVars::Calculate(Function* mf, WIAnalysis* wia)
{
    releaseMemory();
    MF = mf;
    WIA = wia;

    preAllocMemory(*MF);

    initDistance(*MF);

    analyzePHINodes(*mf);

    BasicBlock* Entry = &(*MF->begin());
    typedef df_iterator_default_set<BasicBlock*, 16> VisitedTy;
    VisitedTy Visited;
    for (df_ext_iterator<BasicBlock*, VisitedTy>
        DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
        DFI != DFE; ++DFI)
    {
        BasicBlock* MBB = *DFI;

        // Loop over all of the instructions, processing them.
        for (BasicBlock::iterator I = MBB->begin(), E = MBB->end();
            I != E; ++I)
        {
            Instruction* MI = &(*I);

            // Unless it is a PHI node.  In this case, ONLY process the DEF, not any
            // of the uses.  They will be handled in other basic blocks.
            if (!isa<PHINode>(MI)) {
                // Process all uses.
                for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
                    Value* V = MI->getOperand(i);
                    if (isa<Instruction>(V) || isa<Argument>(V))
                    {
                        HandleVirtRegUse(V, MBB, MI, false, true);
                    }
                }
            }

            // Since we will handle a value only if it is used, no need to explicitly
            // handle def here. And getLVInfo() will handle the def when handling its
            // first use.  So as a result commenting the following out, no LV is created
            // for a dead instruction, which makes sense. Note that this is different
            // from the llvm core, as llvm core getVarInfo is different from getLVInfo()!
#if 0
            // Process all defs.
            if (!MI->getType()->isVoidTy())
            {
                HandleVirtRegDef(MI);
            }
#endif
        }

        // Handle any virtual assignments from PHI nodes which might be at the
        // bottom of this basic block.  We check all of our successor blocks to see
        // if they have PHI nodes, and if so, we simulate an assignment at the end
        // of the current block.
        auto it = PHIVarInfo.find(MBB);
        if (it != PHIVarInfo.end()) {
            SmallVector<Value*, 4> & VarInfoVec = it->second;

            for (SmallVector<Value*, 4>::iterator I = VarInfoVec.begin(),
                E = VarInfoVec.end(); I != E; ++I) {
                // Mark it alive only in the block we are representing.
                Value* DefV = *I;
                BasicBlock* DefBlk = (isa<Instruction>(DefV)) ?
                    cast<Instruction>(DefV)->getParent() : NULL;
                MarkVirtRegAliveInBlock(getLVInfo(DefV), DefBlk, MBB);
#if VECTOR_COALESCING == 0
                // special treatment for ExtractElement
                Instruction * EEI = dyn_cast<ExtractElementInst>(DefV);
                if (EEI) {
                    DefV = EEI->getOperand(0);
                    if (isa<Instruction>(DefV) || isa<Argument>(DefV))
                    {
                        DefBlk = (isa<Instruction>(DefV)) ?
                            cast<Instruction>(DefV)->getParent() : NULL;
                        MarkVirtRegAliveInBlock(getLVInfo(DefV), DefBlk, MBB);
                    }
                }
#endif
            }
        }
    }
}

bool LiveVars::hasInterference(llvm::Value* V0, llvm::Value* V1)
{
    // Skip Constant
    if (isa<Constant>(V0) || isa<Constant>(V1)) {
        return false;
    }

    Instruction* I0 = dyn_cast<Instruction>(V0);
    Instruction* I1 = dyn_cast<Instruction>(V1);
    if (!I0 && !I1) {
        return true;
    }

    if (!I0) {
        // V0 must be argument. Use the first inst in Entry
        I0 = MF->getEntryBlock().getFirstNonPHIOrDbg();
    }
    if (!I1) {
        // V1 must be argument. Use the first inst in Entry
        I1 = MF->getEntryBlock().getFirstNonPHIOrDbg();
    }

    if (isLiveAt(V0, I1) || isLiveAt(V1, I0)) {
        return true;
    }

    return false;
}

// Merge LVInfo for "fromV" into V's.
//
// This function is used after LVInfo has been constructed already and
// we want to merge "fromV" into V. This function will have V's LVInfo
// updated to reflect such a merging.
void LiveVars::mergeUseFrom(Value* V, Value* fromV)
{
    IGC_ASSERT_MESSAGE(VirtRegInfo.count(V), "MergeUseFrom should be used after LVInfo has been contructed!");
    IGC_ASSERT_MESSAGE(VirtRegInfo.count(fromV), "MergeUseFrom should be used after LVInfo has been contructed!");

    LVInfo& LVI = getLVInfo(V);
    LVInfo& fromLVI = getLVInfo(fromV);
    uint32_t newNumUses = LVI.NumUses + fromLVI.NumUses;

    IGC_ASSERT_MESSAGE(LVI.uniform == fromLVI.uniform, "ICE: cannot merge uniform with non-uniform values!");

    // Use V's defining BB, not fromV's
    Instruction* defInst = dyn_cast<Instruction>(V);
    BasicBlock* defBB = defInst ? defInst->getParent() : nullptr;

    // For each AliveBlock of fromV, add it to V's
    for (auto I : fromLVI.AliveBlocks) {
        BasicBlock* BB = I;
        MarkVirtRegAliveInBlock(LVI, defBB, BB);
    }

    // For each kill, add it into V's LVInfo
    for (auto KI : fromLVI.Kills) {
        Instruction* inst = KI;
        BasicBlock* instBB = inst->getParent();

        // Must set ScanAllKills as we are doing updating.
        HandleVirtRegUse(V, instBB, inst, true);
    }

    // Special case. fromV is used in the phi.
    //
    //    If a value is defined in BB and its last use is in
    //    a PHI in succ(BB), both its AliveBlocks and Kills
    //    are empty (see LiveVars.hpp)
    // For example:
    //       BB:
    //           fromV = bitcast V
    //       succ_BB:
    //           phi = fromV
    //
    // In this case, we will need to make sure BB is marked as alive.
    if (Instruction * I = dyn_cast<Instruction>(fromV))
    {
        for (User* user : I->users())
        {
            if (dyn_cast<PHINode>(user))
            {
                BasicBlock* BB = I->getParent();
                MarkVirtRegAliveInBlock(LVI, defBB, BB);
                break;
            }
        }
    }

    LVI.NumUses = newNumUses;
}


IGC_INITIALIZE_PASS_BEGIN(LiveVarsAnalysis, "LiveVarsAnalysis", "LiveVarsAnalysis", false, true)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(LiveVarsAnalysis, "LiveVarsAnalysis", "LiveVarsAnalysis", false, true)

char LiveVarsAnalysis::ID = 0;

LiveVarsAnalysis::LiveVarsAnalysis() : FunctionPass(ID) {
    initializeLiveVarsAnalysisPass(*PassRegistry::getPassRegistry());
}

bool LiveVarsAnalysis::runOnFunction(Function& F) {
    // WIAnalysis skips functions that are not recorded.
    auto pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
    if (pMdUtils->findFunctionsInfoItem(&F) == pMdUtils->end_FunctionsInfo()) {
        return false;
    }

    auto WIA = getAnalysisIfAvailable<WIAnalysis>();
    LV.ComputeLiveness(&F, WIA);
    return false;
}