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 MachineInstr *findSuitableCompare(MachineBasicBlock *
MBB);
120 void modifyCmp(MachineInstr *CmpMI,
const CmpInfo &Info);
123 bool isPureCmp(MachineInstr &CmpMI);
124 bool optimizeIntraBlock(MachineBasicBlock &
MBB);
125 bool optimizeCrossBlock(MachineBasicBlock &HBB);
126 bool runOnMachineFunction(MachineFunction &MF)
override;
128 StringRef getPassName()
const override {
129 return "AArch64 Condition Optimizer";
135char AArch64ConditionOptimizer::ID = 0;
138 "AArch64 CondOpt Pass",
false,
false)
144 return new AArch64ConditionOptimizer();
147void AArch64ConditionOptimizer::getAnalysisUsage(
AnalysisUsage &AU)
const {
156MachineInstr *AArch64ConditionOptimizer::findSuitableCompare(
157 MachineBasicBlock *
MBB) {
162 if (
Term->getOpcode() != AArch64::Bcc)
167 if (SuccBB->isLiveIn(AArch64::NZCV))
173 MachineInstr &
I = *It;
174 assert(!
I.isTerminator() &&
"Spurious terminator");
176 if (
I.readsRegister(AArch64::NZCV,
nullptr))
178 switch (
I.getOpcode()) {
180 case AArch64::SUBSWri:
181 case AArch64::SUBSXri:
183 case AArch64::ADDSWri:
184 case AArch64::ADDSXri: {
186 if (!
I.getOperand(2).isImm()) {
189 }
else if (
I.getOperand(2).getImm() << ShiftAmt >= 0xfff) {
193 }
else if (!
MRI->use_nodbg_empty(
I.getOperand(0).getReg())) {
200 if (
I.modifiesRegister(AArch64::NZCV,
nullptr))
211 case AArch64::ADDSWri:
return AArch64::SUBSWri;
212 case AArch64::ADDSXri:
return AArch64::SUBSXri;
213 case AArch64::SUBSWri:
return AArch64::ADDSWri;
214 case AArch64::SUBSXri:
return AArch64::ADDSXri;
234AArch64ConditionOptimizer::CmpInfo AArch64ConditionOptimizer::adjustCmp(
240 bool Negative = (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri);
245 Correction = -Correction;
249 const int NewImm = std::abs(OldImm + Correction);
253 if (OldImm == 0 && ((Negative && Correction == 1) ||
254 (!Negative && Correction == -1))) {
262void AArch64ConditionOptimizer::modifyCmp(MachineInstr *CmpMI,
263 const CmpInfo &Info) {
267 std::tie(Imm,
Opc, Cmp) =
Info;
289 ++NumConditionsAdjusted;
298 assert(
Cond.size() == 1 &&
"Unknown Cond array format");
308bool AArch64ConditionOptimizer::adjustTo(MachineInstr *CmpMI,
311 CmpInfo
Info = adjustCmp(CmpMI, Cmp);
312 if (std::get<0>(Info) == ToImm && std::get<1>(Info) == To->
getOpcode()) {
313 modifyCmp(CmpMI, Info);
319bool AArch64ConditionOptimizer::isPureCmp(MachineInstr &CmpMI) {
322 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp is symbolic, " << CmpMI <<
'\n');
325 LLVM_DEBUG(
dbgs() <<
"Immediate of cmp may be out of range, " << CmpMI
329 LLVM_DEBUG(
dbgs() <<
"Destination of cmp is not dead, " << CmpMI <<
'\n');
353bool AArch64ConditionOptimizer::optimizeIntraBlock(MachineBasicBlock &
MBB) {
354 MachineInstr *FirstCmp =
nullptr;
355 MachineInstr *FirstCSINC =
nullptr;
356 MachineInstr *SecondCmp =
nullptr;
357 MachineInstr *SecondCSINC =
nullptr;
360 for (MachineInstr &
MI :
MBB) {
361 switch (
MI.getOpcode()) {
363 case AArch64::SUBSWri:
364 case AArch64::SUBSXri:
366 case AArch64::ADDSWri:
367 case AArch64::ADDSXri: {
370 }
else if (FirstCSINC && !SecondCmp) {
376 case AArch64::CSINCWr:
377 case AArch64::CSINCXr: {
379 if (FirstCmp && !FirstCSINC) {
381 }
else if (SecondCmp && !SecondCSINC) {
392 if (!SecondCmp || !SecondCSINC) {
402 if (!isPureCmp(*FirstCmp) || !isPureCmp(*SecondCmp)) {
411 if (&*It != SecondCmp &&
412 It->modifiesRegister(AArch64::NZCV,
nullptr)) {
413 LLVM_DEBUG(
dbgs() <<
"Flags modified between CMPs by: " << *It <<
'\n');
421 if (It->readsRegister(AArch64::NZCV,
nullptr)) {
422 LLVM_DEBUG(
dbgs() <<
"Flags read after second CSINC by: " << *It <<
'\n');
429 if (SuccBB->isLiveIn(AArch64::NZCV))
444 << SecondImm <<
'\n');
449 std::abs(SecondImm - FirstImm) == 1) {
453 bool adjustFirst = (FirstImm < SecondImm);
455 adjustFirst = !adjustFirst;
458 MachineInstr *CmpToAdjust = adjustFirst ? FirstCmp : SecondCmp;
459 MachineInstr *CSINCToAdjust = adjustFirst ? FirstCSINC : SecondCSINC;
461 int TargetImm = adjustFirst ? SecondImm : FirstImm;
463 CmpInfo AdjustedInfo = adjustCmp(CmpToAdjust, CondToAdjust);
465 if (std::get<0>(AdjustedInfo) == TargetImm &&
466 std::get<1>(AdjustedInfo) ==
467 (adjustFirst ? SecondCmp : FirstCmp)->
getOpcode()) {
468 LLVM_DEBUG(
dbgs() <<
"Successfully optimizing intra-block CSINC pair\n");
472 CmpToAdjust->
setDesc(
TII->get(std::get<1>(AdjustedInfo)));
483bool AArch64ConditionOptimizer::optimizeCrossBlock(MachineBasicBlock &HBB) {
485 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
491 if (!
TBB ||
TBB == &HBB) {
496 MachineBasicBlock *TBB_TBB =
nullptr, *TBB_FBB =
nullptr;
501 MachineInstr *HeadCmpMI = findSuitableCompare(&HBB);
506 MachineInstr *TrueCmpMI = findSuitableCompare(
TBB);
518 if (HeadCmpReg != TrueCmpReg) {
548 std::abs(TrueImm - HeadImm) == 2) {
559 CmpInfo HeadCmpInfo = adjustCmp(HeadCmpMI, HeadCmp);
560 CmpInfo TrueCmpInfo = adjustCmp(TrueCmpMI, TrueCmp);
561 if (std::get<0>(HeadCmpInfo) == std::get<0>(TrueCmpInfo) &&
562 std::get<1>(HeadCmpInfo) == std::get<1>(TrueCmpInfo)) {
563 modifyCmp(HeadCmpMI, HeadCmpInfo);
564 modifyCmp(TrueCmpMI, TrueCmpInfo);
569 std::abs(TrueImm - HeadImm) == 1) {
582 bool adjustHeadCond = (HeadImm < TrueImm);
584 adjustHeadCond = !adjustHeadCond;
587 if (adjustHeadCond) {
588 return adjustTo(HeadCmpMI, HeadCmp, TrueCmpMI, TrueImm);
590 return adjustTo(TrueCmpMI, TrueCmp, HeadCmpMI, HeadImm);
599bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
600 LLVM_DEBUG(
dbgs() <<
"********** AArch64 Conditional Compares **********\n"
601 <<
"********** Function: " << MF.
getName() <<
'\n');
607 DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
618 MachineBasicBlock *HBB =
I->getBlock();
619 Changed |= optimizeIntraBlock(*HBB);
620 Changed |= optimizeCrossBlock(*HBB);
unsigned const MachineRegisterInfo * MRI
static int getComplementOpc(int Opc)
static AArch64CC::CondCode getAdjustedCmp(AArch64CC::CondCode Cmp)
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.