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