95#define DEBUG_TYPE "aarch64-condopt"
97STATISTIC(NumConditionsAdjusted,
"Number of conditions adjusted");
109 const TargetInstrInfo *TII;
110 const TargetRegisterInfo *TRI;
111 MachineDominatorTree *DomTree;
112 const MachineRegisterInfo *MRI;
117 AArch64ConditionOptimizer() : MachineFunctionPass(ID) {}
119 void getAnalysisUsage(AnalysisUsage &AU)
const override;
120 bool canAdjustCmp(MachineInstr &CmpMI);
121 bool registersMatch(MachineInstr *FirstMI, MachineInstr *SecondMI);
122 bool nzcvLivesOut(MachineBasicBlock *
MBB);
123 MachineInstr *getBccTerminator(MachineBasicBlock *
MBB);
124 MachineInstr *findAdjustableCmp(MachineInstr *CondMI);
126 void modifyCmp(MachineInstr *CmpMI,
const CmpInfo &Info);
129 bool optimizeIntraBlock(MachineBasicBlock &
MBB);
130 bool optimizeCrossBlock(MachineBasicBlock &HBB);
131 bool runOnMachineFunction(MachineFunction &MF)
override;
133 StringRef getPassName()
const override {
134 return "AArch64 Condition Optimizer";
140char AArch64ConditionOptimizer::ID = 0;
143 "AArch64 CondOpt Pass",
false,
false)
149 return new AArch64ConditionOptimizer();
152void AArch64ConditionOptimizer::getAnalysisUsage(
AnalysisUsage &AU)
const {
160bool AArch64ConditionOptimizer::canAdjustCmp(MachineInstr &CmpMI) {
163 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp is symbolic, " << CmpMI <<
'\n');
166 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp may be out of range, " << CmpMI
170 LLVM_DEBUG(
dbgs() <<
"Destination of cmp is not dead, " << CmpMI <<
'\n');
178bool AArch64ConditionOptimizer::registersMatch(MachineInstr *FirstMI,
179 MachineInstr *SecondMI) {
183 FirstReg.
isVirtual() ?
TRI->lookThruCopyLike(FirstReg, MRI) : FirstReg;
185 SecondReg.
isVirtual() ?
TRI->lookThruCopyLike(SecondReg, MRI) : SecondReg;
186 if (FirstCmpReg != SecondCmpReg) {
195bool AArch64ConditionOptimizer::nzcvLivesOut(MachineBasicBlock *
MBB) {
197 if (SuccBB->isLiveIn(AArch64::NZCV)) {
211 case AArch64::SUBSWri:
212 case AArch64::SUBSXri:
214 case AArch64::ADDSWri:
215 case AArch64::ADDSXri:
223 return Opc == AArch64::CSINCWr ||
Opc == AArch64::CSINCXr;
228AArch64ConditionOptimizer::getBccTerminator(MachineBasicBlock *
MBB) {
236 if (
Term->getOpcode() != AArch64::Bcc) {
250AArch64ConditionOptimizer::findAdjustableCmp(MachineInstr *CondMI) {
251 assert(CondMI &&
"CondMI cannot be null");
260 MachineInstr &
I = *It;
261 assert(!
I.isTerminator() &&
"Spurious terminator");
263 if (
I.readsRegister(AArch64::NZCV,
nullptr))
267 if (!canAdjustCmp(
I)) {
273 if (
I.modifiesRegister(AArch64::NZCV,
nullptr))
284 case AArch64::ADDSWri:
return AArch64::SUBSWri;
285 case AArch64::ADDSXri:
return AArch64::SUBSXri;
286 case AArch64::SUBSWri:
return AArch64::ADDSWri;
287 case AArch64::SUBSXri:
return AArch64::ADDSXri;
319CmpInfo AArch64ConditionOptimizer::getAdjustedCmpInfo(MachineInstr *CmpMI,
328 bool Negative = (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri);
333 Correction = -Correction;
337 const int NewImm = std::abs(OldImm + Correction);
341 if (OldImm == 0 && Negative)
342 return {OldImm,
Opc,
Cmp};
344 if ((OldImm == 1 && Negative && Correction == -1) ||
345 (OldImm == 0 && Correction == -1)) {
350 return {OldImm,
Opc,
Cmp};
358void AArch64ConditionOptimizer::modifyCmp(MachineInstr *CmpMI,
359 const CmpInfo &Info) {
380 ++NumConditionsAdjusted;
389 assert(
Cond.size() == 1 &&
"Unknown Cond array format");
399bool AArch64ConditionOptimizer::adjustTo(MachineInstr *CmpMI,
402 CmpInfo
Info = getAdjustedCmpInfo(CmpMI, Cmp);
404 modifyCmp(CmpMI, Info);
435bool AArch64ConditionOptimizer::optimizeIntraBlock(MachineBasicBlock &
MBB) {
436 MachineInstr *FirstCSINC =
nullptr;
437 MachineInstr *SecondCSINC =
nullptr;
440 for (MachineInstr &
MI :
MBB) {
444 }
else if (!SecondCSINC) {
451 if (!FirstCSINC || !SecondCSINC) {
456 if (nzcvLivesOut(&
MBB))
460 MachineInstr *FirstCmpMI = findAdjustableCmp(FirstCSINC);
461 MachineInstr *SecondCmpMI = findAdjustableCmp(SecondCSINC);
462 if (!FirstCmpMI || !SecondCmpMI)
466 if (FirstCmpMI == SecondCmpMI) {
467 LLVM_DEBUG(
dbgs() <<
"Both CSINCs already controlled by same CMP\n");
471 if (!registersMatch(FirstCmpMI, SecondCmpMI))
478 if (&*It != SecondCmpMI &&
479 It->modifiesRegister(AArch64::NZCV,
nullptr)) {
480 LLVM_DEBUG(
dbgs() <<
"Flags modified between CMPs by: " << *It <<
'\n');
488 if (It->readsRegister(AArch64::NZCV,
nullptr)) {
489 LLVM_DEBUG(
dbgs() <<
"Flags read after second CSINC by: " << *It <<
'\n');
506 << SecondImm <<
'\n');
510 if (FirstCond == SecondCond &&
512 std::abs(SecondImm - FirstImm) == 1) {
516 bool adjustFirst = (FirstImm < SecondImm);
518 adjustFirst = !adjustFirst;
521 MachineInstr *CmpToAdjust = adjustFirst ? FirstCmpMI : SecondCmpMI;
522 MachineInstr *CSINCToAdjust = adjustFirst ? FirstCSINC : SecondCSINC;
524 int TargetImm = adjustFirst ? SecondImm : FirstImm;
526 CmpInfo Adj = getAdjustedCmpInfo(CmpToAdjust, CondToAdjust);
528 if (Adj.Imm == TargetImm &&
529 Adj.Opc == (adjustFirst ? SecondCmpMI : FirstCmpMI)->getOpcode()) {
530 LLVM_DEBUG(
dbgs() <<
"Successfully optimizing intra-block CSINC pair\n");
545bool AArch64ConditionOptimizer::optimizeCrossBlock(MachineBasicBlock &HBB) {
547 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
553 if (!
TBB ||
TBB == &HBB) {
558 MachineBasicBlock *TBB_TBB =
nullptr, *TBB_FBB =
nullptr;
563 MachineInstr *HeadBrMI = getBccTerminator(&HBB);
564 MachineInstr *TrueBrMI = getBccTerminator(
TBB);
565 if (!HeadBrMI || !TrueBrMI)
569 if (nzcvLivesOut(&HBB) || nzcvLivesOut(
TBB))
572 MachineInstr *HeadCmpMI = findAdjustableCmp(HeadBrMI);
573 MachineInstr *TrueCmpMI = findAdjustableCmp(TrueBrMI);
574 if (!HeadCmpMI || !TrueCmpMI)
577 if (!registersMatch(HeadCmpMI, TrueCmpMI))
593 int HeadImmTrueValue = HeadImm;
594 int TrueImmTrueValue = TrueImm;
607 if (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri)
608 HeadImmTrueValue = -HeadImmTrueValue;
611 if (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri)
612 TrueImmTrueValue = -TrueImmTrueValue;
616 std::abs(TrueImmTrueValue - HeadImmTrueValue) == 2) {
627 CmpInfo HeadCmpInfo = getAdjustedCmpInfo(HeadCmpMI, HeadCmp);
628 CmpInfo TrueCmpInfo = getAdjustedCmpInfo(TrueCmpMI, TrueCmp);
629 if (HeadCmpInfo.Imm == TrueCmpInfo.Imm &&
630 HeadCmpInfo.Opc == TrueCmpInfo.Opc) {
631 modifyCmp(HeadCmpMI, HeadCmpInfo);
632 modifyCmp(TrueCmpMI, TrueCmpInfo);
637 std::abs(TrueImmTrueValue - HeadImmTrueValue) == 1) {
650 bool adjustHeadCond = (HeadImmTrueValue < TrueImmTrueValue);
652 adjustHeadCond = !adjustHeadCond;
655 if (adjustHeadCond) {
656 return adjustTo(HeadCmpMI, HeadCmp, TrueCmpMI, TrueImm);
658 return adjustTo(TrueCmpMI, TrueCmp, HeadCmpMI, HeadImm);
667bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
668 LLVM_DEBUG(
dbgs() <<
"********** AArch64 Conditional Compares **********\n"
669 <<
"********** Function: " << MF.
getName() <<
'\n');
675 DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
686 MachineBasicBlock *HBB =
I->getBlock();
687 Changed |= optimizeIntraBlock(*HBB);
688 Changed |= optimizeCrossBlock(*HBB);
static int getComplementOpc(int Opc)
static bool isGreaterThan(AArch64CC::CondCode Cmp)
static AArch64CC::CondCode getAdjustedCmp(AArch64CC::CondCode Cmp)
static bool isCSINCInstruction(unsigned Opc)
static bool isLessThan(AArch64CC::CondCode Cmp)
static bool isCmpInstruction(unsigned Opc)
static bool parseCond(ArrayRef< MachineOperand > Cond, AArch64CC::CondCode &CC)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
FunctionPass class - This class is used to implement most global optimizations.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
iterator_range< succ_iterator > successors()
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
void setImm(int64_t immVal)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static const char * getCondCodeName(CondCode Code)
static unsigned getShiftValue(unsigned Imm)
getShiftValue - Extract the shift value.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createAArch64ConditionOptimizerPass()
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
iterator_range< df_iterator< T > > depth_first(const T &G)
IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It, then continue decrementing it while it points to a debug instruction.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.