Bug Summary

File:lib/CodeGen/SafeStackLayout.cpp
Warning:line 105, column 5
Value stored to 'LastRegionEnd' is never read

Annotated Source Code

1//===- SafeStackLayout.cpp - SafeStack frame layout -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SafeStackLayout.h"
11#include "SafeStackColoring.h"
12#include "llvm/IR/Value.h"
13#include "llvm/Support/CommandLine.h"
14#include "llvm/Support/Compiler.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/MathExtras.h"
17#include "llvm/Support/raw_ostream.h"
18#include <algorithm>
19#include <cassert>
20
21using namespace llvm;
22using namespace llvm::safestack;
23
24#define DEBUG_TYPE"safestacklayout" "safestacklayout"
25
26static cl::opt<bool> ClLayout("safe-stack-layout",
27 cl::desc("enable safe stack layout"), cl::Hidden,
28 cl::init(true));
29
30LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void StackLayout::print(raw_ostream &OS) {
31 OS << "Stack regions:\n";
32 for (unsigned i = 0; i < Regions.size(); ++i) {
33 OS << " " << i << ": [" << Regions[i].Start << ", " << Regions[i].End
34 << "), range " << Regions[i].Range << "\n";
35 }
36 OS << "Stack objects:\n";
37 for (auto &IT : ObjectOffsets) {
38 OS << " at " << IT.getSecond() << ": " << *IT.getFirst() << "\n";
39 }
40}
41
42void StackLayout::addObject(const Value *V, unsigned Size, unsigned Alignment,
43 const StackColoring::LiveRange &Range) {
44 StackObjects.push_back({V, Size, Alignment, Range});
45 MaxAlignment = std::max(MaxAlignment, Alignment);
46}
47
48static unsigned AdjustStackOffset(unsigned Offset, unsigned Size,
49 unsigned Alignment) {
50 return alignTo(Offset + Size, Alignment) - Size;
51}
52
53void StackLayout::layoutObject(StackObject &Obj) {
54 if (!ClLayout) {
55 // If layout is disabled, just grab the next aligned address.
56 // This effectively disables stack coloring as well.
57 unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
58 unsigned Start = AdjustStackOffset(LastRegionEnd, Obj.Size, Obj.Alignment);
59 unsigned End = Start + Obj.Size;
60 Regions.emplace_back(Start, End, Obj.Range);
61 ObjectOffsets[Obj.Handle] = End;
62 return;
63 }
64
65 DEBUG(dbgs() << "Layout: size " << Obj.Size << ", align " << Obj.Alignmentdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << "Layout: size " <<
Obj.Size << ", align " << Obj.Alignment <<
", range " << Obj.Range << "\n"; } } while (false
)
66 << ", range " << Obj.Range << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << "Layout: size " <<
Obj.Size << ", align " << Obj.Alignment <<
", range " << Obj.Range << "\n"; } } while (false
)
;
67 assert(Obj.Alignment <= MaxAlignment)(static_cast <bool> (Obj.Alignment <= MaxAlignment) ?
void (0) : __assert_fail ("Obj.Alignment <= MaxAlignment"
, "/build/llvm-toolchain-snapshot-6.0~svn320940/lib/CodeGen/SafeStackLayout.cpp"
, 67, __extension__ __PRETTY_FUNCTION__))
;
68 unsigned Start = AdjustStackOffset(0, Obj.Size, Obj.Alignment);
69 unsigned End = Start + Obj.Size;
70 DEBUG(dbgs() << " First candidate: " << Start << " .. " << End << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " First candidate: " <<
Start << " .. " << End << "\n"; } } while (
false)
;
71 for (const StackRegion &R : Regions) {
72 DEBUG(dbgs() << " Examining region: " << R.Start << " .. " << R.Enddo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Examining region: "
<< R.Start << " .. " << R.End << ", range "
<< R.Range << "\n"; } } while (false)
73 << ", range " << R.Range << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Examining region: "
<< R.Start << " .. " << R.End << ", range "
<< R.Range << "\n"; } } while (false)
;
74 assert(End >= R.Start)(static_cast <bool> (End >= R.Start) ? void (0) : __assert_fail
("End >= R.Start", "/build/llvm-toolchain-snapshot-6.0~svn320940/lib/CodeGen/SafeStackLayout.cpp"
, 74, __extension__ __PRETTY_FUNCTION__))
;
75 if (Start >= R.End) {
76 DEBUG(dbgs() << " Does not intersect, skip.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Does not intersect, skip.\n"
; } } while (false)
;
77 continue;
78 }
79 if (Obj.Range.Overlaps(R.Range)) {
80 // Find the next appropriate location.
81 Start = AdjustStackOffset(R.End, Obj.Size, Obj.Alignment);
82 End = Start + Obj.Size;
83 DEBUG(dbgs() << " Overlaps. Next candidate: " << Start << " .. " << Enddo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Overlaps. Next candidate: "
<< Start << " .. " << End << "\n"; }
} while (false)
84 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Overlaps. Next candidate: "
<< Start << " .. " << End << "\n"; }
} while (false)
;
85 continue;
86 }
87 if (End <= R.End) {
88 DEBUG(dbgs() << " Reusing region(s).\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Reusing region(s).\n"
; } } while (false)
;
89 break;
90 }
91 }
92
93 unsigned LastRegionEnd = Regions.empty() ? 0 : Regions.back().End;
94 if (End > LastRegionEnd) {
95 // Insert a new region at the end. Maybe two.
96 if (Start > LastRegionEnd) {
97 DEBUG(dbgs() << " Creating gap region: " << LastRegionEnd << " .. "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating gap region: "
<< LastRegionEnd << " .. " << Start <<
"\n"; } } while (false)
98 << Start << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating gap region: "
<< LastRegionEnd << " .. " << Start <<
"\n"; } } while (false)
;
99 Regions.emplace_back(LastRegionEnd, Start, StackColoring::LiveRange());
100 LastRegionEnd = Start;
101 }
102 DEBUG(dbgs() << " Creating new region: " << LastRegionEnd << " .. " << Enddo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating new region: "
<< LastRegionEnd << " .. " << End <<
", range " << Obj.Range << "\n"; } } while (false
)
103 << ", range " << Obj.Range << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { dbgs() << " Creating new region: "
<< LastRegionEnd << " .. " << End <<
", range " << Obj.Range << "\n"; } } while (false
)
;
104 Regions.emplace_back(LastRegionEnd, End, Obj.Range);
105 LastRegionEnd = End;
Value stored to 'LastRegionEnd' is never read
106 }
107
108 // Split starting and ending regions if necessary.
109 for (unsigned i = 0; i < Regions.size(); ++i) {
110 StackRegion &R = Regions[i];
111 if (Start > R.Start && Start < R.End) {
112 StackRegion R0 = R;
113 R.Start = R0.End = Start;
114 Regions.insert(&R, R0);
115 continue;
116 }
117 if (End > R.Start && End < R.End) {
118 StackRegion R0 = R;
119 R0.End = R.Start = End;
120 Regions.insert(&R, R0);
121 break;
122 }
123 }
124
125 // Update live ranges for all affected regions.
126 for (StackRegion &R : Regions) {
127 if (Start < R.End && End > R.Start)
128 R.Range.Join(Obj.Range);
129 if (End <= R.End)
130 break;
131 }
132
133 ObjectOffsets[Obj.Handle] = End;
134}
135
136void StackLayout::computeLayout() {
137 // Simple greedy algorithm.
138 // If this is replaced with something smarter, it must preserve the property
139 // that the first object is always at the offset 0 in the stack frame (for
140 // StackProtectorSlot), or handle stack protector in some other way.
141
142 // Sort objects by size (largest first) to reduce fragmentation.
143 if (StackObjects.size() > 2)
144 std::stable_sort(StackObjects.begin() + 1, StackObjects.end(),
145 [](const StackObject &a, const StackObject &b) {
146 return a.Size > b.Size;
147 });
148
149 for (auto &Obj : StackObjects)
150 layoutObject(Obj);
151
152 DEBUG(print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("safestacklayout")) { print(dbgs()); } } while (false)
;
153}