LLVM 19.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
13
14using 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}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
#define I(x, y, z)
Definition: MD5.cpp:58
This file defines the SmallPtrSet class.
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
bool isEHPad() const
Returns true if the block is a landing pad.
reverse_iterator rend()
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
reverse_iterator rbegin()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
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:342
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:366
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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...