<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64366>64366</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LICM] Missed optimization: It's affected by the negative sign
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ZY546
</td>
</tr>
</table>
<pre>
Hi, I noticed a missed optimization opportunity about LICM in clang16 and this opportunity has been optimized in clang10.
https://godbolt.org/z/5583Ec79r
```c
extern int var_24;
void test(int var_2, int var_3, int var_8, int var_10, int var_14) {
for (int i_2 = -3247424; i_2 < 19; i_2 += var_3 + 1056714155)
{
if(var_3){
//In the context that `200/var_10` cannot be optimized, `-var_8` can be combined with `var_14+var_2` as loop invariants
var_24 += (-(200 / var_10)) + (-var_8); //If here is `var_8`, then clang16 can optimize it
var_24 += var_14 + var_2;
}
i_2+=i_2/3;
}
}
```
But clang16 -O2:
```
entry:
%tobool.not = icmp eq i32 %var_3, 0
%add6 = add i32 %var_3, 1056714155
%var_24.promoted = load i32, ptr @var_24, align 4
%0 = add i32 %var_2, %var_14
br label %for.body
for.cond.cleanup: ; preds = %if.end
ret void
for.body: ; preds = %entry, %if.end
%i_2.016 = phi i32 [ -3247424, %entry ], [ %add8, %if.end ]
%add41415 = phi i32 [ %var_24.promoted, %entry ], [ %add413, %if.end ]
br i1 %tobool.not, label %if.end, label %if.then
if.then: ; preds = %for.body
%div = sdiv i32 200, %var_10
%1 = add i32 %div, %var_8
%add3 = sub i32 %0, %1
%add4 = add i32 %add3, %add41415
store i32 %add4, ptr @var_24, align 4
br label %if.end
if.end: ; preds = %if.then, %for.body
%add413 = phi i32 [ %add4, %if.then ], [ %add41415, %for.body ]
%div5 = sdiv i32 %i_2.016, 3
%add7 = add i32 %add6, %i_2.016
%add8 = add i32 %add7, %div5
%cmp = icmp slt i32 %add8, 19
br i1 %cmp, label %for.body, label %for.cond.cleanup
}
```
Clang10 -O2 does this:
```
entry:
%tobool = icmp eq i32 %var_3, 0
%.neg = sub i32 %var_2, %var_8
%add2 = add i32 %.neg, %var_14
%add6 = add i32 %var_3, 1056714155
br label %for.body
for.cond.cleanup: ; preds = %if.end
ret void
for.body: ; preds = %entry, %if.end
%i_2.014 = phi i32 [ -3247424, %entry ], [ %add8, %if.end ]
br i1 %tobool, label %if.end, label %if.then
if.then: ; preds = %for.body
%div = sdiv i32 200, %var_10
%0 = load i32, i32* @var_24, align 4
%add3 = sub i32 %add2, %div
%add4 = add i32 %add3, %0
store i32 %add4, i32* @var_24, align 4
br label %if.end
if.end: ; preds = %for.body, %if.then
%div5 = sdiv i32 %i_2.014, 3
%add7 = add i32 %add6, %i_2.014
%add8 = add i32 %add7, %div5
%cmp = icmp slt i32 %add8, 19
br i1 %cmp, label %for.body, label %for.cond.cleanup
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV02P2zYQ_TX0ZbAGRX1YOviwXtfoAg16bi8BJdIWC5lURdrJ5tcXQ33LarIpiqIxFmuRnBnOvHl8Frm16qKl3JP4QOLjht9caZr977_FUbLJjXjb_6wIe4FX0MapQgrgcFXWSgGmduqqvnCnjAZT16ZxN63cG_Dc3Bz88vryAZSGouL6EiTAtQBXKjszLbmFXErdB5NidKFbQo-EPrf_S-dqS8Jnwk6EnS5G5KZyW9NcCDt9IewUx2n4U7HLmqkTSWj7V7Rj-dnJRoPSDu68-cgiEh7albtRApy0jrB0WMbC-0E4HaTTQUBno4iwDMiuiwsAMD6dTQPdBuojAxIe4Slk0S7ymXRzLxBkw4gd0MgngAMIaJzsgiiIY7_NuMd0Q_yoM2Fpl3j2sOpdPJKvGlwpoTDayc8OXMkdkIQySgk7deUlFAqutXGQy7FTWDVJ6FMLSGuDBoW55kpLAZ-UK9Gih-XQgppQ4BYqY2pQ-s4bxbWzj-m1DeoRICx9IixllGLeA-6ZR4Ed_HrXGQ9eV9wZStlIUL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