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

    <tr>
        <th>Summary</th>
        <td>
            [flang] missing null check when using where construct
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

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

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

<pre>
    ```
subroutine where_allocatable_assignments(a, b)
  integer :: a(:)
  integer, allocatable :: b(:)
  where(b > 0)
    b = a
  elsewhere
    b(:) = 0
  endwhere
end subroutine
```

Results in FIR like this:
```
func.func @_QPwhere_allocatable_assignments(%arg0: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"}, %arg1: !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> {fir.bindc_name = "b"}) {
    %0 = fir.load %arg1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>> // %arg1 might be null
 %c0 = arith.constant 0 : index
    %1:3 = fir.box_dims %0, %c0 : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
 %2 = fir.box_addr %0 : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
    %3 = fir.shape_shift %1#0, %1#1 : (index, index) -> !fir.shapeshift<1>
    %4 = fir.array_load %2(%3) : (!fir.heap<!fir.array<?xi32>>, !fir.shapeshift<1>) -> !fir.array<?xi32> // load %arg1 without checking if it is nulll
   [... no null check ...]
 %9 = fir.do_loop %arg2 = %c0_0 to %8 step %c1 unordered iter_args(%arg3 = %7) -> (!fir.array<?x!fir.logical<4>>) {
      %34 = fir.array_fetch %4, %arg2 : (!fir.array<?xi32>, index) -> i32 // read from %arg1 without checking if it is null
      [... this loop computes the mask array ...]
 }
    [... array_load %arg0 ...]
    %13 = fir.load %arg1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
    %14 = fir.box_addr %13 : (!fir.box<!fir.heap<!fir.array<?xi32>>>) -> !fir.heap<!fir.array<?xi32>>
    %15 = fir.convert %14 : (!fir.heap<!fir.array<?xi32>>) -> i64
    %c0_i64 = arith.constant 0 : i64
    %16 = arith.cmpi ne, %15, %c0_i64 : i64 // %arg1 is checked here
    [...]
```

The above FIR shows that `%arg1 (b)` is read from before we check if the pointer is null.

See also https://github.com/llvm/llvm-project/issues/56921
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVk9v47YT_TT0ZbACRVqyfPBhN14Dv9uv294NihpZbGjRIKkk--0LUv-dtEmRogUWXpl-M_Pm8Wkywjl1aREPJPtGsuNGdL4x9uBLLZpNaaqfB5LT4R89EvrVdaU1nVctwnODFs9CayOFF6XGc5_tiq13hBWCsAcoCdv3kQCq9XhBC4R_JfwrCMKK8PgKEOIWaUd8-QofGRBWlED4d6CLXwDC2RHEeIDaYQ-fEVO-CKUTtK0WSGwrmJvuz-406T9_oOu0d6BaOP3vB2j1iOAb5UKJt6LqrpVJ-ACypedf_v-enIRlwl5oUIKwtFY2Kc0L4Q_DF2Gt-Bm-8tOL4ozw70ETsvsWkaqt5LkVV4ytEsYEYYzsjkHqPnG6SGyxnhOvqjQobu_UfK9yOVbeB8x8HYRlNGJCmDaiGonBP82MnQg7Tdmv6tJ4KBHaTuuBDmGZ7MkIq3yTSNM6L1oPNJJRbYUvK-ZBPT6xL83LuVJXF3saJJZ06KP4BHv2MNRme_jS91IMB9MvM2Ruhq2oiaqyo9yfZjQR-XDUUrdZM9eIG55do2rfC8r4KF14Hl3wqt11_ZglJiH8Ib2vtp2qRWbn0WWsf714PwqWknykn0jyT6rf0XudYHTjyvDPyjem8yAblI-qvYCqQXlQLnpUTz2R7FuSJNCaeN7DIUkSkh3nu99PTVfmrI25DWXY8EJmkp4peBMeC3AeI0Cm0LXGVmixAuXRnoW9zGOIj8G7pRXf6HI40uaipNCEP2xn46xe_94O9zdUo5dN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