xref: /aosp_15_r20/external/llvm/lib/CodeGen/ProcessImplicitDefs.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker 
10*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
20*9880d681SAndroid Build Coastguard Worker 
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "processimplicitdefs"
24*9880d681SAndroid Build Coastguard Worker 
25*9880d681SAndroid Build Coastguard Worker namespace {
26*9880d681SAndroid Build Coastguard Worker /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
27*9880d681SAndroid Build Coastguard Worker /// for each use. Add isUndef marker to implicit_def defs and their uses.
28*9880d681SAndroid Build Coastguard Worker class ProcessImplicitDefs : public MachineFunctionPass {
29*9880d681SAndroid Build Coastguard Worker   const TargetInstrInfo *TII;
30*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI;
31*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo *MRI;
32*9880d681SAndroid Build Coastguard Worker 
33*9880d681SAndroid Build Coastguard Worker   SmallSetVector<MachineInstr*, 16> WorkList;
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker   void processImplicitDef(MachineInstr *MI);
36*9880d681SAndroid Build Coastguard Worker   bool canTurnIntoImplicitDef(MachineInstr *MI);
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker public:
39*9880d681SAndroid Build Coastguard Worker   static char ID;
40*9880d681SAndroid Build Coastguard Worker 
ProcessImplicitDefs()41*9880d681SAndroid Build Coastguard Worker   ProcessImplicitDefs() : MachineFunctionPass(ID) {
42*9880d681SAndroid Build Coastguard Worker     initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
43*9880d681SAndroid Build Coastguard Worker   }
44*9880d681SAndroid Build Coastguard Worker 
45*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &au) const override;
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &fn) override;
48*9880d681SAndroid Build Coastguard Worker };
49*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
50*9880d681SAndroid Build Coastguard Worker 
51*9880d681SAndroid Build Coastguard Worker char ProcessImplicitDefs::ID = 0;
52*9880d681SAndroid Build Coastguard Worker char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(ProcessImplicitDefs, "processimpdefs",
55*9880d681SAndroid Build Coastguard Worker                 "Process Implicit Definitions", false, false)
56*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(ProcessImplicitDefs, "processimpdefs",
57*9880d681SAndroid Build Coastguard Worker                 "Process Implicit Definitions", false, false)
58*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const59*9880d681SAndroid Build Coastguard Worker void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
60*9880d681SAndroid Build Coastguard Worker   AU.setPreservesCFG();
61*9880d681SAndroid Build Coastguard Worker   AU.addPreserved<AAResultsWrapperPass>();
62*9880d681SAndroid Build Coastguard Worker   MachineFunctionPass::getAnalysisUsage(AU);
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker 
canTurnIntoImplicitDef(MachineInstr * MI)65*9880d681SAndroid Build Coastguard Worker bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
66*9880d681SAndroid Build Coastguard Worker   if (!MI->isCopyLike() &&
67*9880d681SAndroid Build Coastguard Worker       !MI->isInsertSubreg() &&
68*9880d681SAndroid Build Coastguard Worker       !MI->isRegSequence() &&
69*9880d681SAndroid Build Coastguard Worker       !MI->isPHI())
70*9880d681SAndroid Build Coastguard Worker     return false;
71*9880d681SAndroid Build Coastguard Worker   for (const MachineOperand &MO : MI->operands())
72*9880d681SAndroid Build Coastguard Worker     if (MO.isReg() && MO.isUse() && MO.readsReg())
73*9880d681SAndroid Build Coastguard Worker       return false;
74*9880d681SAndroid Build Coastguard Worker   return true;
75*9880d681SAndroid Build Coastguard Worker }
76*9880d681SAndroid Build Coastguard Worker 
processImplicitDef(MachineInstr * MI)77*9880d681SAndroid Build Coastguard Worker void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
78*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Processing " << *MI);
79*9880d681SAndroid Build Coastguard Worker   unsigned Reg = MI->getOperand(0).getReg();
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker   if (TargetRegisterInfo::isVirtualRegister(Reg)) {
82*9880d681SAndroid Build Coastguard Worker     // For virtual registers, mark all uses as <undef>, and convert users to
83*9880d681SAndroid Build Coastguard Worker     // implicit-def when possible.
84*9880d681SAndroid Build Coastguard Worker     for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
85*9880d681SAndroid Build Coastguard Worker       MO.setIsUndef();
86*9880d681SAndroid Build Coastguard Worker       MachineInstr *UserMI = MO.getParent();
87*9880d681SAndroid Build Coastguard Worker       if (!canTurnIntoImplicitDef(UserMI))
88*9880d681SAndroid Build Coastguard Worker         continue;
89*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
90*9880d681SAndroid Build Coastguard Worker       UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
91*9880d681SAndroid Build Coastguard Worker       WorkList.insert(UserMI);
92*9880d681SAndroid Build Coastguard Worker     }
93*9880d681SAndroid Build Coastguard Worker     MI->eraseFromParent();
94*9880d681SAndroid Build Coastguard Worker     return;
95*9880d681SAndroid Build Coastguard Worker   }
96*9880d681SAndroid Build Coastguard Worker 
97*9880d681SAndroid Build Coastguard Worker   // This is a physreg implicit-def.
98*9880d681SAndroid Build Coastguard Worker   // Look for the first instruction to use or define an alias.
99*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
100*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
101*9880d681SAndroid Build Coastguard Worker   bool Found = false;
102*9880d681SAndroid Build Coastguard Worker   for (++UserMI; UserMI != UserE; ++UserMI) {
103*9880d681SAndroid Build Coastguard Worker     for (MachineOperand &MO : UserMI->operands()) {
104*9880d681SAndroid Build Coastguard Worker       if (!MO.isReg())
105*9880d681SAndroid Build Coastguard Worker         continue;
106*9880d681SAndroid Build Coastguard Worker       unsigned UserReg = MO.getReg();
107*9880d681SAndroid Build Coastguard Worker       if (!TargetRegisterInfo::isPhysicalRegister(UserReg) ||
108*9880d681SAndroid Build Coastguard Worker           !TRI->regsOverlap(Reg, UserReg))
109*9880d681SAndroid Build Coastguard Worker         continue;
110*9880d681SAndroid Build Coastguard Worker       // UserMI uses or redefines Reg. Set <undef> flags on all uses.
111*9880d681SAndroid Build Coastguard Worker       Found = true;
112*9880d681SAndroid Build Coastguard Worker       if (MO.isUse())
113*9880d681SAndroid Build Coastguard Worker         MO.setIsUndef();
114*9880d681SAndroid Build Coastguard Worker     }
115*9880d681SAndroid Build Coastguard Worker     if (Found)
116*9880d681SAndroid Build Coastguard Worker       break;
117*9880d681SAndroid Build Coastguard Worker   }
118*9880d681SAndroid Build Coastguard Worker 
119*9880d681SAndroid Build Coastguard Worker   // If we found the using MI, we can erase the IMPLICIT_DEF.
120*9880d681SAndroid Build Coastguard Worker   if (Found) {
121*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Physreg user: " << *UserMI);
122*9880d681SAndroid Build Coastguard Worker     MI->eraseFromParent();
123*9880d681SAndroid Build Coastguard Worker     return;
124*9880d681SAndroid Build Coastguard Worker   }
125*9880d681SAndroid Build Coastguard Worker 
126*9880d681SAndroid Build Coastguard Worker   // Using instr wasn't found, it could be in another block.
127*9880d681SAndroid Build Coastguard Worker   // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
128*9880d681SAndroid Build Coastguard Worker   for (unsigned i = MI->getNumOperands() - 1; i; --i)
129*9880d681SAndroid Build Coastguard Worker     MI->RemoveOperand(i);
130*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Keeping physreg: " << *MI);
131*9880d681SAndroid Build Coastguard Worker }
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
134*9880d681SAndroid Build Coastguard Worker /// <undef> operands.
runOnMachineFunction(MachineFunction & MF)135*9880d681SAndroid Build Coastguard Worker bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
138*9880d681SAndroid Build Coastguard Worker                << "********** Function: " << MF.getName() << '\n');
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker   bool Changed = false;
141*9880d681SAndroid Build Coastguard Worker 
142*9880d681SAndroid Build Coastguard Worker   TII = MF.getSubtarget().getInstrInfo();
143*9880d681SAndroid Build Coastguard Worker   TRI = MF.getSubtarget().getRegisterInfo();
144*9880d681SAndroid Build Coastguard Worker   MRI = &MF.getRegInfo();
145*9880d681SAndroid Build Coastguard Worker   assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
146*9880d681SAndroid Build Coastguard Worker   assert(WorkList.empty() && "Inconsistent worklist state");
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker   for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end();
149*9880d681SAndroid Build Coastguard Worker        MFI != MFE; ++MFI) {
150*9880d681SAndroid Build Coastguard Worker     // Scan the basic block for implicit defs.
151*9880d681SAndroid Build Coastguard Worker     for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(),
152*9880d681SAndroid Build Coastguard Worker          MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI)
153*9880d681SAndroid Build Coastguard Worker       if (MBBI->isImplicitDef())
154*9880d681SAndroid Build Coastguard Worker         WorkList.insert(&*MBBI);
155*9880d681SAndroid Build Coastguard Worker 
156*9880d681SAndroid Build Coastguard Worker     if (WorkList.empty())
157*9880d681SAndroid Build Coastguard Worker       continue;
158*9880d681SAndroid Build Coastguard Worker 
159*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "BB#" << MFI->getNumber() << " has " << WorkList.size()
160*9880d681SAndroid Build Coastguard Worker                  << " implicit defs.\n");
161*9880d681SAndroid Build Coastguard Worker     Changed = true;
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker     // Drain the WorkList to recursively process any new implicit defs.
164*9880d681SAndroid Build Coastguard Worker     do processImplicitDef(WorkList.pop_back_val());
165*9880d681SAndroid Build Coastguard Worker     while (!WorkList.empty());
166*9880d681SAndroid Build Coastguard Worker   }
167*9880d681SAndroid Build Coastguard Worker   return Changed;
168*9880d681SAndroid Build Coastguard Worker }
169