LLVM  3.7.0
PHIEliminationUtils.cpp
Go to the documentation of this file.
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"
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.
23  unsigned SrcReg) {
24  // Handle the trivial case trivially.
25  if (MBB->empty())
26  return MBB->begin();
27 
28  // Usually, we just want to insert the copy before the first terminator
29  // instruction. However, for the edge going to a landing pad, we must insert
30  // the copy before the call/invoke instruction.
31  if (!SuccMBB->isLandingPad())
32  return MBB->getFirstTerminator();
33 
34  // Discover any defs/uses in this basic block.
35  SmallPtrSet<MachineInstr*, 8> DefUsesInMBB;
36  MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
37  for (MachineInstr &RI : MRI.reg_instructions(SrcReg)) {
38  if (RI.getParent() == MBB)
39  DefUsesInMBB.insert(&RI);
40  }
41 
42  MachineBasicBlock::iterator InsertPoint;
43  if (DefUsesInMBB.empty()) {
44  // No defs. Insert the copy at the start of the basic block.
45  InsertPoint = MBB->begin();
46  } else if (DefUsesInMBB.size() == 1) {
47  // Insert the copy immediately after the def/use.
48  InsertPoint = *DefUsesInMBB.begin();
49  ++InsertPoint;
50  } else {
51  // Insert the copy immediately after the last def/use.
52  InsertPoint = MBB->end();
53  while (!DefUsesInMBB.count(&*--InsertPoint)) {}
54  ++InsertPoint;
55  }
56 
57  // Make sure the copy goes after any phi nodes however.
58  return MBB->SkipPHIsAndLabels(InsertPoint);
59 }
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallPtrSet.h:78
iterator getFirstTerminator()
getFirstTerminator - returns an iterator to the first terminator instruction of this basic block...
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:276
MachineBasicBlock::iterator findPHICopyInsertPoint(MachineBasicBlock *MBB, MachineBasicBlock *SuccMBB, unsigned SrcReg)
findPHICopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg when following the CFG...
bundle_iterator< MachineInstr, instr_iterator > iterator
iterator SkipPHIsAndLabels(iterator I)
SkipPHIsAndLabels - Return the first instruction in MBB after I that is not a PHI or a label...
iterator_range< reg_instr_iterator > reg_instructions(unsigned Reg) const
size_type size() const
Definition: SmallPtrSet.h:79
bundle_iterator - MachineBasicBlock iterator that automatically skips over MIs that are inside bundle...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
iterator begin() const
Definition: SmallPtrSet.h:286
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:51
bool isLandingPad() const
isLandingPad - Returns true if the block is a landing pad.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.