Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 1 | //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===// |
| 2 | // |
Chandler Carruth | ca6d719 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "PHIEliminationUtils.h" |
Chandler Carruth | 345b827 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SmallPtrSet.h" |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/MachineFunction.h" |
| 12 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Simon Pilgrim | 145afde | 2020-09-03 16:47:21 +0100 | [diff] [blame] | 13 | |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 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. |
| 20 | MachineBasicBlock::iterator |
| 21 | llvm::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 Knight | 7714527 | 2020-05-15 23:43:30 -0400 | [diff] [blame] | 29 | // the copy before the call/invoke instruction. Similarly for an INLINEASM_BR |
James Y Knight | 1f9d01c | 2020-09-17 18:10:19 -0400 | [diff] [blame] | 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()) |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 36 | return MBB->getFirstTerminator(); |
| 37 | |
James Y Knight | 1f9d01c | 2020-09-17 18:10:19 -0400 | [diff] [blame] | 38 | // Discover any defs in this basic block. |
| 39 | SmallPtrSet<MachineInstr *, 8> DefsInMBB; |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 40 | MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo(); |
James Y Knight | 1f9d01c | 2020-09-17 18:10:19 -0400 | [diff] [blame] | 41 | for (MachineInstr &RI : MRI.def_instructions(SrcReg)) |
Owen Anderson | 70c269e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 42 | if (RI.getParent() == MBB) |
James Y Knight | 1f9d01c | 2020-09-17 18:10:19 -0400 | [diff] [blame] | 43 | DefsInMBB.insert(&RI); |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 44 | |
James Y Knight | 1f9d01c | 2020-09-17 18:10:19 -0400 | [diff] [blame] | 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 | } |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Keith Walker | 5751d5a | 2016-09-16 14:07:29 +0000 | [diff] [blame] | 61 | // Make sure the copy goes after any phi nodes but before |
| 62 | // any debug nodes. |
Cameron Zwarich | 366aabb | 2010-12-05 19:51:05 +0000 | [diff] [blame] | 63 | return MBB->SkipPHIsAndLabels(InsertPoint); |
| 64 | } |