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