10b57cec5SDimitry Andric //===---------------------- ProcessImplicitDefs.cpp -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
100b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
110b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
120b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
130b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
140b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
150b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
17480093f4SDimitry Andric #include "llvm/InitializePasses.h"
180b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
190b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric #define DEBUG_TYPE "processimpdefs"
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric namespace {
260b57cec5SDimitry Andric /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def
270b57cec5SDimitry Andric /// for each use. Add isUndef marker to implicit_def defs and their uses.
280b57cec5SDimitry Andric class ProcessImplicitDefs : public MachineFunctionPass {
290b57cec5SDimitry Andric const TargetInstrInfo *TII;
300b57cec5SDimitry Andric const TargetRegisterInfo *TRI;
310b57cec5SDimitry Andric MachineRegisterInfo *MRI;
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric SmallSetVector<MachineInstr*, 16> WorkList;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric void processImplicitDef(MachineInstr *MI);
360b57cec5SDimitry Andric bool canTurnIntoImplicitDef(MachineInstr *MI);
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric public:
390b57cec5SDimitry Andric static char ID;
400b57cec5SDimitry Andric
ProcessImplicitDefs()410b57cec5SDimitry Andric ProcessImplicitDefs() : MachineFunctionPass(ID) {
420b57cec5SDimitry Andric initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry());
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &au) const override;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
480b57cec5SDimitry Andric };
490b57cec5SDimitry Andric } // end anonymous namespace
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric char ProcessImplicitDefs::ID = 0;
520b57cec5SDimitry Andric char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID;
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE,
550b57cec5SDimitry Andric "Process Implicit Definitions", false, false)
560b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const570b57cec5SDimitry Andric void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const {
580b57cec5SDimitry Andric AU.setPreservesCFG();
590b57cec5SDimitry Andric AU.addPreserved<AAResultsWrapperPass>();
600b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
canTurnIntoImplicitDef(MachineInstr * MI)630b57cec5SDimitry Andric bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) {
640b57cec5SDimitry Andric if (!MI->isCopyLike() &&
650b57cec5SDimitry Andric !MI->isInsertSubreg() &&
660b57cec5SDimitry Andric !MI->isRegSequence() &&
670b57cec5SDimitry Andric !MI->isPHI())
680b57cec5SDimitry Andric return false;
690b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands())
700b57cec5SDimitry Andric if (MO.isReg() && MO.isUse() && MO.readsReg())
710b57cec5SDimitry Andric return false;
720b57cec5SDimitry Andric return true;
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric
processImplicitDef(MachineInstr * MI)750b57cec5SDimitry Andric void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) {
760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Processing " << *MI);
778bcb0991SDimitry Andric Register Reg = MI->getOperand(0).getReg();
780b57cec5SDimitry Andric
798bcb0991SDimitry Andric if (Register::isVirtualRegister(Reg)) {
800b57cec5SDimitry Andric // For virtual registers, mark all uses as <undef>, and convert users to
810b57cec5SDimitry Andric // implicit-def when possible.
820b57cec5SDimitry Andric for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) {
830b57cec5SDimitry Andric MO.setIsUndef();
840b57cec5SDimitry Andric MachineInstr *UserMI = MO.getParent();
850b57cec5SDimitry Andric if (!canTurnIntoImplicitDef(UserMI))
860b57cec5SDimitry Andric continue;
870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI);
880b57cec5SDimitry Andric UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF));
890b57cec5SDimitry Andric WorkList.insert(UserMI);
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric MI->eraseFromParent();
920b57cec5SDimitry Andric return;
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
950b57cec5SDimitry Andric // This is a physreg implicit-def.
960b57cec5SDimitry Andric // Look for the first instruction to use or define an alias.
970b57cec5SDimitry Andric MachineBasicBlock::instr_iterator UserMI = MI->getIterator();
980b57cec5SDimitry Andric MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end();
990b57cec5SDimitry Andric bool Found = false;
1000b57cec5SDimitry Andric for (++UserMI; UserMI != UserE; ++UserMI) {
1010b57cec5SDimitry Andric for (MachineOperand &MO : UserMI->operands()) {
1020b57cec5SDimitry Andric if (!MO.isReg())
1030b57cec5SDimitry Andric continue;
1048bcb0991SDimitry Andric Register UserReg = MO.getReg();
1058bcb0991SDimitry Andric if (!Register::isPhysicalRegister(UserReg) ||
1060b57cec5SDimitry Andric !TRI->regsOverlap(Reg, UserReg))
1070b57cec5SDimitry Andric continue;
1080b57cec5SDimitry Andric // UserMI uses or redefines Reg. Set <undef> flags on all uses.
1090b57cec5SDimitry Andric Found = true;
1100b57cec5SDimitry Andric if (MO.isUse())
1110b57cec5SDimitry Andric MO.setIsUndef();
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric if (Found)
1140b57cec5SDimitry Andric break;
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric // If we found the using MI, we can erase the IMPLICIT_DEF.
1180b57cec5SDimitry Andric if (Found) {
1190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Physreg user: " << *UserMI);
1200b57cec5SDimitry Andric MI->eraseFromParent();
1210b57cec5SDimitry Andric return;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric // Using instr wasn't found, it could be in another block.
1250b57cec5SDimitry Andric // Leave the physreg IMPLICIT_DEF, but trim any extra operands.
1260b57cec5SDimitry Andric for (unsigned i = MI->getNumOperands() - 1; i; --i)
1270b57cec5SDimitry Andric MI->RemoveOperand(i);
1280b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Keeping physreg: " << *MI);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into
1320b57cec5SDimitry Andric /// <undef> operands.
runOnMachineFunction(MachineFunction & MF)1330b57cec5SDimitry Andric bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) {
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n"
1360b57cec5SDimitry Andric << "********** Function: " << MF.getName() << '\n');
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric bool Changed = false;
1390b57cec5SDimitry Andric
1400b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo();
1410b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo();
1420b57cec5SDimitry Andric MRI = &MF.getRegInfo();
1430b57cec5SDimitry Andric assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form.");
1440b57cec5SDimitry Andric assert(WorkList.empty() && "Inconsistent worklist state");
1450b57cec5SDimitry Andric
146*5f7ddb14SDimitry Andric for (MachineBasicBlock &MBB : MF) {
1470b57cec5SDimitry Andric // Scan the basic block for implicit defs.
148*5f7ddb14SDimitry Andric for (MachineInstr &MI : MBB)
149*5f7ddb14SDimitry Andric if (MI.isImplicitDef())
150*5f7ddb14SDimitry Andric WorkList.insert(&MI);
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric if (WorkList.empty())
1530b57cec5SDimitry Andric continue;
1540b57cec5SDimitry Andric
155*5f7ddb14SDimitry Andric LLVM_DEBUG(dbgs() << printMBBReference(MBB) << " has " << WorkList.size()
1560b57cec5SDimitry Andric << " implicit defs.\n");
1570b57cec5SDimitry Andric Changed = true;
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andric // Drain the WorkList to recursively process any new implicit defs.
1600b57cec5SDimitry Andric do processImplicitDef(WorkList.pop_back_val());
1610b57cec5SDimitry Andric while (!WorkList.empty());
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric return Changed;
1640b57cec5SDimitry Andric }
165