LLVM  13.0.0git
PHIEliminationUtils.cpp
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 //===----------------------------------------------------------------------===//
8 
9 #include "PHIEliminationUtils.h"
10 #include "llvm/ADT/SmallPtrSet.h"
13 
14 using namespace llvm;
15 
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();
26 
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. This is similar to SplitKit.cpp's
31  // computeLastInsertPoint, and similarly assumes that there cannot be multiple
32  // instructions that are Calls with EHPad successors or INLINEASM_BR in a
33  // block.
34  bool EHPadSuccessor = SuccMBB->isEHPad();
35  if (!EHPadSuccessor && !SuccMBB->isInlineAsmBrIndirectTarget())
36  return MBB->getFirstTerminator();
37 
38  // Discover any defs in this basic block.
41  for (MachineInstr &RI : MRI.def_instructions(SrcReg))
42  if (RI.getParent() == MBB)
43  DefsInMBB.insert(&RI);
44 
45  MachineBasicBlock::iterator InsertPoint = MBB->begin();
46  // Insert the copy at the _latest_ point of:
47  // 1. Immediately AFTER the last def
48  // 2. Immediately BEFORE a call/inlineasm_br.
49  for (auto I = MBB->rbegin(), E = MBB->rend(); I != E; ++I) {
50  if (DefsInMBB.contains(&*I)) {
51  InsertPoint = std::next(I.getReverse());
52  break;
53  }
54  if ((EHPadSuccessor && I->isCall()) ||
55  I->getOpcode() == TargetOpcode::INLINEASM_BR) {
56  InsertPoint = I.getReverse();
57  break;
58  }
59  }
60 
61  // Make sure the copy goes after any phi nodes but before
62  // any debug nodes.
63  return MBB->SkipPHIsAndLabels(InsertPoint);
64 }
llvm
Definition: AllocatorList.h:23
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
MachineRegisterInfo.h
PHIEliminationUtils.h
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:565
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::MachineBasicBlock::rend
reverse_iterator rend()
Definition: MachineBasicBlock.h:278
SmallPtrSet.h
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::MachineBasicBlock::SkipPHIsAndLabels
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
Definition: MachineBasicBlock.cpp:209
llvm::MachineRegisterInfo::def_instructions
iterator_range< def_instr_iterator > def_instructions(Register Reg) const
Definition: MachineRegisterInfo.h:405
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
llvm::MachineBasicBlock::getFirstTerminator
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Definition: MachineBasicBlock.cpp:239
llvm::MachineBasicBlock::isEHPad
bool isEHPad() const
Returns true if the block is a landing pad.
Definition: MachineBasicBlock.h:435
llvm::MachineBasicBlock::rbegin
reverse_iterator rbegin()
Definition: MachineBasicBlock.h:272
llvm::findPHICopyInsertPoint
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...
Definition: PHIEliminationUtils.cpp:21
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::ISD::INLINEASM_BR
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
Definition: ISDOpcodes.h:968
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
llvm::MachineBasicBlock::isInlineAsmBrIndirectTarget
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
Definition: MachineBasicBlock.h:503
llvm::MachineBasicBlock::empty
bool empty() const
Definition: MachineBasicBlock.h:240
MachineFunction.h
llvm::SmallPtrSetImpl::contains
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:388
llvm::MachineInstrBundleIterator
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i....
Definition: MachineInstrBundleIterator.h:108
llvm::SmallPtrSetImpl::insert
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