// Find the next real instruction from the current position in current basic
// block.
static Iter getNextMachineInstrInBB(Iter Position) {
  Iter I = Position, E = Position->getParent()->end();
  I = std::find_if_not(I, E,
                       [](const Iter &Insn) { return Insn->isTransient(); });

  return I;
}
/// This function inserts clones of Filler into predecessor blocks.
static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
  MachineFunction *MF = Filler->getParent()->getParent();

  for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
    if (I->second) {
      MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
      ++UsefulSlots;
    } else {
      I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
    }
  }
}
// Find the next real instruction from the current position, looking through
// basic block boundaries.
static Iter getNextMachineInstr(Iter Position) {
  if (std::next(Position) == Position->getParent()->end()) {
    const MachineBasicBlock * MBB = (&*Position)->getParent();
    for (auto *Succ : MBB->successors()) {
      if (MBB->isLayoutSuccessor(Succ)) {
        Iter I = Succ->begin();
        Iter Next = getNextMachineInstrInBB(I);
        if (Next == Succ->end()) {
          return getNextMachineInstr(I);
        } else {
          return I;
        }
      }
    }
    llvm_unreachable("Should have identified the end of the function earlier!");
  }

  return getNextMachineInstrInBB(Position);
}