Issue 244906
Summary [IfConversion] Debug-only block changes the branch emitted after if-conversion (canFallThroughTo uses empty())
Labels new issue
Assignees
Reporter yqtian-se
    On ARM, the code generated for the function below differs between `-g` and `-g0`
(debug sections stripped):

```c
void *tab;
int f(int a, int b, int n) {
  tab = &&L2;
 if (b == n) goto L3;
  a += 1;
  if ((unsigned)b <= (unsigned)n) goto L3;
L2: { int y = a; __builtin_annotation(y, ""); }
L3:
  return a * 7 + b;
}
```

```
$ clang --target=armv7a-none-eabi -O2 -g0 -c t.c   # vs -g
-   b    .LBB0_2
+   bhi  .LBB0_2
```

The same difference shows with `llc` alone on the IR below, with and without
`opt -passes=debugify`:

```llvm
@tab = global ptr null

define i32 @f(i32 %a, i32 %b, i32 %n) {
entry:
  store ptr blockaddress(@f, %l2), ptr @tab
 %c1 = icmp eq i32 %b, %n
  br i1 %c1, label %l3, label %if.end
if.end:
 %a1 = add i32 %a, 1
  %c2 = icmp ugt i32 %b, %n
  br i1 %c2, label %l2, label %l3
l2:
  %x = add i32 %a1, 0
  br label %l3
l3:
  %p = phi i32 [ %a, %entry ], [ %a1, %if.end ], [ %a1, %l2 ]
  %m = mul i32 %p, 7
  %r = add i32 %m, %b
  ret i32 %r
}
```

```
llc -mtriple=armv7-unknown-linux-gnueabi -O2 t.ll
opt -passes=debugify t.ll | llc -mtriple=armv7-unknown-linux-gnueabi -O2
```

The first difference is in the If Converter.  After the triangle with early exit
is converted, `IfConvertTriangle` calls `canFallThroughTo(BB, NextMBB)`, which
walks the blocks between them and requires each to be `empty()`
(https://github.com/llvm/llvm-project/blob/3f3bd3f9e0bc3fdb24219fbfebdc2ac4edfc3e5c/llvm/lib/CodeGen/IfConversion.cpp#L1462).
The address-taken block `L2` holds only a `DBG_VALUE` with debug info, so it is
not empty, the function returns false, and an unconditional branch is inserted;
without debug info no branch is inserted.

The obvious fix is to ignore debug instructions there
(`I->getFirstNonDebugInstr() != I->end()` instead of `!I->empty()`).  But the
debug-free path is itself questionable: after if-conversion the entry block
keeps `L3` as a successor while it only falls through to `L2`, and
`-verify-machineinstrs` reports "MBB has unexpected successors which are not
branch targets, fallthrough, EHPads, or inlineasm_br targets" for the `-g0`
compile only.  The `-g0` code is also the worse one (an unconditional `b` to
`L2`, which branches back to `L3`). So the one-line change would make `-g`
match `-g0`, including the invalid CFG.  Should `canFallThroughTo` instead stop
skipping intervening blocks (or should the if-converter fix up the successor
list when it does)?

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to