[llvm] [InstCombine] Evaluate zext nneg as sext where possible (PR #212230)

John Brawn via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 04:50:14 PDT 2026


================
@@ -203,8 +198,8 @@ define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
 ; DBGINFO:       for.body3.us:
 ; DBGINFO-NEXT:    [[TOBOOL:%.*]] = icmp eq i32 [[B]], 0, !dbg [[META95]]
 ; DBGINFO-NEXT:      #dbg_value(i1 [[TOBOOL]], [[META89]], !DIExpression(), [[META95]])
-; DBGINFO-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4, !dbg [[DBG97:![0-9]+]]
-; DBGINFO-NEXT:      #dbg_value(i8 [[SPEC_SELECT]], [[META90:![0-9]+]], !DIExpression(), [[DBG97]])
+; DBGINFO-NEXT:    [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i32 0, i32 4, !dbg [[DBG97:![0-9]+]]
+; DBGINFO-NEXT:      #dbg_value(i8 poison, [[META90:![0-9]+]], !DIExpression(), [[DBG97]])
----------------
john-brawn-arm wrote:

Yes, though it looks like it's not some new problem. If I adjust the test to use a negative immediate in the select, so it doesn't get converted to zext nneg:
```
define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
entry:
  br label %while.body

while.body:
  %tobool = icmp eq i32 %b, 0
  br i1 %c1, label %for.body3.us, label %for.body3

for.body3.us:
  %spec.select = select i1 %tobool, i8 0, i8 -4
  switch i3 %v1, label %exit [
  i3 0, label %for.end
  i3 -1, label %for.end
  ]

for.body3:
  switch i3 %v2, label %exit [
  i3 0, label %for.end
  i3 -1, label %for.end
  ]

for.end:
  %h = phi i8 [ %spec.select, %for.body3.us ], [ %spec.select, %for.body3.us ], [ 0, %for.body3 ], [ 0, %for.body3 ]
  %conv = sext i8 %h to i32
  %cmp = icmp sgt i32 %a, %conv
  br i1 %cmp, label %exit, label %exit2

exit2:
  unreachable

exit:
  unreachable
}
```
then we get similar output without this PR:
```
for.body3.us:                                     ; preds = %while.body
  %tobool = icmp eq i32 %b, 0, !dbg !17
    #dbg_value(i1 %tobool, !9, !DIExpression(), !17)
  %spec.select = select i1 %tobool, i32 0, i32 -4, !dbg !19
    #dbg_value(i8 poison, !11, !DIExpression(), !19)
```


https://github.com/llvm/llvm-project/pull/212230


More information about the llvm-commits mailing list