Index: lib/Target/X86/X86InstrInfo.h =================================================================== --- lib/Target/X86/X86InstrInfo.h +++ lib/Target/X86/X86InstrInfo.h @@ -69,6 +69,15 @@ COND_NE_OR_P, COND_NP_OR_E, + // Artificial condition codes. These are used to represent the "negation" of + // above two conditions. Here "negation" is not in terms of logic. The only + // scenario we need these two conditions is when we try to reverse above two + // conditions in order to remove redundant unconditional jumps. Note that both + // true and false bodies need to be avaiable in order to correctly synthesize + // instructions for them. These are never used in MachineInstrs. + COND_NEG_NE_OR_P, + COND_NEG_NP_OR_E, + COND_INVALID }; Index: lib/Target/X86/X86InstrInfo.cpp =================================================================== --- lib/Target/X86/X86InstrInfo.cpp +++ lib/Target/X86/X86InstrInfo.cpp @@ -3335,6 +3335,10 @@ case X86::COND_NP: return X86::COND_P; case X86::COND_O: return X86::COND_NO; case X86::COND_NO: return X86::COND_O; + case X86::COND_NE_OR_P: return X86::COND_NEG_NE_OR_P; + case X86::COND_NP_OR_E: return X86::COND_NEG_NP_OR_E; + case X86::COND_NEG_NE_OR_P: return X86::COND_NE_OR_P; + case X86::COND_NEG_NP_OR_E: return X86::COND_NP_OR_E; } } @@ -3503,44 +3507,6 @@ // Working from the bottom, handle the first conditional branch. if (Cond.empty()) { - MachineBasicBlock *TargetBB = I->getOperand(0).getMBB(); - if (AllowModify && UnCondBrIter != MBB.end() && - MBB.isLayoutSuccessor(TargetBB)) { - // If we can modify the code and it ends in something like: - // - // jCC L1 - // jmp L2 - // L1: - // ... - // L2: - // - // Then we can change this to: - // - // jnCC L2 - // L1: - // ... - // L2: - // - // Which is a bit more efficient. - // We conditionally jump to the fall-through block. - BranchCode = GetOppositeBranchCondition(BranchCode); - unsigned JNCC = GetCondBranchFromCond(BranchCode); - MachineBasicBlock::iterator OldInst = I; - - BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(JNCC)) - .addMBB(UnCondBrIter->getOperand(0).getMBB()); - BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JMP_1)) - .addMBB(TargetBB); - - OldInst->eraseFromParent(); - UnCondBrIter->eraseFromParent(); - - // Restart the analysis. - UnCondBrIter = MBB.end(); - I = MBB.end(); - continue; - } - FBB = TBB; TBB = I->getOperand(0).getMBB(); Cond.push_back(MachineOperand::CreateImm(BranchCode)); @@ -3720,6 +3686,36 @@ BuildMI(&MBB, DL, get(X86::JP_1)).addMBB(TBB); ++Count; break; + case X86::COND_NEG_NP_OR_E: + // If FBB is null, it is implied to be the next block of MBB. + if (FBB == nullptr) { + MachineFunction::iterator I = &MBB; + FBB = ++I; + assert(I != TBB->getParent()->end() && "MBB cannot be the last block in " + "function when the false body is " + "a fall-through."); + } + // Synthesize NEG_NP_OR_E with two branches. + BuildMI(&MBB, DL, get(X86::JNP_1)).addMBB(FBB); + ++Count; + BuildMI(&MBB, DL, get(X86::JNE_1)).addMBB(TBB); + ++Count; + return Count; + case X86::COND_NEG_NE_OR_P: + // If FBB is null, it is implied to be the next block of MBB. + if (FBB == nullptr) { + MachineFunction::iterator I = &MBB; + FBB = ++I; + assert(I != TBB->getParent()->end() && "MBB cannot be the last block in " + "function when the false body is " + "a fall-through."); + } + // Synthesize NEG_NE_OR_P with two branches. + BuildMI(&MBB, DL, get(X86::JNE_1)).addMBB(FBB); + ++Count; + BuildMI(&MBB, DL, get(X86::JNP_1)).addMBB(TBB); + ++Count; + return Count; default: { unsigned Opc = GetCondBranchFromCond(CC); BuildMI(&MBB, DL, get(Opc)).addMBB(TBB); @@ -6091,8 +6087,6 @@ ReverseBranchCondition(SmallVectorImpl &Cond) const { assert(Cond.size() == 1 && "Invalid X86 branch condition!"); X86::CondCode CC = static_cast(Cond[0].getImm()); - if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E) - return true; Cond[0].setImm(GetOppositeBranchCondition(CC)); return false; }