blob: d327cfa882704ef74f16e5c62cb501d428ade034 [file] [log] [blame]
Jim Grosbache27d2052009-12-02 19:30:24 +00001//===-- MaxStackAlignment.cpp - Compute the required stack alignment -- ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
13// necessary.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineRegisterInfo.h"
20#include "llvm/CodeGen/Passes.h"
21
22using namespace llvm;
23
24namespace {
25 struct MaximalStackAlignmentCalculator : public MachineFunctionPass {
26 static char ID;
27 MaximalStackAlignmentCalculator() : MachineFunctionPass(&ID) {}
28
29 virtual bool runOnMachineFunction(MachineFunction &MF) {
30 MachineFrameInfo *FFI = MF.getFrameInfo();
31 MachineRegisterInfo &RI = MF.getRegInfo();
32
33 // Calculate max stack alignment of all already allocated stack objects.
34 FFI->calculateMaxStackAlignment();
35 unsigned MaxAlign = FFI->getMaxAlignment();
36
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());
45
46 if (FFI->getMaxAlignment() == MaxAlign)
47 return false;
48
49 FFI->setMaxAlignment(MaxAlign);
50 return true;
51 }
52
53 virtual const char *getPassName() const {
54 return "Stack Alignment Requirements Auto-Detector";
55 }
56
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesCFG();
59 MachineFunctionPass::getAnalysisUsage(AU);
60 }
61 };
62
63 char MaximalStackAlignmentCalculator::ID = 0;
64}
65
66FunctionPass*
67llvm::createMaxStackAlignmentCalculatorPass() {
68 return new MaximalStackAlignmentCalculator();
69}
70