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 "aarch64-code-layout-opt-align-functions",
cl::Hidden,
57 cl::desc(
"Function alignment in bytes for code layout optimization "
58 "(must be a power of 2)"),
62 "Number of functions with aligned (to 64-bytes by default)");
64 "Number of CMP/CMN-CSEL pairs detected for alignment");
66 "Number of FCMP-FCSEL pairs detected for alignment");
74 void getAnalysisUsage(AnalysisUsage &AU)
const override;
76 StringRef getPassName()
const override {
81 const AArch64InstrInfo *TII =
nullptr;
86 bool alignLayoutSensitivePatterns(MachineBasicBlock *
MBB,
CodeLayoutOpt CLO);
89 void emitP2Align(MachineInstr &
MI, Align DesiredAlign,
90 unsigned MaxSkipBytes = 4);
97char AArch64CodeLayoutOpt::ID = 0;
102void AArch64CodeLayoutOpt::getAnalysisUsage(
AnalysisUsage &AU)
const {
103 AU.setPreservesAll();
108 return new AArch64CodeLayoutOpt();
114 case AArch64::FCMPSrr:
115 case AArch64::FCMPDrr:
116 case AArch64::FCMPESrr:
117 case AArch64::FCMPEDrr:
118 case AArch64::FCMPHrr:
119 case AArch64::FCMPEHrr:
129 case AArch64::FCSELSrrr:
130 case AArch64::FCSELDrrr:
131 case AArch64::FCSELHrrr:
143 switch (
MI.getOpcode()) {
144 case AArch64::SUBSWrr:
145 case AArch64::ADDSWrr:
146 return MI.definesRegister(AArch64::WZR,
nullptr);
147 case AArch64::SUBSWri:
148 case AArch64::ADDSWri:
149 return MI.definesRegister(AArch64::WZR,
nullptr) &&
150 MI.getOperand(3).getImm() == 0 &&
MI.getOperand(2).getImm() <= 15;
151 case AArch64::SUBSWrs:
152 case AArch64::ADDSWrs:
153 return MI.definesRegister(AArch64::WZR,
nullptr) &&
154 !AArch64InstrInfo::hasShiftedReg(
MI);
155 case AArch64::SUBSWrx:
156 return MI.definesRegister(AArch64::WZR,
nullptr) &&
157 !AArch64InstrInfo::hasExtendedReg(
MI);
169 const auto *Subtarget = &MF.
getSubtarget<AArch64Subtarget>();
172 if (Subtarget->isTargetWindows())
175 TII = Subtarget->getInstrInfo();
185 if (Subtarget->hasAlignCmpCSelPairs()) {
186 if (Subtarget->hasFuseCmpCSel())
188 if (Subtarget->hasFuseFCmpFCSel())
196 return optimizeForCodeLayout(MF, CLO);
199void AArch64CodeLayoutOpt::emitP2Align(MachineInstr &
MI, Align DesiredAlign,
200 unsigned MaxSkipBytes) {
205 if (&*FirstReal != &
MI) {
219bool AArch64CodeLayoutOpt::alignLayoutSensitivePatterns(MachineBasicBlock *
MBB,
232 NextIt->getOpcode() == AArch64::CSELWr) {
246 for (
auto &[
MI, IsCmpCsel] : Pairs) {
248 DBG(
".p2align 6, , 4 before " << *
MI);
249 ++(IsCmpCsel ? NumCmpCselPairsDetected : NumFcmpFcselPairsDetected);
252 return !Pairs.empty();
257 DBG(
"optimizeForCodeLayout: " << MF.
getName() <<
"\n");
261 Changed |= alignLayoutSensitivePatterns(&
MBB, CLO);
268 "aarch64-code-layout-opt-align-functions must be a power of 2");
271 ++NumFunctionsAligned;
273 << MF.getName() <<
"\n");
275 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)
static cl::opt< unsigned > FunctionAlignBytes("aarch64-code-layout-opt-align-functions", cl::Hidden, cl::desc("Function alignment in bytes for code layout optimization " "(must be a power of 2)"), cl::init(64))
#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 ...
initializer< Ty > init(const Ty &Val)
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.