Add simplified aliases for access to DCCR, ICCR, DEAR and ESR
[oota-llvm.git] / lib / Target / NVPTX / NVPTXPrologEpilogPass.cpp
1 //===-- NVPTXPrologEpilogPass.cpp - NVPTX prolog/epilog inserter ----------===//
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 file is a copy of the generic LLVM PrologEpilogInserter pass, modified
11 // to remove unneeded functionality and to handle virtual registers. Most code
12 // here is a copy of PrologEpilogInserter.cpp.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "NVPTX.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetFrameLowering.h"
24 #include "llvm/Target/TargetRegisterInfo.h"
25 #include "llvm/Target/TargetSubtargetInfo.h"
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "nvptx-prolog-epilog"
30
31 namespace {
32 class NVPTXPrologEpilogPass : public MachineFunctionPass {
33 public:
34   static char ID;
35   NVPTXPrologEpilogPass() : MachineFunctionPass(ID) {}
36
37   bool runOnMachineFunction(MachineFunction &MF) override;
38
39 private:
40   void calculateFrameObjectOffsets(MachineFunction &Fn);
41 };
42 }
43
44 MachineFunctionPass *llvm::createNVPTXPrologEpilogPass() {
45   return new NVPTXPrologEpilogPass();
46 }
47
48 char NVPTXPrologEpilogPass::ID = 0;
49
50 bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
51   const TargetMachine &TM = MF.getTarget();
52   const TargetFrameLowering &TFI = *TM.getSubtargetImpl()->getFrameLowering();
53   const TargetRegisterInfo &TRI = *TM.getSubtargetImpl()->getRegisterInfo();
54   bool Modified = false;
55
56   calculateFrameObjectOffsets(MF);
57
58   for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) {
59     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
60       MachineInstr *MI = I;
61       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
62         if (!MI->getOperand(i).isFI())
63           continue;
64         TRI.eliminateFrameIndex(MI, 0, i, nullptr);
65         Modified = true;
66       }
67     }
68   }
69
70   // Add function prolog/epilog
71   TFI.emitPrologue(MF);
72
73   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
74     // If last instruction is a return instruction, add an epilogue
75     if (!I->empty() && I->back().isReturn())
76       TFI.emitEpilogue(MF, *I);
77   }
78
79   return Modified;
80 }
81
82 /// AdjustStackOffset - Helper function used to adjust the stack frame offset.
83 static inline void
84 AdjustStackOffset(MachineFrameInfo *MFI, int FrameIdx,
85                   bool StackGrowsDown, int64_t &Offset,
86                   unsigned &MaxAlign) {
87   // If the stack grows down, add the object size to find the lowest address.
88   if (StackGrowsDown)
89     Offset += MFI->getObjectSize(FrameIdx);
90
91   unsigned Align = MFI->getObjectAlignment(FrameIdx);
92
93   // If the alignment of this object is greater than that of the stack, then
94   // increase the stack alignment to match.
95   MaxAlign = std::max(MaxAlign, Align);
96
97   // Adjust to alignment boundary.
98   Offset = (Offset + Align - 1) / Align * Align;
99
100   if (StackGrowsDown) {
101     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << -Offset << "]\n");
102     MFI->setObjectOffset(FrameIdx, -Offset); // Set the computed offset
103   } else {
104     DEBUG(dbgs() << "alloc FI(" << FrameIdx << ") at SP[" << Offset << "]\n");
105     MFI->setObjectOffset(FrameIdx, Offset);
106     Offset += MFI->getObjectSize(FrameIdx);
107   }
108 }
109
110 void
111 NVPTXPrologEpilogPass::calculateFrameObjectOffsets(MachineFunction &Fn) {
112   const TargetFrameLowering &TFI =
113       *Fn.getTarget().getSubtargetImpl()->getFrameLowering();
114   const TargetRegisterInfo *RegInfo =
115       Fn.getTarget().getSubtargetImpl()->getRegisterInfo();
116
117   bool StackGrowsDown =
118     TFI.getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
119
120   // Loop over all of the stack objects, assigning sequential addresses...
121   MachineFrameInfo *MFI = Fn.getFrameInfo();
122
123   // Start at the beginning of the local area.
124   // The Offset is the distance from the stack top in the direction
125   // of stack growth -- so it's always nonnegative.
126   int LocalAreaOffset = TFI.getOffsetOfLocalArea();
127   if (StackGrowsDown)
128     LocalAreaOffset = -LocalAreaOffset;
129   assert(LocalAreaOffset >= 0
130          && "Local area offset should be in direction of stack growth");
131   int64_t Offset = LocalAreaOffset;
132
133   // If there are fixed sized objects that are preallocated in the local area,
134   // non-fixed objects can't be allocated right at the start of local area.
135   // We currently don't support filling in holes in between fixed sized
136   // objects, so we adjust 'Offset' to point to the end of last fixed sized
137   // preallocated object.
138   for (int i = MFI->getObjectIndexBegin(); i != 0; ++i) {
139     int64_t FixedOff;
140     if (StackGrowsDown) {
141       // The maximum distance from the stack pointer is at lower address of
142       // the object -- which is given by offset. For down growing stack
143       // the offset is negative, so we negate the offset to get the distance.
144       FixedOff = -MFI->getObjectOffset(i);
145     } else {
146       // The maximum distance from the start pointer is at the upper
147       // address of the object.
148       FixedOff = MFI->getObjectOffset(i) + MFI->getObjectSize(i);
149     }
150     if (FixedOff > Offset) Offset = FixedOff;
151   }
152
153   // NOTE: We do not have a call stack
154
155   unsigned MaxAlign = MFI->getMaxAlignment();
156
157   // No scavenger
158
159   // FIXME: Once this is working, then enable flag will change to a target
160   // check for whether the frame is large enough to want to use virtual
161   // frame index registers. Functions which don't want/need this optimization
162   // will continue to use the existing code path.
163   if (MFI->getUseLocalStackAllocationBlock()) {
164     unsigned Align = MFI->getLocalFrameMaxAlign();
165
166     // Adjust to alignment boundary.
167     Offset = (Offset + Align - 1) / Align * Align;
168
169     DEBUG(dbgs() << "Local frame base offset: " << Offset << "\n");
170
171     // Resolve offsets for objects in the local block.
172     for (unsigned i = 0, e = MFI->getLocalFrameObjectCount(); i != e; ++i) {
173       std::pair<int, int64_t> Entry = MFI->getLocalFrameObjectMap(i);
174       int64_t FIOffset = (StackGrowsDown ? -Offset : Offset) + Entry.second;
175       DEBUG(dbgs() << "alloc FI(" << Entry.first << ") at SP[" <<
176             FIOffset << "]\n");
177       MFI->setObjectOffset(Entry.first, FIOffset);
178     }
179     // Allocate the local block
180     Offset += MFI->getLocalFrameSize();
181
182     MaxAlign = std::max(Align, MaxAlign);
183   }
184
185   // No stack protector
186
187   // Then assign frame offsets to stack objects that are not used to spill
188   // callee saved registers.
189   for (unsigned i = 0, e = MFI->getObjectIndexEnd(); i != e; ++i) {
190     if (MFI->isObjectPreAllocated(i) &&
191         MFI->getUseLocalStackAllocationBlock())
192       continue;
193     if (MFI->isDeadObjectIndex(i))
194       continue;
195
196     AdjustStackOffset(MFI, i, StackGrowsDown, Offset, MaxAlign);
197   }
198
199   // No scavenger
200
201   if (!TFI.targetHandlesStackFrameRounding()) {
202     // If we have reserved argument space for call sites in the function
203     // immediately on entry to the current function, count it as part of the
204     // overall stack size.
205     if (MFI->adjustsStack() && TFI.hasReservedCallFrame(Fn))
206       Offset += MFI->getMaxCallFrameSize();
207
208     // Round up the size to a multiple of the alignment.  If the function has
209     // any calls or alloca's, align to the target's StackAlignment value to
210     // ensure that the callee's frame or the alloca data is suitably aligned;
211     // otherwise, for leaf functions, align to the TransientStackAlignment
212     // value.
213     unsigned StackAlign;
214     if (MFI->adjustsStack() || MFI->hasVarSizedObjects() ||
215         (RegInfo->needsStackRealignment(Fn) && MFI->getObjectIndexEnd() != 0))
216       StackAlign = TFI.getStackAlignment();
217     else
218       StackAlign = TFI.getTransientStackAlignment();
219
220     // If the frame pointer is eliminated, all frame offsets will be relative to
221     // SP not FP. Align to MaxAlign so this works.
222     StackAlign = std::max(StackAlign, MaxAlign);
223     unsigned AlignMask = StackAlign - 1;
224     Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
225   }
226
227   // Update frame info to pretend that this is part of the stack...
228   int64_t StackSize = Offset - LocalAreaOffset;
229   MFI->setStackSize(StackSize);
230 }