@@ -26,6 +26,8 @@ class MachineFunction;
2626class MachineBasicBlock ;
2727class BitVector ;
2828class AllocaInst ;
29+ class MachineFrameSizeInfo ;
30+ class TargetInstrInfo ;
2931
3032// / The CalleeSavedInfo class tracks the information need to locate where a
3133// / callee saved register is in the current frame.
@@ -282,6 +284,10 @@ class MachineFrameInfo {
282284 // / It is only valid during and after prolog/epilog code insertion.
283285 uint64_t MaxCallFrameSize = ~UINT64_C (0 );
284286
287+ // / Call frame sizes for the MachineFunction's MachineBasicBlocks. This is set
288+ // / by the MachineFrameSizeInfo constructor and cleared by its destructor.
289+ MachineFrameSizeInfo *SizeInfo = nullptr ;
290+
285291 // / The number of bytes of callee saved registers that the target wants to
286292 // / report for the current function in the CodeView S_FRAMEPROC record.
287293 unsigned CVBytesOfCalleeSavedRegisters = 0 ;
@@ -675,6 +681,13 @@ class MachineFrameInfo {
675681 }
676682 void setMaxCallFrameSize (uint64_t S) { MaxCallFrameSize = S; }
677683
684+ // / Return an object that can be queried for call frame sizes at specific
685+ // / locations in the MachineFunction. Constructing a MachineFrameSizeInfo
686+ // / object for the MachineFunction automatically makes it available via this
687+ // / field during the object's lifetime.
688+ MachineFrameSizeInfo *getSizeInfo () const { return SizeInfo; }
689+ void setSizeInfo (MachineFrameSizeInfo *SI) { SizeInfo = SI; }
690+
678691 // / Returns how many bytes of callee-saved registers the target pushed in the
679692 // / prologue. Only used for debug info.
680693 unsigned getCVBytesOfCalleeSavedRegisters () const {
@@ -846,6 +859,73 @@ class MachineFrameInfo {
846859 void dump (const MachineFunction &MF) const ;
847860};
848861
862+ // / Computes and stores the call frame sizes at the entries and exits of
863+ // / MachineBasicBlocks of a MachineFunction based on call frame setup and
864+ // / destroy pseudo instructions. Usually, no call frame is open at block
865+ // / boundaries, except if a call sequence has been split into multiple blocks.
866+ // /
867+ // / Computing this information is deferred until it is queried. Upon
868+ // / construction, a MachineFrameSizeInfo object registers itself in the
869+ // / MachineFunction's MachineFrameInfo (and it unregisters when destructed).
870+ // / While registered, it can be retrieved via MachineFrameInfo::getSizeInfo().
871+ // /
872+ // / This class assumes that call frame instructions are placed properly, i.e.,
873+ // / every program path hits a frame destroy of equal size after hitting a frame
874+ // / setup, and a frame setup of equal size before a frame destroy. Nested call
875+ // / frame sequences are not allowed.
876+ class MachineFrameSizeInfo {
877+ public:
878+ MachineFrameSizeInfo (MachineFunction &MF) : MF(MF) {
879+ assert (MF.getFrameInfo ().getSizeInfo () == nullptr );
880+ MF.getFrameInfo ().setSizeInfo (this );
881+ }
882+
883+ ~MachineFrameSizeInfo () { MF.getFrameInfo ().setSizeInfo (nullptr ); }
884+
885+ // / Get the call frame size just before MI. Contains no value if MI is not in
886+ // / a call sequence. Zero-sized call frames are possible.
887+ std::optional<unsigned > getCallFrameSizeAt (MachineInstr &MI);
888+
889+ // / Get the call frame size just before MII. Contains no value if MII is not
890+ // / in a call sequence. Zero-sized call frames are possible.
891+ std::optional<unsigned > getCallFrameSizeAt (MachineBasicBlock &MBB,
892+ MachineBasicBlock::iterator MII);
893+
894+ // / Get the call frame size at the entry of MBB. Contains no value if the
895+ // / entry of MBB is not in a call sequence. Zero-sized call frames are
896+ // / possible. Prefer this over getCallFrameSizeAt(MBB, MBB.begin()).
897+ std::optional<unsigned > getCallFrameSizeAtBegin (MachineBasicBlock &MBB);
898+
899+ // / Get the call frame size at the exit of MBB. Contains no value if the exit
900+ // / of MBB is not in a call sequence. Zero-sized call frames are possible.
901+ // / Prefer this over getCallFrameSizeAt(MBB, MBB.end()).
902+ std::optional<unsigned > getCallFrameSizeAtEnd (MachineBasicBlock &MBB);
903+
904+ private:
905+ // / Stores the call frame sizes at the boundaries of a MachineBasicBlock.
906+ struct MachineFrameSizeInfoForBB {
907+ MachineFrameSizeInfoForBB () = default ;
908+ MachineFrameSizeInfoForBB (std::optional<unsigned > EntryVal,
909+ std::optional<unsigned > ExitVal)
910+ : Entry(EntryVal), Exit(ExitVal) {}
911+
912+ std::optional<unsigned > Entry;
913+ std::optional<unsigned > Exit;
914+ };
915+
916+ // / Compute call frame sizes at the boundaries of each MachineBasicBlock.
917+ void computeSizes ();
918+
919+ MachineFunction &MF;
920+ const TargetInstrInfo *TII;
921+ unsigned FrameSetupOpcode = ~0u ;
922+ unsigned FrameDestroyOpcode = ~0u ;
923+ bool HasFrameOpcodes = false ;
924+ bool HasNoBrokenUpCallSeqs = false ;
925+ SmallVector<MachineFrameSizeInfoForBB, 8 > State;
926+ bool IsComputed = false ;
927+ };
928+
849929} // End llvm namespace
850930
851931#endif
0 commit comments