blob: e4e18c3bb54b51e4abb2ec4e0f4bbd6d65e9b66e [file] [log] [blame]
Lang Hames233a60e2009-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
10#define DEBUG_TYPE "processimplicitdefs"
11
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +000012#include "llvm/ADT/SetVector.h"
Lang Hames233a60e2009-11-03 23:52:08 +000013#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesen0cafa132012-06-22 22:27:36 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
Lang Hames233a60e2009-11-03 23:52:08 +000015#include "llvm/CodeGen/MachineInstr.h"
16#include "llvm/CodeGen/MachineRegisterInfo.h"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +000019#include "llvm/Support/raw_ostream.h"
Lang Hames233a60e2009-11-03 23:52:08 +000020#include "llvm/Target/TargetInstrInfo.h"
Lang Hames233a60e2009-11-03 23:52:08 +000021
22using namespace llvm;
23
Jakob Stoklund Olesen0cafa132012-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 Olesen0cafa132012-06-22 22:27:36 +000031
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +000032 SmallSetVector<MachineInstr*, 16> WorkList;
33
34 void processImplicitDef(MachineInstr *MI);
35 bool canTurnIntoImplicitDef(MachineInstr *MI);
Jakob Stoklund Olesen0cafa132012-06-22 22:27:36 +000036
37public:
38 static char ID;
39
40 ProcessImplicitDefs() : MachineFunctionPass(ID) {
41 initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
42 }
43
44 virtual void getAnalysisUsage(AnalysisUsage &au) const;
45
46 virtual bool runOnMachineFunction(MachineFunction &fn);
47};
48} // end anonymous namespace
49
Lang Hames233a60e2009-11-03 23:52:08 +000050char ProcessImplicitDefs::ID = 0;
Andrew Trick8dd26252012-02-10 04:10:36 +000051char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
52
Owen Anderson2ab36d32010-10-12 19:48:12 +000053INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
Cameron Zwarichdd061b32010-12-29 11:49:10 +000054 "Process Implicit Definitions", false, false)
Owen Anderson2ab36d32010-10-12 19:48:12 +000055INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
Cameron Zwarichdd061b32010-12-29 11:49:10 +000056 "Process Implicit Definitions", false, false)
Lang Hames233a60e2009-11-03 23:52:08 +000057
58void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesCFG();
60 AU.addPreserved<AliasAnalysis>();
Lang Hames233a60e2009-11-03 23:52:08 +000061 MachineFunctionPass::getAnalysisUsage(AU);
62}
63
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +000064bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
65 if (!MI->isCopyLike() &&
66 !MI->isInsertSubreg() &&
67 !MI->isRegSequence() &&
68 !MI->isPHI())
Evan Chengdb898092010-07-14 01:22:19 +000069 return false;
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +000070 for (MIOperands MO(MI); MO.isValid(); ++MO)
71 if (MO->isReg() && MO->isUse() && MO->readsReg())
72 return false;
73 return true;
Evan Chengdb898092010-07-14 01:22:19 +000074}
75
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +000076void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
77 DEBUG(dbgs() << "Processing " << *MI);
78 unsigned Reg = MI->getOperand(0).getReg();
79
80 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
81 // For virtual regiusters, mark all uses as <undef>, and convert users to
82 // implicit-def when possible.
83 for (MachineRegisterInfo::use_nodbg_iterator UI =
84 MRI->use_nodbg_begin(Reg),
85 UE = MRI->use_nodbg_end(); UI != UE; ++UI) {
86 MachineOperand &MO = UI.getOperand();
87 MO.setIsUndef();
88 MachineInstr *UserMI = MO.getParent();
89 if (!canTurnIntoImplicitDef(UserMI))
90 continue;
91 DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
92 UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
93 WorkList.insert(UserMI);
94 }
95 MI->eraseFromParent();
96 return;
97 }
98
99 // This is a physreg implicit-def.
100 // Look for the first instruction to use or define an alias.
101 MachineBasicBlock::instr_iterator UserMI = MI;
102 MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
103 bool Found = false;
104 for (++UserMI; UserMI != UserE; ++UserMI) {
105 for (MIOperands MO(UserMI); MO.isValid(); ++MO) {
106 if (!MO->isReg())
107 continue;
108 unsigned UserReg = MO->getReg();
109 if (!TargetRegisterInfo::isPhysicalRegister(UserReg) ||
110 !TRI->regsOverlap(Reg, UserReg))
111 continue;
112 // UserMI uses or redefines Reg. Set <undef> flags on all uses.
113 Found = true;
114 if (MO->isUse())
115 MO->setIsUndef();
116 }
117 if (Found)
118 break;
119 }
120
121 // If we found the using MI, we can erase the IMPLICIT_DEF.
122 if (Found) {
123 DEBUG(dbgs() << "Physreg user: " << *UserMI);
124 MI->eraseFromParent();
125 return;
126 }
127
128 // Using instr wasn't found, it could be in another block.
129 // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
130 for (unsigned i = MI->getNumOperands() - 1; i; --i)
131 MI->RemoveOperand(i);
132 DEBUG(dbgs() << "Keeping physreg: " << *MI);
133}
134
135/// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
136/// <undef> operands.
137bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
Lang Hames233a60e2009-11-03 23:52:08 +0000138
David Greene7530efb2010-01-05 01:24:28 +0000139 DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
David Blaikie986d76d2012-08-22 17:18:53 +0000140 << "********** Function: " << MF.getName() << '\n');
Lang Hames233a60e2009-11-03 23:52:08 +0000141
142 bool Changed = false;
143
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000144 TII = MF.getTarget().getInstrInfo();
145 TRI = MF.getTarget().getRegisterInfo();
146 MRI = &MF.getRegInfo();
147 assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
148 assert(WorkList.empty() && "Inconsistent worklist state");
Lang Hames233a60e2009-11-03 23:52:08 +0000149
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000150 for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
151 MFI != MFE; ++MFI) {
152 // Scan the basic block for implicit defs.
153 for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
154 MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
155 if (MBBI->isImplicitDef())
156 WorkList.insert(MBBI);
Lang Hames233a60e2009-11-03 23:52:08 +0000157
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000158 if (WorkList.empty())
159 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000160
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000161 DEBUG(dbgs() << "BB#" << MFI->getNumber() << " has " << WorkList.size()
162 << " implicit defs.\n");
163 Changed = true;
Jakob Stoklund Olesenf6c69002011-07-28 21:38:51 +0000164
Jakob Stoklund Olesen5984d2b2012-06-25 18:12:18 +0000165 // Drain the WorkList to recursively process any new implicit defs.
166 do processImplicitDef(WorkList.pop_back_val());
167 while (!WorkList.empty());
Lang Hames233a60e2009-11-03 23:52:08 +0000168 }
Lang Hames233a60e2009-11-03 23:52:08 +0000169 return Changed;
170}