@@ -178,6 +178,20 @@ static const ISD::CondCode sourceConditions[] = {
178
178
ISD::SETOEQ, ISD::SETOGE, ISD::SETOLT, ISD::SETONE, ISD::SETUEQ,
179
179
ISD::SETEQ, ISD::SETGE, ISD::SETLT, ISD::SETNE};
180
180
181
+ static MachineInstr *
182
+ getLastNonDebugInstrFrom (MachineBasicBlock::reverse_iterator &I,
183
+ MachineBasicBlock::reverse_iterator REnd) {
184
+ // Skip all the debug instructions.
185
+ while (I != REnd &&
186
+ (I->isDebugValue () || I->getOpcode () == TargetOpcode::DBG_VALUE)) {
187
+ ++I;
188
+ }
189
+ if (I == REnd) {
190
+ return NULL ;
191
+ }
192
+ return &*I;
193
+ }
194
+
181
195
static bool mergeComboInstructionsInMBB (MachineBasicBlock *MBB,
182
196
const DPUInstrInfo &InstrInfo) {
183
197
MachineBasicBlock::reverse_iterator I = MBB->rbegin (), REnd = MBB->rend ();
@@ -201,28 +215,24 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
201
215
std::set<ISD::CondCode> sourceConditionsSet = std::set<ISD::CondCode>(
202
216
std::begin (sourceConditions), std::end (sourceConditions));
203
217
204
- // Skip all the debug instructions.
205
- while (I != REnd && I->isDebugValue ()) {
206
- ++I;
207
- }
208
-
209
- if (I == REnd) {
218
+ LastInst = getLastNonDebugInstrFrom (I, REnd);
219
+ if (LastInst == NULL ) {
220
+ LLVM_DEBUG (dbgs () << " KO: I == REnd\n " );
210
221
return false ;
211
222
}
212
-
213
- LastInst = &*I ;
214
-
215
- if (++I == REnd) {
223
+ I++;
224
+ SecondLastInst = getLastNonDebugInstrFrom (I, REnd) ;
225
+ if (SecondLastInst == NULL ) {
226
+ LLVM_DEBUG ( dbgs () << " KO: I++ == REnd\n " );
216
227
return false ;
217
228
}
218
229
219
- SecondLastInst = &*I;
220
-
221
230
LastOpc = LastInst->getOpcode ();
222
231
SecondLastOpc = SecondLastInst->getOpcode ();
223
232
224
233
switch (SecondLastOpc) {
225
234
default :
235
+ LLVM_DEBUG (dbgs () << " KO: Unknown SecondLastOpc\n " );
226
236
return false ;
227
237
case DPU::MOVEri:
228
238
OpPrototype = OpriLimited;
@@ -600,6 +610,8 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
600
610
if ((OpPrototype == OprriLimited) || (OpPrototype == OprirLimited)) {
601
611
MachineOperand &immOperand = SecondLastInst->getOperand (2 );
602
612
if (!immOperand.isImm ()) {
613
+ LLVM_DEBUG (dbgs () << " KO: (OpPrototype == OprriLimited) || (OpPrototype "
614
+ " == OprirLimited) && !immOperand.isImm()\n " );
603
615
return false ;
604
616
}
605
617
@@ -609,6 +621,9 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
609
621
} else if (OpPrototype == OpriLimited) {
610
622
MachineOperand &immOperand = SecondLastInst->getOperand (1 );
611
623
if (!immOperand.isImm ()) {
624
+ LLVM_DEBUG (
625
+ dbgs ()
626
+ << " KO: (OpPrototype == OpriLimited) && !immOperand.isImm()\n " );
612
627
return false ;
613
628
}
614
629
@@ -619,9 +634,12 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
619
634
620
635
switch (LastOpc) {
621
636
default :
637
+ LLVM_DEBUG (dbgs () << " KO: Unknown LastOpc\n " );
622
638
return false ;
623
639
case DPU::JUMPi: {
624
640
if (!ImmCanBeEncodedOn8Bits) {
641
+ LLVM_DEBUG (
642
+ dbgs () << " KO: LastOpc == DPU::JUMPi && !ImmCanBeEncodedOn8Bits\n " );
625
643
return false ;
626
644
}
627
645
@@ -653,6 +671,7 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
653
671
auto actualConditionOperand = MachineOperand::CreateImm (actualCondition);
654
672
ComboInst.add (actualConditionOperand).add (LastInst->getOperand (0 ));
655
673
674
+ LLVM_DEBUG (dbgs () << " OK\n " ; LastInst->dump (); SecondLastInst->dump (););
656
675
LastInst->eraseFromParent ();
657
676
SecondLastInst->eraseFromParent ();
658
677
@@ -673,12 +692,18 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
673
692
case Oprr:
674
693
if ((SecondLastInst->getOperand (1 ).getReg () !=
675
694
LastInst->getOperand (1 ).getReg ())) {
695
+ LLVM_DEBUG (dbgs () << " KO: LastOpc == DPU::Jcci && "
696
+ " (SecondLastInst->getOperand(1).getReg() != "
697
+ " LastInst->getOperand(1).getReg())\n " );
676
698
return false ;
677
699
}
678
700
isSourceCondition = true ;
679
701
usableConditions = sourceConditionsSet;
680
702
break ;
681
703
case OpriLimited:
704
+ LLVM_DEBUG (
705
+ dbgs ()
706
+ << " KO: LastOpc == DPU::Jcci && OpPrototype == OpriLimited\n " );
682
707
return false ;
683
708
}
684
709
}
@@ -692,6 +717,11 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
692
717
if (!canFindUnaryConditionForCondition (
693
718
LastInst->getOperand (0 ).getImm (), LastInst->getOperand (2 ).getImm (),
694
719
actualCondition, availableConditions)) {
720
+ LLVM_DEBUG (
721
+ dbgs () << " KO: LastOpc == DPU::Jcci && "
722
+ " (!canFindUnaryConditionForCondition(LastInst->getOperand("
723
+ " 0).getImm(), LastInst->getOperand(2).getImm(), "
724
+ " actualCondition, availableConditions))\n " );
695
725
return false ;
696
726
}
697
727
@@ -705,14 +735,23 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
705
735
706
736
if (LastInst->getOperand (1 ).isKill () && !isSourceCondition) {
707
737
if (!ImmCanBeEncodedOn11Bits) {
738
+ LLVM_DEBUG (
739
+ dbgs ()
740
+ << " KO: LastOpc == DPU::Jcci && (LastInst->getOperand(1).isKill() "
741
+ " && !isSourceCondition) && (!ImmCanBeEncodedOn11Bits)\n " );
708
742
return false ;
709
743
}
710
744
// todo: this is not optimal. One register has been allocated but not used
711
745
// now. This can become an issue (unnecessary spilling)
712
746
ComboInst = BuildMI (MBB, SecondLastInst->getDebugLoc (),
713
- InstrInfo.get (OpNullJumpOpc)).addReg (DPU::ZERO);
747
+ InstrInfo.get (OpNullJumpOpc))
748
+ .addReg (DPU::ZERO);
714
749
} else {
715
750
if (!ImmCanBeEncodedOn8Bits) {
751
+ LLVM_DEBUG (
752
+ dbgs ()
753
+ << " KO: LastOpc == DPU::Jcci && !(LastInst->getOperand(1).isKill() "
754
+ " && !isSourceCondition) && (!ImmCanBeEncodedOn11Bits)\n " );
716
755
return false ;
717
756
}
718
757
ComboInst =
@@ -743,12 +782,14 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
743
782
ComboInst.add (LastInst->getOperand (0 ))
744
783
.add (LastInst->getOperand (LastInst->getNumOperands () - 1 ));
745
784
785
+ LLVM_DEBUG (dbgs () << " OK\n " ; LastInst->dump (); SecondLastInst->dump (););
746
786
LastInst->eraseFromParent ();
747
787
SecondLastInst->eraseFromParent ();
748
788
749
789
return true ;
750
790
}
751
791
case DPU::Jcc:
792
+ LLVM_DEBUG (dbgs () << " KO: LastOpc == DPU::Jcc\n " );
752
793
return false ;
753
794
}
754
795
}
@@ -764,6 +805,7 @@ bool DPUMergeComboInstrPass::runOnMachineFunction(MachineFunction &MF) {
764
805
for (auto &MFI : MF) {
765
806
MachineBasicBlock *MBB = &MFI;
766
807
808
+ LLVM_DEBUG (MBB->dump ());
767
809
changeMade |= mergeComboInstructionsInMBB (MBB, InstrInfo);
768
810
}
769
811
0 commit comments