@@ -399,81 +399,81 @@ void TailDuplicator::duplicateInstruction(
399399 return ;
400400 }
401401 MachineInstr &NewMI = TII->duplicate (*PredBB, PredBB->end (), *MI);
402- if (PreRegAlloc) {
403- for (unsigned i = 0 , e = NewMI.getNumOperands (); i != e; ++i) {
404- MachineOperand &MO = NewMI.getOperand (i);
405- if (!MO.isReg ())
406- continue ;
407- Register Reg = MO.getReg ();
408- if (!Reg.isVirtual ())
409- continue ;
410- if (MO.isDef ()) {
411- const TargetRegisterClass *RC = MRI->getRegClass (Reg);
412- Register NewReg = MRI->createVirtualRegister (RC);
413- MO.setReg (NewReg);
414- LocalVRMap.insert (std::make_pair (Reg, RegSubRegPair (NewReg, 0 )));
415- if (isDefLiveOut (Reg, TailBB, MRI) || UsedByPhi.count (Reg))
416- addSSAUpdateEntry (Reg, NewReg, PredBB);
417- } else {
418- auto VI = LocalVRMap.find (Reg);
419- if (VI != LocalVRMap.end ()) {
420- // Need to make sure that the register class of the mapped register
421- // will satisfy the constraints of the class of the register being
422- // replaced.
423- auto *OrigRC = MRI->getRegClass (Reg);
424- auto *MappedRC = MRI->getRegClass (VI->second .Reg );
425- const TargetRegisterClass *ConstrRC;
426- if (VI->second .SubReg != 0 ) {
427- ConstrRC = TRI->getMatchingSuperRegClass (MappedRC, OrigRC,
428- VI->second .SubReg );
429- if (ConstrRC) {
430- // The actual constraining (as in "find appropriate new class")
431- // is done by getMatchingSuperRegClass, so now we only need to
432- // change the class of the mapped register.
433- MRI->setRegClass (VI->second .Reg , ConstrRC);
434- }
435- } else {
436- // For mapped registers that do not have sub-registers, simply
437- // restrict their class to match the original one.
438-
439- // We don't want debug instructions affecting the resulting code so
440- // if we're cloning a debug instruction then just use MappedRC
441- // rather than constraining the register class further.
442- ConstrRC = NewMI.isDebugInstr ()
443- ? MappedRC
444- : MRI->constrainRegClass (VI->second .Reg , OrigRC);
445- }
446-
447- if (ConstrRC) {
448- // If the class constraining succeeded, we can simply replace
449- // the old register with the mapped one.
450- MO.setReg (VI->second .Reg );
451- // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a
452- // sub-register, we need to compose the sub-register indices.
453- MO.setSubReg (
454- TRI->composeSubRegIndices (VI->second .SubReg , MO.getSubReg ()));
455- } else {
456- // The direct replacement is not possible, due to failing register
457- // class constraints. An explicit COPY is necessary. Create one
458- // that can be reused.
459- Register NewReg = MRI->createVirtualRegister (OrigRC);
460- BuildMI (*PredBB, NewMI, NewMI.getDebugLoc (),
461- TII->get (TargetOpcode::COPY), NewReg)
462- .addReg (VI->second .Reg , 0 , VI->second .SubReg );
463- LocalVRMap.erase (VI);
464- LocalVRMap.insert (std::make_pair (Reg, RegSubRegPair (NewReg, 0 )));
465- MO.setReg (NewReg);
466- // The composed VI.Reg:VI.SubReg is replaced with NewReg, which
467- // is equivalent to the whole register Reg. Hence, Reg:subreg
468- // is same as NewReg:subreg, so keep the sub-register index
469- // unchanged.
470- }
471- // Clear any kill flags from this operand. The new register could
472- // have uses after this one, so kills are not valid here.
473- MO.setIsKill (false );
474- }
402+ if (!PreRegAlloc)
403+ return ;
404+ for (unsigned i = 0 , e = NewMI.getNumOperands (); i != e; ++i) {
405+ MachineOperand &MO = NewMI.getOperand (i);
406+ if (!MO.isReg ())
407+ continue ;
408+ Register Reg = MO.getReg ();
409+ if (!Reg.isVirtual ())
410+ continue ;
411+ if (MO.isDef ()) {
412+ const TargetRegisterClass *RC = MRI->getRegClass (Reg);
413+ Register NewReg = MRI->createVirtualRegister (RC);
414+ MO.setReg (NewReg);
415+ LocalVRMap.insert (std::make_pair (Reg, RegSubRegPair (NewReg, 0 )));
416+ if (isDefLiveOut (Reg, TailBB, MRI) || UsedByPhi.count (Reg))
417+ addSSAUpdateEntry (Reg, NewReg, PredBB);
418+ continue ;
419+ }
420+ auto VI = LocalVRMap.find (Reg);
421+ if (VI == LocalVRMap.end ())
422+ continue ;
423+ // Need to make sure that the register class of the mapped register
424+ // will satisfy the constraints of the class of the register being
425+ // replaced.
426+ auto *OrigRC = MRI->getRegClass (Reg);
427+ auto *MappedRC = MRI->getRegClass (VI->second .Reg );
428+ const TargetRegisterClass *ConstrRC;
429+ if (VI->second .SubReg != 0 ) {
430+ ConstrRC =
431+ TRI->getMatchingSuperRegClass (MappedRC, OrigRC, VI->second .SubReg );
432+ if (ConstrRC) {
433+ // The actual constraining (as in "find appropriate new class")
434+ // is done by getMatchingSuperRegClass, so now we only need to
435+ // change the class of the mapped register.
436+ MRI->setRegClass (VI->second .Reg , ConstrRC);
475437 }
438+ } else {
439+ // For mapped registers that do not have sub-registers, simply
440+ // restrict their class to match the original one.
441+
442+ // We don't want debug instructions affecting the resulting code so
443+ // if we're cloning a debug instruction then just use MappedRC
444+ // rather than constraining the register class further.
445+ ConstrRC = NewMI.isDebugInstr ()
446+ ? MappedRC
447+ : MRI->constrainRegClass (VI->second .Reg , OrigRC);
448+ }
449+
450+ if (ConstrRC) {
451+ // If the class constraining succeeded, we can simply replace
452+ // the old register with the mapped one.
453+ MO.setReg (VI->second .Reg );
454+ // We have Reg -> VI.Reg:VI.SubReg, so if Reg is used with a
455+ // sub-register, we need to compose the sub-register indices.
456+ MO.setSubReg (
457+ TRI->composeSubRegIndices (VI->second .SubReg , MO.getSubReg ()));
458+ } else {
459+ // The direct replacement is not possible, due to failing register
460+ // class constraints. An explicit COPY is necessary. Create one
461+ // that can be reused.
462+ Register NewReg = MRI->createVirtualRegister (OrigRC);
463+ BuildMI (*PredBB, NewMI, NewMI.getDebugLoc (), TII->get (TargetOpcode::COPY),
464+ NewReg)
465+ .addReg (VI->second .Reg , 0 , VI->second .SubReg );
466+ LocalVRMap.erase (VI);
467+ LocalVRMap.insert (std::make_pair (Reg, RegSubRegPair (NewReg, 0 )));
468+ MO.setReg (NewReg);
469+ // The composed VI.Reg:VI.SubReg is replaced with NewReg, which
470+ // is equivalent to the whole register Reg. Hence, Reg:subreg
471+ // is same as NewReg:subreg, so keep the sub-register index
472+ // unchanged.
476473 }
474+ // Clear any kill flags from this operand. The new register could
475+ // have uses after this one, so kills are not valid here.
476+ MO.setIsKill (false );
477477 }
478478}
479479
0 commit comments