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");
56 explicit operator bool()
const {
return Def ||
Use; }
72 return "SystemZ Comparison Elimination";
93 unsigned ConvOpc = 0);
110 if ((*SI)->isLiveIn(SystemZ::CC))
153 if (
unsigned MOReg = MO.
getReg()) {
154 if (
TRI->regsOverlap(MOReg, Reg)) {
171 return (MI.
getOpcode() == SystemZ::LTEBR ||
193 bool SystemZElimCompare::convertToBRCT(
199 if (Opcode == SystemZ::AHI)
200 BRCT = SystemZ::BRCT;
201 else if (Opcode == SystemZ::AGHI)
202 BRCT = SystemZ::BRCTG;
203 else if (Opcode == SystemZ::AIH)
204 BRCT = SystemZ::BRCTH;
211 if (CCUsers.
size() != 1)
214 if (Branch->
getOpcode() != SystemZ::BRC ||
224 for (++MBBI; MBBI != MBBE; ++MBBI)
225 if (getRegReferences(*MBBI, SrcReg))
238 if (BRCT != SystemZ::BRCTH)
247 bool SystemZElimCompare::convertToLoadAndTrap(
250 unsigned LATOpcode =
TII->getLoadAndTrap(MI.
getOpcode());
255 if (CCUsers.
size() != 1)
258 if (Branch->
getOpcode() != SystemZ::CondTrap ||
268 for (++MBBI; MBBI != MBBE; ++MBBI)
269 if (getRegReferences(*MBBI, SrcReg))
287 bool SystemZElimCompare::convertToLoadAndTest(
293 if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode))
299 for (
const auto &MO : MI.
operands())
313 bool SystemZElimCompare::adjustCCMasksForInstr(
317 int Opcode = (ConvOpc ? ConvOpc : MI.
getOpcode());
319 unsigned MIFlags = Desc.
TSFlags;
329 if (ReusableCCMask == 0)
333 assert((ReusableCCMask & ~CCValues) == 0 &&
"Invalid CCValues");
335 bool MIEquivalentToCmp =
336 (ReusableCCMask == CCValues &&
339 if (!MIEquivalentToCmp) {
342 for (
unsigned int I = 0,
E = CCUsers.
size();
I !=
E; ++
I) {
360 unsigned OutValid = ~ReusableCCMask & CCValid;
361 unsigned OutMask = ~ReusableCCMask & CCMask;
362 if (OutMask != 0 && OutMask != OutValid)
370 for (
unsigned I = 0,
E = AlterMasks.
size();
I !=
E;
I += 2) {
371 AlterMasks[
I]->setImm(CCValues);
372 unsigned CCMask = AlterMasks[
I + 1]->getImm();
373 if (CCMask & ~ReusableCCMask)
374 AlterMasks[
I + 1]->setImm((CCMask & ReusableCCMask) |
375 (CCValues & ~ReusableCCMask));
382 assert(CCDef >= 0 &&
"Couldn't find CC set");
387 bool BeforeCmp =
false;
389 for (++MBBI; MBBI != MBBE; ++MBBI)
390 if (MBBI == Compare) {
398 for (++MBBI; MBBI != MBBE; ++MBBI)
399 MBBI->clearRegisterKills(SystemZ::CC,
TRI);
408 case SystemZ::LTEBRCompare:
409 case SystemZ::LTDBRCompare:
410 case SystemZ::LTXBRCompare:
425 bool SystemZElimCompare::optimizeCompareZero(
437 MBBE = MBB.
rend(); MBBI != MBBE;) {
443 if (!CCRefs.Use && !SrcRefs) {
444 if (convertToBRCT(MI, Compare, CCUsers)) {
448 if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
454 if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) ||
455 (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
456 EliminatedComparisons += 1;
460 SrcRefs |= getRegReferences(MI, SrcReg);
463 CCRefs |= getRegReferences(MI, SystemZ::CC);
464 if (CCRefs.Use && CCRefs.Def)
477 if (convertToLoadAndTest(MI, Compare, CCUsers)) {
478 EliminatedComparisons += 1;
482 if (getRegReferences(MI, SrcReg).Def)
484 if (getRegReferences(MI, SystemZ::CC))
493 bool SystemZElimCompare::fuseCompareOperations(
496 if (CCUsers.
size() != 1)
504 case SystemZ::CondReturn:
507 case SystemZ::CallBCR:
510 case SystemZ::CondTrap:
518 unsigned FusedOpcode =
531 for (++MBBI; MBBI != MBBE; ++MBBI)
532 if (MBBI->modifiesRegister(SrcReg,
TRI) ||
533 (SrcReg2 && MBBI->modifiesRegister(SrcReg2,
TRI)))
539 "Invalid condition-code mask for integer comparison");
545 RegMask = MBBI->getOperand(2).getRegMask();
548 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC,
false,
TRI);
549 assert(CCUse >= 0 &&
"BRC/BCR must use CC");
561 unsigned SrcNOps = 2;
562 if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
566 for (
unsigned I = 0;
I < SrcNOps;
I++)
583 for (++MBBI; MBBI != MBBE; ++MBBI) {
584 MBBI->clearRegisterKills(SrcReg,
TRI);
586 MBBI->clearRegisterKills(SrcReg2,
TRI);
588 FusedComparisons += 1;
595 bool Changed =
false;
603 while (MBBI != MBB.
begin()) {
606 (optimizeCompareZero(MI, CCUsers) ||
607 fuseCompareOperations(MI, CCUsers))) {
617 CompleteCCUsers =
true;
630 TRI = &
TII->getRegisterInfo();
632 bool Changed =
false;
634 Changed |= processBlock(MBB);
640 return new SystemZElimCompare(TM);
void push_back(const T &Elt)
const MachineInstrBuilder & add(const MachineOperand &MO) const
Compute iterated dominance frontiers using a linear time algorithm.
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.
unsigned getReg() const
getReg - Returns the register number.
static bool resultTests(MachineInstr &MI, unsigned Reg)
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
iterator_range< mop_iterator > operands()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const unsigned CCMASK_ICMP
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Access to explicit operands of the instruction.
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.
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.
static bool isCCLiveOut(MachineBasicBlock &MBB)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
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")
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
FunctionPass class - This class is used to implement most global optimizations.
bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
succ_iterator succ_begin()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
static unsigned getCompareZeroCCMask(unsigned int Flags)
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
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
const unsigned CCMASK_CMP_NE
const Function & getFunction() const
Return the LLVM function that this machine code represents.
Target - Wrapper for Target specific information.
int findRegisterDefOperandIdx(unsigned Reg, bool isDead=false, bool Overlap=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a def of the specified register or -1 if it is not found...
bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
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.
static unsigned getCCValues(unsigned int Flags)
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
FunctionPass * createSystemZElimComparePass(SystemZTargetMachine &TM)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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)
const MachineOperand & getOperand(unsigned i) const
void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd)
Assign this MachineInstr's memory reference descriptor list.
Properties which a MachineFunction may have at a given point in time.
mmo_iterator memoperands_end() const