xref: /src/contrib/llvm-project/llvm/lib/CodeGen/ProcessImplicitDefs.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
136bf506aSRoman Divacky //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
236bf506aSRoman Divacky //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
636bf506aSRoman Divacky //
736bf506aSRoman Divacky //===----------------------------------------------------------------------===//
836bf506aSRoman Divacky 
958b69754SDimitry Andric #include "llvm/ADT/SetVector.h"
1036bf506aSRoman Divacky #include "llvm/Analysis/AliasAnalysis.h"
1158b69754SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
1236bf506aSRoman Divacky #include "llvm/CodeGen/MachineInstr.h"
1336bf506aSRoman Divacky #include "llvm/CodeGen/MachineRegisterInfo.h"
14044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
15044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
16706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
17145449b1SDimitry Andric #include "llvm/Pass.h"
18145449b1SDimitry Andric #include "llvm/PassRegistry.h"
1936bf506aSRoman Divacky #include "llvm/Support/Debug.h"
2058b69754SDimitry Andric #include "llvm/Support/raw_ostream.h"
2136bf506aSRoman Divacky 
2236bf506aSRoman Divacky using namespace llvm;
2336bf506aSRoman Divacky 
24ab44ce3dSDimitry Andric #define DEBUG_TYPE "processimpdefs"
255ca98fd9SDimitry Andric 
2658b69754SDimitry Andric namespace {
2758b69754SDimitry Andric /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
2858b69754SDimitry Andric /// for each use. Add isUndef marker to implicit_def defs and their uses.
2958b69754SDimitry Andric class ProcessImplicitDefs : public MachineFunctionPass {
307fa27ce4SDimitry Andric   const TargetInstrInfo *TII = nullptr;
317fa27ce4SDimitry Andric   const TargetRegisterInfo *TRI = nullptr;
327fa27ce4SDimitry Andric   MachineRegisterInfo *MRI = nullptr;
3358b69754SDimitry Andric 
3458b69754SDimitry Andric   SmallSetVector<MachineInstr*, 16> WorkList;
3558b69754SDimitry Andric 
3658b69754SDimitry Andric   void processImplicitDef(MachineInstr *MI);
3758b69754SDimitry Andric   bool canTurnIntoImplicitDef(MachineInstr *MI);
3858b69754SDimitry Andric 
3958b69754SDimitry Andric public:
4058b69754SDimitry Andric   static char ID;
4158b69754SDimitry Andric 
ProcessImplicitDefs()4258b69754SDimitry Andric   ProcessImplicitDefs() : MachineFunctionPass(ID) {
4358b69754SDimitry Andric     initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
4458b69754SDimitry Andric   }
4558b69754SDimitry Andric 
465ca98fd9SDimitry Andric   void getAnalysisUsage(AnalysisUsage &au) const override;
4758b69754SDimitry Andric 
48eb11fae6SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
49145449b1SDimitry Andric 
getRequiredProperties() const5008e8dd7bSDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
51145449b1SDimitry Andric     return MachineFunctionProperties().set(
52145449b1SDimitry Andric         MachineFunctionProperties::Property::IsSSA);
53145449b1SDimitry Andric   }
5458b69754SDimitry Andric };
5558b69754SDimitry Andric } // end anonymous namespace
5658b69754SDimitry Andric 
5736bf506aSRoman Divacky char ProcessImplicitDefs::ID = 0;
5863faed5bSDimitry Andric char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
5963faed5bSDimitry Andric 
60ab44ce3dSDimitry Andric INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE,
61cf099d11SDimitry Andric                 "Process Implicit Definitions", false, false)
6236bf506aSRoman Divacky 
getAnalysisUsage(AnalysisUsage & AU) const6336bf506aSRoman Divacky void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
6436bf506aSRoman Divacky   AU.setPreservesCFG();
65dd58ef01SDimitry Andric   AU.addPreserved<AAResultsWrapperPass>();
6636bf506aSRoman Divacky   MachineFunctionPass::getAnalysisUsage(AU);
6736bf506aSRoman Divacky }
6836bf506aSRoman Divacky 
canTurnIntoImplicitDef(MachineInstr * MI)6958b69754SDimitry Andric bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
7058b69754SDimitry Andric   if (!MI->isCopyLike() &&
7158b69754SDimitry Andric       !MI->isInsertSubreg() &&
7258b69754SDimitry Andric       !MI->isRegSequence() &&
7358b69754SDimitry Andric       !MI->isPHI())
74f3d15b0bSRoman Divacky     return false;
757fa27ce4SDimitry Andric   for (const MachineOperand &MO : MI->all_uses())
767fa27ce4SDimitry Andric     if (MO.readsReg())
7758b69754SDimitry Andric       return false;
78f3d15b0bSRoman Divacky   return true;
79f3d15b0bSRoman Divacky }
80f3d15b0bSRoman Divacky 
processImplicitDef(MachineInstr * MI)8158b69754SDimitry Andric void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
82eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Processing " << *MI);
831d5ae102SDimitry Andric   Register Reg = MI->getOperand(0).getReg();
8458b69754SDimitry Andric 
85e3b55780SDimitry Andric   if (Reg.isVirtual()) {
86f8af5cf6SDimitry Andric     // For virtual registers, mark all uses as <undef>, and convert users to
8758b69754SDimitry Andric     // implicit-def when possible.
885ca98fd9SDimitry Andric     for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
8958b69754SDimitry Andric       MO.setIsUndef();
9058b69754SDimitry Andric       MachineInstr *UserMI = MO.getParent();
9158b69754SDimitry Andric       if (!canTurnIntoImplicitDef(UserMI))
9258b69754SDimitry Andric         continue;
93eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
9458b69754SDimitry Andric       UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
9558b69754SDimitry Andric       WorkList.insert(UserMI);
9658b69754SDimitry Andric     }
9758b69754SDimitry Andric     MI->eraseFromParent();
9858b69754SDimitry Andric     return;
9958b69754SDimitry Andric   }
10058b69754SDimitry Andric 
10158b69754SDimitry Andric   // This is a physreg implicit-def.
10258b69754SDimitry Andric   // Look for the first instruction to use or define an alias.
103dd58ef01SDimitry Andric   MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
10458b69754SDimitry Andric   MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
10558b69754SDimitry Andric   bool Found = false;
10658b69754SDimitry Andric   for (++UserMI; UserMI != UserE; ++UserMI) {
10785d8b2bbSDimitry Andric     for (MachineOperand &MO : UserMI->operands()) {
10885d8b2bbSDimitry Andric       if (!MO.isReg())
10958b69754SDimitry Andric         continue;
1101d5ae102SDimitry Andric       Register UserReg = MO.getReg();
111e3b55780SDimitry Andric       if (!UserReg.isPhysical() || !TRI->regsOverlap(Reg, UserReg))
11258b69754SDimitry Andric         continue;
11358b69754SDimitry Andric       // UserMI uses or redefines Reg. Set <undef> flags on all uses.
11458b69754SDimitry Andric       Found = true;
11585d8b2bbSDimitry Andric       if (MO.isUse())
11685d8b2bbSDimitry Andric         MO.setIsUndef();
11758b69754SDimitry Andric     }
11858b69754SDimitry Andric     if (Found)
11958b69754SDimitry Andric       break;
12058b69754SDimitry Andric   }
12158b69754SDimitry Andric 
12258b69754SDimitry Andric   // If we found the using MI, we can erase the IMPLICIT_DEF.
12358b69754SDimitry Andric   if (Found) {
124eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "Physreg user: " << *UserMI);
12558b69754SDimitry Andric     MI->eraseFromParent();
12658b69754SDimitry Andric     return;
12758b69754SDimitry Andric   }
12858b69754SDimitry Andric 
12958b69754SDimitry Andric   // Using instr wasn't found, it could be in another block.
13058b69754SDimitry Andric   // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
13158b69754SDimitry Andric   for (unsigned i = MI->getNumOperands() - 1; i; --i)
132145449b1SDimitry Andric     MI->removeOperand(i);
133eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "Keeping physreg: " << *MI);
13458b69754SDimitry Andric }
13558b69754SDimitry Andric 
13658b69754SDimitry Andric /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
13758b69754SDimitry Andric /// <undef> operands.
runOnMachineFunction(MachineFunction & MF)13858b69754SDimitry Andric bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
13936bf506aSRoman Divacky 
140eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
141522600a2SDimitry Andric                     << "********** Function: " << MF.getName() << '\n');
14236bf506aSRoman Divacky 
14336bf506aSRoman Divacky   bool Changed = false;
14436bf506aSRoman Divacky 
14567c32a98SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
14667c32a98SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
14758b69754SDimitry Andric   MRI = &MF.getRegInfo();
14858b69754SDimitry Andric   assert(WorkList.empty() && "Inconsistent worklist state");
14936bf506aSRoman Divacky 
150344a3780SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
15158b69754SDimitry Andric     // Scan the basic block for implicit defs.
152344a3780SDimitry Andric     for (MachineInstr &MI : MBB)
153344a3780SDimitry Andric       if (MI.isImplicitDef())
154344a3780SDimitry Andric         WorkList.insert(&MI);
15536bf506aSRoman Divacky 
15658b69754SDimitry Andric     if (WorkList.empty())
157abdf259dSRoman Divacky       continue;
15836bf506aSRoman Divacky 
159344a3780SDimitry Andric     LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
16058b69754SDimitry Andric                       << " implicit defs.\n");
16136bf506aSRoman Divacky     Changed = true;
16230815c53SDimitry Andric 
16358b69754SDimitry Andric     // Drain the WorkList to recursively process any new implicit defs.
16458b69754SDimitry Andric     do processImplicitDef(WorkList.pop_back_val());
16558b69754SDimitry Andric     while (!WorkList.empty());
16636bf506aSRoman Divacky   }
16736bf506aSRoman Divacky   return Changed;
16836bf506aSRoman Divacky }
169