llvm.org GIT mirror llvm / e27d205
Factor the stack alignment calculations out into a target independent pass. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@90336 91177308-0d34-0410-b5e6-96231b3b80d8 Jim Grosbach 10 years ago
9 changed file(s) with 93 addition(s) and 133 deletion(s). Raw diff Collapse all Expand all
326326 /// setMaxAlignment - Set the preferred alignment.
327327 ///
328328 void setMaxAlignment(unsigned Align) { MaxAlignment = Align; }
329
329
330 /// calculateMaxStackAlignment() - If there is a local object which requires
331 /// greater alignment than the current max alignment, adjust accordingly.
332 void calculateMaxStackAlignment() {
333 for (int i = getObjectIndexBegin(),
334 e = getObjectIndexEnd(); i != e; ++i) {
335 if (isDeadObjectIndex(i))
336 continue;
337
338 unsigned Align = getObjectAlignment(i);
339 MaxAlignment = std::max(MaxAlignment, Align);
340 }
341 }
342
330343 /// hasCalls - Return true if the current function has no function calls.
331344 /// This is only valid during or after prolog/epilog code emission.
332345 ///
190190 /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
191191 FunctionPass *createSjLjEHPass(const TargetLowering *tli);
192192
193 /// createMaxStackAlignmentCalculatorPass() - Determine the maximum required
194 /// alignment for a function.
195 FunctionPass* createMaxStackAlignmentCalculatorPass();
196
193197 } // End llvm namespace
194198
195199 #endif
0 //===-- MaxStackAlignment.cpp - Compute the required stack alignment -- ---===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass looks for vector register usage and aligned local objects to
10 // calculate the maximum required alignment for a function. This is used by
11 // targets which support it to determine if dynamic stack realignment is
12 // necessary.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/Passes.h"
20
21 using namespace llvm;
22
23 namespace {
24 struct MaximalStackAlignmentCalculator : public MachineFunctionPass {
25 static char ID;
26 MaximalStackAlignmentCalculator() : MachineFunctionPass(&ID) {}
27
28 virtual bool runOnMachineFunction(MachineFunction &MF) {
29 MachineFrameInfo *FFI = MF.getFrameInfo();
30 MachineRegisterInfo &RI = MF.getRegInfo();
31
32 // Calculate max stack alignment of all already allocated stack objects.
33 FFI->calculateMaxStackAlignment();
34 unsigned MaxAlign = FFI->getMaxAlignment();
35
36 // Be over-conservative: scan over all vreg defs and find whether vector
37 // registers are used. If yes, there is probability that vector registers
38 // will be spilled and thus the stack needs to be aligned properly.
39 // FIXME: It would be better to only do this if a spill actually
40 // happens rather than conseratively aligning the stack regardless.
41 for (unsigned RegNum = TargetRegisterInfo::FirstVirtualRegister;
42 RegNum < RI.getLastVirtReg(); ++RegNum)
43 MaxAlign = std::max(MaxAlign, RI.getRegClass(RegNum)->getAlignment());
44
45 if (FFI->getMaxAlignment() == MaxAlign)
46 return false;
47
48 FFI->setMaxAlignment(MaxAlign);
49 return true;
50 }
51
52 virtual const char *getPassName() const {
53 return "Stack Alignment Requirements Auto-Detector";
54 }
55
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesCFG();
58 MachineFunctionPass::getAnalysisUsage(AU);
59 }
60 };
61
62 char MaximalStackAlignmentCalculator::ID = 0;
63 }
64
65 FunctionPass*
66 llvm::createMaxStackAlignmentCalculatorPass() {
67 return new MaximalStackAlignmentCalculator();
68 }
69
108108 FunctionPass *createNEONMoveFixPass();
109109 FunctionPass *createThumb2ITBlockPass();
110110 FunctionPass *createThumb2SizeReductionPass();
111 FunctionPass *createARMMaxStackAlignmentCalculatorPass();
112111
113112 extern Target TheARMTarget, TheThumbTarget;
114113
470470 }
471471 }
472472
473 static unsigned calculateMaxStackAlignment(const MachineFrameInfo *FFI) {
474 unsigned MaxAlign = 0;
475
476 for (int i = FFI->getObjectIndexBegin(),
477 e = FFI->getObjectIndexEnd(); i != e; ++i) {
478 if (FFI->isDeadObjectIndex(i))
479 continue;
480
481 unsigned Align = FFI->getObjectAlignment(i);
482 MaxAlign = std::max(MaxAlign, Align);
483 }
484
485 return MaxAlign;
486 }
487
488473 /// hasFP - Return true if the specified function should have a dedicated frame
489474 /// pointer register. This is true if the function has variable sized allocas
490475 /// or if frame pointer elimination is disabled.
584569 SmallVector UnspilledCS2GPRs;
585570 ARMFunctionInfo *AFI = MF.getInfo();
586571
587 MachineFrameInfo *MFI = MF.getFrameInfo();
588572
589573 // Calculate and set max stack object alignment early, so we can decide
590574 // whether we will need stack realignment (and thus FP).
591575 if (RealignStack) {
592 unsigned MaxAlign = std::max(MFI->getMaxAlignment(),
593 calculateMaxStackAlignment(MFI));
594 MFI->setMaxAlignment(MaxAlign);
576 MachineFrameInfo *MFI = MF.getFrameInfo();
577 MFI->calculateMaxStackAlignment();
595578 }
596579
597580 // Don't spill FP if the frame can be eliminated. This is determined
14781461 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
14791462 }
14801463
1481 namespace {
1482 struct MaximalStackAlignmentCalculator : public MachineFunctionPass {
1483 static char ID;
1484 MaximalStackAlignmentCalculator() : MachineFunctionPass(&ID) {}
1485
1486 virtual bool runOnMachineFunction(MachineFunction &MF) {
1487 MachineFrameInfo *FFI = MF.getFrameInfo();
1488 MachineRegisterInfo &RI = MF.getRegInfo();
1489
1490 // Calculate max stack alignment of all already allocated stack objects.
1491 unsigned MaxAlign = calculateMaxStackAlignment(FFI);
1492
1493 // Be over-conservative: scan over all vreg defs and find, whether vector
1494 // registers are used. If yes - there is probability, that vector register
1495 // will be spilled and thus stack needs to be aligned properly.
1496 for (unsigned RegNum = TargetRegisterInfo::FirstVirtualRegister;
1497 RegNum < RI.getLastVirtReg(); ++RegNum)
1498 MaxAlign = std::max(MaxAlign, RI.getRegClass(RegNum)->getAlignment());
1499
1500 if (FFI->getMaxAlignment() == MaxAlign)
1501 return false;
1502
1503 FFI->setMaxAlignment(MaxAlign);
1504 return true;
1505 }
1506
1507 virtual const char *getPassName() const {
1508 return "ARM Stack Required Alignment Auto-Detector";
1509 }
1510
1511 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1512 AU.setPreservesCFG();
1513 MachineFunctionPass::getAnalysisUsage(AU);
1514 }
1515 };
1516
1517 char MaximalStackAlignmentCalculator::ID = 0;
1518 }
1519
1520 FunctionPass*
1521 llvm::createARMMaxStackAlignmentCalculatorPass() {
1522 return new MaximalStackAlignmentCalculator();
1523 }
1524
15251464 #include "ARMGenRegisterInfo.inc"
9494
9595 // Calculate and set max stack object alignment early, so we can decide
9696 // whether we will need stack realignment (and thus FP).
97 PM.add(createARMMaxStackAlignmentCalculatorPass());
97 PM.add(createMaxStackAlignmentCalculatorPass());
9898
9999 // FIXME: temporarily disabling load / store optimization pass for Thumb1.
100100 if (OptLevel != CodeGenOpt::None && !Subtarget.isThumb1Only())
6161 ///
6262 FunctionPass *createEmitX86CodeToMemory();
6363
64 /// createX86MaxStackAlignmentCalculatorPass - This function returns a pass
65 /// which calculates maximal stack alignment required for function
66 ///
67 FunctionPass *createX86MaxStackAlignmentCalculatorPass();
68
6964 extern Target TheX86_32Target, TheX86_64Target;
7065
7166 } // End llvm namespace
422422 // Stack Frame Processing methods
423423 //===----------------------------------------------------------------------===//
424424
425 static unsigned calculateMaxStackAlignment(const MachineFrameInfo *FFI) {
426 unsigned MaxAlign = 0;
427
428 for (int i = FFI->getObjectIndexBegin(),
429 e = FFI->getObjectIndexEnd(); i != e; ++i) {
430 if (FFI->isDeadObjectIndex(i))
431 continue;
432
433 unsigned Align = FFI->getObjectAlignment(i);
434 MaxAlign = std::max(MaxAlign, Align);
435 }
436
437 return MaxAlign;
438 }
439
440425 /// hasFP - Return true if the specified function should have a dedicated frame
441426 /// pointer register. This is true if the function has variable sized allocas
442427 /// or if frame pointer elimination is disabled.
637622
638623 // Calculate and set max stack object alignment early, so we can decide
639624 // whether we will need stack realignment (and thus FP).
640 unsigned MaxAlign = std::max(MFI->getMaxAlignment(),
641 calculateMaxStackAlignment(MFI));
642
643 MFI->setMaxAlignment(MaxAlign);
625 MFI->calculateMaxStackAlignment();
644626
645627 X86MachineFunctionInfo *X86FI = MF.getInfo();
646628 int32_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
14811463 }
14821464
14831465 #include "X86GenRegisterInfo.inc"
1484
1485 namespace {
1486 struct MSAC : public MachineFunctionPass {
1487 static char ID;
1488 MSAC() : MachineFunctionPass(&ID) {}
1489
1490 virtual bool runOnMachineFunction(MachineFunction &MF) {
1491 MachineFrameInfo *FFI = MF.getFrameInfo();
1492 MachineRegisterInfo &RI = MF.getRegInfo();
1493
1494 // Calculate max stack alignment of all already allocated stack objects.
1495 unsigned MaxAlign = calculateMaxStackAlignment(FFI);
1496
1497 // Be over-conservative: scan over all vreg defs and find, whether vector
1498 // registers are used. If yes - there is probability, that vector register
1499 // will be spilled and thus stack needs to be aligned properly.
1500 for (unsigned RegNum = TargetRegisterInfo::FirstVirtualRegister;
1501 RegNum < RI.getLastVirtReg(); ++RegNum)
1502 MaxAlign = std::max(MaxAlign, RI.getRegClass(RegNum)->getAlignment());
1503
1504 if (FFI->getMaxAlignment() == MaxAlign)
1505 return false;
1506
1507 FFI->setMaxAlignment(MaxAlign);
1508 return true;
1509 }
1510
1511 virtual const char *getPassName() const {
1512 return "X86 Maximal Stack Alignment Calculator";
1513 }
1514
1515 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1516 AU.setPreservesCFG();
1517 MachineFunctionPass::getAnalysisUsage(AU);
1518 }
1519 };
1520
1521 char MSAC::ID = 0;
1522 }
1523
1524 FunctionPass*
1525 llvm::createX86MaxStackAlignmentCalculatorPass() { return new MSAC(); }
162162 CodeGenOpt::Level OptLevel) {
163163 // Calculate and set max stack object alignment early, so we can decide
164164 // whether we will need stack realignment (and thus FP).
165 PM.add(createX86MaxStackAlignmentCalculatorPass());
165 PM.add(createMaxStackAlignmentCalculatorPass());
166166 return false; // -print-machineinstr shouldn't print after this.
167167 }
168168