blob: 3a2cdaf3bd3cb8aef2a58ba084c99d09867b5c05 [file] [log] [blame]
Cameron Zwarichda592a9e2010-12-05 19:51:05 +00001//===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===//
2//
Chandler Carruth2946cd72019-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 Zwarichda592a9e2010-12-05 19:51:05 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "PHIEliminationUtils.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000010#include "llvm/ADT/SmallPtrSet.h"
Cameron Zwarichda592a9e2010-12-05 19:51:05 +000011#include "llvm/CodeGen/MachineBasicBlock.h"
12#include "llvm/CodeGen/MachineFunction.h"
13#include "llvm/CodeGen/MachineRegisterInfo.h"
Cameron Zwarichda592a9e2010-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
29 // the copy before the call/invoke instruction.
Reid Kleckner0e288232015-08-27 23:27:47 +000030 if (!SuccMBB->isEHPad())
Cameron Zwarichda592a9e2010-12-05 19:51:05 +000031 return MBB->getFirstTerminator();
32
33 // Discover any defs/uses in this basic block.
34 SmallPtrSet<MachineInstr*, 8> DefUsesInMBB;
35 MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
Owen Andersonb36376e2014-03-17 19:36:09 +000036 for (MachineInstr &RI : MRI.reg_instructions(SrcReg)) {
37 if (RI.getParent() == MBB)
38 DefUsesInMBB.insert(&RI);
Cameron Zwarichda592a9e2010-12-05 19:51:05 +000039 }
40
41 MachineBasicBlock::iterator InsertPoint;
42 if (DefUsesInMBB.empty()) {
43 // No defs. Insert the copy at the start of the basic block.
44 InsertPoint = MBB->begin();
45 } else if (DefUsesInMBB.size() == 1) {
46 // Insert the copy immediately after the def/use.
47 InsertPoint = *DefUsesInMBB.begin();
48 ++InsertPoint;
49 } else {
50 // Insert the copy immediately after the last def/use.
51 InsertPoint = MBB->end();
52 while (!DefUsesInMBB.count(&*--InsertPoint)) {}
53 ++InsertPoint;
54 }
55
Keith Walker830a8c12016-09-16 14:07:29 +000056 // Make sure the copy goes after any phi nodes but before
57 // any debug nodes.
Cameron Zwarichda592a9e2010-12-05 19:51:05 +000058 return MBB->SkipPHIsAndLabels(InsertPoint);
59}