/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/CodeGen/PHIEliminationUtils.cpp
Line | Count | Source |
1 | | //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | |
10 | | #include "PHIEliminationUtils.h" |
11 | | #include "llvm/ADT/SmallPtrSet.h" |
12 | | #include "llvm/CodeGen/MachineBasicBlock.h" |
13 | | #include "llvm/CodeGen/MachineFunction.h" |
14 | | #include "llvm/CodeGen/MachineRegisterInfo.h" |
15 | | using namespace llvm; |
16 | | |
17 | | // findCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg |
18 | | // when following the CFG edge to SuccMBB. This needs to be after any def of |
19 | | // SrcReg, but before any subsequent point where control flow might jump out of |
20 | | // the basic block. |
21 | | MachineBasicBlock::iterator |
22 | | llvm::findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB, |
23 | 2.31M | unsigned SrcReg) { |
24 | 2.31M | // Handle the trivial case trivially. |
25 | 2.31M | if (MBB->empty()) |
26 | 31.1k | return MBB->begin(); |
27 | 2.28M | |
28 | 2.28M | // Usually, we just want to insert the copy before the first terminator |
29 | 2.28M | // instruction. However, for the edge going to a landing pad, we must insert |
30 | 2.28M | // the copy before the call/invoke instruction. |
31 | 2.28M | if (2.28M !SuccMBB->isEHPad()2.28M ) |
32 | 2.28M | return MBB->getFirstTerminator(); |
33 | 733 | |
34 | 733 | // Discover any defs/uses in this basic block. |
35 | 733 | SmallPtrSet<MachineInstr*, 8> DefUsesInMBB; |
36 | 733 | MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo(); |
37 | 4.35k | for (MachineInstr &RI : MRI.reg_instructions(SrcReg)) { |
38 | 4.35k | if (RI.getParent() == MBB) |
39 | 494 | DefUsesInMBB.insert(&RI); |
40 | 4.35k | } |
41 | 733 | |
42 | 733 | MachineBasicBlock::iterator InsertPoint; |
43 | 733 | if (DefUsesInMBB.empty()733 ) { |
44 | 315 | // No defs. Insert the copy at the start of the basic block. |
45 | 315 | InsertPoint = MBB->begin(); |
46 | 733 | } else if (418 DefUsesInMBB.size() == 1418 ) { |
47 | 368 | // Insert the copy immediately after the def/use. |
48 | 368 | InsertPoint = *DefUsesInMBB.begin(); |
49 | 368 | ++InsertPoint; |
50 | 418 | } else { |
51 | 50 | // Insert the copy immediately after the last def/use. |
52 | 50 | InsertPoint = MBB->end(); |
53 | 458 | while (!DefUsesInMBB.count(&*--InsertPoint)458 ) {}408 |
54 | 418 | ++InsertPoint; |
55 | 418 | } |
56 | 2.31M | |
57 | 2.31M | // Make sure the copy goes after any phi nodes but before |
58 | 2.31M | // any debug nodes. |
59 | 2.31M | return MBB->SkipPHIsAndLabels(InsertPoint); |
60 | 2.31M | } |