1 //===- llvm/MC/CodePadder.h - MC Code Padder --------------------*- 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 #ifndef LLVM_MC_MCCODEPADDER_H
11 #define LLVM_MC_MCCODEPADDER_H
13 #include "MCFragment.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/SmallVector.h"
21 class MCCodePaddingPolicy;
24 class MCObjectStreamer;
27 typedef SmallVector<const MCPaddingFragment *, 8> MCPFRange;
29 struct MCCodePaddingContext {
31 bool IsBasicBlockReachableViaFallthrough;
32 bool IsBasicBlockReachableViaBranch;
35 /// Target-independent base class incharge of all code padding decisions for a
36 /// target. During encoding it determines if and where MCPaddingFragments will
37 /// be located, as later on, when layout information is available, it determines
40 MCCodePadder(const MCCodePadder &) = delete;
41 void operator=(const MCCodePadder &) = delete;
43 /// Determines if the MCCodePaddingPolicies are active.
44 bool ArePoliciesActive;
46 /// All the supported MCCodePaddingPolicies.
47 SmallPtrSet<MCCodePaddingPolicy *, 4> CodePaddingPolicies;
49 /// A pointer to the fragment of the instruction whose padding is currently
51 MCPaddingFragment *CurrHandledInstFragment;
53 /// A map holding the jurisdiction for each padding fragment. Key: padding
54 /// fragment. Value: The fragment's jurisdiction. A jurisdiction is a vector
55 /// of padding fragments whose conditions are being controlled by another
56 /// fragment, the key fragment.
57 DenseMap<MCPaddingFragment *, MCPFRange> FragmentToJurisdiction;
58 MCPFRange &getJurisdiction(MCPaddingFragment *Fragment, MCAsmLayout &Layout);
60 /// A map holding the maximal instruction window size relevant for a padding
62 DenseMap<MCPaddingFragment *, uint64_t> FragmentToMaxWindowSize;
63 uint64_t getMaxWindowSize(MCPaddingFragment *Fragment, MCAsmLayout &Layout);
66 /// The current streamer, used to stream code padding.
69 bool addPolicy(MCCodePaddingPolicy *Policy);
72 basicBlockRequiresInsertionPoint(const MCCodePaddingContext &Context) {
76 virtual bool instructionRequiresInsertionPoint(const MCInst &Inst) {
80 virtual bool usePoliciesForBasicBlock(const MCCodePaddingContext &Context) {
81 return Context.IsPaddingActive;
86 : ArePoliciesActive(false), CurrHandledInstFragment(nullptr),
88 virtual ~MCCodePadder();
90 /// Handles all target related code padding when starting to write a new
91 /// basic block to an object file.
93 /// \param OS The streamer used for writing the padding data and function.
94 /// \param Context the context of the padding, Embeds the basic block's
96 void handleBasicBlockStart(MCObjectStreamer *OS,
97 const MCCodePaddingContext &Context);
98 /// Handles all target related code padding when done writing a block to an
101 /// \param Context the context of the padding, Embeds the basic block's
103 void handleBasicBlockEnd(const MCCodePaddingContext &Context);
104 /// Handles all target related code padding before writing a new instruction
105 /// to an object file.
107 /// \param Inst the instruction.
108 void handleInstructionBegin(const MCInst &Inst);
109 /// Handles all target related code padding after writing an instruction to an
112 /// \param Inst the instruction.
113 void handleInstructionEnd(const MCInst &Inst);
115 /// Relaxes a fragment (changes the size of the padding) according to target
116 /// requirements. The new size computation is done w.r.t a layout.
118 /// \param Fragment The fragment to relax.
119 /// \param Layout Code layout information.
121 /// \returns true iff any relaxation occurred.
122 bool relaxFragment(MCPaddingFragment *Fragment, MCAsmLayout &Layout);
125 /// The base class for all padding policies, i.e. a rule or set of rules to pad
126 /// the generated code.
127 class MCCodePaddingPolicy {
128 MCCodePaddingPolicy() = delete;
129 MCCodePaddingPolicy(const MCCodePaddingPolicy &) = delete;
130 void operator=(const MCCodePaddingPolicy &) = delete;
133 /// A mask holding the kind of this policy, i.e. only the i'th bit will be set
134 /// where i is the kind number.
135 const uint64_t KindMask;
136 /// Instruction window size relevant to this policy.
137 const uint64_t WindowSize;
138 /// A boolean indicating which byte of the instruction determies its
139 /// instruction window. If true - the last byte of the instructions, o.w. -
140 /// the first byte of the instruction.
141 const bool InstByteIsLastByte;
143 MCCodePaddingPolicy(uint64_t Kind, uint64_t WindowSize,
144 bool InstByteIsLastByte)
145 : KindMask(UINT64_C(1) << Kind), WindowSize(WindowSize),
146 InstByteIsLastByte(InstByteIsLastByte) {}
148 /// Computes and returns the offset of the consecutive fragment of a given
151 /// \param Fragment The fragment whose consecutive offset will be computed.
152 /// \param Layout Code layout information.
154 /// \returns the offset of the consecutive fragment of \p Fragment.
155 static uint64_t getNextFragmentOffset(const MCFragment *Fragment,
156 const MCAsmLayout &Layout);
157 /// Returns the instruction byte of an instruction pointed by a given
158 /// MCPaddingFragment. An instruction byte is the address of the byte of an
159 /// instruction which determines its instruction window.
161 /// \param Fragment The fragment pointing to the instruction.
162 /// \param Layout Code layout information.
164 /// \returns the instruction byte of an instruction pointed by \p Fragment.
165 uint64_t getFragmentInstByte(const MCPaddingFragment *Fragment,
166 MCAsmLayout &Layout) const;
167 uint64_t computeWindowEndAddress(const MCPaddingFragment *Fragment,
168 uint64_t Offset, MCAsmLayout &Layout) const;
170 /// Computes and returns the penalty weight of a first instruction window in a
171 /// range. This requires a special function since the first window does not
172 /// contain all the padding fragments in that window. It only contains all the
173 /// padding fragments starting from the relevant insertion point.
175 /// \param Window The first window.
176 /// \param Offset The offset of the parent section relative to the beginning
177 /// of the file, mod the window size.
178 /// \param Layout Code layout information.
180 /// \returns the penalty weight of a first instruction window in a range, \p
182 double computeFirstWindowPenaltyWeight(const MCPFRange &Window,
184 MCAsmLayout &Layout) const;
185 /// Computes and returns the penalty caused by an instruction window.
187 /// \param Window The instruction window.
188 /// \param Offset The offset of the parent section relative to the beginning
189 /// of the file, mod the window size.
190 /// \param Layout Code layout information.
192 /// \returns the penalty caused by \p Window.
193 virtual double computeWindowPenaltyWeight(const MCPFRange &Window,
195 MCAsmLayout &Layout) const = 0;
198 virtual ~MCCodePaddingPolicy() {}
200 /// Returns the kind mask of this policy - A mask holding the kind of this
201 /// policy, i.e. only the i'th bit will be set where i is the kind number.
202 uint64_t getKindMask() const { return KindMask; }
203 /// Returns the instruction window size relevant to this policy.
204 uint64_t getWindowSize() const { return WindowSize; }
205 /// Returns true if the last byte of an instruction determines its instruction
206 /// window, or false if the first of an instruction determines it.
207 bool isInstByteLastByte() const { return InstByteIsLastByte; }
209 /// Returns true iff this policy needs padding for a given basic block.
211 /// \param Context the context of the padding, Embeds the basic block's
214 /// \returns true iff this policy needs padding for the basic block.
216 basicBlockRequiresPaddingFragment(const MCCodePaddingContext &Context) const {
219 /// Returns true iff this policy needs padding for a given instruction.
221 /// \param Inst The given instruction.
223 /// \returns true iff this policy needs padding for \p Inst.
224 virtual bool instructionRequiresPaddingFragment(const MCInst &Inst) const {
227 /// Computes and returns the penalty caused by a range of instruction windows.
228 /// The weight is computed for each window separelty and then accumulated.
230 /// \param Range The range.
231 /// \param Offset The offset of the parent section relative to the beginning
232 /// of the file, mod the window size.
233 /// \param Layout Code layout information.
235 /// \returns the penalty caused by \p Range.
236 double computeRangePenaltyWeight(const MCPFRange &Range, uint64_t Offset,
237 MCAsmLayout &Layout) const;
242 #endif // LLVM_MC_MCCODEPADDER_H