blob: b38987ad1c90456832da296990bc01c8dd052cd5 [file] [log] [blame]
Lang Hames05fb9632009-11-03 23:52:08 +00001//===---------------------- ProcessImplicitDefs.cpp -----------------------===//
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
Lang Hames05fb9632009-11-03 23:52:08 +00006//
7//===----------------------------------------------------------------------===//
8
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +00009#include "llvm/ADT/SetVector.h"
Lang Hames05fb9632009-11-03 23:52:08 +000010#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000011#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hames05fb9632009-11-03 23:52:08 +000012#include "llvm/CodeGen/MachineInstr.h"
13#include "llvm/CodeGen/MachineRegisterInfo.h"
14#include "llvm/CodeGen/Passes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000015#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000016#include "llvm/CodeGen/TargetSubtargetInfo.h"
Lang Hames05fb9632009-11-03 23:52:08 +000017#include "llvm/Support/Debug.h"
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000018#include "llvm/Support/raw_ostream.h"
Lang Hames05fb9632009-11-03 23:52:08 +000019
20using namespace llvm;
21
Matthias Braun1527baa2017-05-25 21:26:32 +000022#define DEBUG_TYPE "processimpdefs"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000023
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000024namespace {
25/// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
26/// for each use. Add isUndef marker to implicit_def defs and their uses.
27class ProcessImplicitDefs : public MachineFunctionPass {
28 const TargetInstrInfo *TII;
29 const TargetRegisterInfo *TRI;
30 MachineRegisterInfo *MRI;
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000031
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000032 SmallSetVector<MachineInstr*, 16> WorkList;
33
34 void processImplicitDef(MachineInstr *MI);
35 bool canTurnIntoImplicitDef(MachineInstr *MI);
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000036
37public:
38 static char ID;
39
40 ProcessImplicitDefs() : MachineFunctionPass(ID) {
41 initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
42 }
43
Craig Topper4584cd52014-03-07 09:26:03 +000044 void getAnalysisUsage(AnalysisUsage &au) const override;
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000045
Fangrui Songcb0bab82018-07-16 18:51:40 +000046 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000047};
48} // end anonymous namespace
49
Lang Hames05fb9632009-11-03 23:52:08 +000050char ProcessImplicitDefs::ID = 0;
Andrew Trickd3f8fe82012-02-10 04:10:36 +000051char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
52
Matthias Braun1527baa2017-05-25 21:26:32 +000053INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE,
Cameron Zwarich329cd492010-12-29 11:49:10 +000054 "Process Implicit Definitions", false, false)
Lang Hames05fb9632009-11-03 23:52:08 +000055
56void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +000058 AU.addPreserved<AAResultsWrapperPass>();
Lang Hames05fb9632009-11-03 23:52:08 +000059 MachineFunctionPass::getAnalysisUsage(AU);
60}
61
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000062bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
63 if (!MI->isCopyLike() &&
64 !MI->isInsertSubreg() &&
65 !MI->isRegSequence() &&
66 !MI->isPHI())
Evan Chengd5424142010-07-14 01:22:19 +000067 return false;
Matthias Braune41e1462015-05-29 02:56:46 +000068 for (const MachineOperand &MO : MI->operands())
69 if (MO.isReg() && MO.isUse() && MO.readsReg())
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000070 return false;
71 return true;
Evan Chengd5424142010-07-14 01:22:19 +000072}
73
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000074void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000075 LLVM_DEBUG(dbgs() << "Processing " << *MI);
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000076 unsigned Reg = MI->getOperand(0).getReg();
77
78 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braunc9d5c0f2013-10-04 16:52:58 +000079 // For virtual registers, mark all uses as <undef>, and convert users to
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000080 // implicit-def when possible.
Owen Andersonb36376e2014-03-17 19:36:09 +000081 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000082 MO.setIsUndef();
83 MachineInstr *UserMI = MO.getParent();
84 if (!canTurnIntoImplicitDef(UserMI))
85 continue;
Nicola Zaghend34e60c2018-05-14 12:53:11 +000086 LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000087 UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
88 WorkList.insert(UserMI);
89 }
90 MI->eraseFromParent();
91 return;
92 }
93
94 // This is a physreg implicit-def.
95 // Look for the first instruction to use or define an alias.
Duncan P. N. Exon Smithc5b668d2016-02-22 20:49:58 +000096 MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
97 MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000098 bool Found = false;
99 for (++UserMI; UserMI != UserE; ++UserMI) {
Matthias Braune41e1462015-05-29 02:56:46 +0000100 for (MachineOperand &MO : UserMI->operands()) {
101 if (!MO.isReg())
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000102 continue;
Matthias Braune41e1462015-05-29 02:56:46 +0000103 unsigned UserReg = MO.getReg();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000104 if (!TargetRegisterInfo::isPhysicalRegister(UserReg) ||
105 !TRI->regsOverlap(Reg, UserReg))
106 continue;
107 // UserMI uses or redefines Reg. Set <undef> flags on all uses.
108 Found = true;
Matthias Braune41e1462015-05-29 02:56:46 +0000109 if (MO.isUse())
110 MO.setIsUndef();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000111 }
112 if (Found)
113 break;
114 }
115
116 // If we found the using MI, we can erase the IMPLICIT_DEF.
117 if (Found) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000118 LLVM_DEBUG(dbgs() << "Physreg user: " << *UserMI);
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000119 MI->eraseFromParent();
120 return;
121 }
122
123 // Using instr wasn't found, it could be in another block.
124 // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
125 for (unsigned i = MI->getNumOperands() - 1; i; --i)
126 MI->RemoveOperand(i);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000127 LLVM_DEBUG(dbgs() << "Keeping physreg: " << *MI);
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000128}
129
130/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
131/// <undef> operands.
132bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
Lang Hames05fb9632009-11-03 23:52:08 +0000133
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000134 LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
135 << "********** Function: " << MF.getName() << '\n');
Lang Hames05fb9632009-11-03 23:52:08 +0000136
137 bool Changed = false;
138
Eric Christopherfc6de422014-08-05 02:39:49 +0000139 TII = MF.getSubtarget().getInstrInfo();
140 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000141 MRI = &MF.getRegInfo();
142 assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
143 assert(WorkList.empty() && "Inconsistent worklist state");
Lang Hames05fb9632009-11-03 23:52:08 +0000144
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000145 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
146 MFI != MFE; ++MFI) {
147 // Scan the basic block for implicit defs.
148 for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
149 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
150 if (MBBI->isImplicitDef())
Duncan P. N. Exon Smith6e98cd32015-10-09 21:08:19 +0000151 WorkList.insert(&*MBBI);
Lang Hames05fb9632009-11-03 23:52:08 +0000152
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000153 if (WorkList.empty())
154 continue;
Lang Hames05fb9632009-11-03 23:52:08 +0000155
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000156 LLVM_DEBUG(dbgs() << printMBBReference(*MFI) << " has " << WorkList.size()
157 << " implicit defs.\n");
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000158 Changed = true;
Jakob Stoklund Olesenb16081c2011-07-28 21:38:51 +0000159
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000160 // Drain the WorkList to recursively process any new implicit defs.
161 do processImplicitDef(WorkList.pop_back_val());
162 while (!WorkList.empty());
Lang Hames05fb9632009-11-03 23:52:08 +0000163 }
Lang Hames05fb9632009-11-03 23:52:08 +0000164 return Changed;
165}