88#define DEBUG_TYPE "aarch64-condopt"
90STATISTIC(NumConditionsAdjusted,
"Number of conditions adjusted");
102 using CmpInfo = std::tuple<int, unsigned, AArch64CC::CondCode>;
108 void getAnalysisUsage(AnalysisUsage &AU)
const override;
109 MachineInstr *findSuitableCompare(MachineBasicBlock *
MBB);
111 void modifyCmp(MachineInstr *CmpMI,
const CmpInfo &
Info);
114 bool runOnMachineFunction(MachineFunction &MF)
override;
116 StringRef getPassName()
const override {
117 return "AArch64 Condition Optimizer";
123char AArch64ConditionOptimizer::ID = 0;
126 "AArch64 CondOpt Pass",
false,
false)
132 return new AArch64ConditionOptimizer();
135void AArch64ConditionOptimizer::getAnalysisUsage(
AnalysisUsage &AU)
const {
144MachineInstr *AArch64ConditionOptimizer::findSuitableCompare(
145 MachineBasicBlock *
MBB) {
150 if (
Term->getOpcode() != AArch64::Bcc)
155 if (SuccBB->isLiveIn(AArch64::NZCV))
161 MachineInstr &
I = *It;
162 assert(!
I.isTerminator() &&
"Spurious terminator");
164 if (
I.readsRegister(AArch64::NZCV,
nullptr))
166 switch (
I.getOpcode()) {
168 case AArch64::SUBSWri:
169 case AArch64::SUBSXri:
171 case AArch64::ADDSWri:
172 case AArch64::ADDSXri: {
174 if (!
I.getOperand(2).isImm()) {
177 }
else if (
I.getOperand(2).getImm() << ShiftAmt >= 0xfff) {
181 }
else if (!
MRI->use_nodbg_empty(
I.getOperand(0).getReg())) {
193 case AArch64::FCMPDri:
194 case AArch64::FCMPSri:
195 case AArch64::FCMPESri:
196 case AArch64::FCMPEDri:
198 case AArch64::SUBSWrr:
199 case AArch64::SUBSXrr:
200 case AArch64::ADDSWrr:
201 case AArch64::ADDSXrr:
202 case AArch64::FCMPSrr:
203 case AArch64::FCMPDrr:
204 case AArch64::FCMPESrr:
205 case AArch64::FCMPEDrr:
218 case AArch64::ADDSWri:
return AArch64::SUBSWri;
219 case AArch64::ADDSXri:
return AArch64::SUBSXri;
220 case AArch64::SUBSWri:
return AArch64::ADDSWri;
221 case AArch64::SUBSXri:
return AArch64::ADDSXri;
241AArch64ConditionOptimizer::CmpInfo AArch64ConditionOptimizer::adjustCmp(
247 bool Negative = (
Opc == AArch64::ADDSWri ||
Opc == AArch64::ADDSXri);
252 Correction = -Correction;
256 const int NewImm = std::abs(OldImm + Correction);
260 if (OldImm == 0 && ((Negative && Correction == 1) ||
261 (!Negative && Correction == -1))) {
269void AArch64ConditionOptimizer::modifyCmp(MachineInstr *CmpMI,
270 const CmpInfo &
Info) {
274 std::tie(Imm,
Opc, Cmp) =
Info;
296 ++NumConditionsAdjusted;
305 assert(
Cond.size() == 1 &&
"Unknown Cond array format");
315bool AArch64ConditionOptimizer::adjustTo(MachineInstr *CmpMI,
318 CmpInfo
Info = adjustCmp(CmpMI, Cmp);
320 modifyCmp(CmpMI,
Info);
326bool AArch64ConditionOptimizer::runOnMachineFunction(MachineFunction &MF) {
327 LLVM_DEBUG(
dbgs() <<
"********** AArch64 Conditional Compares **********\n"
328 <<
"********** Function: " << MF.
getName() <<
'\n');
333 DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
344 MachineBasicBlock *HBB =
I->getBlock();
347 MachineBasicBlock *
TBB =
nullptr, *FBB =
nullptr;
358 MachineBasicBlock *TBB_TBB =
nullptr, *TBB_FBB =
nullptr;
363 MachineInstr *HeadCmpMI = findSuitableCompare(HBB);
368 MachineInstr *TrueCmpMI = findSuitableCompare(
TBB);
398 std::abs(TrueImm - HeadImm) == 2) {
409 CmpInfo HeadCmpInfo = adjustCmp(HeadCmpMI, HeadCmp);
410 CmpInfo TrueCmpInfo = adjustCmp(TrueCmpMI, TrueCmp);
411 if (std::get<0>(HeadCmpInfo) == std::get<0>(TrueCmpInfo) &&
412 std::get<1>(HeadCmpInfo) == std::get<1>(TrueCmpInfo)) {
413 modifyCmp(HeadCmpMI, HeadCmpInfo);
414 modifyCmp(TrueCmpMI, TrueCmpInfo);
419 std::abs(TrueImm - HeadImm) == 1) {
432 bool adjustHeadCond = (HeadImm < TrueImm);
434 adjustHeadCond = !adjustHeadCond;
437 if (adjustHeadCond) {
438 Changed |= adjustTo(HeadCmpMI, HeadCmp, TrueCmpMI, TrueImm);
440 Changed |= adjustTo(TrueCmpMI, TrueCmp, HeadCmpMI, HeadImm);
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")
Analysis containing CSE Info
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
const HexagonInstrInfo * TII
#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.
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
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
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
#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.