Specify dominator for BasicBlock to avoid "Instruction does not dominate all uses!"

Hi,

I am working on a pass aimed to unify multiple exits of a loop into a unique
basic block. The approach is straight forward:
I create a unique BasicBlock BB_unique that has as predecessors all the exit
blocks of the loop, it contains a phi instruction and a switch to redirect
the flow correctly. Additionally, for each initial exit block I create an
associated block that will jump to the original successors of the exit
block.
In short:

    B1 B2 B3 BB4
     > > > >
      \ | / |
       ExitBB1 ExitBB2
         > > > >
       / \ / \
EBB1.1 EBB1.2 EBB2.1 EBB2.2

becomes:

    B1 B2 B3 BB4
     > > > >
      \ | / |
     ExitBB1 ExitBB2

One approach is to use insert PHI nodes in BB_unique for values defined in ExitBB1 and use these phi nodes in ExitBB1_redirect.

Hi,

You may run the "RegToMem" pass before unify multiple exits of a loop
into a unique basic block, which will demotes all registers to memory
references, so you do not need to worried if your transform break the
SSA form.

and here is the introduction of RegToMem:
//===----------------------------------------------------------------------===//
//
// This file demotes all registers to memory references. It is intented to be
// the inverse of PromoteMemoryToRegister. By converting to loads, the only
// values live accross basic blocks are allocas and loads before phi nodes.
// It is intended that this should make CFG hacking much easier.
// To make later hacking easier, the entry block is split into two, such that
// all introduced allocas and nothing else are in the entry block.
//
//===----------------------------------------------------------------------===//

best regards
ether

Hi, I tried adding the PHI nodes in BB_unique, and it works for the simple
case described here, but in case the nodes were declared in some
predecessors of ExitBB1 and used in ExitBB1_redirect and its successors, it
won't work, unless I create entries for all of them in BB_unique.

B1 (declares PHI_1) B3
> >
B2 |

                                                     >

ExitBB1 ExitBB2
>
Succ1 (uses PHI_1)

becomes:

B1 (declares PHI_1) B3
> >
B2 |

                                                     >

ExitBB1 ExitBB2

hi,

you may also add something like this in the "getAnalysisUsage" of your pass:

  AU.addRequiredID(DemoteRegisterToMemoryID);

After this, the reg2mem pass will always run before your pass.

best regards
ether