44#define DEBUG_TYPE "aarch64-cond-br-tuning"
45#define AARCH64_CONDBR_TUNING_NAME "AArch64 Conditional Branch Tuning"
70char AArch64CondBrTuning::ID = 0;
75void AArch64CondBrTuning::getAnalysisUsage(
AnalysisUsage &AU)
const {
86MachineInstr *AArch64CondBrTuning::tryConvertToFlagSetting(MachineInstr &
MI,
92 if (
any_of(
MI.operands(), [](
const MachineOperand &
Op) { return Op.isFI(); }))
98 for (MachineOperand &MO :
MI.implicit_operands())
103 unsigned NewOpc =
TII->convertToFlagSettingOpc(
MI.getOpcode());
104 Register NewDestReg =
MI.getOperand(0).getReg();
106 NewDestReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
108 MachineInstrBuilder MIB =
BuildMI(*
MI.getParent(),
MI,
MI.getDebugLoc(),
109 TII->get(NewOpc), NewDestReg);
113 if (
MI.peekDebugInstrNum() != 0)
122MachineInstr *AArch64CondBrTuning::convertToCondBr(MachineInstr &
MI) {
124 MachineBasicBlock *TargetMBB =
TII->getBranchDestBlock(
MI);
125 switch (
MI.getOpcode()) {
146 return BuildMI(*
MI.getParent(),
MI,
MI.getDebugLoc(),
TII->get(AArch64::Bcc))
151bool AArch64CondBrTuning::tryToTuneBranch(MachineInstr &
MI,
152 MachineInstr &
DefMI) {
154 if (
MI.getParent() !=
DefMI.getParent())
157 bool IsFlagSetting =
true;
158 unsigned MIOpc =
MI.getOpcode();
159 MachineInstr *NewCmp =
nullptr, *NewBr =
nullptr;
160 switch (
DefMI.getOpcode()) {
163 case AArch64::ADDWri:
164 case AArch64::ADDWrr:
165 case AArch64::ADDWrs:
166 case AArch64::ADDWrx:
167 case AArch64::ANDWri:
168 case AArch64::ANDWrr:
169 case AArch64::ANDWrs:
170 case AArch64::BICWrr:
171 case AArch64::BICWrs:
172 case AArch64::SUBWri:
173 case AArch64::SUBWrr:
174 case AArch64::SUBWrs:
175 case AArch64::SUBWrx:
176 IsFlagSetting =
false;
178 case AArch64::ADDSWri:
179 case AArch64::ADDSWrr:
180 case AArch64::ADDSWrs:
181 case AArch64::ADDSWrx:
182 case AArch64::ANDSWri:
183 case AArch64::ANDSWrr:
184 case AArch64::ANDSWrs:
185 case AArch64::BICSWrr:
186 case AArch64::BICSWrs:
187 case AArch64::SUBSWri:
188 case AArch64::SUBSWrr:
189 case AArch64::SUBSWrs:
190 case AArch64::SUBSWrx:
200 if ((MIOpc == AArch64::TBZW || MIOpc == AArch64::TBNZW) &&
201 MI.getOperand(1).getImm() != 31)
209 NewCmp = tryConvertToFlagSetting(
DefMI, IsFlagSetting,
false);
218 NewBr = convertToCondBr(
MI);
223 case AArch64::ADDXri:
224 case AArch64::ADDXrr:
225 case AArch64::ADDXrs:
226 case AArch64::ADDXrx:
227 case AArch64::ANDXri:
228 case AArch64::ANDXrr:
229 case AArch64::ANDXrs:
230 case AArch64::BICXrr:
231 case AArch64::BICXrs:
232 case AArch64::SUBXri:
233 case AArch64::SUBXrr:
234 case AArch64::SUBXrs:
235 case AArch64::SUBXrx:
236 IsFlagSetting =
false;
238 case AArch64::ADDSXri:
239 case AArch64::ADDSXrr:
240 case AArch64::ADDSXrs:
241 case AArch64::ADDSXrx:
242 case AArch64::ANDSXri:
243 case AArch64::ANDSXrr:
244 case AArch64::ANDSXrs:
245 case AArch64::BICSXrr:
246 case AArch64::BICSXrs:
247 case AArch64::SUBSXri:
248 case AArch64::SUBSXrr:
249 case AArch64::SUBSXrs:
250 case AArch64::SUBSXrx:
258 case AArch64::TBNZX: {
260 if ((MIOpc == AArch64::TBZX || MIOpc == AArch64::TBNZX) &&
261 MI.getOperand(1).getImm() != 63)
268 NewCmp = tryConvertToFlagSetting(
DefMI, IsFlagSetting,
true);
277 NewBr = convertToCondBr(
MI);
283 (void)NewCmp; (void)NewBr;
284 assert(NewCmp && NewBr &&
"Expected new instructions.");
295 DefMI.eraseFromParent();
296 MI.eraseFromParent();
305 dbgs() <<
"********** AArch64 Conditional Branch Tuning **********\n"
306 <<
"********** Function: " << MF.
getName() <<
'\n');
308 TII =
static_cast<const AArch64InstrInfo *
>(MF.
getSubtarget().getInstrInfo());
313 for (MachineBasicBlock &
MBB : MF) {
314 bool LocalChange =
false;
316 switch (
MI.getOpcode()) {
327 MachineInstr *
DefMI = getOperandDef(
MI.getOperand(0));
343 return new AArch64CondBrTuning();
#define AARCH64_CONDBR_TUNING_NAME
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< iterator > terminators()
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
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
void setDebugInstrNum(unsigned Num)
Set instruction number of this MachineInstr.
LLVM_ABI void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Represent a constant reference to a string, i.e.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createAArch64CondBrTuning()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
DWARFExpression::Operation Op
bool isNZCVTouchedInInstructionRange(const MachineInstr &DefMI, const MachineInstr &UseMI, const TargetRegisterInfo *TRI)
Return true if there is an instruction /after/ DefMI and before UseMI which either reads or clobbers ...