- // short branch:
- // bCC .L_TARGET_MBB
- //
- // long branch:
- // bInverseCC $PC+8
- // b .L_TARGET_MBB
- for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
- ++MFI) {
- MachineBasicBlock *MBB = MFI;
-
- for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
- MBBI != EE; ++MBBI) {
- // We may end up deleting the MachineInstr that MBBI points to, so
- // remember its opcode now so we can refer to it after calling erase()
- unsigned ByteSize = getNumBytesForInstruction(MBBI);
- if (MBBI->getOpcode() != PPC::BCC) {
- ByteCount += ByteSize;
- continue;
- }
-
- // condbranch operands:
- // 0. CR register
- // 1. PPC branch opcode
- // 2. Target MBB
- MachineBasicBlock *DestMBB = MBBI->getOperand(2).getMachineBasicBlock();
- PPC::Predicate Pred = (PPC::Predicate)MBBI->getOperand(0).getImm();
- unsigned CRReg = MBBI->getOperand(1).getReg();
- int Displacement = OffsetMap[DestMBB->getNumber()] - ByteCount;
+ bool MadeChange = true;
+ bool EverMadeChange = false;
+ while (MadeChange) {
+ // Iteratively expand branches until we reach a fixed point.
+ MadeChange = false;
+
+ for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
+ ++MFI) {
+ MachineBasicBlock &MBB = *MFI;
+ unsigned MBBStartOffset = 0;
+ for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
+ I != E; ++I) {
+ if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImm()) {
+ MBBStartOffset += TII->GetInstSizeInBytes(I);
+ continue;
+ }
+
+ // Determine the offset from the current branch to the destination
+ // block.
+ MachineBasicBlock *Dest = I->getOperand(2).getMBB();
+
+ int BranchSize;
+ if (Dest->getNumber() <= MBB.getNumber()) {
+ // If this is a backwards branch, the delta is the offset from the
+ // start of this block to this branch, plus the sizes of all blocks
+ // from this block to the dest.
+ BranchSize = MBBStartOffset;
+
+ for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
+ BranchSize += BlockSizes[i];
+ } else {
+ // Otherwise, add the size of the blocks between this block and the
+ // dest to the number of bytes left in this block.
+ BranchSize = -MBBStartOffset;