1 //===-- CodeGen/MachineFrameInfo.h - Abstract Stack Frame Rep. --*- C++ -*-===//
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 // The file defines the MachineFrameInfo class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEFRAMEINFO_H
15 #define LLVM_CODEGEN_MACHINEFRAMEINFO_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/System/DataTypes.h"
25 class TargetRegisterClass;
27 class MachineFunction;
28 class MachineBasicBlock;
29 class TargetFrameInfo;
32 /// The CalleeSavedInfo class tracks the information need to locate where a
33 /// callee saved register in the current frame.
34 class CalleeSavedInfo {
39 explicit CalleeSavedInfo(unsigned R, int FI = 0)
40 : Reg(R), FrameIdx(FI) {}
43 unsigned getReg() const { return Reg; }
44 int getFrameIdx() const { return FrameIdx; }
45 void setFrameIdx(int FI) { FrameIdx = FI; }
48 /// The MachineFrameInfo class represents an abstract stack frame until
49 /// prolog/epilog code is inserted. This class is key to allowing stack frame
50 /// representation optimizations, such as frame pointer elimination. It also
51 /// allows more mundane (but still important) optimizations, such as reordering
52 /// of abstract objects on the stack frame.
54 /// To support this, the class assigns unique integer identifiers to stack
55 /// objects requested clients. These identifiers are negative integers for
56 /// fixed stack objects (such as arguments passed on the stack) or nonnegative
57 /// for objects that may be reordered. Instructions which refer to stack
58 /// objects use a special MO_FrameIndex operand to represent these frame
61 /// Because this class keeps track of all references to the stack frame, it
62 /// knows when a variable sized object is allocated on the stack. This is the
63 /// sole condition which prevents frame pointer elimination, which is an
64 /// important optimization on register-poor architectures. Because original
65 /// variable sized alloca's in the source program are the only source of
66 /// variable sized stack objects, it is safe to decide whether there will be
67 /// any variable sized objects before all stack objects are known (for
68 /// example, register allocator spill code never needs variable sized
71 /// When prolog/epilog code emission is performed, the final stack frame is
72 /// built and the machine instructions are modified to refer to the actual
73 /// stack offsets of the object, eliminating all MO_FrameIndex operands from
76 /// @brief Abstract Stack Frame Information
77 class MachineFrameInfo {
79 // StackObject - Represent a single object allocated on the stack.
81 // SPOffset - The offset of this object from the stack pointer on entry to
82 // the function. This field has no meaning for a variable sized element.
85 // The size of this object on the stack. 0 means a variable sized object,
86 // ~0ULL means a dead object.
89 // Alignment - The required alignment of this stack slot.
92 // isImmutable - If true, the value of the stack object is set before
93 // entering the function and is not modified inside the function. By
94 // default, fixed objects are immutable unless marked otherwise.
97 // isSpillSlot - If true, the stack object is used as spill slot. It
98 // cannot alias any other memory objects.
101 StackObject(uint64_t Sz, unsigned Al, int64_t SP, bool IM, bool isSS)
102 : SPOffset(SP), Size(Sz), Alignment(Al), isImmutable(IM),
106 /// Objects - The list of stack objects allocated...
108 std::vector<StackObject> Objects;
110 /// NumFixedObjects - This contains the number of fixed objects contained on
111 /// the stack. Because fixed objects are stored at a negative index in the
112 /// Objects list, this is also the index to the 0th object in the list.
114 unsigned NumFixedObjects;
116 /// HasVarSizedObjects - This boolean keeps track of whether any variable
117 /// sized objects have been allocated yet.
119 bool HasVarSizedObjects;
121 /// FrameAddressTaken - This boolean keeps track of whether there is a call
122 /// to builtin \@llvm.frameaddress.
123 bool FrameAddressTaken;
125 /// ReturnAddressTaken - This boolean keeps track of whether there is a call
126 /// to builtin \@llvm.returnaddress.
127 bool ReturnAddressTaken;
129 /// StackSize - The prolog/epilog code inserter calculates the final stack
130 /// offsets for all of the fixed size objects, updating the Objects list
131 /// above. It then updates StackSize to contain the number of bytes that need
132 /// to be allocated on entry to the function.
136 /// OffsetAdjustment - The amount that a frame offset needs to be adjusted to
137 /// have the actual offset from the stack/frame pointer. The exact usage of
138 /// this is target-dependent, but it is typically used to adjust between
139 /// SP-relative and FP-relative offsets. E.G., if objects are accessed via
140 /// SP then OffsetAdjustment is zero; if FP is used, OffsetAdjustment is set
141 /// to the distance between the initial SP and the value in FP. For many
142 /// targets, this value is only used when generating debug info (via
143 /// TargetRegisterInfo::getFrameIndexOffset); when generating code, the
144 /// corresponding adjustments are performed directly.
145 int OffsetAdjustment;
147 /// MaxAlignment - The prolog/epilog code inserter may process objects
148 /// that require greater alignment than the default alignment the target
149 /// provides. To handle this, MaxAlignment is set to the maximum alignment
150 /// needed by the objects on the current frame. If this is greater than the
151 /// native alignment maintained by the compiler, dynamic alignment code will
154 unsigned MaxAlignment;
156 /// AdjustsStack - Set to true if this function adjusts the stack -- e.g.,
157 /// when calling another function. This is only valid during and after
158 /// prolog/epilog code insertion.
161 /// HasCalls - Set to true if this function has any function calls.
164 /// StackProtectorIdx - The frame index for the stack protector.
165 int StackProtectorIdx;
167 /// MaxCallFrameSize - This contains the size of the largest call frame if the
168 /// target uses frame setup/destroy pseudo instructions (as defined in the
169 /// TargetFrameInfo class). This information is important for frame pointer
170 /// elimination. If is only valid during and after prolog/epilog code
173 unsigned MaxCallFrameSize;
175 /// CSInfo - The prolog/epilog code inserter fills in this vector with each
176 /// callee saved register saved in the frame. Beyond its use by the prolog/
177 /// epilog code inserter, this data used for debug info and exception
179 std::vector<CalleeSavedInfo> CSInfo;
181 /// CSIValid - Has CSInfo been set yet?
184 /// SpillObjects - A vector indicating which frame indices refer to
186 SmallVector<bool, 8> SpillObjects;
188 /// TargetFrameInfo - Target information about frame layout.
190 const TargetFrameInfo &TFI;
193 explicit MachineFrameInfo(const TargetFrameInfo &tfi) : TFI(tfi) {
194 StackSize = NumFixedObjects = OffsetAdjustment = MaxAlignment = 0;
195 HasVarSizedObjects = false;
196 FrameAddressTaken = false;
197 ReturnAddressTaken = false;
198 AdjustsStack = false;
200 StackProtectorIdx = -1;
201 MaxCallFrameSize = 0;
205 /// hasStackObjects - Return true if there are any stack objects in this
208 bool hasStackObjects() const { return !Objects.empty(); }
210 /// hasVarSizedObjects - This method may be called any time after instruction
211 /// selection is complete to determine if the stack frame for this function
212 /// contains any variable sized objects.
214 bool hasVarSizedObjects() const { return HasVarSizedObjects; }
216 /// getStackProtectorIndex/setStackProtectorIndex - Return the index for the
217 /// stack protector object.
219 int getStackProtectorIndex() const { return StackProtectorIdx; }
220 void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
222 /// isFrameAddressTaken - This method may be called any time after instruction
223 /// selection is complete to determine if there is a call to
224 /// \@llvm.frameaddress in this function.
225 bool isFrameAddressTaken() const { return FrameAddressTaken; }
226 void setFrameAddressIsTaken(bool T) { FrameAddressTaken = T; }
228 /// isReturnAddressTaken - This method may be called any time after instruction
229 /// selection is complete to determine if there is a call to
230 /// \@llvm.returnaddress in this function.
231 bool isReturnAddressTaken() const { return ReturnAddressTaken; }
232 void setReturnAddressIsTaken(bool s) { ReturnAddressTaken = s; }
234 /// getObjectIndexBegin - Return the minimum frame object index.
236 int getObjectIndexBegin() const { return -NumFixedObjects; }
238 /// getObjectIndexEnd - Return one past the maximum frame object index.
240 int getObjectIndexEnd() const { return (int)Objects.size()-NumFixedObjects; }
242 /// getNumFixedObjects() - Return the number of fixed objects.
243 unsigned getNumFixedObjects() const { return NumFixedObjects; }
245 /// getNumObjects() - Return the number of objects.
247 unsigned getNumObjects() const { return Objects.size(); }
249 /// getObjectSize - Return the size of the specified object.
251 int64_t getObjectSize(int ObjectIdx) const {
252 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
253 "Invalid Object Idx!");
254 return Objects[ObjectIdx+NumFixedObjects].Size;
257 /// setObjectSize - Change the size of the specified stack object.
258 void setObjectSize(int ObjectIdx, int64_t Size) {
259 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
260 "Invalid Object Idx!");
261 Objects[ObjectIdx+NumFixedObjects].Size = Size;
264 /// getObjectAlignment - Return the alignment of the specified stack object.
265 unsigned getObjectAlignment(int ObjectIdx) const {
266 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
267 "Invalid Object Idx!");
268 return Objects[ObjectIdx+NumFixedObjects].Alignment;
271 /// setObjectAlignment - Change the alignment of the specified stack object.
272 void setObjectAlignment(int ObjectIdx, unsigned Align) {
273 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
274 "Invalid Object Idx!");
275 Objects[ObjectIdx+NumFixedObjects].Alignment = Align;
276 MaxAlignment = std::max(MaxAlignment, Align);
279 /// getObjectOffset - Return the assigned stack offset of the specified object
280 /// from the incoming stack pointer.
282 int64_t getObjectOffset(int ObjectIdx) const {
283 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
284 "Invalid Object Idx!");
285 assert(!isDeadObjectIndex(ObjectIdx) &&
286 "Getting frame offset for a dead object?");
287 return Objects[ObjectIdx+NumFixedObjects].SPOffset;
290 /// setObjectOffset - Set the stack frame offset of the specified object. The
291 /// offset is relative to the stack pointer on entry to the function.
293 void setObjectOffset(int ObjectIdx, int64_t SPOffset) {
294 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
295 "Invalid Object Idx!");
296 assert(!isDeadObjectIndex(ObjectIdx) &&
297 "Setting frame offset for a dead object?");
298 Objects[ObjectIdx+NumFixedObjects].SPOffset = SPOffset;
301 /// getStackSize - Return the number of bytes that must be allocated to hold
302 /// all of the fixed size frame objects. This is only valid after
303 /// Prolog/Epilog code insertion has finalized the stack frame layout.
305 uint64_t getStackSize() const { return StackSize; }
307 /// setStackSize - Set the size of the stack...
309 void setStackSize(uint64_t Size) { StackSize = Size; }
311 /// getOffsetAdjustment - Return the correction for frame offsets.
313 int getOffsetAdjustment() const { return OffsetAdjustment; }
315 /// setOffsetAdjustment - Set the correction for frame offsets.
317 void setOffsetAdjustment(int Adj) { OffsetAdjustment = Adj; }
319 /// getMaxAlignment - Return the alignment in bytes that this function must be
320 /// aligned to, which is greater than the default stack alignment provided by
323 unsigned getMaxAlignment() const { return MaxAlignment; }
325 /// setMaxAlignment - Set the preferred alignment.
327 void setMaxAlignment(unsigned Align) { MaxAlignment = Align; }
329 /// AdjustsStack - Return true if this function adjusts the stack -- e.g.,
330 /// when calling another function. This is only valid during and after
331 /// prolog/epilog code insertion.
332 bool adjustsStack() const { return AdjustsStack; }
333 void setAdjustsStack(bool V) { AdjustsStack = V; }
335 /// hasCalls - Return true if the current function has any function calls.
336 bool hasCalls() const { return HasCalls; }
337 void setHasCalls(bool V) { HasCalls = V; }
339 /// getMaxCallFrameSize - Return the maximum size of a call frame that must be
340 /// allocated for an outgoing function call. This is only available if
341 /// CallFrameSetup/Destroy pseudo instructions are used by the target, and
342 /// then only during or after prolog/epilog code insertion.
344 unsigned getMaxCallFrameSize() const { return MaxCallFrameSize; }
345 void setMaxCallFrameSize(unsigned S) { MaxCallFrameSize = S; }
347 /// CreateFixedObject - Create a new object at a fixed location on the stack.
348 /// All fixed objects should be created before other objects are created for
349 /// efficiency. By default, fixed objects are immutable. This returns an
350 /// index with a negative value.
352 int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool Immutable);
355 /// isFixedObjectIndex - Returns true if the specified index corresponds to a
356 /// fixed stack object.
357 bool isFixedObjectIndex(int ObjectIdx) const {
358 return ObjectIdx < 0 && (ObjectIdx >= -(int)NumFixedObjects);
361 /// isImmutableObjectIndex - Returns true if the specified index corresponds
362 /// to an immutable object.
363 bool isImmutableObjectIndex(int ObjectIdx) const {
364 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
365 "Invalid Object Idx!");
366 return Objects[ObjectIdx+NumFixedObjects].isImmutable;
369 /// isSpillSlotObjectIndex - Returns true if the specified index corresponds
370 /// to a spill slot..
371 bool isSpillSlotObjectIndex(int ObjectIdx) const {
372 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
373 "Invalid Object Idx!");
374 return Objects[ObjectIdx+NumFixedObjects].isSpillSlot;;
377 /// isDeadObjectIndex - Returns true if the specified index corresponds to
379 bool isDeadObjectIndex(int ObjectIdx) const {
380 assert(unsigned(ObjectIdx+NumFixedObjects) < Objects.size() &&
381 "Invalid Object Idx!");
382 return Objects[ObjectIdx+NumFixedObjects].Size == ~0ULL;
385 /// CreateStackObject - Create a new statically sized stack object,
386 /// returning a nonnegative identifier to represent it.
388 int CreateStackObject(uint64_t Size, unsigned Alignment, bool isSS) {
389 assert(Size != 0 && "Cannot allocate zero size stack objects!");
390 Objects.push_back(StackObject(Size, Alignment, 0, false, isSS));
391 int Index = (int)Objects.size()-NumFixedObjects-1;
392 assert(Index >= 0 && "Bad frame index!");
393 MaxAlignment = std::max(MaxAlignment, Alignment);
397 /// CreateSpillStackObject - Create a new statically sized stack
398 /// object that represents a spill slot, returning a nonnegative
399 /// identifier to represent it.
401 int CreateSpillStackObject(uint64_t Size, unsigned Alignment) {
402 CreateStackObject(Size, Alignment, true);
403 int Index = (int)Objects.size()-NumFixedObjects-1;
404 MaxAlignment = std::max(MaxAlignment, Alignment);
408 /// RemoveStackObject - Remove or mark dead a statically sized stack object.
410 void RemoveStackObject(int ObjectIdx) {
412 Objects[ObjectIdx+NumFixedObjects].Size = ~0ULL;
415 /// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
416 /// variable sized object has been created. This must be created whenever a
417 /// variable sized object is created, whether or not the index returned is
420 int CreateVariableSizedObject() {
421 HasVarSizedObjects = true;
422 Objects.push_back(StackObject(0, 1, 0, false, false));
423 return (int)Objects.size()-NumFixedObjects-1;
426 /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
427 /// current function.
428 const std::vector<CalleeSavedInfo> &getCalleeSavedInfo() const {
432 /// setCalleeSavedInfo - Used by prolog/epilog inserter to set the function's
433 /// callee saved information.
434 void setCalleeSavedInfo(const std::vector<CalleeSavedInfo> &CSI) {
438 /// isCalleeSavedInfoValid - Has the callee saved info been calculated yet?
439 bool isCalleeSavedInfoValid() const { return CSIValid; }
441 void setCalleeSavedInfoValid(bool v) { CSIValid = v; }
443 /// getPristineRegs - Return a set of physical registers that are pristine on
444 /// entry to the MBB.
446 /// Pristine registers hold a value that is useless to the current function,
447 /// but that must be preserved - they are callee saved registers that have not
450 /// Before the PrologueEpilogueInserter has placed the CSR spill code, this
451 /// method always returns an empty set.
452 BitVector getPristineRegs(const MachineBasicBlock *MBB) const;
454 /// print - Used by the MachineFunction printer to print information about
455 /// stack objects. Implemented in MachineFunction.cpp
457 void print(const MachineFunction &MF, raw_ostream &OS) const;
459 /// dump - Print the function to stderr.
460 void dump(const MachineFunction &MF) const;
463 } // End llvm namespace