blob: 016335f420d3e6895ffcfee0460fef0313ed51e9 [file] [log] [blame]
Cameron Zwarich366aabb2010-12-05 19:51:05 +00001//===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===//
2//
Chandler Carruthca6d7192019-01-19 08:50:56 +00003// 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
Cameron Zwarich366aabb2010-12-05 19:51:05 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "PHIEliminationUtils.h"
Chandler Carruth345b8272012-12-03 16:50:05 +000010#include "llvm/ADT/SmallPtrSet.h"
Cameron Zwarich366aabb2010-12-05 19:51:05 +000011#include "llvm/CodeGen/MachineFunction.h"
12#include "llvm/CodeGen/MachineRegisterInfo.h"
Simon Pilgrim145afde2020-09-03 16:47:21 +010013
Cameron Zwarich366aabb2010-12-05 19:51:05 +000014using 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.
20MachineBasicBlock::iterator
21llvm::findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB,
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
James Y Knight77145272020-05-15 23:43:30 -040029 // the copy before the call/invoke instruction. Similarly for an INLINEASM_BR
James Y Knight1f9d01c2020-09-17 18:10:19 -040030 // 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())
Cameron Zwarich366aabb2010-12-05 19:51:05 +000036 return MBB->getFirstTerminator();
37
James Y Knight1f9d01c2020-09-17 18:10:19 -040038 // Discover any defs in this basic block.
39 SmallPtrSet<MachineInstr *, 8> DefsInMBB;
Cameron Zwarich366aabb2010-12-05 19:51:05 +000040 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
James Y Knight1f9d01c2020-09-17 18:10:19 -040041 for (MachineInstr &RI : MRI.def_instructions(SrcReg))
Owen Anderson70c269e2014-03-17 19:36:09 +000042 if (RI.getParent() == MBB)
James Y Knight1f9d01c2020-09-17 18:10:19 -040043 DefsInMBB.insert(&RI);
Cameron Zwarich366aabb2010-12-05 19:51:05 +000044
James Y Knight1f9d01c2020-09-17 18:10:19 -040045 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 }
Cameron Zwarich366aabb2010-12-05 19:51:05 +000059 }
60
Keith Walker5751d5a2016-09-16 14:07:29 +000061 // Make sure the copy goes after any phi nodes but before
62 // any debug nodes.
Cameron Zwarich366aabb2010-12-05 19:51:05 +000063 return MBB->SkipPHIsAndLabels(InsertPoint);
64}