[llvm-dev] What about multiple MachineMemOperands in one MI (BranchFolding/MachineInstr::mayAlias)?

Björn Pettersson A via llvm-dev llvm-dev at lists.llvm.org
Fri Sep 27 06:07:44 PDT 2019


Hi!

Does anyone know how it should be interpreted when one MI has multiple MachineMemOperands?
(I've tried to find information but could not find any clear definition.)


For example BranchFolder may do things like this (also see https://godbolt.org/z/iphFH4):

# *** IR Dump Before Control Flow Optimizer ***:
bb.0.entry:
  ...
  JCC_1 %bb.2, 5, implicit killed $eflags
  JMP_1 %bb.1
bb.1.s1:
  CALL64pcrel32 @bar, ... , implicit-def $rax
  MOV16mr killed renamable $rax, 1, $noreg, 0, $noreg, renamable $bx :: (store 2 into %ir.r)
  JMP_1 %bb.3
bb.2.s2:
  CALL64pcrel32 @bar, ... , implicit-def $rax
  MOV16mr killed renamable $rax, 1, $noreg, 0, $noreg, renamable $bx :: (store 2 into %ir.r2)
bb.3.cond.end:
  ...


# *** IR Dump After Control Flow Optimizer ***:
bb.0.entry:
  ...
  CALL64pcrel32 @bar, ... , implicit-def $rax
  MOV16mr killed renamable $rax, 1, $noreg, 0, $noreg, renamable $bx :: (store 2 into %ir.r2), (store 2 into %ir.r)
  ...


So after branch folding we get a single store with two MachineMemOperands.

Obviously we do not store into two locations (it is still a single two byte store).
So is it (always) correct to interpret the list of MachineMemOperands as the instruction will store to one of the locations?

Is perhaps allowed for a backend to have a store instruction that that actually stores to multiple locations at once, such as:
MultiStoreInstr $ptr0, 1, $ptr1, 2, $ptr2, 18 :: (store 2 intro %my.ptr0.var), (store 2 intro %my.ptr1.var), (store 2 intro %my.ptr2.var)
(maybe that is impossible for other reasons).


Background to my questions: The problem with the rewrite done by BranchFolder is that it sometimes mess up alias analysis.
In fact, MachineInstr::mayAlias bails out when it finds an MI with more (or less) than one MachineMemOperand.

So if we have (trivially non aliasing) stores like this before branch folding
  STORE ... :: (store 2 into %struct)
  STORE ... :: (store 2 into %struct + 2)
then we get this after branch folding
  STORE ... :: (store 2 into %struct), (store 2 into %struct.2)
  STORE ... :: (store 2 into %struct + 2), (store 2 into %struct.2 + 2)
and then MachineInstr::mayAlias fail to detect the stores as not aliasing.

Without knowing how to interpret the situation with multiple MachineMemOperands I guess it is hard to improve the analysis.
Maybe BranchFolding could do something better here (is it a bug to add multiple operands like this?).


Regards,
Björn


More information about the llvm-dev mailing list