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

    <tr>
        <th>Summary</th>
        <td>
            [InferAddrSpace] The operand with non-address-space got Undefined when inferring its user
        </td>
    </tr>

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

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

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

<pre>
    Consider the following IR targeted to AMDGPU:

```
%agg.tmp1 = alloca %struct.float4, align 16, addrspace(5)
%agg.tmp12.sroa.0.0..sroa_idx.i = getelementptr inbounds %struct.float4, %struct.float4 addrspace(5)* %agg.tmp1, i64 0, i32 0
```

In InferAddrSpace pass, the `%agg.tmp12.sroa.0.0..sroa_idx.i` is considered to be in FLAT address space (0) so it's specific address space 
need to be inferred. From `%agg.tmp1`, the address space is inferred to be PRIVATE address space (5). 

When rewriting `%agg.tmp12.sroa.0.0..sroa_idx.i` to the new address space, the operand `%agg.tmp1` is not taken care of, so `%agg.tmp1` is set to be `Undef`, which is wrong.

To resolve this issue, in the function of `static Value *operandWithNewAddressSpaceOrCreatePoison`, I think below code snippet need to be inserted: 

```
static Value *operandWithNewAddressSpaceOrCreatePoison(
    ...

    if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand)) 
        return NewOperand;

    // __Insert this code snippet below__
    // If the address space of the operand already meets the requirement, just return it 
    if (Operand->getType()->getPointerAddressSpace() == NewAddrSpace) 
        return Operand;
 ...
```

Any ideas? Thanks in advance.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVdtu2zgQ_Rr6ZRCBpixfHvyg1vXCwG4bdN32MaDEkcSGJrXkqN78_YKU0thO9oINApmiOGfOnLlQhqBbi7hlxTtW7GZyoM757fGdU0-lIS9nlVNP2_fOBq3QA3UIjTPGnbVt4fAZSPoWCRWQg_K33S_3X1he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