This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Set the SideEffects of branch & call instructions from 1 to 0
ClosedPublic

Authored by qiucf on Dec 29 2019, 6:56 PM.

Details

Summary

If we didn't set the value for hasSideEffects bit in our td file, llvm-tblgen will set it as true for those instructions which has no match pattern.
But for branch & call instructions, PPC can model, so those instructions don't need the hasSideEffects flag.

This patch is to set the SideEffects of branch & call instructions from 1 to 0. The SideEffects flag of below instructions have been set from 1 to 0:

Below instructions can be generated:

BCC
BCCCTR
BCCCTR8
BCCCTRL
BCCCTRL8
BCCLR
BCCTR
BCCTR8
BCCTR8n
BCCTRL
BCCTRL8
BCCTRL8n
BCCTRLn
BCCTRn
BCLR
BCLRn
BCLalways
BCTR
BCTR8
BDNZ
BDNZ8
BDNZLR
BDNZLR8
BDZ
BDZ8
BDZLR
BDZLR8
BL8_NOP_TLS
BL_TLS
CTRL_DEP
TAILB
TAILB8
TAILBA
TAILBA8
TAILBCTR
TAILBCTR8

Below instructions can't be generated:

BA
BCL
BCLRL
BCLRLn
BCLn
BDNZA
BDNZAm
BDNZAp
BDNZL
BDNZLA
BDNZLAm
BDNZLAp
BDNZLRL
BDNZLRLm
BDNZLRLp
BDNZLRm
BDNZLRp
BDNZLm
BDNZLp
BDNZm
BDNZp
BDZA
BDZAm
BDZAp
BDZL
BDZLA
BDZLAm
BDZLAp
BDZLRL
BDZLRLm
BDZLRLp
BDZLRm
BDZLRp
BDZLm
BDZLp
BDZm
BDZwap
BL8_TLS
BL8_TLS_
BLRL

BCCA
BCCL
BCCLA
BCCLRL
gBC
gBCA
gBCAat
gBCCTR
gBCCTRL
gBCL
gBCLA
gBCLAat
gBCLR
gBCLRL
gBCLat
gBCat

Diff Detail

Event Timeline

ZhangKang created this revision.Dec 29 2019, 6:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 29 2019, 6:56 PM
ZhangKang edited the summary of this revision. (Show Details)Dec 29 2019, 6:57 PM
Jim added a subscriber: Jim.Dec 29 2019, 10:58 PM

I have tested the spec performance for this patch, there is no degression.

Do you expect any effect at all from doing this? These are all scheduling barriers?

Do you expect any effect at all from doing this? These are all scheduling barriers?

I do the performance test just to confirm there is no degression, because this patch is not a NFC patch.
From the definition of hasUnmodeledSideEffects(), we can know calling, branching and returning instructions shouldn't set hasSideEffect flag, because we can model them.
I do this patch to fix the hasSideEffect flag, it may give us more potential optimization opportunities.

/// Return true if this instruction has side
  /// effects that are not modeled by other flags.  This does not return true
  /// for instructions whose effects are captured by:
  ///
  ///  1. Their operand list and implicit definition/use list.  Register use/def
  ///     info is explicit for instructions.
  ///  2. Memory accesses.  Use mayLoad/mayStore.
  ///  3. Calling, branching, returning: use isCall/isReturn/isBranch.
  ///
  /// Examples of side effects would be modifying 'invisible' machine state like
  /// a control register, flushing a cache, modifying a register invisible to
  /// LLVM, etc.
  bool hasUnmodeledSideEffects() const {
    return Flags & (1ULL << MCID::UnmodeledSideEffects);
  }

Do you expect any effect at all from doing this? These are all scheduling barriers?

The SideEffect flag is not only used for scheduling, for example:

bool ImplicitNullChecks::canHandle(const MachineInstr *MI) {
  if (MI->isCall() || MI->mayRaiseFPException() ||
      MI->hasUnmodeledSideEffects())
    return false;
  auto IsRegMask = [](const MachineOperand &MO) { return MO.isRegMask(); };
  (void)IsRegMask;

  assert(!llvm::any_of(MI->operands(), IsRegMask) &&
         "Calls were filtered out above!");

  auto IsUnordered = [](MachineMemOperand *MMO) { return MMO->isUnordered(); };
  return llvm::all_of(MI->memoperands(), IsUnordered);
}

