1 //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "processimplicitdefs"
12 #include "llvm/CodeGen/ProcessImplicitDefs.h"
14 #include "llvm/ADT/DepthFirstIterator.h"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/CodeGen/LiveVariables.h"
18 #include "llvm/CodeGen/MachineInstr.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Target/TargetInstrInfo.h"
23 #include "llvm/Target/TargetRegisterInfo.h"
28 char ProcessImplicitDefs::ID = 0;
29 static RegisterPass<ProcessImplicitDefs> X("processimpdefs",
30 "Process Implicit Definitions.");
32 void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.addPreserved<AliasAnalysis>();
35 AU.addPreserved<LiveVariables>();
36 AU.addRequired<LiveVariables>();
37 AU.addPreservedID(MachineLoopInfoID);
38 AU.addPreservedID(MachineDominatorsID);
39 AU.addPreservedID(TwoAddressInstructionPassID);
40 AU.addPreservedID(PHIEliminationID);
41 MachineFunctionPass::getAnalysisUsage(AU);
44 bool ProcessImplicitDefs::CanTurnIntoImplicitDef(MachineInstr *MI,
45 unsigned Reg, unsigned OpIdx,
46 const TargetInstrInfo *tii_) {
47 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
48 if (tii_->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
49 Reg == SrcReg && DstSubReg == 0)
53 case 1: return (MI->isExtractSubreg() || MI->isCopy()) &&
54 MI->getOperand(0).getSubReg() == 0;
55 case 2: return MI->isSubregToReg() && MI->getOperand(0).getSubReg() == 0;
56 default: return false;
60 /// processImplicitDefs - Process IMPLICIT_DEF instructions and make sure
61 /// there is one implicit_def for each use. Add isUndef marker to
62 /// implicit_def defs and their uses.
63 bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &fn) {
65 DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
66 << "********** Function: "
67 << ((Value*)fn.getFunction())->getName() << '\n');
71 const TargetInstrInfo *tii_ = fn.getTarget().getInstrInfo();
72 const TargetRegisterInfo *tri_ = fn.getTarget().getRegisterInfo();
73 MachineRegisterInfo *mri_ = &fn.getRegInfo();
75 LiveVariables *lv_ = &getAnalysis<LiveVariables>();
77 SmallSet<unsigned, 8> ImpDefRegs;
78 SmallVector<MachineInstr*, 8> ImpDefMIs;
79 SmallVector<MachineInstr*, 4> RUses;
80 SmallPtrSet<MachineBasicBlock*,16> Visited;
81 SmallPtrSet<MachineInstr*, 8> ModInsts;
83 MachineBasicBlock *Entry = fn.begin();
84 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*,16> >
85 DFI = df_ext_begin(Entry, Visited), E = df_ext_end(Entry, Visited);
87 MachineBasicBlock *MBB = *DFI;
88 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
90 MachineInstr *MI = &*I;
92 if (MI->isImplicitDef()) {
93 if (MI->getOperand(0).getSubReg())
95 unsigned Reg = MI->getOperand(0).getReg();
96 ImpDefRegs.insert(Reg);
97 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
98 for (const unsigned *SS = tri_->getSubRegisters(Reg); *SS; ++SS)
99 ImpDefRegs.insert(*SS);
101 ImpDefMIs.push_back(MI);
105 if (MI->isInsertSubreg()) {
106 MachineOperand &MO = MI->getOperand(2);
107 if (ImpDefRegs.count(MO.getReg())) {
108 // %reg1032<def> = INSERT_SUBREG %reg1032, undef, 2
109 // This is an identity copy, eliminate it now.
111 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
114 MI->eraseFromParent();
120 // Eliminate %reg1032:sub<def> = COPY undef.
121 if (MI->isCopy() && MI->getOperand(0).getSubReg()) {
122 MachineOperand &MO = MI->getOperand(1);
123 if (ImpDefRegs.count(MO.getReg())) {
125 LiveVariables::VarInfo& vi = lv_->getVarInfo(MO.getReg());
128 MI->eraseFromParent();
134 bool ChangedToImpDef = false;
135 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
136 MachineOperand& MO = MI->getOperand(i);
137 if (!MO.isReg() || (MO.isDef() && !MO.getSubReg()) || MO.isUndef())
139 unsigned Reg = MO.getReg();
142 if (!ImpDefRegs.count(Reg))
144 // Use is a copy, just turn it into an implicit_def.
145 if (CanTurnIntoImplicitDef(MI, Reg, i, tii_)) {
146 bool isKill = MO.isKill();
147 MI->setDesc(tii_->get(TargetOpcode::IMPLICIT_DEF));
148 for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j)
149 MI->RemoveOperand(j);
151 ImpDefRegs.erase(Reg);
152 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
155 ChangedToImpDef = true;
162 // This is a partial register redef of an implicit def.
163 // Make sure the whole register is defined by the instruction.
165 MI->addRegisterDefined(Reg);
168 if (MO.isKill() || MI->isRegTiedToDefOperand(i)) {
169 // Make sure other uses of
170 for (unsigned j = i+1; j != e; ++j) {
171 MachineOperand &MOJ = MI->getOperand(j);
172 if (MOJ.isReg() && MOJ.isUse() && MOJ.getReg() == Reg)
175 ImpDefRegs.erase(Reg);
179 if (ChangedToImpDef) {
180 // Backtrack to process this new implicit_def.
183 for (unsigned i = 0; i != MI->getNumOperands(); ++i) {
184 MachineOperand& MO = MI->getOperand(i);
185 if (!MO.isReg() || !MO.isDef())
187 ImpDefRegs.erase(MO.getReg());
192 // Any outstanding liveout implicit_def's?
193 for (unsigned i = 0, e = ImpDefMIs.size(); i != e; ++i) {
194 MachineInstr *MI = ImpDefMIs[i];
195 unsigned Reg = MI->getOperand(0).getReg();
196 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
197 !ImpDefRegs.count(Reg)) {
198 // Delete all "local" implicit_def's. That include those which define
199 // physical registers since they cannot be liveout.
200 MI->eraseFromParent();
205 // If there are multiple defs of the same register and at least one
206 // is not an implicit_def, do not insert implicit_def's before the
209 SmallVector<MachineInstr*, 4> DeadImpDefs;
210 for (MachineRegisterInfo::def_iterator DI = mri_->def_begin(Reg),
211 DE = mri_->def_end(); DI != DE; ++DI) {
212 MachineInstr *DeadImpDef = &*DI;
213 if (!DeadImpDef->isImplicitDef()) {
217 DeadImpDefs.push_back(DeadImpDef);
222 // The only implicit_def which we want to keep are those that are live
224 for (unsigned j = 0, ee = DeadImpDefs.size(); j != ee; ++j)
225 DeadImpDefs[j]->eraseFromParent();
228 // Process each use instruction once.
229 for (MachineRegisterInfo::use_iterator UI = mri_->use_begin(Reg),
230 UE = mri_->use_end(); UI != UE; ++UI) {
231 if (UI.getOperand().isUndef())
233 MachineInstr *RMI = &*UI;
234 if (ModInsts.insert(RMI))
235 RUses.push_back(RMI);
238 for (unsigned i = 0, e = RUses.size(); i != e; ++i) {
239 MachineInstr *RMI = RUses[i];
241 // Turn a copy use into an implicit_def.
242 unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
243 if ((RMI->isCopy() && RMI->getOperand(1).getReg() == Reg &&
244 RMI->getOperand(0).getSubReg() == 0) ||
245 (tii_->isMoveInstr(*RMI, SrcReg, DstReg, SrcSubReg, DstSubReg) &&
246 Reg == SrcReg && DstSubReg == 0)) {
247 RMI->setDesc(tii_->get(TargetOpcode::IMPLICIT_DEF));
250 SmallVector<unsigned, 4> Ops;
251 for (unsigned j = 0, ee = RMI->getNumOperands(); j != ee; ++j) {
252 MachineOperand &RRMO = RMI->getOperand(j);
253 if (RRMO.isReg() && RRMO.getReg() == Reg) {
259 // Leave the other operands along.
260 for (unsigned j = 0, ee = Ops.size(); j != ee; ++j) {
261 unsigned OpIdx = Ops[j];
262 RMI->RemoveOperand(OpIdx-j);
265 // Update LiveVariables varinfo if the instruction is a kill.
267 LiveVariables::VarInfo& vi = lv_->getVarInfo(Reg);
273 // Replace Reg with a new vreg that's marked implicit.
274 const TargetRegisterClass* RC = mri_->getRegClass(Reg);
275 unsigned NewVReg = mri_->createVirtualRegister(RC);
277 for (unsigned j = 0, ee = RMI->getNumOperands(); j != ee; ++j) {
278 MachineOperand &RRMO = RMI->getOperand(j);
279 if (RRMO.isReg() && RRMO.getReg() == Reg) {
280 RRMO.setReg(NewVReg);
283 // Only the first operand of NewVReg is marked kill.