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