36#define DEBUG_TYPE "aarch64-code-layout-opt"
37#define DBG(...) LLVM_DEBUG(dbgs() << DEBUG_TYPE ": " << __VA_ARGS__)
38#define AARCH64_CODE_LAYOUT_OPT_NAME "AArch64 Code Layout Optimization"
49 cl::desc(
"Enable code alignment optimization for instruction pairs"),
56 "Number of functions with aligned (to 64-bytes by default)");
58 "Number of CMP/CMN-CSEL pairs detected for alignment");
60 "Number of FCMP-FCSEL pairs detected for alignment");
68 void getAnalysisUsage(AnalysisUsage &AU)
const override;
70 StringRef getPassName()
const override {
75 const AArch64InstrInfo *TII =
nullptr;
80 bool alignLayoutSensitivePatterns(MachineBasicBlock *
MBB,
CodeLayoutOpt CLO);
83 void emitP2Align(MachineInstr &
MI, Align DesiredAlign,
84 unsigned MaxSkipBytes = 4);
91char AArch64CodeLayoutOpt::ID = 0;
96void AArch64CodeLayoutOpt::getAnalysisUsage(
AnalysisUsage &AU)
const {
102 return new AArch64CodeLayoutOpt();
108 case AArch64::FCMPSrr:
109 case AArch64::FCMPDrr:
110 case AArch64::FCMPESrr:
111 case AArch64::FCMPEDrr:
112 case AArch64::FCMPHrr:
113 case AArch64::FCMPEHrr:
123 case AArch64::FCSELSrrr:
124 case AArch64::FCSELDrrr:
125 case AArch64::FCSELHrrr:
137 switch (
MI.getOpcode()) {
138 case AArch64::SUBSWrr:
139 case AArch64::ADDSWrr:
140 return MI.definesRegister(AArch64::WZR,
nullptr);
141 case AArch64::SUBSWri:
142 case AArch64::ADDSWri:
143 return MI.definesRegister(AArch64::WZR,
nullptr) &&
144 MI.getOperand(3).getImm() == 0 &&
MI.getOperand(2).getImm() <= 15;
145 case AArch64::SUBSWrs:
146 case AArch64::ADDSWrs:
147 return MI.definesRegister(AArch64::WZR,
nullptr) &&
148 !AArch64InstrInfo::hasShiftedReg(
MI);
149 case AArch64::SUBSWrx:
150 return MI.definesRegister(AArch64::WZR,
nullptr) &&
151 !AArch64InstrInfo::hasExtendedReg(
MI);
163 const auto *Subtarget = &MF.
getSubtarget<AArch64Subtarget>();
166 if (Subtarget->isTargetWindows())
169 TII = Subtarget->getInstrInfo();
179 if (Subtarget->hasAlignCmpCSelPairs()) {
180 if (Subtarget->hasFuseCmpCSel())
182 if (Subtarget->hasFuseFCmpFCSel())
190 return optimizeForCodeLayout(MF, CLO);
193void AArch64CodeLayoutOpt::emitP2Align(MachineInstr &
MI, Align DesiredAlign,
194 unsigned MaxSkipBytes) {
199 if (&*FirstReal != &
MI) {
213bool AArch64CodeLayoutOpt::alignLayoutSensitivePatterns(MachineBasicBlock *
MBB,
226 NextIt->getOpcode() == AArch64::CSELWr) {
240 for (
auto &[
MI, IsCmpCsel] : Pairs) {
242 DBG(
".p2align 6, , 4 before " << *
MI);
243 ++(IsCmpCsel ? NumCmpCselPairsDetected : NumFcmpFcselPairsDetected);
246 return !Pairs.empty();
251 DBG(
"optimizeForCodeLayout: " << MF.
getName() <<
"\n");
255 Changed |= alignLayoutSensitivePatterns(&
MBB, CLO);
260 unsigned FunctionAlignBytes = MF.getSubtarget<AArch64Subtarget>()
262 .code_layout_opt_align_functions;
265 "aarch64-code-layout-opt-align-functions must be a power of 2");
266 if (MF.getAlignment() <
Align(FunctionAlignBytes)) {
267 MF.setAlignment(
Align(FunctionAlignBytes));
268 ++NumFunctionsAligned;
269 DBG(
"Set " << FunctionAlignBytes <<
"-byte alignment for function "
270 << MF.getName() <<
"\n");
272 DBG(
"Function " << MF.getName() <<
" already has sufficient alignment\n");
static bool isFloatingPointConditionalSelect(unsigned Opc)
#define AARCH64_CODE_LAYOUT_OPT_NAME
static cl::list< CodeLayoutOpt > EnableCodeAlignment("aarch64-code-layout-opt-enable", cl::Hidden, cl::CommaSeparated, cl::desc("Enable code alignment optimization for instruction pairs"), cl::values(clEnumValN(None, "none", "Disable the code alignment pass"), clEnumValN(CmpCsel, "cmp-csel", "CMP/CMN-CSEL pair alignment (32-bit)"), clEnumValN(FcmpFcsel, "fcmp-fcsel", "FCMP-FCSEL pair alignment")))
static bool isFloatingPointCompare(unsigned Opc)
static bool isQualifyingIntCompare(const MachineInstr &MI)
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
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.
FunctionPass class - This class is used to implement most global optimizations.
instr_iterator instr_begin()
void setMaxBytesForAlignment(unsigned MaxBytes)
Set the maximum amount of padding allowed for aligning the basic block.
void setAlignment(Align A)
Set alignment of the basic block.
LLVM_ABI MachineBasicBlock * splitAt(MachineInstr &SplitInst, bool UpdateLiveIns=true, LiveIntervals *LIS=nullptr)
Split a basic block into 2 pieces at SplitPoint.
instr_iterator instr_end()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
void push_back(const T &Elt)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
This is an optimization pass for GlobalISel generic memory operations.
@ LLVM_MARK_AS_BITMASK_ENUM
IterT skipDebugInstructionsForward(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
FunctionPass * createAArch64CodeLayoutOptPass()
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 void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.