<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/118467>118467</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Invalid LLVM IR code generated on x86-64 with a very simple sample (crash the generated executable code)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dje64240
</td>
</tr>
</table>
<pre>
Below is a very simple program that's loading 16 bits table value into a 32 bits variable from a 8 bits table.
On x86-64, this program works fine with LLVM 16.0.0 and below, but since LLVM 17.0.0, the LLVM IR and the generated assembly code are using a wrong calculated offset on the table (doesn't occurs on LLVM armv8 whatever the version).
As you can see below, the offset in the table which is supposed to be **2149675576** (**0x80217238**) has its 64 bits upper bits inverted for **-2145291720** (**0xFFFFFFFF80217238**).
---------------------------------------------------------------------------------------------------
#include <stdint.h>
// Load 16 bits into 32 bits value from data offset 0x80217238
uint32_t Test(const uint8_t* data)
{
uint32_t a, b, c;
b = 0xFFFF8022 << 16;
b += 0xFFFFE808;
a = data[b + 0xFFFF8A31];
c = data[b + 0xFFFF8A30];
c &= ~0x0000FF00;
c |= ((a << 8) | (a >> 24)) & 0x0000FF00;
return c;
}
---------------------------------------------------------------------------------------------------
LLVM IR with LLVM 16.0.0 (from Compiler Explorer)
define dso_local noundef i32 @Test(unsigned char const*)(ptr nocapture noundef readonly %data) local_unnamed_addr {
entry:
%arrayidx4 = getelementptr inbounds i8, ptr %data, i64 2149675576
%0 = load i16, ptr %arrayidx4, align 1
%1 = zext i16 %0 to i32
ret i32 %1
}
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
---------------------------------------------------------------------------------------------------
LLVM IR with LLVM 19.1.0 and trunk (from Compiler Explorer)
define dso_local noundef range(i32 0, 65536) i32 @Test(unsigned char const*)(ptr nocapture noundef readonly %data) local_unnamed_addr {
entry:
%0 = getelementptr i8, ptr %data, i64 -2145291720
%1 = load i16, ptr %0, align 1
%2 = zext i16 %1 to i32
ret i32 %2
}
---------------------------------------------------------------------------------------------------
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVs2O4zgOfhrlQlQgS45_DjkkqQrQQC8WWCzmWpAtJla3IwWSnErmMM8-oOz8dFX1XAYzbQSRLZEfRfITRRWC2VvEJVus2eJ5pobYOb_U37DIRc5njdOX5Rp79wYmgIIT-gsEczj2CEfv9l4dIHYqMlEG6J3Sxu4hK6AxMUBUTY9wUv2AYGx0oECKcemkvEmrO-8OoKB60JgzvvqvhXNVPBU5ExuInQk3a2_Ofw-wMxbhzcQOvn797T-QFXM-56CshoY2S1rNECEY2-IkUpLICDdNfflf0qDvPVr0KqIGFQIemv4CrdMIyiMMgZxS8Oad3UOr-nbok6jb7QJGcDZBjN4yUWmHwTJRRnBtO_hAAsme8odTBW-dinhCn5RO6INxlomavF4FuLgBWmUhIN5dIcnJmHk09taZtqPEhOF4dAE1RAcNbWLFxEpkeV2Ui0VZjN-0t_GNnysuslLI6ZuJGjoVgFJQ5GMqhuMR_fhq7Ak9ObxzfsJ-Elm-EHVWCv4RfDs9H4yQj4yvnv75ZzTEhDS27QeNwOQmRG1snHdMvlyXt0xs4atT-sbZxNM7S4m6iaJaRXXNwUP4Es5gbJTiNcL_MUQmqtbZEIFmq9dIsSFlJmqSLtcwPYyvaLgpq0Ra-muZXE8SjNcNMPkMY1QrLgS5wuQGsoLJ9QTSABPru9hLxas7Bg0qgaR9LNZJ-oq4khlbPN-h2r-Q5O8lRUHCf_Az55xvt5z_sFpuaDURo1LXbVdENlZuYJp8YfIFRE7hoQVRwHs0xmuPcfA2BSYF8flfJtK1XHyoOExUiR4bdziaHj28nI-98-indPOVxlSrdHCvvWtVD9YNVuMOjBTAcj5xZrCpEGtoO-UhMWg8M0xUx-jBulYd4-Dxpu9RaWf7CzCxmAgGycLrYK06oH5VWntgJYUMbfQXJlcpOUwslPfqYvQ5T9neY8QeD2gjmTK2IRMBTEVspKmb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