<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/61268>61268</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            WRONG code: likely instcombine of multiple i1:s.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:instcombine
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            nikic,
            rotateright
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          JonPsson
      </td>
    </tr>
</table>

<pre>
    This program should print 0:

```
int printf(const char *, ...);
long a = 0, d = -1L, h = 0;
char b = 0, c = 0;
unsigned char g = 0;
int ****e = 0;
int *****f = &e;
int main() {
  long i = 0;
  for (g = 4; g; g++) {
    c = 0 >= a;
     //  -1      int64_max 1          0 
    i = d > (9223372036854775807 - c) ? : (d + c);
    if (i) {
      char *j = &b;
      //  0 int64_max         -1
      h = a > 9223372036854775807 - d; // OF 
 *j = h;
      *f = 0;
    }
  }
  printf("%d\n", b);
}

```

I think this program is well defined,as 'i' will always be 0, and the overflowing subtract in the inner clause will never happen. 

clang -O0 -march=z13 wrong0.i -o a.out ; ./a.out
0
clang -O1 -march=z13 wrong0.i -o a.out ; ./a.out
1

I have found this transformation by InstCombine which I think look suspicious:

```
for.body: ; preds = %for.cond  
  %1 = load i64, ptr @a, align 8, !tbaa !7
  %cmp = icmp sge i64 0, %1
  %C = zext i1 %cmp to i32
  %D = load i64, ptr @d, align 8, !tbaa !7
  %MAX_min_C = sub nuw nsw i64 9223372036854775807, %C
  %CC_DGT = icmp sgt i64 %D, %MAX_min_C
 %BOOL_DGT = zext i32 %CC_DGT to i64
  %add = add nsw i64 %D, %C
  %cond = select i1 %CC_DGT, i64 %BOOL_DGT, i64 %add
  %tobool7 = icmp ne i64 %cond, 0
  br i1 %tobool7, label %enter, label %for.inc

*** IR Dump After InstCombinePass on main ***

for.body: ; preds = %for.cond  
  %A = load i64, ptr @a, align 8, !tbaa !7
  %C_CC = icmp slt i64 %A, 1
  %D = load i64, ptr @d, align 8, !tbaa !7
  %C_BOOL = sext i1 %C_CC to i64
  %D_NE_MAX = icmp ne i64 %D, 9223372036854775807
  %NOT_C = xor i1 %C_CC, true
  %NOT_C_or_D_NE_MAX = select i1 %NOT_C, i1 true, i1 %D_NE_MAX
  %D_EQ_C = icmp eq i64 %D, %C_BOOL
  %tobool = select i1 %NOT_C_or_D_NE_MAX, i1 %D_EQ_C, i1 false
 br i1 %tobool, label %for.inc, label %enter
```

Before instcombine, it all makes sense, but then it seems that something has gone wrong: %tobool will be false (D is not equal to C), and as the branch targets have been swapped, the inner clause will now be entered, which is wrong.

I bisected this to b751da4 (HEAD, refs/bisect/bad) "[InstCombine] Handle integer extension in `select` patterns using the condition as value", which might be related as it indeed changes InstCombine.




</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV1tzmzwT_jXyzY49IGxDLnzhQ9L2m7bp1-nM2zuPAAFqhORKIk76699ZAY7I4T17PFhoT89qH2llZq2oFecbstoRSpW4EwWhlNA9odRoxxw3om4czq0OM9a5RpvN_7T6Yq1Ws1yXj5tvjbBwMro2rAXb6E6WcDJCOYhIsiXRgUTjcx0NX_-KKl6xIjQrtLIOioYZIHRL6B4WiwWhVyTZ9dpSqxoYkOQAEYpLP5zHH_GlGeZHZe8nf1Iunsk75fMu-4D1MykC8yCGL_8TOaHbyqsQuuYTrZYJRWhG6BWQdJgH8KmIZ04BKo25Zz2aJUl2UPcPuvPfiQ8YcwKSXOOABZ4AgNAbQm8A5rF_BaHcenls2QMME_iJ4Mmix4Oreo0orihNkpRGyTpbLdN0lUUpzKHwKJIbIMkWtUogdOdnJ9FFhULxAjJcCvxjXK98CvuCOwoQj595HGr2NWce8OtoS1y-weHtzZjrJXrzIvRQxmgiIOlhfAmGF-L67bIqyWqv-p0DebgcF5NXt0D__ACuEeoOn087SVg4cymh5JVQvCR0zywQmgpCUzgLKYHJM3u0kPOe40yV4BoO-p6bSuqzU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