12cab237bSDimitry Andric //===- SafeStackLayout.h - SafeStack frame layout --------------*- C++ -*--===//
23ca95b02SDimitry Andric //
33ca95b02SDimitry Andric //                     The LLVM Compiler Infrastructure
43ca95b02SDimitry Andric //
53ca95b02SDimitry Andric // This file is distributed under the University of Illinois Open Source
63ca95b02SDimitry Andric // License. See LICENSE.TXT for details.
73ca95b02SDimitry Andric //
83ca95b02SDimitry Andric //===----------------------------------------------------------------------===//
93ca95b02SDimitry Andric 
103ca95b02SDimitry Andric #ifndef LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
113ca95b02SDimitry Andric #define LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
123ca95b02SDimitry Andric 
133ca95b02SDimitry Andric #include "SafeStackColoring.h"
142cab237bSDimitry Andric #include "llvm/ADT/DenseMap.h"
152cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
163ca95b02SDimitry Andric 
173ca95b02SDimitry Andric namespace llvm {
182cab237bSDimitry Andric 
192cab237bSDimitry Andric class raw_ostream;
202cab237bSDimitry Andric class Value;
212cab237bSDimitry Andric 
223ca95b02SDimitry Andric namespace safestack {
233ca95b02SDimitry Andric 
243ca95b02SDimitry Andric /// Compute the layout of an unsafe stack frame.
253ca95b02SDimitry Andric class StackLayout {
263ca95b02SDimitry Andric   unsigned MaxAlignment;
273ca95b02SDimitry Andric 
283ca95b02SDimitry Andric   struct StackRegion {
293ca95b02SDimitry Andric     unsigned Start;
303ca95b02SDimitry Andric     unsigned End;
313ca95b02SDimitry Andric     StackColoring::LiveRange Range;
322cab237bSDimitry Andric 
StackRegionStackRegion333ca95b02SDimitry Andric     StackRegion(unsigned Start, unsigned End,
343ca95b02SDimitry Andric                 const StackColoring::LiveRange &Range)
353ca95b02SDimitry Andric         : Start(Start), End(End), Range(Range) {}
363ca95b02SDimitry Andric   };
372cab237bSDimitry Andric 
383ca95b02SDimitry Andric   /// The list of current stack regions, sorted by StackRegion::Start.
393ca95b02SDimitry Andric   SmallVector<StackRegion, 16> Regions;
403ca95b02SDimitry Andric 
413ca95b02SDimitry Andric   struct StackObject {
423ca95b02SDimitry Andric     const Value *Handle;
433ca95b02SDimitry Andric     unsigned Size, Alignment;
443ca95b02SDimitry Andric     StackColoring::LiveRange Range;
453ca95b02SDimitry Andric   };
462cab237bSDimitry Andric 
473ca95b02SDimitry Andric   SmallVector<StackObject, 8> StackObjects;
483ca95b02SDimitry Andric 
493ca95b02SDimitry Andric   DenseMap<const Value *, unsigned> ObjectOffsets;
50*4ba319b5SDimitry Andric   DenseMap<const Value *, unsigned> ObjectAlignments;
513ca95b02SDimitry Andric 
523ca95b02SDimitry Andric   void layoutObject(StackObject &Obj);
533ca95b02SDimitry Andric 
543ca95b02SDimitry Andric public:
StackLayout(unsigned StackAlignment)553ca95b02SDimitry Andric   StackLayout(unsigned StackAlignment) : MaxAlignment(StackAlignment) {}
562cab237bSDimitry Andric 
573ca95b02SDimitry Andric   /// Add an object to the stack frame. Value pointer is opaque and used as a
583ca95b02SDimitry Andric   /// handle to retrieve the object's offset in the frame later.
593ca95b02SDimitry Andric   void addObject(const Value *V, unsigned Size, unsigned Alignment,
603ca95b02SDimitry Andric                  const StackColoring::LiveRange &Range);
613ca95b02SDimitry Andric 
623ca95b02SDimitry Andric   /// Run the layout computation for all previously added objects.
633ca95b02SDimitry Andric   void computeLayout();
643ca95b02SDimitry Andric 
653ca95b02SDimitry Andric   /// Returns the offset to the object start in the stack frame.
getObjectOffset(const Value * V)663ca95b02SDimitry Andric   unsigned getObjectOffset(const Value *V) { return ObjectOffsets[V]; }
673ca95b02SDimitry Andric 
68*4ba319b5SDimitry Andric   /// Returns the alignment of the object
getObjectAlignment(const Value * V)69*4ba319b5SDimitry Andric   unsigned getObjectAlignment(const Value *V) { return ObjectAlignments[V]; }
70*4ba319b5SDimitry Andric 
713ca95b02SDimitry Andric   /// Returns the size of the entire frame.
getFrameSize()723ca95b02SDimitry Andric   unsigned getFrameSize() { return Regions.empty() ? 0 : Regions.back().End; }
733ca95b02SDimitry Andric 
743ca95b02SDimitry Andric   /// Returns the alignment of the frame.
getFrameAlignment()753ca95b02SDimitry Andric   unsigned getFrameAlignment() { return MaxAlignment; }
762cab237bSDimitry Andric 
773ca95b02SDimitry Andric   void print(raw_ostream &OS);
783ca95b02SDimitry Andric };
793ca95b02SDimitry Andric 
802cab237bSDimitry Andric } // end namespace safestack
812cab237bSDimitry Andric 
822cab237bSDimitry Andric } // end namespace llvm
833ca95b02SDimitry Andric 
843ca95b02SDimitry Andric #endif // LLVM_LIB_CODEGEN_SAFESTACKLAYOUT_H
85