Do you expect any effect at all from doing this? These are all scheduling barriers?

I think, there are more than scheduling affected with this flag and from the documentation in the .td, we should clear it for branch instruction as the isBranch bit already model it.

./Target/Lanai/LanaiInstrInfo.cpp:  if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
./Target/Lanai/LanaiDelaySlotFiller.cpp:    if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isLabel() ||
./Target/PowerPC/PPCQPXLoadSplat.cpp:      if (MI->hasUnmodeledSideEffects() || MI->isCall()) {
./Target/PowerPC/PPCInstrInfo.cpp:  if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
./Target/Hexagon/HexagonStoreWidening.cpp:    if (MI->isCall() || MI->hasUnmodeledSideEffects())
./Target/Hexagon/HexagonEarlyIfConv.cpp:  if (MI->hasUnmodeledSideEffects())
./Target/Hexagon/HexagonInstrInfo.cpp:  if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
./Target/Hexagon/RDFDeadCode.cpp:  if (MI->hasOrderedMemoryRef() || MI->hasUnmodeledSideEffects() ||
./Target/Hexagon/HexagonCopyToCombine.cpp:         MI.hasUnmodeledSideEffects() || MI.isInlineAsm() ||
./Target/Hexagon/HexagonExpandCondsets.cpp:  if (MI->hasUnmodeledSideEffects() || MI->mayStore())
./Target/Hexagon/HexagonExpandCondsets.cpp:  if (TheI.hasUnmodeledSideEffects())
./Target/Hexagon/HexagonExpandCondsets.cpp:    if (MI->hasUnmodeledSideEffects())
./Target/Hexagon/HexagonBitSimplify.cpp:    if (MI->isPHI() || MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
./Target/Hexagon/HexagonBitSimplify.cpp:  if (MI->hasUnmodeledSideEffects() || MI->isInlineAsm())
./Target/Mips/MipsDelaySlotFiller.cpp:          Candidate.hasUnmodeledSideEffects());
./Target/WebAssembly/WebAssemblyRegStackify.cpp:      // These instruction have hasUnmodeledSideEffects() returning true
./Target/WebAssembly/WebAssemblyRegStackify.cpp:  if (MI.hasUnmodeledSideEffects()) {
./Target/WebAssembly/WebAssemblyRegStackify.cpp:      // These instructions have hasUnmodeledSideEffects() returning true
./Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp:              I->hasUnmodeledSideEffects() || I->hasOrderedMemoryRef())
./Target/AMDGPU/SIInstrInfo.cpp:    if (!MI.mayLoad() || MI.hasUnmodeledSideEffects())
./Target/AMDGPU/SIInstrInfo.cpp:  if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects())
./Target/AMDGPU/SILoadStoreOptimizer.cpp:      if (MBBI->hasUnmodeledSideEffects()) {
./Target/AMDGPU/log:85f38901266a6 llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (Matt Arsenault          2019-07-19 19:47:30 +0000 1551)     if (!MI.mayLoad() || MI.hasUnmodeledSideEffects())
./Target/AMDGPU/log:9cfc75c214d42 llvm/lib/Target/AMDGPU/SIInstrInfo.cpp (Duncan P. N. Exon Smith 2016-06-30 00:01:54 +0000 2557)   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects())
./Target/ARM/ARMOptimizeBarriersPass.cpp:          MI->hasUnmodeledSideEffects() ||
./Target/ARM/ARMLoadStoreOptimizer.cpp:    if (I->isCall() || I->isTerminator() || I->hasUnmodeledSideEffects())
./Target/ARC/ARCOptAddrMode.cpp:        MI->hasUnmodeledSideEffects())
./Target/ARC/ARCOptAddrMode.cpp:        MI->hasUnmodeledSideEffects())
./Target/RISCV/RISCVInstrInfo.cpp:  if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
./Target/RISCV/RISCVISelLowering.cpp:      if (SequenceMBBI->hasUnmodeledSideEffects() ||
./Target/SystemZ/SystemZElimCompare.cpp:        (MI.isCall() || MI.hasUnmodeledSideEffects()))
./Target/AArch64/AArch64InstrInfo.cpp:  if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
./Target/Sparc/DelaySlotFiller.cpp:    if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
./CodeGen/MachineInstr.cpp:      mayRaiseFPException() || hasUnmodeledSideEffects())
./CodeGen/MachineInstr.cpp:      !hasUnmodeledSideEffects())
./CodeGen/MachineInstr.cpp:bool MachineInstr::hasUnmodeledSideEffects() const {
./CodeGen/MachineInstr.cpp:  return mayStore() || isCall() || hasUnmodeledSideEffects();
./CodeGen/PeepholeOptimizer.cpp:      if (MI->isInlineAsm() || MI->hasUnmodeledSideEffects()) {
./CodeGen/PeepholeOptimizer.cpp:  if (Def->mayRaiseFPException() || Def->hasUnmodeledSideEffects())
./CodeGen/ScheduleDAGInstrs.cpp:  return MI->isCall() || MI->hasUnmodeledSideEffects() ||
./CodeGen/MachineCSE.cpp:      MI->mayRaiseFPException() || MI->hasUnmodeledSideEffects())
./CodeGen/TargetInstrInfo.cpp:      MI.hasUnmodeledSideEffects())
./CodeGen/MachinePipeliner.cpp:         MI.hasUnmodeledSideEffects() ||
./CodeGen/MachinePipeliner.cpp:  if (SI->hasUnmodeledSideEffects() || DI->hasUnmodeledSideEffects() ||
./CodeGen/LiveRangeShrink.cpp:        if (MI.hasUnmodeledSideEffects() && Next != MBB.end()) {
./CodeGen/ImplicitNullChecks.cpp:      MI->hasUnmodeledSideEffects())
./CodeGen/GlobalISel/InstructionSelector.cpp:         !MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();
./CodeGen/MachineLICM.cpp:  if (!MI.mayStore() || MI.hasUnmodeledSideEffects() ||
./CodeGen/TwoAddressInstructionPass.cpp:  if (KillMI->hasUnmodeledSideEffects() || KillMI->isCall() ||
./CodeGen/TwoAddressInstructionPass.cpp:    if (OtherMI.hasUnmodeledSideEffects() || OtherMI.isCall() ||
./CodeGen/TwoAddressInstructionPass.cpp:    if (OtherMI.hasUnmodeledSideEffects() || OtherMI.isCall() ||
./MCA/InstrBuilder.cpp:                        !MCDesc.hasUnmodeledSideEffects();
./MCA/InstrBuilder.cpp:                        !MCDesc.hasUnmodeledSideEffects();
./MCA/InstrBuilder.cpp:  ID->HasSideEffects = MCDesc.hasUnmodeledSideEffects();
jsji added a comment.EditedJan 5 2020, 7:05 PM

Yes, in theory, it might affect scheduling and other opts as well.
Can we please prove it by providing a test case that has changes due to this patch. Thanks.

jsji requested changes to this revision.Jul 17 2020, 11:59 AM

Set it back , please update with testcases.

This revision now requires changes to proceed.Jul 17 2020, 11:59 AM
qiucf commandeered this revision.Sep 9 2020, 11:32 PM
qiucf added a reviewer: ZhangKang.
qiucf planned changes to this revision.Dec 2 2020, 1:17 AM
qiucf updated this revision to Diff 368043.Aug 23 2021, 12:52 AM
qiucf edited the summary of this revision. (Show Details)

Provide a test case. (not pre-commited)

shchenz accepted this revision.Aug 26 2021, 12:51 AM

LGTM.

The sinking in the test after unset hasSideEffects is right and profitable.

jsji accepted this revision.Aug 26 2021, 7:24 AM

LGTM. But maybe we should try to see whether we can set hasSideEffects = 0 as default on PowerPC as well when there are no match patterns?

This revision is now accepted and ready to land.Aug 26 2021, 7:24 AM
This revision was landed with ongoing or failed builds.Aug 29 2021, 9:23 PM
This revision was automatically updated to reflect the committed changes.
qiucf added a comment.Aug 29 2021, 9:23 PM

LGTM. But maybe we should try to see whether we can set hasSideEffects = 0 as default on PowerPC as well when there are no match patterns?

Yes. Will clean them up in a future patch.