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;
50 Reference &
operator|=(
const Reference &Other) {
56 explicit operator bool()
const {
return Def ||
Use; }
72 return "SystemZ Comparison Elimination";
108 if ((*SI)->isLiveIn(SystemZ::CC))
145 if (
unsigned MOReg = MO.
getReg()) {
146 if (TRI->regsOverlap(MOReg, Reg)) {
163 return (MI.
getOpcode() == SystemZ::LTEBR ||
185 bool SystemZElimCompare::convertToBRCT(
191 if (Opcode == SystemZ::AHI)
192 BRCT = SystemZ::BRCT;
193 else if (Opcode == SystemZ::AGHI)
194 BRCT = SystemZ::BRCTG;
195 else if (Opcode == SystemZ::AIH)
196 BRCT = SystemZ::BRCTH;
203 if (CCUsers.
size() != 1)
206 if (Branch->
getOpcode() != SystemZ::BRC ||
216 for (++MBBI; MBBI != MBBE; ++MBBI)
217 if (getRegReferences(*MBBI, SrcReg))
230 if (BRCT != SystemZ::BRCTH)
239 bool SystemZElimCompare::convertToLoadAndTrap(
242 unsigned LATOpcode =
TII->getLoadAndTrap(MI.
getOpcode());
247 if (CCUsers.
size() != 1)
250 if (Branch->
getOpcode() != SystemZ::CondTrap ||
260 for (++MBBI; MBBI != MBBE; ++MBBI)
261 if (getRegReferences(*MBBI, SrcReg))
279 bool SystemZElimCompare::convertToLoadAndTest(
MachineInstr &MI) {
295 bool SystemZElimCompare::adjustCCMasksForInstr(
300 unsigned MIFlags = Desc.
TSFlags;
310 if (ReusableCCMask == 0)
314 assert((ReusableCCMask & ~CCValues) == 0 &&
"Invalid CCValues");
318 for (
unsigned int I = 0,
E = CCUsers.
size();
I !=
E; ++
I) {
336 unsigned OutValid = ~ReusableCCMask & CCValid;
337 unsigned OutMask = ~ReusableCCMask & CCMask;
338 if (OutMask != 0 && OutMask != OutValid)
346 for (
unsigned I = 0,
E = AlterMasks.
size();
I !=
E;
I += 2) {
347 AlterMasks[
I]->setImm(CCValues);
348 unsigned CCMask = AlterMasks[
I + 1]->getImm();
349 if (CCMask & ~ReusableCCMask)
350 AlterMasks[
I + 1]->setImm((CCMask & ReusableCCMask) |
351 (CCValues & ~ReusableCCMask));
356 assert(CCDef >= 0 &&
"Couldn't find CC set");
361 for (++MBBI; MBBI != MBBE; ++MBBI)
362 MBBI->clearRegisterKills(SystemZ::CC, TRI);
370 case SystemZ::LTEBRCompare:
371 case SystemZ::LTDBRCompare:
372 case SystemZ::LTXBRCompare:
387 bool SystemZElimCompare::optimizeCompareZero(
398 while (MBBI != MBBE) {
405 if (!CCRefs.Use && !SrcRefs) {
406 if (convertToBRCT(MI, Compare, CCUsers)) {
410 if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
416 if ((!CCRefs && convertToLoadAndTest(MI)) ||
417 (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
418 EliminatedComparisons += 1;
422 SrcRefs |= getRegReferences(MI, SrcReg);
425 CCRefs |= getRegReferences(MI, SystemZ::CC);
426 if (CCRefs.Use && CCRefs.Def)
434 bool SystemZElimCompare::fuseCompareOperations(
437 if (CCUsers.
size() != 1)
445 case SystemZ::CondReturn:
448 case SystemZ::CallBCR:
451 case SystemZ::CondTrap:
459 unsigned FusedOpcode =
472 for (++MBBI; MBBI != MBBE; ++MBBI)
473 if (MBBI->modifiesRegister(SrcReg, TRI) ||
474 (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
480 "Invalid condition-code mask for integer comparison");
486 RegMask = MBBI->getOperand(2).getRegMask();
489 int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC,
false, TRI);
490 assert(CCUse >= 0 &&
"BRC/BCR must use CC");
502 unsigned SrcNOps = 2;
503 if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
507 for (
unsigned I = 0;
I < SrcNOps;
I++)
524 for (++MBBI; MBBI != MBBE; ++MBBI) {
525 MBBI->clearRegisterKills(SrcReg, TRI);
527 MBBI->clearRegisterKills(SrcReg2, TRI);
529 FusedComparisons += 1;
536 bool Changed =
false;
544 while (MBBI != MBB.
begin()) {
547 (optimizeCompareZero(MI, CCUsers) ||
548 fuseCompareOperations(MI, CCUsers))) {
558 CompleteCCUsers =
true;
573 bool Changed =
false;
575 Changed |= processBlock(MBB);
581 return new SystemZElimCompare(TM);
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
STATISTIC(NumFunctions,"Total number of functions")
Describe properties that are true of each instruction in the target description file.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
const MachineInstrBuilder & add(const MachineOperand &MO) const
static bool resultTests(MachineInstr &MI, unsigned Reg)
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
void setIsDead(bool Val=true)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
const unsigned CCMASK_ICMP
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
A Use represents the edge between a Value definition and its users.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
void RemoveOperand(unsigned i)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
const HexagonRegisterInfo & getRegisterInfo() const
HexagonInstrInfo specifics.
static bool isCompareZero(MachineInstr &Compare)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
bool operator|=(SparseBitVector< ElementSize > &LHS, const SparseBitVector< ElementSize > *RHS)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
static bool isCCLiveOut(MachineBasicBlock &MBB)
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...
const MachineOperand & getOperand(unsigned i) const
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
FunctionPass class - This class is used to implement most global optimizations.
succ_iterator succ_begin()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
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...
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
static const char * Target
const unsigned CCMASK_CMP_NE
bool isCompare(QueryType Type=IgnoreBundle) const
Return true if this instruction is a comparison.
Target - Wrapper for Target specific information.
MachineFunctionProperties & set(Property P)
static std::vector< std::string > Flags
static unsigned getCompareSourceReg(MachineInstr &Compare)
Representation of each machine instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static unsigned getCCValues(unsigned int Flags)
FunctionPass * createSystemZElimComparePass(SystemZTargetMachine &TM)
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual const TargetInstrInfo * getInstrInfo() const
StringRef - Represent a constant reference to a string, i.e.
static bool isLoadAndTestAsCmp(MachineInstr &MI)
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Properties which a MachineFunction may have at a given point in time.