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 13 #include "llvm/ADT/PostOrderIterator.h" 14 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h" 15 #include "llvm/CodeGen/GlobalISel/RegisterBank.h" 16 #include "llvm/CodeGen/MachineRegisterInfo.h" 17 #include "llvm/Support/Debug.h" 18 #include "llvm/Target/TargetSubtargetInfo.h" 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 29 RegBankSelect::RegBankSelect() 30 : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr) { 31 initializeRegBankSelectPass(*PassRegistry::getPassRegistry()); 32 } 33 34 void RegBankSelect::init(MachineFunction &MF) { 35 RBI = MF.getSubtarget().getRegBankInfo(); 36 assert(RBI && "Cannot work without RegisterBankInfo"); 37 MRI = &MF.getRegInfo(); 38 TRI = MF.getSubtarget().getRegisterInfo(); 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 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; 57 } 58 59 unsigned 60 RegBankSelect::repairReg(unsigned Reg, 61 const RegisterBankInfo::ValueMapping &ValMapping, 62 MachineInstr &DefUseMI, bool IsDef) { 63 assert(ValMapping.BreakDown.size() == 1 && 64 "Support for complex break down not supported yet"); 65 const RegisterBankInfo::PartialMapping &PartialMap = ValMapping.BreakDown[0]; 66 assert(PartialMap.Mask.getBitWidth() == 67 (TargetRegisterInfo::isPhysicalRegister(Reg) 68 ? TRI->getMinimalPhysRegClass(Reg)->getSize() * 8 69 : MRI->getSize(Reg)) && 70 "Repairing other than copy not implemented yet"); 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. 114 unsigned NewReg = 115 MRI->createGenericVirtualRegister(PartialMap.Mask.getBitWidth()); 116 // Set the registers for the source and destination of the copy. 117 unsigned Src = Reg, Dst = NewReg; 118 // If this is a definition that we repair, the copy will be 119 // inverted. 120 if (IsDef) 121 std::swap(Src, Dst); 122 (void)MIRBuilder.buildInstr(TargetOpcode::COPY, Dst, Src); 123 124 DEBUG(dbgs() << "Repair: " << PrintReg(Reg) << " with: " 125 << PrintReg(NewReg) << '\n'); 126 127 // Restore the insertion point of the MIRBuilder. 128 MIRBuilder.setInstr(DefUseMI, Before); 129 return NewReg; 130 } 131 132 void RegBankSelect::setSafeInsertionPoint(MachineInstr &InsertPt, bool Before) { 133 // Check that we are not looking to insert before a phi. 134 // Indeed, we would need more information on what to do. 135 // By default that should be all the predecessors, but this is 136 // probably not what we want in general. 137 assert((!Before || !InsertPt.isPHI()) && 138 "Insertion before phis not implemented"); 139 // The same kind of observation hold for terminators if we try to 140 // insert after them. 141 assert((Before || !InsertPt.isTerminator()) && 142 "Insertion after terminatos not implemented"); 143 if (InsertPt.isPHI()) { 144 assert(!Before && "Not supported!!"); 145 MachineBasicBlock *MBB = InsertPt.getParent(); 146 assert(MBB && "Insertion point is not in a basic block"); 147 MachineBasicBlock::iterator FirstNonPHIPt = MBB->getFirstNonPHI(); 148 if (FirstNonPHIPt == MBB->end()) { 149 // If there is not any non-phi instruction, insert at the end of MBB. 150 MIRBuilder.setMBB(*MBB, /*Beginning*/ false); 151 return; 152 } 153 // The insertion point before the first non-phi instruction. 154 MIRBuilder.setInstr(*FirstNonPHIPt, /*Before*/ true); 155 return; 156 } 157 if (InsertPt.isTerminator()) { 158 MachineBasicBlock *MBB = InsertPt.getParent(); 159 assert(MBB && "Insertion point is not in a basic block"); 160 MIRBuilder.setInstr(*MBB->getFirstTerminator(), /*Before*/ true); 161 return; 162 } 163 MIRBuilder.setInstr(InsertPt, /*Before*/ Before); 164 } 165 166 void RegBankSelect::assignInstr(MachineInstr &MI) { 167 DEBUG(dbgs() << "Assign: " << MI); 168 const RegisterBankInfo::InstructionMapping DefaultMapping = 169 RBI->getInstrMapping(MI); 170 // Make sure the mapping is valid for MI. 171 DefaultMapping.verify(MI); 172 173 DEBUG(dbgs() << "Mapping: " << DefaultMapping << '\n'); 174 175 // Set the insertion point before MI. 176 // This is where we are going to insert the repairing code if any. 177 MIRBuilder.setInstr(MI, /*Before*/ true); 178 179 // For now, do not look for alternative mappings. 180 // Alternative mapping may require to rewrite MI and we do not support 181 // that yet. 182 // Walk the operands and assign then to the chosen mapping, possibly with 183 // the insertion of repair code for uses. 184 for (unsigned OpIdx = 0, EndIdx = MI.getNumOperands(); OpIdx != EndIdx; 185 ++OpIdx) { 186 MachineOperand &MO = MI.getOperand(OpIdx); 187 // Nothing to be done for non-register operands. 188 if (!MO.isReg()) 189 continue; 190 unsigned Reg = MO.getReg(); 191 if (!Reg) 192 continue; 193 194 const RegisterBankInfo::ValueMapping &ValMapping = 195 DefaultMapping.getOperandMapping(OpIdx); 196 // If Reg is already properly mapped, move on. 197 if (assignmentMatch(Reg, ValMapping)) 198 continue; 199 200 // For uses, we may need to create a new temporary. 201 // Indeed, if Reg is already assigned a register bank, at this 202 // point, we know it is different from the one defined by the 203 // chosen mapping, we need to adjust for that. 204 // For definitions, changing the register bank will affect all 205 // its uses, and in particular the ones we already visited. 206 // Although this is correct, since with the RPO traversal of the 207 // basic blocks the only uses that we already visisted for this 208 // definition are PHIs (i.e., copies), this may not be the best 209 // solution according to the cost model. 210 // Therefore, create a new temporary for Reg. 211 assert(ValMapping.BreakDown.size() == 1 && 212 "Support for complex break down not supported yet"); 213 if (TargetRegisterInfo::isPhysicalRegister(Reg) || 214 MRI->getRegClassOrRegBank(Reg)) { 215 if (!MO.isDef() && MI.isPHI()) { 216 // Phis are already copies, so there is nothing to repair. 217 // Note: This will not hold when we support break downs with 218 // more than one segment. 219 DEBUG(dbgs() << "Skip PHI use\n"); 220 continue; 221 } 222 // If MO is a definition, since repairing after a terminator is 223 // painful, do not repair. Indeed, this is probably not worse 224 // saving the move in the PHIs that will get reassigned. 225 if (!MO.isDef() || !MI.isTerminator()) 226 Reg = repairReg(Reg, ValMapping, MI, MO.isDef()); 227 } 228 229 // If we end up here, MO should be free of encoding constraints, 230 // i.e., we do not have to constrained the RegBank of Reg to 231 // the requirement of the operands. 232 // If that is not the case, this means the code was broken before 233 // hands because we should have found that the assignment match. 234 // This will not hold when we will consider alternative mappings. 235 DEBUG(dbgs() << "Assign: " << *ValMapping.BreakDown[0].RegBank << " to " 236 << PrintReg(Reg) << '\n'); 237 238 MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank); 239 MO.setReg(Reg); 240 } 241 DEBUG(dbgs() << "Assigned: " << MI); 242 } 243 244 bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) { 245 DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n'); 246 init(MF); 247 // Walk the function and assign register banks to all operands. 248 // Use a RPOT to make sure all registers are assigned before we choose 249 // the best mapping of the current instruction. 250 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF); 251 for (MachineBasicBlock *MBB : RPOT) 252 for (MachineInstr &MI : *MBB) 253 assignInstr(MI); 254 return false; 255 } 256