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

    <tr>
        <th>Summary</th>
        <td>
            Different condition selection as a result of foldICmpWithDominatingICmp using DomConditionCache
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

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

<pre>
    Hello
Thought of getting clarification on a behavior, I am observing with foldCmpWithDominatingICmp ([Patch](https://github.com/llvm/llvm-project/pull/75370/files?diff=split&w=0)). 

**Test case:**
In one of the test scenarios : [test](https://godbolt.org/z/zT6snMsv5) I came across, there is a while loop which executes on condition 'm<=2' (m is input). 
'And' operation is done unconditionally, on a and b. Value of a is changed when m>=1, whereas b is changed when
m>=2. Modified a and b values are stored back, and are added at the end-of-the-loop.

**Behavior**
For this test, I observe a different condition for final comparison 'm>=2' (i.e. if(m>=2) b|= temp1), after improvements were done to foldICmpWithDominatingICmp with DomConditionCache.  The dominator edge (and thus condition and it's constant range) considered for folding seems to be different now, results different behavior.

Relevant IR after instcombine pass, with 18.0.1, shown [here](https://godbolt.org/z/zT6snMsv5)
19: ; preds = %17, %10
       %20 = phi i32 [ %18, %17 ], [ %15, %10 ], !dbg !41
=>  %24 = icmp eq i32 %0, 2, !dbg !60
       br i1 %24, label %25, label %27, !dbg !62

Relevant IR after instcombine pass, with 17.0.01,  shown [here](https://godbolt.org/z/rvj8xaKG4)
19: ; preds = %17, %10
       %20 = phi i32 [ %18, %17 ], [ %15, %10 ], !dbg !41
=>  %24 = icmp ugt i32 %0, 1, !dbg !60
       br i1 %24, label %25, label %27, !dbg !62

I tried debugging code in InstCombineCompares.cpp, and I observe that, for BranchInsts returned by 'DC.conditionsFor(X)', we consider the first BranchInst which satisfies the conditions, and return.
For example, for the icmp instruction corresponding to code 'if(m>=2) b|=temp1;', the else-block in 'for (BranchInst *BI...) {}' block would executed, i.e. Edge (BI->getParent() to BI->getSuccessor(1)) dominates Cmp.Parent(). 

However, if we consider the 2nd Branch returned by 'DC.conditionFor(X)',, (which also dominates Cmp.Parent()), then the dominating condition would be 'm==0' (in BB 10), which would give 'constant range' (1,0), thus would result in choosing condition i.e. 'm!=1'.  This condition was calculated before the modification in [patch](https://github.com/llvm/llvm-project/pull/75370/files?diff=split&w=0). 

Thought of checking, if the issue makes sense or is it expected behavior.   

I tried going through previous conversation/issues/discussions but if I ignored important aspects related to this, kindly help me understand them.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV1-P4jgS_zTmpdRR4kAHHngYoLlFp5VWe6O7e3XsSuJtx87aDszcpz-VDTQ90zfSrnQn3YhpIHb9_VX9qhAh6N4ibtlqx1aHhZjj4Px26p3C3xetU1-3P6ExjpUHVn76PLi5HyK4DnqMUdsepBFed1qKqJ0FZ0FAi4M4a-cZ38MJxAiuDejPdPui4wCdM2o_Tv_QcTi4UVtBik77cQLG12y1-0VEObDVgfH1EOMUWP2J8SPjx17HYW4L6UbGj8acb29Pk3e_oYyMH6fZGMaPzapuSsaPnTYYWH1UuutYfQiT0ZHx5wurDyXjG8Y3BeTQrn85vT5jiCBFwGQ5vdLpiQJEij4OCJFuBYlWeO0CsPoTsNWOnn7ou1OtM7Fwvmf8-C_6__k52J_DecX4Bk4gxYggpHchUOLigB5BBxBwGbRBMM5N9FEOgF9QzhEDpVs6q3TKPePNyOo9qw-c8YZyOZK8ttMcHwPlzSer6Iab0GfYdABFkc32rk4Y85X8SIgKq6At4O_CzCl8QRJyELZHBZcBLYysfmH1oSKRC7kuArTf3soOXK_yAn52Snca1c0CnMlAAOERQnQeFbRCvpJOOqfHQim6HxMCaNWT657igE-UneJ7JHf3UnxD8eg8xEGHBGCu0VygCAKoUNCjjQ-J7ZyHTlthQLpxEl6He7ZfHrKtCyxAd5T3-8EGWtYQJBBxnKpUcnsQXUQP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