@@ -268,7 +268,7 @@ class RegAllocFast : public MachineFunctionPass {
268
268
Register VirtReg);
269
269
bool defineVirtReg (MachineInstr &MI, unsigned OpNum, Register VirtReg,
270
270
bool LookAtPhysRegUses = false );
271
- bool useVirtReg (MachineInstr &MI, unsigned OpNum , Register VirtReg);
271
+ bool useVirtReg (MachineInstr &MI, MachineOperand &MO , Register VirtReg);
272
272
273
273
MachineBasicBlock::iterator
274
274
getMBBBeginInsertionPoint (MachineBasicBlock &MBB,
@@ -984,17 +984,15 @@ bool RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
984
984
985
985
// / Allocates a register for a VirtReg use.
986
986
// / \return true if MI's MachineOperands were re-arranged/invalidated.
987
- bool RegAllocFast::useVirtReg (MachineInstr &MI, unsigned OpNum ,
987
+ bool RegAllocFast::useVirtReg (MachineInstr &MI, MachineOperand &MO ,
988
988
Register VirtReg) {
989
989
assert (VirtReg.isVirtual () && " Not a virtual register" );
990
990
if (!shouldAllocateRegister (VirtReg))
991
991
return false ;
992
- MachineOperand &MO = MI.getOperand (OpNum);
993
992
LiveRegMap::iterator LRI;
994
993
bool New;
995
994
std::tie (LRI, New) = LiveVirtRegs.insert (LiveReg (VirtReg));
996
995
if (New) {
997
- MachineOperand &MO = MI.getOperand (OpNum);
998
996
if (!MO.isKill ()) {
999
997
if (mayLiveOut (VirtReg)) {
1000
998
LRI->LiveOut = true ;
@@ -1224,6 +1222,17 @@ void RegAllocFast::findAndSortDefOperandIndexes(const MachineInstr &MI) {
1224
1222
});
1225
1223
}
1226
1224
1225
+ // Returns true if MO is tied and the operand it's tied to is not Undef (not
1226
+ // Undef is not the same thing as Def).
1227
+ static bool isTiedToNotUndef (const MachineOperand &MO) {
1228
+ if (!MO.isTied ())
1229
+ return false ;
1230
+ const MachineInstr &MI = *MO.getParent ();
1231
+ unsigned TiedIdx = MI.findTiedOperandIdx (MI.getOperandNo (&MO));
1232
+ const MachineOperand &TiedMO = MI.getOperand (TiedIdx);
1233
+ return !TiedMO.isUndef ();
1234
+ }
1235
+
1227
1236
void RegAllocFast::allocateInstruction (MachineInstr &MI) {
1228
1237
// The basic algorithm here is:
1229
1238
// 1. Mark registers of def operands as free
@@ -1241,21 +1250,14 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1241
1250
RegMasks.clear ();
1242
1251
BundleVirtRegsMap.clear ();
1243
1252
1244
- auto TiedOpIsUndef = [&](const MachineOperand &MO, unsigned Idx) {
1245
- assert (MO.isTied ());
1246
- unsigned TiedIdx = MI.findTiedOperandIdx (Idx);
1247
- const MachineOperand &TiedMO = MI.getOperand (TiedIdx);
1248
- return TiedMO.isUndef ();
1249
- };
1250
1253
// Scan for special cases; Apply pre-assigned register defs to state.
1251
1254
bool HasPhysRegUse = false ;
1252
1255
bool HasRegMask = false ;
1253
1256
bool HasVRegDef = false ;
1254
1257
bool HasDef = false ;
1255
1258
bool HasEarlyClobber = false ;
1256
1259
bool NeedToAssignLiveThroughs = false ;
1257
- for (unsigned I = 0 ; I < MI.getNumOperands (); ++I) {
1258
- MachineOperand &MO = MI.getOperand (I);
1260
+ for (MachineOperand &MO : MI.operands ()) {
1259
1261
if (MO.isReg ()) {
1260
1262
Register Reg = MO.getReg ();
1261
1263
if (Reg.isVirtual ()) {
@@ -1268,8 +1270,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1268
1270
HasEarlyClobber = true ;
1269
1271
NeedToAssignLiveThroughs = true ;
1270
1272
}
1271
- if ((MO.isTied () && !TiedOpIsUndef (MO, I)) ||
1272
- (MO.getSubReg () != 0 && !MO.isUndef ()))
1273
+ if (isTiedToNotUndef (MO) || (MO.getSubReg () != 0 && !MO.isUndef ()))
1273
1274
NeedToAssignLiveThroughs = true ;
1274
1275
}
1275
1276
} else if (Reg.isPhysical ()) {
@@ -1314,35 +1315,32 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1314
1315
for (uint16_t OpIdx : DefOperandIndexes) {
1315
1316
MachineOperand &MO = MI.getOperand (OpIdx);
1316
1317
LLVM_DEBUG (dbgs () << " Allocating " << MO << ' \n ' );
1317
- unsigned Reg = MO.getReg ();
1318
- if (MO.isEarlyClobber () ||
1319
- (MO.isTied () && !TiedOpIsUndef (MO, OpIdx)) ||
1318
+ Register Reg = MO.getReg ();
1319
+ if (MO.isEarlyClobber () || isTiedToNotUndef (MO) ||
1320
1320
(MO.getSubReg () && !MO.isUndef ())) {
1321
1321
ReArrangedImplicitOps = defineLiveThroughVirtReg (MI, OpIdx, Reg);
1322
1322
} else {
1323
1323
ReArrangedImplicitOps = defineVirtReg (MI, OpIdx, Reg);
1324
1324
}
1325
- if (ReArrangedImplicitOps) {
1326
- // Implicit operands of MI were re-arranged,
1327
- // re-compute DefOperandIndexes.
1325
+ // Implicit operands of MI were re-arranged,
1326
+ // re-compute DefOperandIndexes.
1327
+ if (ReArrangedImplicitOps)
1328
1328
break ;
1329
- }
1330
1329
}
1331
1330
}
1332
1331
} else {
1333
1332
// Assign virtual register defs.
1334
1333
while (ReArrangedImplicitOps) {
1335
1334
ReArrangedImplicitOps = false ;
1336
- for (unsigned I = 0 , E = MI.getNumOperands (); I < E; ++I) {
1337
- MachineOperand &MO = MI.getOperand (I);
1335
+ for (MachineOperand &MO : MI.operands ()) {
1338
1336
if (!MO.isReg () || !MO.isDef ())
1339
1337
continue ;
1340
1338
Register Reg = MO.getReg ();
1341
1339
if (Reg.isVirtual ()) {
1342
- ReArrangedImplicitOps = defineVirtReg (MI, I, Reg);
1343
- if (ReArrangedImplicitOps) {
1340
+ ReArrangedImplicitOps =
1341
+ defineVirtReg (MI, MI.getOperandNo (&MO), Reg);
1342
+ if (ReArrangedImplicitOps)
1344
1343
break ;
1345
- }
1346
1344
}
1347
1345
}
1348
1346
}
@@ -1352,8 +1350,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1352
1350
// Free registers occupied by defs.
1353
1351
// Iterate operands in reverse order, so we see the implicit super register
1354
1352
// defs first (we added them earlier in case of <def,read-undef>).
1355
- for (signed I = MI.getNumOperands () - 1 ; I >= 0 ; --I) {
1356
- MachineOperand &MO = MI.getOperand (I);
1353
+ for (MachineOperand &MO : reverse (MI.operands ())) {
1357
1354
if (!MO.isReg () || !MO.isDef ())
1358
1355
continue ;
1359
1356
@@ -1370,7 +1367,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1370
1367
" tied def assigned to clobbered register" );
1371
1368
1372
1369
// Do not free tied operands and early clobbers.
1373
- if ((MO. isTied () && ! TiedOpIsUndef (MO, I) ) || MO.isEarlyClobber ())
1370
+ if (isTiedToNotUndef (MO) || MO.isEarlyClobber ())
1374
1371
continue ;
1375
1372
if (!Reg)
1376
1373
continue ;
@@ -1411,8 +1408,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1411
1408
continue ;
1412
1409
if (MRI->isReserved (Reg))
1413
1410
continue ;
1414
- bool displacedAny = usePhysReg (MI, Reg);
1415
- if (!displacedAny)
1411
+ if (!usePhysReg (MI, Reg))
1416
1412
MO.setIsKill (true );
1417
1413
}
1418
1414
}
@@ -1424,8 +1420,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1424
1420
bool ReArrangedImplicitMOs = true ;
1425
1421
while (ReArrangedImplicitMOs) {
1426
1422
ReArrangedImplicitMOs = false ;
1427
- for (unsigned I = 0 ; I < MI.getNumOperands (); ++I) {
1428
- MachineOperand &MO = MI.getOperand (I);
1423
+ for (MachineOperand &MO : MI.operands ()) {
1429
1424
if (!MO.isReg () || !MO.isUse ())
1430
1425
continue ;
1431
1426
Register Reg = MO.getReg ();
@@ -1443,7 +1438,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1443
1438
1444
1439
assert (!MO.isInternalRead () && " Bundles not supported" );
1445
1440
assert (MO.readsReg () && " reading use" );
1446
- ReArrangedImplicitMOs = useVirtReg (MI, I , Reg);
1441
+ ReArrangedImplicitMOs = useVirtReg (MI, MO , Reg);
1447
1442
if (ReArrangedImplicitMOs)
1448
1443
break ;
1449
1444
}
@@ -1465,7 +1460,7 @@ void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1465
1460
1466
1461
// Free early clobbers.
1467
1462
if (HasEarlyClobber) {
1468
- for (MachineOperand &MO : llvm:: reverse (MI.all_defs ())) {
1463
+ for (MachineOperand &MO : reverse (MI.all_defs ())) {
1469
1464
if (!MO.isEarlyClobber ())
1470
1465
continue ;
1471
1466
assert (!MO.getSubReg () && " should be already handled in def processing" );
0 commit comments