runtime: Bad interaction between constant prop and CSE

Exploring the invariant hoisting code I found that it doesn’t work with the assignments.

In optimizer.cpp there’s a code:

   if (treeIsInvariant)
    {
        // Tree must be a suitable CSE candidate for us to be able to hoist it.
        treeIsHoistable = optIsCSEcandidate(tree);

optIsCSEcandidate is false for assignments.

Though it won’t work with the assignment the analysis will detect that the the expression subtree of the assignment is invariant and will actually clone the expression to the prehead block of the loop.

We’ll get a tree looking e.g. like this:

------------ BB04 [029..???), preds={BB01} succs={BB02}

***** BB04, stmt 8 (top level)
     (  5,  5) [000098] -------------             *  stmtExpr  void  (top level) (IL   ???...  ???)
N006 (  0,  0) [000096] -------------             |  /--*  nop       void  
N007 (  5,  5) [000097] -------------             \--*  comma     void  
N004 (  1,  1) [000095] -------H-----                |  /--*  lclVar    int    V04 loc3         u:3 <l:$46, c:$1c1>
N005 (  5,  5) [000091] -------H-----                \--*  +         int    <l:$4a, c:$1c6>
N002 (  1,  1) [000094] -------H-----                   |  /--*  lclVar    int    V03 loc2         u:3 <l:$44, c:$1c0>
N003 (  3,  3) [000092] -------H-----                   \--*  +         int    <l:$49, c:$1c5>
N001 (  1,  1) [000093] -------H-----                      \--*  lclVar    int    V02 loc1         u:3 <l:$42, c:$101>

------------ BB02 [029..043) -> BB02 (cond), preds={BB02,BB04} succs={BB03,BB02}

The result of such a tree is not used and on the code generator no code is produced for such a tree.

/cc @BruceForstall @brucehoult @briansull @Dmitri-Botcharnikov

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 40 (40 by maintainers)

Commits related to this issue

Most upvoted comments

VN identifies the invariant expression as being constant and CSE rejects constants.

There’s a bit of conflict between constant propagation and CSE. Constant propagation uses conservative VNs and thinks that the expression is not a constant because other threads may change the static variable ii. CSE uses liberal VNs and thinks that the expression is a constant so it ignores it.

The fact that constant prop uses conservative VNs is debatable given that ii isn’t volatile. But if that’s how it’s supposed to work then optValnumCSE_Locate should also use conservative VNs to identify constant expressions.