This is an archive of the discontinued LLVM Phabricator instance.

[Mips][msa] Fix inifinite loop for mips.nori.b intrinsic
ClosedPublic

Authored by mbrkusanin on Sep 6 2019, 8:29 AM.

Details

Summary

When value of immediate in mips.nori.b is 255 (which has all ones in binary form
as 8bit integer) DAGCombiner and Legalizer would fall in an infinite loop.
DAGCombiner would try to simplify 'or %value, -1' by turning %value into UNDEF.
Legalizer will turn it back into Constant<0> which would then be again turned
into UNDEF by DAGCombiner. To avoid this loop we make UNDEF legal for MSA int
types on Mips.

Diff Detail

Event Timeline

mbrkusanin created this revision.Sep 6 2019, 8:29 AM

More detailed explanation follows:

Expansion for mips.nori.b goes something like this:

mips.nori.b %dst, %val, imm
%dst = not (or %value, imm)
%dst = xor (or %value, imm), -1

For -1 as immediate we should get:

%dst = xor (or %value, -1), -1
%dst = xor -1, -1
%dst = 0

When value of immediate is 255 (-1 for i8, so all bits are 1) issue shows up in DAGCombiner when it runs into node for XOR:

SelectionDAG:

...
              t31: v2i64 = BUILD_VECTOR Constant:i64<0>, Constant:i64<0>
            t2: i64,ch = CopyFromReg t0, Register:i64 %0
          t28: v2i64 = insert_vector_elt t31, t2, Constant:i32<0>
          t4: i64,ch = CopyFromReg t0, Register:i64 %1
        t30: v2i64 = insert_vector_elt t28, t4, Constant:i32<1>
      t6: v16i8 = bitcast t30
    t24: v16i8 = or t6, t23
  t25: v16i8 = xor t24, t23
...
t23: v16i8 = BUILD_VECTOR Constant:i32<255>, Constant:i32<255>, ...

-DAGCombiner::visitXOR() is called which in turns calles SimplifyDemandedBits().
--SimplifyDemandedBits() for XOR will call SimplifyDemandedBits() for OR.
--SimplifyDemandedBits() for OR will call SimplifyDemandedBits() for Operand(0)
--SimplifyDemandedBits() for Operand(0) will make a new UNDEF node.
-Legalizer for Mips will replace UNDEF node with a new Constant<0> node.
-DAGCombiner::visitXOR() is called which again makes a new UNDEF node (this time from Constant<0>).
Legalizer for Mips will replace UNDEF node with a new Constant<0> node.
...
now we are in a loop.

Making UNDEF legal avoids making a loop. It also produces shorter code which is why there are changes to other tests.

This revision is now accepted and ready to land.Sep 10 2019, 10:13 PM
This revision was automatically updated to reflect the committed changes.