@@ -368,6 +368,9 @@ class RegAllocFastImpl {
368368 bool LookAtPhysRegUses = false );
369369 bool useVirtReg (MachineInstr &MI, MachineOperand &MO, Register VirtReg);
370370
371+ MCPhysReg getErrorAssignment (const LiveReg &LR, MachineInstr &MI,
372+ const TargetRegisterClass &RC);
373+
371374 MachineBasicBlock::iterator
372375 getMBBBeginInsertionPoint (MachineBasicBlock &MBB,
373376 SmallSet<Register, 2 > &PrologLiveIns) const ;
@@ -963,22 +966,8 @@ void RegAllocFastImpl::allocVirtReg(MachineInstr &MI, LiveReg &LR,
963966 if (!BestReg) {
964967 // Nothing we can do: Report an error and keep going with an invalid
965968 // allocation.
966- if (MI.isInlineAsm ()) {
967- MI.emitInlineAsmError (
968- " inline assembly requires more registers than available" );
969- } else {
970- const Function &Fn = MBB->getParent ()->getFunction ();
971- DiagnosticInfoRegAllocFailure DI (
972- " ran out of registers during register allocation" , Fn,
973- MI.getDebugLoc ());
974- Fn.getContext ().diagnose (DI);
975- }
976-
969+ LR.PhysReg = getErrorAssignment (LR, MI, RC);
977970 LR.Error = true ;
978- if (!AllocationOrder.empty ())
979- LR.PhysReg = AllocationOrder.front ();
980- else
981- LR.PhysReg = 0 ;
982971 return ;
983972 }
984973
@@ -1000,6 +989,7 @@ void RegAllocFastImpl::allocVirtRegUndef(MachineOperand &MO) {
1000989 } else {
1001990 const TargetRegisterClass &RC = *MRI->getRegClass (VirtReg);
1002991 ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder (&RC);
992+ // FIXME: This can happen, and should fall back to a reserved entry in RC.
1003993 assert (!AllocationOrder.empty () && " Allocation order must not be empty" );
1004994 PhysReg = AllocationOrder[0 ];
1005995 }
@@ -1074,15 +1064,6 @@ bool RegAllocFastImpl::defineVirtReg(MachineInstr &MI, unsigned OpNum,
10741064 }
10751065 if (LRI->PhysReg == 0 ) {
10761066 allocVirtReg (MI, *LRI, 0 , LookAtPhysRegUses);
1077- // If no physical register is available for LRI, we assign one at random
1078- // and bail out of this function immediately.
1079- if (LRI->Error ) {
1080- const TargetRegisterClass &RC = *MRI->getRegClass (VirtReg);
1081- ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder (&RC);
1082- if (AllocationOrder.empty ())
1083- return setPhysReg (MI, MO, MCRegister::NoRegister);
1084- return setPhysReg (MI, MO, *AllocationOrder.begin ());
1085- }
10861067 } else {
10871068 assert ((!isRegUsedInInstr (LRI->PhysReg , LookAtPhysRegUses) || LRI->Error ) &&
10881069 " TODO: preassign mismatch" );
@@ -1167,13 +1148,6 @@ bool RegAllocFastImpl::useVirtReg(MachineInstr &MI, MachineOperand &MO,
11671148 }
11681149 }
11691150 allocVirtReg (MI, *LRI, Hint, false );
1170- if (LRI->Error ) {
1171- const TargetRegisterClass &RC = *MRI->getRegClass (VirtReg);
1172- ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder (&RC);
1173- if (AllocationOrder.empty ())
1174- return setPhysReg (MI, MO, MCRegister::NoRegister);
1175- return setPhysReg (MI, MO, *AllocationOrder.begin ());
1176- }
11771151 }
11781152
11791153 LRI->LastUse = &MI;
@@ -1185,6 +1159,56 @@ bool RegAllocFastImpl::useVirtReg(MachineInstr &MI, MachineOperand &MO,
11851159 return setPhysReg (MI, MO, LRI->PhysReg );
11861160}
11871161
1162+ // / Query a physical register to use as a filler in contexts where the
1163+ // / allocation has failed. This will raise an error, but not abort the
1164+ // / compilation.
1165+ MCPhysReg RegAllocFastImpl::getErrorAssignment (const LiveReg &LR,
1166+ MachineInstr &MI,
1167+ const TargetRegisterClass &RC) {
1168+ MachineFunction &MF = *MI.getMF ();
1169+
1170+ // Avoid repeating the error every time a register is used.
1171+ bool EmitError = !MF.getProperties ().hasProperty (
1172+ MachineFunctionProperties::Property::FailedRegAlloc);
1173+ if (EmitError)
1174+ MF.getProperties ().set (MachineFunctionProperties::Property::FailedRegAlloc);
1175+
1176+ // If the allocation order was empty, all registers in the class were
1177+ // probably reserved. Fall back to taking the first register in the class,
1178+ // even if it's reserved.
1179+ ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder (&RC);
1180+ if (AllocationOrder.empty ()) {
1181+ const Function &Fn = MF.getFunction ();
1182+ if (EmitError) {
1183+ DiagnosticInfoRegAllocFailure DI (
1184+ " no registers from class available to allocate" , Fn,
1185+ MI.getDebugLoc ());
1186+ Fn.getContext ().diagnose (DI);
1187+ }
1188+
1189+ ArrayRef<MCPhysReg> RawRegs = RC.getRegisters ();
1190+ assert (!RawRegs.empty () && " register classes cannot have no registers" );
1191+ return RawRegs.front ();
1192+ }
1193+
1194+ if (!LR.Error && EmitError) {
1195+ // Nothing we can do: Report an error and keep going with an invalid
1196+ // allocation.
1197+ if (MI.isInlineAsm ()) {
1198+ MI.emitInlineAsmError (
1199+ " inline assembly requires more registers than available" );
1200+ } else {
1201+ const Function &Fn = MBB->getParent ()->getFunction ();
1202+ DiagnosticInfoRegAllocFailure DI (
1203+ " ran out of registers during register allocation" , Fn,
1204+ MI.getDebugLoc ());
1205+ Fn.getContext ().diagnose (DI);
1206+ }
1207+ }
1208+
1209+ return AllocationOrder.front ();
1210+ }
1211+
11881212// / Changes operand OpNum in MI the refer the PhysReg, considering subregs.
11891213// / \return true if MI's MachineOperands were re-arranged/invalidated.
11901214bool RegAllocFastImpl::setPhysReg (MachineInstr &MI, MachineOperand &MO,
0 commit comments