LLVM  11.0.0git
Go to the documentation of this file.
1 //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "PHIEliminationUtils.h"
10 #include "llvm/ADT/SmallPtrSet.h"
14 using namespace llvm;
16 // findCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg
17 // when following the CFG edge to SuccMBB. This needs to be after any def of
18 // SrcReg, but before any subsequent point where control flow might jump out of
19 // the basic block.
22  unsigned SrcReg) {
23  // Handle the trivial case trivially.
24  if (MBB->empty())
25  return MBB->begin();
27  // Usually, we just want to insert the copy before the first terminator
28  // instruction. However, for the edge going to a landing pad, we must insert
29  // the copy before the call/invoke instruction. Similarly for an INLINEASM_BR
30  // going to an indirect target.
31  if (!SuccMBB->isEHPad() && !SuccMBB->isInlineAsmBrIndirectTarget())
32  return MBB->getFirstTerminator();
34  // Discover any defs/uses in this basic block.
35  SmallPtrSet<MachineInstr*, 8> DefUsesInMBB;
37  for (MachineInstr &RI : MRI.reg_instructions(SrcReg)) {
38  if (RI.getParent() == MBB)
39  DefUsesInMBB.insert(&RI);
40  }
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  }
57  // Make sure the copy goes after any phi nodes but before
58  // any debug nodes.
59  return MBB->SkipPHIsAndLabels(InsertPoint);
60 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
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...
MachineBasicBlock & MBB
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
unsigned const MachineRegisterInfo * MRI
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
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:364
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:375
size_type size() const
Definition: SmallPtrSet.h:92
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:439
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:62
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator begin() const
Definition: SmallPtrSet.h:392
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool isEHPad() const
Returns true if the block is a landing pad.
iterator_range< reg_instr_iterator > reg_instructions(Register Reg) const