<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64197>64197</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
             [BFI][CGP] Assertion failed: (Match && "BFI mismatch"), function verifyMatch, file BlockFrequencyInfoImpl.h, line 1800.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:crash,
            llvm
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            hjyamauchi
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          PavelKopyl
      </td>
    </tr>
</table>

<pre>
    Hello,

I faced with a failed assertion in BlockFrequencyInfoImpl on LLVM TOT.

Repro case: bfi-bug.ll
```
; Function Attrs: noreturn
define private fastcc void @__test() unnamed_addr #0 {
entry:
  br i1 undef, label %join, label %division
 
division:
  br label %join
 
join:
  tail call fastcc void @encode(i32 1024)
  unreachable
}
 
define private fastcc void @encode(i32 %0) unnamed_addr {
entry:
  unreachable
}
 
attributes #0 = { noreturn }
```

LLVM should be built in the debug mode.
```
opt --codegenprepare --mtriple=riscv32 --cgp-verify-bfi-updates bfi-bug3.ll -S
Freq mismatch: entry 8 vs 16
This
block-frequency-info: __test
 - entry: float = 1.0, int = 8

Other
block-frequency-info: __test
 - entry: float = 1.0, int = 16

Assertion failed: (Match && "BFI mismatch"), function verifyMatch, file BlockFrequencyInfoImpl.h, line 1800.
```
Please, note that the actual triplet value doesn't seem to matter, as the failed assertion is in target-independent part of the code. 

As I understand, the issues happens because the BFI updates are not synced with actual code transformations performed by CodeGenPrepare pass. Namely, the BBs frequency info, expressed as FrequencyData::Integer, is not updated. 

Does it make sense to compare Scaled values instead?
```
--- a/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
+++ b/llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
@@ -1777,7 +1777,7 @@ void BlockFrequencyInfoImpl<BT>::verifyMatch(
         BlockNode OtherNode = OtherValidNodes[BB];
         const auto &Freq = Freqs[Node.Index];
         const auto &OtherFreq = Other.Freqs[OtherNode.Index];
-        if (Freq.Integer != OtherFreq.Integer) {
+        if (Freq.Scaled != OtherFreq.Scaled) {
 Match = false;
           dbgs() << "Freq mismatch: " << bfi_detail::getBlockName(BB) << " "
                  << Freq.Integer << " vs " << OtherFreq.Integer << "\n";
```


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVl1zozoS_TXySxcUCOOPBz8EvJ517XykdlLzmhKoMZoREisJ7_jf35IAJ44z996Hqboux0FCfdR9-nRLzFpxUog7kheE0vb7hXVsqFtBKCX5fsEG12qze2RnlP_R_UUuKs0vu3-jlJrQkiR7kjyMv0doWI0c_i9cCwwaJiRyYNaicUIrEAoKqesfB4P_G1DVl6Nq9LHrJWgFHz9--wRPX57i14j_xd5oqJlFkj1A1YioGk6xlNOaVTJ9x2FWwGFQddjrwTljvZHSBt1g1LiGYyMUQm_EmTmEhllX13DWggNZJs_PDq0jdEPoFgalWIf8mXFugNAsAbIuRhRUzlxINjkJUBkQKQyKY0NoCZJVKIHQ_LsW6maCi7OwQk_OwOTTPHkDeAvyen2YeFnrmJBQMynfRoOq1hwJ3YiMQprQJaHb2WhQBlndskriRN56f-vUnxJ1A01ontwT9iuu_nJn5pwR1eDQTqxne492TSRcDd7mP_wGHdlWD5JDhVANQjqvPNcicKyGE3SaY_wugu4dRJEP7YSqN9gzgxBFnTOil0iyvRG2PmfULzr10RmNaC6Rl-XQc-Y9niSaxVJC9HVE9XKHTtiOubr1kgyUwAbOFtLVuOapFXZ8qnyFRM1cIpFQjfZGkzZHpiKYaYVGauYCSWmceLEJNQ43r1n54lo0v3mD2ffx9-Fa5mPde1NCN5980EDoitAVEEqLw_GFC0q9JGkJzVy2I6WfxrclNELiL3pGHBZIL9J0kyTvJ_RRou8dtASlHYJrmQtCYLUbmIQxrw7OTA4IXKNVhK4dWMQOnIaOOYfGmzMb7O5bmg3aYuaELhKKY4-Ko3LQM-NAN8H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