This is an archive of the discontinued LLVM Phabricator instance.

[ImplicitNullChecks] Uphold an invariant in areMemoryOpsAliased
AbandonedPublic

Authored by sanjoy on May 17 2017, 3:17 PM.

Details

Reviewers
skatkov
Summary

Right now areMemoryOpsAliased has an assertion justified as:

// MMO1 should have a value due it comes from operation we'd like to use
// as implicit null check.
assert(MMO1->getValue() && "MMO1 should have a Value!");

However, it is possible for that invariant to not be upheld in the
following situation (conceptually):

  Null check %RAX

NotNullSucc:
  %RAX = LEA %RSP, 16            // I0
  %RDX = MOV64rm %RAX            // I1

With the current code, we will have an early exit from
ImplicitNullChecks::isSuitableMemoryOp on I0 with SR_Unsuitable.
However, I1 will look plausible (since it loads from %RAX) and
will go ahead and call areMemoryOpsAliased(I1, I0). This will cause
us to fail the assert mentioned above since I1 does not load from an
IR level value and thus is allowed to have a non-Value base address.

The fix is to return SR_Impossible whenever we see an unsuitable
instruction overwrite PointerReg. This would guarantee that when we
call areMemoryOpsAliased, we're guaranteed to be looking at an
instruction that loads from or stores to an IR level value.

Event Timeline

sanjoy created this revision.May 17 2017, 3:17 PM
sanjoy added inline comments.May 17 2017, 3:23 PM
lib/CodeGen/ImplicitNullChecks.cpp
375

I also noticed that there was no need to loop over PrevMI->operands() for the second clause here (if (Suitable == SR_Suitable) ...). I'll remove that control flow in a next patch which should be NFC.

sanjoy added inline comments.May 17 2017, 3:35 PM
test/CodeGen/X86/ImplicitNullChecks/non-value-mem-operand.mir
2

In a later change, I'll move the other implicit null checks to test/CodeGen/X86/ImplicitNullChecks as well.

skatkov requested changes to this revision.May 17 2017, 7:48 PM

It seems there is a bug.

lib/CodeGen/ImplicitNullChecks.cpp
375

Agreed.

384

It seems that RedefinesPointerReg will check always the current MI, while you need a check of PrevMI.

This revision now requires changes to proceed.May 17 2017, 7:48 PM
sanjoy added inline comments.May 17 2017, 8:05 PM
lib/CodeGen/ImplicitNullChecks.cpp
384

My intent was that as we scan down NotNullSucc, we will bail out of the entire transform (by returning SR_Impossible) the first time we see an instruction redefining PointerReg. But this logic can be made clearer by adding an assert, which I will do in an update tomorrow.

skatkov added inline comments.May 17 2017, 8:11 PM
lib/CodeGen/ImplicitNullChecks.cpp
384

Sanjoy, let's consier the first instruction in NotNullSucc, It might happen that it is a Suitable but canHoistInst will return false.
In this case no one ever will check whether it re-defines PointerReg, correct?

sanjoy abandoned this revision.Jun 22 2017, 11:53 PM

@skatkov has checked in a fix for this issue.