blob: d27ea2f51867a60353328703ee901bd47dd338df [file] [log] [blame]
Lang Hames05fb9632009-11-03 23:52:08 +00001//===---------------------- ProcessImplicitDefs.cpp -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000010#include "llvm/ADT/SetVector.h"
Lang Hames05fb9632009-11-03 23:52:08 +000011#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000012#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hames05fb9632009-11-03 23:52:08 +000013#include "llvm/CodeGen/MachineInstr.h"
14#include "llvm/CodeGen/MachineRegisterInfo.h"
15#include "llvm/CodeGen/Passes.h"
16#include "llvm/Support/Debug.h"
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000017#include "llvm/Support/raw_ostream.h"
Lang Hames05fb9632009-11-03 23:52:08 +000018#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000019#include "llvm/Target/TargetSubtargetInfo.h"
Lang Hames05fb9632009-11-03 23:52:08 +000020
21using namespace llvm;
22
Chandler Carruth1b9dde02014-04-22 02:02:50 +000023#define DEBUG_TYPE "processimplicitdefs"
24
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000025namespace {
26/// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
27/// for each use. Add isUndef marker to implicit_def defs and their uses.
28class ProcessImplicitDefs : public MachineFunctionPass {
29 const TargetInstrInfo *TII;
30 const TargetRegisterInfo *TRI;
31 MachineRegisterInfo *MRI;
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000032
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000033 SmallSetVector<MachineInstr*, 16> WorkList;
34
35 void processImplicitDef(MachineInstr *MI);
36 bool canTurnIntoImplicitDef(MachineInstr *MI);
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000037
38public:
39 static char ID;
40
41 ProcessImplicitDefs() : MachineFunctionPass(ID) {
42 initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
43 }
44
Craig Topper4584cd52014-03-07 09:26:03 +000045 void getAnalysisUsage(AnalysisUsage &au) const override;
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000046
Craig Topper4584cd52014-03-07 09:26:03 +000047 bool runOnMachineFunction(MachineFunction &fn) override;
Jakob Stoklund Olesena127fc72012-06-22 22:27:36 +000048};
49} // end anonymous namespace
50
Lang Hames05fb9632009-11-03 23:52:08 +000051char ProcessImplicitDefs::ID = 0;
Andrew Trickd3f8fe82012-02-10 04:10:36 +000052char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
53
Owen Anderson8ac477f2010-10-12 19:48:12 +000054INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
Cameron Zwarich329cd492010-12-29 11:49:10 +000055 "Process Implicit Definitions", false, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000056INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
Cameron Zwarich329cd492010-12-29 11:49:10 +000057 "Process Implicit Definitions", false, false)
Lang Hames05fb9632009-11-03 23:52:08 +000058
59void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +000061 AU.addPreserved<AAResultsWrapperPass>();
Lang Hames05fb9632009-11-03 23:52:08 +000062 MachineFunctionPass::getAnalysisUsage(AU);
63}
64
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000065bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
66 if (!MI->isCopyLike() &&
67 !MI->isInsertSubreg() &&
68 !MI->isRegSequence() &&
69 !MI->isPHI())
Evan Chengd5424142010-07-14 01:22:19 +000070 return false;
Matthias Braune41e1462015-05-29 02:56:46 +000071 for (const MachineOperand &MO : MI->operands())
72 if (MO.isReg() && MO.isUse() && MO.readsReg())
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000073 return false;
74 return true;
Evan Chengd5424142010-07-14 01:22:19 +000075}
76
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000077void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
78 DEBUG(dbgs() << "Processing " << *MI);
79 unsigned Reg = MI->getOperand(0).getReg();
80
81 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Matthias Braunc9d5c0f2013-10-04 16:52:58 +000082 // For virtual registers, mark all uses as <undef>, and convert users to
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000083 // implicit-def when possible.
Owen Andersonb36376e2014-03-17 19:36:09 +000084 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +000085 MO.setIsUndef();
86 MachineInstr *UserMI = MO.getParent();
87 if (!canTurnIntoImplicitDef(UserMI))
88 continue;
89 DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
90 UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
91 WorkList.insert(UserMI);
92 }
93 MI->eraseFromParent();
94 return;
95 }
96
97 // This is a physreg implicit-def.
98 // Look for the first instruction to use or define an alias.
Duncan P. N. Exon Smith6e98cd32015-10-09 21:08:19 +000099 MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000100 MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
101 bool Found = false;
102 for (++UserMI; UserMI != UserE; ++UserMI) {
Matthias Braune41e1462015-05-29 02:56:46 +0000103 for (MachineOperand &MO : UserMI->operands()) {
104 if (!MO.isReg())
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000105 continue;
Matthias Braune41e1462015-05-29 02:56:46 +0000106 unsigned UserReg = MO.getReg();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000107 if (!TargetRegisterInfo::isPhysicalRegister(UserReg) ||
108 !TRI->regsOverlap(Reg, UserReg))
109 continue;
110 // UserMI uses or redefines Reg. Set <undef> flags on all uses.
111 Found = true;
Matthias Braune41e1462015-05-29 02:56:46 +0000112 if (MO.isUse())
113 MO.setIsUndef();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000114 }
115 if (Found)
116 break;
117 }
118
119 // If we found the using MI, we can erase the IMPLICIT_DEF.
120 if (Found) {
121 DEBUG(dbgs() << "Physreg user: " << *UserMI);
122 MI->eraseFromParent();
123 return;
124 }
125
126 // Using instr wasn't found, it could be in another block.
127 // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
128 for (unsigned i = MI->getNumOperands() - 1; i; --i)
129 MI->RemoveOperand(i);
130 DEBUG(dbgs() << "Keeping physreg: " << *MI);
131}
132
133/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
134/// <undef> operands.
135bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
Lang Hames05fb9632009-11-03 23:52:08 +0000136
David Greened7c266b2010-01-05 01:24:28 +0000137 DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +0000138 << "********** Function: " << MF.getName() << '\n');
Lang Hames05fb9632009-11-03 23:52:08 +0000139
140 bool Changed = false;
141
Eric Christopherfc6de422014-08-05 02:39:49 +0000142 TII = MF.getSubtarget().getInstrInfo();
143 TRI = MF.getSubtarget().getRegisterInfo();
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000144 MRI = &MF.getRegInfo();
145 assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
146 assert(WorkList.empty() && "Inconsistent worklist state");
Lang Hames05fb9632009-11-03 23:52:08 +0000147
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000148 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
149 MFI != MFE; ++MFI) {
150 // Scan the basic block for implicit defs.
151 for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
152 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
153 if (MBBI->isImplicitDef())
Duncan P. N. Exon Smith6e98cd32015-10-09 21:08:19 +0000154 WorkList.insert(&*MBBI);
Lang Hames05fb9632009-11-03 23:52:08 +0000155
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000156 if (WorkList.empty())
157 continue;
Lang Hames05fb9632009-11-03 23:52:08 +0000158
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000159 DEBUG(dbgs() << "BB#" << MFI->getNumber() << " has " << WorkList.size()
160 << " implicit defs.\n");
161 Changed = true;
Jakob Stoklund Olesenb16081c2011-07-28 21:38:51 +0000162
Jakob Stoklund Oleseneb495662012-06-25 18:12:18 +0000163 // Drain the WorkList to recursively process any new implicit defs.
164 do processImplicitDef(WorkList.pop_back_val());
165 while (!WorkList.empty());
Lang Hames05fb9632009-11-03 23:52:08 +0000166 }
Lang Hames05fb9632009-11-03 23:52:08 +0000167 return Changed;
168}