Index: lib/Target/AArch64/AArch64BranchRelaxation.cpp =================================================================== --- lib/Target/AArch64/AArch64BranchRelaxation.cpp +++ lib/Target/AArch64/AArch64BranchRelaxation.cpp @@ -90,7 +90,7 @@ void scanFunction(); MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI); void adjustBlockOffsets(MachineBasicBlock &MBB); - bool isBlockInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); + bool isBlockInRange(MachineInstr &MI, MachineBasicBlock *BB, unsigned Disp); bool fixupConditionalBranch(MachineInstr *MI); void computeBlockSize(const MachineBasicBlock &MBB); unsigned getInstrOffset(MachineInstr *MI) const; @@ -262,18 +262,18 @@ /// isBlockInRange - Returns true if the distance between specific MI and /// specific BB can fit in MI's displacement field. -bool AArch64BranchRelaxation::isBlockInRange(MachineInstr *MI, +bool AArch64BranchRelaxation::isBlockInRange(MachineInstr &MI, MachineBasicBlock *DestBB, unsigned Bits) { unsigned MaxOffs = ((1 << (Bits - 1)) - 1) << 2; - unsigned BrOffset = getInstrOffset(MI); + unsigned BrOffset = getInstrOffset(&MI); unsigned DestOffset = BlockInfo[DestBB->getNumber()].Offset; DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber() - << " from BB#" << MI->getParent()->getNumber() - << " max delta=" << MaxOffs << " from " << getInstrOffset(MI) + << " from BB#" << MI.getParent()->getNumber() + << " max delta=" << MaxOffs << " from " << BrOffset << " to " << DestOffset << " offset " - << int(DestOffset - BrOffset) << "\t" << *MI); + << int(DestOffset - BrOffset) << '\t' << MI); // Branch before the Dest. if (BrOffset <= DestOffset) @@ -298,21 +298,21 @@ } } -static MachineBasicBlock *getDestBlock(MachineInstr *MI) { - switch (MI->getOpcode()) { +static MachineBasicBlock *getDestBlock(MachineInstr &MI) { + switch (MI.getOpcode()) { default: llvm_unreachable("unexpected opcode!"); case AArch64::TBZW: case AArch64::TBNZW: case AArch64::TBZX: case AArch64::TBNZX: - return MI->getOperand(2).getMBB(); + return MI.getOperand(2).getMBB(); case AArch64::CBZW: case AArch64::CBNZW: case AArch64::CBZX: case AArch64::CBNZX: case AArch64::Bcc: - return MI->getOperand(1).getMBB(); + return MI.getOperand(1).getMBB(); } } @@ -362,7 +362,7 @@ /// too far away to fit in its displacement field. It is converted to an inverse /// conditional branch + an unconditional branch to the destination. bool AArch64BranchRelaxation::fixupConditionalBranch(MachineInstr *MI) { - MachineBasicBlock *DestBB = getDestBlock(MI); + MachineBasicBlock *DestBB = getDestBlock(*MI); // Add an unconditional branch to the destination and invert the branch // condition to jump over it: @@ -391,7 +391,7 @@ // bne L2 // b L1 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB(); - if (isBlockInRange(MI, NewDest, + if (isBlockInRange(*MI, NewDest, getBranchDisplacementBits(MI->getOpcode()))) { DEBUG(dbgs() << " Invert condition and swap its destination with " << *BMI); @@ -465,11 +465,15 @@ // end() for termination. for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) { MachineBasicBlock &MBB = *I; - MachineInstr *MI = MBB.getFirstTerminator(); - if (isConditionalBranch(MI->getOpcode()) && + MachineBasicBlock::iterator J = MBB.getFirstTerminator(); + if (J == MBB.end()) + continue; + + MachineInstr &MI = *J; + if (isConditionalBranch(MI.getOpcode()) && !isBlockInRange(MI, getDestBlock(MI), - getBranchDisplacementBits(MI->getOpcode()))) { - fixupConditionalBranch(MI); + getBranchDisplacementBits(MI.getOpcode()))) { + fixupConditionalBranch(&MI); ++NumRelaxed; Changed = true; }