1 //===- SafeStackColoring.h - SafeStack frame coloring ----------*- C++ -*--===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIB_CODEGEN_SAFESTACKCOLORING_H
10 #define LLVM_LIB_CODEGEN_SAFESTACKCOLORING_H
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/Support/raw_ostream.h"
29 /// Compute live ranges of allocas.
30 /// Live ranges are represented as sets of "interesting" instructions, which are
31 /// defined as instructions that may start or end an alloca's lifetime. These
33 /// * lifetime.start and lifetime.end intrinsics
34 /// * first instruction of any basic block
35 /// Interesting instructions are numbered in the depth-first walk of the CFG,
36 /// and in the program order inside each basic block.
38 /// A class representing liveness information for a single basic block.
39 /// Each bit in the BitVector represents the liveness property
40 /// for a different stack slot.
41 struct BlockLifetimeInfo
{
42 /// Which slots BEGINs in each basic block.
45 /// Which slots ENDs in each basic block.
48 /// Which slots are marked as LIVE_IN, coming into each basic block.
51 /// Which slots are marked as LIVE_OUT, coming out of each basic block.
56 /// This class represents a set of interesting instructions where an alloca is
61 void SetMaximum(int size
) { bv
.resize(size
); }
62 void AddRange(unsigned start
, unsigned end
) { bv
.set(start
, end
); }
64 bool Overlaps(const LiveRange
&Other
) const {
65 return bv
.anyCommon(Other
.bv
);
68 void Join(const LiveRange
&Other
) { bv
|= Other
.bv
; }
74 /// Maps active slots (per bit) for each basic block.
75 using LivenessMap
= DenseMap
<BasicBlock
*, BlockLifetimeInfo
>;
76 LivenessMap BlockLiveness
;
78 /// Number of interesting instructions.
81 /// Numeric ids for interesting instructions.
82 DenseMap
<Instruction
*, unsigned> InstructionNumbering
;
84 /// A range [Start, End) of instruction ids for each basic block.
85 /// Instructions inside each BB have monotonic and consecutive ids.
86 DenseMap
<const BasicBlock
*, std::pair
<unsigned, unsigned>> BlockInstRange
;
88 ArrayRef
<AllocaInst
*> Allocas
;
90 DenseMap
<AllocaInst
*, unsigned> AllocaNumbering
;
92 /// LiveRange for allocas.
93 SmallVector
<LiveRange
, 8> LiveRanges
;
95 /// The set of allocas that have at least one lifetime.start. All other
96 /// allocas get LiveRange that corresponds to the entire function.
97 BitVector InterestingAllocas
;
98 SmallVector
<Instruction
*, 8> Markers
;
105 /// List of {InstNo, {AllocaNo, IsStart}} for each BB, ordered by InstNo.
106 DenseMap
<BasicBlock
*, SmallVector
<std::pair
<unsigned, Marker
>, 4>> BBMarkers
;
109 void dumpBlockLiveness();
110 void dumpLiveRanges();
112 bool readMarker(Instruction
*I
, bool *IsStart
);
113 void collectMarkers();
114 void calculateLocalLiveness();
115 void calculateLiveIntervals();
118 StackColoring(Function
&F
, ArrayRef
<AllocaInst
*> Allocas
)
119 : F(F
), Allocas(Allocas
), NumAllocas(Allocas
.size()) {}
122 void removeAllMarkers();
124 /// Returns a set of "interesting" instructions where the given alloca is
125 /// live. Not all instructions in a function are interesting: we pick a set
126 /// that is large enough for LiveRange::Overlaps to be correct.
127 const LiveRange
&getLiveRange(AllocaInst
*AI
);
129 /// Returns a live range that represents an alloca that is live throughout the
131 LiveRange
getFullLiveRange() {
132 assert(NumInst
>= 0);
134 R
.SetMaximum(NumInst
);
135 R
.AddRange(0, NumInst
);
140 static inline raw_ostream
&operator<<(raw_ostream
&OS
, const BitVector
&V
) {
142 int idx
= V
.find_first();
150 idx
= V
.find_next(idx
);
156 static inline raw_ostream
&operator<<(raw_ostream
&OS
,
157 const StackColoring::LiveRange
&R
) {
161 } // end namespace safestack
163 } // end namespace llvm
165 #endif // LLVM_LIB_CODEGEN_SAFESTACKCOLORING_H