<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Clang generates i32 type index instead of i64 for getelementptr when using structs"
   href="https://bugs.llvm.org/show_bug.cgi?id=38070">38070</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang generates i32 type index instead of i64 for getelementptr when using structs
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>janekvoirschot@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When using an array, GEP with i64 indices are emitted; however, when using a
struct, GEP with i32 indices are emitted.

Example:

typedef struct {
    unsigned a;
    unsigned b;
} structure;

unsigned f(structure s) {
    return s.b;
}

Compiled with: -S -emit-llvm
Results in:

%struct.structure = type { i32, i32 }

define dso_local i32 @f(i64 %s.coerce) #0 {
entry:
  %s = alloca %struct.structure, align 4
  %0 = bitcast %struct.structure* %s to i64*
  store i64 %s.coerce, i64* %0, align 4
  %b = getelementptr inbounds %struct.structure, %struct.structure* %s, i32 0,
i32 1
  %1 = load i32, i32* %b, align 4
  ret i32 %1
}

Whereas using an array as follows (using the same compiler flags):

unsigned f(unsigned a[50]) {
  return a[12];
}

results in:

define dso_local i32 @f(i32* %a) #0 {
entry:
  %a.addr = alloca i32*, align 8
  store i32* %a, i32** %a.addr, align 8
  %0 = load i32*, i32** %a.addr, align 8
  %arrayidx = getelementptr inbounds i32, i32* %0, i64 12
  %1 = load i32, i32* %arrayidx, align 4
  ret i32 %1
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>