1 //===- StackColoring.cpp --------------------------------------------------===//
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 // This pass implements the stack-coloring optimization that looks for
10 // lifetime markers machine instructions (LIFETIME_START and LIFETIME_END),
11 // which represent the possible lifetime of stack slots. It attempts to
12 // merge disjoint stack slots and reduce the used stack space.
13 // NOTE: This pass is not StackSlotColoring, which optimizes spill slots.
15 // TODO: In the future we plan to improve stack coloring in the following ways:
16 // 1. Allow merging multiple small slots into a single larger slot at different
18 // 2. Merge this pass with StackSlotColoring and allow merging of allocas with
21 //===----------------------------------------------------------------------===//
23 #include "llvm/ADT/BitVector.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/ValueTracking.h"
30 #include "llvm/CodeGen/LiveInterval.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineFrameInfo.h"
33 #include "llvm/CodeGen/MachineFunction.h"
34 #include "llvm/CodeGen/MachineFunctionPass.h"
35 #include "llvm/CodeGen/MachineInstr.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineOperand.h"
38 #include "llvm/CodeGen/Passes.h"
39 #include "llvm/CodeGen/SlotIndexes.h"
40 #include "llvm/CodeGen/TargetOpcodes.h"
41 #include "llvm/CodeGen/WinEHFuncInfo.h"
42 #include "llvm/Config/llvm-config.h"
43 #include "llvm/IR/Constants.h"
44 #include "llvm/IR/DebugInfoMetadata.h"
45 #include "llvm/IR/Instructions.h"
46 #include "llvm/IR/Metadata.h"
47 #include "llvm/IR/Use.h"
48 #include "llvm/IR/Value.h"
49 #include "llvm/InitializePasses.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Compiler.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/raw_ostream.h"
64 #define DEBUG_TYPE "stack-coloring"
67 DisableColoring("no-stack-coloring",
68 cl::init(false), cl::Hidden
,
69 cl::desc("Disable stack coloring"));
71 /// The user may write code that uses allocas outside of the declared lifetime
72 /// zone. This can happen when the user returns a reference to a local
73 /// data-structure. We can detect these cases and decide not to optimize the
74 /// code. If this flag is enabled, we try to save the user. This option
75 /// is treated as overriding LifetimeStartOnFirstUse below.
77 ProtectFromEscapedAllocas("protect-from-escaped-allocas",
78 cl::init(false), cl::Hidden
,
79 cl::desc("Do not optimize lifetime zones that "
82 /// Enable enhanced dataflow scheme for lifetime analysis (treat first
83 /// use of stack slot as start of slot lifetime, as opposed to looking
84 /// for LIFETIME_START marker). See "Implementation notes" below for
87 LifetimeStartOnFirstUse("stackcoloring-lifetime-start-on-first-use",
88 cl::init(true), cl::Hidden
,
89 cl::desc("Treat stack lifetimes as starting on first use, not on START marker."));
92 STATISTIC(NumMarkerSeen
, "Number of lifetime markers found.");
93 STATISTIC(StackSpaceSaved
, "Number of bytes saved due to merging slots.");
94 STATISTIC(StackSlotMerged
, "Number of stack slot merged.");
95 STATISTIC(EscapedAllocas
, "Number of allocas that escaped the lifetime region");
97 //===----------------------------------------------------------------------===//
99 //===----------------------------------------------------------------------===//
101 // Stack Coloring reduces stack usage by merging stack slots when they
102 // can't be used together. For example, consider the following C program:
104 // void bar(char *, int);
105 // void foo(bool var) {
124 // Naively-compiled, this program would use 12k of stack space. However, the
125 // stack slot corresponding to `z` is always destroyed before either of the
126 // stack slots for `x` or `y` are used, and then `x` is only used if `var`
127 // is true, while `y` is only used if `var` is false. So in no time are 2
128 // of the stack slots used together, and therefore we can merge them,
129 // compiling the function using only a single 4k alloca:
131 // void foo(bool var) { // equivalent
144 // This is an important optimization if we want stack space to be under
145 // control in large functions, both open-coded ones and ones created by
148 // Implementation Notes:
149 // ---------------------
151 // An important part of the above reasoning is that `z` can't be accessed
152 // while the latter 2 calls to `bar` are running. This is justified because
153 // `z`'s lifetime is over after we exit from block `A:`, so any further
154 // accesses to it would be UB. The way we represent this information
155 // in LLVM is by having frontends delimit blocks with `lifetime.start`
156 // and `lifetime.end` intrinsics.
158 // The effect of these intrinsics seems to be as follows (maybe I should
159 // specify this in the reference?):
161 // L1) at start, each stack-slot is marked as *out-of-scope*, unless no
162 // lifetime intrinsic refers to that stack slot, in which case
163 // it is marked as *in-scope*.
164 // L2) on a `lifetime.start`, a stack slot is marked as *in-scope* and
165 // the stack slot is overwritten with `undef`.
166 // L3) on a `lifetime.end`, a stack slot is marked as *out-of-scope*.
167 // L4) on function exit, all stack slots are marked as *out-of-scope*.
168 // L5) `lifetime.end` is a no-op when called on a slot that is already
170 // L6) memory accesses to *out-of-scope* stack slots are UB.
171 // L7) when a stack-slot is marked as *out-of-scope*, all pointers to it
172 // are invalidated, unless the slot is "degenerate". This is used to
173 // justify not marking slots as in-use until the pointer to them is
174 // used, but feels a bit hacky in the presence of things like LICM. See
175 // the "Degenerate Slots" section for more details.
177 // Now, let's ground stack coloring on these rules. We'll define a slot
178 // as *in-use* at a (dynamic) point in execution if it either can be
179 // written to at that point, or if it has a live and non-undef content
182 // Obviously, slots that are never *in-use* together can be merged, and
183 // in our example `foo`, the slots for `x`, `y` and `z` are never
184 // in-use together (of course, sometimes slots that *are* in-use together
185 // might still be mergable, but we don't care about that here).
187 // In this implementation, we successively merge pairs of slots that are
188 // not *in-use* together. We could be smarter - for example, we could merge
189 // a single large slot with 2 small slots, or we could construct the
190 // interference graph and run a "smart" graph coloring algorithm, but with
191 // that aside, how do we find out whether a pair of slots might be *in-use*
194 // From our rules, we see that *out-of-scope* slots are never *in-use*,
195 // and from (L7) we see that "non-degenerate" slots remain non-*in-use*
196 // until their address is taken. Therefore, we can approximate slot activity
199 // A subtle point: naively, we might try to figure out which pairs of
200 // stack-slots interfere by propagating `S in-use` through the CFG for every
201 // stack-slot `S`, and having `S` and `T` interfere if there is a CFG point in
202 // which they are both *in-use*.
204 // That is sound, but overly conservative in some cases: in our (artificial)
205 // example `foo`, either `x` or `y` might be in use at the label `B:`, but
206 // as `x` is only in use if we came in from the `var` edge and `y` only
207 // if we came from the `!var` edge, they still can't be in use together.
208 // See PR32488 for an important real-life case.
210 // If we wanted to find all points of interference precisely, we could
211 // propagate `S in-use` and `S&T in-use` predicates through the CFG. That
212 // would be precise, but requires propagating `O(n^2)` dataflow facts.
214 // However, we aren't interested in the *set* of points of interference
215 // between 2 stack slots, only *whether* there *is* such a point. So we
216 // can rely on a little trick: for `S` and `T` to be in-use together,
217 // one of them needs to become in-use while the other is in-use (or
218 // they might both become in use simultaneously). We can check this
219 // by also keeping track of the points at which a stack slot might *start*
225 // Consider the following motivating example:
228 // char b1[1024], b2[1024];
234 // char b4[1024], b5[1024];
235 // <uses of b2, b4, b5>;
240 // In the code above, "b3" and "b4" are declared in distinct lexical
241 // scopes, meaning that it is easy to prove that they can share the
242 // same stack slot. Variables "b1" and "b2" are declared in the same
243 // scope, meaning that from a lexical point of view, their lifetimes
244 // overlap. From a control flow pointer of view, however, the two
245 // variables are accessed in disjoint regions of the CFG, thus it
246 // should be possible for them to share the same stack slot. An ideal
247 // stack allocation for the function above would look like:
253 // Achieving this allocation is tricky, however, due to the way
254 // lifetime markers are inserted. Here is a simplified view of the
255 // control flow graph for the code above:
257 // +------ block 0 -------+
258 // 0| LIFETIME_START b1, b2 |
259 // 1| <test 'if' condition> |
260 // +-----------------------+
262 // +------ block 1 -------+ +------ block 2 -------+
263 // 2| LIFETIME_START b3 | 5| LIFETIME_START b4, b5 |
264 // 3| <uses of b1, b3> | 6| <uses of b2, b4, b5> |
265 // 4| LIFETIME_END b3 | 7| LIFETIME_END b4, b5 |
266 // +-----------------------+ +-----------------------+
268 // +------ block 3 -------+
269 // 8| <cleanupcode> |
270 // 9| LIFETIME_END b1, b2 |
272 // +-----------------------+
274 // If we create live intervals for the variables above strictly based
275 // on the lifetime markers, we'll get the set of intervals on the
276 // left. If we ignore the lifetime start markers and instead treat a
277 // variable's lifetime as beginning with the first reference to the
278 // var, then we get the intervals on the right.
280 // LIFETIME_START First Use
281 // b1: [0,9] [3,4] [8,9]
287 // For the intervals on the left, the best we can do is overlap two
288 // variables (b3 and b4, for example); this gives us a stack size of
289 // 4*1024 bytes, not ideal. When treating first-use as the start of a
290 // lifetime, we can additionally overlap b1 and b5, giving us a 3*1024
291 // byte stack (better).
296 // Relying entirely on first-use of stack slots is problematic,
297 // however, due to the fact that optimizations can sometimes migrate
298 // uses of a variable outside of its lifetime start/end region. Here
302 // char b1[1024], b2[1024];
315 // Before optimization, the control flow graph for the code above
316 // might look like the following:
318 // +------ block 0 -------+
319 // 0| LIFETIME_START b1, b2 |
320 // 1| <test 'if' condition> |
321 // +-----------------------+
323 // +------ block 1 -------+ +------- block 2 -------+
324 // 2| <uses of b2> | 3| <uses of b1> |
325 // +-----------------------+ +-----------------------+
327 // | +------- block 3 -------+ <-\.
328 // | 4| <while condition> | |
329 // | +-----------------------+ |
331 // | / +------- block 4 -------+
332 // \ / 5| LIFETIME_START b3 | |
333 // \ / 6| <uses of b3> | |
334 // \ / 7| LIFETIME_END b3 | |
335 // \ | +------------------------+ |
337 // +------ block 5 -----+ \---------------
338 // 8| <cleanupcode> |
339 // 9| LIFETIME_END b1, b2 |
341 // +---------------------+
343 // During optimization, however, it can happen that an instruction
344 // computing an address in "b3" (for example, a loop-invariant GEP) is
345 // hoisted up out of the loop from block 4 to block 2. [Note that
346 // this is not an actual load from the stack, only an instruction that
347 // computes the address to be loaded]. If this happens, there is now a
348 // path leading from the first use of b3 to the return instruction
349 // that does not encounter the b3 LIFETIME_END, hence b3's lifetime is
350 // now larger than if we were computing live intervals strictly based
351 // on lifetime markers. In the example above, this lengthened lifetime
352 // would mean that it would appear illegal to overlap b3 with b2.
354 // To deal with this such cases, the code in ::collectMarkers() below
355 // tries to identify "degenerate" slots -- those slots where on a single
356 // forward pass through the CFG we encounter a first reference to slot
357 // K before we hit the slot K lifetime start marker. For such slots,
358 // we fall back on using the lifetime start marker as the beginning of
359 // the variable's lifetime. NB: with this implementation, slots can
360 // appear degenerate in cases where there is unstructured control flow:
365 // memcpy(&b[0], ...);
370 // If in RPO ordering chosen to walk the CFG we happen to visit the b[k]
371 // before visiting the memcpy block (which will contain the lifetime start
372 // for "b" then it will appear that 'b' has a degenerate lifetime.
376 /// StackColoring - A machine pass for merging disjoint stack allocations,
377 /// marked by the LIFETIME_START and LIFETIME_END pseudo instructions.
378 class StackColoring
: public MachineFunctionPass
{
379 MachineFrameInfo
*MFI
= nullptr;
380 MachineFunction
*MF
= nullptr;
382 /// A class representing liveness information for a single basic block.
383 /// Each bit in the BitVector represents the liveness property
384 /// for a different stack slot.
385 struct BlockLifetimeInfo
{
386 /// Which slots BEGINs in each basic block.
389 /// Which slots ENDs in each basic block.
392 /// Which slots are marked as LIVE_IN, coming into each basic block.
395 /// Which slots are marked as LIVE_OUT, coming out of each basic block.
399 /// Maps active slots (per bit) for each basic block.
400 using LivenessMap
= DenseMap
<const MachineBasicBlock
*, BlockLifetimeInfo
>;
401 LivenessMap BlockLiveness
;
403 /// Maps serial numbers to basic blocks.
404 DenseMap
<const MachineBasicBlock
*, int> BasicBlocks
;
406 /// Maps basic blocks to a serial number.
407 SmallVector
<const MachineBasicBlock
*, 8> BasicBlockNumbering
;
409 /// Maps slots to their use interval. Outside of this interval, slots
410 /// values are either dead or `undef` and they will not be written to.
411 SmallVector
<std::unique_ptr
<LiveInterval
>, 16> Intervals
;
413 /// Maps slots to the points where they can become in-use.
414 SmallVector
<SmallVector
<SlotIndex
, 4>, 16> LiveStarts
;
416 /// VNInfo is used for the construction of LiveIntervals.
417 VNInfo::Allocator VNInfoAllocator
;
419 /// SlotIndex analysis object.
420 SlotIndexes
*Indexes
= nullptr;
422 /// The list of lifetime markers found. These markers are to be removed
423 /// once the coloring is done.
424 SmallVector
<MachineInstr
*, 8> Markers
;
426 /// Record the FI slots for which we have seen some sort of
427 /// lifetime marker (either start or end).
428 BitVector InterestingSlots
;
430 /// FI slots that need to be handled conservatively (for these
431 /// slots lifetime-start-on-first-use is disabled).
432 BitVector ConservativeSlots
;
434 /// Number of iterations taken during data flow analysis.
435 unsigned NumIterations
;
440 StackColoring() : MachineFunctionPass(ID
) {
441 initializeStackColoringPass(*PassRegistry::getPassRegistry());
444 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
445 bool runOnMachineFunction(MachineFunction
&Func
) override
;
448 /// Used in collectMarkers
449 using BlockBitVecMap
= DenseMap
<const MachineBasicBlock
*, BitVector
>;
453 void dumpIntervals() const;
454 void dumpBB(MachineBasicBlock
*MBB
) const;
455 void dumpBV(const char *tag
, const BitVector
&BV
) const;
457 /// Removes all of the lifetime marker instructions from the function.
458 /// \returns true if any markers were removed.
459 bool removeAllMarkers();
461 /// Scan the machine function and find all of the lifetime markers.
462 /// Record the findings in the BEGIN and END vectors.
463 /// \returns the number of markers found.
464 unsigned collectMarkers(unsigned NumSlot
);
466 /// Perform the dataflow calculation and calculate the lifetime for each of
467 /// the slots, based on the BEGIN/END vectors. Set the LifetimeLIVE_IN and
468 /// LifetimeLIVE_OUT maps that represent which stack slots are live coming
469 /// in and out blocks.
470 void calculateLocalLiveness();
472 /// Returns TRUE if we're using the first-use-begins-lifetime method for
473 /// this slot (if FALSE, then the start marker is treated as start of lifetime).
474 bool applyFirstUse(int Slot
) {
475 if (!LifetimeStartOnFirstUse
|| ProtectFromEscapedAllocas
)
477 if (ConservativeSlots
.test(Slot
))
482 /// Examines the specified instruction and returns TRUE if the instruction
483 /// represents the start or end of an interesting lifetime. The slot or slots
484 /// starting or ending are added to the vector "slots" and "isStart" is set
486 /// \returns True if inst contains a lifetime start or end
487 bool isLifetimeStartOrEnd(const MachineInstr
&MI
,
488 SmallVector
<int, 4> &slots
,
491 /// Construct the LiveIntervals for the slots.
492 void calculateLiveIntervals(unsigned NumSlots
);
494 /// Go over the machine function and change instructions which use stack
495 /// slots to use the joint slots.
496 void remapInstructions(DenseMap
<int, int> &SlotRemap
);
498 /// The input program may contain instructions which are not inside lifetime
499 /// markers. This can happen due to a bug in the compiler or due to a bug in
500 /// user code (for example, returning a reference to a local variable).
501 /// This procedure checks all of the instructions in the function and
502 /// invalidates lifetime ranges which do not contain all of the instructions
503 /// which access that frame slot.
504 void removeInvalidSlotRanges();
506 /// Map entries which point to other entries to their destination.
507 /// A->B->C becomes A->C.
508 void expungeSlotMap(DenseMap
<int, int> &SlotRemap
, unsigned NumSlots
);
511 } // end anonymous namespace
513 char StackColoring::ID
= 0;
515 char &llvm::StackColoringID
= StackColoring::ID
;
517 INITIALIZE_PASS_BEGIN(StackColoring
, DEBUG_TYPE
,
518 "Merge disjoint stack slots", false, false)
519 INITIALIZE_PASS_DEPENDENCY(SlotIndexes
)
520 INITIALIZE_PASS_END(StackColoring
, DEBUG_TYPE
,
521 "Merge disjoint stack slots", false, false)
523 void StackColoring::getAnalysisUsage(AnalysisUsage
&AU
) const {
524 AU
.addRequired
<SlotIndexes
>();
525 MachineFunctionPass::getAnalysisUsage(AU
);
528 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
529 LLVM_DUMP_METHOD
void StackColoring::dumpBV(const char *tag
,
530 const BitVector
&BV
) const {
531 dbgs() << tag
<< " : { ";
532 for (unsigned I
= 0, E
= BV
.size(); I
!= E
; ++I
)
533 dbgs() << BV
.test(I
) << " ";
537 LLVM_DUMP_METHOD
void StackColoring::dumpBB(MachineBasicBlock
*MBB
) const {
538 LivenessMap::const_iterator BI
= BlockLiveness
.find(MBB
);
539 assert(BI
!= BlockLiveness
.end() && "Block not found");
540 const BlockLifetimeInfo
&BlockInfo
= BI
->second
;
542 dumpBV("BEGIN", BlockInfo
.Begin
);
543 dumpBV("END", BlockInfo
.End
);
544 dumpBV("LIVE_IN", BlockInfo
.LiveIn
);
545 dumpBV("LIVE_OUT", BlockInfo
.LiveOut
);
548 LLVM_DUMP_METHOD
void StackColoring::dump() const {
549 for (MachineBasicBlock
*MBB
: depth_first(MF
)) {
550 dbgs() << "Inspecting block #" << MBB
->getNumber() << " ["
551 << MBB
->getName() << "]\n";
556 LLVM_DUMP_METHOD
void StackColoring::dumpIntervals() const {
557 for (unsigned I
= 0, E
= Intervals
.size(); I
!= E
; ++I
) {
558 dbgs() << "Interval[" << I
<< "]:\n";
559 Intervals
[I
]->dump();
564 static inline int getStartOrEndSlot(const MachineInstr
&MI
)
566 assert((MI
.getOpcode() == TargetOpcode::LIFETIME_START
||
567 MI
.getOpcode() == TargetOpcode::LIFETIME_END
) &&
568 "Expected LIFETIME_START or LIFETIME_END op");
569 const MachineOperand
&MO
= MI
.getOperand(0);
570 int Slot
= MO
.getIndex();
576 // At the moment the only way to end a variable lifetime is with
577 // a VARIABLE_LIFETIME op (which can't contain a start). If things
578 // change and the IR allows for a single inst that both begins
579 // and ends lifetime(s), this interface will need to be reworked.
580 bool StackColoring::isLifetimeStartOrEnd(const MachineInstr
&MI
,
581 SmallVector
<int, 4> &slots
,
583 if (MI
.getOpcode() == TargetOpcode::LIFETIME_START
||
584 MI
.getOpcode() == TargetOpcode::LIFETIME_END
) {
585 int Slot
= getStartOrEndSlot(MI
);
588 if (!InterestingSlots
.test(Slot
))
590 slots
.push_back(Slot
);
591 if (MI
.getOpcode() == TargetOpcode::LIFETIME_END
) {
595 if (!applyFirstUse(Slot
)) {
599 } else if (LifetimeStartOnFirstUse
&& !ProtectFromEscapedAllocas
) {
600 if (!MI
.isDebugInstr()) {
602 for (const MachineOperand
&MO
: MI
.operands()) {
605 int Slot
= MO
.getIndex();
608 if (InterestingSlots
.test(Slot
) && applyFirstUse(Slot
)) {
609 slots
.push_back(Slot
);
622 unsigned StackColoring::collectMarkers(unsigned NumSlot
) {
623 unsigned MarkersFound
= 0;
624 BlockBitVecMap SeenStartMap
;
625 InterestingSlots
.clear();
626 InterestingSlots
.resize(NumSlot
);
627 ConservativeSlots
.clear();
628 ConservativeSlots
.resize(NumSlot
);
630 // number of start and end lifetime ops for each slot
631 SmallVector
<int, 8> NumStartLifetimes(NumSlot
, 0);
632 SmallVector
<int, 8> NumEndLifetimes(NumSlot
, 0);
634 // Step 1: collect markers and populate the "InterestingSlots"
635 // and "ConservativeSlots" sets.
636 for (MachineBasicBlock
*MBB
: depth_first(MF
)) {
637 // Compute the set of slots for which we've seen a START marker but have
638 // not yet seen an END marker at this point in the walk (e.g. on entry
640 BitVector BetweenStartEnd
;
641 BetweenStartEnd
.resize(NumSlot
);
642 for (const MachineBasicBlock
*Pred
: MBB
->predecessors()) {
643 BlockBitVecMap::const_iterator I
= SeenStartMap
.find(Pred
);
644 if (I
!= SeenStartMap
.end()) {
645 BetweenStartEnd
|= I
->second
;
649 // Walk the instructions in the block to look for start/end ops.
650 for (MachineInstr
&MI
: *MBB
) {
651 if (MI
.isDebugInstr())
653 if (MI
.getOpcode() == TargetOpcode::LIFETIME_START
||
654 MI
.getOpcode() == TargetOpcode::LIFETIME_END
) {
655 int Slot
= getStartOrEndSlot(MI
);
658 InterestingSlots
.set(Slot
);
659 if (MI
.getOpcode() == TargetOpcode::LIFETIME_START
) {
660 BetweenStartEnd
.set(Slot
);
661 NumStartLifetimes
[Slot
] += 1;
663 BetweenStartEnd
.reset(Slot
);
664 NumEndLifetimes
[Slot
] += 1;
666 const AllocaInst
*Allocation
= MFI
->getObjectAllocation(Slot
);
668 LLVM_DEBUG(dbgs() << "Found a lifetime ");
669 LLVM_DEBUG(dbgs() << (MI
.getOpcode() == TargetOpcode::LIFETIME_START
672 LLVM_DEBUG(dbgs() << " marker for slot #" << Slot
);
674 << " with allocation: " << Allocation
->getName() << "\n");
676 Markers
.push_back(&MI
);
679 for (const MachineOperand
&MO
: MI
.operands()) {
682 int Slot
= MO
.getIndex();
685 if (! BetweenStartEnd
.test(Slot
)) {
686 ConservativeSlots
.set(Slot
);
691 BitVector
&SeenStart
= SeenStartMap
[MBB
];
692 SeenStart
|= BetweenStartEnd
;
698 // PR27903: slots with multiple start or end lifetime ops are not
699 // safe to enable for "lifetime-start-on-first-use".
700 for (unsigned slot
= 0; slot
< NumSlot
; ++slot
) {
701 if (NumStartLifetimes
[slot
] > 1 || NumEndLifetimes
[slot
] > 1)
702 ConservativeSlots
.set(slot
);
705 // The write to the catch object by the personality function is not propely
706 // modeled in IR: It happens before any cleanuppads are executed, even if the
707 // first mention of the catch object is in a catchpad. As such, mark catch
708 // object slots as conservative, so they are excluded from first-use analysis.
709 if (WinEHFuncInfo
*EHInfo
= MF
->getWinEHFuncInfo())
710 for (WinEHTryBlockMapEntry
&TBME
: EHInfo
->TryBlockMap
)
711 for (WinEHHandlerType
&H
: TBME
.HandlerArray
)
712 if (H
.CatchObj
.FrameIndex
!= std::numeric_limits
<int>::max() &&
713 H
.CatchObj
.FrameIndex
>= 0)
714 ConservativeSlots
.set(H
.CatchObj
.FrameIndex
);
716 LLVM_DEBUG(dumpBV("Conservative slots", ConservativeSlots
));
718 // Step 2: compute begin/end sets for each block
720 // NOTE: We use a depth-first iteration to ensure that we obtain a
721 // deterministic numbering.
722 for (MachineBasicBlock
*MBB
: depth_first(MF
)) {
723 // Assign a serial number to this basic block.
724 BasicBlocks
[MBB
] = BasicBlockNumbering
.size();
725 BasicBlockNumbering
.push_back(MBB
);
727 // Keep a reference to avoid repeated lookups.
728 BlockLifetimeInfo
&BlockInfo
= BlockLiveness
[MBB
];
730 BlockInfo
.Begin
.resize(NumSlot
);
731 BlockInfo
.End
.resize(NumSlot
);
733 SmallVector
<int, 4> slots
;
734 for (MachineInstr
&MI
: *MBB
) {
735 bool isStart
= false;
737 if (isLifetimeStartOrEnd(MI
, slots
, isStart
)) {
739 assert(slots
.size() == 1 && "unexpected: MI ends multiple slots");
741 if (BlockInfo
.Begin
.test(Slot
)) {
742 BlockInfo
.Begin
.reset(Slot
);
744 BlockInfo
.End
.set(Slot
);
746 for (auto Slot
: slots
) {
747 LLVM_DEBUG(dbgs() << "Found a use of slot #" << Slot
);
749 << " at " << printMBBReference(*MBB
) << " index ");
750 LLVM_DEBUG(Indexes
->getInstructionIndex(MI
).print(dbgs()));
751 const AllocaInst
*Allocation
= MFI
->getObjectAllocation(Slot
);
754 << " with allocation: " << Allocation
->getName());
756 LLVM_DEBUG(dbgs() << "\n");
757 if (BlockInfo
.End
.test(Slot
)) {
758 BlockInfo
.End
.reset(Slot
);
760 BlockInfo
.Begin
.set(Slot
);
767 // Update statistics.
768 NumMarkerSeen
+= MarkersFound
;
772 void StackColoring::calculateLocalLiveness() {
773 unsigned NumIters
= 0;
779 for (const MachineBasicBlock
*BB
: BasicBlockNumbering
) {
780 // Use an iterator to avoid repeated lookups.
781 LivenessMap::iterator BI
= BlockLiveness
.find(BB
);
782 assert(BI
!= BlockLiveness
.end() && "Block not found");
783 BlockLifetimeInfo
&BlockInfo
= BI
->second
;
785 // Compute LiveIn by unioning together the LiveOut sets of all preds.
786 BitVector LocalLiveIn
;
787 for (MachineBasicBlock
*Pred
: BB
->predecessors()) {
788 LivenessMap::const_iterator I
= BlockLiveness
.find(Pred
);
789 // PR37130: transformations prior to stack coloring can
790 // sometimes leave behind statically unreachable blocks; these
791 // can be safely skipped here.
792 if (I
!= BlockLiveness
.end())
793 LocalLiveIn
|= I
->second
.LiveOut
;
796 // Compute LiveOut by subtracting out lifetimes that end in this
797 // block, then adding in lifetimes that begin in this block. If
798 // we have both BEGIN and END markers in the same basic block
799 // then we know that the BEGIN marker comes after the END,
800 // because we already handle the case where the BEGIN comes
801 // before the END when collecting the markers (and building the
802 // BEGIN/END vectors).
803 BitVector LocalLiveOut
= LocalLiveIn
;
804 LocalLiveOut
.reset(BlockInfo
.End
);
805 LocalLiveOut
|= BlockInfo
.Begin
;
807 // Update block LiveIn set, noting whether it has changed.
808 if (LocalLiveIn
.test(BlockInfo
.LiveIn
)) {
810 BlockInfo
.LiveIn
|= LocalLiveIn
;
813 // Update block LiveOut set, noting whether it has changed.
814 if (LocalLiveOut
.test(BlockInfo
.LiveOut
)) {
816 BlockInfo
.LiveOut
|= LocalLiveOut
;
821 NumIterations
= NumIters
;
824 void StackColoring::calculateLiveIntervals(unsigned NumSlots
) {
825 SmallVector
<SlotIndex
, 16> Starts
;
826 SmallVector
<bool, 16> DefinitelyInUse
;
828 // For each block, find which slots are active within this block
829 // and update the live intervals.
830 for (const MachineBasicBlock
&MBB
: *MF
) {
832 Starts
.resize(NumSlots
);
833 DefinitelyInUse
.clear();
834 DefinitelyInUse
.resize(NumSlots
);
836 // Start the interval of the slots that we previously found to be 'in-use'.
837 BlockLifetimeInfo
&MBBLiveness
= BlockLiveness
[&MBB
];
838 for (int pos
= MBBLiveness
.LiveIn
.find_first(); pos
!= -1;
839 pos
= MBBLiveness
.LiveIn
.find_next(pos
)) {
840 Starts
[pos
] = Indexes
->getMBBStartIdx(&MBB
);
843 // Create the interval for the basic blocks containing lifetime begin/end.
844 for (const MachineInstr
&MI
: MBB
) {
845 SmallVector
<int, 4> slots
;
846 bool IsStart
= false;
847 if (!isLifetimeStartOrEnd(MI
, slots
, IsStart
))
849 SlotIndex ThisIndex
= Indexes
->getInstructionIndex(MI
);
850 for (auto Slot
: slots
) {
852 // If a slot is already definitely in use, we don't have to emit
853 // a new start marker because there is already a pre-existing
855 if (!DefinitelyInUse
[Slot
]) {
856 LiveStarts
[Slot
].push_back(ThisIndex
);
857 DefinitelyInUse
[Slot
] = true;
859 if (!Starts
[Slot
].isValid())
860 Starts
[Slot
] = ThisIndex
;
862 if (Starts
[Slot
].isValid()) {
863 VNInfo
*VNI
= Intervals
[Slot
]->getValNumInfo(0);
864 Intervals
[Slot
]->addSegment(
865 LiveInterval::Segment(Starts
[Slot
], ThisIndex
, VNI
));
866 Starts
[Slot
] = SlotIndex(); // Invalidate the start index
867 DefinitelyInUse
[Slot
] = false;
873 // Finish up started segments
874 for (unsigned i
= 0; i
< NumSlots
; ++i
) {
875 if (!Starts
[i
].isValid())
878 SlotIndex EndIdx
= Indexes
->getMBBEndIdx(&MBB
);
879 VNInfo
*VNI
= Intervals
[i
]->getValNumInfo(0);
880 Intervals
[i
]->addSegment(LiveInterval::Segment(Starts
[i
], EndIdx
, VNI
));
885 bool StackColoring::removeAllMarkers() {
887 for (MachineInstr
*MI
: Markers
) {
888 MI
->eraseFromParent();
893 LLVM_DEBUG(dbgs() << "Removed " << Count
<< " markers.\n");
897 void StackColoring::remapInstructions(DenseMap
<int, int> &SlotRemap
) {
898 unsigned FixedInstr
= 0;
899 unsigned FixedMemOp
= 0;
900 unsigned FixedDbg
= 0;
902 // Remap debug information that refers to stack slots.
903 for (auto &VI
: MF
->getVariableDbgInfo()) {
904 if (!VI
.Var
|| !VI
.inStackSlot())
906 int Slot
= VI
.getStackSlot();
907 if (SlotRemap
.count(Slot
)) {
908 LLVM_DEBUG(dbgs() << "Remapping debug info for ["
909 << cast
<DILocalVariable
>(VI
.Var
)->getName() << "].\n");
910 VI
.updateStackSlot(SlotRemap
[Slot
]);
915 // Keep a list of *allocas* which need to be remapped.
916 DenseMap
<const AllocaInst
*, const AllocaInst
*> Allocas
;
918 // Keep a list of allocas which has been affected by the remap.
919 SmallPtrSet
<const AllocaInst
*, 32> MergedAllocas
;
921 for (const std::pair
<int, int> &SI
: SlotRemap
) {
922 const AllocaInst
*From
= MFI
->getObjectAllocation(SI
.first
);
923 const AllocaInst
*To
= MFI
->getObjectAllocation(SI
.second
);
924 assert(To
&& From
&& "Invalid allocation object");
927 // If From is before wo, its possible that there is a use of From between
929 if (From
->comesBefore(To
))
930 const_cast<AllocaInst
*>(To
)->moveBefore(const_cast<AllocaInst
*>(From
));
932 // AA might be used later for instruction scheduling, and we need it to be
933 // able to deduce the correct aliasing releationships between pointers
934 // derived from the alloca being remapped and the target of that remapping.
935 // The only safe way, without directly informing AA about the remapping
936 // somehow, is to directly update the IR to reflect the change being made
938 Instruction
*Inst
= const_cast<AllocaInst
*>(To
);
939 if (From
->getType() != To
->getType()) {
940 BitCastInst
*Cast
= new BitCastInst(Inst
, From
->getType());
941 Cast
->insertAfter(Inst
);
945 // We keep both slots to maintain AliasAnalysis metadata later.
946 MergedAllocas
.insert(From
);
947 MergedAllocas
.insert(To
);
949 // Transfer the stack protector layout tag, but make sure that SSPLK_AddrOf
950 // does not overwrite SSPLK_SmallArray or SSPLK_LargeArray, and make sure
951 // that SSPLK_SmallArray does not overwrite SSPLK_LargeArray.
952 MachineFrameInfo::SSPLayoutKind FromKind
953 = MFI
->getObjectSSPLayout(SI
.first
);
954 MachineFrameInfo::SSPLayoutKind ToKind
= MFI
->getObjectSSPLayout(SI
.second
);
955 if (FromKind
!= MachineFrameInfo::SSPLK_None
&&
956 (ToKind
== MachineFrameInfo::SSPLK_None
||
957 (ToKind
!= MachineFrameInfo::SSPLK_LargeArray
&&
958 FromKind
!= MachineFrameInfo::SSPLK_AddrOf
)))
959 MFI
->setObjectSSPLayout(SI
.second
, FromKind
);
961 // The new alloca might not be valid in a llvm.dbg.declare for this
962 // variable, so undef out the use to make the verifier happy.
963 AllocaInst
*FromAI
= const_cast<AllocaInst
*>(From
);
964 if (FromAI
->isUsedByMetadata())
965 ValueAsMetadata::handleRAUW(FromAI
, UndefValue::get(FromAI
->getType()));
966 for (auto &Use
: FromAI
->uses()) {
967 if (BitCastInst
*BCI
= dyn_cast
<BitCastInst
>(Use
.get()))
968 if (BCI
->isUsedByMetadata())
969 ValueAsMetadata::handleRAUW(BCI
, UndefValue::get(BCI
->getType()));
972 // Note that this will not replace uses in MMOs (which we'll update below),
973 // or anywhere else (which is why we won't delete the original
975 FromAI
->replaceAllUsesWith(Inst
);
978 // Remap all instructions to the new stack slots.
979 std::vector
<std::vector
<MachineMemOperand
*>> SSRefs(
980 MFI
->getObjectIndexEnd());
981 for (MachineBasicBlock
&BB
: *MF
)
982 for (MachineInstr
&I
: BB
) {
983 // Skip lifetime markers. We'll remove them soon.
984 if (I
.getOpcode() == TargetOpcode::LIFETIME_START
||
985 I
.getOpcode() == TargetOpcode::LIFETIME_END
)
988 // Update the MachineMemOperand to use the new alloca.
989 for (MachineMemOperand
*MMO
: I
.memoperands()) {
990 // We've replaced IR-level uses of the remapped allocas, so we only
991 // need to replace direct uses here.
992 const AllocaInst
*AI
= dyn_cast_or_null
<AllocaInst
>(MMO
->getValue());
996 if (!Allocas
.count(AI
))
999 MMO
->setValue(Allocas
[AI
]);
1003 // Update all of the machine instruction operands.
1004 for (MachineOperand
&MO
: I
.operands()) {
1007 int FromSlot
= MO
.getIndex();
1009 // Don't touch arguments.
1013 // Only look at mapped slots.
1014 if (!SlotRemap
.count(FromSlot
))
1017 // In a debug build, check that the instruction that we are modifying is
1018 // inside the expected live range. If the instruction is not inside
1019 // the calculated range then it means that the alloca usage moved
1020 // outside of the lifetime markers, or that the user has a bug.
1021 // NOTE: Alloca address calculations which happen outside the lifetime
1022 // zone are okay, despite the fact that we don't have a good way
1023 // for validating all of the usages of the calculation.
1025 bool TouchesMemory
= I
.mayLoadOrStore();
1026 // If we *don't* protect the user from escaped allocas, don't bother
1027 // validating the instructions.
1028 if (!I
.isDebugInstr() && TouchesMemory
&& ProtectFromEscapedAllocas
) {
1029 SlotIndex Index
= Indexes
->getInstructionIndex(I
);
1030 const LiveInterval
*Interval
= &*Intervals
[FromSlot
];
1031 assert(Interval
->find(Index
) != Interval
->end() &&
1032 "Found instruction usage outside of live range.");
1036 // Fix the machine instructions.
1037 int ToSlot
= SlotRemap
[FromSlot
];
1038 MO
.setIndex(ToSlot
);
1042 // We adjust AliasAnalysis information for merged stack slots.
1043 SmallVector
<MachineMemOperand
*, 2> NewMMOs
;
1044 bool ReplaceMemOps
= false;
1045 for (MachineMemOperand
*MMO
: I
.memoperands()) {
1046 // Collect MachineMemOperands which reference
1047 // FixedStackPseudoSourceValues with old frame indices.
1048 if (const auto *FSV
= dyn_cast_or_null
<FixedStackPseudoSourceValue
>(
1049 MMO
->getPseudoValue())) {
1050 int FI
= FSV
->getFrameIndex();
1051 auto To
= SlotRemap
.find(FI
);
1052 if (To
!= SlotRemap
.end())
1053 SSRefs
[FI
].push_back(MMO
);
1056 // If this memory location can be a slot remapped here,
1057 // we remove AA information.
1058 bool MayHaveConflictingAAMD
= false;
1059 if (MMO
->getAAInfo()) {
1060 if (const Value
*MMOV
= MMO
->getValue()) {
1061 SmallVector
<Value
*, 4> Objs
;
1062 getUnderlyingObjectsForCodeGen(MMOV
, Objs
);
1065 MayHaveConflictingAAMD
= true;
1067 for (Value
*V
: Objs
) {
1068 // If this memory location comes from a known stack slot
1069 // that is not remapped, we continue checking.
1070 // Otherwise, we need to invalidate AA infomation.
1071 const AllocaInst
*AI
= dyn_cast_or_null
<AllocaInst
>(V
);
1072 if (AI
&& MergedAllocas
.count(AI
)) {
1073 MayHaveConflictingAAMD
= true;
1079 if (MayHaveConflictingAAMD
) {
1080 NewMMOs
.push_back(MF
->getMachineMemOperand(MMO
, AAMDNodes()));
1081 ReplaceMemOps
= true;
1083 NewMMOs
.push_back(MMO
);
1087 // If any memory operand is updated, set memory references of
1088 // this instruction.
1090 I
.setMemRefs(*MF
, NewMMOs
);
1093 // Rewrite MachineMemOperands that reference old frame indices.
1094 for (auto E
: enumerate(SSRefs
))
1095 if (!E
.value().empty()) {
1096 const PseudoSourceValue
*NewSV
=
1097 MF
->getPSVManager().getFixedStack(SlotRemap
.find(E
.index())->second
);
1098 for (MachineMemOperand
*Ref
: E
.value())
1099 Ref
->setValue(NewSV
);
1102 // Update the location of C++ catch objects for the MSVC personality routine.
1103 if (WinEHFuncInfo
*EHInfo
= MF
->getWinEHFuncInfo())
1104 for (WinEHTryBlockMapEntry
&TBME
: EHInfo
->TryBlockMap
)
1105 for (WinEHHandlerType
&H
: TBME
.HandlerArray
)
1106 if (H
.CatchObj
.FrameIndex
!= std::numeric_limits
<int>::max() &&
1107 SlotRemap
.count(H
.CatchObj
.FrameIndex
))
1108 H
.CatchObj
.FrameIndex
= SlotRemap
[H
.CatchObj
.FrameIndex
];
1110 LLVM_DEBUG(dbgs() << "Fixed " << FixedMemOp
<< " machine memory operands.\n");
1111 LLVM_DEBUG(dbgs() << "Fixed " << FixedDbg
<< " debug locations.\n");
1112 LLVM_DEBUG(dbgs() << "Fixed " << FixedInstr
<< " machine instructions.\n");
1118 void StackColoring::removeInvalidSlotRanges() {
1119 for (MachineBasicBlock
&BB
: *MF
)
1120 for (MachineInstr
&I
: BB
) {
1121 if (I
.getOpcode() == TargetOpcode::LIFETIME_START
||
1122 I
.getOpcode() == TargetOpcode::LIFETIME_END
|| I
.isDebugInstr())
1125 // Some intervals are suspicious! In some cases we find address
1126 // calculations outside of the lifetime zone, but not actual memory
1127 // read or write. Memory accesses outside of the lifetime zone are a clear
1128 // violation, but address calculations are okay. This can happen when
1129 // GEPs are hoisted outside of the lifetime zone.
1130 // So, in here we only check instructions which can read or write memory.
1131 if (!I
.mayLoad() && !I
.mayStore())
1134 // Check all of the machine operands.
1135 for (const MachineOperand
&MO
: I
.operands()) {
1139 int Slot
= MO
.getIndex();
1144 if (Intervals
[Slot
]->empty())
1147 // Check that the used slot is inside the calculated lifetime range.
1148 // If it is not, warn about it and invalidate the range.
1149 LiveInterval
*Interval
= &*Intervals
[Slot
];
1150 SlotIndex Index
= Indexes
->getInstructionIndex(I
);
1151 if (Interval
->find(Index
) == Interval
->end()) {
1153 LLVM_DEBUG(dbgs() << "Invalidating range #" << Slot
<< "\n");
1160 void StackColoring::expungeSlotMap(DenseMap
<int, int> &SlotRemap
,
1161 unsigned NumSlots
) {
1162 // Expunge slot remap map.
1163 for (unsigned i
=0; i
< NumSlots
; ++i
) {
1164 // If we are remapping i
1165 if (SlotRemap
.count(i
)) {
1166 int Target
= SlotRemap
[i
];
1167 // As long as our target is mapped to something else, follow it.
1168 while (SlotRemap
.count(Target
)) {
1169 Target
= SlotRemap
[Target
];
1170 SlotRemap
[i
] = Target
;
1176 bool StackColoring::runOnMachineFunction(MachineFunction
&Func
) {
1177 LLVM_DEBUG(dbgs() << "********** Stack Coloring **********\n"
1178 << "********** Function: " << Func
.getName() << '\n');
1180 MFI
= &MF
->getFrameInfo();
1181 Indexes
= &getAnalysis
<SlotIndexes
>();
1182 BlockLiveness
.clear();
1183 BasicBlocks
.clear();
1184 BasicBlockNumbering
.clear();
1188 VNInfoAllocator
.Reset();
1190 unsigned NumSlots
= MFI
->getObjectIndexEnd();
1192 // If there are no stack slots then there are no markers to remove.
1196 SmallVector
<int, 8> SortedSlots
;
1197 SortedSlots
.reserve(NumSlots
);
1198 Intervals
.reserve(NumSlots
);
1199 LiveStarts
.resize(NumSlots
);
1201 unsigned NumMarkers
= collectMarkers(NumSlots
);
1203 unsigned TotalSize
= 0;
1204 LLVM_DEBUG(dbgs() << "Found " << NumMarkers
<< " markers and " << NumSlots
1206 LLVM_DEBUG(dbgs() << "Slot structure:\n");
1208 for (int i
=0; i
< MFI
->getObjectIndexEnd(); ++i
) {
1209 LLVM_DEBUG(dbgs() << "Slot #" << i
<< " - " << MFI
->getObjectSize(i
)
1211 TotalSize
+= MFI
->getObjectSize(i
);
1214 LLVM_DEBUG(dbgs() << "Total Stack size: " << TotalSize
<< " bytes\n\n");
1216 // Don't continue because there are not enough lifetime markers, or the
1217 // stack is too small, or we are told not to optimize the slots.
1218 if (NumMarkers
< 2 || TotalSize
< 16 || DisableColoring
||
1219 skipFunction(Func
.getFunction())) {
1220 LLVM_DEBUG(dbgs() << "Will not try to merge slots.\n");
1221 return removeAllMarkers();
1224 for (unsigned i
=0; i
< NumSlots
; ++i
) {
1225 std::unique_ptr
<LiveInterval
> LI(new LiveInterval(i
, 0));
1226 LI
->getNextValue(Indexes
->getZeroIndex(), VNInfoAllocator
);
1227 Intervals
.push_back(std::move(LI
));
1228 SortedSlots
.push_back(i
);
1231 // Calculate the liveness of each block.
1232 calculateLocalLiveness();
1233 LLVM_DEBUG(dbgs() << "Dataflow iterations: " << NumIterations
<< "\n");
1236 // Propagate the liveness information.
1237 calculateLiveIntervals(NumSlots
);
1238 LLVM_DEBUG(dumpIntervals());
1240 // Search for allocas which are used outside of the declared lifetime
1242 if (ProtectFromEscapedAllocas
)
1243 removeInvalidSlotRanges();
1245 // Maps old slots to new slots.
1246 DenseMap
<int, int> SlotRemap
;
1247 unsigned RemovedSlots
= 0;
1248 unsigned ReducedSize
= 0;
1250 // Do not bother looking at empty intervals.
1251 for (unsigned I
= 0; I
< NumSlots
; ++I
) {
1252 if (Intervals
[SortedSlots
[I
]]->empty())
1253 SortedSlots
[I
] = -1;
1256 // This is a simple greedy algorithm for merging allocas. First, sort the
1257 // slots, placing the largest slots first. Next, perform an n^2 scan and look
1258 // for disjoint slots. When you find disjoint slots, merge the smaller one
1259 // into the bigger one and update the live interval. Remove the small alloca
1262 // Sort the slots according to their size. Place unused slots at the end.
1263 // Use stable sort to guarantee deterministic code generation.
1264 llvm::stable_sort(SortedSlots
, [this](int LHS
, int RHS
) {
1265 // We use -1 to denote a uninteresting slot. Place these slots at the end.
1270 // Sort according to size.
1271 return MFI
->getObjectSize(LHS
) > MFI
->getObjectSize(RHS
);
1274 for (auto &s
: LiveStarts
)
1277 bool Changed
= true;
1280 for (unsigned I
= 0; I
< NumSlots
; ++I
) {
1281 if (SortedSlots
[I
] == -1)
1284 for (unsigned J
=I
+1; J
< NumSlots
; ++J
) {
1285 if (SortedSlots
[J
] == -1)
1288 int FirstSlot
= SortedSlots
[I
];
1289 int SecondSlot
= SortedSlots
[J
];
1291 // Objects with different stack IDs cannot be merged.
1292 if (MFI
->getStackID(FirstSlot
) != MFI
->getStackID(SecondSlot
))
1295 LiveInterval
*First
= &*Intervals
[FirstSlot
];
1296 LiveInterval
*Second
= &*Intervals
[SecondSlot
];
1297 auto &FirstS
= LiveStarts
[FirstSlot
];
1298 auto &SecondS
= LiveStarts
[SecondSlot
];
1299 assert(!First
->empty() && !Second
->empty() && "Found an empty range");
1301 // Merge disjoint slots. This is a little bit tricky - see the
1302 // Implementation Notes section for an explanation.
1303 if (!First
->isLiveAtIndexes(SecondS
) &&
1304 !Second
->isLiveAtIndexes(FirstS
)) {
1306 First
->MergeSegmentsInAsValue(*Second
, First
->getValNumInfo(0));
1308 int OldSize
= FirstS
.size();
1309 FirstS
.append(SecondS
.begin(), SecondS
.end());
1310 auto Mid
= FirstS
.begin() + OldSize
;
1311 std::inplace_merge(FirstS
.begin(), Mid
, FirstS
.end());
1313 SlotRemap
[SecondSlot
] = FirstSlot
;
1314 SortedSlots
[J
] = -1;
1315 LLVM_DEBUG(dbgs() << "Merging #" << FirstSlot
<< " and slots #"
1316 << SecondSlot
<< " together.\n");
1317 Align MaxAlignment
= std::max(MFI
->getObjectAlign(FirstSlot
),
1318 MFI
->getObjectAlign(SecondSlot
));
1320 assert(MFI
->getObjectSize(FirstSlot
) >=
1321 MFI
->getObjectSize(SecondSlot
) &&
1322 "Merging a small object into a larger one");
1325 ReducedSize
+= MFI
->getObjectSize(SecondSlot
);
1326 MFI
->setObjectAlignment(FirstSlot
, MaxAlignment
);
1327 MFI
->RemoveStackObject(SecondSlot
);
1333 // Record statistics.
1334 StackSpaceSaved
+= ReducedSize
;
1335 StackSlotMerged
+= RemovedSlots
;
1336 LLVM_DEBUG(dbgs() << "Merge " << RemovedSlots
<< " slots. Saved "
1337 << ReducedSize
<< " bytes\n");
1339 // Scan the entire function and update all machine operands that use frame
1340 // indices to use the remapped frame index.
1341 expungeSlotMap(SlotRemap
, NumSlots
);
1342 remapInstructions(SlotRemap
);
1344 return removeAllMarkers();