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 *findSuitableCompare(MachineBasicBlock *
MBB);
123 void modifyCmp(MachineInstr *CmpMI,
const CmpInfo &Info);
126 bool optimizeIntraBlock(MachineBasicBlock &
MBB);
127 bool optimizeCrossBlock(MachineBasicBlock &HBB);
128 bool runOnMachineFunction(MachineFunction &MF)
override;
130 StringRef getPassName()
const override {
131 return "AArch64 Condition Optimizer";
137char AArch64ConditionOptimizer::ID = 0;
140 "AArch64 CondOpt Pass",
false,
false)
146 return new AArch64ConditionOptimizer();
149void AArch64ConditionOptimizer::getAnalysisUsage(
AnalysisUsage &AU)
const {
157bool AArch64ConditionOptimizer::canAdjustCmp(MachineInstr &CmpMI) {
160 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp is symbolic, " << CmpMI <<
'\n');
163 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp may be out of range, " << CmpMI
167 LLVM_DEBUG(
dbgs() <<
"Destination of cmp is not dead, " << CmpMI <<
'\n');
175bool AArch64ConditionOptimizer::registersMatch(MachineInstr *FirstMI,
176 MachineInstr *SecondMI) {
180 FirstReg.
isVirtual() ?
TRI->lookThruCopyLike(FirstReg,
MRI) : FirstReg;
182 SecondReg.
isVirtual() ?
TRI->lookThruCopyLike(SecondReg,
MRI) : SecondReg;
183 if (FirstCmpReg != SecondCmpReg) {
192bool AArch64ConditionOptimizer::nzcvLivesOut(MachineBasicBlock *
MBB) {
194 if (SuccBB->isLiveIn(AArch64::NZCV)) {
208 case AArch64::SUBSWri:
209 case AArch64::SUBSXri:
211 case AArch64::ADDSWri:
212 case AArch64::ADDSXri:
220 return Opc == AArch64::CSINCWr ||
Opc == AArch64::CSINCXr;
226MachineInstr *AArch64ConditionOptimizer::findSuitableCompare(
227 MachineBasicBlock *
MBB) {
232 if (
Term->getOpcode() != AArch64::Bcc)
236 if (nzcvLivesOut(
MBB))
242 MachineInstr &
I = *It;
243 assert(!
I.isTerminator() &&
"Spurious terminator");
245 if (
I.readsRegister(AArch64::NZCV,
nullptr))
249 if (!canAdjustCmp(
I)) {
255 if (
I.modifiesRegister(AArch64::NZCV,
nullptr))
266 case AArch64::ADDSWri:
return AArch64::SUBSWri;
267 case AArch64::ADDSXri:
return AArch64::SUBSXri;
268 case AArch64::SUBSWri:
return AArch64::ADDSWri;
269 case AArch64::SUBSXri:
return AArch64::ADDSXri;
301AArch64ConditionOptimizer::CmpInfo
302AArch64ConditionOptimizer::adjustCmp(MachineInstr *CmpMI,
311 bool Negative = (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri);
316 Correction = -Correction;
320 const int NewImm = std::abs(OldImm + Correction);
324 if (OldImm == 0 && Negative)
325 return CmpInfo(OldImm,
Opc, Cmp);
327 if ((OldImm == 1 && Negative && Correction == -1) ||
328 (OldImm == 0 && Correction == -1)) {
333 return CmpInfo(OldImm,
Opc, Cmp);
341void AArch64ConditionOptimizer::modifyCmp(MachineInstr *CmpMI,
342 const CmpInfo &Info) {
346 std::tie(Imm,
Opc, Cmp) =
Info;
368 ++NumConditionsAdjusted;
377 assert(
Cond.size() == 1 &&
"Unknown Cond array format");
387bool AArch64ConditionOptimizer::adjustTo(MachineInstr *CmpMI,
390 CmpInfo
Info = adjustCmp(CmpMI, Cmp);
391 if (std::get<0>(Info) == ToImm && std::get<1>(Info) == To->
getOpcode()) {
392 modifyCmp(CmpMI, Info);
423bool AArch64ConditionOptimizer::optimizeIntraBlock(MachineBasicBlock &
MBB) {
424 MachineInstr *FirstCmp =
nullptr;
425 MachineInstr *FirstCSINC =
nullptr;
426 MachineInstr *SecondCmp =
nullptr;
427 MachineInstr *SecondCSINC =
nullptr;
430 for (MachineInstr &
MI :
MBB) {
434 }
else if (FirstCSINC && !SecondCmp) {
442 if (FirstCmp && !FirstCSINC) {
444 }
else if (SecondCmp && !SecondCSINC) {
453 if (!SecondCmp || !SecondCSINC) {
459 if (nzcvLivesOut(&
MBB))
462 if (!registersMatch(FirstCmp, SecondCmp))
465 if (!canAdjustCmp(*FirstCmp) || !canAdjustCmp(*SecondCmp)) {
474 if (&*It != SecondCmp &&
475 It->modifiesRegister(AArch64::NZCV,
nullptr)) {
476 LLVM_DEBUG(
dbgs() <<
"Flags modified between CMPs by: " << *It <<
'\n');
484 if (It->readsRegister(AArch64::NZCV,
nullptr)) {
485 LLVM_DEBUG(
dbgs() <<
"Flags read after second CSINC by: " << *It <<
'\n');
502 << SecondImm <<
'\n');
506 if (FirstCond == SecondCond &&
508 std::abs(SecondImm - FirstImm) == 1) {
512 bool adjustFirst = (FirstImm < SecondImm);
514 adjustFirst = !adjustFirst;
517 MachineInstr *CmpToAdjust = adjustFirst ? FirstCmp : SecondCmp;
518 MachineInstr *CSINCToAdjust = adjustFirst ? FirstCSINC : SecondCSINC;
520 int TargetImm = adjustFirst ? SecondImm : FirstImm;
522 CmpInfo AdjustedInfo = adjustCmp(CmpToAdjust, CondToAdjust);
524 if (std::get<0>(AdjustedInfo) == TargetImm &&
525 std::get<1>(AdjustedInfo) ==
526 (adjustFirst ? SecondCmp : FirstCmp)->
getOpcode()) {
527 LLVM_DEBUG(
dbgs() <<
"Successfully optimizing intra-block CSINC pair\n");
531 CmpToAdjust->
setDesc(
TII->get(std::get<1>(AdjustedInfo)));
542bool AArch64ConditionOptimizer::optimizeCrossBlock(MachineBasicBlock &HBB) {
544 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
550 if (!
TBB ||
TBB == &HBB) {
555 MachineBasicBlock *TBB_TBB =
nullptr, *TBB_FBB =
nullptr;
560 MachineInstr *HeadCmpMI = findSuitableCompare(&HBB);
565 MachineInstr *TrueCmpMI = findSuitableCompare(
TBB);
570 if (!registersMatch(HeadCmpMI, TrueCmpMI)) {
587 int HeadImmTrueValue = HeadImm;
588 int TrueImmTrueValue = TrueImm;
601 if (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri)
602 HeadImmTrueValue = -HeadImmTrueValue;
605 if (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri)
606 TrueImmTrueValue = -TrueImmTrueValue;
610 std::abs(TrueImmTrueValue - HeadImmTrueValue) == 2) {
621 CmpInfo HeadCmpInfo = adjustCmp(HeadCmpMI, HeadCmp);
622 CmpInfo TrueCmpInfo = adjustCmp(TrueCmpMI, TrueCmp);
623 if (std::get<0>(HeadCmpInfo) == std::get<0>(TrueCmpInfo) &&
624 std::get<1>(HeadCmpInfo) == std::get<1>(TrueCmpInfo)) {
625 modifyCmp(HeadCmpMI, HeadCmpInfo);
626 modifyCmp(TrueCmpMI, TrueCmpInfo);
631 std::abs(TrueImmTrueValue - HeadImmTrueValue) == 1) {
644 bool adjustHeadCond = (HeadImmTrueValue < TrueImmTrueValue);
646 adjustHeadCond = !adjustHeadCond;
649 if (adjustHeadCond) {
650 return adjustTo(HeadCmpMI, HeadCmp, TrueCmpMI, TrueImm);
652 return adjustTo(TrueCmpMI, TrueCmp, HeadCmpMI, HeadImm);
661bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
662 LLVM_DEBUG(
dbgs() <<
"********** AArch64 Conditional Compares **********\n"
663 <<
"********** Function: " << MF.
getName() <<
'\n');
669 DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
680 MachineBasicBlock *HBB =
I->getBlock();
681 Changed |= optimizeIntraBlock(*HBB);
682 Changed |= optimizeCrossBlock(*HBB);
unsigned const MachineRegisterInfo * MRI
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,...
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.