Skip to content

Commit 6b3c9e5

Browse files
authored
[X86] Speed up X86 Domain Reassignment pass by early return (#108108)
Current implementation of X86 Domain Reassignment pass is finding out the complete closure of a general register, then check if it's possible to change the domain. It causes compile time issue when compiling large functions. This patch checks the possibility of change domain in the process of constructing closure, if it's illegal to change domain, we can return immedietely. For one of our large files, it reduced X86 Domain Reassignment pass time from 200+ seconds to less than 1s.
1 parent 2731be7 commit 6b3c9e5

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

llvm/lib/Target/X86/X86DomainReassignment.cpp

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ class X86DomainReassignment : public MachineFunctionPass {
367367
const X86InstrInfo *TII = nullptr;
368368

369369
/// All edges that are included in some closure
370-
BitVector EnclosedEdges{8, false};
370+
DenseMap<Register, unsigned> EnclosedEdges;
371371

372372
/// All instructions that are included in some closure.
373373
DenseMap<MachineInstr *, unsigned> EnclosedInstrs;
@@ -399,14 +399,16 @@ class X86DomainReassignment : public MachineFunctionPass {
399399
void buildClosure(Closure &, Register Reg);
400400

401401
/// Enqueue \p Reg to be considered for addition to the closure.
402-
void visitRegister(Closure &, Register Reg, RegDomain &Domain,
402+
/// Return false if the closure becomes invalid.
403+
bool visitRegister(Closure &, Register Reg, RegDomain &Domain,
403404
SmallVectorImpl<unsigned> &Worklist);
404405

405406
/// Reassign the closure to \p Domain.
406407
void reassign(const Closure &C, RegDomain Domain) const;
407408

408409
/// Add \p MI to the closure.
409-
void encloseInstr(Closure &C, MachineInstr *MI);
410+
/// Return false if the closure becomes invalid.
411+
bool encloseInstr(Closure &C, MachineInstr *MI);
410412

411413
/// /returns true if it is profitable to reassign the closure to \p Domain.
412414
bool isReassignmentProfitable(const Closure &C, RegDomain Domain) const;
@@ -419,37 +421,46 @@ char X86DomainReassignment::ID = 0;
419421

420422
} // End anonymous namespace.
421423

422-
void X86DomainReassignment::visitRegister(Closure &C, Register Reg,
424+
bool X86DomainReassignment::visitRegister(Closure &C, Register Reg,
423425
RegDomain &Domain,
424426
SmallVectorImpl<unsigned> &Worklist) {
425427
if (!Reg.isVirtual())
426-
return;
428+
return true;
427429

428-
if (EnclosedEdges.test(Register::virtReg2Index(Reg)))
429-
return;
430+
auto I = EnclosedEdges.find(Reg);
431+
if (I != EnclosedEdges.end()) {
432+
if (I->second != C.getID()) {
433+
C.setAllIllegal();
434+
return false;
435+
}
436+
return true;
437+
}
430438

431439
if (!MRI->hasOneDef(Reg))
432-
return;
440+
return true;
433441

434442
RegDomain RD = getDomain(MRI->getRegClass(Reg), MRI->getTargetRegisterInfo());
435443
// First edge in closure sets the domain.
436444
if (Domain == NoDomain)
437445
Domain = RD;
438446

439447
if (Domain != RD)
440-
return;
448+
return true;
441449

442450
Worklist.push_back(Reg);
451+
return true;
443452
}
444453

445-
void X86DomainReassignment::encloseInstr(Closure &C, MachineInstr *MI) {
454+
bool X86DomainReassignment::encloseInstr(Closure &C, MachineInstr *MI) {
446455
auto I = EnclosedInstrs.find(MI);
447456
if (I != EnclosedInstrs.end()) {
448-
if (I->second != C.getID())
457+
if (I->second != C.getID()) {
449458
// Instruction already belongs to another closure, avoid conflicts between
450459
// closure and mark this closure as illegal.
451460
C.setAllIllegal();
452-
return;
461+
return false;
462+
}
463+
return true;
453464
}
454465

455466
EnclosedInstrs[MI] = C.getID();
@@ -465,6 +476,7 @@ void X86DomainReassignment::encloseInstr(Closure &C, MachineInstr *MI) {
465476
C.setIllegal((RegDomain)i);
466477
}
467478
}
479+
return C.hasLegalDstDomain();
468480
}
469481

470482
double X86DomainReassignment::calculateCost(const Closure &C,
@@ -543,10 +555,11 @@ void X86DomainReassignment::buildClosure(Closure &C, Register Reg) {
543555
// Register already in this closure.
544556
if (!C.insertEdge(CurReg))
545557
continue;
546-
EnclosedEdges.set(Register::virtReg2Index(Reg));
558+
EnclosedEdges[Reg] = C.getID();
547559

548560
MachineInstr *DefMI = MRI->getVRegDef(CurReg);
549-
encloseInstr(C, DefMI);
561+
if (!encloseInstr(C, DefMI))
562+
return;
550563

551564
// Add register used by the defining MI to the worklist.
552565
// Do not add registers which are used in address calculation, they will be
@@ -565,7 +578,8 @@ void X86DomainReassignment::buildClosure(Closure &C, Register Reg) {
565578
auto &Op = DefMI->getOperand(OpIdx);
566579
if (!Op.isReg() || !Op.isUse())
567580
continue;
568-
visitRegister(C, Op.getReg(), Domain, Worklist);
581+
if (!visitRegister(C, Op.getReg(), Domain, Worklist))
582+
return;
569583
}
570584

571585
// Expand closure through register uses.
@@ -574,9 +588,10 @@ void X86DomainReassignment::buildClosure(Closure &C, Register Reg) {
574588
// as this should remain in GPRs.
575589
if (usedAsAddr(UseMI, CurReg, TII)) {
576590
C.setAllIllegal();
577-
continue;
591+
return;
578592
}
579-
encloseInstr(C, &UseMI);
593+
if (!encloseInstr(C, &UseMI))
594+
return;
580595

581596
for (auto &DefOp : UseMI.defs()) {
582597
if (!DefOp.isReg())
@@ -585,9 +600,10 @@ void X86DomainReassignment::buildClosure(Closure &C, Register Reg) {
585600
Register DefReg = DefOp.getReg();
586601
if (!DefReg.isVirtual()) {
587602
C.setAllIllegal();
588-
continue;
603+
return;
589604
}
590-
visitRegister(C, DefReg, Domain, Worklist);
605+
if (!visitRegister(C, DefReg, Domain, Worklist))
606+
return;
591607
}
592608
}
593609
}
@@ -775,7 +791,6 @@ bool X86DomainReassignment::runOnMachineFunction(MachineFunction &MF) {
775791
bool Changed = false;
776792

777793
EnclosedEdges.clear();
778-
EnclosedEdges.resize(MRI->getNumVirtRegs());
779794
EnclosedInstrs.clear();
780795

781796
std::vector<Closure> Closures;
@@ -795,7 +810,7 @@ bool X86DomainReassignment::runOnMachineFunction(MachineFunction &MF) {
795810
continue;
796811

797812
// Register already in closure.
798-
if (EnclosedEdges.test(Idx))
813+
if (EnclosedEdges.contains(Reg))
799814
continue;
800815

801816
// Calculate closure starting with Reg.

0 commit comments

Comments
 (0)