LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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;
37  for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(SrcReg),
38  RE = MRI.reg_end(); RI != RE; ++RI) {
39  MachineInstr* DefUseMI = &*RI;
40  if (DefUseMI->getParent() == MBB)
41  DefUsesInMBB.insert(DefUseMI);
42  }
43 
44  MachineBasicBlock::iterator InsertPoint;
45  if (DefUsesInMBB.empty()) {
46  // No defs. Insert the copy at the start of the basic block.
47  InsertPoint = MBB->begin();
48  } else if (DefUsesInMBB.size() == 1) {
49  // Insert the copy immediately after the def/use.
50  InsertPoint = *DefUsesInMBB.begin();
51  ++InsertPoint;
52  } else {
53  // Insert the copy immediately after the last def/use.
54  InsertPoint = MBB->end();
55  while (!DefUsesInMBB.count(&*--InsertPoint)) {}
56  ++InsertPoint;
57  }
58 
59  // Make sure the copy goes after any phi nodes however.
60  return MBB->SkipPHIsAndLabels(InsertPoint);
61 }
const MachineFunction * getParent() const
MachineBasicBlock::iterator findPHICopyInsertPoint(MachineBasicBlock *MBB, MachineBasicBlock *SuccMBB, unsigned SrcReg)
bool insert(PtrType Ptr)
Definition: SmallPtrSet.h:253
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
Definition: SmallPtrSet.h:264
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:119
bundle_iterator< MachineInstr, instr_iterator > iterator
iterator SkipPHIsAndLabels(iterator I)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallPtrSet.h:74
unsigned size() const
Definition: SmallPtrSet.h:75
MachineRegisterInfo & getRegInfo()
iterator begin() const
Definition: SmallPtrSet.h:276
reg_iterator reg_begin(unsigned RegNo) const
const MCRegisterInfo & MRI
static reg_iterator reg_end()