llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

dominator optimizer/gvn fails to recognize identical tests

Open echristo opened this issue 17 years ago • 6 comments

Bugzilla Link 3921
Resolution FIXED
Resolved on Apr 03, 2009 19:18
Version trunk
OS All
CC @lattner

Extended Description

Here's a simple testcase that shows the problem:

extern int a;

int foo (void) { if (a == 4) { return 5; } if (a == 5) { return 4; } if (a == 4) { return 5 + a; } if (a == 5) { return 4 + a; }

return a; }

and the corresponding llvm code (from llvm-gcc -O2):

define i32 @​foo() nounwind readonly ssp { entry: %0 = load i32* @​a, align 4 ; [#uses=5] %1 = icmp eq i32 %0, 4 ; [#uses=2] br i1 %1, label %bb8, label %bb1

bb1: ; preds = %entry %2 = icmp eq i32 %0, 5 ; [#uses=2] br i1 %2, label %bb8, label %bb3

bb3: ; preds = %bb1 br i1 %1, label %bb4, label %bb5

bb4: ; preds = %bb3 %3 = add i32 %0, 5 ; [#uses=1] ret i32 %3

bb5: ; preds = %bb3 br i1 %2, label %bb6, label %bb8

bb6: ; preds = %bb5 %4 = add i32 %0, 4 ; [#uses=1] ret i32 %4

bb8: ; preds = %bb5, %bb1, %entry %.0 = phi i32 [ 5, %entry ], [ 4, %bb1 ], [ %0, %bb5 ] ; [#uses=1] ret i32 %.0 }

echristo avatar Mar 31 '09 23:03 echristo

Works for me. Excellent.

echristo avatar Apr 04 '09 02:04 echristo

nice!

lattner avatar Apr 01 '09 03:04 lattner

This is fixed in r68172.

Note that GVN doesn't handle his case very generally, since it's not a constant propagator. For instance, if you pass in the IR Eric posted, the GVN will not be able to simplify it. If you start from the original IR, where it re-does the comparison, then it will successfully simplify it.

llvmbot avatar Apr 01 '09 03:04 llvmbot

Neither follow the dom tree.

lattner avatar Apr 01 '09 00:04 lattner

I can add it, but it seems like other optzns should be picking this up. Why isn't CondPropagate or SCCP getting this?

llvmbot avatar Apr 01 '09 00:04 llvmbot

Owen, can this be easily handled in GVN? I'm thinking of something like:

if we see a cond branch on an i1 value with that is not ->hasOneUse(), add an equivalence to the next BB's table that says the i1 is true or false, depending on the edge.

Since GVN often eliminates the redundant conditions, this would let it turn the dominated branches into a branch on constant.

lattner avatar Mar 31 '09 23:03 lattner