@@ -116,12 +116,33 @@ class TwoAddressInstructionPass : public MachineFunctionPass {
116
116
// registers. e.g. r1 = move v1024.
117
117
DenseMap<Register, Register> DstRegMap;
118
118
119
- void removeClobberedSrcRegMap (MachineInstr *MI) ;
119
+ MachineInstr * getSingleDef (Register Reg, MachineBasicBlock *BB) const ;
120
120
121
121
bool isRevCopyChain (Register FromReg, Register ToReg, int Maxlen);
122
122
123
123
bool noUseAfterLastDef (Register Reg, unsigned Dist, unsigned &LastDef);
124
124
125
+ bool isCopyToReg (MachineInstr &MI, Register &SrcReg, Register &DstReg,
126
+ bool &IsSrcPhys, bool &IsDstPhys) const ;
127
+
128
+ bool isPlainlyKilled (const MachineInstr *MI, Register Reg) const ;
129
+ bool isPlainlyKilled (const MachineOperand &MO) const ;
130
+
131
+ bool isKilled (MachineInstr &MI, Register Reg, bool allowFalsePositives) const ;
132
+
133
+ MachineInstr *findOnlyInterestingUse (Register Reg, MachineBasicBlock *MBB,
134
+ bool &IsCopy, Register &DstReg,
135
+ bool &IsDstPhys) const ;
136
+
137
+ bool regsAreCompatible (Register RegA, Register RegB) const ;
138
+
139
+ void removeMapRegEntry (const MachineOperand &MO,
140
+ DenseMap<Register, Register> &RegMap) const ;
141
+
142
+ void removeClobberedSrcRegMap (MachineInstr *MI);
143
+
144
+ bool regOverlapsSet (const SmallVectorImpl<Register> &Set, Register Reg) const ;
145
+
125
146
bool isProfitableToCommute (Register RegA, Register RegB, Register RegC,
126
147
MachineInstr *MI, unsigned Dist);
127
148
@@ -199,8 +220,9 @@ INITIALIZE_PASS_END(TwoAddressInstructionPass, DEBUG_TYPE,
199
220
" Two-Address instruction pass" , false , false )
200
221
201
222
// / Return the MachineInstr* if it is the single def of the Reg in current BB.
202
- static MachineInstr *getSingleDef(Register Reg, MachineBasicBlock *BB,
203
- const MachineRegisterInfo *MRI) {
223
+ MachineInstr *
224
+ TwoAddressInstructionPass::getSingleDef(Register Reg,
225
+ MachineBasicBlock *BB) const {
204
226
MachineInstr *Ret = nullptr ;
205
227
for (MachineInstr &DefMI : MRI->def_instructions (Reg)) {
206
228
if (DefMI.getParent () != BB || DefMI.isDebugValue ())
@@ -224,7 +246,7 @@ bool TwoAddressInstructionPass::isRevCopyChain(Register FromReg, Register ToReg,
224
246
int Maxlen) {
225
247
Register TmpReg = FromReg;
226
248
for (int i = 0 ; i < Maxlen; i++) {
227
- MachineInstr *Def = getSingleDef (TmpReg, MBB, MRI );
249
+ MachineInstr *Def = getSingleDef (TmpReg, MBB);
228
250
if (!Def || !Def->isCopy ())
229
251
return false ;
230
252
@@ -263,9 +285,9 @@ bool TwoAddressInstructionPass::noUseAfterLastDef(Register Reg, unsigned Dist,
263
285
// / Return true if the specified MI is a copy instruction or an extract_subreg
264
286
// / instruction. It also returns the source and destination registers and
265
287
// / whether they are physical registers by reference.
266
- static bool isCopyToReg (MachineInstr &MI, const TargetInstrInfo *TII ,
267
- Register &SrcReg, Register &DstReg, bool &IsSrcPhys,
268
- bool &IsDstPhys) {
288
+ bool TwoAddressInstructionPass:: isCopyToReg (MachineInstr &MI, Register &SrcReg ,
289
+ Register &DstReg, bool &IsSrcPhys,
290
+ bool &IsDstPhys) const {
269
291
SrcReg = 0 ;
270
292
DstReg = 0 ;
271
293
if (MI.isCopy ()) {
@@ -285,8 +307,8 @@ static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII,
285
307
286
308
// / Test if the given register value, which is used by the
287
309
// / given instruction, is killed by the given instruction.
288
- static bool isPlainlyKilled (const MachineInstr *MI, Register Reg ,
289
- LiveIntervals *LIS) {
310
+ bool TwoAddressInstructionPass:: isPlainlyKilled (const MachineInstr *MI,
311
+ Register Reg) const {
290
312
if (LIS && Reg.isVirtual () && !LIS->isNotInMIMap (*MI)) {
291
313
// FIXME: Sometimes tryInstructionTransform() will add instructions and
292
314
// test whether they can be folded before keeping them. In this case it
@@ -311,8 +333,9 @@ static bool isPlainlyKilled(const MachineInstr *MI, Register Reg,
311
333
312
334
// / Test if the register used by the given operand is killed by the operand's
313
335
// / instruction.
314
- static bool isPlainlyKilled (const MachineOperand &MO, LiveIntervals *LIS) {
315
- return MO.isKill () || isPlainlyKilled (MO.getParent (), MO.getReg (), LIS);
336
+ bool TwoAddressInstructionPass::isPlainlyKilled (
337
+ const MachineOperand &MO) const {
338
+ return MO.isKill () || isPlainlyKilled (MO.getParent (), MO.getReg ());
316
339
}
317
340
318
341
// / Test if the given register value, which is used by the given
@@ -332,15 +355,14 @@ static bool isPlainlyKilled(const MachineOperand &MO, LiveIntervals *LIS) {
332
355
// /
333
356
// / If allowFalsePositives is true then likely kills are treated as kills even
334
357
// / if it can't be proven that they are kills.
335
- static bool isKilled (MachineInstr &MI, Register Reg,
336
- const MachineRegisterInfo *MRI, const TargetInstrInfo *TII,
337
- LiveIntervals *LIS, bool allowFalsePositives) {
358
+ bool TwoAddressInstructionPass::isKilled (MachineInstr &MI, Register Reg,
359
+ bool allowFalsePositives) const {
338
360
MachineInstr *DefMI = &MI;
339
361
while (true ) {
340
362
// All uses of physical registers are likely to be kills.
341
363
if (Reg.isPhysical () && (allowFalsePositives || MRI->hasOneUse (Reg)))
342
364
return true ;
343
- if (!isPlainlyKilled (DefMI, Reg, LIS ))
365
+ if (!isPlainlyKilled (DefMI, Reg))
344
366
return false ;
345
367
if (Reg.isPhysical ())
346
368
return true ;
@@ -354,7 +376,7 @@ static bool isKilled(MachineInstr &MI, Register Reg,
354
376
Register SrcReg, DstReg;
355
377
// If the def is something other than a copy, then it isn't going to
356
378
// be coalesced, so follow the kill flag.
357
- if (!isCopyToReg (*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
379
+ if (!isCopyToReg (*DefMI, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
358
380
return true ;
359
381
Reg = SrcReg;
360
382
}
@@ -378,17 +400,15 @@ static bool isTwoAddrUse(MachineInstr &MI, Register Reg, Register &DstReg) {
378
400
379
401
// / Given a register, if all its uses are in the same basic block, return the
380
402
// / last use instruction if it's a copy or a two-address use.
381
- static MachineInstr *
382
- findOnlyInterestingUse (Register Reg, MachineBasicBlock *MBB,
383
- MachineRegisterInfo *MRI, const TargetInstrInfo *TII,
384
- bool &IsCopy, Register &DstReg, bool &IsDstPhys,
385
- LiveIntervals *LIS) {
403
+ MachineInstr *TwoAddressInstructionPass::findOnlyInterestingUse (
404
+ Register Reg, MachineBasicBlock *MBB, bool &IsCopy, Register &DstReg,
405
+ bool &IsDstPhys) const {
386
406
MachineOperand *UseOp = nullptr ;
387
407
for (MachineOperand &MO : MRI->use_nodbg_operands (Reg)) {
388
408
MachineInstr *MI = MO.getParent ();
389
409
if (MI->getParent () != MBB)
390
410
return nullptr ;
391
- if (isPlainlyKilled (MI, Reg, LIS ))
411
+ if (isPlainlyKilled (MI, Reg))
392
412
UseOp = &MO;
393
413
}
394
414
if (!UseOp)
@@ -397,7 +417,7 @@ findOnlyInterestingUse(Register Reg, MachineBasicBlock *MBB,
397
417
398
418
Register SrcReg;
399
419
bool IsSrcPhys;
400
- if (isCopyToReg (UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
420
+ if (isCopyToReg (UseMI, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) {
401
421
IsCopy = true ;
402
422
return &UseMI;
403
423
}
@@ -437,8 +457,8 @@ static MCRegister getMappedReg(Register Reg,
437
457
}
438
458
439
459
// / Return true if the two registers are equal or aliased.
440
- static bool regsAreCompatible (Register RegA, Register RegB ,
441
- const TargetRegisterInfo *TRI) {
460
+ bool TwoAddressInstructionPass:: regsAreCompatible (Register RegA,
461
+ Register RegB) const {
442
462
if (RegA == RegB)
443
463
return true ;
444
464
if (!RegA || !RegB)
@@ -447,9 +467,8 @@ static bool regsAreCompatible(Register RegA, Register RegB,
447
467
}
448
468
449
469
// / From RegMap remove entries mapped to a physical register which overlaps MO.
450
- static void removeMapRegEntry (const MachineOperand &MO,
451
- DenseMap<Register, Register> &RegMap,
452
- const TargetRegisterInfo *TRI) {
470
+ void TwoAddressInstructionPass::removeMapRegEntry (
471
+ const MachineOperand &MO, DenseMap<Register, Register> &RegMap) const {
453
472
assert (
454
473
(MO.isReg () || MO.isRegMask ()) &&
455
474
" removeMapRegEntry must be called with a register or regmask operand." );
@@ -497,27 +516,27 @@ void TwoAddressInstructionPass::removeClobberedSrcRegMap(MachineInstr *MI) {
497
516
return ;
498
517
499
518
Register Src = MI->getOperand (1 ).getReg ();
500
- if (regsAreCompatible (Dst, getMappedReg (Src, SrcRegMap), TRI ))
519
+ if (regsAreCompatible (Dst, getMappedReg (Src, SrcRegMap)))
501
520
return ;
502
521
}
503
522
504
523
for (const MachineOperand &MO : MI->operands ()) {
505
524
if (MO.isRegMask ()) {
506
- removeMapRegEntry (MO, SrcRegMap, TRI );
525
+ removeMapRegEntry (MO, SrcRegMap);
507
526
continue ;
508
527
}
509
528
if (!MO.isReg () || !MO.isDef ())
510
529
continue ;
511
530
Register Reg = MO.getReg ();
512
531
if (!Reg || Reg.isVirtual ())
513
532
continue ;
514
- removeMapRegEntry (MO, SrcRegMap, TRI );
533
+ removeMapRegEntry (MO, SrcRegMap);
515
534
}
516
535
}
517
536
518
537
// Returns true if Reg is equal or aliased to at least one register in Set.
519
- static bool regOverlapsSet (const SmallVectorImpl<Register> &Set, Register Reg,
520
- const TargetRegisterInfo *TRI) {
538
+ bool TwoAddressInstructionPass:: regOverlapsSet (
539
+ const SmallVectorImpl<Register> &Set, Register Reg) const {
521
540
for (unsigned R : Set)
522
541
if (TRI->regsOverlap (R, Reg))
523
542
return true ;
@@ -553,7 +572,7 @@ bool TwoAddressInstructionPass::isProfitableToCommute(Register RegA,
553
572
// insert => %reg1030 = COPY %reg1029
554
573
// %reg1030 = ADD8rr killed %reg1029, killed %reg1028, implicit dead %eflags
555
574
556
- if (!isPlainlyKilled (MI, RegC, LIS ))
575
+ if (!isPlainlyKilled (MI, RegC))
557
576
return false ;
558
577
559
578
// Ok, we have something like:
@@ -570,8 +589,8 @@ bool TwoAddressInstructionPass::isProfitableToCommute(Register RegA,
570
589
if (ToRegA) {
571
590
MCRegister FromRegB = getMappedReg (RegB, SrcRegMap);
572
591
MCRegister FromRegC = getMappedReg (RegC, SrcRegMap);
573
- bool CompB = FromRegB && regsAreCompatible (FromRegB, ToRegA, TRI );
574
- bool CompC = FromRegC && regsAreCompatible (FromRegC, ToRegA, TRI );
592
+ bool CompB = FromRegB && regsAreCompatible (FromRegB, ToRegA);
593
+ bool CompC = FromRegC && regsAreCompatible (FromRegC, ToRegA);
575
594
576
595
// Compute if any of the following are true:
577
596
// -RegB is not tied to a register and RegC is compatible with RegA.
@@ -675,7 +694,7 @@ bool TwoAddressInstructionPass::isProfitableToConv3Addr(Register RegA,
675
694
if (!FromRegB)
676
695
return false ;
677
696
MCRegister ToRegA = getMappedReg (RegA, DstRegMap);
678
- return (ToRegA && !regsAreCompatible (FromRegB, ToRegA, TRI ));
697
+ return (ToRegA && !regsAreCompatible (FromRegB, ToRegA));
679
698
}
680
699
681
700
// / Convert the specified two-address instruction into a three address one.
@@ -728,8 +747,8 @@ void TwoAddressInstructionPass::scanUses(Register DstReg) {
728
747
bool IsCopy = false ;
729
748
Register NewReg;
730
749
Register Reg = DstReg;
731
- while (MachineInstr *UseMI = findOnlyInterestingUse (Reg, MBB, MRI, TII,IsCopy,
732
- NewReg, IsDstPhys, LIS )) {
750
+ while (MachineInstr *UseMI =
751
+ findOnlyInterestingUse (Reg, MBB, IsCopy, NewReg, IsDstPhys)) {
733
752
if (IsCopy && !Processed.insert (UseMI).second )
734
753
break ;
735
754
@@ -781,7 +800,7 @@ void TwoAddressInstructionPass::processCopy(MachineInstr *MI) {
781
800
782
801
bool IsSrcPhys, IsDstPhys;
783
802
Register SrcReg, DstReg;
784
- if (!isCopyToReg (*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
803
+ if (!isCopyToReg (*MI, SrcReg, DstReg, IsSrcPhys, IsDstPhys))
785
804
return ;
786
805
787
806
if (IsDstPhys && !IsSrcPhys) {
@@ -865,7 +884,7 @@ bool TwoAddressInstructionPass::rescheduleMIBelowKill(
865
884
Defs.push_back (MOReg);
866
885
else {
867
886
Uses.push_back (MOReg);
868
- if (MOReg != Reg && isPlainlyKilled (MO, LIS ))
887
+ if (MOReg != Reg && isPlainlyKilled (MO))
869
888
Kills.push_back (MOReg);
870
889
}
871
890
}
@@ -876,7 +895,7 @@ bool TwoAddressInstructionPass::rescheduleMIBelowKill(
876
895
MachineBasicBlock::iterator End = AfterMI;
877
896
while (End != MBB->end ()) {
878
897
End = skipDebugInstructionsForward (End, MBB->end ());
879
- if (End->isCopy () && regOverlapsSet (Defs, End->getOperand (1 ).getReg (), TRI ))
898
+ if (End->isCopy () && regOverlapsSet (Defs, End->getOperand (1 ).getReg ()))
880
899
Defs.push_back (End->getOperand (0 ).getReg ());
881
900
else
882
901
break ;
@@ -905,20 +924,20 @@ bool TwoAddressInstructionPass::rescheduleMIBelowKill(
905
924
if (!MOReg)
906
925
continue ;
907
926
if (MO.isDef ()) {
908
- if (regOverlapsSet (Uses, MOReg, TRI ))
927
+ if (regOverlapsSet (Uses, MOReg))
909
928
// Physical register use would be clobbered.
910
929
return false ;
911
- if (!MO.isDead () && regOverlapsSet (Defs, MOReg, TRI ))
930
+ if (!MO.isDead () && regOverlapsSet (Defs, MOReg))
912
931
// May clobber a physical register def.
913
932
// FIXME: This may be too conservative. It's ok if the instruction
914
933
// is sunken completely below the use.
915
934
return false ;
916
935
} else {
917
- if (regOverlapsSet (Defs, MOReg, TRI ))
936
+ if (regOverlapsSet (Defs, MOReg))
918
937
return false ;
919
- bool isKill = isPlainlyKilled (MO, LIS );
920
- if (MOReg != Reg && ((isKill && regOverlapsSet (Uses, MOReg, TRI )) ||
921
- regOverlapsSet (Kills, MOReg, TRI )))
938
+ bool isKill = isPlainlyKilled (MO);
939
+ if (MOReg != Reg && ((isKill && regOverlapsSet (Uses, MOReg)) ||
940
+ regOverlapsSet (Kills, MOReg)))
922
941
// Don't want to extend other live ranges and update kills.
923
942
return false ;
924
943
if (MOReg == Reg && !isKill)
@@ -1044,7 +1063,7 @@ bool TwoAddressInstructionPass::rescheduleKillAboveMI(
1044
1063
continue ;
1045
1064
if (isDefTooClose (MOReg, DI->second , MI))
1046
1065
return false ;
1047
- bool isKill = isPlainlyKilled (MO, LIS );
1066
+ bool isKill = isPlainlyKilled (MO);
1048
1067
if (MOReg == Reg && !isKill)
1049
1068
return false ;
1050
1069
Uses.push_back (MOReg);
@@ -1079,14 +1098,14 @@ bool TwoAddressInstructionPass::rescheduleKillAboveMI(
1079
1098
if (!MOReg)
1080
1099
continue ;
1081
1100
if (MO.isUse ()) {
1082
- if (regOverlapsSet (Defs, MOReg, TRI ))
1101
+ if (regOverlapsSet (Defs, MOReg))
1083
1102
// Moving KillMI can clobber the physical register if the def has
1084
1103
// not been seen.
1085
1104
return false ;
1086
- if (regOverlapsSet (Kills, MOReg, TRI ))
1105
+ if (regOverlapsSet (Kills, MOReg))
1087
1106
// Don't want to extend other live ranges and update kills.
1088
1107
return false ;
1089
- if (&OtherMI != MI && MOReg == Reg && !isPlainlyKilled (MO, LIS ))
1108
+ if (&OtherMI != MI && MOReg == Reg && !isPlainlyKilled (MO))
1090
1109
// We can't schedule across a use of the register in question.
1091
1110
return false ;
1092
1111
} else {
@@ -1096,9 +1115,9 @@ bool TwoAddressInstructionPass::rescheduleKillAboveMI(
1096
1115
1097
1116
for (unsigned i = 0 , e = OtherDefs.size (); i != e; ++i) {
1098
1117
Register MOReg = OtherDefs[i];
1099
- if (regOverlapsSet (Uses, MOReg, TRI ))
1118
+ if (regOverlapsSet (Uses, MOReg))
1100
1119
return false ;
1101
- if (MOReg.isPhysical () && regOverlapsSet (LiveDefs, MOReg, TRI ))
1120
+ if (MOReg.isPhysical () && regOverlapsSet (LiveDefs, MOReg))
1102
1121
return false ;
1103
1122
// Physical register def is seen.
1104
1123
llvm::erase_value (Defs, MOReg);
@@ -1169,7 +1188,7 @@ bool TwoAddressInstructionPass::tryInstructionCommute(MachineInstr *MI,
1169
1188
1170
1189
// If OtherOp dies but BaseOp does not, swap the OtherOp and BaseOp
1171
1190
// operands. This makes the live ranges of DstOp and OtherOp joinable.
1172
- bool OtherOpKilled = isKilled (*MI, OtherOpReg, MRI, TII, LIS, false );
1191
+ bool OtherOpKilled = isKilled (*MI, OtherOpReg, false );
1173
1192
bool DoCommute = !BaseOpKilled && OtherOpKilled;
1174
1193
1175
1194
if (!DoCommute &&
@@ -1220,7 +1239,7 @@ tryInstructionTransform(MachineBasicBlock::iterator &mi,
1220
1239
Register regB = MI.getOperand (SrcIdx).getReg ();
1221
1240
1222
1241
assert (regB.isVirtual () && " cannot make instruction into two-address form" );
1223
- bool regBKilled = isKilled (MI, regB, MRI, TII, LIS, true );
1242
+ bool regBKilled = isKilled (MI, regB, true );
1224
1243
1225
1244
if (regA.isVirtual ())
1226
1245
scanUses (regA);
@@ -1252,7 +1271,7 @@ tryInstructionTransform(MachineBasicBlock::iterator &mi,
1252
1271
// confusing the three address conversion below.
1253
1272
if (Commuted) {
1254
1273
regB = MI.getOperand (SrcIdx).getReg ();
1255
- regBKilled = isKilled (MI, regB, MRI, TII, LIS, true );
1274
+ regBKilled = isKilled (MI, regB, true );
1256
1275
}
1257
1276
1258
1277
if (MI.isConvertibleTo3Addr ()) {
0 commit comments