1 //===-- MaxStackAlignment.cpp - Compute the required stack alignment -- ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass looks for vector register usage and aligned local objects to
11 // calculate the maximum required alignment for a function. This is used by
12 // targets which support it to determine if dynamic stack realignment is
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/Passes.h"
25 struct MaximalStackAlignmentCalculator : public MachineFunctionPass {
27 MaximalStackAlignmentCalculator() : MachineFunctionPass(&ID) {}
29 virtual bool runOnMachineFunction(MachineFunction &MF) {
30 MachineFrameInfo *FFI = MF.getFrameInfo();
31 MachineRegisterInfo &RI = MF.getRegInfo();
33 // Calculate max stack alignment of all already allocated stack objects.
34 FFI->calculateMaxStackAlignment();
35 unsigned MaxAlign = FFI->getMaxAlignment();
37 // Be over-conservative: scan over all vreg defs and find whether vector
38 // registers are used. If yes, there is probability that vector registers
39 // will be spilled and thus the stack needs to be aligned properly.
40 // FIXME: It would be better to only do this if a spill actually
41 // happens rather than conseratively aligning the stack regardless.
42 for (unsigned RegNum = TargetRegisterInfo::FirstVirtualRegister;
43 RegNum < RI.getLastVirtReg(); ++RegNum)
44 MaxAlign = std::max(MaxAlign, RI.getRegClass(RegNum)->getAlignment());
46 if (FFI->getMaxAlignment() == MaxAlign)
49 FFI->setMaxAlignment(MaxAlign);
53 virtual const char *getPassName() const {
54 return "Stack Alignment Requirements Auto-Detector";
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 MachineFunctionPass::getAnalysisUsage(AU);
63 char MaximalStackAlignmentCalculator::ID = 0;
67 llvm::createMaxStackAlignmentCalculatorPass() {
68 return new MaximalStackAlignmentCalculator();