Bug in TailDuplicator?

Hi,

I think there might be a bug in the tail duplicator (called from MachineBlockPlacement in my case), when duplicating a block that contains an implicit fall-through.
Suppose you have the following blocks

BB#1:
      Predecessors according to CFG: BB#2
  ...
  conditional_branch <BB#3>
      < implicit fall-through to BB#2 >
      Successors according to CFG: BB#2 BB#3

BB#2:
      Predecessors according to CFG: BB#1 BB#0
  ...
  unconditional_branch <BB#1>
      Successors according to CFG: BB#1

BB#3:
      Predecessors according to CFG: BB#1
  ...

It duplicates BB#1 into BB#2 but misses out the branch that goes back up:

BB#2:
      Predecessors according to CFG: BB#1 BB#0
  <BB#2 instructions>
  <BB#1 instructions>
  conditional_branch <BB#3>
      Successors according to CFG: BB#3

So this is not a loop any more.

Instead, a branch needs to be inserted to replace the implicit fall-through in BB#1.

I have attached a patch for this.

(Incidentally, I see a duplication of the statement "TII->removeBranch(*PrevBB);" if run after register allocation. I've removed this call in the patch as well.)

Unfortunately, I am working on a proprietary target, so cannot provide a test case. I have, however, attached a "pseudo mir" which should be easily adaptable for a public target.

Maybe this is not a bug, but my function at this stage is not canonical? Also, I am not on tip (though I checked the TailDuplicator tip), so it is possible this is fixed elsewhere.

Thanks!

tailDuplicatorPatch.diff (1.67 KB)

tail_duplicator_bug.pseudo_mir (1.29 KB)