96#define DEBUG_TYPE "aarch64-condopt"
98STATISTIC(NumConditionsAdjusted,
"Number of conditions adjusted");
111 using CmpInfo = std::tuple<int, unsigned, AArch64CC::CondCode>;
117 void getAnalysisUsage(AnalysisUsage &AU)
const override;
118 bool canAdjustCmp(MachineInstr &CmpMI);
119 bool registersMatch(MachineInstr *FirstMI, MachineInstr *SecondMI);
120 bool nzcvLivesOut(MachineBasicBlock *
MBB);
121 MachineInstr *getBccTerminator(MachineBasicBlock *
MBB);
122 MachineInstr *findAdjustableCmp(MachineInstr *CondMI);
124 void modifyCmp(MachineInstr *CmpMI,
const CmpInfo &Info);
127 bool optimizeIntraBlock(MachineBasicBlock &
MBB);
128 bool optimizeCrossBlock(MachineBasicBlock &HBB);
129 bool runOnMachineFunction(MachineFunction &MF)
override;
131 StringRef getPassName()
const override {
132 return "AArch64 Condition Optimizer";
138char AArch64ConditionOptimizer::ID = 0;
141 "AArch64 CondOpt Pass",
false,
false)
147 return new AArch64ConditionOptimizer();
150void AArch64ConditionOptimizer::getAnalysisUsage(
AnalysisUsage &AU)
const {
158bool AArch64ConditionOptimizer::canAdjustCmp(MachineInstr &CmpMI) {
161 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp is symbolic, " << CmpMI <<
'\n');
164 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp may be out of range, " << CmpMI
168 LLVM_DEBUG(
dbgs() <<
"Destination of cmp is not dead, " << CmpMI <<
'\n');
176bool AArch64ConditionOptimizer::registersMatch(MachineInstr *FirstMI,
177 MachineInstr *SecondMI) {
181 FirstReg.
isVirtual() ?
TRI->lookThruCopyLike(FirstReg, MRI) : FirstReg;
183 SecondReg.
isVirtual() ?
TRI->lookThruCopyLike(SecondReg, MRI) : SecondReg;
184 if (FirstCmpReg != SecondCmpReg) {
193bool AArch64ConditionOptimizer::nzcvLivesOut(MachineBasicBlock *
MBB) {
195 if (SuccBB->isLiveIn(AArch64::NZCV)) {
209 case AArch64::SUBSWri:
210 case AArch64::SUBSXri:
212 case AArch64::ADDSWri:
213 case AArch64::ADDSXri:
221 return Opc == AArch64::CSINCWr ||
Opc == AArch64::CSINCXr;
226AArch64ConditionOptimizer::getBccTerminator(MachineBasicBlock *
MBB) {
234 if (
Term->getOpcode() != AArch64::Bcc) {
248AArch64ConditionOptimizer::findAdjustableCmp(MachineInstr *CondMI) {
249 assert(CondMI &&
"CondMI cannot be null");
258 MachineInstr &
I = *It;
259 assert(!
I.isTerminator() &&
"Spurious terminator");
261 if (
I.readsRegister(AArch64::NZCV,
nullptr))
265 if (!canAdjustCmp(
I)) {
271 if (
I.modifiesRegister(AArch64::NZCV,
nullptr))
282 case AArch64::ADDSWri:
return AArch64::SUBSWri;
283 case AArch64::ADDSXri:
return AArch64::SUBSXri;
284 case AArch64::SUBSWri:
return AArch64::ADDSWri;
285 case AArch64::SUBSXri:
return AArch64::ADDSXri;
317AArch64ConditionOptimizer::CmpInfo
318AArch64ConditionOptimizer::adjustCmp(MachineInstr *CmpMI,
327 bool Negative = (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri);
332 Correction = -Correction;
336 const int NewImm = std::abs(OldImm + Correction);
340 if (OldImm == 0 && Negative)
341 return CmpInfo(OldImm,
Opc, Cmp);
343 if ((OldImm == 1 && Negative && Correction == -1) ||
344 (OldImm == 0 && Correction == -1)) {
349 return CmpInfo(OldImm,
Opc, Cmp);
357void AArch64ConditionOptimizer::modifyCmp(MachineInstr *CmpMI,
358 const CmpInfo &Info) {
362 std::tie(Imm,
Opc, Cmp) =
Info;
384 ++NumConditionsAdjusted;
393 assert(
Cond.size() == 1 &&
"Unknown Cond array format");
403bool AArch64ConditionOptimizer::adjustTo(MachineInstr *CmpMI,
406 CmpInfo
Info = adjustCmp(CmpMI, Cmp);
407 if (std::get<0>(Info) == ToImm && std::get<1>(Info) == To->
getOpcode()) {
408 modifyCmp(CmpMI, Info);
439bool AArch64ConditionOptimizer::optimizeIntraBlock(MachineBasicBlock &
MBB) {
440 MachineInstr *FirstCSINC =
nullptr;
441 MachineInstr *SecondCSINC =
nullptr;
444 for (MachineInstr &
MI :
MBB) {
448 }
else if (!SecondCSINC) {
455 if (!FirstCSINC || !SecondCSINC) {
460 if (nzcvLivesOut(&
MBB))
464 MachineInstr *FirstCmpMI = findAdjustableCmp(FirstCSINC);
465 MachineInstr *SecondCmpMI = findAdjustableCmp(SecondCSINC);
466 if (!FirstCmpMI || !SecondCmpMI)
470 if (FirstCmpMI == SecondCmpMI) {
471 LLVM_DEBUG(
dbgs() <<
"Both CSINCs already controlled by same CMP\n");
475 if (!registersMatch(FirstCmpMI, SecondCmpMI))
482 if (&*It != SecondCmpMI &&
483 It->modifiesRegister(AArch64::NZCV,
nullptr)) {
484 LLVM_DEBUG(
dbgs() <<
"Flags modified between CMPs by: " << *It <<
'\n');
492 if (It->readsRegister(AArch64::NZCV,
nullptr)) {
493 LLVM_DEBUG(
dbgs() <<
"Flags read after second CSINC by: " << *It <<
'\n');
510 << SecondImm <<
'\n');
514 if (FirstCond == SecondCond &&
516 std::abs(SecondImm - FirstImm) == 1) {
520 bool adjustFirst = (FirstImm < SecondImm);
522 adjustFirst = !adjustFirst;
525 MachineInstr *CmpToAdjust = adjustFirst ? FirstCmpMI : SecondCmpMI;
526 MachineInstr *CSINCToAdjust = adjustFirst ? FirstCSINC : SecondCSINC;
528 int TargetImm = adjustFirst ? SecondImm : FirstImm;
530 CmpInfo AdjustedInfo = adjustCmp(CmpToAdjust, CondToAdjust);
532 if (std::get<0>(AdjustedInfo) == TargetImm &&
533 std::get<1>(AdjustedInfo) ==
534 (adjustFirst ? SecondCmpMI : FirstCmpMI)->
getOpcode()) {
535 LLVM_DEBUG(
dbgs() <<
"Successfully optimizing intra-block CSINC pair\n");
539 CmpToAdjust->
setDesc(
TII->get(std::get<1>(AdjustedInfo)));
550bool AArch64ConditionOptimizer::optimizeCrossBlock(MachineBasicBlock &HBB) {
552 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
558 if (!
TBB ||
TBB == &HBB) {
563 MachineBasicBlock *TBB_TBB =
nullptr, *TBB_FBB =
nullptr;
568 MachineInstr *HeadBrMI = getBccTerminator(&HBB);
569 MachineInstr *TrueBrMI = getBccTerminator(
TBB);
570 if (!HeadBrMI || !TrueBrMI)
574 if (nzcvLivesOut(&HBB) || nzcvLivesOut(
TBB))
577 MachineInstr *HeadCmpMI = findAdjustableCmp(HeadBrMI);
578 MachineInstr *TrueCmpMI = findAdjustableCmp(TrueBrMI);
579 if (!HeadCmpMI || !TrueCmpMI)
582 if (!registersMatch(HeadCmpMI, TrueCmpMI))
598 int HeadImmTrueValue = HeadImm;
599 int TrueImmTrueValue = TrueImm;
612 if (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri)
613 HeadImmTrueValue = -HeadImmTrueValue;
616 if (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri)
617 TrueImmTrueValue = -TrueImmTrueValue;
621 std::abs(TrueImmTrueValue - HeadImmTrueValue) == 2) {
632 CmpInfo HeadCmpInfo = adjustCmp(HeadCmpMI, HeadCmp);
633 CmpInfo TrueCmpInfo = adjustCmp(TrueCmpMI, TrueCmp);
634 if (std::get<0>(HeadCmpInfo) == std::get<0>(TrueCmpInfo) &&
635 std::get<1>(HeadCmpInfo) == std::get<1>(TrueCmpInfo)) {
636 modifyCmp(HeadCmpMI, HeadCmpInfo);
637 modifyCmp(TrueCmpMI, TrueCmpInfo);
642 std::abs(TrueImmTrueValue - HeadImmTrueValue) == 1) {
655 bool adjustHeadCond = (HeadImmTrueValue < TrueImmTrueValue);
657 adjustHeadCond = !adjustHeadCond;
660 if (adjustHeadCond) {
661 return adjustTo(HeadCmpMI, HeadCmp, TrueCmpMI, TrueImm);
663 return adjustTo(TrueCmpMI, TrueCmp, HeadCmpMI, HeadImm);
672bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
673 LLVM_DEBUG(
dbgs() <<
"********** AArch64 Conditional Compares **********\n"
674 <<
"********** Function: " << MF.
getName() <<
'\n');
680 DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
691 MachineBasicBlock *HBB =
I->getBlock();
692 Changed |= optimizeIntraBlock(*HBB);
693 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)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
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.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
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.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
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.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
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.
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
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.