49 #define DEBUG_TYPE "structcfg"
51 #define DEFAULT_VEC_SLOTS 8
61 STATISTIC(numSerialPatternMatch,
"CFGStructurizer number of serial pattern "
63 STATISTIC(numIfPatternMatch,
"CFGStructurizer number of if pattern "
65 STATISTIC(numClonedBlock,
"CFGStructurizer cloned blocks");
66 STATISTIC(numClonedInstr,
"CFGStructurizer cloned instructions");
82 #define SHOWNEWINSTR(i) \
83 DEBUG(dbgs() << "New instr: " << *i << "\n");
85 #define SHOWNEWBLK(b, msg) \
87 dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
91 #define SHOWBLK_DETAIL(b, msg) \
94 dbgs() << msg << "BB" << b->getNumber() << "size " << b->size(); \
100 #define INVALIDSCCNUM -1
108 class BlockInformation {
110 bool IsRetired =
false;
113 BlockInformation() =
default;
125 typedef std::map<MachineBasicBlock *, BlockInformation *> MBBInfoMap;
126 typedef std::map<MachineLoop *, MachineBasicBlock *> LoopLandInfoMap;
130 SinglePath_InPath = 1,
131 SinglePath_NotInPath = 2
141 return "AMDGPU Control Flow Graph structurizer Pass";
166 MLI = &getAnalysis<MachineLoopInfo>();
167 DEBUG(
dbgs() <<
"LoopInfo:\n"; PrintLoopinfo(*MLI););
168 MDT = &getAnalysis<MachineDominatorTree>();
170 PDT = &getAnalysis<MachinePostDominatorTree>();
187 void printOrderedBlocks()
const {
190 iterBlkEnd = OrderedBlks.end(); iterBlk != iterBlkEnd; ++iterBlk, ++i) {
191 dbgs() <<
"BB" << (*iterBlk)->getNumber();
192 dbgs() <<
"(" << getSCCNum(*iterBlk) <<
"," << (*iterBlk)->size() <<
")";
193 if (i != 0 && i % 10 == 0) {
203 iterEnd = LoopInfo.
end(); iter != iterEnd; ++iter) {
204 (*iter)->print(
dbgs(), 0);
215 bool AllowSideEntry =
true)
const;
237 static int getBranchNzeroOpcode(
int OldOpcode);
238 static int getBranchZeroOpcode(
int OldOpcode);
239 static int getContinueNzeroOpcode(
int OldOpcode);
240 static int getContinueZeroOpcode(
int OldOpcode);
270 int loopendPatternMatch();
331 MBBInfoMap BlockInfoMap;
332 LoopLandInfoMap LLInfoMap;
333 std::map<MachineLoop *, bool> Visited;
343 MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
344 if (It == BlockInfoMap.end())
346 return (*It).second->SccNum;
351 LoopLandInfoMap::const_iterator It = LLInfoMap.find(LoopRep);
352 if (It == LLInfoMap.end())
366 MBBInfoMap::const_iterator It = BlockInfoMap.find(MBB);
367 if (It == BlockInfoMap.end())
369 return (*It).second->IsRetired;
374 while (LoopRep && LoopRep->
getHeader() == MBB) {
378 if (!isRetiredBlock(LoopLand))
385 AMDGPUCFGStructurizer::PathToKind AMDGPUCFGStructurizer::singlePathTo(
387 bool AllowSideEntry)
const {
389 if (SrcMBB == DstMBB)
390 return SinglePath_InPath;
391 while (SrcMBB && SrcMBB->
succ_size() == 1) {
393 if (SrcMBB == DstMBB)
394 return SinglePath_InPath;
395 if (!AllowSideEntry && SrcMBB->
pred_size() > 1)
396 return Not_SinglePath;
399 return SinglePath_NotInPath;
400 return Not_SinglePath;
407 if (!isRetiredBlock(*It))
415 unsigned BlockSizeThreshold = 30;
416 unsigned CloneInstrThreshold = 100;
417 bool MultiplePreds = MBB && (MBB->
pred_size() > 1);
421 unsigned BlkSize = MBB->
size();
422 return ((BlkSize > BlockSizeThreshold) &&
423 (BlkSize * (MBB->
pred_size() - 1) > CloneInstrThreshold));
426 void AMDGPUCFGStructurizer::reversePredicateSetter(
432 if (I->getOpcode() == AMDGPU::PRED_X) {
433 switch (I->getOperand(2).getImm()) {
434 case AMDGPU::PRED_SETE_INT:
435 I->getOperand(2).setImm(AMDGPU::PRED_SETNE_INT);
437 case AMDGPU::PRED_SETNE_INT:
438 I->getOperand(2).setImm(AMDGPU::PRED_SETE_INT);
440 case AMDGPU::PRED_SETE:
441 I->getOperand(2).setImm(AMDGPU::PRED_SETNE);
443 case AMDGPU::PRED_SETNE:
444 I->getOperand(2).setImm(AMDGPU::PRED_SETE);
487 void AMDGPUCFGStructurizer::insertCondBranchBefore(
500 void AMDGPUCFGStructurizer::insertCondBranchBefore(
511 int AMDGPUCFGStructurizer::getBranchNzeroOpcode(
int OldOpcode) {
513 case AMDGPU::JUMP_COND:
514 case AMDGPU::JUMP:
return AMDGPU::IF_PREDICATE_SET;
515 case AMDGPU::BRANCH_COND_i32:
516 case AMDGPU::BRANCH_COND_f32:
return AMDGPU::IF_LOGICALNZ_f32;
522 int AMDGPUCFGStructurizer::getBranchZeroOpcode(
int OldOpcode) {
524 case AMDGPU::JUMP_COND:
525 case AMDGPU::JUMP:
return AMDGPU::IF_PREDICATE_SET;
526 case AMDGPU::BRANCH_COND_i32:
527 case AMDGPU::BRANCH_COND_f32:
return AMDGPU::IF_LOGICALZ_f32;
533 int AMDGPUCFGStructurizer::getContinueNzeroOpcode(
int OldOpcode) {
535 case AMDGPU::JUMP_COND:
536 case AMDGPU::JUMP:
return AMDGPU::CONTINUE_LOGICALNZ_i32;
542 int AMDGPUCFGStructurizer::getContinueZeroOpcode(
int OldOpcode) {
544 case AMDGPU::JUMP_COND:
545 case AMDGPU::JUMP:
return AMDGPU::CONTINUE_LOGICALZ_i32;
555 void AMDGPUCFGStructurizer::setTrueBranch(
MachineInstr *MI,
568 return (*It == TrueBranch) ? *Next : *It;
571 bool AMDGPUCFGStructurizer::isCondBranch(
MachineInstr *MI) {
573 case AMDGPU::JUMP_COND:
574 case AMDGPU::BRANCH_COND_i32:
575 case AMDGPU::BRANCH_COND_f32:
return true;
582 bool AMDGPUCFGStructurizer::isUncondBranch(
MachineInstr *MI) {
605 MachineInstr *AMDGPUCFGStructurizer::getNormalBlockBranchInstr(
609 if (MI && (isCondBranch(MI) || isUncondBranch(MI)))
614 MachineInstr *AMDGPUCFGStructurizer::getLoopendBlockBranchInstr(
621 if (isCondBranch(MI) || isUncondBranch(MI))
632 if (It != MBB->
rend()) {
648 <<
" is return block without RETURN instr\n";);
655 iterEnd = SrcMBB->
succ_end(); It != iterEnd; ++It)
668 void AMDGPUCFGStructurizer::replaceInstrUseOfBlockWith(
671 MachineInstr *BranchMI = getLoopendBlockBranchInstr(SrcMBB);
672 if (BranchMI && isCondBranch(BranchMI) &&
673 getTrueBranch(BranchMI) == OldMBB)
674 setTrueBranch(BranchMI, NewBlk);
680 &&
"found a jump table");
688 if (Pre->getOpcode() == AMDGPU::CONTINUE
689 && It->getOpcode() == AMDGPU::ENDLOOP)
696 for (
unsigned i = 0; i < ContInstr.
size(); ++i)
697 ContInstr[i]->eraseFromParent();
705 bool AMDGPUCFGStructurizer::prepare() {
706 bool Changed =
false;
710 DEBUG(
dbgs() <<
"AMDGPUCFGStructurizer::prepare\n";);
712 orderBlocks(FuncRep);
718 E = MLI->end(); It !=
E; ++It) {
723 if (ExitingMBBs.
size() == 0) {
733 It = OrderedBlks.begin(), E = OrderedBlks.end(); It !=
E; ++It) {
735 removeUnconditionalBranch(MBB);
736 removeRedundantConditionalBranch(MBB);
737 if (isReturnBlock(MBB)) {
743 if (RetBlks.
size() >= 2) {
744 addDummyExitBlock(RetBlks);
751 bool AMDGPUCFGStructurizer::run() {
753 DEBUG(
dbgs() <<
"AMDGPUCFGStructurizer::run\n");
757 ReverseVector(orderedBlks);
760 DEBUG(
dbgs() <<
"Ordered blocks:\n"; printOrderedBlocks(););
764 bool MakeProgress =
false;
765 int NumRemainedBlk = countActiveBlock(OrderedBlks.begin(),
771 dbgs() <<
"numIter = " << NumIter
772 <<
", numRemaintedBlk = " << NumRemainedBlk <<
"\n";
794 SccNumBlk = NumRemainedBlk;
796 dbgs() <<
"start processing SCC" << getSCCNum(SccBeginMBB);
801 if (!isRetiredBlock(MBB))
806 bool ContNextScc =
true;
808 || getSCCNum(SccBeginMBB) != getSCCNum(*It)) {
811 int sccRemainedNumBlk = countActiveBlock(SccBeginIter, It);
812 if (sccRemainedNumBlk != 1 && sccRemainedNumBlk >= SccNumBlk) {
814 dbgs() <<
"Can't reduce SCC " << getSCCNum(MBB)
815 <<
", sccNumIter = " << SccNumIter;
816 dbgs() <<
"doesn't make any progress\n";
819 }
else if (sccRemainedNumBlk != 1 && sccRemainedNumBlk < SccNumBlk) {
820 SccNumBlk = sccRemainedNumBlk;
824 dbgs() <<
"repeat processing SCC" << getSCCNum(MBB)
825 <<
"sccNumIter = " << SccNumIter <<
'\n';
837 SccBeginMBB =
nullptr;
845 dbgs() <<
"Reduce to one block\n";
848 int NewnumRemainedBlk
849 = countActiveBlock(OrderedBlks.begin(), OrderedBlks.end());
851 if (NewnumRemainedBlk == 1 || NewnumRemainedBlk < NumRemainedBlk) {
853 NumRemainedBlk = NewnumRemainedBlk;
855 MakeProgress =
false;
857 dbgs() <<
"No progress\n";
861 }
while (!Finish && MakeProgress);
867 for (MBBInfoMap::iterator It = BlockInfoMap.begin(), E = BlockInfoMap.
end();
869 if ((*It).second && (*It).second->IsRetired) {
870 assert(((*It).first)->getNumber() != -1);
872 dbgs() <<
"Erase BB" << ((*It).first)->getNumber() <<
"\n";
874 (*It).first->eraseFromParent();
878 BlockInfoMap.clear();
882 DEBUG(FuncRep->viewCFG());
894 const std::vector<MachineBasicBlock *> &SccNext = *It;
895 for (std::vector<MachineBasicBlock *>::const_iterator
896 blockIter = SccNext.begin(), blockEnd = SccNext.end();
897 blockIter != blockEnd; ++blockIter) {
900 recordSccnum(MBB, SccNum);
905 for (
auto *MBB :
nodes(MF)) {
906 SccNum = getSCCNum(MBB);
908 dbgs() <<
"unreachable block BB" << MBB->
getNumber() <<
"\n";
917 dbgs() <<
"Begin patternMatch BB" << MBB->
getNumber() <<
"\n";
920 while ((CurMatch = patternMatchGroup(MBB)) > 0)
921 NumMatch += CurMatch;
925 <<
", numMatch = " << NumMatch <<
"\n";
933 NumMatch += loopendPatternMatch();
934 NumMatch += serialPatternMatch(MBB);
935 NumMatch += ifPatternMatch(MBB);
944 if (childBlk->
pred_size() != 1 || isActiveLoophead(childBlk))
947 mergeSerialBlock(MBB, childBlk);
948 ++numSerialPatternMatch;
956 if (hasBackEdge(MBB))
958 MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
962 assert(isCondBranch(BranchMI));
966 NumMatch += serialPatternMatch(TrueMBB);
967 NumMatch += ifPatternMatch(TrueMBB);
969 NumMatch += serialPatternMatch(FalseMBB);
970 NumMatch += ifPatternMatch(FalseMBB);
989 reversePredicateSetter(MBB->
end(), *MBB);
993 && isSameloopDetachedContbreak(TrueMBB, FalseMBB)) {
996 && isSameloopDetachedContbreak(FalseMBB, TrueMBB)) {
999 return NumMatch + handleJumpintoIf(MBB, TrueMBB, FalseMBB);
1007 || (FalseMBB && FalseMBB->
pred_size() > 1))) {
1008 Cloned += improveSimpleJumpintoIf(MBB, TrueMBB, FalseMBB, &LandBlk);
1011 if (TrueMBB && TrueMBB->
pred_size() > 1) {
1012 TrueMBB = cloneBlockForPredecessor(TrueMBB, MBB);
1016 if (FalseMBB && FalseMBB->
pred_size() > 1) {
1017 FalseMBB = cloneBlockForPredecessor(FalseMBB, MBB);
1021 mergeIfthenelseBlock(BranchMI, MBB, TrueMBB, FalseMBB, LandBlk);
1023 ++numIfPatternMatch;
1025 numClonedBlock += Cloned;
1027 return 1 + Cloned + NumMatch;
1030 int AMDGPUCFGStructurizer::loopendPatternMatch() {
1031 std::deque<MachineLoop *> NestedLoops;
1032 for (
auto &It: *MLI)
1034 NestedLoops.push_front(ML);
1036 if (NestedLoops.empty())
1044 if (ExaminedLoop->getNumBlocks() == 0 || Visited[ExaminedLoop])
1046 DEBUG(
dbgs() <<
"Processing:\n"; ExaminedLoop->dump(););
1047 int NumBreak = mergeLoop(ExaminedLoop);
1055 int AMDGPUCFGStructurizer::mergeLoop(
MachineLoop *LoopRep) {
1059 assert(!ExitingMBBs.
empty() &&
"Infinite Loop not supported");
1060 DEBUG(
dbgs() <<
"Loop has " << ExitingMBBs.
size() <<
" exiting blocks\n";);
1065 for (
unsigned i = 0, e = ExitBlks.size(); i < e; ++i)
1066 ExitBlkSet.
insert(ExitBlks[i]);
1069 assert(ExitBlk &&
"Loop has several exit block");
1071 for (
auto *LB : inverse_children<MachineBasicBlock*>(LoopHeader))
1075 for (
unsigned i = 0, e = ExitingMBBs.
size(); i < e; ++i)
1076 mergeLoopbreakBlock(ExitingMBBs[i], ExitBlk);
1077 for (
unsigned i = 0, e = LatchBlks.
size(); i < e; ++i)
1078 settleLoopcontBlock(LatchBlks[i], LoopHeader);
1082 Match += serialPatternMatch(LoopHeader);
1083 Match += ifPatternMatch(LoopHeader);
1084 }
while (Match > 0);
1085 mergeLooplandBlock(LoopHeader, ExitBlk);
1088 MLI->changeLoopFor(LoopHeader, ParentLoop);
1090 MLI->removeBlock(LoopHeader);
1091 Visited[LoopRep] =
true;
1095 bool AMDGPUCFGStructurizer::isSameloopDetachedContbreak(
1099 if (LoopRep&& LoopRep == MLI->getLoopFor(Src2MBB)) {
1103 dbgs() <<
"isLoopContBreakBlock yes src1 = BB"
1105 <<
" src2 = BB" << Src2MBB->
getNumber() <<
"\n";
1116 int Num = handleJumpintoIfImp(HeadMBB, TrueMBB, FalseMBB);
1119 dbgs() <<
"handleJumpintoIf swap trueBlk and FalseBlk" <<
"\n";
1121 Num = handleJumpintoIfImp(HeadMBB, FalseMBB, TrueMBB);
1135 dbgs() <<
"handleJumpintoIfImp head = BB" << HeadMBB->
getNumber()
1138 <<
" false = BB" << FalseMBB->
getNumber() <<
"\n";
1146 if (singlePathTo(FalseMBB, DownBlk) == SinglePath_InPath) {
1148 dbgs() <<
" working\n";
1151 Num += cloneOnSideEntryTo(HeadMBB, TrueMBB, DownBlk);
1152 Num += cloneOnSideEntryTo(HeadMBB, FalseMBB, DownBlk);
1154 numClonedBlock += Num;
1155 Num += serialPatternMatch(*HeadMBB->
succ_begin());
1156 Num += serialPatternMatch(*std::next(HeadMBB->
succ_begin()));
1157 Num += ifPatternMatch(HeadMBB);
1163 dbgs() <<
" not working\n";
1165 DownBlk = (DownBlk->succ_size() == 1) ? (*DownBlk->succ_begin()) :
nullptr;
1171 void AMDGPUCFGStructurizer::showImproveSimpleJumpintoIf(
1175 <<
" size = " << HeadMBB->
size();
1183 dbgs() <<
", true = BB" << TrueMBB->
getNumber() <<
" size = "
1184 << TrueMBB->
size() <<
" numPred = " << TrueMBB->
pred_size();
1192 dbgs() <<
", false = BB" << FalseMBB->
getNumber() <<
" size = "
1193 << FalseMBB->
size() <<
" numPred = " << FalseMBB->
pred_size();
1201 dbgs() <<
", land = BB" << LandMBB->
getNumber() <<
" size = "
1202 << LandMBB->
size() <<
" numPred = " << LandMBB->
pred_size();
1216 bool MigrateTrue =
false;
1217 bool MigrateFalse =
false;
1222 && (!FalseMBB || FalseMBB->
succ_size() <= 1));
1224 if (TrueMBB == FalseMBB)
1227 MigrateTrue = needMigrateBlock(TrueMBB);
1228 MigrateFalse = needMigrateBlock(FalseMBB);
1230 if (!MigrateTrue && !MigrateFalse)
1236 if (!MigrateTrue && TrueMBB && TrueMBB->
pred_size() > 1)
1238 if (!MigrateFalse && FalseMBB && FalseMBB->
pred_size() > 1)
1239 MigrateFalse =
true;
1242 dbgs() <<
"before improveSimpleJumpintoIf: ";
1243 showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
1258 if (!MigrateTrue || !MigrateFalse) {
1329 bool LandBlkHasOtherPred = (LandBlk->
pred_size() > 2);
1334 if (LandBlkHasOtherPred) {
1336 unsigned CmpResReg =
1339 insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET,
1347 insertCondBranchBefore(LandBlk, I, AMDGPU::IF_PREDICATE_SET, InitReg,
1351 migrateInstruction(TrueMBB, LandBlk, I);
1360 migrateInstruction(FalseMBB, LandBlk, I);
1367 if (LandBlkHasOtherPred) {
1369 insertInstrBefore(I, AMDGPU::ENDIF);
1373 PE = LandBlk->
pred_end(); PI != PE; ++PI) {
1375 if (MBB != TrueMBB && MBB != FalseMBB)
1380 dbgs() <<
"result from improveSimpleJumpintoIf: ";
1381 showImproveSimpleJumpintoIf(HeadMBB, TrueMBB, FalseMBB, LandBlk, 0);
1385 *LandMBBPtr = LandBlk;
1394 <<
" <= BB" << SrcMBB->
getNumber() <<
"\n";
1399 cloneSuccessorList(DstMBB, SrcMBB);
1401 removeSuccessor(SrcMBB);
1402 MLI->removeBlock(SrcMBB);
1403 retireBlock(SrcMBB);
1406 void AMDGPUCFGStructurizer::mergeIfthenelseBlock(
MachineInstr *BranchMI,
1416 dbgs() <<
" } else ";
1422 dbgs() <<
"landBlock: ";
1443 insertCondBranchBefore(I, getBranchNzeroOpcode(OldOpcode),
1451 retireBlock(TrueMBB);
1452 MLI->removeBlock(TrueMBB);
1460 if (LandMBB && FalseMBB->
succ_size() != 0)
1462 retireBlock(FalseMBB);
1463 MLI->removeBlock(FalseMBB);
1465 insertInstrBefore(I, AMDGPU::ENDIF);
1469 if (LandMBB && TrueMBB && FalseMBB)
1476 <<
" land = BB" << LandMBB->
getNumber() <<
"\n";);
1478 insertInstrBefore(DstBlk, AMDGPU::WHILELOOP,
DebugLoc());
1479 insertInstrEnd(DstBlk, AMDGPU::ENDLOOP,
DebugLoc());
1486 <<
" land = BB" << LandMBB->
getNumber() <<
"\n";);
1487 MachineInstr *BranchMI = getLoopendBlockBranchInstr(ExitingMBB);
1488 assert(BranchMI && isCondBranch(BranchMI));
1489 DebugLoc DL = BranchMI->getDebugLoc();
1492 if (TrueBranch != LandMBB)
1493 reversePredicateSetter(I, *I->getParent());
1494 insertCondBranchBefore(ExitingMBB, I, AMDGPU::IF_PREDICATE_SET, AMDGPU::PREDICATE_BIT, DL);
1495 insertInstrBefore(I, AMDGPU::BREAK);
1496 insertInstrBefore(I, AMDGPU::ENDIF);
1498 BranchMI->eraseFromParent();
1505 DEBUG(
dbgs() <<
"settleLoopcontBlock conting = BB"
1507 <<
", cont = BB" << ContMBB->
getNumber() <<
"\n";);
1509 MachineInstr *MI = getLoopendBlockBranchInstr(ContingMBB);
1511 assert(isCondBranch(MI));
1514 int OldOpcode = MI->getOpcode();
1517 bool UseContinueLogical = ((&*ContingMBB->
rbegin()) == MI);
1519 if (!UseContinueLogical) {
1521 TrueBranch == ContMBB ? getBranchNzeroOpcode(OldOpcode) :
1522 getBranchZeroOpcode(OldOpcode);
1523 insertCondBranchBefore(I, BranchOpcode, DL);
1525 insertInstrEnd(ContingMBB, AMDGPU::CONTINUE, DL);
1526 insertInstrEnd(ContingMBB, AMDGPU::ENDIF, DL);
1529 TrueBranch == ContMBB ? getContinueNzeroOpcode(OldOpcode) :
1530 getContinueZeroOpcode(OldOpcode);
1531 insertCondBranchBefore(I, BranchOpcode, DL);
1534 MI->eraseFromParent();
1541 insertInstrEnd(ContingMBB, AMDGPU::CONTINUE,
1542 getLastDebugLocInBB(ContingMBB));
1550 while (SrcMBB && SrcMBB != DstMBB) {
1553 SrcMBB = cloneBlockForPredecessor(SrcMBB, PreMBB);
1568 "succBlk is not a prececessor of curBlk");
1571 replaceInstrUseOfBlockWith(PredMBB, MBB, CloneMBB);
1577 cloneSuccessorList(CloneMBB, MBB);
1579 numClonedInstr += MBB->
size();
1582 dbgs() <<
"Cloned block: " <<
"BB"
1586 SHOWNEWBLK(CloneMBB,
"result of Cloned block: ");
1595 MachineInstr *BranchMI = getNormalBlockBranchInstr(SrcMBB);
1598 dbgs() <<
"migrateInstruction don't see branch instr\n" ;
1600 SpliceEnd = SrcMBB->
end();
1602 DEBUG(
dbgs() <<
"migrateInstruction see branch instr: " << *BranchMI);
1603 SpliceEnd = BranchMI;
1606 dbgs() <<
"migrateInstruction before splice dstSize = " << DstMBB->
size()
1607 <<
"srcSize = " << SrcMBB->
size() <<
"\n";
1611 DstMBB->
splice(I, SrcMBB, SrcMBB->
begin(), SpliceEnd);
1614 dbgs() <<
"migrateInstruction after splice dstSize = " << DstMBB->
size()
1615 <<
"srcSize = " << SrcMBB->
size() <<
'\n';
1620 AMDGPUCFGStructurizer::normalizeInfiniteLoopExit(
MachineLoop* LoopRep) {
1624 if (!LoopHeader || !LoopLatch)
1626 MachineInstr *BranchMI = getLoopendBlockBranchInstr(LoopLatch);
1628 if (!BranchMI || !isUncondBranch(BranchMI))
1633 SHOWNEWBLK(DummyExitBlk,
"DummyExitBlock to normalize infiniteLoop: ");
1634 DEBUG(
dbgs() <<
"Old branch instr: " << *BranchMI <<
"\n";);
1636 Ctx.
emitError(
"Extra register needed to handle CFG");
1645 while ((BranchMI = getLoopendBlockBranchInstr(MBB))
1646 && isUncondBranch(BranchMI)) {
1647 DEBUG(
dbgs() <<
"Removing uncond branch instr: " << *BranchMI);
1652 void AMDGPUCFGStructurizer::removeRedundantConditionalBranch(
1661 MachineInstr *BranchMI = getNormalBlockBranchInstr(MBB);
1662 assert(BranchMI && isCondBranch(BranchMI));
1663 DEBUG(
dbgs() <<
"Removing unneeded cond branch instr: " << *BranchMI);
1665 SHOWNEWBLK(MBB1,
"Removing redundant successor");
1669 void AMDGPUCFGStructurizer::addDummyExitBlock(
1676 E = RetMBB.
end(); It !=
E; ++It) {
1687 SHOWNEWBLK(DummyExitBlk,
"DummyExitBlock: ");
1697 BlockInformation *&srcBlkInfo = BlockInfoMap[MBB];
1699 srcBlkInfo =
new BlockInformation();
1700 srcBlkInfo->SccNum = SccNum;
1708 BlockInformation *&SrcBlkInfo = BlockInfoMap[MBB];
1711 SrcBlkInfo =
new BlockInformation();
1713 SrcBlkInfo->IsRetired =
true;
1715 &&
"can't retire block yet");
1719 "AMDGPU CFG Structurizer",
false,
false)
1727 return new AMDGPUCFGStructurizer();
unsigned succ_size() const
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
AMDGPU specific subclass of TargetSubtarget.
typename SuperClass::const_iterator const_iterator
STATISTIC(NumFunctions,"Total number of functions")
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Interface definition for R600InstrInfo.
MachineBasicBlock * getMBB() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
A Module instance is used to store all the information related to an LLVM module. ...
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
LoopT * getParentLoop() const
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
SmallVector< MachineBasicBlock *, 4 > MBBVector
FunctionPass * createAMDGPUCFGStructurizerPass()
Interface definition for R600RegisterInfo.
MachineInstr * CreateMachineInstr(const MCInstrDesc &MCID, const DebugLoc &DL, bool NoImp=false)
CreateMachineInstr - Allocate a new MachineInstr.
BlockT * getHeader() const
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void initializeAMDGPUCFGStructurizerPass(PassRegistry &)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
LLVM_NODISCARD bool empty() const
INITIALIZE_PASS_BEGIN(AMDGPUCFGStructurizer,"amdgpustructurizer","AMDGPU CFG Structurizer", false, false) INITIALIZE_PASS_END(AMDGPUCFGStructurizer
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
void emitError(unsigned LocCookie, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
const HexagonRegisterInfo & getRegisterInfo() const
HexagonInstrInfo specifics.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
reverse_iterator rbegin()
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
This is an important class for using LLVM in a threaded context.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
#define SHOWNEWBLK(b, msg)
void setMBB(MachineBasicBlock *MBB)
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
bool isEmpty() const
isEmpty - Return true if there are no jump tables.
std::vector< MachineBasicBlock * >::iterator pred_iterator
succ_iterator succ_begin()
pred_iterator pred_begin()
void dump() const
dump - Print the current MachineFunction to cerr, useful for debugger use.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
CloneMachineInstr - Create a new MachineInstr which is a copy of the 'Orig' instruction, identical in all ways except the instruction has no parent, prev, or next.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
typename SuperClass::iterator iterator
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
Representation of each machine instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
iterator_range< typename GraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void push_back(MachineInstr *MI)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
LoopInfoBase< MachineBasicBlock, MachineLoop >::iterator iterator
The iterator interface to the top-level loops in the current function.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
iterator_range< df_iterator< T > > depth_first(const T &G)
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void push_back(MachineBasicBlock *MBB)
StringRef - Represent a constant reference to a string, i.e.
static uint32_t blk(uint32_t *Buf, int I)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
bool isValid() const
Check for null.
std::vector< LoopT * >::const_iterator iterator
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
unsigned pred_size() const
std::vector< MachineBasicBlock * >::iterator succ_iterator
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.