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

    <tr>
        <th>Summary</th>
        <td>
            [DSE] crash on applying * to std::nullopt
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    [reduced5.zip](https://github.com/llvm/llvm-project/files/15433764/reduced5.zip)

The command
```
opt -passes=dse reduced5.ll
```
fails occasionally. Debug version reliably complains
```
include/c++/v1/optional:805: assertion this->has_value() failed: optional operator* called on a disengaged value
```
with stack trace
```
    frame #3: 0x00005555663c6a43 opt`std::__u::optional<llvm::MemoryLocation>::operator*(this= Has Value=false ) && at optional:805:5
    frame #4: 0x00005555663c4d58 opt`(anonymous namespace)::DSEState::eliminateRedundantStoresOfExistingValues(this=0x00007fffffff9130)::'lambda'()::operator()() const at DeadStoreElimination.cpp:2119:39
    frame #5: 0x00005555663bf842 opt`(anonymous namespace)::DSEState::eliminateRedundantStoresOfExistingValues(this=0x00007fffffff9960) at DeadStoreElimination.cpp:2132:12
    frame #6: 0x00005555663b8956 opt`(anonymous namespace)::eliminateDeadStores(F=0x000033187fe3e008, AA=0x000033187feaafc8, MSSA=0x000033187fe603f0, DT=0x000033187fef6d78, PDT=0x000033187fed18d0, TLI=0x000033187fe0cd18, LI=0x000033187fe11cc8) at DeadStoreElimination.cpp:2332:23
    frame #7: 0x00005555663b6b3e opt`llvm::DSEPass::run(this=0x000033187fe08ad8, F=0x000033187fe3e008, AM=0x00007fffffffc1d8) at DeadStoreElimination.cpp:2355:18
```

The local fix is
```
==== llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp#184
# action=edit type=text
--- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp 2024-04-15 20:27:56.000000000 -0700
+++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp 2024-05-24 04:36:17.000000000 -0700
@@ -2109,10 +2109,12 @@
         if (auto *MemSetI = dyn_cast<MemSetInst>(UpperInst)) {
           if (auto *SI = dyn_cast<StoreInst>(DefInst)) {
             // MemSetInst must have a write location.
-            MemoryLocation UpperLoc = *getLocForWrite(UpperInst);
+            auto UpperLoc = getLocForWrite(UpperInst);
+            if (!UpperLoc)
+ return false;
             int64_t InstWriteOffset = 0;
 int64_t DepWriteOffset = 0;
-            auto OR = isOverwrite(UpperInst, DefInst, UpperLoc, *MaybeDefLoc,
+            auto OR = isOverwrite(UpperInst, DefInst, *UpperLoc, *MaybeDefLoc,
 InstWriteOffset, DepWriteOffset);
             Value *StoredByte = isBytewiseValue(SI->getValueOperand(), DL);
 return StoredByte && StoredByte == MemSetI->getOperand(1) &&
```
which basically reverts a piece of https://github.com/llvm/llvm-project/commit/81d69e1bda9e4b6a83f29ba1f614e43ab4700972

Looks like the actual problem happened earlier?

Please take a look, this is blocking us significantly.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV11v47oR_TX0y8AGRUqy9OAHJ4rRBbLI4np7-xiMpJHNhhYFkUrW_fUFKX8ktrebbQtcQ0hEDnnmzCHFGaK1atMSLVhyx5JigoPbmn5BSNpZ1U5KU--9rad6qKhOZv9SHUsKJrKtc51lcsnEionVRrntUM4qs2NipfXr8d-0680_qXJMrBqlyTKxipJYynkaM7H6gCpyxgvGl-Pf71uCyux22NaH7pQfntA0nYNph9aSZbKoLcEJTOubMxpU2oKpKrTKtKj1fgYFlcMGXqn3XdCTVljqvXfcaVStvQmk2koPNTGxqpi4C8_qNWJiZToXkJlcZjxhcgmeX-87wW2VnTL5sEX7_Ip6ICYyJnLwrKj2Y4-zwXTUozM9E0uoUGuqwbSAUCtL7QY3VMOIcIvcm3JbsA6rF3A9VrcHAQA0Pe4ImJDSO-c_OOc8SZIkTWWVYiw9H5Zy6zw5JpfPz8P4co7yPix06PxK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