Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from llvm:master #43

Merged
merged 8 commits into from
Aug 29, 2019
Merged

[pull] master from llvm:master #43

merged 8 commits into from
Aug 29, 2019

Commits on Aug 29, 2019

  1. [InstCombine] Fold '(-1 u/ %x) u< %y' to '@llvm.umul.with.overflow' +…

    … overflow bit extraction
    
    Summary:
    `(-1 u/ %x) u< %y` is one of (3?) common ways to check that
    some unsigned multiplication (will not) overflow.
    Currently, we don't catch it. We could:
    ```
    ----------------------------------------
    Name: no overflow
      %o0 = udiv i4 -1, %x
      %r = icmp ult i4 %o0, %y
    =>
      %o0 = udiv i4 -1, %x
      %n0 = umul_overflow i4 %x, %y
      %r = extractvalue {i4, i1} %n0, 1
    
    Done: 1
    Optimization is correct!
    
    ----------------------------------------
    Name: no overflow, swapped
      %o0 = udiv i4 -1, %x
      %r = icmp ugt i4 %y, %o0
    =>
      %o0 = udiv i4 -1, %x
      %n0 = umul_overflow i4 %x, %y
      %r = extractvalue {i4, i1} %n0, 1
    
    Done: 1
    Optimization is correct!
    
    ----------------------------------------
    Name: overflow
      %o0 = udiv i4 -1, %x
      %r = icmp uge i4 %o0, %y
    =>
      %o0 = udiv i4 -1, %x
      %n0 = umul_overflow i4 %x, %y
      %n1 = extractvalue {i4, i1} %n0, 1
      %r = xor %n1, -1
    
    Done: 1
    Optimization is correct!
    
    ----------------------------------------
    Name: overflow
      %o0 = udiv i4 -1, %x
      %r = icmp ule i4 %y, %o0
    =>
      %o0 = udiv i4 -1, %x
      %n0 = umul_overflow i4 %x, %y
      %n1 = extractvalue {i4, i1} %n0, 1
      %r = xor %n1, -1
    
    Done: 1
    Optimization is correct!
    ```
    
    As it can be observed from tests, while simply forming the `@llvm.umul.with.overflow`
    is easy, if we were looking for the inverted answer, then more work needs to be done
    to cleanup the now-pointless control-flow that was guarding against division-by-zero.
    This is being addressed in follow-up patches.
    
    Reviewers: nikic, spatel, efriedma, xbolva00, RKSimon
    
    Reviewed By: nikic, xbolva00
    
    Subscribers: hiraditya, llvm-commits
    
    Tags: #llvm
    
    Differential Revision: https://reviews.llvm.org/D65143
    
    llvm-svn: 370347
    LebedevRI committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    fb38b7a View commit details
    Browse the repository at this point in the history
  2. [InstCombine] Fold '((%x * %y) u/ %x) != %y' to '@llvm.umul.with.over…

    …flow' + overflow bit extraction
    
    Summary:
    `((%x * %y) u/ %x) != %y` is one of (3?) common ways to check that
    some unsigned multiplication (will not) overflow.
    Currently, we don't catch it. We could:
    ```
    $ /repositories/alive2/build-Clang-unknown/alive -root-only ~/llvm-patch1.ll
    Processing /home/lebedevri/llvm-patch1.ll..
    
    ----------------------------------------
    Name: no overflow
      %o0 = mul i4 %y, %x
      %o1 = udiv i4 %o0, %x
      %r = icmp ne i4 %o1, %y
      ret i1 %r
    =>
      %n0 = umul_overflow i4 %x, %y
      %o0 = extractvalue {i4, i1} %n0, 0
      %o1 = udiv %o0, %x
      %r = extractvalue {i4, i1} %n0, 1
      ret %r
    
    Done: 1
    Optimization is correct!
    
    ----------------------------------------
    Name: no overflow
      %o0 = mul i4 %y, %x
      %o1 = udiv i4 %o0, %x
      %r = icmp eq i4 %o1, %y
      ret i1 %r
    =>
      %n0 = umul_overflow i4 %x, %y
      %o0 = extractvalue {i4, i1} %n0, 0
      %o1 = udiv %o0, %x
      %n1 = extractvalue {i4, i1} %n0, 1
      %r = xor %n1, -1
      ret i1 %r
    
    Done: 1
    Optimization is correct!
    
    ```
    
    Reviewers: nikic, spatel, efriedma, xbolva00, RKSimon
    
    Reviewed By: nikic
    
    Subscribers: hiraditya, llvm-commits
    
    Tags: #llvm
    
    Differential Revision: https://reviews.llvm.org/D65144
    
    llvm-svn: 370348
    LebedevRI committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    473a063 View commit details
    Browse the repository at this point in the history
  3. [SimplifyCFG] FoldTwoEntryPHINode(): don't bailout on i1 PHI's if we …

    …can hoist a 'not' from incoming values
    
    Summary:
    As it can be seen in the tests in D65143/D65144, even though we have formed an '@llvm.umul.with.overflow'
    and got rid of potential for division-by-zero, the control flow remains, we still have that branch.
    
    We have this condition:
    ```
      // Don't fold i1 branches on PHIs which contain binary operators
      // These can often be turned into switches and other things.
      if (PN->getType()->isIntegerTy(1) &&
          (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
           isa<BinaryOperator>(PN->getIncomingValue(1)) ||
           isa<BinaryOperator>(IfCond)))
        return false;
    ```
    which was added back in rL121764 to help with `select` formation i think?
    
    That check prevents us to flatten the CFG here, even though we know
    we no longer need that guard and will be able to drop everything
    but the '@llvm.umul.with.overflow' + `not`.
    
    As it can be seen from tests, we end here because the `not` is being
    sinked into the PHI's incoming values by InstCombine,
    so we can't workaround this by hoisting it to after PHI.
    
    Thus i suggest that we relax that check to not bailout if we'd get to hoist the `not`.
    
    Reviewers: craig.topper, spatel, fhahn, nikic
    
    Reviewed By: spatel
    
    Subscribers: hiraditya, llvm-commits
    
    Tags: #llvm
    
    Differential Revision: https://reviews.llvm.org/D65147
    
    llvm-svn: 370349
    LebedevRI committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    9f35d2b View commit details
    Browse the repository at this point in the history
  4. [InstSimplify] Drop leftover "division-by-zero guard" around `@llvm.u…

    …mul.with.overflow` overflow bit
    
    Summary:
    Now that with D65143/D65144 we've produce `@llvm.umul.with.overflow`,
    and with D65147 we've flattened the CFG, we now can see that
    the guard may have been there to prevent division by zero is redundant.
    We can simply drop it:
    ```
    ----------------------------------------
    Name: no overflow and not zero
      %iszero = icmp ne i4 %y, 0
      %umul = umul_overflow i4 %x, %y
      %umul.ov = extractvalue {i4, i1} %umul, 1
      %retval.0 = and i1 %iszero, %umul.ov
      ret i1 %retval.0
    =>
      %iszero = icmp ne i4 %y, 0
      %umul = umul_overflow i4 %x, %y
      %umul.ov = extractvalue {i4, i1} %umul, 1
      %retval.0 = and i1 %iszero, %umul.ov
      ret %umul.ov
    
    Done: 1
    Optimization is correct!
    ```
    
    Reviewers: nikic, spatel, xbolva00
    
    Reviewed By: spatel
    
    Subscribers: hiraditya, llvm-commits
    
    Tags: #llvm
    
    Differential Revision: https://reviews.llvm.org/D65150
    
    llvm-svn: 370350
    LebedevRI committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    aaf6ab4 View commit details
    Browse the repository at this point in the history
  5. [InstSimplify] Drop leftover "division-by-zero guard" around `@llvm.u…

    …mul.with.overflow` inverted overflow bit
    
    Summary:
    Now that with D65143/D65144 we've produce `@llvm.umul.with.overflow`,
    and with D65147 we've flattened the CFG, we now can see that
    the guard may have been there to prevent division by zero is redundant.
    We can simply drop it:
    ```
    ----------------------------------------
    Name: no overflow or zero
      %iszero = icmp eq i4 %y, 0
      %umul = smul_overflow i4 %x, %y
      %umul.ov = extractvalue {i4, i1} %umul, 1
      %umul.ov.not = xor %umul.ov, -1
      %retval.0 = or i1 %iszero, %umul.ov.not
      ret i1 %retval.0
    =>
      %iszero = icmp eq i4 %y, 0
      %umul = smul_overflow i4 %x, %y
      %umul.ov = extractvalue {i4, i1} %umul, 1
      %umul.ov.not = xor %umul.ov, -1
      %retval.0 = or i1 %iszero, %umul.ov.not
      ret i1 %umul.ov.not
    
    Done: 1
    Optimization is correct!
    ```
    Note that this is inverted from what we have in a previous patch,
    here we are looking for the inverted overflow bit.
    And that inversion is kinda problematic - given this particular
    pattern we neither hoist that `not` closer to `ret` (then the pattern
    would have been identical to the one without inversion,
    and would have been handled by the previous patch), neither
    do the opposite transform. But regardless, we should handle this too.
    I've filled [[ https://bugs.llvm.org/show_bug.cgi?id=42720 | PR42720 ]].
    
    Reviewers: nikic, spatel, xbolva00, RKSimon
    
    Reviewed By: spatel
    
    Subscribers: hiraditya, llvm-commits
    
    Tags: #llvm
    
    Differential Revision: https://reviews.llvm.org/D65151
    
    llvm-svn: 370351
    LebedevRI committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    c584786 View commit details
    Browse the repository at this point in the history
  6. [mips] Fix expanding lw/sw $reg1, symbol($reg2) instruction

    When a "base" in the `lw/sw $reg1, symbol($reg2)` instruction is
    a register and generated code is position independent, backend
    does not add the "base" value to the symbol address.
    ```
    lw     $reg1, %got(symbol)($gp)
    lw/sw  $reg1, 0($reg1)
    ```
    
    This patch fixes the bug and adds the missed `addu` instruction by
    passing `BaseReg` into the `loadAndAddSymbolAddress` routine and handles
    the case when the `BaseReg` is the zero register to escape redundant
    `move reg, reg` instruction:
    ```
    lw     $reg1, %got(symbol)($gp)
    addu   $reg1, $reg1, $reg2
    lw/sw  $reg1, 0($reg1)
    ```
    
    Differential Revision: https://reviews.llvm.org/D66894
    
    llvm-svn: 370353
    atanasyan committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    3464b91 View commit details
    Browse the repository at this point in the history
  7. [mips] Inline emitStoreWithSymOffset and emitLoadWithSymOffset method…

    …s. NFC
    
    Both methods `MipsTargetStreamer::emitStoreWithSymOffset` and
    `MipsTargetStreamer::emitLoadWithSymOffset` are almost the same and
    differ argument names only. These methods are used in the single place
    so it's better to inline their code and remove original methods.
    
    llvm-svn: 370354
    atanasyan committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    b23857c View commit details
    Browse the repository at this point in the history
  8. Allow replaceAndRecursivelySimplify to list unsimplified visitees.

    This is part of D65280 and split it to avoid ABI changes on the 9.0
    release branch.
    
    llvm-svn: 370355
    jsonn committed Aug 29, 2019
    Configuration menu
    Copy the full SHA
    799c966 View commit details
    Browse the repository at this point in the history