10b57cec5SDimitry Andric //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "PHIEliminationUtils.h"
100b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
110b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
120b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
13*e8d8bef9SDimitry Andric
140b57cec5SDimitry Andric using namespace llvm;
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric // findCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg
170b57cec5SDimitry Andric // when following the CFG edge to SuccMBB. This needs to be after any def of
180b57cec5SDimitry Andric // SrcReg, but before any subsequent point where control flow might jump out of
190b57cec5SDimitry Andric // the basic block.
200b57cec5SDimitry Andric MachineBasicBlock::iterator
findPHICopyInsertPoint(MachineBasicBlock * MBB,MachineBasicBlock * SuccMBB,unsigned SrcReg)210b57cec5SDimitry Andric llvm::findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB,
220b57cec5SDimitry Andric unsigned SrcReg) {
230b57cec5SDimitry Andric // Handle the trivial case trivially.
240b57cec5SDimitry Andric if (MBB->empty())
250b57cec5SDimitry Andric return MBB->begin();
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric // Usually, we just want to insert the copy before the first terminator
280b57cec5SDimitry Andric // instruction. However, for the edge going to a landing pad, we must insert
295ffd83dbSDimitry Andric // the copy before the call/invoke instruction. Similarly for an INLINEASM_BR
308833aad7SDimitry Andric // going to an indirect target. This is similar to SplitKit.cpp's
318833aad7SDimitry Andric // computeLastInsertPoint, and similarly assumes that there cannot be multiple
328833aad7SDimitry Andric // instructions that are Calls with EHPad successors or INLINEASM_BR in a
338833aad7SDimitry Andric // block.
348833aad7SDimitry Andric bool EHPadSuccessor = SuccMBB->isEHPad();
358833aad7SDimitry Andric if (!EHPadSuccessor && !SuccMBB->isInlineAsmBrIndirectTarget())
360b57cec5SDimitry Andric return MBB->getFirstTerminator();
370b57cec5SDimitry Andric
388833aad7SDimitry Andric // Discover any defs in this basic block.
398833aad7SDimitry Andric SmallPtrSet<MachineInstr *, 8> DefsInMBB;
400b57cec5SDimitry Andric MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
418833aad7SDimitry Andric for (MachineInstr &RI : MRI.def_instructions(SrcReg))
420b57cec5SDimitry Andric if (RI.getParent() == MBB)
438833aad7SDimitry Andric DefsInMBB.insert(&RI);
440b57cec5SDimitry Andric
458833aad7SDimitry Andric MachineBasicBlock::iterator InsertPoint = MBB->begin();
468833aad7SDimitry Andric // Insert the copy at the _latest_ point of:
478833aad7SDimitry Andric // 1. Immediately AFTER the last def
488833aad7SDimitry Andric // 2. Immediately BEFORE a call/inlineasm_br.
498833aad7SDimitry Andric for (auto I = MBB->rbegin(), E = MBB->rend(); I != E; ++I) {
508833aad7SDimitry Andric if (DefsInMBB.contains(&*I)) {
518833aad7SDimitry Andric InsertPoint = std::next(I.getReverse());
528833aad7SDimitry Andric break;
538833aad7SDimitry Andric }
548833aad7SDimitry Andric if ((EHPadSuccessor && I->isCall()) ||
558833aad7SDimitry Andric I->getOpcode() == TargetOpcode::INLINEASM_BR) {
568833aad7SDimitry Andric InsertPoint = I.getReverse();
578833aad7SDimitry Andric break;
588833aad7SDimitry Andric }
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric // Make sure the copy goes after any phi nodes but before
620b57cec5SDimitry Andric // any debug nodes.
630b57cec5SDimitry Andric return MBB->SkipPHIsAndLabels(InsertPoint);
640b57cec5SDimitry Andric }
65