<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63159>63159</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
The check on a pointer in linux kernel code is removed with undef optimizations
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
linkeLi0421
</td>
</tr>
</table>
<pre>
clang version : LLVM 16.0.5 Release
linux kernel version : 5.17.4
Hello
When I am using clang to compile linux kernel after modifying some options, I got wired result. I think it is a bug.
In linux-5.17.4/arch/x86/kernel/unwind_orc.c
```
static struct orc_entry *orc_find(unsigned long ip)
{
...
/* Module lookup: */
orc = orc_module_find(ip);
if (orc)
return orc;
return orc_ftrace_find(ip);
}
```
I got binary that removes `if (orc) `check. So I use `--print-after-all` to observe the IR code, the initial IR code below use a store and load to copy the return value of @orc_module_find() to static variable **orc**. Then let **orc** to compare with NULL, and branch.
```
%163 = call %struct.orc_entry* @orc_module_find(i64 noundef %162) #13, !dbg !5262
store %struct.orc_entry* %163, %struct.orc_entry** @orc_find.orc, align 8, !dbg !5263, !tbaa !4238
%164 = load %struct.orc_entry*, %struct.orc_entry** @orc_find.orc, align 8, !dbg !5264, !tbaa !4238
%165 = icmp ne %struct.orc_entry* %164, null, !dbg !5264
br i1 %165, label %166, label %168, !dbg !5266
```
However, after InstCombinePass, this piece code is optimized to
```
%160 = getelementptr inbounds %struct.orc_entry, %struct.orc_entry* %117, i64 %159, !dbg !5016
store %struct.orc_entry* %160, %struct.orc_entry** @orc_find.orc, align 8, !dbg !5013, !tbaa !4267
br i1 false, label %161, label %162, !dbg !5014
```
I try to find out why by looking up llvm source code, then I find this function. As the comment suggests, it seems that because instruction `store %struct.orc_entry* %160, %struct.orc_entry** @orc_find.orc`, llvm think %160 can't be null, and set the condition to false, then transfer it to unconditional branch eventually.
```
In llvm/lib/Analysis/ValueTracking.cpp
static bool isKnownNonNullFromDominatingCondition(const Value *V,
const Instruction *CtxI,
const DominatorTree *DT) {
...
// If the value is used as a load/store, then the pointer must be non null.
if (V == getLoadStorePointerOperand(U)) {
const Instruction *I = cast<Instruction>(U);
if (!NullPointerIsDefined(I->getFunction(),
V->getType()->getPointerAddressSpace()) &&
DT->dominates(I, CtxI))
return true;
}
...
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV09v4zoO_zTKhahhy7GTHHLItC94wfbNPsx0useBLDOOtrJkSHI62U-_oOS06d_dwxRBE1kU-eOPFEkL71VnENes-sKqm5kYw8G6tVbmAW9VPufFrLHtaS21MB0c0XllDbByA7e3939BUWd5VsE31Cg8svyG5RutzPgLHtAZ1C9OVFmxyOZJ6E_U2qaf_zqggR2IHkavTAfJVLAgbT8ojfBCodgHdNDbVu1PJO1tj2CHoKzxjF_DDjob4FE5bMGhH3XIYAfhoMwDqADKg4Bm7LJkO_3fmWTjakLIt8LJA-PbX8ua8W2yzPh2NI_KtD-tk5m8PM_qfPrEpQ8iKAk-uFEGsE7-RBPcCRjf0GKvTMv4cjSR-Ra0NR2ogfHVpG3xJf0AyLInnCvGt4xv4C_bjsSJtQ_jQKwyvqGts5h1Elh5E832UfZsMJkovzyJqj0wvrROPpvOVyxfOQyjM6ThQnraft77uQ9OyI-0L27epWbiO8aoUUa4E4SDCOCwt0f0wOr8EhWt5QHlQwbw3cIORo_07OpqcMqEq5gMV0JrVueUMbbx6I4I4YCw-wbStkg5QUtlVFBC0-P4HBrU9jEqFOCDdQjCUDBEm3JvOMVzk8NHoUcEuwc2z99SS1CDhSnwR-GUaDSm0GyiK_QjgztKdY3h9c4524VDeFThAF9_3N4ScoLUOGHkIYNPMo7xqqjLGHcptAbGq5R92VP2kZV3sat6DsaOpkUivipqHonnZVESAsaLtunoq-I1Pyc40fWRkYglHX1P4AIIIcgiCdcgtOoMLN-YPIMIjRD0Pefl8sLpeXQ6Ru0jc78JyPxzIFUEomQ_gPmcm6jIjF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