Quentin Colombet | 8e8e85c | 2016-04-05 19:06:01 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/GlobalISel/RegBankSelect.cpp - RegBankSelect -*- C++ -*-==// |
| 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 | /// \file |
| 10 | /// This file implements the RegBankSelect class. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Quentin Colombet | ab8c21f | 2016-04-08 17:19:10 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/PostOrderIterator.h" |
Quentin Colombet | 8e8e85c | 2016-04-05 19:06:01 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/GlobalISel/RegisterBank.h" |
| 16 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Quentin Colombet | e16f561 | 2016-04-07 23:53:55 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetSubtargetInfo.h" |
Quentin Colombet | 8e8e85c | 2016-04-05 19:06:01 +0000 | [diff] [blame] | 19 | |
| 20 | #define DEBUG_TYPE "regbankselect" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | char RegBankSelect::ID = 0; |
| 25 | INITIALIZE_PASS(RegBankSelect, "regbankselect", |
| 26 | "Assign register bank of generic virtual registers", |
| 27 | false, false); |
| 28 | |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 29 | RegBankSelect::RegBankSelect() |
| 30 | : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr) { |
Quentin Colombet | 8e8e85c | 2016-04-05 19:06:01 +0000 | [diff] [blame] | 31 | initializeRegBankSelectPass(*PassRegistry::getPassRegistry()); |
| 32 | } |
| 33 | |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 34 | void RegBankSelect::init(MachineFunction &MF) { |
| 35 | RBI = MF.getSubtarget().getRegBankInfo(); |
| 36 | assert(RBI && "Cannot work without RegisterBankInfo"); |
| 37 | MRI = &MF.getRegInfo(); |
Quentin Colombet | aac71a4 | 2016-04-07 21:32:23 +0000 | [diff] [blame] | 38 | TRI = MF.getSubtarget().getRegisterInfo(); |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 39 | MIRBuilder.setMF(MF); |
| 40 | } |
| 41 | |
| 42 | bool RegBankSelect::assignmentMatch( |
| 43 | unsigned Reg, const RegisterBankInfo::ValueMapping &ValMapping) const { |
| 44 | // Each part of a break down needs to end up in a different register. |
| 45 | // In other word, Reg assignement does not match. |
| 46 | if (ValMapping.BreakDown.size() > 1) |
| 47 | return false; |
| 48 | |
Quentin Colombet | 6d6d6af | 2016-04-08 16:48:16 +0000 | [diff] [blame] | 49 | const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI); |
| 50 | const RegisterBank *DesiredRegBrank = ValMapping.BreakDown[0].RegBank; |
| 51 | DEBUG(dbgs() << "Does assignment already match: "; |
| 52 | if (CurRegBank) dbgs() << *CurRegBank; else dbgs() << "none"; |
| 53 | dbgs() << " against "; |
| 54 | assert(DesiredRegBrank && "The mapping must be valid"); |
| 55 | dbgs() << *DesiredRegBrank << '\n';); |
| 56 | return CurRegBank == DesiredRegBrank; |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | unsigned |
| 60 | RegBankSelect::repairReg(unsigned Reg, |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 61 | const RegisterBankInfo::ValueMapping &ValMapping, |
| 62 | MachineInstr &DefUseMI, bool IsDef) { |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 63 | assert(ValMapping.BreakDown.size() == 1 && |
| 64 | "Support for complex break down not supported yet"); |
| 65 | const RegisterBankInfo::PartialMapping &PartialMap = ValMapping.BreakDown[0]; |
Quentin Colombet | 0e5ff58 | 2016-04-21 18:09:34 +0000 | [diff] [blame] | 66 | assert(PartialMap.Length == |
Quentin Colombet | 777a771 | 2016-04-12 00:38:51 +0000 | [diff] [blame] | 67 | (TargetRegisterInfo::isPhysicalRegister(Reg) |
| 68 | ? TRI->getMinimalPhysRegClass(Reg)->getSize() * 8 |
| 69 | : MRI->getSize(Reg)) && |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 70 | "Repairing other than copy not implemented yet"); |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 71 | // If the MIRBuilder is configured to insert somewhere else than |
| 72 | // DefUseMI, we may not use this function like was it first |
| 73 | // internded (local repairing), so make sure we pay attention before |
| 74 | // we remove the assert. |
| 75 | // In particular, it is likely that we will have to properly save |
| 76 | // the insertion point of the MIRBuilder and restore it at the end |
| 77 | // of this method. |
| 78 | assert(&DefUseMI == &(*MIRBuilder.getInsertPt()) && |
| 79 | "Need to save and restore the insertion point"); |
| 80 | // For use, we will add a copy just in front of the instruction. |
| 81 | // For def, we will add a copy just after the instruction. |
| 82 | // In either case, the insertion point must be valid. In particular, |
| 83 | // make sure we do not insert in the middle of terminators or phis. |
| 84 | bool Before = !IsDef; |
| 85 | setSafeInsertionPoint(DefUseMI, Before); |
| 86 | if (DefUseMI.isTerminator() && Before) { |
| 87 | // Check that the insertion point does not happen |
| 88 | // before the definition of Reg. |
| 89 | // This can happen if Reg is defined by a terminator |
| 90 | // and used by another one. |
| 91 | // In that case the repairing code is actually more involved |
| 92 | // because we have to split the block. |
| 93 | |
| 94 | // Assert that this is not a physical register. |
| 95 | // The target independent code does not insert physical registers |
| 96 | // on terminators, so if we end up in this situation, this is |
| 97 | // likely a bug in the target. |
| 98 | assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 99 | "Check for physical register not implemented"); |
| 100 | const MachineInstr *RegDef = MRI->getVRegDef(Reg); |
| 101 | assert(RegDef && "Reg has more than one definition?"); |
| 102 | // Assert to make the code more readable; Reg is used by DefUseMI, i.e., |
| 103 | // (Before == !IsDef == true), so DefUseMI != RegDef otherwise we have |
| 104 | // a use (that is not a PHI) that is not dominated by its def. |
| 105 | assert(&DefUseMI != RegDef && "Def does not dominate all of its uses"); |
| 106 | if (RegDef->isTerminator() && RegDef->getParent() == DefUseMI.getParent()) |
| 107 | // By construction, the repairing should happen between two |
| 108 | // terminators: RegDef and DefUseMI. |
| 109 | // This is not implemented. |
| 110 | report_fatal_error("Repairing between terminators not implemented yet"); |
| 111 | } |
| 112 | |
| 113 | // Create a new temporary to hold the repaired value. |
Quentin Colombet | 0e5ff58 | 2016-04-21 18:09:34 +0000 | [diff] [blame] | 114 | unsigned NewReg = MRI->createGenericVirtualRegister(PartialMap.Length); |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 115 | // Set the registers for the source and destination of the copy. |
| 116 | unsigned Src = Reg, Dst = NewReg; |
| 117 | // If this is a definition that we repair, the copy will be |
| 118 | // inverted. |
| 119 | if (IsDef) |
| 120 | std::swap(Src, Dst); |
| 121 | (void)MIRBuilder.buildInstr(TargetOpcode::COPY, Dst, Src); |
| 122 | |
Quentin Colombet | e16f561 | 2016-04-07 23:53:55 +0000 | [diff] [blame] | 123 | DEBUG(dbgs() << "Repair: " << PrintReg(Reg) << " with: " |
| 124 | << PrintReg(NewReg) << '\n'); |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 125 | |
| 126 | // Restore the insertion point of the MIRBuilder. |
| 127 | MIRBuilder.setInstr(DefUseMI, Before); |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 128 | return NewReg; |
| 129 | } |
| 130 | |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 131 | void RegBankSelect::setSafeInsertionPoint(MachineInstr &InsertPt, bool Before) { |
| 132 | // Check that we are not looking to insert before a phi. |
| 133 | // Indeed, we would need more information on what to do. |
| 134 | // By default that should be all the predecessors, but this is |
| 135 | // probably not what we want in general. |
| 136 | assert((!Before || !InsertPt.isPHI()) && |
| 137 | "Insertion before phis not implemented"); |
| 138 | // The same kind of observation hold for terminators if we try to |
| 139 | // insert after them. |
| 140 | assert((Before || !InsertPt.isTerminator()) && |
| 141 | "Insertion after terminatos not implemented"); |
| 142 | if (InsertPt.isPHI()) { |
| 143 | assert(!Before && "Not supported!!"); |
| 144 | MachineBasicBlock *MBB = InsertPt.getParent(); |
| 145 | assert(MBB && "Insertion point is not in a basic block"); |
| 146 | MachineBasicBlock::iterator FirstNonPHIPt = MBB->getFirstNonPHI(); |
| 147 | if (FirstNonPHIPt == MBB->end()) { |
| 148 | // If there is not any non-phi instruction, insert at the end of MBB. |
| 149 | MIRBuilder.setMBB(*MBB, /*Beginning*/ false); |
| 150 | return; |
| 151 | } |
| 152 | // The insertion point before the first non-phi instruction. |
| 153 | MIRBuilder.setInstr(*FirstNonPHIPt, /*Before*/ true); |
| 154 | return; |
| 155 | } |
| 156 | if (InsertPt.isTerminator()) { |
| 157 | MachineBasicBlock *MBB = InsertPt.getParent(); |
| 158 | assert(MBB && "Insertion point is not in a basic block"); |
| 159 | MIRBuilder.setInstr(*MBB->getFirstTerminator(), /*Before*/ true); |
| 160 | return; |
| 161 | } |
| 162 | MIRBuilder.setInstr(InsertPt, /*Before*/ Before); |
| 163 | } |
| 164 | |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 165 | void RegBankSelect::assignInstr(MachineInstr &MI) { |
Quentin Colombet | e16f561 | 2016-04-07 23:53:55 +0000 | [diff] [blame] | 166 | DEBUG(dbgs() << "Assign: " << MI); |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 167 | const RegisterBankInfo::InstructionMapping DefaultMapping = |
| 168 | RBI->getInstrMapping(MI); |
| 169 | // Make sure the mapping is valid for MI. |
Quentin Colombet | c320fb4 | 2016-04-21 18:34:43 +0000 | [diff] [blame^] | 170 | assert(DefaultMapping.verify(MI) && "Invalid instruction mapping"); |
Quentin Colombet | e16f561 | 2016-04-07 23:53:55 +0000 | [diff] [blame] | 171 | |
| 172 | DEBUG(dbgs() << "Mapping: " << DefaultMapping << '\n'); |
| 173 | |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 174 | // Set the insertion point before MI. |
| 175 | // This is where we are going to insert the repairing code if any. |
| 176 | MIRBuilder.setInstr(MI, /*Before*/ true); |
| 177 | |
| 178 | // For now, do not look for alternative mappings. |
| 179 | // Alternative mapping may require to rewrite MI and we do not support |
| 180 | // that yet. |
| 181 | // Walk the operands and assign then to the chosen mapping, possibly with |
| 182 | // the insertion of repair code for uses. |
| 183 | for (unsigned OpIdx = 0, EndIdx = MI.getNumOperands(); OpIdx != EndIdx; |
| 184 | ++OpIdx) { |
| 185 | MachineOperand &MO = MI.getOperand(OpIdx); |
| 186 | // Nothing to be done for non-register operands. |
| 187 | if (!MO.isReg()) |
| 188 | continue; |
| 189 | unsigned Reg = MO.getReg(); |
| 190 | if (!Reg) |
| 191 | continue; |
| 192 | |
| 193 | const RegisterBankInfo::ValueMapping &ValMapping = |
| 194 | DefaultMapping.getOperandMapping(OpIdx); |
| 195 | // If Reg is already properly mapped, move on. |
| 196 | if (assignmentMatch(Reg, ValMapping)) |
| 197 | continue; |
| 198 | |
| 199 | // For uses, we may need to create a new temporary. |
| 200 | // Indeed, if Reg is already assigned a register bank, at this |
| 201 | // point, we know it is different from the one defined by the |
| 202 | // chosen mapping, we need to adjust for that. |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 203 | // For definitions, changing the register bank will affect all |
| 204 | // its uses, and in particular the ones we already visited. |
| 205 | // Although this is correct, since with the RPO traversal of the |
| 206 | // basic blocks the only uses that we already visisted for this |
| 207 | // definition are PHIs (i.e., copies), this may not be the best |
| 208 | // solution according to the cost model. |
| 209 | // Therefore, create a new temporary for Reg. |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 210 | assert(ValMapping.BreakDown.size() == 1 && |
| 211 | "Support for complex break down not supported yet"); |
Quentin Colombet | 777a771 | 2016-04-12 00:38:51 +0000 | [diff] [blame] | 212 | if (TargetRegisterInfo::isPhysicalRegister(Reg) || |
| 213 | MRI->getRegClassOrRegBank(Reg)) { |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 214 | if (!MO.isDef() && MI.isPHI()) { |
| 215 | // Phis are already copies, so there is nothing to repair. |
| 216 | // Note: This will not hold when we support break downs with |
| 217 | // more than one segment. |
| 218 | DEBUG(dbgs() << "Skip PHI use\n"); |
| 219 | continue; |
| 220 | } |
| 221 | // If MO is a definition, since repairing after a terminator is |
| 222 | // painful, do not repair. Indeed, this is probably not worse |
| 223 | // saving the move in the PHIs that will get reassigned. |
| 224 | if (!MO.isDef() || !MI.isTerminator()) |
| 225 | Reg = repairReg(Reg, ValMapping, MI, MO.isDef()); |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 226 | } |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 227 | |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 228 | // If we end up here, MO should be free of encoding constraints, |
| 229 | // i.e., we do not have to constrained the RegBank of Reg to |
| 230 | // the requirement of the operands. |
| 231 | // If that is not the case, this means the code was broken before |
| 232 | // hands because we should have found that the assignment match. |
| 233 | // This will not hold when we will consider alternative mappings. |
Quentin Colombet | 6d6d6af | 2016-04-08 16:48:16 +0000 | [diff] [blame] | 234 | DEBUG(dbgs() << "Assign: " << *ValMapping.BreakDown[0].RegBank << " to " |
| 235 | << PrintReg(Reg) << '\n'); |
Quentin Colombet | 904a2c7 | 2016-04-12 00:12:59 +0000 | [diff] [blame] | 236 | |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 237 | MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank); |
| 238 | MO.setReg(Reg); |
| 239 | } |
Quentin Colombet | e16f561 | 2016-04-07 23:53:55 +0000 | [diff] [blame] | 240 | DEBUG(dbgs() << "Assigned: " << MI); |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Quentin Colombet | 8e8e85c | 2016-04-05 19:06:01 +0000 | [diff] [blame] | 243 | bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) { |
Quentin Colombet | e16f561 | 2016-04-07 23:53:55 +0000 | [diff] [blame] | 244 | DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n'); |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 245 | init(MF); |
| 246 | // Walk the function and assign register banks to all operands. |
Quentin Colombet | ab8c21f | 2016-04-08 17:19:10 +0000 | [diff] [blame] | 247 | // Use a RPOT to make sure all registers are assigned before we choose |
| 248 | // the best mapping of the current instruction. |
| 249 | ReversePostOrderTraversal<MachineFunction*> RPOT(&MF); |
| 250 | for (MachineBasicBlock *MBB : RPOT) |
| 251 | for (MachineInstr &MI : *MBB) |
Quentin Colombet | 40ad573 | 2016-04-07 18:19:27 +0000 | [diff] [blame] | 252 | assignInstr(MI); |
Quentin Colombet | 8e8e85c | 2016-04-05 19:06:01 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |