Different condition selection as a result of foldICmpWithDominatingICmp using DomConditionCache
Hello Thought of getting clarification on a behavior, I am observing with foldCmpWithDominatingICmp (Patch).
Test case: In one of the test scenarios : test 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 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 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.
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.
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)
10: ; preds = %8
%11 = call i32 @time(ptr noundef null) #1
%12 = shl nuw i32 1, %2
%13 = xor i32 %12, -1
%14 = load i32, ptr %1, align 4, !tbaa !7
%15 = and i32 %14, %13
%16 = icmp eq i32 %0, 0
br i1 %16, label %19, label %17
17: ; preds = %10
%18 = or i32 %14, %12
br label %19
19: ; preds = %17, %10
%20 = phi i32 [ %18, %17 ], [ %15, %10 ]
CFG:
bb10
| \
| \
| bb17
| /
| /
bb19
The edge (bb10, bb19) doesn't dominate bb19. So m==0 is not a valid dominating condition.