llvm.org GIT mirror llvm / 13049ae
Cleanup PPCEarlyReturn Some general cleanup and only scan the end of a BB for branches (once we're done with the terminators and debug values, then there should not be any other branches). These address post-commit review suggestions by Bill Schmidt. No functionality change intended. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179112 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 7 years ago
1 changed file(s) with 33 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
921921 const PPCInstrInfo *TII;
922922
923923 protected:
924 bool processBlock(MachineBasicBlock &LastMBB) {
924 bool processBlock(MachineBasicBlock &ReturnMBB) {
925925 bool Changed = false;
926926
927 MachineBasicBlock::iterator I = LastMBB.begin();
928 I = LastMBB.SkipPHIsAndLabels(I);
927 MachineBasicBlock::iterator I = ReturnMBB.begin();
928 I = ReturnMBB.SkipPHIsAndLabels(I);
929929
930930 // The block must be essentially empty except for the blr.
931 if (I == LastMBB.end() || I->getOpcode() != PPC::BLR ||
932 I != LastMBB.getLastNonDebugInstr())
931 if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
932 I != ReturnMBB.getLastNonDebugInstr())
933933 return Changed;
934934
935935 SmallVector PredToRemove;
936 for (MachineBasicBlock::pred_iterator PI = LastMBB.pred_begin(),
937 PIE = LastMBB.pred_end(); PI != PIE; ++PI) {
936 for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
937 PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
938938 bool OtherReference = false, BlockChanged = false;
939 for (MachineBasicBlock::iterator J = (*PI)->begin();
940 J != (*PI)->end();) {
939 for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
941940 if (J->getOpcode() == PPC::B) {
942 if (J->getOperand(0).getMBB() == &LastMBB) {
941 if (J->getOperand(0).getMBB() == &ReturnMBB) {
943942 // This is an unconditional branch to the return. Replace the
944943 // branch with a blr.
945944 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
946 MachineBasicBlock::iterator K = J++;
945 MachineBasicBlock::iterator K = J--;
947946 K->eraseFromParent();
948947 BlockChanged = true;
949948 ++NumBLR;
950949 continue;
951950 }
952951 } else if (J->getOpcode() == PPC::BCC) {
953 if (J->getOperand(2).getMBB() == &LastMBB) {
952 if (J->getOperand(2).getMBB() == &ReturnMBB) {
954953 // This is a conditional branch to the return. Replace the branch
955954 // with a bclr.
956955 BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR))
957956 .addImm(J->getOperand(0).getImm())
958957 .addReg(J->getOperand(1).getReg());
959 MachineBasicBlock::iterator K = J++;
958 MachineBasicBlock::iterator K = J--;
960959 K->eraseFromParent();
961960 BlockChanged = true;
962961 ++NumBCLR;
964963 }
965964 } else if (J->isBranch()) {
966965 if (J->isIndirectBranch()) {
967 if (LastMBB.hasAddressTaken())
966 if (ReturnMBB.hasAddressTaken())
968967 OtherReference = true;
969968 } else
970969 for (unsigned i = 0; i < J->getNumOperands(); ++i)
971970 if (J->getOperand(i).isMBB() &&
972 J->getOperand(i).getMBB() == &LastMBB)
971 J->getOperand(i).getMBB() == &ReturnMBB)
973972 OtherReference = true;
974 }
975
976 ++J;
973 } else if (!J->isTerminator() && !J->isDebugValue())
974 break;
975
976 if (J == (*PI)->begin())
977 break;
978
979 --J;
977980 }
978981
979 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&LastMBB))
982 if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
980983 OtherReference = true;
981984
982985 // Predecessors are stored in a vector and can't be removed here.
989992 }
990993
991994 for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
992 PredToRemove[i]->removeSuccessor(&LastMBB);
993
994 if (Changed && !LastMBB.hasAddressTaken()) {
995 PredToRemove[i]->removeSuccessor(&ReturnMBB);
996
997 if (Changed && !ReturnMBB.hasAddressTaken()) {
995998 // We now might be able to merge this blr-only block into its
996999 // by-layout predecessor.
997 if (LastMBB.pred_size() == 1 &&
998 (*LastMBB.pred_begin())->isLayoutSuccessor(&LastMBB)) {
1000 if (ReturnMBB.pred_size() == 1 &&
1001 (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
9991002 // Move the blr into the preceding block.
1000 MachineBasicBlock &PrevMBB = **LastMBB.pred_begin();
1001 PrevMBB.splice(PrevMBB.end(), &LastMBB, I);
1002 PrevMBB.removeSuccessor(&LastMBB);
1003 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1004 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1005 PrevMBB.removeSuccessor(&ReturnMBB);
10031006 }
10041007
1005 if (LastMBB.pred_empty())
1006 LastMBB.eraseFromParent();
1008 if (ReturnMBB.pred_empty())
1009 ReturnMBB.eraseFromParent();
10071010 }
10081011
10091012 return Changed;
10161019
10171020 bool Changed = false;
10181021
1019 // If the function does not have at least two block, then there is
1022 // If the function does not have at least two blocks, then there is
10201023 // nothing to do.
10211024 if (MF.size() < 2)
10221025 return Changed;