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

    <tr>
        <th>Summary</th>
        <td>
            expandAddress() crashes on freeze node.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:SystemZ
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            uweigand,
            JonPsson
      </td>
    </tr>

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

<pre>
    Csmith discovered a test case which had a comparison between pointer and int, which caused some optimizer to insert a freeze instruction. After DAGCombining this resulted in a DAG where expandAddress() got an ISD::FREEZE instead of a PCREL_WRAPPER node.

It seems it would be ideal to handle this somehow other than triggering the assert when casting to GlobalAddressSDNode.  Seems like there could be many places where this could show up, and not sure if it is something to try to handle everywhere or if it's ok to ignore - after all the input is kind of broken... I guess I am not quite sure what the benefit would be - to allow undefined values and still complete the compilation?

One fix for this test particular case might be:

```
--- a/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp
@@ -491,6 +491,8 @@ bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
   if (Opcode == SystemZISD::PCREL_OFFSET) {
     SDValue Full = N.getOperand(0);
     SDValue Base = N.getOperand(1);
+    if (Base->getOpcode() == ISD::FREEZE)
+      Base = Base->getOperand(0);
 SDValue Anchor = Base.getOperand(0);
     uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
 cast<GlobalAddressSDNode>(Anchor)->getOffset());
```

Reduced C program:
./bin/clang crash0_aftercreduce.i -o -  -fno-inline -fno-PIC -O3

```
crash0_aftercreduce.c:14:7: warning: comparison between pointer and integer ('char *' and 'char') [-Wpointer-integer-compare]

Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed. 
...
 #8 0x0000000002b04e30 (anonymous namespace)::SystemZDAGToDAGISel::expandAddress
...
```

llc input:
./bin/llc -mtriple=s390x-linux-gnu ./crash0_aftercreduce.ll
[testcase.tar.gz](https://github.com/llvm/llvm-project/files/10437766/testcase.tar.gz)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVl1vozwW_jXOzRGI2AkkF7mg-ai62plWbTUjzc3IwAG8NTZrm6aZX7-yIW06inbeV28VUePDec5zPm1urWgU4oYsbwilwxFFw1VFKCV0Syj9l1YP1mrlN5a7GR9cq83mvDsrdHXabG0nXAuVsKV-RYMVcHBoHZTcIhxbUbbQcr9b6q7nRlitoEB3RFTQa6EcGuCqAqEcodtJo-SDxQqs7hB070QnfqEBp0Eoi8YBh9og_kL_7sxQOqFVDHntwXb57VZ3hVBCNeBaYcGgHaRDbwO4l8OxRYOAbz1XVV5VBq0ldEXoGhrtgCu4e9oRlhOWHx73-x_7YAd5BboGDg_bx_2_f35_zB8e9o-gdIUxSXYkycfnnQOL2FkQDo56kBUUCKJCLr0HLVeVxJGY96_VR9Cu9e61XIEzomnQjNwRuA3-HltUPqIu7Gu4lbrgcmL-tPvqKQA8BatSvHh472B5tt5xdYJe8hLt5HuwP8qtpzD0Pvo-EUo7sINBELX3YKLp2sm0M6cLN_AVzWlE1GbUIDSzoF9CthqlDUIEPGSGSxmcEqofAvCLUCGkhdEvqOI4hjtoBrQW7oB3gcl_B-Fw5HNsuQv6BSqsL4MbeWNcSu-HqrAWCit45XJAG1yyTkgZClCiC8EJL0JyXziEHS7Td68QavEGtTZjlEI599w4UQ6Sm7GyO9G0Dgr0VXKhTNJk-oXXKIqAE3qQ8rXz_0RB6OGZmwYdoYenk3XY_fhY3T2h3OW3z3qX38Zl30-Y9Gb8QfGPoRYJWSQQLdZzQrcpEHozLlcwiQqtJUwYZ32PNXbD7x0zfThtCNV80RUCoWn-xY-QYBPA1wWhq_u-DFK2I2wH7zynRhub6v5weNo_-0Yk2c27PsDT7ptPJxwGKT0CfI0bdPc9mjCwVgmha8Kuadz4b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