Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 1 | //===---------------------- 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 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/SetVector.h" |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/AliasAnalysis.h" |
Jakob Stoklund Olesen | a127fc7 | 2012-06-22 22:27:36 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineInstr.h" |
| 14 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 15 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "processimpdefs" |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 24 | |
Jakob Stoklund Olesen | a127fc7 | 2012-06-22 22:27:36 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | /// Process IMPLICIT_DEF instructions and make sure there is one implicit_def |
| 27 | /// for each use. Add isUndef marker to implicit_def defs and their uses. |
| 28 | class ProcessImplicitDefs : public MachineFunctionPass { |
| 29 | const TargetInstrInfo *TII; |
| 30 | const TargetRegisterInfo *TRI; |
| 31 | MachineRegisterInfo *MRI; |
Jakob Stoklund Olesen | a127fc7 | 2012-06-22 22:27:36 +0000 | [diff] [blame] | 32 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 33 | SmallSetVector<MachineInstr*, 16> WorkList; |
| 34 | |
| 35 | void processImplicitDef(MachineInstr *MI); |
| 36 | bool canTurnIntoImplicitDef(MachineInstr *MI); |
Jakob Stoklund Olesen | a127fc7 | 2012-06-22 22:27:36 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
| 39 | static char ID; |
| 40 | |
| 41 | ProcessImplicitDefs() : MachineFunctionPass(ID) { |
| 42 | initializeProcessImplicitDefsPass(*PassRegistry::getPassRegistry()); |
| 43 | } |
| 44 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 45 | void getAnalysisUsage(AnalysisUsage &au) const override; |
Jakob Stoklund Olesen | a127fc7 | 2012-06-22 22:27:36 +0000 | [diff] [blame] | 46 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 47 | bool runOnMachineFunction(MachineFunction &fn) override; |
Jakob Stoklund Olesen | a127fc7 | 2012-06-22 22:27:36 +0000 | [diff] [blame] | 48 | }; |
| 49 | } // end anonymous namespace |
| 50 | |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 51 | char ProcessImplicitDefs::ID = 0; |
Andrew Trick | d3f8fe8 | 2012-02-10 04:10:36 +0000 | [diff] [blame] | 52 | char &llvm::ProcessImplicitDefsID = ProcessImplicitDefs::ID; |
| 53 | |
Matthias Braun | 1527baa | 2017-05-25 21:26:32 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS(ProcessImplicitDefs, DEBUG_TYPE, |
Cameron Zwarich | 329cd49 | 2010-12-29 11:49:10 +0000 | [diff] [blame] | 55 | "Process Implicit Definitions", false, false) |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 56 | |
| 57 | void ProcessImplicitDefs::getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.setPreservesCFG(); |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 59 | AU.addPreserved<AAResultsWrapperPass>(); |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 60 | MachineFunctionPass::getAnalysisUsage(AU); |
| 61 | } |
| 62 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 63 | bool ProcessImplicitDefs::canTurnIntoImplicitDef(MachineInstr *MI) { |
| 64 | if (!MI->isCopyLike() && |
| 65 | !MI->isInsertSubreg() && |
| 66 | !MI->isRegSequence() && |
| 67 | !MI->isPHI()) |
Evan Cheng | d542414 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 68 | return false; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 69 | for (const MachineOperand &MO : MI->operands()) |
| 70 | if (MO.isReg() && MO.isUse() && MO.readsReg()) |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 71 | return false; |
| 72 | return true; |
Evan Cheng | d542414 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 75 | void ProcessImplicitDefs::processImplicitDef(MachineInstr *MI) { |
| 76 | DEBUG(dbgs() << "Processing " << *MI); |
| 77 | unsigned Reg = MI->getOperand(0).getReg(); |
| 78 | |
| 79 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | c9d5c0f | 2013-10-04 16:52:58 +0000 | [diff] [blame] | 80 | // For virtual registers, mark all uses as <undef>, and convert users to |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 81 | // implicit-def when possible. |
Owen Anderson | b36376e | 2014-03-17 19:36:09 +0000 | [diff] [blame] | 82 | for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 83 | MO.setIsUndef(); |
| 84 | MachineInstr *UserMI = MO.getParent(); |
| 85 | if (!canTurnIntoImplicitDef(UserMI)) |
| 86 | continue; |
| 87 | DEBUG(dbgs() << "Converting to IMPLICIT_DEF: " << *UserMI); |
| 88 | UserMI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF)); |
| 89 | WorkList.insert(UserMI); |
| 90 | } |
| 91 | MI->eraseFromParent(); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // This is a physreg implicit-def. |
| 96 | // Look for the first instruction to use or define an alias. |
Duncan P. N. Exon Smith | c5b668d | 2016-02-22 20:49:58 +0000 | [diff] [blame] | 97 | MachineBasicBlock::instr_iterator UserMI = MI->getIterator(); |
| 98 | MachineBasicBlock::instr_iterator UserE = MI->getParent()->instr_end(); |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 99 | bool Found = false; |
| 100 | for (++UserMI; UserMI != UserE; ++UserMI) { |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 101 | for (MachineOperand &MO : UserMI->operands()) { |
| 102 | if (!MO.isReg()) |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 103 | continue; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 104 | unsigned UserReg = MO.getReg(); |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 105 | if (!TargetRegisterInfo::isPhysicalRegister(UserReg) || |
| 106 | !TRI->regsOverlap(Reg, UserReg)) |
| 107 | continue; |
| 108 | // UserMI uses or redefines Reg. Set <undef> flags on all uses. |
| 109 | Found = true; |
Matthias Braun | e41e146 | 2015-05-29 02:56:46 +0000 | [diff] [blame] | 110 | if (MO.isUse()) |
| 111 | MO.setIsUndef(); |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 112 | } |
| 113 | if (Found) |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | // If we found the using MI, we can erase the IMPLICIT_DEF. |
| 118 | if (Found) { |
| 119 | DEBUG(dbgs() << "Physreg user: " << *UserMI); |
| 120 | MI->eraseFromParent(); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // Using instr wasn't found, it could be in another block. |
| 125 | // Leave the physreg IMPLICIT_DEF, but trim any extra operands. |
| 126 | for (unsigned i = MI->getNumOperands() - 1; i; --i) |
| 127 | MI->RemoveOperand(i); |
| 128 | DEBUG(dbgs() << "Keeping physreg: " << *MI); |
| 129 | } |
| 130 | |
| 131 | /// processImplicitDefs - Process IMPLICIT_DEF instructions and turn them into |
| 132 | /// <undef> operands. |
| 133 | bool ProcessImplicitDefs::runOnMachineFunction(MachineFunction &MF) { |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 134 | |
David Greene | d7c266b | 2010-01-05 01:24:28 +0000 | [diff] [blame] | 135 | DEBUG(dbgs() << "********** PROCESS IMPLICIT DEFS **********\n" |
David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 136 | << "********** Function: " << MF.getName() << '\n'); |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 137 | |
| 138 | bool Changed = false; |
| 139 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 140 | TII = MF.getSubtarget().getInstrInfo(); |
| 141 | TRI = MF.getSubtarget().getRegisterInfo(); |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 142 | MRI = &MF.getRegInfo(); |
| 143 | assert(MRI->isSSA() && "ProcessImplicitDefs only works on SSA form."); |
| 144 | assert(WorkList.empty() && "Inconsistent worklist state"); |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 145 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 146 | for (MachineFunction::iterator MFI = MF.begin(), MFE = MF.end(); |
| 147 | MFI != MFE; ++MFI) { |
| 148 | // Scan the basic block for implicit defs. |
| 149 | for (MachineBasicBlock::instr_iterator MBBI = MFI->instr_begin(), |
| 150 | MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) |
| 151 | if (MBBI->isImplicitDef()) |
Duncan P. N. Exon Smith | 6e98cd3 | 2015-10-09 21:08:19 +0000 | [diff] [blame] | 152 | WorkList.insert(&*MBBI); |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 153 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 154 | if (WorkList.empty()) |
| 155 | continue; |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 156 | |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 157 | DEBUG(dbgs() << printMBBReference(*MFI) << " has " << WorkList.size() |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 158 | << " implicit defs.\n"); |
| 159 | Changed = true; |
Jakob Stoklund Olesen | b16081c | 2011-07-28 21:38:51 +0000 | [diff] [blame] | 160 | |
Jakob Stoklund Olesen | eb49566 | 2012-06-25 18:12:18 +0000 | [diff] [blame] | 161 | // Drain the WorkList to recursively process any new implicit defs. |
| 162 | do processImplicitDef(WorkList.pop_back_val()); |
| 163 | while (!WorkList.empty()); |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 164 | } |
Lang Hames | 05fb963 | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 165 | return Changed; |
| 166 | } |