Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1 | //===-- lib/CodeGen/MachineInstrBundle.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 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallSet.h" |
| 12 | #include "llvm/ADT/SmallVector.h" |
| 13 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 15 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 18 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetMachine.h" |
Benjamin Kramer | 14aae01 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 20 | #include <utility> |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | class UnpackMachineBundles : public MachineFunctionPass { |
| 25 | public: |
| 26 | static char ID; // Pass identification |
Matthias Braun | cc92828 | 2016-10-24 23:23:02 +0000 | [diff] [blame] | 27 | UnpackMachineBundles( |
| 28 | std::function<bool(const MachineFunction &)> Ftor = nullptr) |
Benjamin Kramer | 14aae01 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 29 | : MachineFunctionPass(ID), PredicateFtor(std::move(Ftor)) { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 30 | initializeUnpackMachineBundlesPass(*PassRegistry::getPassRegistry()); |
| 31 | } |
| 32 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 33 | bool runOnMachineFunction(MachineFunction &MF) override; |
Akira Hatanaka | fa6bc2e | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 34 | |
| 35 | private: |
Matthias Braun | cc92828 | 2016-10-24 23:23:02 +0000 | [diff] [blame] | 36 | std::function<bool(const MachineFunction &)> PredicateFtor; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 37 | }; |
| 38 | } // end anonymous namespace |
| 39 | |
| 40 | char UnpackMachineBundles::ID = 0; |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 41 | char &llvm::UnpackMachineBundlesID = UnpackMachineBundles::ID; |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 42 | INITIALIZE_PASS(UnpackMachineBundles, "unpack-mi-bundles", |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 43 | "Unpack machine instruction bundles", false, false) |
| 44 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 45 | bool UnpackMachineBundles::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | cc92828 | 2016-10-24 23:23:02 +0000 | [diff] [blame] | 46 | if (PredicateFtor && !PredicateFtor(MF)) |
Akira Hatanaka | fa6bc2e | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 47 | return false; |
| 48 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 49 | bool Changed = false; |
| 50 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 51 | MachineBasicBlock *MBB = &*I; |
| 52 | |
| 53 | for (MachineBasicBlock::instr_iterator MII = MBB->instr_begin(), |
| 54 | MIE = MBB->instr_end(); MII != MIE; ) { |
| 55 | MachineInstr *MI = &*MII; |
| 56 | |
| 57 | // Remove BUNDLE instruction and the InsideBundle flags from bundled |
| 58 | // instructions. |
| 59 | if (MI->isBundle()) { |
Jakob Stoklund Olesen | caf946e | 2012-12-13 23:23:46 +0000 | [diff] [blame] | 60 | while (++MII != MIE && MII->isBundledWithPred()) { |
| 61 | MII->unbundleFromPred(); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 62 | for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) { |
| 63 | MachineOperand &MO = MII->getOperand(i); |
| 64 | if (MO.isReg() && MO.isInternalRead()) |
| 65 | MO.setIsInternalRead(false); |
| 66 | } |
| 67 | } |
| 68 | MI->eraseFromParent(); |
| 69 | |
| 70 | Changed = true; |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | ++MII; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return Changed; |
| 79 | } |
| 80 | |
Akira Hatanaka | fa6bc2e | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 81 | FunctionPass * |
Matthias Braun | cc92828 | 2016-10-24 23:23:02 +0000 | [diff] [blame] | 82 | llvm::createUnpackMachineBundles( |
| 83 | std::function<bool(const MachineFunction &)> Ftor) { |
Benjamin Kramer | 022a899 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 84 | return new UnpackMachineBundles(std::move(Ftor)); |
Akira Hatanaka | fa6bc2e | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 85 | } |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 86 | |
| 87 | namespace { |
| 88 | class FinalizeMachineBundles : public MachineFunctionPass { |
| 89 | public: |
| 90 | static char ID; // Pass identification |
| 91 | FinalizeMachineBundles() : MachineFunctionPass(ID) { |
| 92 | initializeFinalizeMachineBundlesPass(*PassRegistry::getPassRegistry()); |
| 93 | } |
| 94 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 95 | bool runOnMachineFunction(MachineFunction &MF) override; |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 96 | }; |
| 97 | } // end anonymous namespace |
| 98 | |
| 99 | char FinalizeMachineBundles::ID = 0; |
Andrew Trick | 1dd8c85 | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 100 | char &llvm::FinalizeMachineBundlesID = FinalizeMachineBundles::ID; |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 101 | INITIALIZE_PASS(FinalizeMachineBundles, "finalize-mi-bundles", |
| 102 | "Finalize machine instruction bundles", false, false) |
| 103 | |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 104 | bool FinalizeMachineBundles::runOnMachineFunction(MachineFunction &MF) { |
| 105 | return llvm::finalizeBundles(MF); |
| 106 | } |
| 107 | |
Bjorn Pettersson | 6a7eb84 | 2018-08-21 10:59:50 +0000 | [diff] [blame] | 108 | /// Return the first found DebugLoc that has a DILocation, given a range of |
| 109 | /// instructions. The search range is from FirstMI to LastMI (exclusive). If no |
| 110 | /// DILocation is found, then an empty location is returned. |
| 111 | static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, |
| 112 | MachineBasicBlock::instr_iterator LastMI) { |
| 113 | for (auto MII = FirstMI; MII != LastMI; ++MII) |
| 114 | if (MII->getDebugLoc().get()) |
| 115 | return MII->getDebugLoc(); |
| 116 | return DebugLoc(); |
| 117 | } |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 118 | |
Evan Cheng | 9b15971 | 2012-01-19 00:06:10 +0000 | [diff] [blame] | 119 | /// finalizeBundle - Finalize a machine instruction bundle which includes |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 120 | /// a sequence of instructions starting from FirstMI to LastMI (exclusive). |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 121 | /// This routine adds a BUNDLE instruction to represent the bundle, it adds |
| 122 | /// IsInternalRead markers to MachineOperands which are defined inside the |
| 123 | /// bundle, and it copies externally visible defs and uses to the BUNDLE |
| 124 | /// instruction. |
Evan Cheng | 9b15971 | 2012-01-19 00:06:10 +0000 | [diff] [blame] | 125 | void llvm::finalizeBundle(MachineBasicBlock &MBB, |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 126 | MachineBasicBlock::instr_iterator FirstMI, |
| 127 | MachineBasicBlock::instr_iterator LastMI) { |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 128 | assert(FirstMI != LastMI && "Empty bundle?"); |
Jakob Stoklund Olesen | caf946e | 2012-12-13 23:23:46 +0000 | [diff] [blame] | 129 | MIBundleBuilder Bundle(MBB, FirstMI, LastMI); |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 130 | |
Eric Christopher | c026db7 | 2014-10-14 06:26:55 +0000 | [diff] [blame] | 131 | MachineFunction &MF = *MBB.getParent(); |
| 132 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
| 133 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 134 | |
Eric Christopher | c026db7 | 2014-10-14 06:26:55 +0000 | [diff] [blame] | 135 | MachineInstrBuilder MIB = |
Bjorn Pettersson | 6a7eb84 | 2018-08-21 10:59:50 +0000 | [diff] [blame] | 136 | BuildMI(MF, getDebugLoc(FirstMI, LastMI), TII->get(TargetOpcode::BUNDLE)); |
Jakob Stoklund Olesen | caf946e | 2012-12-13 23:23:46 +0000 | [diff] [blame] | 137 | Bundle.prepend(MIB); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 138 | |
Michael Ilseman | 8dcc994 | 2012-09-17 18:31:15 +0000 | [diff] [blame] | 139 | SmallVector<unsigned, 32> LocalDefs; |
| 140 | SmallSet<unsigned, 32> LocalDefSet; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 141 | SmallSet<unsigned, 8> DeadDefSet; |
Michael Ilseman | 8dcc994 | 2012-09-17 18:31:15 +0000 | [diff] [blame] | 142 | SmallSet<unsigned, 16> KilledDefSet; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 143 | SmallVector<unsigned, 8> ExternUses; |
| 144 | SmallSet<unsigned, 8> ExternUseSet; |
| 145 | SmallSet<unsigned, 8> KilledUseSet; |
| 146 | SmallSet<unsigned, 8> UndefUseSet; |
| 147 | SmallVector<MachineOperand*, 4> Defs; |
Bjorn Pettersson | 9a9829f | 2018-08-25 11:26:17 +0000 | [diff] [blame] | 148 | for (auto MII = FirstMI; MII != LastMI; ++MII) { |
| 149 | for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) { |
| 150 | MachineOperand &MO = MII->getOperand(i); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 151 | if (!MO.isReg()) |
| 152 | continue; |
| 153 | if (MO.isDef()) { |
| 154 | Defs.push_back(&MO); |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | unsigned Reg = MO.getReg(); |
| 159 | if (!Reg) |
| 160 | continue; |
| 161 | assert(TargetRegisterInfo::isPhysicalRegister(Reg)); |
| 162 | if (LocalDefSet.count(Reg)) { |
| 163 | MO.setIsInternalRead(); |
| 164 | if (MO.isKill()) |
| 165 | // Internal def is now killed. |
| 166 | KilledDefSet.insert(Reg); |
| 167 | } else { |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 168 | if (ExternUseSet.insert(Reg).second) { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 169 | ExternUses.push_back(Reg); |
| 170 | if (MO.isUndef()) |
| 171 | UndefUseSet.insert(Reg); |
| 172 | } |
| 173 | if (MO.isKill()) |
| 174 | // External def is now killed. |
| 175 | KilledUseSet.insert(Reg); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | for (unsigned i = 0, e = Defs.size(); i != e; ++i) { |
| 180 | MachineOperand &MO = *Defs[i]; |
| 181 | unsigned Reg = MO.getReg(); |
| 182 | if (!Reg) |
| 183 | continue; |
| 184 | |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 185 | if (LocalDefSet.insert(Reg).second) { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 186 | LocalDefs.push_back(Reg); |
| 187 | if (MO.isDead()) { |
| 188 | DeadDefSet.insert(Reg); |
| 189 | } |
| 190 | } else { |
| 191 | // Re-defined inside the bundle, it's no longer killed. |
| 192 | KilledDefSet.erase(Reg); |
| 193 | if (!MO.isDead()) |
| 194 | // Previously defined but dead. |
| 195 | DeadDefSet.erase(Reg); |
| 196 | } |
| 197 | |
| 198 | if (!MO.isDead()) { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 199 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 200 | unsigned SubReg = *SubRegs; |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 201 | if (LocalDefSet.insert(SubReg).second) |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 202 | LocalDefs.push_back(SubReg); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 207 | Defs.clear(); |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 208 | } |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 209 | |
Michael Ilseman | 8dcc994 | 2012-09-17 18:31:15 +0000 | [diff] [blame] | 210 | SmallSet<unsigned, 32> Added; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 211 | for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) { |
| 212 | unsigned Reg = LocalDefs[i]; |
David Blaikie | 5401ba7 | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 213 | if (Added.insert(Reg).second) { |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 214 | // If it's not live beyond end of the bundle, mark it dead. |
| 215 | bool isDead = DeadDefSet.count(Reg) || KilledDefSet.count(Reg); |
| 216 | MIB.addReg(Reg, getDefRegState(true) | getDeadRegState(isDead) | |
| 217 | getImplRegState(true)); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | for (unsigned i = 0, e = ExternUses.size(); i != e; ++i) { |
| 222 | unsigned Reg = ExternUses[i]; |
| 223 | bool isKill = KilledUseSet.count(Reg); |
| 224 | bool isUndef = UndefUseSet.count(Reg); |
| 225 | MIB.addReg(Reg, getKillRegState(isKill) | getUndefRegState(isUndef) | |
| 226 | getImplRegState(true)); |
| 227 | } |
Bjorn Pettersson | 9a9829f | 2018-08-25 11:26:17 +0000 | [diff] [blame] | 228 | |
| 229 | // Set FrameSetup/FrameDestroy for the bundle. If any of the instructions got |
| 230 | // the property, then also set it on the bundle. |
| 231 | for (auto MII = FirstMI; MII != LastMI; ++MII) { |
| 232 | if (MII->getFlag(MachineInstr::FrameSetup)) |
| 233 | MIB.setMIFlag(MachineInstr::FrameSetup); |
| 234 | if (MII->getFlag(MachineInstr::FrameDestroy)) |
| 235 | MIB.setMIFlag(MachineInstr::FrameDestroy); |
| 236 | } |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 237 | } |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 238 | |
| 239 | /// finalizeBundle - Same functionality as the previous finalizeBundle except |
| 240 | /// the last instruction in the bundle is not provided as an input. This is |
| 241 | /// used in cases where bundles are pre-determined by marking instructions |
Evan Cheng | a2e435c | 2012-01-19 06:13:10 +0000 | [diff] [blame] | 242 | /// with 'InsideBundle' marker. It returns the MBB instruction iterator that |
| 243 | /// points to the end of the bundle. |
| 244 | MachineBasicBlock::instr_iterator |
| 245 | llvm::finalizeBundle(MachineBasicBlock &MBB, |
| 246 | MachineBasicBlock::instr_iterator FirstMI) { |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 247 | MachineBasicBlock::instr_iterator E = MBB.instr_end(); |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 248 | MachineBasicBlock::instr_iterator LastMI = std::next(FirstMI); |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 249 | while (LastMI != E && LastMI->isInsideBundle()) |
| 250 | ++LastMI; |
| 251 | finalizeBundle(MBB, FirstMI, LastMI); |
Evan Cheng | a2e435c | 2012-01-19 06:13:10 +0000 | [diff] [blame] | 252 | return LastMI; |
Evan Cheng | bca15f9 | 2012-01-19 00:46:06 +0000 | [diff] [blame] | 253 | } |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 254 | |
| 255 | /// finalizeBundles - Finalize instruction bundles in the specified |
| 256 | /// MachineFunction. Return true if any bundles are finalized. |
| 257 | bool llvm::finalizeBundles(MachineFunction &MF) { |
| 258 | bool Changed = false; |
| 259 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 260 | MachineBasicBlock &MBB = *I; |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 261 | MachineBasicBlock::instr_iterator MII = MBB.instr_begin(); |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 262 | MachineBasicBlock::instr_iterator MIE = MBB.instr_end(); |
Evan Cheng | 8250d73 | 2012-03-06 02:00:52 +0000 | [diff] [blame] | 263 | if (MII == MIE) |
| 264 | continue; |
Jakob Stoklund Olesen | 73a853f | 2013-01-04 22:17:31 +0000 | [diff] [blame] | 265 | assert(!MII->isInsideBundle() && |
| 266 | "First instr cannot be inside bundle before finalization!"); |
| 267 | |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 268 | for (++MII; MII != MIE; ) { |
| 269 | if (!MII->isInsideBundle()) |
| 270 | ++MII; |
| 271 | else { |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 272 | MII = finalizeBundle(MBB, std::prev(MII)); |
Evan Cheng | ef2887d | 2012-01-19 07:47:03 +0000 | [diff] [blame] | 273 | Changed = true; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return Changed; |
| 279 | } |
Jakob Stoklund Olesen | a36fe73 | 2012-02-29 01:40:37 +0000 | [diff] [blame] | 280 | |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | // MachineOperand iterator |
| 283 | //===----------------------------------------------------------------------===// |
| 284 | |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 285 | MachineOperandIteratorBase::VirtRegInfo |
Jakob Stoklund Olesen | a36fe73 | 2012-02-29 01:40:37 +0000 | [diff] [blame] | 286 | MachineOperandIteratorBase::analyzeVirtReg(unsigned Reg, |
| 287 | SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops) { |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 288 | VirtRegInfo RI = { false, false, false }; |
Jakob Stoklund Olesen | a36fe73 | 2012-02-29 01:40:37 +0000 | [diff] [blame] | 289 | for(; isValid(); ++*this) { |
| 290 | MachineOperand &MO = deref(); |
| 291 | if (!MO.isReg() || MO.getReg() != Reg) |
| 292 | continue; |
| 293 | |
| 294 | // Remember each (MI, OpNo) that refers to Reg. |
| 295 | if (Ops) |
| 296 | Ops->push_back(std::make_pair(MO.getParent(), getOperandNo())); |
| 297 | |
| 298 | // Both defs and uses can read virtual registers. |
| 299 | if (MO.readsReg()) { |
| 300 | RI.Reads = true; |
| 301 | if (MO.isDef()) |
| 302 | RI.Tied = true; |
| 303 | } |
| 304 | |
| 305 | // Only defs can write. |
| 306 | if (MO.isDef()) |
| 307 | RI.Writes = true; |
| 308 | else if (!RI.Tied && MO.getParent()->isRegTiedToDefOperand(getOperandNo())) |
| 309 | RI.Tied = true; |
| 310 | } |
| 311 | return RI; |
| 312 | } |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 313 | |
| 314 | MachineOperandIteratorBase::PhysRegInfo |
| 315 | MachineOperandIteratorBase::analyzePhysReg(unsigned Reg, |
| 316 | const TargetRegisterInfo *TRI) { |
| 317 | bool AllDefsDead = true; |
Quentin Colombet | b62e247 | 2016-04-26 23:14:24 +0000 | [diff] [blame] | 318 | PhysRegInfo PRI = {false, false, false, false, false, false, false, false}; |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 319 | |
| 320 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 321 | "analyzePhysReg not given a physical register!"); |
| 322 | for (; isValid(); ++*this) { |
| 323 | MachineOperand &MO = deref(); |
| 324 | |
Matthias Braun | f43272c | 2015-12-11 19:42:09 +0000 | [diff] [blame] | 325 | if (MO.isRegMask() && MO.clobbersPhysReg(Reg)) { |
| 326 | PRI.Clobbered = true; |
| 327 | continue; |
| 328 | } |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 329 | |
| 330 | if (!MO.isReg()) |
| 331 | continue; |
| 332 | |
| 333 | unsigned MOReg = MO.getReg(); |
| 334 | if (!MOReg || !TargetRegisterInfo::isPhysicalRegister(MOReg)) |
| 335 | continue; |
| 336 | |
Matthias Braun | f43272c | 2015-12-11 19:42:09 +0000 | [diff] [blame] | 337 | if (!TRI->regsOverlap(MOReg, Reg)) |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 338 | continue; |
| 339 | |
Matthias Braun | 96b46d1 | 2016-01-05 00:45:35 +0000 | [diff] [blame] | 340 | bool Covered = TRI->isSuperRegisterEq(Reg, MOReg); |
Matthias Braun | f43272c | 2015-12-11 19:42:09 +0000 | [diff] [blame] | 341 | if (MO.readsReg()) { |
| 342 | PRI.Read = true; |
| 343 | if (Covered) { |
| 344 | PRI.FullyRead = true; |
| 345 | if (MO.isKill()) |
| 346 | PRI.Killed = true; |
| 347 | } |
| 348 | } else if (MO.isDef()) { |
| 349 | PRI.Defined = true; |
| 350 | if (Covered) |
| 351 | PRI.FullyDefined = true; |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 352 | if (!MO.isDead()) |
| 353 | AllDefsDead = false; |
| 354 | } |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Quentin Colombet | b62e247 | 2016-04-26 23:14:24 +0000 | [diff] [blame] | 357 | if (AllDefsDead) { |
| 358 | if (PRI.FullyDefined || PRI.Clobbered) |
| 359 | PRI.DeadDef = true; |
Quentin Colombet | 7ec26d4 | 2016-04-27 00:16:29 +0000 | [diff] [blame] | 360 | else if (PRI.Defined) |
Quentin Colombet | b62e247 | 2016-04-26 23:14:24 +0000 | [diff] [blame] | 361 | PRI.PartialDeadDef = true; |
| 362 | } |
James Molloy | b17cf29 | 2012-09-12 10:03:31 +0000 | [diff] [blame] | 363 | |
| 364 | return PRI; |
| 365 | } |