2013-08-05 18:58:53 +08:00
|
|
|
//===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-05 18:58:53 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass:
|
|
|
|
// (1) tries to remove compares if CC already contains the required information
|
|
|
|
// (2) fuses compares and branches into COMPARE AND BRANCH instructions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "SystemZ.h"
|
|
|
|
#include "SystemZInstrInfo.h"
|
2013-08-05 18:58:53 +08:00
|
|
|
#include "SystemZTargetMachine.h"
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2013-08-05 18:58:53 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-11-04 23:11:12 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2013-08-05 18:58:53 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2013-08-05 18:58:53 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-01-25 06:10:43 +08:00
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2013-08-05 18:58:53 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "systemz-elim-compare"
|
|
|
|
|
2013-08-05 19:23:46 +08:00
|
|
|
STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
|
2016-11-28 21:59:22 +08:00
|
|
|
STATISTIC(LoadAndTraps, "Number of load-and-trap instructions");
|
2013-08-05 18:58:53 +08:00
|
|
|
STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
|
|
|
|
STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
|
|
|
|
|
|
|
|
namespace {
|
2017-01-25 06:10:43 +08:00
|
|
|
|
2014-03-06 18:38:30 +08:00
|
|
|
// Represents the references to a particular register in one or more
|
|
|
|
// instructions.
|
|
|
|
struct Reference {
|
2017-01-25 06:10:43 +08:00
|
|
|
Reference() = default;
|
2014-03-06 18:38:30 +08:00
|
|
|
|
|
|
|
Reference &operator|=(const Reference &Other) {
|
|
|
|
Def |= Other.Def;
|
|
|
|
Use |= Other.Use;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:00:20 +08:00
|
|
|
explicit operator bool() const { return Def || Use; }
|
2014-03-06 18:38:30 +08:00
|
|
|
|
|
|
|
// True if the register is defined or used in some form, either directly or
|
|
|
|
// via a sub- or super-register.
|
2017-01-25 06:10:43 +08:00
|
|
|
bool Def = false;
|
|
|
|
bool Use = false;
|
2014-03-06 18:38:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class SystemZElimCompare : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2017-01-25 06:10:43 +08:00
|
|
|
|
2014-03-06 18:38:30 +08:00
|
|
|
SystemZElimCompare(const SystemZTargetMachine &tm)
|
2017-01-25 06:10:43 +08:00
|
|
|
: MachineFunctionPass(ID) {}
|
2014-03-06 18:38:30 +08:00
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2014-03-06 18:38:30 +08:00
|
|
|
return "SystemZ Comparison Elimination";
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:00:15 +08:00
|
|
|
bool processBlock(MachineBasicBlock &MBB);
|
2014-04-29 15:58:41 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
2017-01-25 06:10:43 +08:00
|
|
|
|
2016-04-05 01:09:25 +08:00
|
|
|
MachineFunctionProperties getRequiredProperties() const override {
|
|
|
|
return MachineFunctionProperties().set(
|
2016-08-25 09:27:13 +08:00
|
|
|
MachineFunctionProperties::Property::NoVRegs);
|
2016-04-05 01:09:25 +08:00
|
|
|
}
|
2014-03-06 18:38:30 +08:00
|
|
|
|
|
|
|
private:
|
2016-07-12 09:39:01 +08:00
|
|
|
Reference getRegReferences(MachineInstr &MI, unsigned Reg);
|
|
|
|
bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare,
|
2014-03-06 18:38:30 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers);
|
2016-11-28 21:59:22 +08:00
|
|
|
bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers);
|
2018-01-15 23:41:26 +08:00
|
|
|
bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers);
|
2019-12-18 02:14:34 +08:00
|
|
|
bool convertToLogical(MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers);
|
2016-07-12 09:39:01 +08:00
|
|
|
bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
|
2018-01-15 23:41:26 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers,
|
|
|
|
unsigned ConvOpc = 0);
|
2016-07-12 09:39:01 +08:00
|
|
|
bool optimizeCompareZero(MachineInstr &Compare,
|
2014-03-06 18:38:30 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers);
|
2016-07-12 09:39:01 +08:00
|
|
|
bool fuseCompareOperations(MachineInstr &Compare,
|
2016-06-11 03:58:10 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers);
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2017-01-25 06:10:43 +08:00
|
|
|
const SystemZInstrInfo *TII = nullptr;
|
|
|
|
const TargetRegisterInfo *TRI = nullptr;
|
2014-03-06 18:38:30 +08:00
|
|
|
};
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2014-03-06 18:38:30 +08:00
|
|
|
char SystemZElimCompare::ID = 0;
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2017-01-25 06:10:43 +08:00
|
|
|
} // end anonymous namespace
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2017-09-21 21:52:24 +08:00
|
|
|
// Returns true if MI is an instruction whose output equals the value in Reg.
|
|
|
|
static bool preservesValueOf(MachineInstr &MI, unsigned Reg) {
|
2016-07-12 09:39:01 +08:00
|
|
|
switch (MI.getOpcode()) {
|
2013-08-05 19:03:20 +08:00
|
|
|
case SystemZ::LR:
|
|
|
|
case SystemZ::LGR:
|
|
|
|
case SystemZ::LGFR:
|
|
|
|
case SystemZ::LTR:
|
|
|
|
case SystemZ::LTGR:
|
|
|
|
case SystemZ::LTGFR:
|
2013-08-07 19:10:06 +08:00
|
|
|
case SystemZ::LER:
|
|
|
|
case SystemZ::LDR:
|
|
|
|
case SystemZ::LXR:
|
|
|
|
case SystemZ::LTEBR:
|
|
|
|
case SystemZ::LTDBR:
|
|
|
|
case SystemZ::LTXBR:
|
2016-07-12 09:39:01 +08:00
|
|
|
if (MI.getOperand(1).getReg() == Reg)
|
2013-08-05 19:03:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 18:58:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-21 21:52:24 +08:00
|
|
|
// Return true if any CC result of MI would (perhaps after conversion)
|
|
|
|
// reflect the value of Reg.
|
|
|
|
static bool resultTests(MachineInstr &MI, unsigned Reg) {
|
|
|
|
if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() &&
|
|
|
|
MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return (preservesValueOf(MI, Reg));
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:27:44 +08:00
|
|
|
// Describe the references to Reg or any of its aliases in MI.
|
2016-07-12 09:39:01 +08:00
|
|
|
Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
|
2013-08-05 19:23:46 +08:00
|
|
|
Reference Ref;
|
2019-01-23 15:42:26 +08:00
|
|
|
if (MI.isDebugInstr())
|
|
|
|
return Ref;
|
|
|
|
|
2016-07-12 09:39:01 +08:00
|
|
|
for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(I);
|
2013-08-05 19:23:46 +08:00
|
|
|
if (MO.isReg()) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
if (Register MOReg = MO.getReg()) {
|
2015-10-09 19:27:44 +08:00
|
|
|
if (TRI->regsOverlap(MOReg, Reg)) {
|
|
|
|
if (MO.isUse())
|
2013-08-05 19:23:46 +08:00
|
|
|
Ref.Use = true;
|
2015-10-09 19:27:44 +08:00
|
|
|
else if (MO.isDef())
|
2013-08-05 19:23:46 +08:00
|
|
|
Ref.Def = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ref;
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:40:23 +08:00
|
|
|
// Return true if this is a load and test which can be optimized the
|
|
|
|
// same way as compare instruction.
|
2016-07-12 09:39:01 +08:00
|
|
|
static bool isLoadAndTestAsCmp(MachineInstr &MI) {
|
2015-10-08 15:40:23 +08:00
|
|
|
// If we during isel used a load-and-test as a compare with 0, the
|
|
|
|
// def operand is dead.
|
2016-07-12 09:39:01 +08:00
|
|
|
return (MI.getOpcode() == SystemZ::LTEBR ||
|
|
|
|
MI.getOpcode() == SystemZ::LTDBR ||
|
|
|
|
MI.getOpcode() == SystemZ::LTXBR) &&
|
|
|
|
MI.getOperand(0).isDead();
|
2015-10-08 15:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the source register of Compare, which is the unknown value
|
|
|
|
// being tested.
|
2016-07-12 09:39:01 +08:00
|
|
|
static unsigned getCompareSourceReg(MachineInstr &Compare) {
|
2015-10-08 15:40:23 +08:00
|
|
|
unsigned reg = 0;
|
2016-07-12 09:39:01 +08:00
|
|
|
if (Compare.isCompare())
|
|
|
|
reg = Compare.getOperand(0).getReg();
|
2015-10-08 15:40:23 +08:00
|
|
|
else if (isLoadAndTestAsCmp(Compare))
|
2016-07-12 09:39:01 +08:00
|
|
|
reg = Compare.getOperand(1).getReg();
|
2017-01-25 06:10:43 +08:00
|
|
|
assert(reg);
|
2015-10-08 15:40:23 +08:00
|
|
|
|
|
|
|
return reg;
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:23:46 +08:00
|
|
|
// Compare compares the result of MI against zero. If MI is an addition
|
|
|
|
// of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
|
2016-11-28 21:40:08 +08:00
|
|
|
// and convert the branch to a BRCT(G) or BRCTH. Return true on success.
|
2016-07-12 09:39:01 +08:00
|
|
|
bool SystemZElimCompare::convertToBRCT(
|
|
|
|
MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers) {
|
2013-08-05 19:23:46 +08:00
|
|
|
// Check whether we have an addition of -1.
|
2016-07-12 09:39:01 +08:00
|
|
|
unsigned Opcode = MI.getOpcode();
|
2013-08-05 19:23:46 +08:00
|
|
|
unsigned BRCT;
|
|
|
|
if (Opcode == SystemZ::AHI)
|
|
|
|
BRCT = SystemZ::BRCT;
|
|
|
|
else if (Opcode == SystemZ::AGHI)
|
|
|
|
BRCT = SystemZ::BRCTG;
|
2016-11-28 21:40:08 +08:00
|
|
|
else if (Opcode == SystemZ::AIH)
|
|
|
|
BRCT = SystemZ::BRCTH;
|
2013-08-05 19:23:46 +08:00
|
|
|
else
|
|
|
|
return false;
|
2016-07-12 09:39:01 +08:00
|
|
|
if (MI.getOperand(2).getImm() != -1)
|
2013-08-05 19:23:46 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check whether we have a single JLH.
|
|
|
|
if (CCUsers.size() != 1)
|
|
|
|
return false;
|
|
|
|
MachineInstr *Branch = CCUsers[0];
|
|
|
|
if (Branch->getOpcode() != SystemZ::BRC ||
|
|
|
|
Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
|
|
|
|
Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We already know that there are no references to the register between
|
|
|
|
// MI and Compare. Make sure that there are also no references between
|
|
|
|
// Compare and Branch.
|
2015-10-08 15:40:23 +08:00
|
|
|
unsigned SrcReg = getCompareSourceReg(Compare);
|
2013-08-05 19:23:46 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
|
|
|
|
for (++MBBI; MBBI != MBBE; ++MBBI)
|
2016-07-12 09:39:01 +08:00
|
|
|
if (getRegReferences(*MBBI, SrcReg))
|
2013-08-05 19:23:46 +08:00
|
|
|
return false;
|
|
|
|
|
2016-11-28 21:40:08 +08:00
|
|
|
// The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH.
|
2013-08-05 19:23:46 +08:00
|
|
|
MachineOperand Target(Branch->getOperand(2));
|
2015-10-10 15:14:24 +08:00
|
|
|
while (Branch->getNumOperands())
|
|
|
|
Branch->RemoveOperand(0);
|
2013-08-05 19:23:46 +08:00
|
|
|
Branch->setDesc(TII->get(BRCT));
|
2016-11-28 21:40:08 +08:00
|
|
|
MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
|
2017-01-13 17:58:52 +08:00
|
|
|
MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target);
|
2016-11-28 21:40:08 +08:00
|
|
|
// Add a CC def to BRCT(G), since we may have to split them again if the
|
|
|
|
// branch displacement overflows. BRCTH has a 32-bit displacement, so
|
|
|
|
// this is not necessary there.
|
|
|
|
if (BRCT != SystemZ::BRCTH)
|
|
|
|
MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
|
2016-07-12 09:39:01 +08:00
|
|
|
MI.eraseFromParent();
|
2013-08-05 19:23:46 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:59:22 +08:00
|
|
|
// Compare compares the result of MI against zero. If MI is a suitable load
|
|
|
|
// instruction and if CCUsers is a single conditional trap on zero, eliminate
|
|
|
|
// the load and convert the branch to a load-and-trap. Return true on success.
|
|
|
|
bool SystemZElimCompare::convertToLoadAndTrap(
|
|
|
|
MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers) {
|
|
|
|
unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode());
|
|
|
|
if (!LATOpcode)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check whether we have a single CondTrap that traps on zero.
|
|
|
|
if (CCUsers.size() != 1)
|
|
|
|
return false;
|
|
|
|
MachineInstr *Branch = CCUsers[0];
|
|
|
|
if (Branch->getOpcode() != SystemZ::CondTrap ||
|
|
|
|
Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
|
|
|
|
Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// We already know that there are no references to the register between
|
|
|
|
// MI and Compare. Make sure that there are also no references between
|
|
|
|
// Compare and Branch.
|
|
|
|
unsigned SrcReg = getCompareSourceReg(Compare);
|
|
|
|
MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
|
|
|
|
for (++MBBI; MBBI != MBBE; ++MBBI)
|
|
|
|
if (getRegReferences(*MBBI, SrcReg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The transformation is OK. Rebuild Branch as a load-and-trap.
|
|
|
|
while (Branch->getNumOperands())
|
|
|
|
Branch->RemoveOperand(0);
|
|
|
|
Branch->setDesc(TII->get(LATOpcode));
|
|
|
|
MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
|
2017-01-13 17:58:52 +08:00
|
|
|
.add(MI.getOperand(0))
|
|
|
|
.add(MI.getOperand(1))
|
|
|
|
.add(MI.getOperand(2))
|
|
|
|
.add(MI.getOperand(3));
|
2016-11-28 21:59:22 +08:00
|
|
|
MI.eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-05 19:03:20 +08:00
|
|
|
// If MI is a load instruction, try to convert it into a LOAD AND TEST.
|
|
|
|
// Return true on success.
|
2018-01-15 23:41:26 +08:00
|
|
|
bool SystemZElimCompare::convertToLoadAndTest(
|
|
|
|
MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers) {
|
|
|
|
|
|
|
|
// Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI.
|
2016-07-12 09:39:01 +08:00
|
|
|
unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
|
2018-01-15 23:41:26 +08:00
|
|
|
if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode))
|
2013-08-05 19:03:20 +08:00
|
|
|
return false;
|
|
|
|
|
2018-06-07 13:59:07 +08:00
|
|
|
// Rebuild to get the CC operand in the right place.
|
2018-08-17 05:30:05 +08:00
|
|
|
auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode));
|
2018-06-07 13:59:07 +08:00
|
|
|
for (const auto &MO : MI.operands())
|
2018-08-17 05:30:05 +08:00
|
|
|
MIB.add(MO);
|
|
|
|
MIB.setMemRefs(MI.memoperands());
|
2018-06-07 13:59:07 +08:00
|
|
|
MI.eraseFromParent();
|
|
|
|
|
2020-01-10 22:31:10 +08:00
|
|
|
// Mark instruction as not raising an FP exception if applicable. We already
|
[FPEnv] Constrained FCmp intrinsics
This adds support for constrained floating-point comparison intrinsics.
Specifically, we add:
declare <ty2>
@llvm.experimental.constrained.fcmp(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
declare <ty2>
@llvm.experimental.constrained.fcmps(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
The first variant implements an IEEE "quiet" comparison (i.e. we only
get an invalid FP exception if either argument is a SNaN), while the
second variant implements an IEEE "signaling" comparison (i.e. we get
an invalid FP exception if either argument is any NaN).
The condition code is implemented as a metadata string. The same set
of predicates as for the fcmp instruction is supported (except for the
"true" and "false" predicates).
These new intrinsics are mapped by SelectionDAG codegen onto two new
ISD opcodes, ISD::STRICT_FSETCC and ISD::STRICT_FSETCCS, again
representing quiet vs. signaling comparison operations. Otherwise
those nodes look like SETCC nodes, with an additional chain argument
and result as usual for strict FP nodes. The patch includes support
for the common legalization operations for those nodes.
The patch also includes full SystemZ back-end support for the new
ISD nodes, mapping them to all available SystemZ instruction to
fully implement strict semantics (scalar and vector).
Differential Revision: https://reviews.llvm.org/D69281
2019-12-06 18:30:04 +08:00
|
|
|
// verified earlier that this move is valid.
|
2020-01-10 22:31:10 +08:00
|
|
|
if (!Compare.mayRaiseFPException())
|
|
|
|
MIB.setMIFlag(MachineInstr::MIFlag::NoFPExcept);
|
[FPEnv] Constrained FCmp intrinsics
This adds support for constrained floating-point comparison intrinsics.
Specifically, we add:
declare <ty2>
@llvm.experimental.constrained.fcmp(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
declare <ty2>
@llvm.experimental.constrained.fcmps(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
The first variant implements an IEEE "quiet" comparison (i.e. we only
get an invalid FP exception if either argument is a SNaN), while the
second variant implements an IEEE "signaling" comparison (i.e. we get
an invalid FP exception if either argument is any NaN).
The condition code is implemented as a metadata string. The same set
of predicates as for the fcmp instruction is supported (except for the
"true" and "false" predicates).
These new intrinsics are mapped by SelectionDAG codegen onto two new
ISD opcodes, ISD::STRICT_FSETCC and ISD::STRICT_FSETCCS, again
representing quiet vs. signaling comparison operations. Otherwise
those nodes look like SETCC nodes, with an additional chain argument
and result as usual for strict FP nodes. The patch includes support
for the common legalization operations for those nodes.
The patch also includes full SystemZ back-end support for the new
ISD nodes, mapping them to all available SystemZ instruction to
fully implement strict semantics (scalar and vector).
Differential Revision: https://reviews.llvm.org/D69281
2019-12-06 18:30:04 +08:00
|
|
|
|
2013-08-05 19:03:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-18 02:14:34 +08:00
|
|
|
// See if MI is an instruction with an equivalent "logical" opcode that can
|
|
|
|
// be used and replace MI. This is useful for EQ/NE comparisons where the
|
|
|
|
// "nsw" flag is missing since the "logical" opcode always sets CC to reflect
|
|
|
|
// the result being zero or non-zero.
|
|
|
|
bool SystemZElimCompare::convertToLogical(
|
|
|
|
MachineInstr &MI, MachineInstr &Compare,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers) {
|
|
|
|
|
|
|
|
unsigned ConvOpc = 0;
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
case SystemZ::AR: ConvOpc = SystemZ::ALR; break;
|
|
|
|
case SystemZ::ARK: ConvOpc = SystemZ::ALRK; break;
|
|
|
|
case SystemZ::AGR: ConvOpc = SystemZ::ALGR; break;
|
|
|
|
case SystemZ::AGRK: ConvOpc = SystemZ::ALGRK; break;
|
|
|
|
case SystemZ::A: ConvOpc = SystemZ::AL; break;
|
|
|
|
case SystemZ::AY: ConvOpc = SystemZ::ALY; break;
|
|
|
|
case SystemZ::AG: ConvOpc = SystemZ::ALG; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
if (!ConvOpc || !adjustCCMasksForInstr(MI, Compare, CCUsers, ConvOpc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Operands should be identical, so just change the opcode and remove the
|
|
|
|
// dead flag on CC.
|
|
|
|
MI.setDesc(TII->get(ConvOpc));
|
|
|
|
MI.clearRegisterDeads(SystemZ::CC);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
static bool isAddWithImmediate(unsigned Opcode) {
|
|
|
|
switch(Opcode) {
|
|
|
|
case SystemZ::AHI:
|
|
|
|
case SystemZ::AHIK:
|
|
|
|
case SystemZ::AGHI:
|
|
|
|
case SystemZ::AGHIK:
|
|
|
|
case SystemZ::AFI:
|
|
|
|
case SystemZ::AIH:
|
|
|
|
case SystemZ::AGFI:
|
|
|
|
return true;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-05 18:58:53 +08:00
|
|
|
// The CC users in CCUsers are testing the result of a comparison of some
|
2018-01-15 23:41:26 +08:00
|
|
|
// value X against zero and we know that any CC value produced by MI would
|
|
|
|
// also reflect the value of X. ConvOpc may be used to pass the transfomed
|
|
|
|
// opcode MI will have if this succeeds. Try to adjust CCUsers so that they
|
|
|
|
// test the result of MI directly, returning true on success. Leave
|
|
|
|
// everything unchanged on failure.
|
2016-07-12 09:39:01 +08:00
|
|
|
bool SystemZElimCompare::adjustCCMasksForInstr(
|
|
|
|
MachineInstr &MI, MachineInstr &Compare,
|
2018-01-15 23:41:26 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &CCUsers,
|
|
|
|
unsigned ConvOpc) {
|
2019-12-18 02:14:34 +08:00
|
|
|
unsigned CompareFlags = Compare.getDesc().TSFlags;
|
|
|
|
unsigned CompareCCValues = SystemZII::getCCValues(CompareFlags);
|
2018-01-15 23:41:26 +08:00
|
|
|
int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode());
|
2013-08-05 18:58:53 +08:00
|
|
|
const MCInstrDesc &Desc = TII->get(Opcode);
|
|
|
|
unsigned MIFlags = Desc.TSFlags;
|
|
|
|
|
[FPEnv] Constrained FCmp intrinsics
This adds support for constrained floating-point comparison intrinsics.
Specifically, we add:
declare <ty2>
@llvm.experimental.constrained.fcmp(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
declare <ty2>
@llvm.experimental.constrained.fcmps(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
The first variant implements an IEEE "quiet" comparison (i.e. we only
get an invalid FP exception if either argument is a SNaN), while the
second variant implements an IEEE "signaling" comparison (i.e. we get
an invalid FP exception if either argument is any NaN).
The condition code is implemented as a metadata string. The same set
of predicates as for the fcmp instruction is supported (except for the
"true" and "false" predicates).
These new intrinsics are mapped by SelectionDAG codegen onto two new
ISD opcodes, ISD::STRICT_FSETCC and ISD::STRICT_FSETCCS, again
representing quiet vs. signaling comparison operations. Otherwise
those nodes look like SETCC nodes, with an additional chain argument
and result as usual for strict FP nodes. The patch includes support
for the common legalization operations for those nodes.
The patch also includes full SystemZ back-end support for the new
ISD nodes, mapping them to all available SystemZ instruction to
fully implement strict semantics (scalar and vector).
Differential Revision: https://reviews.llvm.org/D69281
2019-12-06 18:30:04 +08:00
|
|
|
// If Compare may raise an FP exception, we can only eliminate it
|
|
|
|
// if MI itself would have already raised the exception.
|
|
|
|
if (Compare.mayRaiseFPException()) {
|
|
|
|
// If the caller will change MI to use ConvOpc, only test whether
|
|
|
|
// ConvOpc is suitable; it is on the caller to set the MI flag.
|
|
|
|
if (ConvOpc && !Desc.mayRaiseFPException())
|
|
|
|
return false;
|
|
|
|
// If the caller will not change MI, we test the MI flag here.
|
|
|
|
if (!ConvOpc && !MI.mayRaiseFPException())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-05 18:58:53 +08:00
|
|
|
// See which compare-style condition codes are available.
|
2019-12-18 02:14:34 +08:00
|
|
|
unsigned CCValues = SystemZII::getCCValues(MIFlags);
|
|
|
|
unsigned ReusableCCMask = CCValues;
|
2013-08-05 18:58:53 +08:00
|
|
|
// For unsigned comparisons with zero, only equality makes sense.
|
2013-08-07 19:10:06 +08:00
|
|
|
if (CompareFlags & SystemZII::IsLogical)
|
|
|
|
ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
|
2019-12-18 02:14:34 +08:00
|
|
|
unsigned OFImplies = 0;
|
|
|
|
bool LogicalMI = false;
|
|
|
|
bool MIEquivalentToCmp = false;
|
|
|
|
if (MI.getFlag(MachineInstr::NoSWrap) &&
|
|
|
|
(MIFlags & SystemZII::CCIfNoSignedWrap)) {
|
|
|
|
// If MI has the NSW flag set in combination with the
|
|
|
|
// SystemZII::CCIfNoSignedWrap flag, all CCValues are valid.
|
|
|
|
}
|
|
|
|
else if ((MIFlags & SystemZII::CCIfNoSignedWrap) &&
|
|
|
|
MI.getOperand(2).isImm()) {
|
|
|
|
// Signed addition of immediate. If adding a positive immediate
|
|
|
|
// overflows, the result must be less than zero. If adding a negative
|
|
|
|
// immediate overflows, the result must be larger than zero (except in
|
|
|
|
// the special case of adding the minimum value of the result range, in
|
|
|
|
// which case we cannot predict whether the result is larger than or
|
|
|
|
// equal to zero).
|
|
|
|
assert(isAddWithImmediate(Opcode) && "Expected an add with immediate.");
|
|
|
|
assert(!MI.mayLoadOrStore() && "Expected an immediate term.");
|
|
|
|
int64_t RHS = MI.getOperand(2).getImm();
|
|
|
|
if (SystemZ::GRX32BitRegClass.contains(MI.getOperand(0).getReg()) &&
|
|
|
|
RHS == INT32_MIN)
|
|
|
|
return false;
|
|
|
|
OFImplies = (RHS > 0 ? SystemZ::CCMASK_CMP_LT : SystemZ::CCMASK_CMP_GT);
|
|
|
|
}
|
|
|
|
else if ((MIFlags & SystemZII::IsLogical) && CCValues) {
|
|
|
|
// Use CCMASK_CMP_EQ to match with CCUsers. On success CCMask:s will be
|
|
|
|
// converted to CCMASK_LOGICAL_ZERO or CCMASK_LOGICAL_NONZERO.
|
|
|
|
LogicalMI = true;
|
|
|
|
ReusableCCMask = SystemZ::CCMASK_CMP_EQ;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ReusableCCMask &= SystemZII::getCompareZeroCCMask(MIFlags);
|
|
|
|
assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
|
|
|
|
MIEquivalentToCmp =
|
|
|
|
ReusableCCMask == CCValues && CCValues == CompareCCValues;
|
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
if (ReusableCCMask == 0)
|
|
|
|
return false;
|
|
|
|
|
2018-01-15 23:41:26 +08:00
|
|
|
if (!MIEquivalentToCmp) {
|
|
|
|
// Now check whether these flags are enough for all users.
|
|
|
|
SmallVector<MachineOperand *, 4> AlterMasks;
|
|
|
|
for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
|
2019-12-18 02:14:34 +08:00
|
|
|
MachineInstr *CCUserMI = CCUsers[I];
|
2018-01-15 23:41:26 +08:00
|
|
|
|
|
|
|
// Fail if this isn't a use of CC that we understand.
|
2019-12-18 02:14:34 +08:00
|
|
|
unsigned Flags = CCUserMI->getDesc().TSFlags;
|
2018-01-15 23:41:26 +08:00
|
|
|
unsigned FirstOpNum;
|
|
|
|
if (Flags & SystemZII::CCMaskFirst)
|
|
|
|
FirstOpNum = 0;
|
|
|
|
else if (Flags & SystemZII::CCMaskLast)
|
2019-12-18 02:14:34 +08:00
|
|
|
FirstOpNum = CCUserMI->getNumExplicitOperands() - 2;
|
2018-01-15 23:41:26 +08:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check whether the instruction predicate treats all CC values
|
|
|
|
// outside of ReusableCCMask in the same way. In that case it
|
|
|
|
// doesn't matter what those CC values mean.
|
2019-12-18 02:14:34 +08:00
|
|
|
unsigned CCValid = CCUserMI->getOperand(FirstOpNum).getImm();
|
|
|
|
unsigned CCMask = CCUserMI->getOperand(FirstOpNum + 1).getImm();
|
|
|
|
assert(CCValid == CompareCCValues && (CCMask & ~CCValid) == 0 &&
|
|
|
|
"Corrupt CC operands of CCUser.");
|
2018-01-15 23:41:26 +08:00
|
|
|
unsigned OutValid = ~ReusableCCMask & CCValid;
|
|
|
|
unsigned OutMask = ~ReusableCCMask & CCMask;
|
|
|
|
if (OutMask != 0 && OutMask != OutValid)
|
|
|
|
return false;
|
|
|
|
|
2019-12-18 02:14:34 +08:00
|
|
|
AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum));
|
|
|
|
AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum + 1));
|
2018-01-15 23:41:26 +08:00
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2018-01-15 23:41:26 +08:00
|
|
|
// All users are OK. Adjust the masks for MI.
|
|
|
|
for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
|
|
|
|
AlterMasks[I]->setImm(CCValues);
|
|
|
|
unsigned CCMask = AlterMasks[I + 1]->getImm();
|
2019-12-18 02:14:34 +08:00
|
|
|
if (LogicalMI) {
|
|
|
|
// Translate the CCMask into its "logical" value.
|
|
|
|
CCMask = (CCMask == SystemZ::CCMASK_CMP_EQ ?
|
|
|
|
SystemZ::CCMASK_LOGICAL_ZERO : SystemZ::CCMASK_LOGICAL_NONZERO);
|
|
|
|
CCMask &= CCValues; // Logical subtracts never set CC=0.
|
|
|
|
} else {
|
|
|
|
if (CCMask & ~ReusableCCMask)
|
|
|
|
CCMask = (CCMask & ReusableCCMask) | (CCValues & ~ReusableCCMask);
|
|
|
|
CCMask |= (CCMask & OFImplies) ? SystemZ::CCMASK_ARITH_OVERFLOW : 0;
|
|
|
|
}
|
|
|
|
AlterMasks[I + 1]->setImm(CCMask);
|
2018-01-15 23:41:26 +08:00
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 23:41:26 +08:00
|
|
|
// CC is now live after MI.
|
2019-09-09 15:58:57 +08:00
|
|
|
if (!ConvOpc)
|
|
|
|
MI.clearRegisterDeads(SystemZ::CC);
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2018-01-15 23:41:26 +08:00
|
|
|
// Check if MI lies before Compare.
|
|
|
|
bool BeforeCmp = false;
|
|
|
|
MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end();
|
|
|
|
for (++MBBI; MBBI != MBBE; ++MBBI)
|
|
|
|
if (MBBI == Compare) {
|
|
|
|
BeforeCmp = true;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
|
|
|
|
// Clear any intervening kills of CC.
|
2018-01-15 23:41:26 +08:00
|
|
|
if (BeforeCmp) {
|
|
|
|
MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
|
|
|
|
for (++MBBI; MBBI != MBBE; ++MBBI)
|
|
|
|
MBBI->clearRegisterKills(SystemZ::CC, TRI);
|
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-07 19:10:06 +08:00
|
|
|
// Return true if Compare is a comparison against zero.
|
2016-07-12 09:39:01 +08:00
|
|
|
static bool isCompareZero(MachineInstr &Compare) {
|
|
|
|
switch (Compare.getOpcode()) {
|
2013-08-07 19:10:06 +08:00
|
|
|
case SystemZ::LTEBRCompare:
|
|
|
|
case SystemZ::LTDBRCompare:
|
|
|
|
case SystemZ::LTXBRCompare:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
2015-10-08 15:40:23 +08:00
|
|
|
if (isLoadAndTestAsCmp(Compare))
|
|
|
|
return true;
|
2016-07-12 09:39:01 +08:00
|
|
|
return Compare.getNumExplicitOperands() == 2 &&
|
|
|
|
Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
|
2013-08-07 19:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 18:58:53 +08:00
|
|
|
// Try to optimize cases where comparison instruction Compare is testing
|
|
|
|
// a value against zero. Return true on success and if Compare should be
|
|
|
|
// deleted as dead. CCUsers is the list of instructions that use the CC
|
|
|
|
// value produced by Compare.
|
2016-07-12 09:39:01 +08:00
|
|
|
bool SystemZElimCompare::optimizeCompareZero(
|
|
|
|
MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
|
2013-08-07 19:10:06 +08:00
|
|
|
if (!isCompareZero(Compare))
|
2013-08-05 18:58:53 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Search back for CC results that are based on the first operand.
|
2015-10-08 15:40:23 +08:00
|
|
|
unsigned SrcReg = getCompareSourceReg(Compare);
|
2016-07-12 09:39:01 +08:00
|
|
|
MachineBasicBlock &MBB = *Compare.getParent();
|
2013-08-05 19:23:46 +08:00
|
|
|
Reference CCRefs;
|
|
|
|
Reference SrcRefs;
|
2018-06-07 13:59:07 +08:00
|
|
|
for (MachineBasicBlock::reverse_iterator MBBI =
|
|
|
|
std::next(MachineBasicBlock::reverse_iterator(&Compare)),
|
|
|
|
MBBE = MBB.rend(); MBBI != MBBE;) {
|
|
|
|
MachineInstr &MI = *MBBI++;
|
2015-10-08 15:40:11 +08:00
|
|
|
if (resultTests(MI, SrcReg)) {
|
2013-08-05 19:23:46 +08:00
|
|
|
// Try to remove both MI and Compare by converting a branch to BRCT(G).
|
2016-11-28 21:59:22 +08:00
|
|
|
// or a load-and-trap instruction. We don't care in this case whether
|
|
|
|
// CC is modified between MI and Compare.
|
|
|
|
if (!CCRefs.Use && !SrcRefs) {
|
|
|
|
if (convertToBRCT(MI, Compare, CCUsers)) {
|
|
|
|
BranchOnCounts += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
|
|
|
|
LoadAndTraps += 1;
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-05 19:23:46 +08:00
|
|
|
}
|
|
|
|
// Try to eliminate Compare by reusing a CC result from MI.
|
2018-01-15 23:41:26 +08:00
|
|
|
if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) ||
|
2019-12-18 02:14:34 +08:00
|
|
|
(!CCRefs.Def &&
|
|
|
|
(adjustCCMasksForInstr(MI, Compare, CCUsers) ||
|
|
|
|
convertToLogical(MI, Compare, CCUsers)))) {
|
2013-08-05 19:23:46 +08:00
|
|
|
EliminatedComparisons += 1;
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
}
|
2013-08-05 19:23:46 +08:00
|
|
|
SrcRefs |= getRegReferences(MI, SrcReg);
|
|
|
|
if (SrcRefs.Def)
|
2017-09-21 21:52:24 +08:00
|
|
|
break;
|
2013-08-05 19:23:46 +08:00
|
|
|
CCRefs |= getRegReferences(MI, SystemZ::CC);
|
|
|
|
if (CCRefs.Use && CCRefs.Def)
|
2017-09-21 21:52:24 +08:00
|
|
|
break;
|
[FPEnv] Constrained FCmp intrinsics
This adds support for constrained floating-point comparison intrinsics.
Specifically, we add:
declare <ty2>
@llvm.experimental.constrained.fcmp(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
declare <ty2>
@llvm.experimental.constrained.fcmps(<type> <op1>, <type> <op2>,
metadata <condition code>,
metadata <exception behavior>)
The first variant implements an IEEE "quiet" comparison (i.e. we only
get an invalid FP exception if either argument is a SNaN), while the
second variant implements an IEEE "signaling" comparison (i.e. we get
an invalid FP exception if either argument is any NaN).
The condition code is implemented as a metadata string. The same set
of predicates as for the fcmp instruction is supported (except for the
"true" and "false" predicates).
These new intrinsics are mapped by SelectionDAG codegen onto two new
ISD opcodes, ISD::STRICT_FSETCC and ISD::STRICT_FSETCCS, again
representing quiet vs. signaling comparison operations. Otherwise
those nodes look like SETCC nodes, with an additional chain argument
and result as usual for strict FP nodes. The patch includes support
for the common legalization operations for those nodes.
The patch also includes full SystemZ back-end support for the new
ISD nodes, mapping them to all available SystemZ instruction to
fully implement strict semantics (scalar and vector).
Differential Revision: https://reviews.llvm.org/D69281
2019-12-06 18:30:04 +08:00
|
|
|
// Eliminating a Compare that may raise an FP exception will move
|
|
|
|
// raising the exception to some earlier MI. We cannot do this if
|
|
|
|
// there is anything in between that might change exception flags.
|
|
|
|
if (Compare.mayRaiseFPException() &&
|
|
|
|
(MI.isCall() || MI.hasUnmodeledSideEffects()))
|
|
|
|
break;
|
2017-09-21 21:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also do a forward search to handle cases where an instruction after the
|
2018-01-08 20:52:40 +08:00
|
|
|
// compare can be converted, like
|
|
|
|
// LTEBRCompare %f0s, %f0s; %f2s = LER %f0s => LTEBRCompare %f2s, %f0s
|
2018-06-07 13:59:07 +08:00
|
|
|
for (MachineBasicBlock::iterator MBBI =
|
|
|
|
std::next(MachineBasicBlock::iterator(&Compare)), MBBE = MBB.end();
|
|
|
|
MBBI != MBBE;) {
|
|
|
|
MachineInstr &MI = *MBBI++;
|
2017-09-21 21:52:24 +08:00
|
|
|
if (preservesValueOf(MI, SrcReg)) {
|
|
|
|
// Try to eliminate Compare by reusing a CC result from MI.
|
2018-01-15 23:41:26 +08:00
|
|
|
if (convertToLoadAndTest(MI, Compare, CCUsers)) {
|
2017-09-21 21:52:24 +08:00
|
|
|
EliminatedComparisons += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getRegReferences(MI, SrcReg).Def)
|
|
|
|
return false;
|
|
|
|
if (getRegReferences(MI, SystemZ::CC))
|
2013-08-05 18:58:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-21 21:52:24 +08:00
|
|
|
|
2013-08-05 18:58:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fuse comparison instruction Compare into a later branch.
|
|
|
|
// Return true on success and if Compare is therefore redundant.
|
2016-07-12 09:39:01 +08:00
|
|
|
bool SystemZElimCompare::fuseCompareOperations(
|
|
|
|
MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
|
2013-08-05 18:58:53 +08:00
|
|
|
// See whether we have a single branch with which to fuse.
|
|
|
|
if (CCUsers.size() != 1)
|
|
|
|
return false;
|
|
|
|
MachineInstr *Branch = CCUsers[0];
|
2016-06-11 03:58:10 +08:00
|
|
|
SystemZII::FusedCompareType Type;
|
2016-04-08 00:11:44 +08:00
|
|
|
switch (Branch->getOpcode()) {
|
|
|
|
case SystemZ::BRC:
|
|
|
|
Type = SystemZII::CompareAndBranch;
|
|
|
|
break;
|
|
|
|
case SystemZ::CondReturn:
|
|
|
|
Type = SystemZII::CompareAndReturn;
|
|
|
|
break;
|
2016-04-11 20:12:32 +08:00
|
|
|
case SystemZ::CallBCR:
|
|
|
|
Type = SystemZII::CompareAndSibcall;
|
|
|
|
break;
|
2016-06-11 03:58:10 +08:00
|
|
|
case SystemZ::CondTrap:
|
|
|
|
Type = SystemZII::CompareAndTrap;
|
|
|
|
break;
|
2016-04-08 00:11:44 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See whether we have a comparison that can be fused.
|
2016-07-12 09:39:01 +08:00
|
|
|
unsigned FusedOpcode =
|
|
|
|
TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
|
2016-04-08 00:11:44 +08:00
|
|
|
if (!FusedOpcode)
|
2013-08-05 18:58:53 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure that the operands are available at the branch.
|
2016-11-11 20:48:26 +08:00
|
|
|
// SrcReg2 is the register if the source operand is a register,
|
|
|
|
// 0 if the source operand is immediate, and the base register
|
|
|
|
// if the source operand is memory (index is not supported).
|
2019-06-24 23:50:29 +08:00
|
|
|
Register SrcReg = Compare.getOperand(0).getReg();
|
|
|
|
Register SrcReg2 =
|
|
|
|
Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : Register();
|
2013-08-05 18:58:53 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
|
|
|
|
for (++MBBI; MBBI != MBBE; ++MBBI)
|
|
|
|
if (MBBI->modifiesRegister(SrcReg, TRI) ||
|
|
|
|
(SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
|
|
|
|
return false;
|
|
|
|
|
2016-04-11 20:12:32 +08:00
|
|
|
// Read the branch mask, target (if applicable), regmask (if applicable).
|
2013-08-05 18:58:53 +08:00
|
|
|
MachineOperand CCMask(MBBI->getOperand(1));
|
|
|
|
assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
|
|
|
|
"Invalid condition-code mask for integer comparison");
|
2016-04-08 00:11:44 +08:00
|
|
|
// This is only valid for CompareAndBranch.
|
|
|
|
MachineOperand Target(MBBI->getOperand(
|
|
|
|
Type == SystemZII::CompareAndBranch ? 2 : 0));
|
2016-04-11 20:12:32 +08:00
|
|
|
const uint32_t *RegMask;
|
|
|
|
if (Type == SystemZII::CompareAndSibcall)
|
|
|
|
RegMask = MBBI->getOperand(2).getRegMask();
|
2013-08-05 18:58:53 +08:00
|
|
|
|
|
|
|
// Clear out all current operands.
|
|
|
|
int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
|
2016-04-08 00:11:44 +08:00
|
|
|
assert(CCUse >= 0 && "BRC/BCR must use CC");
|
2013-08-05 18:58:53 +08:00
|
|
|
Branch->RemoveOperand(CCUse);
|
2016-04-11 20:12:32 +08:00
|
|
|
// Remove target (branch) or regmask (sibcall).
|
|
|
|
if (Type == SystemZII::CompareAndBranch ||
|
|
|
|
Type == SystemZII::CompareAndSibcall)
|
2016-04-08 00:11:44 +08:00
|
|
|
Branch->RemoveOperand(2);
|
2013-08-05 18:58:53 +08:00
|
|
|
Branch->RemoveOperand(1);
|
|
|
|
Branch->RemoveOperand(0);
|
|
|
|
|
|
|
|
// Rebuild Branch as a fused compare and branch.
|
2016-11-11 20:48:26 +08:00
|
|
|
// SrcNOps is the number of MI operands of the compare instruction
|
|
|
|
// that we need to copy over.
|
|
|
|
unsigned SrcNOps = 2;
|
|
|
|
if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
|
|
|
|
SrcNOps = 3;
|
2013-08-05 18:58:53 +08:00
|
|
|
Branch->setDesc(TII->get(FusedOpcode));
|
2016-04-08 00:11:44 +08:00
|
|
|
MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
|
2016-11-11 20:48:26 +08:00
|
|
|
for (unsigned I = 0; I < SrcNOps; I++)
|
2017-01-13 17:58:52 +08:00
|
|
|
MIB.add(Compare.getOperand(I));
|
|
|
|
MIB.add(CCMask);
|
2016-04-08 00:11:44 +08:00
|
|
|
|
|
|
|
if (Type == SystemZII::CompareAndBranch) {
|
|
|
|
// Only conditional branches define CC, as they may be converted back
|
|
|
|
// to a non-fused branch because of a long displacement. Conditional
|
|
|
|
// returns don't have that problem.
|
2017-01-13 17:58:52 +08:00
|
|
|
MIB.add(Target).addReg(SystemZ::CC,
|
|
|
|
RegState::ImplicitDefine | RegState::Dead);
|
2016-04-08 00:11:44 +08:00
|
|
|
}
|
2013-08-05 18:58:53 +08:00
|
|
|
|
2016-04-11 20:12:32 +08:00
|
|
|
if (Type == SystemZII::CompareAndSibcall)
|
|
|
|
MIB.addRegMask(RegMask);
|
|
|
|
|
2013-08-05 18:58:53 +08:00
|
|
|
// Clear any intervening kills of SrcReg and SrcReg2.
|
|
|
|
MBBI = Compare;
|
|
|
|
for (++MBBI; MBBI != MBBE; ++MBBI) {
|
|
|
|
MBBI->clearRegisterKills(SrcReg, TRI);
|
|
|
|
if (SrcReg2)
|
|
|
|
MBBI->clearRegisterKills(SrcReg2, TRI);
|
|
|
|
}
|
|
|
|
FusedComparisons += 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process all comparison instructions in MBB. Return true if something
|
|
|
|
// changed.
|
2014-03-06 19:00:15 +08:00
|
|
|
bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
|
2013-08-05 18:58:53 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Walk backwards through the block looking for comparisons, recording
|
|
|
|
// all CC users as we go. The subroutines can delete Compare and
|
|
|
|
// instructions before it.
|
2019-11-04 23:11:12 +08:00
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(MBB);
|
|
|
|
bool CompleteCCUsers = !LiveRegs.contains(SystemZ::CC);
|
2013-08-05 18:58:53 +08:00
|
|
|
SmallVector<MachineInstr *, 4> CCUsers;
|
2014-03-06 19:00:15 +08:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.end();
|
|
|
|
while (MBBI != MBB.begin()) {
|
2016-07-12 09:39:01 +08:00
|
|
|
MachineInstr &MI = *--MBBI;
|
|
|
|
if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
|
2013-08-05 18:58:53 +08:00
|
|
|
(optimizeCompareZero(MI, CCUsers) ||
|
2016-06-11 03:58:10 +08:00
|
|
|
fuseCompareOperations(MI, CCUsers))) {
|
2013-08-05 18:58:53 +08:00
|
|
|
++MBBI;
|
2016-07-12 09:39:01 +08:00
|
|
|
MI.eraseFromParent();
|
2013-08-05 18:58:53 +08:00
|
|
|
Changed = true;
|
|
|
|
CCUsers.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-12 09:39:01 +08:00
|
|
|
if (MI.definesRegister(SystemZ::CC)) {
|
2013-08-05 18:58:53 +08:00
|
|
|
CCUsers.clear();
|
2015-10-08 15:39:55 +08:00
|
|
|
CompleteCCUsers = true;
|
2013-08-05 19:23:46 +08:00
|
|
|
}
|
2016-07-12 09:39:01 +08:00
|
|
|
if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
|
|
|
|
CCUsers.push_back(&MI);
|
2013-08-05 18:58:53 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(F.getFunction()))
|
2016-04-27 07:49:41 +08:00
|
|
|
return false;
|
|
|
|
|
2014-08-05 10:39:49 +08:00
|
|
|
TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
|
2013-08-05 18:58:53 +08:00
|
|
|
TRI = &TII->getRegisterInfo();
|
|
|
|
|
|
|
|
bool Changed = false;
|
2014-03-06 19:00:15 +08:00
|
|
|
for (auto &MBB : F)
|
|
|
|
Changed |= processBlock(MBB);
|
2013-08-05 18:58:53 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2017-01-25 06:10:43 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
|
|
|
|
return new SystemZElimCompare(TM);
|
|
|
|
}
|