Has anyone written this?

Has anyone written a pass at the MachineFunction level which combines
machine basic blocks which is guaranteed to be the single predecessor
to another block? Or is there a reason not to combine them?

- Thanks
Jeff Kunkel

I'm not sure exactly what transformation you're referring to, but BranchFolder::OptimizeBranches does a lot of things like that.

It would go something like like the code below. The goal would be to
turn the basic blocks which the graph looks like "...->x->y->..."
where the instructions of x and y could live in the same basic block
without a jump or fall through in between.

    bool runOnMachineFunction(MachineFunction &mf) {
      BitVector seen( mf.size() );
      for( unsigned i = 0, e = mf.size(); i != e; ++i ) {
        if( seen[i] )
        seen[i] = true;
        MachineBasicBlock * start, *block;
        start = block = mf.getBlockNumbered(i);
        std::vector< MachineBasicBlock* > blocks;
        while( block->succ_size() == 1 &&
(*block->succ_begin())->pred_size() == 1 ) {
          block = *block->succ_begin();
          seen[block->getNumber()] = true;
          blocks.push_back( block );
        // TODO:
        // For each basic block bb in blocks in order of insersion:
        // 1. Remove basic blocks in the block vector from the machine function.
        // 2. Remove the jump from the start block if it exists.
        // 3. Add the instruction from bb into the start block.

Jeff Kunkel

I thought that the BranchFolder pass already handled that. Did you check?

I forgot to CC the forum.

I found what was happening. The BranchFolder documentation says:
// Note that this pass must be run after register allocation, it cannot handle
// SSA form.