Skip to content

Commit f4c6f49

Browse files
author
Romaric JODIN
committed
dpu: llvm: fix merge of combo when having debug instructions
fix #19
1 parent 515169b commit f4c6f49

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

llvm/lib/Target/DPU/DPUMergeComboInstrPass.cpp

+55-13
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ static const ISD::CondCode sourceConditions[] = {
178178
ISD::SETOEQ, ISD::SETOGE, ISD::SETOLT, ISD::SETONE, ISD::SETUEQ,
179179
ISD::SETEQ, ISD::SETGE, ISD::SETLT, ISD::SETNE};
180180

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+
181195
static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
182196
const DPUInstrInfo &InstrInfo) {
183197
MachineBasicBlock::reverse_iterator I = MBB->rbegin(), REnd = MBB->rend();
@@ -201,28 +215,24 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
201215
std::set<ISD::CondCode> sourceConditionsSet = std::set<ISD::CondCode>(
202216
std::begin(sourceConditions), std::end(sourceConditions));
203217

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");
210221
return false;
211222
}
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");
216227
return false;
217228
}
218229

219-
SecondLastInst = &*I;
220-
221230
LastOpc = LastInst->getOpcode();
222231
SecondLastOpc = SecondLastInst->getOpcode();
223232

224233
switch (SecondLastOpc) {
225234
default:
235+
LLVM_DEBUG(dbgs() << "KO: Unknown SecondLastOpc\n");
226236
return false;
227237
case DPU::MOVEri:
228238
OpPrototype = OpriLimited;
@@ -600,6 +610,8 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
600610
if ((OpPrototype == OprriLimited) || (OpPrototype == OprirLimited)) {
601611
MachineOperand &immOperand = SecondLastInst->getOperand(2);
602612
if (!immOperand.isImm()) {
613+
LLVM_DEBUG(dbgs() << "KO: (OpPrototype == OprriLimited) || (OpPrototype "
614+
"== OprirLimited) && !immOperand.isImm()\n");
603615
return false;
604616
}
605617

@@ -609,6 +621,9 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
609621
} else if (OpPrototype == OpriLimited) {
610622
MachineOperand &immOperand = SecondLastInst->getOperand(1);
611623
if (!immOperand.isImm()) {
624+
LLVM_DEBUG(
625+
dbgs()
626+
<< "KO: (OpPrototype == OpriLimited) && !immOperand.isImm()\n");
612627
return false;
613628
}
614629

@@ -619,9 +634,12 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
619634

620635
switch (LastOpc) {
621636
default:
637+
LLVM_DEBUG(dbgs() << "KO: Unknown LastOpc\n");
622638
return false;
623639
case DPU::JUMPi: {
624640
if (!ImmCanBeEncodedOn8Bits) {
641+
LLVM_DEBUG(
642+
dbgs() << "KO: LastOpc == DPU::JUMPi && !ImmCanBeEncodedOn8Bits\n");
625643
return false;
626644
}
627645

@@ -653,6 +671,7 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
653671
auto actualConditionOperand = MachineOperand::CreateImm(actualCondition);
654672
ComboInst.add(actualConditionOperand).add(LastInst->getOperand(0));
655673

674+
LLVM_DEBUG(dbgs() << "OK\n"; LastInst->dump(); SecondLastInst->dump(););
656675
LastInst->eraseFromParent();
657676
SecondLastInst->eraseFromParent();
658677

@@ -673,12 +692,18 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
673692
case Oprr:
674693
if ((SecondLastInst->getOperand(1).getReg() !=
675694
LastInst->getOperand(1).getReg())) {
695+
LLVM_DEBUG(dbgs() << "KO: LastOpc == DPU::Jcci && "
696+
"(SecondLastInst->getOperand(1).getReg() != "
697+
"LastInst->getOperand(1).getReg())\n");
676698
return false;
677699
}
678700
isSourceCondition = true;
679701
usableConditions = sourceConditionsSet;
680702
break;
681703
case OpriLimited:
704+
LLVM_DEBUG(
705+
dbgs()
706+
<< "KO: LastOpc == DPU::Jcci && OpPrototype == OpriLimited\n");
682707
return false;
683708
}
684709
}
@@ -692,6 +717,11 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
692717
if (!canFindUnaryConditionForCondition(
693718
LastInst->getOperand(0).getImm(), LastInst->getOperand(2).getImm(),
694719
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");
695725
return false;
696726
}
697727

@@ -705,14 +735,23 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
705735

706736
if (LastInst->getOperand(1).isKill() && !isSourceCondition) {
707737
if (!ImmCanBeEncodedOn11Bits) {
738+
LLVM_DEBUG(
739+
dbgs()
740+
<< "KO: LastOpc == DPU::Jcci && (LastInst->getOperand(1).isKill() "
741+
"&& !isSourceCondition) && (!ImmCanBeEncodedOn11Bits)\n");
708742
return false;
709743
}
710744
// todo: this is not optimal. One register has been allocated but not used
711745
// now. This can become an issue (unnecessary spilling)
712746
ComboInst = BuildMI(MBB, SecondLastInst->getDebugLoc(),
713-
InstrInfo.get(OpNullJumpOpc)).addReg(DPU::ZERO);
747+
InstrInfo.get(OpNullJumpOpc))
748+
.addReg(DPU::ZERO);
714749
} else {
715750
if (!ImmCanBeEncodedOn8Bits) {
751+
LLVM_DEBUG(
752+
dbgs()
753+
<< "KO: LastOpc == DPU::Jcci && !(LastInst->getOperand(1).isKill() "
754+
"&& !isSourceCondition) && (!ImmCanBeEncodedOn11Bits)\n");
716755
return false;
717756
}
718757
ComboInst =
@@ -743,12 +782,14 @@ static bool mergeComboInstructionsInMBB(MachineBasicBlock *MBB,
743782
ComboInst.add(LastInst->getOperand(0))
744783
.add(LastInst->getOperand(LastInst->getNumOperands() - 1));
745784

785+
LLVM_DEBUG(dbgs() << "OK\n"; LastInst->dump(); SecondLastInst->dump(););
746786
LastInst->eraseFromParent();
747787
SecondLastInst->eraseFromParent();
748788

749789
return true;
750790
}
751791
case DPU::Jcc:
792+
LLVM_DEBUG(dbgs() << "KO: LastOpc == DPU::Jcc\n");
752793
return false;
753794
}
754795
}
@@ -764,6 +805,7 @@ bool DPUMergeComboInstrPass::runOnMachineFunction(MachineFunction &MF) {
764805
for (auto &MFI : MF) {
765806
MachineBasicBlock *MBB = &MFI;
766807

808+
LLVM_DEBUG(MBB->dump());
767809
changeMade |= mergeComboInstructionsInMBB(MBB, InstrInfo);
768810
}
769811

0 commit comments

Comments
 (0)