36 #define DEBUG_TYPE "systemz-elim-compare" 38 STATISTIC(BranchOnCounts,
"Number of branch-on-count instructions");
39 STATISTIC(LoadAndTraps,
"Number of load-and-trap instructions");
40 STATISTIC(EliminatedComparisons,
"Number of eliminated comparisons");
41 STATISTIC(FusedComparisons,
"Number of fused compare-and-branch instructions");
48 Reference() =
default;
56 explicit operator bool()
const {
return Def ||
Use; }
72 return "SystemZ Comparison Elimination";
95 unsigned ConvOpc = 0);
151 if (
TRI->regsOverlap(MOReg, Reg)) {
168 return (MI.
getOpcode() == SystemZ::LTEBR ||
190 bool SystemZElimCompare::convertToBRCT(
196 if (Opcode == SystemZ::AHI)
197 BRCT = SystemZ::BRCT;
198 else if (Opcode == SystemZ::AGHI)
199 BRCT = SystemZ::BRCTG;
200 else if (Opcode == SystemZ::AIH)
201 BRCT = SystemZ::BRCTH;
208 if (CCUsers.
size() != 1)
211 if (Branch->
getOpcode() != SystemZ::BRC ||
221 for (++MBBI; MBBI != MBBE; ++MBBI)
222 if (getRegReferences(*MBBI, SrcReg))
235 if (BRCT != SystemZ::BRCTH)
244 bool SystemZElimCompare::convertToLoadAndTrap(
247 unsigned LATOpcode =
TII->getLoadAndTrap(MI.
getOpcode());
252 if (CCUsers.
size() != 1)
255 if (Branch->
getOpcode() != SystemZ::CondTrap ||
265 for (++MBBI; MBBI != MBBE; ++MBBI)
266 if (getRegReferences(*MBBI, SrcReg))
284 bool SystemZElimCompare::convertToLoadAndTest(
290 if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode))
295 for (
const auto &MO : MI.
operands())
303 MIB.setMIFlag(MachineInstr::MIFlag::NoFPExcept);
312 bool SystemZElimCompare::convertToLogical(
316 unsigned ConvOpc = 0;
318 case SystemZ::AR: ConvOpc = SystemZ::ALR;
break;
319 case SystemZ::ARK: ConvOpc = SystemZ::ALRK;
break;
320 case SystemZ::AGR: ConvOpc = SystemZ::ALGR;
break;
321 case SystemZ::AGRK: ConvOpc = SystemZ::ALGRK;
break;
323 case SystemZ::AY: ConvOpc = SystemZ::ALY;
break;
324 case SystemZ::AG: ConvOpc = SystemZ::ALG;
break;
327 if (!ConvOpc || !adjustCCMasksForInstr(MI, Compare, CCUsers, ConvOpc))
360 bool SystemZElimCompare::adjustCCMasksForInstr(
366 int Opcode = (ConvOpc ? ConvOpc : MI.
getOpcode());
368 unsigned MIFlags = Desc.
TSFlags;
384 unsigned ReusableCCMask = CCValues;
388 unsigned OFImplies = 0;
389 bool LogicalMI =
false;
390 bool MIEquivalentToCmp =
false;
412 else if ((MIFlags & SystemZII::IsLogical) && CCValues) {
420 assert((ReusableCCMask & ~CCValues) == 0 &&
"Invalid CCValues");
422 ReusableCCMask == CCValues && CCValues == CompareCCValues;
424 if (ReusableCCMask == 0)
427 if (!MIEquivalentToCmp) {
430 for (
unsigned int I = 0,
E = CCUsers.
size();
I !=
E; ++
I) {
448 assert(CCValid == CompareCCValues && (CCMask & ~CCValid) == 0 &&
449 "Corrupt CC operands of CCUser.");
450 unsigned OutValid = ~ReusableCCMask & CCValid;
451 unsigned OutMask = ~ReusableCCMask & CCMask;
452 if (OutMask != 0 && OutMask != OutValid)
460 for (
unsigned I = 0,
E = AlterMasks.
size();
I !=
E;
I += 2) {
461 AlterMasks[
I]->setImm(CCValues);
462 unsigned CCMask = AlterMasks[
I + 1]->getImm();
469 if (CCMask & ~ReusableCCMask)
470 CCMask = (CCMask & ReusableCCMask) | (CCValues & ~ReusableCCMask);
473 AlterMasks[
I + 1]->setImm(CCMask);
482 bool BeforeCmp =
false;
484 for (++MBBI; MBBI != MBBE; ++MBBI)
485 if (MBBI == Compare) {
493 for (++MBBI; MBBI != MBBE; ++MBBI)
494 MBBI->clearRegisterKills(SystemZ::CC,
TRI);
503 case SystemZ::LTEBRCompare:
504 case SystemZ::LTDBRCompare:
505 case SystemZ::LTXBRCompare:
520 bool SystemZElimCompare::optimizeCompareZero(
532 MBBE = MBB.
rend(); MBBI != MBBE;) {
538 if (!CCRefs.Use && !SrcRefs) {
539 if (convertToBRCT(MI, Compare, CCUsers)) {
543 if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
549 if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) ||
551 (adjustCCMasksForInstr(MI, Compare, CCUsers) ||
552 convertToLogical(MI, Compare, CCUsers)))) {
553 EliminatedComparisons += 1;
557 SrcRefs |= getRegReferences(MI, SrcReg);
560 CCRefs |= getRegReferences(MI, SystemZ::CC);
561 if (CCRefs.Use && CCRefs.Def)
580 if (convertToLoadAndTest(MI, Compare, CCUsers)) {
581 EliminatedComparisons += 1;
585 if (getRegReferences(MI, SrcReg).Def)
587 if (getRegReferences(MI, SystemZ::CC))
596 bool SystemZElimCompare::fuseCompareOperations(
599 if (CCUsers.
size() != 1)
607 case SystemZ::CondReturn:
610 case SystemZ::CallBCR:
613 case SystemZ::CondTrap:
621 unsigned FusedOpcode =
634 for (++MBBI; MBBI != MBBE; ++MBBI)
635 if (MBBI->modifiesRegister(SrcReg,
TRI) ||
636 (SrcReg2 && MBBI->modifiesRegister(SrcReg2,
TRI)))
642 "Invalid condition-code mask for integer comparison");
648 RegMask = MBBI->getOperand(2).getRegMask();
651 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC,
false,
TRI);
652 assert(CCUse >= 0 &&
"BRC/BCR must use CC");
664 unsigned SrcNOps = 2;
665 if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
669 for (
unsigned I = 0;
I < SrcNOps;
I++)
686 for (++MBBI; MBBI != MBBE; ++MBBI) {
687 MBBI->clearRegisterKills(SrcReg,
TRI);
689 MBBI->clearRegisterKills(SrcReg2,
TRI);
691 FusedComparisons += 1;
698 bool Changed =
false;
705 bool CompleteCCUsers = !LiveRegs.
contains(SystemZ::CC);
708 while (MBBI != MBB.
begin()) {
711 (optimizeCompareZero(MI, CCUsers) ||
712 fuseCompareOperations(MI, CCUsers))) {
722 CompleteCCUsers =
true;
735 TRI = &
TII->getRegisterInfo();
737 bool Changed =
false;
739 Changed |= processBlock(MBB);
745 return new SystemZElimCompare(TM);
const unsigned CCMASK_CMP_GT
const MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand *> MMOs) const
const MachineInstrBuilder & add(const MachineOperand &MO) const
bool isCall(QueryType Type=AnyInBundle) const
This class represents lattice values for constants.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
static bool resultTests(MachineInstr &MI, unsigned Reg)
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
bool contains(MCPhysReg Reg) const
Returns true if register Reg is contained in the set.
static bool isAddWithImmediate(unsigned Opcode)
iterator_range< mop_iterator > operands()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const unsigned CCMASK_ICMP
void clearRegisterDeads(Register Reg)
Clear all dead flags on operands defining register Reg.
return AArch64::GPR64RegClass contains(Reg)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
The access may reference the value stored in memory.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
bool mayRaiseFPException() const
Return true if this instruction could possibly raise a floating-point exception.
static bool isCompareZero(MachineInstr &Compare)
virtual const TargetInstrInfo * getInstrInfo() const
bool operator|=(SparseBitVector< ElementSize > &LHS, const SparseBitVector< ElementSize > *RHS)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
The instances of the Type class are immutable: once they are created, they are never changed...
bool isCompare(QueryType Type=IgnoreBundle) const
Return true if this instruction is a comparison.
static bool preservesValueOf(MachineInstr &MI, unsigned Reg)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
bool definesRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
FunctionPass class - This class is used to implement most global optimizations.
const unsigned CCMASK_CMP_LT
const unsigned CCMASK_LOGICAL_ZERO
const unsigned CCMASK_LOGICAL_NONZERO
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
bool mayRaiseFPException() const
Return true if this instruction may raise a floating-point exception.
bool isDebugInstr() const
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
static unsigned getCompareZeroCCMask(unsigned int Flags)
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
const unsigned CCMASK_CMP_EQ
Promote Memory to Register
const unsigned CCMASK_CMP_NE
const Function & getFunction() const
Return the LLVM function that this machine code represents.
Target - Wrapper for Target specific information.
const MachineBasicBlock * getParent() const
MachineFunctionProperties & set(Property P)
static unsigned getCompareSourceReg(MachineInstr &Compare)
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
static unsigned getCCValues(unsigned int Flags)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
FunctionPass * createSystemZElimComparePass(SystemZTargetMachine &TM)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const unsigned CCMASK_ARITH_OVERFLOW
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
StringRef - Represent a constant reference to a string, i.e.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
static bool isLoadAndTestAsCmp(MachineInstr &MI)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
Properties which a MachineFunction may have at a given point in time.
Wrapper class representing virtual and physical registers.