Bug Summary

File:llvm/lib/Transforms/Scalar/SROA.cpp
Warning:line 2147, column 3
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SROA.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/build-llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/build-llvm/lib/Transforms/Scalar -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2021-01-24-223304-31662-1 -x c++ /build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp
1//===- SROA.cpp - Scalar Replacement Of Aggregates ------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This transformation implements the well known scalar replacement of
10/// aggregates transformation. It tries to identify promotable elements of an
11/// aggregate alloca, and promote them to registers. It will also try to
12/// convert uses of an element (or set of elements) of an alloca into a vector
13/// or bitfield-style integer scalar if appropriate.
14///
15/// It works to do this with minimal slicing of the alloca so that regions
16/// which are merely transferred in and out of external memory remain unchanged
17/// and are not decomposed to scalar code.
18///
19/// Because this also performs alloca promotion, it can be thought of as also
20/// serving the purpose of SSA formation. The algorithm iterates on the
21/// function until all opportunities for promotion have been realized.
22///
23//===----------------------------------------------------------------------===//
24
25#include "llvm/Transforms/Scalar/SROA.h"
26#include "llvm/ADT/APInt.h"
27#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/PointerIntPair.h"
30#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/SetVector.h"
32#include "llvm/ADT/SmallBitVector.h"
33#include "llvm/ADT/SmallPtrSet.h"
34#include "llvm/ADT/SmallVector.h"
35#include "llvm/ADT/Statistic.h"
36#include "llvm/ADT/StringRef.h"
37#include "llvm/ADT/Twine.h"
38#include "llvm/ADT/iterator.h"
39#include "llvm/ADT/iterator_range.h"
40#include "llvm/Analysis/AssumptionCache.h"
41#include "llvm/Analysis/GlobalsModRef.h"
42#include "llvm/Analysis/Loads.h"
43#include "llvm/Analysis/PtrUseVisitor.h"
44#include "llvm/Config/llvm-config.h"
45#include "llvm/IR/BasicBlock.h"
46#include "llvm/IR/Constant.h"
47#include "llvm/IR/ConstantFolder.h"
48#include "llvm/IR/Constants.h"
49#include "llvm/IR/DIBuilder.h"
50#include "llvm/IR/DataLayout.h"
51#include "llvm/IR/DebugInfoMetadata.h"
52#include "llvm/IR/DerivedTypes.h"
53#include "llvm/IR/Dominators.h"
54#include "llvm/IR/Function.h"
55#include "llvm/IR/GetElementPtrTypeIterator.h"
56#include "llvm/IR/GlobalAlias.h"
57#include "llvm/IR/IRBuilder.h"
58#include "llvm/IR/InstVisitor.h"
59#include "llvm/IR/InstrTypes.h"
60#include "llvm/IR/Instruction.h"
61#include "llvm/IR/Instructions.h"
62#include "llvm/IR/IntrinsicInst.h"
63#include "llvm/IR/Intrinsics.h"
64#include "llvm/IR/LLVMContext.h"
65#include "llvm/IR/Metadata.h"
66#include "llvm/IR/Module.h"
67#include "llvm/IR/Operator.h"
68#include "llvm/IR/PassManager.h"
69#include "llvm/IR/Type.h"
70#include "llvm/IR/Use.h"
71#include "llvm/IR/User.h"
72#include "llvm/IR/Value.h"
73#include "llvm/InitializePasses.h"
74#include "llvm/Pass.h"
75#include "llvm/Support/Casting.h"
76#include "llvm/Support/CommandLine.h"
77#include "llvm/Support/Compiler.h"
78#include "llvm/Support/Debug.h"
79#include "llvm/Support/ErrorHandling.h"
80#include "llvm/Support/MathExtras.h"
81#include "llvm/Support/raw_ostream.h"
82#include "llvm/Transforms/Scalar.h"
83#include "llvm/Transforms/Utils/Local.h"
84#include "llvm/Transforms/Utils/PromoteMemToReg.h"
85#include <algorithm>
86#include <cassert>
87#include <chrono>
88#include <cstddef>
89#include <cstdint>
90#include <cstring>
91#include <iterator>
92#include <string>
93#include <tuple>
94#include <utility>
95#include <vector>
96
97using namespace llvm;
98using namespace llvm::sroa;
99
100#define DEBUG_TYPE"sroa" "sroa"
101
102STATISTIC(NumAllocasAnalyzed, "Number of allocas analyzed for replacement")static llvm::Statistic NumAllocasAnalyzed = {"sroa", "NumAllocasAnalyzed"
, "Number of allocas analyzed for replacement"}
;
103STATISTIC(NumAllocaPartitions, "Number of alloca partitions formed")static llvm::Statistic NumAllocaPartitions = {"sroa", "NumAllocaPartitions"
, "Number of alloca partitions formed"}
;
104STATISTIC(MaxPartitionsPerAlloca, "Maximum number of partitions per alloca")static llvm::Statistic MaxPartitionsPerAlloca = {"sroa", "MaxPartitionsPerAlloca"
, "Maximum number of partitions per alloca"}
;
105STATISTIC(NumAllocaPartitionUses, "Number of alloca partition uses rewritten")static llvm::Statistic NumAllocaPartitionUses = {"sroa", "NumAllocaPartitionUses"
, "Number of alloca partition uses rewritten"}
;
106STATISTIC(MaxUsesPerAllocaPartition, "Maximum number of uses of a partition")static llvm::Statistic MaxUsesPerAllocaPartition = {"sroa", "MaxUsesPerAllocaPartition"
, "Maximum number of uses of a partition"}
;
107STATISTIC(NumNewAllocas, "Number of new, smaller allocas introduced")static llvm::Statistic NumNewAllocas = {"sroa", "NumNewAllocas"
, "Number of new, smaller allocas introduced"}
;
108STATISTIC(NumPromoted, "Number of allocas promoted to SSA values")static llvm::Statistic NumPromoted = {"sroa", "NumPromoted", "Number of allocas promoted to SSA values"
}
;
109STATISTIC(NumLoadsSpeculated, "Number of loads speculated to allow promotion")static llvm::Statistic NumLoadsSpeculated = {"sroa", "NumLoadsSpeculated"
, "Number of loads speculated to allow promotion"}
;
110STATISTIC(NumDeleted, "Number of instructions deleted")static llvm::Statistic NumDeleted = {"sroa", "NumDeleted", "Number of instructions deleted"
}
;
111STATISTIC(NumVectorized, "Number of vectorized aggregates")static llvm::Statistic NumVectorized = {"sroa", "NumVectorized"
, "Number of vectorized aggregates"}
;
112
113/// Hidden option to experiment with completely strict handling of inbounds
114/// GEPs.
115static cl::opt<bool> SROAStrictInbounds("sroa-strict-inbounds", cl::init(false),
116 cl::Hidden);
117
118namespace {
119
120/// A custom IRBuilder inserter which prefixes all names, but only in
121/// Assert builds.
122class IRBuilderPrefixedInserter final : public IRBuilderDefaultInserter {
123 std::string Prefix;
124
125 const Twine getNameWithPrefix(const Twine &Name) const {
126 return Name.isTriviallyEmpty() ? Name : Prefix + Name;
127 }
128
129public:
130 void SetNamePrefix(const Twine &P) { Prefix = P.str(); }
131
132 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
133 BasicBlock::iterator InsertPt) const override {
134 IRBuilderDefaultInserter::InsertHelper(I, getNameWithPrefix(Name), BB,
135 InsertPt);
136 }
137};
138
139/// Provide a type for IRBuilder that drops names in release builds.
140using IRBuilderTy = IRBuilder<ConstantFolder, IRBuilderPrefixedInserter>;
141
142/// A used slice of an alloca.
143///
144/// This structure represents a slice of an alloca used by some instruction. It
145/// stores both the begin and end offsets of this use, a pointer to the use
146/// itself, and a flag indicating whether we can classify the use as splittable
147/// or not when forming partitions of the alloca.
148class Slice {
149 /// The beginning offset of the range.
150 uint64_t BeginOffset = 0;
151
152 /// The ending offset, not included in the range.
153 uint64_t EndOffset = 0;
154
155 /// Storage for both the use of this slice and whether it can be
156 /// split.
157 PointerIntPair<Use *, 1, bool> UseAndIsSplittable;
158
159public:
160 Slice() = default;
161
162 Slice(uint64_t BeginOffset, uint64_t EndOffset, Use *U, bool IsSplittable)
163 : BeginOffset(BeginOffset), EndOffset(EndOffset),
164 UseAndIsSplittable(U, IsSplittable) {}
165
166 uint64_t beginOffset() const { return BeginOffset; }
167 uint64_t endOffset() const { return EndOffset; }
168
169 bool isSplittable() const { return UseAndIsSplittable.getInt(); }
170 void makeUnsplittable() { UseAndIsSplittable.setInt(false); }
171
172 Use *getUse() const { return UseAndIsSplittable.getPointer(); }
173
174 bool isDead() const { return getUse() == nullptr; }
175 void kill() { UseAndIsSplittable.setPointer(nullptr); }
176
177 /// Support for ordering ranges.
178 ///
179 /// This provides an ordering over ranges such that start offsets are
180 /// always increasing, and within equal start offsets, the end offsets are
181 /// decreasing. Thus the spanning range comes first in a cluster with the
182 /// same start position.
183 bool operator<(const Slice &RHS) const {
184 if (beginOffset() < RHS.beginOffset())
185 return true;
186 if (beginOffset() > RHS.beginOffset())
187 return false;
188 if (isSplittable() != RHS.isSplittable())
189 return !isSplittable();
190 if (endOffset() > RHS.endOffset())
191 return true;
192 return false;
193 }
194
195 /// Support comparison with a single offset to allow binary searches.
196 friend LLVM_ATTRIBUTE_UNUSED__attribute__((__unused__)) bool operator<(const Slice &LHS,
197 uint64_t RHSOffset) {
198 return LHS.beginOffset() < RHSOffset;
199 }
200 friend LLVM_ATTRIBUTE_UNUSED__attribute__((__unused__)) bool operator<(uint64_t LHSOffset,
201 const Slice &RHS) {
202 return LHSOffset < RHS.beginOffset();
203 }
204
205 bool operator==(const Slice &RHS) const {
206 return isSplittable() == RHS.isSplittable() &&
207 beginOffset() == RHS.beginOffset() && endOffset() == RHS.endOffset();
208 }
209 bool operator!=(const Slice &RHS) const { return !operator==(RHS); }
210};
211
212} // end anonymous namespace
213
214/// Representation of the alloca slices.
215///
216/// This class represents the slices of an alloca which are formed by its
217/// various uses. If a pointer escapes, we can't fully build a representation
218/// for the slices used and we reflect that in this structure. The uses are
219/// stored, sorted by increasing beginning offset and with unsplittable slices
220/// starting at a particular offset before splittable slices.
221class llvm::sroa::AllocaSlices {
222public:
223 /// Construct the slices of a particular alloca.
224 AllocaSlices(const DataLayout &DL, AllocaInst &AI);
225
226 /// Test whether a pointer to the allocation escapes our analysis.
227 ///
228 /// If this is true, the slices are never fully built and should be
229 /// ignored.
230 bool isEscaped() const { return PointerEscapingInstr; }
231
232 /// Support for iterating over the slices.
233 /// @{
234 using iterator = SmallVectorImpl<Slice>::iterator;
235 using range = iterator_range<iterator>;
236
237 iterator begin() { return Slices.begin(); }
238 iterator end() { return Slices.end(); }
239
240 using const_iterator = SmallVectorImpl<Slice>::const_iterator;
241 using const_range = iterator_range<const_iterator>;
242
243 const_iterator begin() const { return Slices.begin(); }
244 const_iterator end() const { return Slices.end(); }
245 /// @}
246
247 /// Erase a range of slices.
248 void erase(iterator Start, iterator Stop) { Slices.erase(Start, Stop); }
249
250 /// Insert new slices for this alloca.
251 ///
252 /// This moves the slices into the alloca's slices collection, and re-sorts
253 /// everything so that the usual ordering properties of the alloca's slices
254 /// hold.
255 void insert(ArrayRef<Slice> NewSlices) {
256 int OldSize = Slices.size();
257 Slices.append(NewSlices.begin(), NewSlices.end());
258 auto SliceI = Slices.begin() + OldSize;
259 llvm::sort(SliceI, Slices.end());
260 std::inplace_merge(Slices.begin(), SliceI, Slices.end());
261 }
262
263 // Forward declare the iterator and range accessor for walking the
264 // partitions.
265 class partition_iterator;
266 iterator_range<partition_iterator> partitions();
267
268 /// Access the dead users for this alloca.
269 ArrayRef<Instruction *> getDeadUsers() const { return DeadUsers; }
270
271 /// Access Uses that should be dropped if the alloca is promotable.
272 ArrayRef<Use *> getDeadUsesIfPromotable() const {
273 return DeadUseIfPromotable;
274 }
275
276 /// Access the dead operands referring to this alloca.
277 ///
278 /// These are operands which have cannot actually be used to refer to the
279 /// alloca as they are outside its range and the user doesn't correct for
280 /// that. These mostly consist of PHI node inputs and the like which we just
281 /// need to replace with undef.
282 ArrayRef<Use *> getDeadOperands() const { return DeadOperands; }
283
284#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
285 void print(raw_ostream &OS, const_iterator I, StringRef Indent = " ") const;
286 void printSlice(raw_ostream &OS, const_iterator I,
287 StringRef Indent = " ") const;
288 void printUse(raw_ostream &OS, const_iterator I,
289 StringRef Indent = " ") const;
290 void print(raw_ostream &OS) const;
291 void dump(const_iterator I) const;
292 void dump() const;
293#endif
294
295private:
296 template <typename DerivedT, typename RetT = void> class BuilderBase;
297 class SliceBuilder;
298
299 friend class AllocaSlices::SliceBuilder;
300
301#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
302 /// Handle to alloca instruction to simplify method interfaces.
303 AllocaInst &AI;
304#endif
305
306 /// The instruction responsible for this alloca not having a known set
307 /// of slices.
308 ///
309 /// When an instruction (potentially) escapes the pointer to the alloca, we
310 /// store a pointer to that here and abort trying to form slices of the
311 /// alloca. This will be null if the alloca slices are analyzed successfully.
312 Instruction *PointerEscapingInstr;
313
314 /// The slices of the alloca.
315 ///
316 /// We store a vector of the slices formed by uses of the alloca here. This
317 /// vector is sorted by increasing begin offset, and then the unsplittable
318 /// slices before the splittable ones. See the Slice inner class for more
319 /// details.
320 SmallVector<Slice, 8> Slices;
321
322 /// Instructions which will become dead if we rewrite the alloca.
323 ///
324 /// Note that these are not separated by slice. This is because we expect an
325 /// alloca to be completely rewritten or not rewritten at all. If rewritten,
326 /// all these instructions can simply be removed and replaced with undef as
327 /// they come from outside of the allocated space.
328 SmallVector<Instruction *, 8> DeadUsers;
329
330 /// Uses which will become dead if can promote the alloca.
331 SmallVector<Use *, 8> DeadUseIfPromotable;
332
333 /// Operands which will become dead if we rewrite the alloca.
334 ///
335 /// These are operands that in their particular use can be replaced with
336 /// undef when we rewrite the alloca. These show up in out-of-bounds inputs
337 /// to PHI nodes and the like. They aren't entirely dead (there might be
338 /// a GEP back into the bounds using it elsewhere) and nor is the PHI, but we
339 /// want to swap this particular input for undef to simplify the use lists of
340 /// the alloca.
341 SmallVector<Use *, 8> DeadOperands;
342};
343
344/// A partition of the slices.
345///
346/// An ephemeral representation for a range of slices which can be viewed as
347/// a partition of the alloca. This range represents a span of the alloca's
348/// memory which cannot be split, and provides access to all of the slices
349/// overlapping some part of the partition.
350///
351/// Objects of this type are produced by traversing the alloca's slices, but
352/// are only ephemeral and not persistent.
353class llvm::sroa::Partition {
354private:
355 friend class AllocaSlices;
356 friend class AllocaSlices::partition_iterator;
357
358 using iterator = AllocaSlices::iterator;
359
360 /// The beginning and ending offsets of the alloca for this
361 /// partition.
362 uint64_t BeginOffset = 0, EndOffset = 0;
363
364 /// The start and end iterators of this partition.
365 iterator SI, SJ;
366
367 /// A collection of split slice tails overlapping the partition.
368 SmallVector<Slice *, 4> SplitTails;
369
370 /// Raw constructor builds an empty partition starting and ending at
371 /// the given iterator.
372 Partition(iterator SI) : SI(SI), SJ(SI) {}
373
374public:
375 /// The start offset of this partition.
376 ///
377 /// All of the contained slices start at or after this offset.
378 uint64_t beginOffset() const { return BeginOffset; }
379
380 /// The end offset of this partition.
381 ///
382 /// All of the contained slices end at or before this offset.
383 uint64_t endOffset() const { return EndOffset; }
384
385 /// The size of the partition.
386 ///
387 /// Note that this can never be zero.
388 uint64_t size() const {
389 assert(BeginOffset < EndOffset && "Partitions must span some bytes!")((BeginOffset < EndOffset && "Partitions must span some bytes!"
) ? static_cast<void> (0) : __assert_fail ("BeginOffset < EndOffset && \"Partitions must span some bytes!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 389, __PRETTY_FUNCTION__))
;
390 return EndOffset - BeginOffset;
391 }
392
393 /// Test whether this partition contains no slices, and merely spans
394 /// a region occupied by split slices.
395 bool empty() const { return SI == SJ; }
396
397 /// \name Iterate slices that start within the partition.
398 /// These may be splittable or unsplittable. They have a begin offset >= the
399 /// partition begin offset.
400 /// @{
401 // FIXME: We should probably define a "concat_iterator" helper and use that
402 // to stitch together pointee_iterators over the split tails and the
403 // contiguous iterators of the partition. That would give a much nicer
404 // interface here. We could then additionally expose filtered iterators for
405 // split, unsplit, and unsplittable splices based on the usage patterns.
406 iterator begin() const { return SI; }
407 iterator end() const { return SJ; }
408 /// @}
409
410 /// Get the sequence of split slice tails.
411 ///
412 /// These tails are of slices which start before this partition but are
413 /// split and overlap into the partition. We accumulate these while forming
414 /// partitions.
415 ArrayRef<Slice *> splitSliceTails() const { return SplitTails; }
416};
417
418/// An iterator over partitions of the alloca's slices.
419///
420/// This iterator implements the core algorithm for partitioning the alloca's
421/// slices. It is a forward iterator as we don't support backtracking for
422/// efficiency reasons, and re-use a single storage area to maintain the
423/// current set of split slices.
424///
425/// It is templated on the slice iterator type to use so that it can operate
426/// with either const or non-const slice iterators.
427class AllocaSlices::partition_iterator
428 : public iterator_facade_base<partition_iterator, std::forward_iterator_tag,
429 Partition> {
430 friend class AllocaSlices;
431
432 /// Most of the state for walking the partitions is held in a class
433 /// with a nice interface for examining them.
434 Partition P;
435
436 /// We need to keep the end of the slices to know when to stop.
437 AllocaSlices::iterator SE;
438
439 /// We also need to keep track of the maximum split end offset seen.
440 /// FIXME: Do we really?
441 uint64_t MaxSplitSliceEndOffset = 0;
442
443 /// Sets the partition to be empty at given iterator, and sets the
444 /// end iterator.
445 partition_iterator(AllocaSlices::iterator SI, AllocaSlices::iterator SE)
446 : P(SI), SE(SE) {
447 // If not already at the end, advance our state to form the initial
448 // partition.
449 if (SI != SE)
450 advance();
451 }
452
453 /// Advance the iterator to the next partition.
454 ///
455 /// Requires that the iterator not be at the end of the slices.
456 void advance() {
457 assert((P.SI != SE || !P.SplitTails.empty()) &&(((P.SI != SE || !P.SplitTails.empty()) && "Cannot advance past the end of the slices!"
) ? static_cast<void> (0) : __assert_fail ("(P.SI != SE || !P.SplitTails.empty()) && \"Cannot advance past the end of the slices!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 458, __PRETTY_FUNCTION__))
458 "Cannot advance past the end of the slices!")(((P.SI != SE || !P.SplitTails.empty()) && "Cannot advance past the end of the slices!"
) ? static_cast<void> (0) : __assert_fail ("(P.SI != SE || !P.SplitTails.empty()) && \"Cannot advance past the end of the slices!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 458, __PRETTY_FUNCTION__))
;
459
460 // Clear out any split uses which have ended.
461 if (!P.SplitTails.empty()) {
462 if (P.EndOffset >= MaxSplitSliceEndOffset) {
463 // If we've finished all splits, this is easy.
464 P.SplitTails.clear();
465 MaxSplitSliceEndOffset = 0;
466 } else {
467 // Remove the uses which have ended in the prior partition. This
468 // cannot change the max split slice end because we just checked that
469 // the prior partition ended prior to that max.
470 llvm::erase_if(P.SplitTails,
471 [&](Slice *S) { return S->endOffset() <= P.EndOffset; });
472 assert(llvm::any_of(P.SplitTails,((llvm::any_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() == MaxSplitSliceEndOffset; }) && "Could not find the current max split slice offset!"
) ? static_cast<void> (0) : __assert_fail ("llvm::any_of(P.SplitTails, [&](Slice *S) { return S->endOffset() == MaxSplitSliceEndOffset; }) && \"Could not find the current max split slice offset!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 476, __PRETTY_FUNCTION__))
473 [&](Slice *S) {((llvm::any_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() == MaxSplitSliceEndOffset; }) && "Could not find the current max split slice offset!"
) ? static_cast<void> (0) : __assert_fail ("llvm::any_of(P.SplitTails, [&](Slice *S) { return S->endOffset() == MaxSplitSliceEndOffset; }) && \"Could not find the current max split slice offset!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 476, __PRETTY_FUNCTION__))
474 return S->endOffset() == MaxSplitSliceEndOffset;((llvm::any_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() == MaxSplitSliceEndOffset; }) && "Could not find the current max split slice offset!"
) ? static_cast<void> (0) : __assert_fail ("llvm::any_of(P.SplitTails, [&](Slice *S) { return S->endOffset() == MaxSplitSliceEndOffset; }) && \"Could not find the current max split slice offset!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 476, __PRETTY_FUNCTION__))
475 }) &&((llvm::any_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() == MaxSplitSliceEndOffset; }) && "Could not find the current max split slice offset!"
) ? static_cast<void> (0) : __assert_fail ("llvm::any_of(P.SplitTails, [&](Slice *S) { return S->endOffset() == MaxSplitSliceEndOffset; }) && \"Could not find the current max split slice offset!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 476, __PRETTY_FUNCTION__))
476 "Could not find the current max split slice offset!")((llvm::any_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() == MaxSplitSliceEndOffset; }) && "Could not find the current max split slice offset!"
) ? static_cast<void> (0) : __assert_fail ("llvm::any_of(P.SplitTails, [&](Slice *S) { return S->endOffset() == MaxSplitSliceEndOffset; }) && \"Could not find the current max split slice offset!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 476, __PRETTY_FUNCTION__))
;
477 assert(llvm::all_of(P.SplitTails,((llvm::all_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() <= MaxSplitSliceEndOffset; }) && "Max split slice end offset is not actually the max!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(P.SplitTails, [&](Slice *S) { return S->endOffset() <= MaxSplitSliceEndOffset; }) && \"Max split slice end offset is not actually the max!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 481, __PRETTY_FUNCTION__))
478 [&](Slice *S) {((llvm::all_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() <= MaxSplitSliceEndOffset; }) && "Max split slice end offset is not actually the max!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(P.SplitTails, [&](Slice *S) { return S->endOffset() <= MaxSplitSliceEndOffset; }) && \"Max split slice end offset is not actually the max!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 481, __PRETTY_FUNCTION__))
479 return S->endOffset() <= MaxSplitSliceEndOffset;((llvm::all_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() <= MaxSplitSliceEndOffset; }) && "Max split slice end offset is not actually the max!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(P.SplitTails, [&](Slice *S) { return S->endOffset() <= MaxSplitSliceEndOffset; }) && \"Max split slice end offset is not actually the max!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 481, __PRETTY_FUNCTION__))
480 }) &&((llvm::all_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() <= MaxSplitSliceEndOffset; }) && "Max split slice end offset is not actually the max!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(P.SplitTails, [&](Slice *S) { return S->endOffset() <= MaxSplitSliceEndOffset; }) && \"Max split slice end offset is not actually the max!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 481, __PRETTY_FUNCTION__))
481 "Max split slice end offset is not actually the max!")((llvm::all_of(P.SplitTails, [&](Slice *S) { return S->
endOffset() <= MaxSplitSliceEndOffset; }) && "Max split slice end offset is not actually the max!"
) ? static_cast<void> (0) : __assert_fail ("llvm::all_of(P.SplitTails, [&](Slice *S) { return S->endOffset() <= MaxSplitSliceEndOffset; }) && \"Max split slice end offset is not actually the max!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 481, __PRETTY_FUNCTION__))
;
482 }
483 }
484
485 // If P.SI is already at the end, then we've cleared the split tail and
486 // now have an end iterator.
487 if (P.SI == SE) {
488 assert(P.SplitTails.empty() && "Failed to clear the split slices!")((P.SplitTails.empty() && "Failed to clear the split slices!"
) ? static_cast<void> (0) : __assert_fail ("P.SplitTails.empty() && \"Failed to clear the split slices!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 488, __PRETTY_FUNCTION__))
;
489 return;
490 }
491
492 // If we had a non-empty partition previously, set up the state for
493 // subsequent partitions.
494 if (P.SI != P.SJ) {
495 // Accumulate all the splittable slices which started in the old
496 // partition into the split list.
497 for (Slice &S : P)
498 if (S.isSplittable() && S.endOffset() > P.EndOffset) {
499 P.SplitTails.push_back(&S);
500 MaxSplitSliceEndOffset =
501 std::max(S.endOffset(), MaxSplitSliceEndOffset);
502 }
503
504 // Start from the end of the previous partition.
505 P.SI = P.SJ;
506
507 // If P.SI is now at the end, we at most have a tail of split slices.
508 if (P.SI == SE) {
509 P.BeginOffset = P.EndOffset;
510 P.EndOffset = MaxSplitSliceEndOffset;
511 return;
512 }
513
514 // If the we have split slices and the next slice is after a gap and is
515 // not splittable immediately form an empty partition for the split
516 // slices up until the next slice begins.
517 if (!P.SplitTails.empty() && P.SI->beginOffset() != P.EndOffset &&
518 !P.SI->isSplittable()) {
519 P.BeginOffset = P.EndOffset;
520 P.EndOffset = P.SI->beginOffset();
521 return;
522 }
523 }
524
525 // OK, we need to consume new slices. Set the end offset based on the
526 // current slice, and step SJ past it. The beginning offset of the
527 // partition is the beginning offset of the next slice unless we have
528 // pre-existing split slices that are continuing, in which case we begin
529 // at the prior end offset.
530 P.BeginOffset = P.SplitTails.empty() ? P.SI->beginOffset() : P.EndOffset;
531 P.EndOffset = P.SI->endOffset();
532 ++P.SJ;
533
534 // There are two strategies to form a partition based on whether the
535 // partition starts with an unsplittable slice or a splittable slice.
536 if (!P.SI->isSplittable()) {
537 // When we're forming an unsplittable region, it must always start at
538 // the first slice and will extend through its end.
539 assert(P.BeginOffset == P.SI->beginOffset())((P.BeginOffset == P.SI->beginOffset()) ? static_cast<void
> (0) : __assert_fail ("P.BeginOffset == P.SI->beginOffset()"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 539, __PRETTY_FUNCTION__))
;
540
541 // Form a partition including all of the overlapping slices with this
542 // unsplittable slice.
543 while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) {
544 if (!P.SJ->isSplittable())
545 P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset());
546 ++P.SJ;
547 }
548
549 // We have a partition across a set of overlapping unsplittable
550 // partitions.
551 return;
552 }
553
554 // If we're starting with a splittable slice, then we need to form
555 // a synthetic partition spanning it and any other overlapping splittable
556 // splices.
557 assert(P.SI->isSplittable() && "Forming a splittable partition!")((P.SI->isSplittable() && "Forming a splittable partition!"
) ? static_cast<void> (0) : __assert_fail ("P.SI->isSplittable() && \"Forming a splittable partition!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 557, __PRETTY_FUNCTION__))
;
558
559 // Collect all of the overlapping splittable slices.
560 while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset &&
561 P.SJ->isSplittable()) {
562 P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset());
563 ++P.SJ;
564 }
565
566 // Back upiP.EndOffset if we ended the span early when encountering an
567 // unsplittable slice. This synthesizes the early end offset of
568 // a partition spanning only splittable slices.
569 if (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) {
570 assert(!P.SJ->isSplittable())((!P.SJ->isSplittable()) ? static_cast<void> (0) : __assert_fail
("!P.SJ->isSplittable()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 570, __PRETTY_FUNCTION__))
;
571 P.EndOffset = P.SJ->beginOffset();
572 }
573 }
574
575public:
576 bool operator==(const partition_iterator &RHS) const {
577 assert(SE == RHS.SE &&((SE == RHS.SE && "End iterators don't match between compared partition iterators!"
) ? static_cast<void> (0) : __assert_fail ("SE == RHS.SE && \"End iterators don't match between compared partition iterators!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 578, __PRETTY_FUNCTION__))
578 "End iterators don't match between compared partition iterators!")((SE == RHS.SE && "End iterators don't match between compared partition iterators!"
) ? static_cast<void> (0) : __assert_fail ("SE == RHS.SE && \"End iterators don't match between compared partition iterators!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 578, __PRETTY_FUNCTION__))
;
579
580 // The observed positions of partitions is marked by the P.SI iterator and
581 // the emptiness of the split slices. The latter is only relevant when
582 // P.SI == SE, as the end iterator will additionally have an empty split
583 // slices list, but the prior may have the same P.SI and a tail of split
584 // slices.
585 if (P.SI == RHS.P.SI && P.SplitTails.empty() == RHS.P.SplitTails.empty()) {
586 assert(P.SJ == RHS.P.SJ &&((P.SJ == RHS.P.SJ && "Same set of slices formed two different sized partitions!"
) ? static_cast<void> (0) : __assert_fail ("P.SJ == RHS.P.SJ && \"Same set of slices formed two different sized partitions!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 587, __PRETTY_FUNCTION__))
587 "Same set of slices formed two different sized partitions!")((P.SJ == RHS.P.SJ && "Same set of slices formed two different sized partitions!"
) ? static_cast<void> (0) : __assert_fail ("P.SJ == RHS.P.SJ && \"Same set of slices formed two different sized partitions!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 587, __PRETTY_FUNCTION__))
;
588 assert(P.SplitTails.size() == RHS.P.SplitTails.size() &&((P.SplitTails.size() == RHS.P.SplitTails.size() && "Same slice position with differently sized non-empty split "
"slice tails!") ? static_cast<void> (0) : __assert_fail
("P.SplitTails.size() == RHS.P.SplitTails.size() && \"Same slice position with differently sized non-empty split \" \"slice tails!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 590, __PRETTY_FUNCTION__))
589 "Same slice position with differently sized non-empty split "((P.SplitTails.size() == RHS.P.SplitTails.size() && "Same slice position with differently sized non-empty split "
"slice tails!") ? static_cast<void> (0) : __assert_fail
("P.SplitTails.size() == RHS.P.SplitTails.size() && \"Same slice position with differently sized non-empty split \" \"slice tails!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 590, __PRETTY_FUNCTION__))
590 "slice tails!")((P.SplitTails.size() == RHS.P.SplitTails.size() && "Same slice position with differently sized non-empty split "
"slice tails!") ? static_cast<void> (0) : __assert_fail
("P.SplitTails.size() == RHS.P.SplitTails.size() && \"Same slice position with differently sized non-empty split \" \"slice tails!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 590, __PRETTY_FUNCTION__))
;
591 return true;
592 }
593 return false;
594 }
595
596 partition_iterator &operator++() {
597 advance();
598 return *this;
599 }
600
601 Partition &operator*() { return P; }
602};
603
604/// A forward range over the partitions of the alloca's slices.
605///
606/// This accesses an iterator range over the partitions of the alloca's
607/// slices. It computes these partitions on the fly based on the overlapping
608/// offsets of the slices and the ability to split them. It will visit "empty"
609/// partitions to cover regions of the alloca only accessed via split
610/// slices.
611iterator_range<AllocaSlices::partition_iterator> AllocaSlices::partitions() {
612 return make_range(partition_iterator(begin(), end()),
613 partition_iterator(end(), end()));
614}
615
616static Value *foldSelectInst(SelectInst &SI) {
617 // If the condition being selected on is a constant or the same value is
618 // being selected between, fold the select. Yes this does (rarely) happen
619 // early on.
620 if (ConstantInt *CI = dyn_cast<ConstantInt>(SI.getCondition()))
621 return SI.getOperand(1 + CI->isZero());
622 if (SI.getOperand(1) == SI.getOperand(2))
623 return SI.getOperand(1);
624
625 return nullptr;
626}
627
628/// A helper that folds a PHI node or a select.
629static Value *foldPHINodeOrSelectInst(Instruction &I) {
630 if (PHINode *PN = dyn_cast<PHINode>(&I)) {
631 // If PN merges together the same value, return that value.
632 return PN->hasConstantValue();
633 }
634 return foldSelectInst(cast<SelectInst>(I));
635}
636
637/// Builder for the alloca slices.
638///
639/// This class builds a set of alloca slices by recursively visiting the uses
640/// of an alloca and making a slice for each load and store at each offset.
641class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
642 friend class PtrUseVisitor<SliceBuilder>;
643 friend class InstVisitor<SliceBuilder>;
644
645 using Base = PtrUseVisitor<SliceBuilder>;
646
647 const uint64_t AllocSize;
648 AllocaSlices &AS;
649
650 SmallDenseMap<Instruction *, unsigned> MemTransferSliceMap;
651 SmallDenseMap<Instruction *, uint64_t> PHIOrSelectSizes;
652
653 /// Set to de-duplicate dead instructions found in the use walk.
654 SmallPtrSet<Instruction *, 4> VisitedDeadInsts;
655
656public:
657 SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS)
658 : PtrUseVisitor<SliceBuilder>(DL),
659 AllocSize(DL.getTypeAllocSize(AI.getAllocatedType()).getFixedSize()),
660 AS(AS) {}
661
662private:
663 void markAsDead(Instruction &I) {
664 if (VisitedDeadInsts.insert(&I).second)
665 AS.DeadUsers.push_back(&I);
666 }
667
668 void insertUse(Instruction &I, const APInt &Offset, uint64_t Size,
669 bool IsSplittable = false) {
670 // Completely skip uses which have a zero size or start either before or
671 // past the end of the allocation.
672 if (Size == 0 || Offset.uge(AllocSize)) {
673 LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte use @"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte use @" << Offset << " which has zero size or starts outside of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
674 << Offsetdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte use @" << Offset << " which has zero size or starts outside of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
675 << " which has zero size or starts outside of the "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte use @" << Offset << " which has zero size or starts outside of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
676 << AllocSize << " byte alloca:\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte use @" << Offset << " which has zero size or starts outside of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
677 << " alloca: " << AS.AI << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte use @" << Offset << " which has zero size or starts outside of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
678 << " use: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte use @" << Offset << " which has zero size or starts outside of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
;
679 return markAsDead(I);
680 }
681
682 uint64_t BeginOffset = Offset.getZExtValue();
683 uint64_t EndOffset = BeginOffset + Size;
684
685 // Clamp the end offset to the end of the allocation. Note that this is
686 // formulated to handle even the case where "BeginOffset + Size" overflows.
687 // This may appear superficially to be something we could ignore entirely,
688 // but that is not so! There may be widened loads or PHI-node uses where
689 // some instructions are dead but not others. We can't completely ignore
690 // them, and so have to record at least the information here.
691 assert(AllocSize >= BeginOffset)((AllocSize >= BeginOffset) ? static_cast<void> (0) :
__assert_fail ("AllocSize >= BeginOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 691, __PRETTY_FUNCTION__))
; // Established above.
692 if (Size > AllocSize - BeginOffset) {
693 LLVM_DEBUG(dbgs() << "WARNING: Clamping a " << Size << " byte use @"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Clamping a " << Size
<< " byte use @" << Offset << " to remain within the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
694 << Offset << " to remain within the " << AllocSizedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Clamping a " << Size
<< " byte use @" << Offset << " to remain within the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
695 << " byte alloca:\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Clamping a " << Size
<< " byte use @" << Offset << " to remain within the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
696 << " alloca: " << AS.AI << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Clamping a " << Size
<< " byte use @" << Offset << " to remain within the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
697 << " use: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Clamping a " << Size
<< " byte use @" << Offset << " to remain within the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
I << "\n"; } } while (false)
;
698 EndOffset = AllocSize;
699 }
700
701 AS.Slices.push_back(Slice(BeginOffset, EndOffset, U, IsSplittable));
702 }
703
704 void visitBitCastInst(BitCastInst &BC) {
705 if (BC.use_empty())
706 return markAsDead(BC);
707
708 return Base::visitBitCastInst(BC);
709 }
710
711 void visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) {
712 if (ASC.use_empty())
713 return markAsDead(ASC);
714
715 return Base::visitAddrSpaceCastInst(ASC);
716 }
717
718 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
719 if (GEPI.use_empty())
720 return markAsDead(GEPI);
721
722 if (SROAStrictInbounds && GEPI.isInBounds()) {
723 // FIXME: This is a manually un-factored variant of the basic code inside
724 // of GEPs with checking of the inbounds invariant specified in the
725 // langref in a very strict sense. If we ever want to enable
726 // SROAStrictInbounds, this code should be factored cleanly into
727 // PtrUseVisitor, but it is easier to experiment with SROAStrictInbounds
728 // by writing out the code here where we have the underlying allocation
729 // size readily available.
730 APInt GEPOffset = Offset;
731 const DataLayout &DL = GEPI.getModule()->getDataLayout();
732 for (gep_type_iterator GTI = gep_type_begin(GEPI),
733 GTE = gep_type_end(GEPI);
734 GTI != GTE; ++GTI) {
735 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
736 if (!OpC)
737 break;
738
739 // Handle a struct index, which adds its field offset to the pointer.
740 if (StructType *STy = GTI.getStructTypeOrNull()) {
741 unsigned ElementIdx = OpC->getZExtValue();
742 const StructLayout *SL = DL.getStructLayout(STy);
743 GEPOffset +=
744 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
745 } else {
746 // For array or vector indices, scale the index by the size of the
747 // type.
748 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
749 GEPOffset +=
750 Index *
751 APInt(Offset.getBitWidth(),
752 DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize());
753 }
754
755 // If this index has computed an intermediate pointer which is not
756 // inbounds, then the result of the GEP is a poison value and we can
757 // delete it and all uses.
758 if (GEPOffset.ugt(AllocSize))
759 return markAsDead(GEPI);
760 }
761 }
762
763 return Base::visitGetElementPtrInst(GEPI);
764 }
765
766 void handleLoadOrStore(Type *Ty, Instruction &I, const APInt &Offset,
767 uint64_t Size, bool IsVolatile) {
768 // We allow splitting of non-volatile loads and stores where the type is an
769 // integer type. These may be used to implement 'memcpy' or other "transfer
770 // of bits" patterns.
771 bool IsSplittable = Ty->isIntegerTy() && !IsVolatile;
772
773 insertUse(I, Offset, Size, IsSplittable);
774 }
775
776 void visitLoadInst(LoadInst &LI) {
777 assert((!LI.isSimple() || LI.getType()->isSingleValueType()) &&(((!LI.isSimple() || LI.getType()->isSingleValueType()) &&
"All simple FCA loads should have been pre-split") ? static_cast
<void> (0) : __assert_fail ("(!LI.isSimple() || LI.getType()->isSingleValueType()) && \"All simple FCA loads should have been pre-split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 778, __PRETTY_FUNCTION__))
778 "All simple FCA loads should have been pre-split")(((!LI.isSimple() || LI.getType()->isSingleValueType()) &&
"All simple FCA loads should have been pre-split") ? static_cast
<void> (0) : __assert_fail ("(!LI.isSimple() || LI.getType()->isSingleValueType()) && \"All simple FCA loads should have been pre-split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 778, __PRETTY_FUNCTION__))
;
779
780 if (!IsOffsetKnown)
781 return PI.setAborted(&LI);
782
783 if (LI.isVolatile() &&
784 LI.getPointerAddressSpace() != DL.getAllocaAddrSpace())
785 return PI.setAborted(&LI);
786
787 if (isa<ScalableVectorType>(LI.getType()))
788 return PI.setAborted(&LI);
789
790 uint64_t Size = DL.getTypeStoreSize(LI.getType()).getFixedSize();
791 return handleLoadOrStore(LI.getType(), LI, Offset, Size, LI.isVolatile());
792 }
793
794 void visitStoreInst(StoreInst &SI) {
795 Value *ValOp = SI.getValueOperand();
796 if (ValOp == *U)
797 return PI.setEscapedAndAborted(&SI);
798 if (!IsOffsetKnown)
799 return PI.setAborted(&SI);
800
801 if (SI.isVolatile() &&
802 SI.getPointerAddressSpace() != DL.getAllocaAddrSpace())
803 return PI.setAborted(&SI);
804
805 if (isa<ScalableVectorType>(ValOp->getType()))
806 return PI.setAborted(&SI);
807
808 uint64_t Size = DL.getTypeStoreSize(ValOp->getType()).getFixedSize();
809
810 // If this memory access can be shown to *statically* extend outside the
811 // bounds of the allocation, it's behavior is undefined, so simply
812 // ignore it. Note that this is more strict than the generic clamping
813 // behavior of insertUse. We also try to handle cases which might run the
814 // risk of overflow.
815 // FIXME: We should instead consider the pointer to have escaped if this
816 // function is being instrumented for addressing bugs or race conditions.
817 if (Size > AllocSize || Offset.ugt(AllocSize - Size)) {
818 LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte store @"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte store @" << Offset << " which extends past the end of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
SI << "\n"; } } while (false)
819 << Offset << " which extends past the end of the "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte store @" << Offset << " which extends past the end of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
SI << "\n"; } } while (false)
820 << AllocSize << " byte alloca:\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte store @" << Offset << " which extends past the end of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
SI << "\n"; } } while (false)
821 << " alloca: " << AS.AI << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte store @" << Offset << " which extends past the end of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
SI << "\n"; } } while (false)
822 << " use: " << SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "WARNING: Ignoring " << Size
<< " byte store @" << Offset << " which extends past the end of the "
<< AllocSize << " byte alloca:\n" << " alloca: "
<< AS.AI << "\n" << " use: " <<
SI << "\n"; } } while (false)
;
823 return markAsDead(SI);
824 }
825
826 assert((!SI.isSimple() || ValOp->getType()->isSingleValueType()) &&(((!SI.isSimple() || ValOp->getType()->isSingleValueType
()) && "All simple FCA stores should have been pre-split"
) ? static_cast<void> (0) : __assert_fail ("(!SI.isSimple() || ValOp->getType()->isSingleValueType()) && \"All simple FCA stores should have been pre-split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 827, __PRETTY_FUNCTION__))
827 "All simple FCA stores should have been pre-split")(((!SI.isSimple() || ValOp->getType()->isSingleValueType
()) && "All simple FCA stores should have been pre-split"
) ? static_cast<void> (0) : __assert_fail ("(!SI.isSimple() || ValOp->getType()->isSingleValueType()) && \"All simple FCA stores should have been pre-split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 827, __PRETTY_FUNCTION__))
;
828 handleLoadOrStore(ValOp->getType(), SI, Offset, Size, SI.isVolatile());
829 }
830
831 void visitMemSetInst(MemSetInst &II) {
832 assert(II.getRawDest() == *U && "Pointer use is not the destination?")((II.getRawDest() == *U && "Pointer use is not the destination?"
) ? static_cast<void> (0) : __assert_fail ("II.getRawDest() == *U && \"Pointer use is not the destination?\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 832, __PRETTY_FUNCTION__))
;
833 ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength());
834 if ((Length && Length->getValue() == 0) ||
835 (IsOffsetKnown && Offset.uge(AllocSize)))
836 // Zero-length mem transfer intrinsics can be ignored entirely.
837 return markAsDead(II);
838
839 if (!IsOffsetKnown)
840 return PI.setAborted(&II);
841
842 // Don't replace this with a store with a different address space. TODO:
843 // Use a store with the casted new alloca?
844 if (II.isVolatile() && II.getDestAddressSpace() != DL.getAllocaAddrSpace())
845 return PI.setAborted(&II);
846
847 insertUse(II, Offset, Length ? Length->getLimitedValue()
848 : AllocSize - Offset.getLimitedValue(),
849 (bool)Length);
850 }
851
852 void visitMemTransferInst(MemTransferInst &II) {
853 ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength());
854 if (Length && Length->getValue() == 0)
855 // Zero-length mem transfer intrinsics can be ignored entirely.
856 return markAsDead(II);
857
858 // Because we can visit these intrinsics twice, also check to see if the
859 // first time marked this instruction as dead. If so, skip it.
860 if (VisitedDeadInsts.count(&II))
861 return;
862
863 if (!IsOffsetKnown)
864 return PI.setAborted(&II);
865
866 // Don't replace this with a load/store with a different address space.
867 // TODO: Use a store with the casted new alloca?
868 if (II.isVolatile() &&
869 (II.getDestAddressSpace() != DL.getAllocaAddrSpace() ||
870 II.getSourceAddressSpace() != DL.getAllocaAddrSpace()))
871 return PI.setAborted(&II);
872
873 // This side of the transfer is completely out-of-bounds, and so we can
874 // nuke the entire transfer. However, we also need to nuke the other side
875 // if already added to our partitions.
876 // FIXME: Yet another place we really should bypass this when
877 // instrumenting for ASan.
878 if (Offset.uge(AllocSize)) {
879 SmallDenseMap<Instruction *, unsigned>::iterator MTPI =
880 MemTransferSliceMap.find(&II);
881 if (MTPI != MemTransferSliceMap.end())
882 AS.Slices[MTPI->second].kill();
883 return markAsDead(II);
884 }
885
886 uint64_t RawOffset = Offset.getLimitedValue();
887 uint64_t Size = Length ? Length->getLimitedValue() : AllocSize - RawOffset;
888
889 // Check for the special case where the same exact value is used for both
890 // source and dest.
891 if (*U == II.getRawDest() && *U == II.getRawSource()) {
892 // For non-volatile transfers this is a no-op.
893 if (!II.isVolatile())
894 return markAsDead(II);
895
896 return insertUse(II, Offset, Size, /*IsSplittable=*/false);
897 }
898
899 // If we have seen both source and destination for a mem transfer, then
900 // they both point to the same alloca.
901 bool Inserted;
902 SmallDenseMap<Instruction *, unsigned>::iterator MTPI;
903 std::tie(MTPI, Inserted) =
904 MemTransferSliceMap.insert(std::make_pair(&II, AS.Slices.size()));
905 unsigned PrevIdx = MTPI->second;
906 if (!Inserted) {
907 Slice &PrevP = AS.Slices[PrevIdx];
908
909 // Check if the begin offsets match and this is a non-volatile transfer.
910 // In that case, we can completely elide the transfer.
911 if (!II.isVolatile() && PrevP.beginOffset() == RawOffset) {
912 PrevP.kill();
913 return markAsDead(II);
914 }
915
916 // Otherwise we have an offset transfer within the same alloca. We can't
917 // split those.
918 PrevP.makeUnsplittable();
919 }
920
921 // Insert the use now that we've fixed up the splittable nature.
922 insertUse(II, Offset, Size, /*IsSplittable=*/Inserted && Length);
923
924 // Check that we ended up with a valid index in the map.
925 assert(AS.Slices[PrevIdx].getUse()->getUser() == &II &&((AS.Slices[PrevIdx].getUse()->getUser() == &II &&
"Map index doesn't point back to a slice with this user.") ?
static_cast<void> (0) : __assert_fail ("AS.Slices[PrevIdx].getUse()->getUser() == &II && \"Map index doesn't point back to a slice with this user.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 926, __PRETTY_FUNCTION__))
926 "Map index doesn't point back to a slice with this user.")((AS.Slices[PrevIdx].getUse()->getUser() == &II &&
"Map index doesn't point back to a slice with this user.") ?
static_cast<void> (0) : __assert_fail ("AS.Slices[PrevIdx].getUse()->getUser() == &II && \"Map index doesn't point back to a slice with this user.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 926, __PRETTY_FUNCTION__))
;
927 }
928
929 // Disable SRoA for any intrinsics except for lifetime invariants.
930 // FIXME: What about debug intrinsics? This matches old behavior, but
931 // doesn't make sense.
932 void visitIntrinsicInst(IntrinsicInst &II) {
933 if (II.isDroppable()) {
934 AS.DeadUseIfPromotable.push_back(U);
935 return;
936 }
937
938 if (!IsOffsetKnown)
939 return PI.setAborted(&II);
940
941 if (II.isLifetimeStartOrEnd()) {
942 ConstantInt *Length = cast<ConstantInt>(II.getArgOperand(0));
943 uint64_t Size = std::min(AllocSize - Offset.getLimitedValue(),
944 Length->getLimitedValue());
945 insertUse(II, Offset, Size, true);
946 return;
947 }
948
949 Base::visitIntrinsicInst(II);
950 }
951
952 Instruction *hasUnsafePHIOrSelectUse(Instruction *Root, uint64_t &Size) {
953 // We consider any PHI or select that results in a direct load or store of
954 // the same offset to be a viable use for slicing purposes. These uses
955 // are considered unsplittable and the size is the maximum loaded or stored
956 // size.
957 SmallPtrSet<Instruction *, 4> Visited;
958 SmallVector<std::pair<Instruction *, Instruction *>, 4> Uses;
959 Visited.insert(Root);
960 Uses.push_back(std::make_pair(cast<Instruction>(*U), Root));
961 const DataLayout &DL = Root->getModule()->getDataLayout();
962 // If there are no loads or stores, the access is dead. We mark that as
963 // a size zero access.
964 Size = 0;
965 do {
966 Instruction *I, *UsedI;
967 std::tie(UsedI, I) = Uses.pop_back_val();
968
969 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
970 Size = std::max(Size,
971 DL.getTypeStoreSize(LI->getType()).getFixedSize());
972 continue;
973 }
974 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
975 Value *Op = SI->getOperand(0);
976 if (Op == UsedI)
977 return SI;
978 Size = std::max(Size,
979 DL.getTypeStoreSize(Op->getType()).getFixedSize());
980 continue;
981 }
982
983 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
984 if (!GEP->hasAllZeroIndices())
985 return GEP;
986 } else if (!isa<BitCastInst>(I) && !isa<PHINode>(I) &&
987 !isa<SelectInst>(I) && !isa<AddrSpaceCastInst>(I)) {
988 return I;
989 }
990
991 for (User *U : I->users())
992 if (Visited.insert(cast<Instruction>(U)).second)
993 Uses.push_back(std::make_pair(I, cast<Instruction>(U)));
994 } while (!Uses.empty());
995
996 return nullptr;
997 }
998
999 void visitPHINodeOrSelectInst(Instruction &I) {
1000 assert(isa<PHINode>(I) || isa<SelectInst>(I))((isa<PHINode>(I) || isa<SelectInst>(I)) ? static_cast
<void> (0) : __assert_fail ("isa<PHINode>(I) || isa<SelectInst>(I)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1000, __PRETTY_FUNCTION__))
;
1001 if (I.use_empty())
1002 return markAsDead(I);
1003
1004 // TODO: We could use SimplifyInstruction here to fold PHINodes and
1005 // SelectInsts. However, doing so requires to change the current
1006 // dead-operand-tracking mechanism. For instance, suppose neither loading
1007 // from %U nor %other traps. Then "load (select undef, %U, %other)" does not
1008 // trap either. However, if we simply replace %U with undef using the
1009 // current dead-operand-tracking mechanism, "load (select undef, undef,
1010 // %other)" may trap because the select may return the first operand
1011 // "undef".
1012 if (Value *Result = foldPHINodeOrSelectInst(I)) {
1013 if (Result == *U)
1014 // If the result of the constant fold will be the pointer, recurse
1015 // through the PHI/select as if we had RAUW'ed it.
1016 enqueueUsers(I);
1017 else
1018 // Otherwise the operand to the PHI/select is dead, and we can replace
1019 // it with undef.
1020 AS.DeadOperands.push_back(U);
1021
1022 return;
1023 }
1024
1025 if (!IsOffsetKnown)
1026 return PI.setAborted(&I);
1027
1028 // See if we already have computed info on this node.
1029 uint64_t &Size = PHIOrSelectSizes[&I];
1030 if (!Size) {
1031 // This is a new PHI/Select, check for an unsafe use of it.
1032 if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&I, Size))
1033 return PI.setAborted(UnsafeI);
1034 }
1035
1036 // For PHI and select operands outside the alloca, we can't nuke the entire
1037 // phi or select -- the other side might still be relevant, so we special
1038 // case them here and use a separate structure to track the operands
1039 // themselves which should be replaced with undef.
1040 // FIXME: This should instead be escaped in the event we're instrumenting
1041 // for address sanitization.
1042 if (Offset.uge(AllocSize)) {
1043 AS.DeadOperands.push_back(U);
1044 return;
1045 }
1046
1047 insertUse(I, Offset, Size);
1048 }
1049
1050 void visitPHINode(PHINode &PN) { visitPHINodeOrSelectInst(PN); }
1051
1052 void visitSelectInst(SelectInst &SI) { visitPHINodeOrSelectInst(SI); }
1053
1054 /// Disable SROA entirely if there are unhandled users of the alloca.
1055 void visitInstruction(Instruction &I) { PI.setAborted(&I); }
1056};
1057
1058AllocaSlices::AllocaSlices(const DataLayout &DL, AllocaInst &AI)
1059 :
1060#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1061 AI(AI),
1062#endif
1063 PointerEscapingInstr(nullptr) {
1064 SliceBuilder PB(DL, AI, *this);
1065 SliceBuilder::PtrInfo PtrI = PB.visitPtr(AI);
1066 if (PtrI.isEscaped() || PtrI.isAborted()) {
1067 // FIXME: We should sink the escape vs. abort info into the caller nicely,
1068 // possibly by just storing the PtrInfo in the AllocaSlices.
1069 PointerEscapingInstr = PtrI.getEscapingInst() ? PtrI.getEscapingInst()
1070 : PtrI.getAbortingInst();
1071 assert(PointerEscapingInstr && "Did not track a bad instruction")((PointerEscapingInstr && "Did not track a bad instruction"
) ? static_cast<void> (0) : __assert_fail ("PointerEscapingInstr && \"Did not track a bad instruction\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1071, __PRETTY_FUNCTION__))
;
1072 return;
1073 }
1074
1075 llvm::erase_if(Slices, [](const Slice &S) { return S.isDead(); });
1076
1077 // Sort the uses. This arranges for the offsets to be in ascending order,
1078 // and the sizes to be in descending order.
1079 llvm::stable_sort(Slices);
1080}
1081
1082#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1083
1084void AllocaSlices::print(raw_ostream &OS, const_iterator I,
1085 StringRef Indent) const {
1086 printSlice(OS, I, Indent);
1087 OS << "\n";
1088 printUse(OS, I, Indent);
1089}
1090
1091void AllocaSlices::printSlice(raw_ostream &OS, const_iterator I,
1092 StringRef Indent) const {
1093 OS << Indent << "[" << I->beginOffset() << "," << I->endOffset() << ")"
1094 << " slice #" << (I - begin())
1095 << (I->isSplittable() ? " (splittable)" : "");
1096}
1097
1098void AllocaSlices::printUse(raw_ostream &OS, const_iterator I,
1099 StringRef Indent) const {
1100 OS << Indent << " used by: " << *I->getUse()->getUser() << "\n";
1101}
1102
1103void AllocaSlices::print(raw_ostream &OS) const {
1104 if (PointerEscapingInstr) {
1105 OS << "Can't analyze slices for alloca: " << AI << "\n"
1106 << " A pointer to this alloca escaped by:\n"
1107 << " " << *PointerEscapingInstr << "\n";
1108 return;
1109 }
1110
1111 OS << "Slices of alloca: " << AI << "\n";
1112 for (const_iterator I = begin(), E = end(); I != E; ++I)
1113 print(OS, I);
1114}
1115
1116LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void AllocaSlices::dump(const_iterator I) const {
1117 print(dbgs(), I);
1118}
1119LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void AllocaSlices::dump() const { print(dbgs()); }
1120
1121#endif // !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1122
1123/// Walk the range of a partitioning looking for a common type to cover this
1124/// sequence of slices.
1125static std::pair<Type *, IntegerType *>
1126findCommonType(AllocaSlices::const_iterator B, AllocaSlices::const_iterator E,
1127 uint64_t EndOffset) {
1128 Type *Ty = nullptr;
1129 bool TyIsCommon = true;
1130 IntegerType *ITy = nullptr;
1131
1132 // Note that we need to look at *every* alloca slice's Use to ensure we
1133 // always get consistent results regardless of the order of slices.
1134 for (AllocaSlices::const_iterator I = B; I != E; ++I) {
1135 Use *U = I->getUse();
1136 if (isa<IntrinsicInst>(*U->getUser()))
1137 continue;
1138 if (I->beginOffset() != B->beginOffset() || I->endOffset() != EndOffset)
1139 continue;
1140
1141 Type *UserTy = nullptr;
1142 if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
1143 UserTy = LI->getType();
1144 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
1145 UserTy = SI->getValueOperand()->getType();
1146 }
1147
1148 if (IntegerType *UserITy = dyn_cast_or_null<IntegerType>(UserTy)) {
1149 // If the type is larger than the partition, skip it. We only encounter
1150 // this for split integer operations where we want to use the type of the
1151 // entity causing the split. Also skip if the type is not a byte width
1152 // multiple.
1153 if (UserITy->getBitWidth() % 8 != 0 ||
1154 UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset()))
1155 continue;
1156
1157 // Track the largest bitwidth integer type used in this way in case there
1158 // is no common type.
1159 if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth())
1160 ITy = UserITy;
1161 }
1162
1163 // To avoid depending on the order of slices, Ty and TyIsCommon must not
1164 // depend on types skipped above.
1165 if (!UserTy || (Ty && Ty != UserTy))
1166 TyIsCommon = false; // Give up on anything but an iN type.
1167 else
1168 Ty = UserTy;
1169 }
1170
1171 return {TyIsCommon ? Ty : nullptr, ITy};
1172}
1173
1174/// PHI instructions that use an alloca and are subsequently loaded can be
1175/// rewritten to load both input pointers in the pred blocks and then PHI the
1176/// results, allowing the load of the alloca to be promoted.
1177/// From this:
1178/// %P2 = phi [i32* %Alloca, i32* %Other]
1179/// %V = load i32* %P2
1180/// to:
1181/// %V1 = load i32* %Alloca -> will be mem2reg'd
1182/// ...
1183/// %V2 = load i32* %Other
1184/// ...
1185/// %V = phi [i32 %V1, i32 %V2]
1186///
1187/// We can do this to a select if its only uses are loads and if the operands
1188/// to the select can be loaded unconditionally.
1189///
1190/// FIXME: This should be hoisted into a generic utility, likely in
1191/// Transforms/Util/Local.h
1192static bool isSafePHIToSpeculate(PHINode &PN) {
1193 const DataLayout &DL = PN.getModule()->getDataLayout();
1194
1195 // For now, we can only do this promotion if the load is in the same block
1196 // as the PHI, and if there are no stores between the phi and load.
1197 // TODO: Allow recursive phi users.
1198 // TODO: Allow stores.
1199 BasicBlock *BB = PN.getParent();
1200 Align MaxAlign;
1201 uint64_t APWidth = DL.getIndexTypeSizeInBits(PN.getType());
1202 APInt MaxSize(APWidth, 0);
1203 bool HaveLoad = false;
1204 for (User *U : PN.users()) {
1205 LoadInst *LI = dyn_cast<LoadInst>(U);
1206 if (!LI || !LI->isSimple())
1207 return false;
1208
1209 // For now we only allow loads in the same block as the PHI. This is
1210 // a common case that happens when instcombine merges two loads through
1211 // a PHI.
1212 if (LI->getParent() != BB)
1213 return false;
1214
1215 // Ensure that there are no instructions between the PHI and the load that
1216 // could store.
1217 for (BasicBlock::iterator BBI(PN); &*BBI != LI; ++BBI)
1218 if (BBI->mayWriteToMemory())
1219 return false;
1220
1221 uint64_t Size = DL.getTypeStoreSize(LI->getType()).getFixedSize();
1222 MaxAlign = std::max(MaxAlign, LI->getAlign());
1223 MaxSize = MaxSize.ult(Size) ? APInt(APWidth, Size) : MaxSize;
1224 HaveLoad = true;
1225 }
1226
1227 if (!HaveLoad)
1228 return false;
1229
1230 // We can only transform this if it is safe to push the loads into the
1231 // predecessor blocks. The only thing to watch out for is that we can't put
1232 // a possibly trapping load in the predecessor if it is a critical edge.
1233 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1234 Instruction *TI = PN.getIncomingBlock(Idx)->getTerminator();
1235 Value *InVal = PN.getIncomingValue(Idx);
1236
1237 // If the value is produced by the terminator of the predecessor (an
1238 // invoke) or it has side-effects, there is no valid place to put a load
1239 // in the predecessor.
1240 if (TI == InVal || TI->mayHaveSideEffects())
1241 return false;
1242
1243 // If the predecessor has a single successor, then the edge isn't
1244 // critical.
1245 if (TI->getNumSuccessors() == 1)
1246 continue;
1247
1248 // If this pointer is always safe to load, or if we can prove that there
1249 // is already a load in the block, then we can move the load to the pred
1250 // block.
1251 if (isSafeToLoadUnconditionally(InVal, MaxAlign, MaxSize, DL, TI))
1252 continue;
1253
1254 return false;
1255 }
1256
1257 return true;
1258}
1259
1260static void speculatePHINodeLoads(PHINode &PN) {
1261 LLVM_DEBUG(dbgs() << " original: " << PN << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << PN <<
"\n"; } } while (false)
;
1262
1263 LoadInst *SomeLoad = cast<LoadInst>(PN.user_back());
1264 Type *LoadTy = SomeLoad->getType();
1265 IRBuilderTy PHIBuilder(&PN);
1266 PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(),
1267 PN.getName() + ".sroa.speculated");
1268
1269 // Get the AA tags and alignment to use from one of the loads. It does not
1270 // matter which one we get and if any differ.
1271 AAMDNodes AATags;
1272 SomeLoad->getAAMetadata(AATags);
1273 Align Alignment = SomeLoad->getAlign();
1274
1275 // Rewrite all loads of the PN to use the new PHI.
1276 while (!PN.use_empty()) {
1277 LoadInst *LI = cast<LoadInst>(PN.user_back());
1278 LI->replaceAllUsesWith(NewPN);
1279 LI->eraseFromParent();
1280 }
1281
1282 // Inject loads into all of the pred blocks.
1283 DenseMap<BasicBlock*, Value*> InjectedLoads;
1284 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1285 BasicBlock *Pred = PN.getIncomingBlock(Idx);
1286 Value *InVal = PN.getIncomingValue(Idx);
1287
1288 // A PHI node is allowed to have multiple (duplicated) entries for the same
1289 // basic block, as long as the value is the same. So if we already injected
1290 // a load in the predecessor, then we should reuse the same load for all
1291 // duplicated entries.
1292 if (Value* V = InjectedLoads.lookup(Pred)) {
1293 NewPN->addIncoming(V, Pred);
1294 continue;
1295 }
1296
1297 Instruction *TI = Pred->getTerminator();
1298 IRBuilderTy PredBuilder(TI);
1299
1300 LoadInst *Load = PredBuilder.CreateAlignedLoad(
1301 LoadTy, InVal, Alignment,
1302 (PN.getName() + ".sroa.speculate.load." + Pred->getName()));
1303 ++NumLoadsSpeculated;
1304 if (AATags)
1305 Load->setAAMetadata(AATags);
1306 NewPN->addIncoming(Load, Pred);
1307 InjectedLoads[Pred] = Load;
1308 }
1309
1310 LLVM_DEBUG(dbgs() << " speculated to: " << *NewPN << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " speculated to: " <<
*NewPN << "\n"; } } while (false)
;
1311 PN.eraseFromParent();
1312}
1313
1314/// Select instructions that use an alloca and are subsequently loaded can be
1315/// rewritten to load both input pointers and then select between the result,
1316/// allowing the load of the alloca to be promoted.
1317/// From this:
1318/// %P2 = select i1 %cond, i32* %Alloca, i32* %Other
1319/// %V = load i32* %P2
1320/// to:
1321/// %V1 = load i32* %Alloca -> will be mem2reg'd
1322/// %V2 = load i32* %Other
1323/// %V = select i1 %cond, i32 %V1, i32 %V2
1324///
1325/// We can do this to a select if its only uses are loads and if the operand
1326/// to the select can be loaded unconditionally.
1327static bool isSafeSelectToSpeculate(SelectInst &SI) {
1328 Value *TValue = SI.getTrueValue();
1329 Value *FValue = SI.getFalseValue();
1330 const DataLayout &DL = SI.getModule()->getDataLayout();
1331
1332 for (User *U : SI.users()) {
1333 LoadInst *LI = dyn_cast<LoadInst>(U);
1334 if (!LI || !LI->isSimple())
1335 return false;
1336
1337 // Both operands to the select need to be dereferenceable, either
1338 // absolutely (e.g. allocas) or at this point because we can see other
1339 // accesses to it.
1340 if (!isSafeToLoadUnconditionally(TValue, LI->getType(),
1341 LI->getAlign(), DL, LI))
1342 return false;
1343 if (!isSafeToLoadUnconditionally(FValue, LI->getType(),
1344 LI->getAlign(), DL, LI))
1345 return false;
1346 }
1347
1348 return true;
1349}
1350
1351static void speculateSelectInstLoads(SelectInst &SI) {
1352 LLVM_DEBUG(dbgs() << " original: " << SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << SI <<
"\n"; } } while (false)
;
1353
1354 IRBuilderTy IRB(&SI);
1355 Value *TV = SI.getTrueValue();
1356 Value *FV = SI.getFalseValue();
1357 // Replace the loads of the select with a select of two loads.
1358 while (!SI.use_empty()) {
1359 LoadInst *LI = cast<LoadInst>(SI.user_back());
1360 assert(LI->isSimple() && "We only speculate simple loads")((LI->isSimple() && "We only speculate simple loads"
) ? static_cast<void> (0) : __assert_fail ("LI->isSimple() && \"We only speculate simple loads\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1360, __PRETTY_FUNCTION__))
;
1361
1362 IRB.SetInsertPoint(LI);
1363 LoadInst *TL = IRB.CreateLoad(LI->getType(), TV,
1364 LI->getName() + ".sroa.speculate.load.true");
1365 LoadInst *FL = IRB.CreateLoad(LI->getType(), FV,
1366 LI->getName() + ".sroa.speculate.load.false");
1367 NumLoadsSpeculated += 2;
1368
1369 // Transfer alignment and AA info if present.
1370 TL->setAlignment(LI->getAlign());
1371 FL->setAlignment(LI->getAlign());
1372
1373 AAMDNodes Tags;
1374 LI->getAAMetadata(Tags);
1375 if (Tags) {
1376 TL->setAAMetadata(Tags);
1377 FL->setAAMetadata(Tags);
1378 }
1379
1380 Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL,
1381 LI->getName() + ".sroa.speculated");
1382
1383 LLVM_DEBUG(dbgs() << " speculated to: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " speculated to: " <<
*V << "\n"; } } while (false)
;
1384 LI->replaceAllUsesWith(V);
1385 LI->eraseFromParent();
1386 }
1387 SI.eraseFromParent();
1388}
1389
1390/// Build a GEP out of a base pointer and indices.
1391///
1392/// This will return the BasePtr if that is valid, or build a new GEP
1393/// instruction using the IRBuilder if GEP-ing is needed.
1394static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr,
1395 SmallVectorImpl<Value *> &Indices,
1396 const Twine &NamePrefix) {
1397 if (Indices.empty())
1398 return BasePtr;
1399
1400 // A single zero index is a no-op, so check for this and avoid building a GEP
1401 // in that case.
1402 if (Indices.size() == 1 && cast<ConstantInt>(Indices.back())->isZero())
1403 return BasePtr;
1404
1405 return IRB.CreateInBoundsGEP(BasePtr->getType()->getPointerElementType(),
1406 BasePtr, Indices, NamePrefix + "sroa_idx");
1407}
1408
1409/// Get a natural GEP off of the BasePtr walking through Ty toward
1410/// TargetTy without changing the offset of the pointer.
1411///
1412/// This routine assumes we've already established a properly offset GEP with
1413/// Indices, and arrived at the Ty type. The goal is to continue to GEP with
1414/// zero-indices down through type layers until we find one the same as
1415/// TargetTy. If we can't find one with the same type, we at least try to use
1416/// one with the same size. If none of that works, we just produce the GEP as
1417/// indicated by Indices to have the correct offset.
1418static Value *getNaturalGEPWithType(IRBuilderTy &IRB, const DataLayout &DL,
1419 Value *BasePtr, Type *Ty, Type *TargetTy,
1420 SmallVectorImpl<Value *> &Indices,
1421 const Twine &NamePrefix) {
1422 if (Ty == TargetTy)
1423 return buildGEP(IRB, BasePtr, Indices, NamePrefix);
1424
1425 // Offset size to use for the indices.
1426 unsigned OffsetSize = DL.getIndexTypeSizeInBits(BasePtr->getType());
1427
1428 // See if we can descend into a struct and locate a field with the correct
1429 // type.
1430 unsigned NumLayers = 0;
1431 Type *ElementTy = Ty;
1432 do {
1433 if (ElementTy->isPointerTy())
1434 break;
1435
1436 if (ArrayType *ArrayTy = dyn_cast<ArrayType>(ElementTy)) {
1437 ElementTy = ArrayTy->getElementType();
1438 Indices.push_back(IRB.getIntN(OffsetSize, 0));
1439 } else if (VectorType *VectorTy = dyn_cast<VectorType>(ElementTy)) {
1440 ElementTy = VectorTy->getElementType();
1441 Indices.push_back(IRB.getInt32(0));
1442 } else if (StructType *STy = dyn_cast<StructType>(ElementTy)) {
1443 if (STy->element_begin() == STy->element_end())
1444 break; // Nothing left to descend into.
1445 ElementTy = *STy->element_begin();
1446 Indices.push_back(IRB.getInt32(0));
1447 } else {
1448 break;
1449 }
1450 ++NumLayers;
1451 } while (ElementTy != TargetTy);
1452 if (ElementTy != TargetTy)
1453 Indices.erase(Indices.end() - NumLayers, Indices.end());
1454
1455 return buildGEP(IRB, BasePtr, Indices, NamePrefix);
1456}
1457
1458/// Recursively compute indices for a natural GEP.
1459///
1460/// This is the recursive step for getNaturalGEPWithOffset that walks down the
1461/// element types adding appropriate indices for the GEP.
1462static Value *getNaturalGEPRecursively(IRBuilderTy &IRB, const DataLayout &DL,
1463 Value *Ptr, Type *Ty, APInt &Offset,
1464 Type *TargetTy,
1465 SmallVectorImpl<Value *> &Indices,
1466 const Twine &NamePrefix) {
1467 if (Offset == 0)
1468 return getNaturalGEPWithType(IRB, DL, Ptr, Ty, TargetTy, Indices,
1469 NamePrefix);
1470
1471 // We can't recurse through pointer types.
1472 if (Ty->isPointerTy())
1473 return nullptr;
1474
1475 // We try to analyze GEPs over vectors here, but note that these GEPs are
1476 // extremely poorly defined currently. The long-term goal is to remove GEPing
1477 // over a vector from the IR completely.
1478 if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) {
1479 unsigned ElementSizeInBits =
1480 DL.getTypeSizeInBits(VecTy->getScalarType()).getFixedSize();
1481 if (ElementSizeInBits % 8 != 0) {
1482 // GEPs over non-multiple of 8 size vector elements are invalid.
1483 return nullptr;
1484 }
1485 APInt ElementSize(Offset.getBitWidth(), ElementSizeInBits / 8);
1486 APInt NumSkippedElements = Offset.sdiv(ElementSize);
1487 if (NumSkippedElements.ugt(cast<FixedVectorType>(VecTy)->getNumElements()))
1488 return nullptr;
1489 Offset -= NumSkippedElements * ElementSize;
1490 Indices.push_back(IRB.getInt(NumSkippedElements));
1491 return getNaturalGEPRecursively(IRB, DL, Ptr, VecTy->getElementType(),
1492 Offset, TargetTy, Indices, NamePrefix);
1493 }
1494
1495 if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) {
1496 Type *ElementTy = ArrTy->getElementType();
1497 APInt ElementSize(Offset.getBitWidth(),
1498 DL.getTypeAllocSize(ElementTy).getFixedSize());
1499 APInt NumSkippedElements = Offset.sdiv(ElementSize);
1500 if (NumSkippedElements.ugt(ArrTy->getNumElements()))
1501 return nullptr;
1502
1503 Offset -= NumSkippedElements * ElementSize;
1504 Indices.push_back(IRB.getInt(NumSkippedElements));
1505 return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy,
1506 Indices, NamePrefix);
1507 }
1508
1509 StructType *STy = dyn_cast<StructType>(Ty);
1510 if (!STy)
1511 return nullptr;
1512
1513 const StructLayout *SL = DL.getStructLayout(STy);
1514 uint64_t StructOffset = Offset.getZExtValue();
1515 if (StructOffset >= SL->getSizeInBytes())
1516 return nullptr;
1517 unsigned Index = SL->getElementContainingOffset(StructOffset);
1518 Offset -= APInt(Offset.getBitWidth(), SL->getElementOffset(Index));
1519 Type *ElementTy = STy->getElementType(Index);
1520 if (Offset.uge(DL.getTypeAllocSize(ElementTy).getFixedSize()))
1521 return nullptr; // The offset points into alignment padding.
1522
1523 Indices.push_back(IRB.getInt32(Index));
1524 return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy,
1525 Indices, NamePrefix);
1526}
1527
1528/// Get a natural GEP from a base pointer to a particular offset and
1529/// resulting in a particular type.
1530///
1531/// The goal is to produce a "natural" looking GEP that works with the existing
1532/// composite types to arrive at the appropriate offset and element type for
1533/// a pointer. TargetTy is the element type the returned GEP should point-to if
1534/// possible. We recurse by decreasing Offset, adding the appropriate index to
1535/// Indices, and setting Ty to the result subtype.
1536///
1537/// If no natural GEP can be constructed, this function returns null.
1538static Value *getNaturalGEPWithOffset(IRBuilderTy &IRB, const DataLayout &DL,
1539 Value *Ptr, APInt Offset, Type *TargetTy,
1540 SmallVectorImpl<Value *> &Indices,
1541 const Twine &NamePrefix) {
1542 PointerType *Ty = cast<PointerType>(Ptr->getType());
29
The object is a 'PointerType'
1543
1544 // Don't consider any GEPs through an i8* as natural unless the TargetTy is
1545 // an i8.
1546 if (Ty == IRB.getInt8PtrTy(Ty->getAddressSpace()) && TargetTy->isIntegerTy(8))
30
Assuming the condition is false
1547 return nullptr;
1548
1549 Type *ElementTy = Ty->getElementType();
1550 if (!ElementTy->isSized())
31
Assuming the condition is false
32
Taking false branch
1551 return nullptr; // We can't GEP through an unsized element.
1552 if (isa<ScalableVectorType>(ElementTy))
33
Assuming 'ElementTy' is not a 'ScalableVectorType'
34
Taking false branch
1553 return nullptr;
1554 APInt ElementSize(Offset.getBitWidth(),
1555 DL.getTypeAllocSize(ElementTy).getFixedSize());
1556 if (ElementSize == 0)
35
Taking false branch
1557 return nullptr; // Zero-length arrays can't help us build a natural GEP.
1558 APInt NumSkippedElements = Offset.sdiv(ElementSize);
1559
1560 Offset -= NumSkippedElements * ElementSize;
1561 Indices.push_back(IRB.getInt(NumSkippedElements));
1562 return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy,
36
Value assigned to field 'VecTy', which participates in a condition later
37
Value assigned to field 'IntTy', which participates in a condition later
1563 Indices, NamePrefix);
1564}
1565
1566/// Compute an adjusted pointer from Ptr by Offset bytes where the
1567/// resulting pointer has PointerTy.
1568///
1569/// This tries very hard to compute a "natural" GEP which arrives at the offset
1570/// and produces the pointer type desired. Where it cannot, it will try to use
1571/// the natural GEP to arrive at the offset and bitcast to the type. Where that
1572/// fails, it will try to use an existing i8* and GEP to the byte offset and
1573/// bitcast to the type.
1574///
1575/// The strategy for finding the more natural GEPs is to peel off layers of the
1576/// pointer, walking back through bit casts and GEPs, searching for a base
1577/// pointer from which we can compute a natural GEP with the desired
1578/// properties. The algorithm tries to fold as many constant indices into
1579/// a single GEP as possible, thus making each GEP more independent of the
1580/// surrounding code.
1581static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
1582 APInt Offset, Type *PointerTy,
1583 const Twine &NamePrefix) {
1584 // Even though we don't look through PHI nodes, we could be called on an
1585 // instruction in an unreachable block, which may be on a cycle.
1586 SmallPtrSet<Value *, 4> Visited;
1587 Visited.insert(Ptr);
1588 SmallVector<Value *, 4> Indices;
1589
1590 // We may end up computing an offset pointer that has the wrong type. If we
1591 // never are able to compute one directly that has the correct type, we'll
1592 // fall back to it, so keep it and the base it was computed from around here.
1593 Value *OffsetPtr = nullptr;
1594 Value *OffsetBasePtr;
1595
1596 // Remember any i8 pointer we come across to re-use if we need to do a raw
1597 // byte offset.
1598 Value *Int8Ptr = nullptr;
1599 APInt Int8PtrOffset(Offset.getBitWidth(), 0);
1600
1601 PointerType *TargetPtrTy = cast<PointerType>(PointerTy);
24
'PointerTy' is a 'PointerType'
1602 Type *TargetTy = TargetPtrTy->getElementType();
1603
1604 // As `addrspacecast` is , `Ptr` (the storage pointer) may have different
1605 // address space from the expected `PointerTy` (the pointer to be used).
1606 // Adjust the pointer type based the original storage pointer.
1607 auto AS = cast<PointerType>(Ptr->getType())->getAddressSpace();
25
The object is a 'PointerType'
1608 PointerTy = TargetTy->getPointerTo(AS);
1609
1610 do {
1611 // First fold any existing GEPs into the offset.
1612 while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
26
Assuming 'Ptr' is not a 'GEPOperator'
27
Loop condition is false. Execution continues on line 1623
1613 APInt GEPOffset(Offset.getBitWidth(), 0);
1614 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
1615 break;
1616 Offset += GEPOffset;
1617 Ptr = GEP->getPointerOperand();
1618 if (!Visited.insert(Ptr).second)
1619 break;
1620 }
1621
1622 // See if we can perform a natural GEP here.
1623 Indices.clear();
1624 if (Value *P = getNaturalGEPWithOffset(IRB, DL, Ptr, Offset, TargetTy,
28
Calling 'getNaturalGEPWithOffset'
38
Returning from 'getNaturalGEPWithOffset'
39
Assuming 'P' is null
40
Taking false branch
1625 Indices, NamePrefix)) {
1626 // If we have a new natural pointer at the offset, clear out any old
1627 // offset pointer we computed. Unless it is the base pointer or
1628 // a non-instruction, we built a GEP we don't need. Zap it.
1629 if (OffsetPtr && OffsetPtr != OffsetBasePtr)
1630 if (Instruction *I = dyn_cast<Instruction>(OffsetPtr)) {
1631 assert(I->use_empty() && "Built a GEP with uses some how!")((I->use_empty() && "Built a GEP with uses some how!"
) ? static_cast<void> (0) : __assert_fail ("I->use_empty() && \"Built a GEP with uses some how!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1631, __PRETTY_FUNCTION__))
;
1632 I->eraseFromParent();
1633 }
1634 OffsetPtr = P;
1635 OffsetBasePtr = Ptr;
1636 // If we also found a pointer of the right type, we're done.
1637 if (P->getType() == PointerTy)
1638 break;
1639 }
1640
1641 // Stash this pointer if we've found an i8*.
1642 if (Ptr->getType()->isIntegerTy(8)) {
41
Assuming the condition is false
42
Taking false branch
1643 Int8Ptr = Ptr;
1644 Int8PtrOffset = Offset;
1645 }
1646
1647 // Peel off a layer of the pointer and update the offset appropriately.
1648 if (Operator::getOpcode(Ptr) == Instruction::BitCast) {
43
Taking false branch
1649 Ptr = cast<Operator>(Ptr)->getOperand(0);
1650 } else if (GlobalAlias *GA
44.1
'GA' is null
= dyn_cast<GlobalAlias>(Ptr)) {
44
Assuming 'Ptr' is not a 'GlobalAlias'
45
Taking false branch
1651 if (GA->isInterposable())
1652 break;
1653 Ptr = GA->getAliasee();
1654 } else {
1655 break;
46
Execution continues on line 1660
1656 }
1657 assert(Ptr->getType()->isPointerTy() && "Unexpected operand type!")((Ptr->getType()->isPointerTy() && "Unexpected operand type!"
) ? static_cast<void> (0) : __assert_fail ("Ptr->getType()->isPointerTy() && \"Unexpected operand type!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1657, __PRETTY_FUNCTION__))
;
1658 } while (Visited.insert(Ptr).second);
1659
1660 if (!OffsetPtr
46.1
'OffsetPtr' is null
) {
47
Taking true branch
1661 if (!Int8Ptr
47.1
'Int8Ptr' is null
) {
48
Taking true branch
1662 Int8Ptr = IRB.CreateBitCast(
1663 Ptr, IRB.getInt8PtrTy(PointerTy->getPointerAddressSpace()),
1664 NamePrefix + "sroa_raw_cast");
1665 Int8PtrOffset = Offset;
1666 }
1667
1668 OffsetPtr = Int8PtrOffset == 0
49
'?' condition is false
1669 ? Int8Ptr
1670 : IRB.CreateInBoundsGEP(IRB.getInt8Ty(), Int8Ptr,
1671 IRB.getInt(Int8PtrOffset),
1672 NamePrefix + "sroa_raw_idx");
1673 }
1674 Ptr = OffsetPtr;
1675
1676 // On the off chance we were targeting i8*, guard the bitcast here.
1677 if (cast<PointerType>(Ptr->getType()) != TargetPtrTy) {
50
The object is a 'PointerType'
51
Assuming the condition is false
52
Taking false branch
1678 Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr,
1679 TargetPtrTy,
1680 NamePrefix + "sroa_cast");
1681 }
1682
1683 return Ptr;
1684}
1685
1686/// Compute the adjusted alignment for a load or store from an offset.
1687static Align getAdjustedAlignment(Instruction *I, uint64_t Offset) {
1688 return commonAlignment(getLoadStoreAlignment(I), Offset);
1689}
1690
1691/// Test whether we can convert a value from the old to the new type.
1692///
1693/// This predicate should be used to guard calls to convertValue in order to
1694/// ensure that we only try to convert viable values. The strategy is that we
1695/// will peel off single element struct and array wrappings to get to an
1696/// underlying value, and convert that value.
1697static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy) {
1698 if (OldTy == NewTy)
1699 return true;
1700
1701 // For integer types, we can't handle any bit-width differences. This would
1702 // break both vector conversions with extension and introduce endianness
1703 // issues when in conjunction with loads and stores.
1704 if (isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) {
1705 assert(cast<IntegerType>(OldTy)->getBitWidth() !=((cast<IntegerType>(OldTy)->getBitWidth() != cast<
IntegerType>(NewTy)->getBitWidth() && "We can't have the same bitwidth for different int types"
) ? static_cast<void> (0) : __assert_fail ("cast<IntegerType>(OldTy)->getBitWidth() != cast<IntegerType>(NewTy)->getBitWidth() && \"We can't have the same bitwidth for different int types\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1707, __PRETTY_FUNCTION__))
1706 cast<IntegerType>(NewTy)->getBitWidth() &&((cast<IntegerType>(OldTy)->getBitWidth() != cast<
IntegerType>(NewTy)->getBitWidth() && "We can't have the same bitwidth for different int types"
) ? static_cast<void> (0) : __assert_fail ("cast<IntegerType>(OldTy)->getBitWidth() != cast<IntegerType>(NewTy)->getBitWidth() && \"We can't have the same bitwidth for different int types\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1707, __PRETTY_FUNCTION__))
1707 "We can't have the same bitwidth for different int types")((cast<IntegerType>(OldTy)->getBitWidth() != cast<
IntegerType>(NewTy)->getBitWidth() && "We can't have the same bitwidth for different int types"
) ? static_cast<void> (0) : __assert_fail ("cast<IntegerType>(OldTy)->getBitWidth() != cast<IntegerType>(NewTy)->getBitWidth() && \"We can't have the same bitwidth for different int types\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1707, __PRETTY_FUNCTION__))
;
1708 return false;
1709 }
1710
1711 if (DL.getTypeSizeInBits(NewTy).getFixedSize() !=
1712 DL.getTypeSizeInBits(OldTy).getFixedSize())
1713 return false;
1714 if (!NewTy->isSingleValueType() || !OldTy->isSingleValueType())
1715 return false;
1716
1717 // We can convert pointers to integers and vice-versa. Same for vectors
1718 // of pointers and integers.
1719 OldTy = OldTy->getScalarType();
1720 NewTy = NewTy->getScalarType();
1721 if (NewTy->isPointerTy() || OldTy->isPointerTy()) {
1722 if (NewTy->isPointerTy() && OldTy->isPointerTy()) {
1723 unsigned OldAS = OldTy->getPointerAddressSpace();
1724 unsigned NewAS = NewTy->getPointerAddressSpace();
1725 // Convert pointers if they are pointers from the same address space or
1726 // different integral (not non-integral) address spaces with the same
1727 // pointer size.
1728 return OldAS == NewAS ||
1729 (!DL.isNonIntegralAddressSpace(OldAS) &&
1730 !DL.isNonIntegralAddressSpace(NewAS) &&
1731 DL.getPointerSize(OldAS) == DL.getPointerSize(NewAS));
1732 }
1733
1734 // We can convert integers to integral pointers, but not to non-integral
1735 // pointers.
1736 if (OldTy->isIntegerTy())
1737 return !DL.isNonIntegralPointerType(NewTy);
1738
1739 // We can convert integral pointers to integers, but non-integral pointers
1740 // need to remain pointers.
1741 if (!DL.isNonIntegralPointerType(OldTy))
1742 return NewTy->isIntegerTy();
1743
1744 return false;
1745 }
1746
1747 return true;
1748}
1749
1750/// Generic routine to convert an SSA value to a value of a different
1751/// type.
1752///
1753/// This will try various different casting techniques, such as bitcasts,
1754/// inttoptr, and ptrtoint casts. Use the \c canConvertValue predicate to test
1755/// two types for viability with this routine.
1756static Value *convertValue(const DataLayout &DL, IRBuilderTy &IRB, Value *V,
1757 Type *NewTy) {
1758 Type *OldTy = V->getType();
1759 assert(canConvertValue(DL, OldTy, NewTy) && "Value not convertable to type")((canConvertValue(DL, OldTy, NewTy) && "Value not convertable to type"
) ? static_cast<void> (0) : __assert_fail ("canConvertValue(DL, OldTy, NewTy) && \"Value not convertable to type\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1759, __PRETTY_FUNCTION__))
;
1760
1761 if (OldTy == NewTy)
1762 return V;
1763
1764 assert(!(isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) &&((!(isa<IntegerType>(OldTy) && isa<IntegerType
>(NewTy)) && "Integer types must be the exact same to convert."
) ? static_cast<void> (0) : __assert_fail ("!(isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) && \"Integer types must be the exact same to convert.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1765, __PRETTY_FUNCTION__))
1765 "Integer types must be the exact same to convert.")((!(isa<IntegerType>(OldTy) && isa<IntegerType
>(NewTy)) && "Integer types must be the exact same to convert."
) ? static_cast<void> (0) : __assert_fail ("!(isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) && \"Integer types must be the exact same to convert.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1765, __PRETTY_FUNCTION__))
;
1766
1767 // See if we need inttoptr for this type pair. May require additional bitcast.
1768 if (OldTy->isIntOrIntVectorTy() && NewTy->isPtrOrPtrVectorTy()) {
1769 // Expand <2 x i32> to i8* --> <2 x i32> to i64 to i8*
1770 // Expand i128 to <2 x i8*> --> i128 to <2 x i64> to <2 x i8*>
1771 // Expand <4 x i32> to <2 x i8*> --> <4 x i32> to <2 x i64> to <2 x i8*>
1772 // Directly handle i64 to i8*
1773 return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)),
1774 NewTy);
1775 }
1776
1777 // See if we need ptrtoint for this type pair. May require additional bitcast.
1778 if (OldTy->isPtrOrPtrVectorTy() && NewTy->isIntOrIntVectorTy()) {
1779 // Expand <2 x i8*> to i128 --> <2 x i8*> to <2 x i64> to i128
1780 // Expand i8* to <2 x i32> --> i8* to i64 to <2 x i32>
1781 // Expand <2 x i8*> to <4 x i32> --> <2 x i8*> to <2 x i64> to <4 x i32>
1782 // Expand i8* to i64 --> i8* to i64 to i64
1783 return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)),
1784 NewTy);
1785 }
1786
1787 if (OldTy->isPtrOrPtrVectorTy() && NewTy->isPtrOrPtrVectorTy()) {
1788 unsigned OldAS = OldTy->getPointerAddressSpace();
1789 unsigned NewAS = NewTy->getPointerAddressSpace();
1790 // To convert pointers with different address spaces (they are already
1791 // checked convertible, i.e. they have the same pointer size), so far we
1792 // cannot use `bitcast` (which has restrict on the same address space) or
1793 // `addrspacecast` (which is not always no-op casting). Instead, use a pair
1794 // of no-op `ptrtoint`/`inttoptr` casts through an integer with the same bit
1795 // size.
1796 if (OldAS != NewAS) {
1797 assert(DL.getPointerSize(OldAS) == DL.getPointerSize(NewAS))((DL.getPointerSize(OldAS) == DL.getPointerSize(NewAS)) ? static_cast
<void> (0) : __assert_fail ("DL.getPointerSize(OldAS) == DL.getPointerSize(NewAS)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1797, __PRETTY_FUNCTION__))
;
1798 return IRB.CreateIntToPtr(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)),
1799 NewTy);
1800 }
1801 }
1802
1803 return IRB.CreateBitCast(V, NewTy);
1804}
1805
1806/// Test whether the given slice use can be promoted to a vector.
1807///
1808/// This function is called to test each entry in a partition which is slated
1809/// for a single slice.
1810static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S,
1811 VectorType *Ty,
1812 uint64_t ElementSize,
1813 const DataLayout &DL) {
1814 // First validate the slice offsets.
1815 uint64_t BeginOffset =
1816 std::max(S.beginOffset(), P.beginOffset()) - P.beginOffset();
1817 uint64_t BeginIndex = BeginOffset / ElementSize;
1818 if (BeginIndex * ElementSize != BeginOffset ||
1819 BeginIndex >= cast<FixedVectorType>(Ty)->getNumElements())
1820 return false;
1821 uint64_t EndOffset =
1822 std::min(S.endOffset(), P.endOffset()) - P.beginOffset();
1823 uint64_t EndIndex = EndOffset / ElementSize;
1824 if (EndIndex * ElementSize != EndOffset ||
1825 EndIndex > cast<FixedVectorType>(Ty)->getNumElements())
1826 return false;
1827
1828 assert(EndIndex > BeginIndex && "Empty vector!")((EndIndex > BeginIndex && "Empty vector!") ? static_cast
<void> (0) : __assert_fail ("EndIndex > BeginIndex && \"Empty vector!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1828, __PRETTY_FUNCTION__))
;
1829 uint64_t NumElements = EndIndex - BeginIndex;
1830 Type *SliceTy = (NumElements == 1)
1831 ? Ty->getElementType()
1832 : FixedVectorType::get(Ty->getElementType(), NumElements);
1833
1834 Type *SplitIntTy =
1835 Type::getIntNTy(Ty->getContext(), NumElements * ElementSize * 8);
1836
1837 Use *U = S.getUse();
1838
1839 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) {
1840 if (MI->isVolatile())
1841 return false;
1842 if (!S.isSplittable())
1843 return false; // Skip any unsplittable intrinsics.
1844 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) {
1845 if (!II->isLifetimeStartOrEnd() && !II->isDroppable())
1846 return false;
1847 } else if (U->get()->getType()->getPointerElementType()->isStructTy()) {
1848 // Disable vector promotion when there are loads or stores of an FCA.
1849 return false;
1850 } else if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
1851 if (LI->isVolatile())
1852 return false;
1853 Type *LTy = LI->getType();
1854 if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) {
1855 assert(LTy->isIntegerTy())((LTy->isIntegerTy()) ? static_cast<void> (0) : __assert_fail
("LTy->isIntegerTy()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1855, __PRETTY_FUNCTION__))
;
1856 LTy = SplitIntTy;
1857 }
1858 if (!canConvertValue(DL, SliceTy, LTy))
1859 return false;
1860 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
1861 if (SI->isVolatile())
1862 return false;
1863 Type *STy = SI->getValueOperand()->getType();
1864 if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) {
1865 assert(STy->isIntegerTy())((STy->isIntegerTy()) ? static_cast<void> (0) : __assert_fail
("STy->isIntegerTy()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1865, __PRETTY_FUNCTION__))
;
1866 STy = SplitIntTy;
1867 }
1868 if (!canConvertValue(DL, STy, SliceTy))
1869 return false;
1870 } else {
1871 return false;
1872 }
1873
1874 return true;
1875}
1876
1877/// Test whether the given alloca partitioning and range of slices can be
1878/// promoted to a vector.
1879///
1880/// This is a quick test to check whether we can rewrite a particular alloca
1881/// partition (and its newly formed alloca) into a vector alloca with only
1882/// whole-vector loads and stores such that it could be promoted to a vector
1883/// SSA value. We only can ensure this for a limited set of operations, and we
1884/// don't want to do the rewrites unless we are confident that the result will
1885/// be promotable, so we have an early test here.
1886static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) {
1887 // Collect the candidate types for vector-based promotion. Also track whether
1888 // we have different element types.
1889 SmallVector<VectorType *, 4> CandidateTys;
1890 Type *CommonEltTy = nullptr;
1891 bool HaveCommonEltTy = true;
1892 auto CheckCandidateType = [&](Type *Ty) {
1893 if (auto *VTy = dyn_cast<VectorType>(Ty)) {
1894 // Return if bitcast to vectors is different for total size in bits.
1895 if (!CandidateTys.empty()) {
1896 VectorType *V = CandidateTys[0];
1897 if (DL.getTypeSizeInBits(VTy).getFixedSize() !=
1898 DL.getTypeSizeInBits(V).getFixedSize()) {
1899 CandidateTys.clear();
1900 return;
1901 }
1902 }
1903 CandidateTys.push_back(VTy);
1904 if (!CommonEltTy)
1905 CommonEltTy = VTy->getElementType();
1906 else if (CommonEltTy != VTy->getElementType())
1907 HaveCommonEltTy = false;
1908 }
1909 };
1910 // Consider any loads or stores that are the exact size of the slice.
1911 for (const Slice &S : P)
1912 if (S.beginOffset() == P.beginOffset() &&
1913 S.endOffset() == P.endOffset()) {
1914 if (auto *LI = dyn_cast<LoadInst>(S.getUse()->getUser()))
1915 CheckCandidateType(LI->getType());
1916 else if (auto *SI = dyn_cast<StoreInst>(S.getUse()->getUser()))
1917 CheckCandidateType(SI->getValueOperand()->getType());
1918 }
1919
1920 // If we didn't find a vector type, nothing to do here.
1921 if (CandidateTys.empty())
1922 return nullptr;
1923
1924 // Remove non-integer vector types if we had multiple common element types.
1925 // FIXME: It'd be nice to replace them with integer vector types, but we can't
1926 // do that until all the backends are known to produce good code for all
1927 // integer vector types.
1928 if (!HaveCommonEltTy) {
1929 llvm::erase_if(CandidateTys, [](VectorType *VTy) {
1930 return !VTy->getElementType()->isIntegerTy();
1931 });
1932
1933 // If there were no integer vector types, give up.
1934 if (CandidateTys.empty())
1935 return nullptr;
1936
1937 // Rank the remaining candidate vector types. This is easy because we know
1938 // they're all integer vectors. We sort by ascending number of elements.
1939 auto RankVectorTypes = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
1940 (void)DL;
1941 assert(DL.getTypeSizeInBits(RHSTy).getFixedSize() ==((DL.getTypeSizeInBits(RHSTy).getFixedSize() == DL.getTypeSizeInBits
(LHSTy).getFixedSize() && "Cannot have vector types of different sizes!"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeSizeInBits(RHSTy).getFixedSize() == DL.getTypeSizeInBits(LHSTy).getFixedSize() && \"Cannot have vector types of different sizes!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1943, __PRETTY_FUNCTION__))
1942 DL.getTypeSizeInBits(LHSTy).getFixedSize() &&((DL.getTypeSizeInBits(RHSTy).getFixedSize() == DL.getTypeSizeInBits
(LHSTy).getFixedSize() && "Cannot have vector types of different sizes!"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeSizeInBits(RHSTy).getFixedSize() == DL.getTypeSizeInBits(LHSTy).getFixedSize() && \"Cannot have vector types of different sizes!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1943, __PRETTY_FUNCTION__))
1943 "Cannot have vector types of different sizes!")((DL.getTypeSizeInBits(RHSTy).getFixedSize() == DL.getTypeSizeInBits
(LHSTy).getFixedSize() && "Cannot have vector types of different sizes!"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeSizeInBits(RHSTy).getFixedSize() == DL.getTypeSizeInBits(LHSTy).getFixedSize() && \"Cannot have vector types of different sizes!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1943, __PRETTY_FUNCTION__))
;
1944 assert(RHSTy->getElementType()->isIntegerTy() &&((RHSTy->getElementType()->isIntegerTy() && "All non-integer types eliminated!"
) ? static_cast<void> (0) : __assert_fail ("RHSTy->getElementType()->isIntegerTy() && \"All non-integer types eliminated!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1945, __PRETTY_FUNCTION__))
1945 "All non-integer types eliminated!")((RHSTy->getElementType()->isIntegerTy() && "All non-integer types eliminated!"
) ? static_cast<void> (0) : __assert_fail ("RHSTy->getElementType()->isIntegerTy() && \"All non-integer types eliminated!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1945, __PRETTY_FUNCTION__))
;
1946 assert(LHSTy->getElementType()->isIntegerTy() &&((LHSTy->getElementType()->isIntegerTy() && "All non-integer types eliminated!"
) ? static_cast<void> (0) : __assert_fail ("LHSTy->getElementType()->isIntegerTy() && \"All non-integer types eliminated!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1947, __PRETTY_FUNCTION__))
1947 "All non-integer types eliminated!")((LHSTy->getElementType()->isIntegerTy() && "All non-integer types eliminated!"
) ? static_cast<void> (0) : __assert_fail ("LHSTy->getElementType()->isIntegerTy() && \"All non-integer types eliminated!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1947, __PRETTY_FUNCTION__))
;
1948 return cast<FixedVectorType>(RHSTy)->getNumElements() <
1949 cast<FixedVectorType>(LHSTy)->getNumElements();
1950 };
1951 llvm::sort(CandidateTys, RankVectorTypes);
1952 CandidateTys.erase(
1953 std::unique(CandidateTys.begin(), CandidateTys.end(), RankVectorTypes),
1954 CandidateTys.end());
1955 } else {
1956// The only way to have the same element type in every vector type is to
1957// have the same vector type. Check that and remove all but one.
1958#ifndef NDEBUG
1959 for (VectorType *VTy : CandidateTys) {
1960 assert(VTy->getElementType() == CommonEltTy &&((VTy->getElementType() == CommonEltTy && "Unaccounted for element type!"
) ? static_cast<void> (0) : __assert_fail ("VTy->getElementType() == CommonEltTy && \"Unaccounted for element type!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1961, __PRETTY_FUNCTION__))
1961 "Unaccounted for element type!")((VTy->getElementType() == CommonEltTy && "Unaccounted for element type!"
) ? static_cast<void> (0) : __assert_fail ("VTy->getElementType() == CommonEltTy && \"Unaccounted for element type!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1961, __PRETTY_FUNCTION__))
;
1962 assert(VTy == CandidateTys[0] &&((VTy == CandidateTys[0] && "Different vector types with the same element type!"
) ? static_cast<void> (0) : __assert_fail ("VTy == CandidateTys[0] && \"Different vector types with the same element type!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1963, __PRETTY_FUNCTION__))
1963 "Different vector types with the same element type!")((VTy == CandidateTys[0] && "Different vector types with the same element type!"
) ? static_cast<void> (0) : __assert_fail ("VTy == CandidateTys[0] && \"Different vector types with the same element type!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1963, __PRETTY_FUNCTION__))
;
1964 }
1965#endif
1966 CandidateTys.resize(1);
1967 }
1968
1969 // Try each vector type, and return the one which works.
1970 auto CheckVectorTypeForPromotion = [&](VectorType *VTy) {
1971 uint64_t ElementSize =
1972 DL.getTypeSizeInBits(VTy->getElementType()).getFixedSize();
1973
1974 // While the definition of LLVM vectors is bitpacked, we don't support sizes
1975 // that aren't byte sized.
1976 if (ElementSize % 8)
1977 return false;
1978 assert((DL.getTypeSizeInBits(VTy).getFixedSize() % 8) == 0 &&(((DL.getTypeSizeInBits(VTy).getFixedSize() % 8) == 0 &&
"vector size not a multiple of element size?") ? static_cast
<void> (0) : __assert_fail ("(DL.getTypeSizeInBits(VTy).getFixedSize() % 8) == 0 && \"vector size not a multiple of element size?\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1979, __PRETTY_FUNCTION__))
1979 "vector size not a multiple of element size?")(((DL.getTypeSizeInBits(VTy).getFixedSize() % 8) == 0 &&
"vector size not a multiple of element size?") ? static_cast
<void> (0) : __assert_fail ("(DL.getTypeSizeInBits(VTy).getFixedSize() % 8) == 0 && \"vector size not a multiple of element size?\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 1979, __PRETTY_FUNCTION__))
;
1980 ElementSize /= 8;
1981
1982 for (const Slice &S : P)
1983 if (!isVectorPromotionViableForSlice(P, S, VTy, ElementSize, DL))
1984 return false;
1985
1986 for (const Slice *S : P.splitSliceTails())
1987 if (!isVectorPromotionViableForSlice(P, *S, VTy, ElementSize, DL))
1988 return false;
1989
1990 return true;
1991 };
1992 for (VectorType *VTy : CandidateTys)
1993 if (CheckVectorTypeForPromotion(VTy))
1994 return VTy;
1995
1996 return nullptr;
1997}
1998
1999/// Test whether a slice of an alloca is valid for integer widening.
2000///
2001/// This implements the necessary checking for the \c isIntegerWideningViable
2002/// test below on a single slice of the alloca.
2003static bool isIntegerWideningViableForSlice(const Slice &S,
2004 uint64_t AllocBeginOffset,
2005 Type *AllocaTy,
2006 const DataLayout &DL,
2007 bool &WholeAllocaOp) {
2008 uint64_t Size = DL.getTypeStoreSize(AllocaTy).getFixedSize();
2009
2010 uint64_t RelBegin = S.beginOffset() - AllocBeginOffset;
2011 uint64_t RelEnd = S.endOffset() - AllocBeginOffset;
2012
2013 // We can't reasonably handle cases where the load or store extends past
2014 // the end of the alloca's type and into its padding.
2015 if (RelEnd > Size)
2016 return false;
2017
2018 Use *U = S.getUse();
2019
2020 if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
2021 if (LI->isVolatile())
2022 return false;
2023 // We can't handle loads that extend past the allocated memory.
2024 if (DL.getTypeStoreSize(LI->getType()).getFixedSize() > Size)
2025 return false;
2026 // So far, AllocaSliceRewriter does not support widening split slice tails
2027 // in rewriteIntegerLoad.
2028 if (S.beginOffset() < AllocBeginOffset)
2029 return false;
2030 // Note that we don't count vector loads or stores as whole-alloca
2031 // operations which enable integer widening because we would prefer to use
2032 // vector widening instead.
2033 if (!isa<VectorType>(LI->getType()) && RelBegin == 0 && RelEnd == Size)
2034 WholeAllocaOp = true;
2035 if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) {
2036 if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy).getFixedSize())
2037 return false;
2038 } else if (RelBegin != 0 || RelEnd != Size ||
2039 !canConvertValue(DL, AllocaTy, LI->getType())) {
2040 // Non-integer loads need to be convertible from the alloca type so that
2041 // they are promotable.
2042 return false;
2043 }
2044 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
2045 Type *ValueTy = SI->getValueOperand()->getType();
2046 if (SI->isVolatile())
2047 return false;
2048 // We can't handle stores that extend past the allocated memory.
2049 if (DL.getTypeStoreSize(ValueTy).getFixedSize() > Size)
2050 return false;
2051 // So far, AllocaSliceRewriter does not support widening split slice tails
2052 // in rewriteIntegerStore.
2053 if (S.beginOffset() < AllocBeginOffset)
2054 return false;
2055 // Note that we don't count vector loads or stores as whole-alloca
2056 // operations which enable integer widening because we would prefer to use
2057 // vector widening instead.
2058 if (!isa<VectorType>(ValueTy) && RelBegin == 0 && RelEnd == Size)
2059 WholeAllocaOp = true;
2060 if (IntegerType *ITy = dyn_cast<IntegerType>(ValueTy)) {
2061 if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy).getFixedSize())
2062 return false;
2063 } else if (RelBegin != 0 || RelEnd != Size ||
2064 !canConvertValue(DL, ValueTy, AllocaTy)) {
2065 // Non-integer stores need to be convertible to the alloca type so that
2066 // they are promotable.
2067 return false;
2068 }
2069 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) {
2070 if (MI->isVolatile() || !isa<Constant>(MI->getLength()))
2071 return false;
2072 if (!S.isSplittable())
2073 return false; // Skip any unsplittable intrinsics.
2074 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) {
2075 if (!II->isLifetimeStartOrEnd() && !II->isDroppable())
2076 return false;
2077 } else {
2078 return false;
2079 }
2080
2081 return true;
2082}
2083
2084/// Test whether the given alloca partition's integer operations can be
2085/// widened to promotable ones.
2086///
2087/// This is a quick test to check whether we can rewrite the integer loads and
2088/// stores to a particular alloca into wider loads and stores and be able to
2089/// promote the resulting alloca.
2090static bool isIntegerWideningViable(Partition &P, Type *AllocaTy,
2091 const DataLayout &DL) {
2092 uint64_t SizeInBits = DL.getTypeSizeInBits(AllocaTy).getFixedSize();
2093 // Don't create integer types larger than the maximum bitwidth.
2094 if (SizeInBits > IntegerType::MAX_INT_BITS)
2095 return false;
2096
2097 // Don't try to handle allocas with bit-padding.
2098 if (SizeInBits != DL.getTypeStoreSizeInBits(AllocaTy).getFixedSize())
2099 return false;
2100
2101 // We need to ensure that an integer type with the appropriate bitwidth can
2102 // be converted to the alloca type, whatever that is. We don't want to force
2103 // the alloca itself to have an integer type if there is a more suitable one.
2104 Type *IntTy = Type::getIntNTy(AllocaTy->getContext(), SizeInBits);
2105 if (!canConvertValue(DL, AllocaTy, IntTy) ||
2106 !canConvertValue(DL, IntTy, AllocaTy))
2107 return false;
2108
2109 // While examining uses, we ensure that the alloca has a covering load or
2110 // store. We don't want to widen the integer operations only to fail to
2111 // promote due to some other unsplittable entry (which we may make splittable
2112 // later). However, if there are only splittable uses, go ahead and assume
2113 // that we cover the alloca.
2114 // FIXME: We shouldn't consider split slices that happen to start in the
2115 // partition here...
2116 bool WholeAllocaOp = P.empty() && DL.isLegalInteger(SizeInBits);
2117
2118 for (const Slice &S : P)
2119 if (!isIntegerWideningViableForSlice(S, P.beginOffset(), AllocaTy, DL,
2120 WholeAllocaOp))
2121 return false;
2122
2123 for (const Slice *S : P.splitSliceTails())
2124 if (!isIntegerWideningViableForSlice(*S, P.beginOffset(), AllocaTy, DL,
2125 WholeAllocaOp))
2126 return false;
2127
2128 return WholeAllocaOp;
2129}
2130
2131static Value *extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V,
2132 IntegerType *Ty, uint64_t Offset,
2133 const Twine &Name) {
2134 LLVM_DEBUG(dbgs() << " start: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " start: " << *V <<
"\n"; } } while (false)
;
60
Assuming 'DebugFlag' is false
61
Loop condition is false. Exiting loop
2135 IntegerType *IntTy = cast<IntegerType>(V->getType());
62
The object is a 'IntegerType'
2136 assert(DL.getTypeStoreSize(Ty).getFixedSize() + Offset <=((DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize
(IntTy).getFixedSize() && "Element extends past full value"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize(IntTy).getFixedSize() && \"Element extends past full value\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2138, __PRETTY_FUNCTION__))
63
Assuming the condition is true
64
'?' condition is true
2137 DL.getTypeStoreSize(IntTy).getFixedSize() &&((DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize
(IntTy).getFixedSize() && "Element extends past full value"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize(IntTy).getFixedSize() && \"Element extends past full value\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2138, __PRETTY_FUNCTION__))
2138 "Element extends past full value")((DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize
(IntTy).getFixedSize() && "Element extends past full value"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize(IntTy).getFixedSize() && \"Element extends past full value\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2138, __PRETTY_FUNCTION__))
;
2139 uint64_t ShAmt = 8 * Offset;
2140 if (DL.isBigEndian())
65
Assuming the condition is false
66
Taking false branch
2141 ShAmt = 8 * (DL.getTypeStoreSize(IntTy).getFixedSize() -
2142 DL.getTypeStoreSize(Ty).getFixedSize() - Offset);
2143 if (ShAmt) {
67
Assuming 'ShAmt' is 0
68
Taking false branch
2144 V = IRB.CreateLShr(V, ShAmt, Name + ".shift");
2145 LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " shifted: " << *V <<
"\n"; } } while (false)
;
2146 }
2147 assert(Ty->getBitWidth() <= IntTy->getBitWidth() &&((Ty->getBitWidth() <= IntTy->getBitWidth() &&
"Cannot extract to a larger integer!") ? static_cast<void
> (0) : __assert_fail ("Ty->getBitWidth() <= IntTy->getBitWidth() && \"Cannot extract to a larger integer!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2148, __PRETTY_FUNCTION__))
69
Called C++ object pointer is null
2148 "Cannot extract to a larger integer!")((Ty->getBitWidth() <= IntTy->getBitWidth() &&
"Cannot extract to a larger integer!") ? static_cast<void
> (0) : __assert_fail ("Ty->getBitWidth() <= IntTy->getBitWidth() && \"Cannot extract to a larger integer!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2148, __PRETTY_FUNCTION__))
;
2149 if (Ty != IntTy) {
2150 V = IRB.CreateTrunc(V, Ty, Name + ".trunc");
2151 LLVM_DEBUG(dbgs() << " trunced: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " trunced: " << *V <<
"\n"; } } while (false)
;
2152 }
2153 return V;
2154}
2155
2156static Value *insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old,
2157 Value *V, uint64_t Offset, const Twine &Name) {
2158 IntegerType *IntTy = cast<IntegerType>(Old->getType());
2159 IntegerType *Ty = cast<IntegerType>(V->getType());
2160 assert(Ty->getBitWidth() <= IntTy->getBitWidth() &&((Ty->getBitWidth() <= IntTy->getBitWidth() &&
"Cannot insert a larger integer!") ? static_cast<void>
(0) : __assert_fail ("Ty->getBitWidth() <= IntTy->getBitWidth() && \"Cannot insert a larger integer!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2161, __PRETTY_FUNCTION__))
2161 "Cannot insert a larger integer!")((Ty->getBitWidth() <= IntTy->getBitWidth() &&
"Cannot insert a larger integer!") ? static_cast<void>
(0) : __assert_fail ("Ty->getBitWidth() <= IntTy->getBitWidth() && \"Cannot insert a larger integer!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2161, __PRETTY_FUNCTION__))
;
2162 LLVM_DEBUG(dbgs() << " start: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " start: " << *V <<
"\n"; } } while (false)
;
2163 if (Ty != IntTy) {
2164 V = IRB.CreateZExt(V, IntTy, Name + ".ext");
2165 LLVM_DEBUG(dbgs() << " extended: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " extended: " << *V <<
"\n"; } } while (false)
;
2166 }
2167 assert(DL.getTypeStoreSize(Ty).getFixedSize() + Offset <=((DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize
(IntTy).getFixedSize() && "Element store outside of alloca store"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize(IntTy).getFixedSize() && \"Element store outside of alloca store\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2169, __PRETTY_FUNCTION__))
2168 DL.getTypeStoreSize(IntTy).getFixedSize() &&((DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize
(IntTy).getFixedSize() && "Element store outside of alloca store"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize(IntTy).getFixedSize() && \"Element store outside of alloca store\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2169, __PRETTY_FUNCTION__))
2169 "Element store outside of alloca store")((DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize
(IntTy).getFixedSize() && "Element store outside of alloca store"
) ? static_cast<void> (0) : __assert_fail ("DL.getTypeStoreSize(Ty).getFixedSize() + Offset <= DL.getTypeStoreSize(IntTy).getFixedSize() && \"Element store outside of alloca store\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2169, __PRETTY_FUNCTION__))
;
2170 uint64_t ShAmt = 8 * Offset;
2171 if (DL.isBigEndian())
2172 ShAmt = 8 * (DL.getTypeStoreSize(IntTy).getFixedSize() -
2173 DL.getTypeStoreSize(Ty).getFixedSize() - Offset);
2174 if (ShAmt) {
2175 V = IRB.CreateShl(V, ShAmt, Name + ".shift");
2176 LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " shifted: " << *V <<
"\n"; } } while (false)
;
2177 }
2178
2179 if (ShAmt || Ty->getBitWidth() < IntTy->getBitWidth()) {
2180 APInt Mask = ~Ty->getMask().zext(IntTy->getBitWidth()).shl(ShAmt);
2181 Old = IRB.CreateAnd(Old, Mask, Name + ".mask");
2182 LLVM_DEBUG(dbgs() << " masked: " << *Old << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " masked: " << *Old <<
"\n"; } } while (false)
;
2183 V = IRB.CreateOr(Old, V, Name + ".insert");
2184 LLVM_DEBUG(dbgs() << " inserted: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " inserted: " << *V <<
"\n"; } } while (false)
;
2185 }
2186 return V;
2187}
2188
2189static Value *extractVector(IRBuilderTy &IRB, Value *V, unsigned BeginIndex,
2190 unsigned EndIndex, const Twine &Name) {
2191 auto *VecTy = cast<FixedVectorType>(V->getType());
2192 unsigned NumElements = EndIndex - BeginIndex;
2193 assert(NumElements <= VecTy->getNumElements() && "Too many elements!")((NumElements <= VecTy->getNumElements() && "Too many elements!"
) ? static_cast<void> (0) : __assert_fail ("NumElements <= VecTy->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2193, __PRETTY_FUNCTION__))
;
2194
2195 if (NumElements == VecTy->getNumElements())
2196 return V;
2197
2198 if (NumElements == 1) {
2199 V = IRB.CreateExtractElement(V, IRB.getInt32(BeginIndex),
2200 Name + ".extract");
2201 LLVM_DEBUG(dbgs() << " extract: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " extract: " << *V <<
"\n"; } } while (false)
;
2202 return V;
2203 }
2204
2205 SmallVector<int, 8> Mask;
2206 Mask.reserve(NumElements);
2207 for (unsigned i = BeginIndex; i != EndIndex; ++i)
2208 Mask.push_back(i);
2209 V = IRB.CreateShuffleVector(V, Mask, Name + ".extract");
2210 LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " shuffle: " << *V <<
"\n"; } } while (false)
;
2211 return V;
2212}
2213
2214static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V,
2215 unsigned BeginIndex, const Twine &Name) {
2216 VectorType *VecTy = cast<VectorType>(Old->getType());
2217 assert(VecTy && "Can only insert a vector into a vector")((VecTy && "Can only insert a vector into a vector") ?
static_cast<void> (0) : __assert_fail ("VecTy && \"Can only insert a vector into a vector\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2217, __PRETTY_FUNCTION__))
;
2218
2219 VectorType *Ty = dyn_cast<VectorType>(V->getType());
2220 if (!Ty) {
2221 // Single element to insert.
2222 V = IRB.CreateInsertElement(Old, V, IRB.getInt32(BeginIndex),
2223 Name + ".insert");
2224 LLVM_DEBUG(dbgs() << " insert: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " insert: " << *V <<
"\n"; } } while (false)
;
2225 return V;
2226 }
2227
2228 assert(cast<FixedVectorType>(Ty)->getNumElements() <=((cast<FixedVectorType>(Ty)->getNumElements() <= cast
<FixedVectorType>(VecTy)->getNumElements() &&
"Too many elements!") ? static_cast<void> (0) : __assert_fail
("cast<FixedVectorType>(Ty)->getNumElements() <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2230, __PRETTY_FUNCTION__))
2229 cast<FixedVectorType>(VecTy)->getNumElements() &&((cast<FixedVectorType>(Ty)->getNumElements() <= cast
<FixedVectorType>(VecTy)->getNumElements() &&
"Too many elements!") ? static_cast<void> (0) : __assert_fail
("cast<FixedVectorType>(Ty)->getNumElements() <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2230, __PRETTY_FUNCTION__))
2230 "Too many elements!")((cast<FixedVectorType>(Ty)->getNumElements() <= cast
<FixedVectorType>(VecTy)->getNumElements() &&
"Too many elements!") ? static_cast<void> (0) : __assert_fail
("cast<FixedVectorType>(Ty)->getNumElements() <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2230, __PRETTY_FUNCTION__))
;
2231 if (cast<FixedVectorType>(Ty)->getNumElements() ==
2232 cast<FixedVectorType>(VecTy)->getNumElements()) {
2233 assert(V->getType() == VecTy && "Vector type mismatch")((V->getType() == VecTy && "Vector type mismatch")
? static_cast<void> (0) : __assert_fail ("V->getType() == VecTy && \"Vector type mismatch\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2233, __PRETTY_FUNCTION__))
;
2234 return V;
2235 }
2236 unsigned EndIndex = BeginIndex + cast<FixedVectorType>(Ty)->getNumElements();
2237
2238 // When inserting a smaller vector into the larger to store, we first
2239 // use a shuffle vector to widen it with undef elements, and then
2240 // a second shuffle vector to select between the loaded vector and the
2241 // incoming vector.
2242 SmallVector<int, 8> Mask;
2243 Mask.reserve(cast<FixedVectorType>(VecTy)->getNumElements());
2244 for (unsigned i = 0; i != cast<FixedVectorType>(VecTy)->getNumElements(); ++i)
2245 if (i >= BeginIndex && i < EndIndex)
2246 Mask.push_back(i - BeginIndex);
2247 else
2248 Mask.push_back(-1);
2249 V = IRB.CreateShuffleVector(V, Mask, Name + ".expand");
2250 LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " shuffle: " << *V <<
"\n"; } } while (false)
;
2251
2252 SmallVector<Constant *, 8> Mask2;
2253 Mask2.reserve(cast<FixedVectorType>(VecTy)->getNumElements());
2254 for (unsigned i = 0; i != cast<FixedVectorType>(VecTy)->getNumElements(); ++i)
2255 Mask2.push_back(IRB.getInt1(i >= BeginIndex && i < EndIndex));
2256
2257 V = IRB.CreateSelect(ConstantVector::get(Mask2), V, Old, Name + "blend");
2258
2259 LLVM_DEBUG(dbgs() << " blend: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " blend: " << *V <<
"\n"; } } while (false)
;
2260 return V;
2261}
2262
2263/// Visitor to rewrite instructions using p particular slice of an alloca
2264/// to use a new alloca.
2265///
2266/// Also implements the rewriting to vector-based accesses when the partition
2267/// passes the isVectorPromotionViable predicate. Most of the rewriting logic
2268/// lives here.
2269class llvm::sroa::AllocaSliceRewriter
2270 : public InstVisitor<AllocaSliceRewriter, bool> {
2271 // Befriend the base class so it can delegate to private visit methods.
2272 friend class InstVisitor<AllocaSliceRewriter, bool>;
2273
2274 using Base = InstVisitor<AllocaSliceRewriter, bool>;
2275
2276 const DataLayout &DL;
2277 AllocaSlices &AS;
2278 SROA &Pass;
2279 AllocaInst &OldAI, &NewAI;
2280 const uint64_t NewAllocaBeginOffset, NewAllocaEndOffset;
2281 Type *NewAllocaTy;
2282
2283 // This is a convenience and flag variable that will be null unless the new
2284 // alloca's integer operations should be widened to this integer type due to
2285 // passing isIntegerWideningViable above. If it is non-null, the desired
2286 // integer type will be stored here for easy access during rewriting.
2287 IntegerType *IntTy;
2288
2289 // If we are rewriting an alloca partition which can be written as pure
2290 // vector operations, we stash extra information here. When VecTy is
2291 // non-null, we have some strict guarantees about the rewritten alloca:
2292 // - The new alloca is exactly the size of the vector type here.
2293 // - The accesses all either map to the entire vector or to a single
2294 // element.
2295 // - The set of accessing instructions is only one of those handled above
2296 // in isVectorPromotionViable. Generally these are the same access kinds
2297 // which are promotable via mem2reg.
2298 VectorType *VecTy;
2299 Type *ElementTy;
2300 uint64_t ElementSize;
2301
2302 // The original offset of the slice currently being rewritten relative to
2303 // the original alloca.
2304 uint64_t BeginOffset = 0;
2305 uint64_t EndOffset = 0;
2306
2307 // The new offsets of the slice currently being rewritten relative to the
2308 // original alloca.
2309 uint64_t NewBeginOffset = 0, NewEndOffset = 0;
2310
2311 uint64_t SliceSize = 0;
2312 bool IsSplittable = false;
2313 bool IsSplit = false;
2314 Use *OldUse = nullptr;
2315 Instruction *OldPtr = nullptr;
2316
2317 // Track post-rewrite users which are PHI nodes and Selects.
2318 SmallSetVector<PHINode *, 8> &PHIUsers;
2319 SmallSetVector<SelectInst *, 8> &SelectUsers;
2320
2321 // Utility IR builder, whose name prefix is setup for each visited use, and
2322 // the insertion point is set to point to the user.
2323 IRBuilderTy IRB;
2324
2325public:
2326 AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &AS, SROA &Pass,
2327 AllocaInst &OldAI, AllocaInst &NewAI,
2328 uint64_t NewAllocaBeginOffset,
2329 uint64_t NewAllocaEndOffset, bool IsIntegerPromotable,
2330 VectorType *PromotableVecTy,
2331 SmallSetVector<PHINode *, 8> &PHIUsers,
2332 SmallSetVector<SelectInst *, 8> &SelectUsers)
2333 : DL(DL), AS(AS), Pass(Pass), OldAI(OldAI), NewAI(NewAI),
2334 NewAllocaBeginOffset(NewAllocaBeginOffset),
2335 NewAllocaEndOffset(NewAllocaEndOffset),
2336 NewAllocaTy(NewAI.getAllocatedType()),
2337 IntTy(
2338 IsIntegerPromotable
2339 ? Type::getIntNTy(NewAI.getContext(),
2340 DL.getTypeSizeInBits(NewAI.getAllocatedType())
2341 .getFixedSize())
2342 : nullptr),
2343 VecTy(PromotableVecTy),
2344 ElementTy(VecTy ? VecTy->getElementType() : nullptr),
2345 ElementSize(VecTy ? DL.getTypeSizeInBits(ElementTy).getFixedSize() / 8
2346 : 0),
2347 PHIUsers(PHIUsers), SelectUsers(SelectUsers),
2348 IRB(NewAI.getContext(), ConstantFolder()) {
2349 if (VecTy) {
2350 assert((DL.getTypeSizeInBits(ElementTy).getFixedSize() % 8) == 0 &&(((DL.getTypeSizeInBits(ElementTy).getFixedSize() % 8) == 0 &&
"Only multiple-of-8 sized vector elements are viable") ? static_cast
<void> (0) : __assert_fail ("(DL.getTypeSizeInBits(ElementTy).getFixedSize() % 8) == 0 && \"Only multiple-of-8 sized vector elements are viable\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2351, __PRETTY_FUNCTION__))
2351 "Only multiple-of-8 sized vector elements are viable")(((DL.getTypeSizeInBits(ElementTy).getFixedSize() % 8) == 0 &&
"Only multiple-of-8 sized vector elements are viable") ? static_cast
<void> (0) : __assert_fail ("(DL.getTypeSizeInBits(ElementTy).getFixedSize() % 8) == 0 && \"Only multiple-of-8 sized vector elements are viable\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2351, __PRETTY_FUNCTION__))
;
2352 ++NumVectorized;
2353 }
2354 assert((!IntTy && !VecTy) || (IntTy && !VecTy) || (!IntTy && VecTy))(((!IntTy && !VecTy) || (IntTy && !VecTy) || (
!IntTy && VecTy)) ? static_cast<void> (0) : __assert_fail
("(!IntTy && !VecTy) || (IntTy && !VecTy) || (!IntTy && VecTy)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2354, __PRETTY_FUNCTION__))
;
2355 }
2356
2357 bool visit(AllocaSlices::const_iterator I) {
2358 bool CanSROA = true;
2359 BeginOffset = I->beginOffset();
2360 EndOffset = I->endOffset();
2361 IsSplittable = I->isSplittable();
2362 IsSplit =
2363 BeginOffset < NewAllocaBeginOffset || EndOffset > NewAllocaEndOffset;
2364 LLVM_DEBUG(dbgs() << " rewriting " << (IsSplit ? "split " : ""))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " rewriting " << (IsSplit ?
"split " : ""); } } while (false)
;
2365 LLVM_DEBUG(AS.printSlice(dbgs(), I, ""))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { AS.printSlice(dbgs(), I, ""); } } while (false)
;
2366 LLVM_DEBUG(dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "\n"; } } while (false)
;
2367
2368 // Compute the intersecting offset range.
2369 assert(BeginOffset < NewAllocaEndOffset)((BeginOffset < NewAllocaEndOffset) ? static_cast<void>
(0) : __assert_fail ("BeginOffset < NewAllocaEndOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2369, __PRETTY_FUNCTION__))
;
2370 assert(EndOffset > NewAllocaBeginOffset)((EndOffset > NewAllocaBeginOffset) ? static_cast<void>
(0) : __assert_fail ("EndOffset > NewAllocaBeginOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2370, __PRETTY_FUNCTION__))
;
2371 NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset);
2372 NewEndOffset = std::min(EndOffset, NewAllocaEndOffset);
2373
2374 SliceSize = NewEndOffset - NewBeginOffset;
2375
2376 OldUse = I->getUse();
2377 OldPtr = cast<Instruction>(OldUse->get());
2378
2379 Instruction *OldUserI = cast<Instruction>(OldUse->getUser());
2380 IRB.SetInsertPoint(OldUserI);
2381 IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc());
2382 IRB.getInserter().SetNamePrefix(
2383 Twine(NewAI.getName()) + "." + Twine(BeginOffset) + ".");
2384
2385 CanSROA &= visit(cast<Instruction>(OldUse->getUser()));
2386 if (VecTy || IntTy)
2387 assert(CanSROA)((CanSROA) ? static_cast<void> (0) : __assert_fail ("CanSROA"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2387, __PRETTY_FUNCTION__))
;
2388 return CanSROA;
2389 }
2390
2391private:
2392 // Make sure the other visit overloads are visible.
2393 using Base::visit;
2394
2395 // Every instruction which can end up as a user must have a rewrite rule.
2396 bool visitInstruction(Instruction &I) {
2397 LLVM_DEBUG(dbgs() << " !!!! Cannot rewrite: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " !!!! Cannot rewrite: " <<
I << "\n"; } } while (false)
;
2398 llvm_unreachable("No rewrite rule for this instruction!")::llvm::llvm_unreachable_internal("No rewrite rule for this instruction!"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2398)
;
2399 }
2400
2401 Value *getNewAllocaSlicePtr(IRBuilderTy &IRB, Type *PointerTy) {
2402 // Note that the offset computation can use BeginOffset or NewBeginOffset
2403 // interchangeably for unsplit slices.
2404 assert(IsSplit || BeginOffset == NewBeginOffset)((IsSplit || BeginOffset == NewBeginOffset) ? static_cast<
void> (0) : __assert_fail ("IsSplit || BeginOffset == NewBeginOffset"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2404, __PRETTY_FUNCTION__))
;
2405 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
2406
2407#ifndef NDEBUG
2408 StringRef OldName = OldPtr->getName();
2409 // Skip through the last '.sroa.' component of the name.
2410 size_t LastSROAPrefix = OldName.rfind(".sroa.");
2411 if (LastSROAPrefix != StringRef::npos) {
2412 OldName = OldName.substr(LastSROAPrefix + strlen(".sroa."));
2413 // Look for an SROA slice index.
2414 size_t IndexEnd = OldName.find_first_not_of("0123456789");
2415 if (IndexEnd != StringRef::npos && OldName[IndexEnd] == '.') {
2416 // Strip the index and look for the offset.
2417 OldName = OldName.substr(IndexEnd + 1);
2418 size_t OffsetEnd = OldName.find_first_not_of("0123456789");
2419 if (OffsetEnd != StringRef::npos && OldName[OffsetEnd] == '.')
2420 // Strip the offset.
2421 OldName = OldName.substr(OffsetEnd + 1);
2422 }
2423 }
2424 // Strip any SROA suffixes as well.
2425 OldName = OldName.substr(0, OldName.find(".sroa_"));
2426#endif
2427
2428 return getAdjustedPtr(IRB, DL, &NewAI,
2429 APInt(DL.getIndexTypeSizeInBits(PointerTy), Offset),
2430 PointerTy,
2431#ifndef NDEBUG
2432 Twine(OldName) + "."
2433#else
2434 Twine()
2435#endif
2436 );
2437 }
2438
2439 /// Compute suitable alignment to access this slice of the *new*
2440 /// alloca.
2441 ///
2442 /// You can optionally pass a type to this routine and if that type's ABI
2443 /// alignment is itself suitable, this will return zero.
2444 Align getSliceAlign() {
2445 return commonAlignment(NewAI.getAlign(),
2446 NewBeginOffset - NewAllocaBeginOffset);
2447 }
2448
2449 unsigned getIndex(uint64_t Offset) {
2450 assert(VecTy && "Can only call getIndex when rewriting a vector")((VecTy && "Can only call getIndex when rewriting a vector"
) ? static_cast<void> (0) : __assert_fail ("VecTy && \"Can only call getIndex when rewriting a vector\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2450, __PRETTY_FUNCTION__))
;
2451 uint64_t RelOffset = Offset - NewAllocaBeginOffset;
2452 assert(RelOffset / ElementSize < UINT32_MAX && "Index out of bounds")((RelOffset / ElementSize < (4294967295U) && "Index out of bounds"
) ? static_cast<void> (0) : __assert_fail ("RelOffset / ElementSize < UINT32_MAX && \"Index out of bounds\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2452, __PRETTY_FUNCTION__))
;
2453 uint32_t Index = RelOffset / ElementSize;
2454 assert(Index * ElementSize == RelOffset)((Index * ElementSize == RelOffset) ? static_cast<void>
(0) : __assert_fail ("Index * ElementSize == RelOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2454, __PRETTY_FUNCTION__))
;
2455 return Index;
2456 }
2457
2458 void deleteIfTriviallyDead(Value *V) {
2459 Instruction *I = cast<Instruction>(V);
2460 if (isInstructionTriviallyDead(I))
2461 Pass.DeadInsts.push_back(I);
2462 }
2463
2464 Value *rewriteVectorizedLoadInst() {
2465 unsigned BeginIndex = getIndex(NewBeginOffset);
2466 unsigned EndIndex = getIndex(NewEndOffset);
2467 assert(EndIndex > BeginIndex && "Empty vector!")((EndIndex > BeginIndex && "Empty vector!") ? static_cast
<void> (0) : __assert_fail ("EndIndex > BeginIndex && \"Empty vector!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2467, __PRETTY_FUNCTION__))
;
2468
2469 Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2470 NewAI.getAlign(), "load");
2471 return extractVector(IRB, V, BeginIndex, EndIndex, "vec");
2472 }
2473
2474 Value *rewriteIntegerLoad(LoadInst &LI) {
2475 assert(IntTy && "We cannot insert an integer to the alloca")((IntTy && "We cannot insert an integer to the alloca"
) ? static_cast<void> (0) : __assert_fail ("IntTy && \"We cannot insert an integer to the alloca\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2475, __PRETTY_FUNCTION__))
;
2476 assert(!LI.isVolatile())((!LI.isVolatile()) ? static_cast<void> (0) : __assert_fail
("!LI.isVolatile()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2476, __PRETTY_FUNCTION__))
;
2477 Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2478 NewAI.getAlign(), "load");
2479 V = convertValue(DL, IRB, V, IntTy);
2480 assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset")((NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"
) ? static_cast<void> (0) : __assert_fail ("NewBeginOffset >= NewAllocaBeginOffset && \"Out of bounds offset\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2480, __PRETTY_FUNCTION__))
;
2481 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
2482 if (Offset > 0 || NewEndOffset < NewAllocaEndOffset) {
2483 IntegerType *ExtractTy = Type::getIntNTy(LI.getContext(), SliceSize * 8);
2484 V = extractInteger(DL, IRB, V, ExtractTy, Offset, "extract");
2485 }
2486 // It is possible that the extracted type is not the load type. This
2487 // happens if there is a load past the end of the alloca, and as
2488 // a consequence the slice is narrower but still a candidate for integer
2489 // lowering. To handle this case, we just zero extend the extracted
2490 // integer.
2491 assert(cast<IntegerType>(LI.getType())->getBitWidth() >= SliceSize * 8 &&((cast<IntegerType>(LI.getType())->getBitWidth() >=
SliceSize * 8 && "Can only handle an extract for an overly wide load"
) ? static_cast<void> (0) : __assert_fail ("cast<IntegerType>(LI.getType())->getBitWidth() >= SliceSize * 8 && \"Can only handle an extract for an overly wide load\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2492, __PRETTY_FUNCTION__))
2492 "Can only handle an extract for an overly wide load")((cast<IntegerType>(LI.getType())->getBitWidth() >=
SliceSize * 8 && "Can only handle an extract for an overly wide load"
) ? static_cast<void> (0) : __assert_fail ("cast<IntegerType>(LI.getType())->getBitWidth() >= SliceSize * 8 && \"Can only handle an extract for an overly wide load\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2492, __PRETTY_FUNCTION__))
;
2493 if (cast<IntegerType>(LI.getType())->getBitWidth() > SliceSize * 8)
2494 V = IRB.CreateZExt(V, LI.getType());
2495 return V;
2496 }
2497
2498 bool visitLoadInst(LoadInst &LI) {
2499 LLVM_DEBUG(dbgs() << " original: " << LI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << LI <<
"\n"; } } while (false)
;
2500 Value *OldOp = LI.getOperand(0);
2501 assert(OldOp == OldPtr)((OldOp == OldPtr) ? static_cast<void> (0) : __assert_fail
("OldOp == OldPtr", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2501, __PRETTY_FUNCTION__))
;
2502
2503 AAMDNodes AATags;
2504 LI.getAAMetadata(AATags);
2505
2506 unsigned AS = LI.getPointerAddressSpace();
2507
2508 Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), SliceSize * 8)
2509 : LI.getType();
2510 const bool IsLoadPastEnd =
2511 DL.getTypeStoreSize(TargetTy).getFixedSize() > SliceSize;
2512 bool IsPtrAdjusted = false;
2513 Value *V;
2514 if (VecTy) {
2515 V = rewriteVectorizedLoadInst();
2516 } else if (IntTy && LI.getType()->isIntegerTy()) {
2517 V = rewriteIntegerLoad(LI);
2518 } else if (NewBeginOffset == NewAllocaBeginOffset &&
2519 NewEndOffset == NewAllocaEndOffset &&
2520 (canConvertValue(DL, NewAllocaTy, TargetTy) ||
2521 (IsLoadPastEnd && NewAllocaTy->isIntegerTy() &&
2522 TargetTy->isIntegerTy()))) {
2523 LoadInst *NewLI = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2524 NewAI.getAlign(), LI.isVolatile(),
2525 LI.getName());
2526 if (AATags)
2527 NewLI->setAAMetadata(AATags);
2528 if (LI.isVolatile())
2529 NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
2530 if (NewLI->isAtomic())
2531 NewLI->setAlignment(LI.getAlign());
2532
2533 // Any !nonnull metadata or !range metadata on the old load is also valid
2534 // on the new load. This is even true in some cases even when the loads
2535 // are different types, for example by mapping !nonnull metadata to
2536 // !range metadata by modeling the null pointer constant converted to the
2537 // integer type.
2538 // FIXME: Add support for range metadata here. Currently the utilities
2539 // for this don't propagate range metadata in trivial cases from one
2540 // integer load to another, don't handle non-addrspace-0 null pointers
2541 // correctly, and don't have any support for mapping ranges as the
2542 // integer type becomes winder or narrower.
2543 if (MDNode *N = LI.getMetadata(LLVMContext::MD_nonnull))
2544 copyNonnullMetadata(LI, N, *NewLI);
2545
2546 // Try to preserve nonnull metadata
2547 V = NewLI;
2548
2549 // If this is an integer load past the end of the slice (which means the
2550 // bytes outside the slice are undef or this load is dead) just forcibly
2551 // fix the integer size with correct handling of endianness.
2552 if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy))
2553 if (auto *TITy = dyn_cast<IntegerType>(TargetTy))
2554 if (AITy->getBitWidth() < TITy->getBitWidth()) {
2555 V = IRB.CreateZExt(V, TITy, "load.ext");
2556 if (DL.isBigEndian())
2557 V = IRB.CreateShl(V, TITy->getBitWidth() - AITy->getBitWidth(),
2558 "endian_shift");
2559 }
2560 } else {
2561 Type *LTy = TargetTy->getPointerTo(AS);
2562 LoadInst *NewLI =
2563 IRB.CreateAlignedLoad(TargetTy, getNewAllocaSlicePtr(IRB, LTy),
2564 getSliceAlign(), LI.isVolatile(), LI.getName());
2565 if (AATags)
2566 NewLI->setAAMetadata(AATags);
2567 if (LI.isVolatile())
2568 NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
2569
2570 V = NewLI;
2571 IsPtrAdjusted = true;
2572 }
2573 V = convertValue(DL, IRB, V, TargetTy);
2574
2575 if (IsSplit) {
2576 assert(!LI.isVolatile())((!LI.isVolatile()) ? static_cast<void> (0) : __assert_fail
("!LI.isVolatile()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2576, __PRETTY_FUNCTION__))
;
2577 assert(LI.getType()->isIntegerTy() &&((LI.getType()->isIntegerTy() && "Only integer type loads and stores are split"
) ? static_cast<void> (0) : __assert_fail ("LI.getType()->isIntegerTy() && \"Only integer type loads and stores are split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2578, __PRETTY_FUNCTION__))
2578 "Only integer type loads and stores are split")((LI.getType()->isIntegerTy() && "Only integer type loads and stores are split"
) ? static_cast<void> (0) : __assert_fail ("LI.getType()->isIntegerTy() && \"Only integer type loads and stores are split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2578, __PRETTY_FUNCTION__))
;
2579 assert(SliceSize < DL.getTypeStoreSize(LI.getType()).getFixedSize() &&((SliceSize < DL.getTypeStoreSize(LI.getType()).getFixedSize
() && "Split load isn't smaller than original load") ?
static_cast<void> (0) : __assert_fail ("SliceSize < DL.getTypeStoreSize(LI.getType()).getFixedSize() && \"Split load isn't smaller than original load\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2580, __PRETTY_FUNCTION__))
2580 "Split load isn't smaller than original load")((SliceSize < DL.getTypeStoreSize(LI.getType()).getFixedSize
() && "Split load isn't smaller than original load") ?
static_cast<void> (0) : __assert_fail ("SliceSize < DL.getTypeStoreSize(LI.getType()).getFixedSize() && \"Split load isn't smaller than original load\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2580, __PRETTY_FUNCTION__))
;
2581 assert(DL.typeSizeEqualsStoreSize(LI.getType()) &&((DL.typeSizeEqualsStoreSize(LI.getType()) && "Non-byte-multiple bit width"
) ? static_cast<void> (0) : __assert_fail ("DL.typeSizeEqualsStoreSize(LI.getType()) && \"Non-byte-multiple bit width\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2582, __PRETTY_FUNCTION__))
2582 "Non-byte-multiple bit width")((DL.typeSizeEqualsStoreSize(LI.getType()) && "Non-byte-multiple bit width"
) ? static_cast<void> (0) : __assert_fail ("DL.typeSizeEqualsStoreSize(LI.getType()) && \"Non-byte-multiple bit width\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2582, __PRETTY_FUNCTION__))
;
2583 // Move the insertion point just past the load so that we can refer to it.
2584 IRB.SetInsertPoint(&*std::next(BasicBlock::iterator(&LI)));
2585 // Create a placeholder value with the same type as LI to use as the
2586 // basis for the new value. This allows us to replace the uses of LI with
2587 // the computed value, and then replace the placeholder with LI, leaving
2588 // LI only used for this computation.
2589 Value *Placeholder = new LoadInst(
2590 LI.getType(), UndefValue::get(LI.getType()->getPointerTo(AS)), "",
2591 false, Align(1));
2592 V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset - BeginOffset,
2593 "insert");
2594 LI.replaceAllUsesWith(V);
2595 Placeholder->replaceAllUsesWith(&LI);
2596 Placeholder->deleteValue();
2597 } else {
2598 LI.replaceAllUsesWith(V);
2599 }
2600
2601 Pass.DeadInsts.push_back(&LI);
2602 deleteIfTriviallyDead(OldOp);
2603 LLVM_DEBUG(dbgs() << " to: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *V <<
"\n"; } } while (false)
;
2604 return !LI.isVolatile() && !IsPtrAdjusted;
2605 }
2606
2607 bool rewriteVectorizedStoreInst(Value *V, StoreInst &SI, Value *OldOp,
2608 AAMDNodes AATags) {
2609 if (V->getType() != VecTy) {
2610 unsigned BeginIndex = getIndex(NewBeginOffset);
2611 unsigned EndIndex = getIndex(NewEndOffset);
2612 assert(EndIndex > BeginIndex && "Empty vector!")((EndIndex > BeginIndex && "Empty vector!") ? static_cast
<void> (0) : __assert_fail ("EndIndex > BeginIndex && \"Empty vector!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2612, __PRETTY_FUNCTION__))
;
2613 unsigned NumElements = EndIndex - BeginIndex;
2614 assert(NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() &&((NumElements <= cast<FixedVectorType>(VecTy)->getNumElements
() && "Too many elements!") ? static_cast<void>
(0) : __assert_fail ("NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2615, __PRETTY_FUNCTION__))
2615 "Too many elements!")((NumElements <= cast<FixedVectorType>(VecTy)->getNumElements
() && "Too many elements!") ? static_cast<void>
(0) : __assert_fail ("NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2615, __PRETTY_FUNCTION__))
;
2616 Type *SliceTy = (NumElements == 1)
2617 ? ElementTy
2618 : FixedVectorType::get(ElementTy, NumElements);
2619 if (V->getType() != SliceTy)
2620 V = convertValue(DL, IRB, V, SliceTy);
2621
2622 // Mix in the existing elements.
2623 Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2624 NewAI.getAlign(), "load");
2625 V = insertVector(IRB, Old, V, BeginIndex, "vec");
2626 }
2627 StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlign());
2628 if (AATags)
2629 Store->setAAMetadata(AATags);
2630 Pass.DeadInsts.push_back(&SI);
2631
2632 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *Store <<
"\n"; } } while (false)
;
2633 return true;
2634 }
2635
2636 bool rewriteIntegerStore(Value *V, StoreInst &SI, AAMDNodes AATags) {
2637 assert(IntTy && "We cannot extract an integer from the alloca")((IntTy && "We cannot extract an integer from the alloca"
) ? static_cast<void> (0) : __assert_fail ("IntTy && \"We cannot extract an integer from the alloca\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2637, __PRETTY_FUNCTION__))
;
2638 assert(!SI.isVolatile())((!SI.isVolatile()) ? static_cast<void> (0) : __assert_fail
("!SI.isVolatile()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2638, __PRETTY_FUNCTION__))
;
2639 if (DL.getTypeSizeInBits(V->getType()).getFixedSize() !=
2640 IntTy->getBitWidth()) {
2641 Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2642 NewAI.getAlign(), "oldload");
2643 Old = convertValue(DL, IRB, Old, IntTy);
2644 assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset")((BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"
) ? static_cast<void> (0) : __assert_fail ("BeginOffset >= NewAllocaBeginOffset && \"Out of bounds offset\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2644, __PRETTY_FUNCTION__))
;
2645 uint64_t Offset = BeginOffset - NewAllocaBeginOffset;
2646 V = insertInteger(DL, IRB, Old, SI.getValueOperand(), Offset, "insert");
2647 }
2648 V = convertValue(DL, IRB, V, NewAllocaTy);
2649 StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlign());
2650 Store->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access,
2651 LLVMContext::MD_access_group});
2652 if (AATags)
2653 Store->setAAMetadata(AATags);
2654 Pass.DeadInsts.push_back(&SI);
2655 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *Store <<
"\n"; } } while (false)
;
2656 return true;
2657 }
2658
2659 bool visitStoreInst(StoreInst &SI) {
2660 LLVM_DEBUG(dbgs() << " original: " << SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << SI <<
"\n"; } } while (false)
;
2661 Value *OldOp = SI.getOperand(1);
2662 assert(OldOp == OldPtr)((OldOp == OldPtr) ? static_cast<void> (0) : __assert_fail
("OldOp == OldPtr", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2662, __PRETTY_FUNCTION__))
;
2663
2664 AAMDNodes AATags;
2665 SI.getAAMetadata(AATags);
2666
2667 Value *V = SI.getValueOperand();
2668
2669 // Strip all inbounds GEPs and pointer casts to try to dig out any root
2670 // alloca that should be re-examined after promoting this alloca.
2671 if (V->getType()->isPointerTy())
2672 if (AllocaInst *AI = dyn_cast<AllocaInst>(V->stripInBoundsOffsets()))
2673 Pass.PostPromotionWorklist.insert(AI);
2674
2675 if (SliceSize < DL.getTypeStoreSize(V->getType()).getFixedSize()) {
2676 assert(!SI.isVolatile())((!SI.isVolatile()) ? static_cast<void> (0) : __assert_fail
("!SI.isVolatile()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2676, __PRETTY_FUNCTION__))
;
2677 assert(V->getType()->isIntegerTy() &&((V->getType()->isIntegerTy() && "Only integer type loads and stores are split"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntegerTy() && \"Only integer type loads and stores are split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2678, __PRETTY_FUNCTION__))
2678 "Only integer type loads and stores are split")((V->getType()->isIntegerTy() && "Only integer type loads and stores are split"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntegerTy() && \"Only integer type loads and stores are split\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2678, __PRETTY_FUNCTION__))
;
2679 assert(DL.typeSizeEqualsStoreSize(V->getType()) &&((DL.typeSizeEqualsStoreSize(V->getType()) && "Non-byte-multiple bit width"
) ? static_cast<void> (0) : __assert_fail ("DL.typeSizeEqualsStoreSize(V->getType()) && \"Non-byte-multiple bit width\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2680, __PRETTY_FUNCTION__))
2680 "Non-byte-multiple bit width")((DL.typeSizeEqualsStoreSize(V->getType()) && "Non-byte-multiple bit width"
) ? static_cast<void> (0) : __assert_fail ("DL.typeSizeEqualsStoreSize(V->getType()) && \"Non-byte-multiple bit width\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2680, __PRETTY_FUNCTION__))
;
2681 IntegerType *NarrowTy = Type::getIntNTy(SI.getContext(), SliceSize * 8);
2682 V = extractInteger(DL, IRB, V, NarrowTy, NewBeginOffset - BeginOffset,
2683 "extract");
2684 }
2685
2686 if (VecTy)
2687 return rewriteVectorizedStoreInst(V, SI, OldOp, AATags);
2688 if (IntTy && V->getType()->isIntegerTy())
2689 return rewriteIntegerStore(V, SI, AATags);
2690
2691 const bool IsStorePastEnd =
2692 DL.getTypeStoreSize(V->getType()).getFixedSize() > SliceSize;
2693 StoreInst *NewSI;
2694 if (NewBeginOffset == NewAllocaBeginOffset &&
2695 NewEndOffset == NewAllocaEndOffset &&
2696 (canConvertValue(DL, V->getType(), NewAllocaTy) ||
2697 (IsStorePastEnd && NewAllocaTy->isIntegerTy() &&
2698 V->getType()->isIntegerTy()))) {
2699 // If this is an integer store past the end of slice (and thus the bytes
2700 // past that point are irrelevant or this is unreachable), truncate the
2701 // value prior to storing.
2702 if (auto *VITy = dyn_cast<IntegerType>(V->getType()))
2703 if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy))
2704 if (VITy->getBitWidth() > AITy->getBitWidth()) {
2705 if (DL.isBigEndian())
2706 V = IRB.CreateLShr(V, VITy->getBitWidth() - AITy->getBitWidth(),
2707 "endian_shift");
2708 V = IRB.CreateTrunc(V, AITy, "load.trunc");
2709 }
2710
2711 V = convertValue(DL, IRB, V, NewAllocaTy);
2712 NewSI =
2713 IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlign(), SI.isVolatile());
2714 } else {
2715 unsigned AS = SI.getPointerAddressSpace();
2716 Value *NewPtr = getNewAllocaSlicePtr(IRB, V->getType()->getPointerTo(AS));
2717 NewSI =
2718 IRB.CreateAlignedStore(V, NewPtr, getSliceAlign(), SI.isVolatile());
2719 }
2720 NewSI->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access,
2721 LLVMContext::MD_access_group});
2722 if (AATags)
2723 NewSI->setAAMetadata(AATags);
2724 if (SI.isVolatile())
2725 NewSI->setAtomic(SI.getOrdering(), SI.getSyncScopeID());
2726 if (NewSI->isAtomic())
2727 NewSI->setAlignment(SI.getAlign());
2728 Pass.DeadInsts.push_back(&SI);
2729 deleteIfTriviallyDead(OldOp);
2730
2731 LLVM_DEBUG(dbgs() << " to: " << *NewSI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *NewSI <<
"\n"; } } while (false)
;
2732 return NewSI->getPointerOperand() == &NewAI && !SI.isVolatile();
2733 }
2734
2735 /// Compute an integer value from splatting an i8 across the given
2736 /// number of bytes.
2737 ///
2738 /// Note that this routine assumes an i8 is a byte. If that isn't true, don't
2739 /// call this routine.
2740 /// FIXME: Heed the advice above.
2741 ///
2742 /// \param V The i8 value to splat.
2743 /// \param Size The number of bytes in the output (assuming i8 is one byte)
2744 Value *getIntegerSplat(Value *V, unsigned Size) {
2745 assert(Size > 0 && "Expected a positive number of bytes.")((Size > 0 && "Expected a positive number of bytes."
) ? static_cast<void> (0) : __assert_fail ("Size > 0 && \"Expected a positive number of bytes.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2745, __PRETTY_FUNCTION__))
;
2746 IntegerType *VTy = cast<IntegerType>(V->getType());
2747 assert(VTy->getBitWidth() == 8 && "Expected an i8 value for the byte")((VTy->getBitWidth() == 8 && "Expected an i8 value for the byte"
) ? static_cast<void> (0) : __assert_fail ("VTy->getBitWidth() == 8 && \"Expected an i8 value for the byte\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2747, __PRETTY_FUNCTION__))
;
2748 if (Size == 1)
2749 return V;
2750
2751 Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8);
2752 V = IRB.CreateMul(
2753 IRB.CreateZExt(V, SplatIntTy, "zext"),
2754 ConstantExpr::getUDiv(
2755 Constant::getAllOnesValue(SplatIntTy),
2756 ConstantExpr::getZExt(Constant::getAllOnesValue(V->getType()),
2757 SplatIntTy)),
2758 "isplat");
2759 return V;
2760 }
2761
2762 /// Compute a vector splat for a given element value.
2763 Value *getVectorSplat(Value *V, unsigned NumElements) {
2764 V = IRB.CreateVectorSplat(NumElements, V, "vsplat");
2765 LLVM_DEBUG(dbgs() << " splat: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " splat: " << *V <<
"\n"; } } while (false)
;
2766 return V;
2767 }
2768
2769 bool visitMemSetInst(MemSetInst &II) {
2770 LLVM_DEBUG(dbgs() << " original: " << II << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << II <<
"\n"; } } while (false)
;
2771 assert(II.getRawDest() == OldPtr)((II.getRawDest() == OldPtr) ? static_cast<void> (0) : __assert_fail
("II.getRawDest() == OldPtr", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2771, __PRETTY_FUNCTION__))
;
2772
2773 AAMDNodes AATags;
2774 II.getAAMetadata(AATags);
2775
2776 // If the memset has a variable size, it cannot be split, just adjust the
2777 // pointer to the new alloca.
2778 if (!isa<Constant>(II.getLength())) {
2779 assert(!IsSplit)((!IsSplit) ? static_cast<void> (0) : __assert_fail ("!IsSplit"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2779, __PRETTY_FUNCTION__))
;
2780 assert(NewBeginOffset == BeginOffset)((NewBeginOffset == BeginOffset) ? static_cast<void> (0
) : __assert_fail ("NewBeginOffset == BeginOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2780, __PRETTY_FUNCTION__))
;
2781 II.setDest(getNewAllocaSlicePtr(IRB, OldPtr->getType()));
2782 II.setDestAlignment(getSliceAlign());
2783
2784 deleteIfTriviallyDead(OldPtr);
2785 return false;
2786 }
2787
2788 // Record this instruction for deletion.
2789 Pass.DeadInsts.push_back(&II);
2790
2791 Type *AllocaTy = NewAI.getAllocatedType();
2792 Type *ScalarTy = AllocaTy->getScalarType();
2793
2794 const bool CanContinue = [&]() {
2795 if (VecTy || IntTy)
2796 return true;
2797 if (BeginOffset > NewAllocaBeginOffset ||
2798 EndOffset < NewAllocaEndOffset)
2799 return false;
2800 auto *C = cast<ConstantInt>(II.getLength());
2801 if (C->getBitWidth() > 64)
2802 return false;
2803 const auto Len = C->getZExtValue();
2804 auto *Int8Ty = IntegerType::getInt8Ty(NewAI.getContext());
2805 auto *SrcTy = FixedVectorType::get(Int8Ty, Len);
2806 return canConvertValue(DL, SrcTy, AllocaTy) &&
2807 DL.isLegalInteger(DL.getTypeSizeInBits(ScalarTy).getFixedSize());
2808 }();
2809
2810 // If this doesn't map cleanly onto the alloca type, and that type isn't
2811 // a single value type, just emit a memset.
2812 if (!CanContinue) {
2813 Type *SizeTy = II.getLength()->getType();
2814 Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset);
2815 CallInst *New = IRB.CreateMemSet(
2816 getNewAllocaSlicePtr(IRB, OldPtr->getType()), II.getValue(), Size,
2817 MaybeAlign(getSliceAlign()), II.isVolatile());
2818 if (AATags)
2819 New->setAAMetadata(AATags);
2820 LLVM_DEBUG(dbgs() << " to: " << *New << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *New <<
"\n"; } } while (false)
;
2821 return false;
2822 }
2823
2824 // If we can represent this as a simple value, we have to build the actual
2825 // value to store, which requires expanding the byte present in memset to
2826 // a sensible representation for the alloca type. This is essentially
2827 // splatting the byte to a sufficiently wide integer, splatting it across
2828 // any desired vector width, and bitcasting to the final type.
2829 Value *V;
2830
2831 if (VecTy) {
2832 // If this is a memset of a vectorized alloca, insert it.
2833 assert(ElementTy == ScalarTy)((ElementTy == ScalarTy) ? static_cast<void> (0) : __assert_fail
("ElementTy == ScalarTy", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2833, __PRETTY_FUNCTION__))
;
2834
2835 unsigned BeginIndex = getIndex(NewBeginOffset);
2836 unsigned EndIndex = getIndex(NewEndOffset);
2837 assert(EndIndex > BeginIndex && "Empty vector!")((EndIndex > BeginIndex && "Empty vector!") ? static_cast
<void> (0) : __assert_fail ("EndIndex > BeginIndex && \"Empty vector!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2837, __PRETTY_FUNCTION__))
;
2838 unsigned NumElements = EndIndex - BeginIndex;
2839 assert(NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() &&((NumElements <= cast<FixedVectorType>(VecTy)->getNumElements
() && "Too many elements!") ? static_cast<void>
(0) : __assert_fail ("NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2840, __PRETTY_FUNCTION__))
2840 "Too many elements!")((NumElements <= cast<FixedVectorType>(VecTy)->getNumElements
() && "Too many elements!") ? static_cast<void>
(0) : __assert_fail ("NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() && \"Too many elements!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2840, __PRETTY_FUNCTION__))
;
2841
2842 Value *Splat = getIntegerSplat(
2843 II.getValue(), DL.getTypeSizeInBits(ElementTy).getFixedSize() / 8);
2844 Splat = convertValue(DL, IRB, Splat, ElementTy);
2845 if (NumElements > 1)
2846 Splat = getVectorSplat(Splat, NumElements);
2847
2848 Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2849 NewAI.getAlign(), "oldload");
2850 V = insertVector(IRB, Old, Splat, BeginIndex, "vec");
2851 } else if (IntTy) {
2852 // If this is a memset on an alloca where we can widen stores, insert the
2853 // set integer.
2854 assert(!II.isVolatile())((!II.isVolatile()) ? static_cast<void> (0) : __assert_fail
("!II.isVolatile()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2854, __PRETTY_FUNCTION__))
;
2855
2856 uint64_t Size = NewEndOffset - NewBeginOffset;
2857 V = getIntegerSplat(II.getValue(), Size);
2858
2859 if (IntTy && (BeginOffset != NewAllocaBeginOffset ||
2860 EndOffset != NewAllocaBeginOffset)) {
2861 Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
2862 NewAI.getAlign(), "oldload");
2863 Old = convertValue(DL, IRB, Old, IntTy);
2864 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
2865 V = insertInteger(DL, IRB, Old, V, Offset, "insert");
2866 } else {
2867 assert(V->getType() == IntTy &&((V->getType() == IntTy && "Wrong type for an alloca wide integer!"
) ? static_cast<void> (0) : __assert_fail ("V->getType() == IntTy && \"Wrong type for an alloca wide integer!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2868, __PRETTY_FUNCTION__))
2868 "Wrong type for an alloca wide integer!")((V->getType() == IntTy && "Wrong type for an alloca wide integer!"
) ? static_cast<void> (0) : __assert_fail ("V->getType() == IntTy && \"Wrong type for an alloca wide integer!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2868, __PRETTY_FUNCTION__))
;
2869 }
2870 V = convertValue(DL, IRB, V, AllocaTy);
2871 } else {
2872 // Established these invariants above.
2873 assert(NewBeginOffset == NewAllocaBeginOffset)((NewBeginOffset == NewAllocaBeginOffset) ? static_cast<void
> (0) : __assert_fail ("NewBeginOffset == NewAllocaBeginOffset"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2873, __PRETTY_FUNCTION__))
;
2874 assert(NewEndOffset == NewAllocaEndOffset)((NewEndOffset == NewAllocaEndOffset) ? static_cast<void>
(0) : __assert_fail ("NewEndOffset == NewAllocaEndOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2874, __PRETTY_FUNCTION__))
;
2875
2876 V = getIntegerSplat(II.getValue(),
2877 DL.getTypeSizeInBits(ScalarTy).getFixedSize() / 8);
2878 if (VectorType *AllocaVecTy = dyn_cast<VectorType>(AllocaTy))
2879 V = getVectorSplat(
2880 V, cast<FixedVectorType>(AllocaVecTy)->getNumElements());
2881
2882 V = convertValue(DL, IRB, V, AllocaTy);
2883 }
2884
2885 StoreInst *New =
2886 IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlign(), II.isVolatile());
2887 if (AATags)
2888 New->setAAMetadata(AATags);
2889 LLVM_DEBUG(dbgs() << " to: " << *New << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *New <<
"\n"; } } while (false)
;
2890 return !II.isVolatile();
2891 }
2892
2893 bool visitMemTransferInst(MemTransferInst &II) {
2894 // Rewriting of memory transfer instructions can be a bit tricky. We break
2895 // them into two categories: split intrinsics and unsplit intrinsics.
2896
2897 LLVM_DEBUG(dbgs() << " original: " << II << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << II <<
"\n"; } } while (false)
;
1
Assuming 'DebugFlag' is false
2
Loop condition is false. Exiting loop
2898
2899 AAMDNodes AATags;
2900 II.getAAMetadata(AATags);
2901
2902 bool IsDest = &II.getRawDestUse() == OldUse;
2903 assert((IsDest && II.getRawDest() == OldPtr) ||(((IsDest && II.getRawDest() == OldPtr) || (!IsDest &&
II.getRawSource() == OldPtr)) ? static_cast<void> (0) :
__assert_fail ("(IsDest && II.getRawDest() == OldPtr) || (!IsDest && II.getRawSource() == OldPtr)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2904, __PRETTY_FUNCTION__))
3
Assuming 'IsDest' is false
4
Assuming the condition is true
5
'?' condition is true
2904 (!IsDest && II.getRawSource() == OldPtr))(((IsDest && II.getRawDest() == OldPtr) || (!IsDest &&
II.getRawSource() == OldPtr)) ? static_cast<void> (0) :
__assert_fail ("(IsDest && II.getRawDest() == OldPtr) || (!IsDest && II.getRawSource() == OldPtr)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2904, __PRETTY_FUNCTION__))
;
2905
2906 MaybeAlign SliceAlign = getSliceAlign();
2907
2908 // For unsplit intrinsics, we simply modify the source and destination
2909 // pointers in place. This isn't just an optimization, it is a matter of
2910 // correctness. With unsplit intrinsics we may be dealing with transfers
2911 // within a single alloca before SROA ran, or with transfers that have
2912 // a variable length. We may also be dealing with memmove instead of
2913 // memcpy, and so simply updating the pointers is the necessary for us to
2914 // update both source and dest of a single call.
2915 if (!IsSplittable) {
6
Assuming field 'IsSplittable' is true
7
Taking false branch
2916 Value *AdjustedPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
2917 if (IsDest) {
2918 II.setDest(AdjustedPtr);
2919 II.setDestAlignment(SliceAlign);
2920 }
2921 else {
2922 II.setSource(AdjustedPtr);
2923 II.setSourceAlignment(SliceAlign);
2924 }
2925
2926 LLVM_DEBUG(dbgs() << " to: " << II << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << II <<
"\n"; } } while (false)
;
2927 deleteIfTriviallyDead(OldPtr);
2928 return false;
2929 }
2930 // For split transfer intrinsics we have an incredibly useful assurance:
2931 // the source and destination do not reside within the same alloca, and at
2932 // least one of them does not escape. This means that we can replace
2933 // memmove with memcpy, and we don't need to worry about all manner of
2934 // downsides to splitting and transforming the operations.
2935
2936 // If this doesn't map cleanly onto the alloca type, and that type isn't
2937 // a single value type, just emit a memcpy.
2938 bool EmitMemCpy =
2939 !VecTy && !IntTy &&
8
Assuming field 'VecTy' is non-null
2940 (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset ||
2941 SliceSize !=
2942 DL.getTypeStoreSize(NewAI.getAllocatedType()).getFixedSize() ||
2943 !NewAI.getAllocatedType()->isSingleValueType());
2944
2945 // If we're just going to emit a memcpy, the alloca hasn't changed, and the
2946 // size hasn't been shrunk based on analysis of the viable range, this is
2947 // a no-op.
2948 if (EmitMemCpy
8.1
'EmitMemCpy' is false
&& &OldAI == &NewAI) {
2949 // Ensure the start lines up.
2950 assert(NewBeginOffset == BeginOffset)((NewBeginOffset == BeginOffset) ? static_cast<void> (0
) : __assert_fail ("NewBeginOffset == BeginOffset", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2950, __PRETTY_FUNCTION__))
;
2951
2952 // Rewrite the size as needed.
2953 if (NewEndOffset != EndOffset)
2954 II.setLength(ConstantInt::get(II.getLength()->getType(),
2955 NewEndOffset - NewBeginOffset));
2956 return false;
2957 }
2958 // Record this instruction for deletion.
2959 Pass.DeadInsts.push_back(&II);
2960
2961 // Strip all inbounds GEPs and pointer casts to try to dig out any root
2962 // alloca that should be re-examined after rewriting this instruction.
2963 Value *OtherPtr = IsDest
8.2
'IsDest' is false
? II.getRawSource() : II.getRawDest();
9
'?' condition is false
2964 if (AllocaInst *AI
10.1
'AI' is null
=
11
Taking false branch
2965 dyn_cast<AllocaInst>(OtherPtr->stripInBoundsOffsets())) {
10
Assuming the object is not a 'AllocaInst'
2966 assert(AI != &OldAI && AI != &NewAI &&((AI != &OldAI && AI != &NewAI && "Splittable transfers cannot reach the same alloca on both ends."
) ? static_cast<void> (0) : __assert_fail ("AI != &OldAI && AI != &NewAI && \"Splittable transfers cannot reach the same alloca on both ends.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2967, __PRETTY_FUNCTION__))
2967 "Splittable transfers cannot reach the same alloca on both ends.")((AI != &OldAI && AI != &NewAI && "Splittable transfers cannot reach the same alloca on both ends."
) ? static_cast<void> (0) : __assert_fail ("AI != &OldAI && AI != &NewAI && \"Splittable transfers cannot reach the same alloca on both ends.\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 2967, __PRETTY_FUNCTION__))
;
2968 Pass.Worklist.insert(AI);
2969 }
2970
2971 Type *OtherPtrTy = OtherPtr->getType();
2972 unsigned OtherAS = OtherPtrTy->getPointerAddressSpace();
2973
2974 // Compute the relative offset for the other pointer within the transfer.
2975 unsigned OffsetWidth = DL.getIndexSizeInBits(OtherAS);
2976 APInt OtherOffset(OffsetWidth, NewBeginOffset - BeginOffset);
2977 Align OtherAlign =
2978 (IsDest
11.1
'IsDest' is false
? II.getSourceAlign() : II.getDestAlign()).valueOrOne();
12
'?' condition is false
2979 OtherAlign =
2980 commonAlignment(OtherAlign, OtherOffset.zextOrTrunc(64).getZExtValue());
2981
2982 if (EmitMemCpy
12.1
'EmitMemCpy' is false
) {
13
Taking false branch
2983 // Compute the other pointer, folding as much as possible to produce
2984 // a single, simple GEP in most cases.
2985 OtherPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy,
2986 OtherPtr->getName() + ".");
2987
2988 Value *OurPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
2989 Type *SizeTy = II.getLength()->getType();
2990 Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset);
2991
2992 Value *DestPtr, *SrcPtr;
2993 MaybeAlign DestAlign, SrcAlign;
2994 // Note: IsDest is true iff we're copying into the new alloca slice
2995 if (IsDest) {
2996 DestPtr = OurPtr;
2997 DestAlign = SliceAlign;
2998 SrcPtr = OtherPtr;
2999 SrcAlign = OtherAlign;
3000 } else {
3001 DestPtr = OtherPtr;
3002 DestAlign = OtherAlign;
3003 SrcPtr = OurPtr;
3004 SrcAlign = SliceAlign;
3005 }
3006 CallInst *New = IRB.CreateMemCpy(DestPtr, DestAlign, SrcPtr, SrcAlign,
3007 Size, II.isVolatile());
3008 if (AATags)
3009 New->setAAMetadata(AATags);
3010 LLVM_DEBUG(dbgs() << " to: " << *New << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *New <<
"\n"; } } while (false)
;
3011 return false;
3012 }
3013
3014 bool IsWholeAlloca = NewBeginOffset == NewAllocaBeginOffset &&
14
Assuming field 'NewBeginOffset' is not equal to field 'NewAllocaBeginOffset'
3015 NewEndOffset == NewAllocaEndOffset;
3016 uint64_t Size = NewEndOffset - NewBeginOffset;
3017 unsigned BeginIndex = VecTy
14.1
Field 'VecTy' is non-null
? getIndex(NewBeginOffset) : 0;
15
'?' condition is true
3018 unsigned EndIndex = VecTy
15.1
Field 'VecTy' is non-null
? getIndex(NewEndOffset) : 0;
16
'?' condition is true
3019 unsigned NumElements = EndIndex - BeginIndex;
3020 IntegerType *SubIntTy =
19
'SubIntTy' initialized to a null pointer value
3021 IntTy ? Type::getIntNTy(IntTy->getContext(), Size * 8) : nullptr;
17
Assuming field 'IntTy' is null
18
'?' condition is false
3022
3023 // Reset the other pointer type to match the register type we're going to
3024 // use, but using the address space of the original other pointer.
3025 Type *OtherTy;
3026 if (VecTy
19.1
Field 'VecTy' is non-null
&& !IsWholeAlloca
19.2
'IsWholeAlloca' is false
) {
20
Taking true branch
3027 if (NumElements == 1)
21
Assuming 'NumElements' is not equal to 1
22
Taking false branch
3028 OtherTy = VecTy->getElementType();
3029 else
3030 OtherTy = FixedVectorType::get(VecTy->getElementType(), NumElements);
3031 } else if (IntTy && !IsWholeAlloca) {
3032 OtherTy = SubIntTy;
3033 } else {
3034 OtherTy = NewAllocaTy;
3035 }
3036 OtherPtrTy = OtherTy->getPointerTo(OtherAS);
3037
3038 Value *SrcPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy,
23
Calling 'getAdjustedPtr'
53
Returning from 'getAdjustedPtr'
3039 OtherPtr->getName() + ".");
3040 MaybeAlign SrcAlign = OtherAlign;
3041 Value *DstPtr = &NewAI;
3042 MaybeAlign DstAlign = SliceAlign;
3043 if (!IsDest
53.1
'IsDest' is false
) {
54
Taking true branch
3044 std::swap(SrcPtr, DstPtr);
3045 std::swap(SrcAlign, DstAlign);
3046 }
3047
3048 Value *Src;
3049 if (VecTy && !IsWholeAlloca && !IsDest) {
55
Assuming field 'VecTy' is null
3050 Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
3051 NewAI.getAlign(), "load");
3052 Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec");
3053 } else if (IntTy && !IsWholeAlloca
56.1
'IsWholeAlloca' is false
&& !IsDest
56.2
'IsDest' is false
) {
56
Assuming field 'IntTy' is non-null
57
Taking true branch
3054 Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
3055 NewAI.getAlign(), "load");
3056 Src = convertValue(DL, IRB, Src, IntTy);
3057 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3058 Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract");
58
Passing null pointer value via 4th parameter 'Ty'
59
Calling 'extractInteger'
3059 } else {
3060 LoadInst *Load = IRB.CreateAlignedLoad(OtherTy, SrcPtr, SrcAlign,
3061 II.isVolatile(), "copyload");
3062 if (AATags)
3063 Load->setAAMetadata(AATags);
3064 Src = Load;
3065 }
3066
3067 if (VecTy && !IsWholeAlloca && IsDest) {
3068 Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
3069 NewAI.getAlign(), "oldload");
3070 Src = insertVector(IRB, Old, Src, BeginIndex, "vec");
3071 } else if (IntTy && !IsWholeAlloca && IsDest) {
3072 Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI,
3073 NewAI.getAlign(), "oldload");
3074 Old = convertValue(DL, IRB, Old, IntTy);
3075 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3076 Src = insertInteger(DL, IRB, Old, Src, Offset, "insert");
3077 Src = convertValue(DL, IRB, Src, NewAllocaTy);
3078 }
3079
3080 StoreInst *Store = cast<StoreInst>(
3081 IRB.CreateAlignedStore(Src, DstPtr, DstAlign, II.isVolatile()));
3082 if (AATags)
3083 Store->setAAMetadata(AATags);
3084 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *Store <<
"\n"; } } while (false)
;
3085 return !II.isVolatile();
3086 }
3087
3088 bool visitIntrinsicInst(IntrinsicInst &II) {
3089 assert((II.isLifetimeStartOrEnd() || II.isDroppable()) &&(((II.isLifetimeStartOrEnd() || II.isDroppable()) && "Unexpected intrinsic!"
) ? static_cast<void> (0) : __assert_fail ("(II.isLifetimeStartOrEnd() || II.isDroppable()) && \"Unexpected intrinsic!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3090, __PRETTY_FUNCTION__))
3090 "Unexpected intrinsic!")(((II.isLifetimeStartOrEnd() || II.isDroppable()) && "Unexpected intrinsic!"
) ? static_cast<void> (0) : __assert_fail ("(II.isLifetimeStartOrEnd() || II.isDroppable()) && \"Unexpected intrinsic!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3090, __PRETTY_FUNCTION__))
;
3091 LLVM_DEBUG(dbgs() << " original: " << II << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << II <<
"\n"; } } while (false)
;
3092
3093 // Record this instruction for deletion.
3094 Pass.DeadInsts.push_back(&II);
3095
3096 if (II.isDroppable()) {
3097 assert(II.getIntrinsicID() == Intrinsic::assume && "Expected assume")((II.getIntrinsicID() == Intrinsic::assume && "Expected assume"
) ? static_cast<void> (0) : __assert_fail ("II.getIntrinsicID() == Intrinsic::assume && \"Expected assume\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3097, __PRETTY_FUNCTION__))
;
3098 // TODO For now we forget assumed information, this can be improved.
3099 OldPtr->dropDroppableUsesIn(II);
3100 return true;
3101 }
3102
3103 assert(II.getArgOperand(1) == OldPtr)((II.getArgOperand(1) == OldPtr) ? static_cast<void> (0
) : __assert_fail ("II.getArgOperand(1) == OldPtr", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3103, __PRETTY_FUNCTION__))
;
3104 // Lifetime intrinsics are only promotable if they cover the whole alloca.
3105 // Therefore, we drop lifetime intrinsics which don't cover the whole
3106 // alloca.
3107 // (In theory, intrinsics which partially cover an alloca could be
3108 // promoted, but PromoteMemToReg doesn't handle that case.)
3109 // FIXME: Check whether the alloca is promotable before dropping the
3110 // lifetime intrinsics?
3111 if (NewBeginOffset != NewAllocaBeginOffset ||
3112 NewEndOffset != NewAllocaEndOffset)
3113 return true;
3114
3115 ConstantInt *Size =
3116 ConstantInt::get(cast<IntegerType>(II.getArgOperand(0)->getType()),
3117 NewEndOffset - NewBeginOffset);
3118 // Lifetime intrinsics always expect an i8* so directly get such a pointer
3119 // for the new alloca slice.
3120 Type *PointerTy = IRB.getInt8PtrTy(OldPtr->getType()->getPointerAddressSpace());
3121 Value *Ptr = getNewAllocaSlicePtr(IRB, PointerTy);
3122 Value *New;
3123 if (II.getIntrinsicID() == Intrinsic::lifetime_start)
3124 New = IRB.CreateLifetimeStart(Ptr, Size);
3125 else
3126 New = IRB.CreateLifetimeEnd(Ptr, Size);
3127
3128 (void)New;
3129 LLVM_DEBUG(dbgs() << " to: " << *New << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *New <<
"\n"; } } while (false)
;
3130
3131 return true;
3132 }
3133
3134 void fixLoadStoreAlign(Instruction &Root) {
3135 // This algorithm implements the same visitor loop as
3136 // hasUnsafePHIOrSelectUse, and fixes the alignment of each load
3137 // or store found.
3138 SmallPtrSet<Instruction *, 4> Visited;
3139 SmallVector<Instruction *, 4> Uses;
3140 Visited.insert(&Root);
3141 Uses.push_back(&Root);
3142 do {
3143 Instruction *I = Uses.pop_back_val();
3144
3145 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
3146 LI->setAlignment(std::min(LI->getAlign(), getSliceAlign()));
3147 continue;
3148 }
3149 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
3150 SI->setAlignment(std::min(SI->getAlign(), getSliceAlign()));
3151 continue;
3152 }
3153
3154 assert(isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I) ||((isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I
) || isa<PHINode>(I) || isa<SelectInst>(I) || isa
<GetElementPtrInst>(I)) ? static_cast<void> (0) :
__assert_fail ("isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I) || isa<PHINode>(I) || isa<SelectInst>(I) || isa<GetElementPtrInst>(I)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3156, __PRETTY_FUNCTION__))
3155 isa<PHINode>(I) || isa<SelectInst>(I) ||((isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I
) || isa<PHINode>(I) || isa<SelectInst>(I) || isa
<GetElementPtrInst>(I)) ? static_cast<void> (0) :
__assert_fail ("isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I) || isa<PHINode>(I) || isa<SelectInst>(I) || isa<GetElementPtrInst>(I)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3156, __PRETTY_FUNCTION__))
3156 isa<GetElementPtrInst>(I))((isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I
) || isa<PHINode>(I) || isa<SelectInst>(I) || isa
<GetElementPtrInst>(I)) ? static_cast<void> (0) :
__assert_fail ("isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I) || isa<PHINode>(I) || isa<SelectInst>(I) || isa<GetElementPtrInst>(I)"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3156, __PRETTY_FUNCTION__))
;
3157 for (User *U : I->users())
3158 if (Visited.insert(cast<Instruction>(U)).second)
3159 Uses.push_back(cast<Instruction>(U));
3160 } while (!Uses.empty());
3161 }
3162
3163 bool visitPHINode(PHINode &PN) {
3164 LLVM_DEBUG(dbgs() << " original: " << PN << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << PN <<
"\n"; } } while (false)
;
3165 assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable")((BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable"
) ? static_cast<void> (0) : __assert_fail ("BeginOffset >= NewAllocaBeginOffset && \"PHIs are unsplittable\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3165, __PRETTY_FUNCTION__))
;
3166 assert(EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable")((EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable"
) ? static_cast<void> (0) : __assert_fail ("EndOffset <= NewAllocaEndOffset && \"PHIs are unsplittable\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3166, __PRETTY_FUNCTION__))
;
3167
3168 // We would like to compute a new pointer in only one place, but have it be
3169 // as local as possible to the PHI. To do that, we re-use the location of
3170 // the old pointer, which necessarily must be in the right position to
3171 // dominate the PHI.
3172 IRBuilderBase::InsertPointGuard Guard(IRB);
3173 if (isa<PHINode>(OldPtr))
3174 IRB.SetInsertPoint(&*OldPtr->getParent()->getFirstInsertionPt());
3175 else
3176 IRB.SetInsertPoint(OldPtr);
3177 IRB.SetCurrentDebugLocation(OldPtr->getDebugLoc());
3178
3179 Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
3180 // Replace the operands which were using the old pointer.
3181 std::replace(PN.op_begin(), PN.op_end(), cast<Value>(OldPtr), NewPtr);
3182
3183 LLVM_DEBUG(dbgs() << " to: " << PN << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << PN <<
"\n"; } } while (false)
;
3184 deleteIfTriviallyDead(OldPtr);
3185
3186 // Fix the alignment of any loads or stores using this PHI node.
3187 fixLoadStoreAlign(PN);
3188
3189 // PHIs can't be promoted on their own, but often can be speculated. We
3190 // check the speculation outside of the rewriter so that we see the
3191 // fully-rewritten alloca.
3192 PHIUsers.insert(&PN);
3193 return true;
3194 }
3195
3196 bool visitSelectInst(SelectInst &SI) {
3197 LLVM_DEBUG(dbgs() << " original: " << SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << SI <<
"\n"; } } while (false)
;
3198 assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) &&(((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr
) && "Pointer isn't an operand!") ? static_cast<void
> (0) : __assert_fail ("(SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) && \"Pointer isn't an operand!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3199, __PRETTY_FUNCTION__))
3199 "Pointer isn't an operand!")(((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr
) && "Pointer isn't an operand!") ? static_cast<void
> (0) : __assert_fail ("(SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) && \"Pointer isn't an operand!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3199, __PRETTY_FUNCTION__))
;
3200 assert(BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable")((BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable"
) ? static_cast<void> (0) : __assert_fail ("BeginOffset >= NewAllocaBeginOffset && \"Selects are unsplittable\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3200, __PRETTY_FUNCTION__))
;
3201 assert(EndOffset <= NewAllocaEndOffset && "Selects are unsplittable")((EndOffset <= NewAllocaEndOffset && "Selects are unsplittable"
) ? static_cast<void> (0) : __assert_fail ("EndOffset <= NewAllocaEndOffset && \"Selects are unsplittable\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3201, __PRETTY_FUNCTION__))
;
3202
3203 Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
3204 // Replace the operands which were using the old pointer.
3205 if (SI.getOperand(1) == OldPtr)
3206 SI.setOperand(1, NewPtr);
3207 if (SI.getOperand(2) == OldPtr)
3208 SI.setOperand(2, NewPtr);
3209
3210 LLVM_DEBUG(dbgs() << " to: " << SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << SI <<
"\n"; } } while (false)
;
3211 deleteIfTriviallyDead(OldPtr);
3212
3213 // Fix the alignment of any loads or stores using this select.
3214 fixLoadStoreAlign(SI);
3215
3216 // Selects can't be promoted on their own, but often can be speculated. We
3217 // check the speculation outside of the rewriter so that we see the
3218 // fully-rewritten alloca.
3219 SelectUsers.insert(&SI);
3220 return true;
3221 }
3222};
3223
3224namespace {
3225
3226/// Visitor to rewrite aggregate loads and stores as scalar.
3227///
3228/// This pass aggressively rewrites all aggregate loads and stores on
3229/// a particular pointer (or any pointer derived from it which we can identify)
3230/// with scalar loads and stores.
3231class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3232 // Befriend the base class so it can delegate to private visit methods.
3233 friend class InstVisitor<AggLoadStoreRewriter, bool>;
3234
3235 /// Queue of pointer uses to analyze and potentially rewrite.
3236 SmallVector<Use *, 8> Queue;
3237
3238 /// Set to prevent us from cycling with phi nodes and loops.
3239 SmallPtrSet<User *, 8> Visited;
3240
3241 /// The current pointer use being rewritten. This is used to dig up the used
3242 /// value (as opposed to the user).
3243 Use *U = nullptr;
3244
3245 /// Used to calculate offsets, and hence alignment, of subobjects.
3246 const DataLayout &DL;
3247
3248public:
3249 AggLoadStoreRewriter(const DataLayout &DL) : DL(DL) {}
3250
3251 /// Rewrite loads and stores through a pointer and all pointers derived from
3252 /// it.
3253 bool rewrite(Instruction &I) {
3254 LLVM_DEBUG(dbgs() << " Rewriting FCA loads and stores...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting FCA loads and stores...\n"
; } } while (false)
;
3255 enqueueUsers(I);
3256 bool Changed = false;
3257 while (!Queue.empty()) {
3258 U = Queue.pop_back_val();
3259 Changed |= visit(cast<Instruction>(U->getUser()));
3260 }
3261 return Changed;
3262 }
3263
3264private:
3265 /// Enqueue all the users of the given instruction for further processing.
3266 /// This uses a set to de-duplicate users.
3267 void enqueueUsers(Instruction &I) {
3268 for (Use &U : I.uses())
3269 if (Visited.insert(U.getUser()).second)
3270 Queue.push_back(&U);
3271 }
3272
3273 // Conservative default is to not rewrite anything.
3274 bool visitInstruction(Instruction &I) { return false; }
3275
3276 /// Generic recursive split emission class.
3277 template <typename Derived> class OpSplitter {
3278 protected:
3279 /// The builder used to form new instructions.
3280 IRBuilderTy IRB;
3281
3282 /// The indices which to be used with insert- or extractvalue to select the
3283 /// appropriate value within the aggregate.
3284 SmallVector<unsigned, 4> Indices;
3285
3286 /// The indices to a GEP instruction which will move Ptr to the correct slot
3287 /// within the aggregate.
3288 SmallVector<Value *, 4> GEPIndices;
3289
3290 /// The base pointer of the original op, used as a base for GEPing the
3291 /// split operations.
3292 Value *Ptr;
3293
3294 /// The base pointee type being GEPed into.
3295 Type *BaseTy;
3296
3297 /// Known alignment of the base pointer.
3298 Align BaseAlign;
3299
3300 /// To calculate offset of each component so we can correctly deduce
3301 /// alignments.
3302 const DataLayout &DL;
3303
3304 /// Initialize the splitter with an insertion point, Ptr and start with a
3305 /// single zero GEP index.
3306 OpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
3307 Align BaseAlign, const DataLayout &DL)
3308 : IRB(InsertionPoint), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr),
3309 BaseTy(BaseTy), BaseAlign(BaseAlign), DL(DL) {}
3310
3311 public:
3312 /// Generic recursive split emission routine.
3313 ///
3314 /// This method recursively splits an aggregate op (load or store) into
3315 /// scalar or vector ops. It splits recursively until it hits a single value
3316 /// and emits that single value operation via the template argument.
3317 ///
3318 /// The logic of this routine relies on GEPs and insertvalue and
3319 /// extractvalue all operating with the same fundamental index list, merely
3320 /// formatted differently (GEPs need actual values).
3321 ///
3322 /// \param Ty The type being split recursively into smaller ops.
3323 /// \param Agg The aggregate value being built up or stored, depending on
3324 /// whether this is splitting a load or a store respectively.
3325 void emitSplitOps(Type *Ty, Value *&Agg, const Twine &Name) {
3326 if (Ty->isSingleValueType()) {
3327 unsigned Offset = DL.getIndexedOffsetInType(BaseTy, GEPIndices);
3328 return static_cast<Derived *>(this)->emitFunc(
3329 Ty, Agg, commonAlignment(BaseAlign, Offset), Name);
3330 }
3331
3332 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
3333 unsigned OldSize = Indices.size();
3334 (void)OldSize;
3335 for (unsigned Idx = 0, Size = ATy->getNumElements(); Idx != Size;
3336 ++Idx) {
3337 assert(Indices.size() == OldSize && "Did not return to the old size")((Indices.size() == OldSize && "Did not return to the old size"
) ? static_cast<void> (0) : __assert_fail ("Indices.size() == OldSize && \"Did not return to the old size\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3337, __PRETTY_FUNCTION__))
;
3338 Indices.push_back(Idx);
3339 GEPIndices.push_back(IRB.getInt32(Idx));
3340 emitSplitOps(ATy->getElementType(), Agg, Name + "." + Twine(Idx));
3341 GEPIndices.pop_back();
3342 Indices.pop_back();
3343 }
3344 return;
3345 }
3346
3347 if (StructType *STy = dyn_cast<StructType>(Ty)) {
3348 unsigned OldSize = Indices.size();
3349 (void)OldSize;
3350 for (unsigned Idx = 0, Size = STy->getNumElements(); Idx != Size;
3351 ++Idx) {
3352 assert(Indices.size() == OldSize && "Did not return to the old size")((Indices.size() == OldSize && "Did not return to the old size"
) ? static_cast<void> (0) : __assert_fail ("Indices.size() == OldSize && \"Did not return to the old size\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3352, __PRETTY_FUNCTION__))
;
3353 Indices.push_back(Idx);
3354 GEPIndices.push_back(IRB.getInt32(Idx));
3355 emitSplitOps(STy->getElementType(Idx), Agg, Name + "." + Twine(Idx));
3356 GEPIndices.pop_back();
3357 Indices.pop_back();
3358 }
3359 return;
3360 }
3361
3362 llvm_unreachable("Only arrays and structs are aggregate loadable types")::llvm::llvm_unreachable_internal("Only arrays and structs are aggregate loadable types"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3362)
;
3363 }
3364 };
3365
3366 struct LoadOpSplitter : public OpSplitter<LoadOpSplitter> {
3367 AAMDNodes AATags;
3368
3369 LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
3370 AAMDNodes AATags, Align BaseAlign, const DataLayout &DL)
3371 : OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign,
3372 DL),
3373 AATags(AATags) {}
3374
3375 /// Emit a leaf load of a single value. This is called at the leaves of the
3376 /// recursive emission to actually load values.
3377 void emitFunc(Type *Ty, Value *&Agg, Align Alignment, const Twine &Name) {
3378 assert(Ty->isSingleValueType())((Ty->isSingleValueType()) ? static_cast<void> (0) :
__assert_fail ("Ty->isSingleValueType()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3378, __PRETTY_FUNCTION__))
;
3379 // Load the single value and insert it using the indices.
3380 Value *GEP =
3381 IRB.CreateInBoundsGEP(BaseTy, Ptr, GEPIndices, Name + ".gep");
3382 LoadInst *Load =
3383 IRB.CreateAlignedLoad(Ty, GEP, Alignment, Name + ".load");
3384 if (AATags)
3385 Load->setAAMetadata(AATags);
3386 Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert");
3387 LLVM_DEBUG(dbgs() << " to: " << *Load << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *Load <<
"\n"; } } while (false)
;
3388 }
3389 };
3390
3391 bool visitLoadInst(LoadInst &LI) {
3392 assert(LI.getPointerOperand() == *U)((LI.getPointerOperand() == *U) ? static_cast<void> (0)
: __assert_fail ("LI.getPointerOperand() == *U", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3392, __PRETTY_FUNCTION__))
;
3393 if (!LI.isSimple() || LI.getType()->isSingleValueType())
3394 return false;
3395
3396 // We have an aggregate being loaded, split it apart.
3397 LLVM_DEBUG(dbgs() << " original: " << LI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << LI <<
"\n"; } } while (false)
;
3398 AAMDNodes AATags;
3399 LI.getAAMetadata(AATags);
3400 LoadOpSplitter Splitter(&LI, *U, LI.getType(), AATags,
3401 getAdjustedAlignment(&LI, 0), DL);
3402 Value *V = UndefValue::get(LI.getType());
3403 Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca");
3404 Visited.erase(&LI);
3405 LI.replaceAllUsesWith(V);
3406 LI.eraseFromParent();
3407 return true;
3408 }
3409
3410 struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> {
3411 StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
3412 AAMDNodes AATags, Align BaseAlign, const DataLayout &DL)
3413 : OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign,
3414 DL),
3415 AATags(AATags) {}
3416 AAMDNodes AATags;
3417 /// Emit a leaf store of a single value. This is called at the leaves of the
3418 /// recursive emission to actually produce stores.
3419 void emitFunc(Type *Ty, Value *&Agg, Align Alignment, const Twine &Name) {
3420 assert(Ty->isSingleValueType())((Ty->isSingleValueType()) ? static_cast<void> (0) :
__assert_fail ("Ty->isSingleValueType()", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3420, __PRETTY_FUNCTION__))
;
3421 // Extract the single value and store it using the indices.
3422 //
3423 // The gep and extractvalue values are factored out of the CreateStore
3424 // call to make the output independent of the argument evaluation order.
3425 Value *ExtractValue =
3426 IRB.CreateExtractValue(Agg, Indices, Name + ".extract");
3427 Value *InBoundsGEP =
3428 IRB.CreateInBoundsGEP(BaseTy, Ptr, GEPIndices, Name + ".gep");
3429 StoreInst *Store =
3430 IRB.CreateAlignedStore(ExtractValue, InBoundsGEP, Alignment);
3431 if (AATags)
3432 Store->setAAMetadata(AATags);
3433 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " to: " << *Store <<
"\n"; } } while (false)
;
3434 }
3435 };
3436
3437 bool visitStoreInst(StoreInst &SI) {
3438 if (!SI.isSimple() || SI.getPointerOperand() != *U)
3439 return false;
3440 Value *V = SI.getValueOperand();
3441 if (V->getType()->isSingleValueType())
3442 return false;
3443
3444 // We have an aggregate being stored, split it apart.
3445 LLVM_DEBUG(dbgs() << " original: " << SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " original: " << SI <<
"\n"; } } while (false)
;
3446 AAMDNodes AATags;
3447 SI.getAAMetadata(AATags);
3448 StoreOpSplitter Splitter(&SI, *U, V->getType(), AATags,
3449 getAdjustedAlignment(&SI, 0), DL);
3450 Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca");
3451 Visited.erase(&SI);
3452 SI.eraseFromParent();
3453 return true;
3454 }
3455
3456 bool visitBitCastInst(BitCastInst &BC) {
3457 enqueueUsers(BC);
3458 return false;
3459 }
3460
3461 bool visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) {
3462 enqueueUsers(ASC);
3463 return false;
3464 }
3465
3466 // Fold gep (select cond, ptr1, ptr2) => select cond, gep(ptr1), gep(ptr2)
3467 bool foldGEPSelect(GetElementPtrInst &GEPI) {
3468 if (!GEPI.hasAllConstantIndices())
3469 return false;
3470
3471 SelectInst *Sel = cast<SelectInst>(GEPI.getPointerOperand());
3472
3473 LLVM_DEBUG(dbgs() << " Rewriting gep(select) -> select(gep):"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(select) -> select(gep):"
<< "\n original: " << *Sel << "\n "
<< GEPI; } } while (false)
3474 << "\n original: " << *Seldo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(select) -> select(gep):"
<< "\n original: " << *Sel << "\n "
<< GEPI; } } while (false)
3475 << "\n " << GEPI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(select) -> select(gep):"
<< "\n original: " << *Sel << "\n "
<< GEPI; } } while (false)
;
3476
3477 IRBuilderTy Builder(&GEPI);
3478 SmallVector<Value *, 4> Index(GEPI.indices());
3479 bool IsInBounds = GEPI.isInBounds();
3480
3481 Value *True = Sel->getTrueValue();
3482 Value *NTrue =
3483 IsInBounds
3484 ? Builder.CreateInBoundsGEP(True, Index,
3485 True->getName() + ".sroa.gep")
3486 : Builder.CreateGEP(True, Index, True->getName() + ".sroa.gep");
3487
3488 Value *False = Sel->getFalseValue();
3489
3490 Value *NFalse =
3491 IsInBounds
3492 ? Builder.CreateInBoundsGEP(False, Index,
3493 False->getName() + ".sroa.gep")
3494 : Builder.CreateGEP(False, Index, False->getName() + ".sroa.gep");
3495
3496 Value *NSel = Builder.CreateSelect(Sel->getCondition(), NTrue, NFalse,
3497 Sel->getName() + ".sroa.sel");
3498 Visited.erase(&GEPI);
3499 GEPI.replaceAllUsesWith(NSel);
3500 GEPI.eraseFromParent();
3501 Instruction *NSelI = cast<Instruction>(NSel);
3502 Visited.insert(NSelI);
3503 enqueueUsers(*NSelI);
3504
3505 LLVM_DEBUG(dbgs() << "\n to: " << *NTruedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "\n to: " << *NTrue
<< "\n " << *NFalse << "\n "
<< *NSel << '\n'; } } while (false)
3506 << "\n " << *NFalsedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "\n to: " << *NTrue
<< "\n " << *NFalse << "\n "
<< *NSel << '\n'; } } while (false)
3507 << "\n " << *NSel << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "\n to: " << *NTrue
<< "\n " << *NFalse << "\n "
<< *NSel << '\n'; } } while (false)
;
3508
3509 return true;
3510 }
3511
3512 // Fold gep (phi ptr1, ptr2) => phi gep(ptr1), gep(ptr2)
3513 bool foldGEPPhi(GetElementPtrInst &GEPI) {
3514 if (!GEPI.hasAllConstantIndices())
3515 return false;
3516
3517 PHINode *PHI = cast<PHINode>(GEPI.getPointerOperand());
3518 if (GEPI.getParent() != PHI->getParent() ||
3519 llvm::any_of(PHI->incoming_values(), [](Value *In)
3520 { Instruction *I = dyn_cast<Instruction>(In);
3521 return !I || isa<GetElementPtrInst>(I) || isa<PHINode>(I) ||
3522 succ_empty(I->getParent()) ||
3523 !I->getParent()->isLegalToHoistInto();
3524 }))
3525 return false;
3526
3527 LLVM_DEBUG(dbgs() << " Rewriting gep(phi) -> phi(gep):"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(phi) -> phi(gep):"
<< "\n original: " << *PHI << "\n "
<< GEPI << "\n to: "; } } while (false)
3528 << "\n original: " << *PHIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(phi) -> phi(gep):"
<< "\n original: " << *PHI << "\n "
<< GEPI << "\n to: "; } } while (false)
3529 << "\n " << GEPIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(phi) -> phi(gep):"
<< "\n original: " << *PHI << "\n "
<< GEPI << "\n to: "; } } while (false)
3530 << "\n to: ")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Rewriting gep(phi) -> phi(gep):"
<< "\n original: " << *PHI << "\n "
<< GEPI << "\n to: "; } } while (false)
;
3531
3532 SmallVector<Value *, 4> Index(GEPI.indices());
3533 bool IsInBounds = GEPI.isInBounds();
3534 IRBuilderTy PHIBuilder(GEPI.getParent()->getFirstNonPHI());
3535 PHINode *NewPN = PHIBuilder.CreatePHI(GEPI.getType(),
3536 PHI->getNumIncomingValues(),
3537 PHI->getName() + ".sroa.phi");
3538 for (unsigned I = 0, E = PHI->getNumIncomingValues(); I != E; ++I) {
3539 BasicBlock *B = PHI->getIncomingBlock(I);
3540 Value *NewVal = nullptr;
3541 int Idx = NewPN->getBasicBlockIndex(B);
3542 if (Idx >= 0) {
3543 NewVal = NewPN->getIncomingValue(Idx);
3544 } else {
3545 Instruction *In = cast<Instruction>(PHI->getIncomingValue(I));
3546
3547 IRBuilderTy B(In->getParent(), std::next(In->getIterator()));
3548 NewVal = IsInBounds
3549 ? B.CreateInBoundsGEP(In, Index, In->getName() + ".sroa.gep")
3550 : B.CreateGEP(In, Index, In->getName() + ".sroa.gep");
3551 }
3552 NewPN->addIncoming(NewVal, B);
3553 }
3554
3555 Visited.erase(&GEPI);
3556 GEPI.replaceAllUsesWith(NewPN);
3557 GEPI.eraseFromParent();
3558 Visited.insert(NewPN);
3559 enqueueUsers(*NewPN);
3560
3561 LLVM_DEBUG(for (Value *In : NewPN->incoming_values())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { for (Value *In : NewPN->incoming_values()) dbgs
() << "\n " << *In; dbgs() << "\n "
<< *NewPN << '\n'; } } while (false)
3562 dbgs() << "\n " << *In;do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { for (Value *In : NewPN->incoming_values()) dbgs
() << "\n " << *In; dbgs() << "\n "
<< *NewPN << '\n'; } } while (false)
3563 dbgs() << "\n " << *NewPN << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { for (Value *In : NewPN->incoming_values()) dbgs
() << "\n " << *In; dbgs() << "\n "
<< *NewPN << '\n'; } } while (false)
;
3564
3565 return true;
3566 }
3567
3568 bool visitGetElementPtrInst(GetElementPtrInst &GEPI) {
3569 if (isa<SelectInst>(GEPI.getPointerOperand()) &&
3570 foldGEPSelect(GEPI))
3571 return true;
3572
3573 if (isa<PHINode>(GEPI.getPointerOperand()) &&
3574 foldGEPPhi(GEPI))
3575 return true;
3576
3577 enqueueUsers(GEPI);
3578 return false;
3579 }
3580
3581 bool visitPHINode(PHINode &PN) {
3582 enqueueUsers(PN);
3583 return false;
3584 }
3585
3586 bool visitSelectInst(SelectInst &SI) {
3587 enqueueUsers(SI);
3588 return false;
3589 }
3590};
3591
3592} // end anonymous namespace
3593
3594/// Strip aggregate type wrapping.
3595///
3596/// This removes no-op aggregate types wrapping an underlying type. It will
3597/// strip as many layers of types as it can without changing either the type
3598/// size or the allocated size.
3599static Type *stripAggregateTypeWrapping(const DataLayout &DL, Type *Ty) {
3600 if (Ty->isSingleValueType())
3601 return Ty;
3602
3603 uint64_t AllocSize = DL.getTypeAllocSize(Ty).getFixedSize();
3604 uint64_t TypeSize = DL.getTypeSizeInBits(Ty).getFixedSize();
3605
3606 Type *InnerTy;
3607 if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) {
3608 InnerTy = ArrTy->getElementType();
3609 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
3610 const StructLayout *SL = DL.getStructLayout(STy);
3611 unsigned Index = SL->getElementContainingOffset(0);
3612 InnerTy = STy->getElementType(Index);
3613 } else {
3614 return Ty;
3615 }
3616
3617 if (AllocSize > DL.getTypeAllocSize(InnerTy).getFixedSize() ||
3618 TypeSize > DL.getTypeSizeInBits(InnerTy).getFixedSize())
3619 return Ty;
3620
3621 return stripAggregateTypeWrapping(DL, InnerTy);
3622}
3623
3624/// Try to find a partition of the aggregate type passed in for a given
3625/// offset and size.
3626///
3627/// This recurses through the aggregate type and tries to compute a subtype
3628/// based on the offset and size. When the offset and size span a sub-section
3629/// of an array, it will even compute a new array type for that sub-section,
3630/// and the same for structs.
3631///
3632/// Note that this routine is very strict and tries to find a partition of the
3633/// type which produces the *exact* right offset and size. It is not forgiving
3634/// when the size or offset cause either end of type-based partition to be off.
3635/// Also, this is a best-effort routine. It is reasonable to give up and not
3636/// return a type if necessary.
3637static Type *getTypePartition(const DataLayout &DL, Type *Ty, uint64_t Offset,
3638 uint64_t Size) {
3639 if (Offset == 0 && DL.getTypeAllocSize(Ty).getFixedSize() == Size)
3640 return stripAggregateTypeWrapping(DL, Ty);
3641 if (Offset > DL.getTypeAllocSize(Ty).getFixedSize() ||
3642 (DL.getTypeAllocSize(Ty).getFixedSize() - Offset) < Size)
3643 return nullptr;
3644
3645 if (isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
3646 Type *ElementTy;
3647 uint64_t TyNumElements;
3648 if (auto *AT = dyn_cast<ArrayType>(Ty)) {
3649 ElementTy = AT->getElementType();
3650 TyNumElements = AT->getNumElements();
3651 } else {
3652 // FIXME: This isn't right for vectors with non-byte-sized or
3653 // non-power-of-two sized elements.
3654 auto *VT = cast<FixedVectorType>(Ty);
3655 ElementTy = VT->getElementType();
3656 TyNumElements = VT->getNumElements();
3657 }
3658 uint64_t ElementSize = DL.getTypeAllocSize(ElementTy).getFixedSize();
3659 uint64_t NumSkippedElements = Offset / ElementSize;
3660 if (NumSkippedElements >= TyNumElements)
3661 return nullptr;
3662 Offset -= NumSkippedElements * ElementSize;
3663
3664 // First check if we need to recurse.
3665 if (Offset > 0 || Size < ElementSize) {
3666 // Bail if the partition ends in a different array element.
3667 if ((Offset + Size) > ElementSize)
3668 return nullptr;
3669 // Recurse through the element type trying to peel off offset bytes.
3670 return getTypePartition(DL, ElementTy, Offset, Size);
3671 }
3672 assert(Offset == 0)((Offset == 0) ? static_cast<void> (0) : __assert_fail (
"Offset == 0", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3672, __PRETTY_FUNCTION__))
;
3673
3674 if (Size == ElementSize)
3675 return stripAggregateTypeWrapping(DL, ElementTy);
3676 assert(Size > ElementSize)((Size > ElementSize) ? static_cast<void> (0) : __assert_fail
("Size > ElementSize", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3676, __PRETTY_FUNCTION__))
;
3677 uint64_t NumElements = Size / ElementSize;
3678 if (NumElements * ElementSize != Size)
3679 return nullptr;
3680 return ArrayType::get(ElementTy, NumElements);
3681 }
3682
3683 StructType *STy = dyn_cast<StructType>(Ty);
3684 if (!STy)
3685 return nullptr;
3686
3687 const StructLayout *SL = DL.getStructLayout(STy);
3688 if (Offset >= SL->getSizeInBytes())
3689 return nullptr;
3690 uint64_t EndOffset = Offset + Size;
3691 if (EndOffset > SL->getSizeInBytes())
3692 return nullptr;
3693
3694 unsigned Index = SL->getElementContainingOffset(Offset);
3695 Offset -= SL->getElementOffset(Index);
3696
3697 Type *ElementTy = STy->getElementType(Index);
3698 uint64_t ElementSize = DL.getTypeAllocSize(ElementTy).getFixedSize();
3699 if (Offset >= ElementSize)
3700 return nullptr; // The offset points into alignment padding.
3701
3702 // See if any partition must be contained by the element.
3703 if (Offset > 0 || Size < ElementSize) {
3704 if ((Offset + Size) > ElementSize)
3705 return nullptr;
3706 return getTypePartition(DL, ElementTy, Offset, Size);
3707 }
3708 assert(Offset == 0)((Offset == 0) ? static_cast<void> (0) : __assert_fail (
"Offset == 0", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3708, __PRETTY_FUNCTION__))
;
3709
3710 if (Size == ElementSize)
3711 return stripAggregateTypeWrapping(DL, ElementTy);
3712
3713 StructType::element_iterator EI = STy->element_begin() + Index,
3714 EE = STy->element_end();
3715 if (EndOffset < SL->getSizeInBytes()) {
3716 unsigned EndIndex = SL->getElementContainingOffset(EndOffset);
3717 if (Index == EndIndex)
3718 return nullptr; // Within a single element and its padding.
3719
3720 // Don't try to form "natural" types if the elements don't line up with the
3721 // expected size.
3722 // FIXME: We could potentially recurse down through the last element in the
3723 // sub-struct to find a natural end point.
3724 if (SL->getElementOffset(EndIndex) != EndOffset)
3725 return nullptr;
3726
3727 assert(Index < EndIndex)((Index < EndIndex) ? static_cast<void> (0) : __assert_fail
("Index < EndIndex", "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3727, __PRETTY_FUNCTION__))
;
3728 EE = STy->element_begin() + EndIndex;
3729 }
3730
3731 // Try to build up a sub-structure.
3732 StructType *SubTy =
3733 StructType::get(STy->getContext(), makeArrayRef(EI, EE), STy->isPacked());
3734 const StructLayout *SubSL = DL.getStructLayout(SubTy);
3735 if (Size != SubSL->getSizeInBytes())
3736 return nullptr; // The sub-struct doesn't have quite the size needed.
3737
3738 return SubTy;
3739}
3740
3741/// Pre-split loads and stores to simplify rewriting.
3742///
3743/// We want to break up the splittable load+store pairs as much as
3744/// possible. This is important to do as a preprocessing step, as once we
3745/// start rewriting the accesses to partitions of the alloca we lose the
3746/// necessary information to correctly split apart paired loads and stores
3747/// which both point into this alloca. The case to consider is something like
3748/// the following:
3749///
3750/// %a = alloca [12 x i8]
3751/// %gep1 = getelementptr [12 x i8]* %a, i32 0, i32 0
3752/// %gep2 = getelementptr [12 x i8]* %a, i32 0, i32 4
3753/// %gep3 = getelementptr [12 x i8]* %a, i32 0, i32 8
3754/// %iptr1 = bitcast i8* %gep1 to i64*
3755/// %iptr2 = bitcast i8* %gep2 to i64*
3756/// %fptr1 = bitcast i8* %gep1 to float*
3757/// %fptr2 = bitcast i8* %gep2 to float*
3758/// %fptr3 = bitcast i8* %gep3 to float*
3759/// store float 0.0, float* %fptr1
3760/// store float 1.0, float* %fptr2
3761/// %v = load i64* %iptr1
3762/// store i64 %v, i64* %iptr2
3763/// %f1 = load float* %fptr2
3764/// %f2 = load float* %fptr3
3765///
3766/// Here we want to form 3 partitions of the alloca, each 4 bytes large, and
3767/// promote everything so we recover the 2 SSA values that should have been
3768/// there all along.
3769///
3770/// \returns true if any changes are made.
3771bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
3772 LLVM_DEBUG(dbgs() << "Pre-splitting loads and stores\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "Pre-splitting loads and stores\n"
; } } while (false)
;
3773
3774 // Track the loads and stores which are candidates for pre-splitting here, in
3775 // the order they first appear during the partition scan. These give stable
3776 // iteration order and a basis for tracking which loads and stores we
3777 // actually split.
3778 SmallVector<LoadInst *, 4> Loads;
3779 SmallVector<StoreInst *, 4> Stores;
3780
3781 // We need to accumulate the splits required of each load or store where we
3782 // can find them via a direct lookup. This is important to cross-check loads
3783 // and stores against each other. We also track the slice so that we can kill
3784 // all the slices that end up split.
3785 struct SplitOffsets {
3786 Slice *S;
3787 std::vector<uint64_t> Splits;
3788 };
3789 SmallDenseMap<Instruction *, SplitOffsets, 8> SplitOffsetsMap;
3790
3791 // Track loads out of this alloca which cannot, for any reason, be pre-split.
3792 // This is important as we also cannot pre-split stores of those loads!
3793 // FIXME: This is all pretty gross. It means that we can be more aggressive
3794 // in pre-splitting when the load feeding the store happens to come from
3795 // a separate alloca. Put another way, the effectiveness of SROA would be
3796 // decreased by a frontend which just concatenated all of its local allocas
3797 // into one big flat alloca. But defeating such patterns is exactly the job
3798 // SROA is tasked with! Sadly, to not have this discrepancy we would have
3799 // change store pre-splitting to actually force pre-splitting of the load
3800 // that feeds it *and all stores*. That makes pre-splitting much harder, but
3801 // maybe it would make it more principled?
3802 SmallPtrSet<LoadInst *, 8> UnsplittableLoads;
3803
3804 LLVM_DEBUG(dbgs() << " Searching for candidate loads and stores\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Searching for candidate loads and stores\n"
; } } while (false)
;
3805 for (auto &P : AS.partitions()) {
3806 for (Slice &S : P) {
3807 Instruction *I = cast<Instruction>(S.getUse()->getUser());
3808 if (!S.isSplittable() || S.endOffset() <= P.endOffset()) {
3809 // If this is a load we have to track that it can't participate in any
3810 // pre-splitting. If this is a store of a load we have to track that
3811 // that load also can't participate in any pre-splitting.
3812 if (auto *LI = dyn_cast<LoadInst>(I))
3813 UnsplittableLoads.insert(LI);
3814 else if (auto *SI = dyn_cast<StoreInst>(I))
3815 if (auto *LI = dyn_cast<LoadInst>(SI->getValueOperand()))
3816 UnsplittableLoads.insert(LI);
3817 continue;
3818 }
3819 assert(P.endOffset() > S.beginOffset() &&((P.endOffset() > S.beginOffset() && "Empty or backwards partition!"
) ? static_cast<void> (0) : __assert_fail ("P.endOffset() > S.beginOffset() && \"Empty or backwards partition!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3820, __PRETTY_FUNCTION__))
3820 "Empty or backwards partition!")((P.endOffset() > S.beginOffset() && "Empty or backwards partition!"
) ? static_cast<void> (0) : __assert_fail ("P.endOffset() > S.beginOffset() && \"Empty or backwards partition!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3820, __PRETTY_FUNCTION__))
;
3821
3822 // Determine if this is a pre-splittable slice.
3823 if (auto *LI = dyn_cast<LoadInst>(I)) {
3824 assert(!LI->isVolatile() && "Cannot split volatile loads!")((!LI->isVolatile() && "Cannot split volatile loads!"
) ? static_cast<void> (0) : __assert_fail ("!LI->isVolatile() && \"Cannot split volatile loads!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3824, __PRETTY_FUNCTION__))
;
3825
3826 // The load must be used exclusively to store into other pointers for
3827 // us to be able to arbitrarily pre-split it. The stores must also be
3828 // simple to avoid changing semantics.
3829 auto IsLoadSimplyStored = [](LoadInst *LI) {
3830 for (User *LU : LI->users()) {
3831 auto *SI = dyn_cast<StoreInst>(LU);
3832 if (!SI || !SI->isSimple())
3833 return false;
3834 }
3835 return true;
3836 };
3837 if (!IsLoadSimplyStored(LI)) {
3838 UnsplittableLoads.insert(LI);
3839 continue;
3840 }
3841
3842 Loads.push_back(LI);
3843 } else if (auto *SI = dyn_cast<StoreInst>(I)) {
3844 if (S.getUse() != &SI->getOperandUse(SI->getPointerOperandIndex()))
3845 // Skip stores *of* pointers. FIXME: This shouldn't even be possible!
3846 continue;
3847 auto *StoredLoad = dyn_cast<LoadInst>(SI->getValueOperand());
3848 if (!StoredLoad || !StoredLoad->isSimple())
3849 continue;
3850 assert(!SI->isVolatile() && "Cannot split volatile stores!")((!SI->isVolatile() && "Cannot split volatile stores!"
) ? static_cast<void> (0) : __assert_fail ("!SI->isVolatile() && \"Cannot split volatile stores!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3850, __PRETTY_FUNCTION__))
;
3851
3852 Stores.push_back(SI);
3853 } else {
3854 // Other uses cannot be pre-split.
3855 continue;
3856 }
3857
3858 // Record the initial split.
3859 LLVM_DEBUG(dbgs() << " Candidate: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Candidate: " << *I <<
"\n"; } } while (false)
;
3860 auto &Offsets = SplitOffsetsMap[I];
3861 assert(Offsets.Splits.empty() &&((Offsets.Splits.empty() && "Should not have splits the first time we see an instruction!"
) ? static_cast<void> (0) : __assert_fail ("Offsets.Splits.empty() && \"Should not have splits the first time we see an instruction!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3862, __PRETTY_FUNCTION__))
3862 "Should not have splits the first time we see an instruction!")((Offsets.Splits.empty() && "Should not have splits the first time we see an instruction!"
) ? static_cast<void> (0) : __assert_fail ("Offsets.Splits.empty() && \"Should not have splits the first time we see an instruction!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3862, __PRETTY_FUNCTION__))
;
3863 Offsets.S = &S;
3864 Offsets.Splits.push_back(P.endOffset() - S.beginOffset());
3865 }
3866
3867 // Now scan the already split slices, and add a split for any of them which
3868 // we're going to pre-split.
3869 for (Slice *S : P.splitSliceTails()) {
3870 auto SplitOffsetsMapI =
3871 SplitOffsetsMap.find(cast<Instruction>(S->getUse()->getUser()));
3872 if (SplitOffsetsMapI == SplitOffsetsMap.end())
3873 continue;
3874 auto &Offsets = SplitOffsetsMapI->second;
3875
3876 assert(Offsets.S == S && "Found a mismatched slice!")((Offsets.S == S && "Found a mismatched slice!") ? static_cast
<void> (0) : __assert_fail ("Offsets.S == S && \"Found a mismatched slice!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3876, __PRETTY_FUNCTION__))
;
3877 assert(!Offsets.Splits.empty() &&((!Offsets.Splits.empty() && "Cannot have an empty set of splits on the second partition!"
) ? static_cast<void> (0) : __assert_fail ("!Offsets.Splits.empty() && \"Cannot have an empty set of splits on the second partition!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3878, __PRETTY_FUNCTION__))
3878 "Cannot have an empty set of splits on the second partition!")((!Offsets.Splits.empty() && "Cannot have an empty set of splits on the second partition!"
) ? static_cast<void> (0) : __assert_fail ("!Offsets.Splits.empty() && \"Cannot have an empty set of splits on the second partition!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3878, __PRETTY_FUNCTION__))
;
3879 assert(Offsets.Splits.back() ==((Offsets.Splits.back() == P.beginOffset() - Offsets.S->beginOffset
() && "Previous split does not end where this one begins!"
) ? static_cast<void> (0) : __assert_fail ("Offsets.Splits.back() == P.beginOffset() - Offsets.S->beginOffset() && \"Previous split does not end where this one begins!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3881, __PRETTY_FUNCTION__))
3880 P.beginOffset() - Offsets.S->beginOffset() &&((Offsets.Splits.back() == P.beginOffset() - Offsets.S->beginOffset
() && "Previous split does not end where this one begins!"
) ? static_cast<void> (0) : __assert_fail ("Offsets.Splits.back() == P.beginOffset() - Offsets.S->beginOffset() && \"Previous split does not end where this one begins!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3881, __PRETTY_FUNCTION__))
3881 "Previous split does not end where this one begins!")((Offsets.Splits.back() == P.beginOffset() - Offsets.S->beginOffset
() && "Previous split does not end where this one begins!"
) ? static_cast<void> (0) : __assert_fail ("Offsets.Splits.back() == P.beginOffset() - Offsets.S->beginOffset() && \"Previous split does not end where this one begins!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3881, __PRETTY_FUNCTION__))
;
3882
3883 // Record each split. The last partition's end isn't needed as the size
3884 // of the slice dictates that.
3885 if (S->endOffset() > P.endOffset())
3886 Offsets.Splits.push_back(P.endOffset() - Offsets.S->beginOffset());
3887 }
3888 }
3889
3890 // We may have split loads where some of their stores are split stores. For
3891 // such loads and stores, we can only pre-split them if their splits exactly
3892 // match relative to their starting offset. We have to verify this prior to
3893 // any rewriting.
3894 llvm::erase_if(Stores, [&UnsplittableLoads, &SplitOffsetsMap](StoreInst *SI) {
3895 // Lookup the load we are storing in our map of split
3896 // offsets.
3897 auto *LI = cast<LoadInst>(SI->getValueOperand());
3898 // If it was completely unsplittable, then we're done,
3899 // and this store can't be pre-split.
3900 if (UnsplittableLoads.count(LI))
3901 return true;
3902
3903 auto LoadOffsetsI = SplitOffsetsMap.find(LI);
3904 if (LoadOffsetsI == SplitOffsetsMap.end())
3905 return false; // Unrelated loads are definitely safe.
3906 auto &LoadOffsets = LoadOffsetsI->second;
3907
3908 // Now lookup the store's offsets.
3909 auto &StoreOffsets = SplitOffsetsMap[SI];
3910
3911 // If the relative offsets of each split in the load and
3912 // store match exactly, then we can split them and we
3913 // don't need to remove them here.
3914 if (LoadOffsets.Splits == StoreOffsets.Splits)
3915 return false;
3916
3917 LLVM_DEBUG(dbgs() << " Mismatched splits for load and store:\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Mismatched splits for load and store:\n"
<< " " << *LI << "\n" << " "
<< *SI << "\n"; } } while (false)
3918 << " " << *LI << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Mismatched splits for load and store:\n"
<< " " << *LI << "\n" << " "
<< *SI << "\n"; } } while (false)
3919 << " " << *SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Mismatched splits for load and store:\n"
<< " " << *LI << "\n" << " "
<< *SI << "\n"; } } while (false)
;
3920
3921 // We've found a store and load that we need to split
3922 // with mismatched relative splits. Just give up on them
3923 // and remove both instructions from our list of
3924 // candidates.
3925 UnsplittableLoads.insert(LI);
3926 return true;
3927 });
3928 // Now we have to go *back* through all the stores, because a later store may
3929 // have caused an earlier store's load to become unsplittable and if it is
3930 // unsplittable for the later store, then we can't rely on it being split in
3931 // the earlier store either.
3932 llvm::erase_if(Stores, [&UnsplittableLoads](StoreInst *SI) {
3933 auto *LI = cast<LoadInst>(SI->getValueOperand());
3934 return UnsplittableLoads.count(LI);
3935 });
3936 // Once we've established all the loads that can't be split for some reason,
3937 // filter any that made it into our list out.
3938 llvm::erase_if(Loads, [&UnsplittableLoads](LoadInst *LI) {
3939 return UnsplittableLoads.count(LI);
3940 });
3941
3942 // If no loads or stores are left, there is no pre-splitting to be done for
3943 // this alloca.
3944 if (Loads.empty() && Stores.empty())
3945 return false;
3946
3947 // From here on, we can't fail and will be building new accesses, so rig up
3948 // an IR builder.
3949 IRBuilderTy IRB(&AI);
3950
3951 // Collect the new slices which we will merge into the alloca slices.
3952 SmallVector<Slice, 4> NewSlices;
3953
3954 // Track any allocas we end up splitting loads and stores for so we iterate
3955 // on them.
3956 SmallPtrSet<AllocaInst *, 4> ResplitPromotableAllocas;
3957
3958 // At this point, we have collected all of the loads and stores we can
3959 // pre-split, and the specific splits needed for them. We actually do the
3960 // splitting in a specific order in order to handle when one of the loads in
3961 // the value operand to one of the stores.
3962 //
3963 // First, we rewrite all of the split loads, and just accumulate each split
3964 // load in a parallel structure. We also build the slices for them and append
3965 // them to the alloca slices.
3966 SmallDenseMap<LoadInst *, std::vector<LoadInst *>, 1> SplitLoadsMap;
3967 std::vector<LoadInst *> SplitLoads;
3968 const DataLayout &DL = AI.getModule()->getDataLayout();
3969 for (LoadInst *LI : Loads) {
3970 SplitLoads.clear();
3971
3972 IntegerType *Ty = cast<IntegerType>(LI->getType());
3973 uint64_t LoadSize = Ty->getBitWidth() / 8;
3974 assert(LoadSize > 0 && "Cannot have a zero-sized integer load!")((LoadSize > 0 && "Cannot have a zero-sized integer load!"
) ? static_cast<void> (0) : __assert_fail ("LoadSize > 0 && \"Cannot have a zero-sized integer load!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3974, __PRETTY_FUNCTION__))
;
3975
3976 auto &Offsets = SplitOffsetsMap[LI];
3977 assert(LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset() &&((LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset
() && "Slice size should always match load size exactly!"
) ? static_cast<void> (0) : __assert_fail ("LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && \"Slice size should always match load size exactly!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3978, __PRETTY_FUNCTION__))
3978 "Slice size should always match load size exactly!")((LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset
() && "Slice size should always match load size exactly!"
) ? static_cast<void> (0) : __assert_fail ("LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && \"Slice size should always match load size exactly!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3978, __PRETTY_FUNCTION__))
;
3979 uint64_t BaseOffset = Offsets.S->beginOffset();
3980 assert(BaseOffset + LoadSize > BaseOffset &&((BaseOffset + LoadSize > BaseOffset && "Cannot represent alloca access size using 64-bit integers!"
) ? static_cast<void> (0) : __assert_fail ("BaseOffset + LoadSize > BaseOffset && \"Cannot represent alloca access size using 64-bit integers!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3981, __PRETTY_FUNCTION__))
3981 "Cannot represent alloca access size using 64-bit integers!")((BaseOffset + LoadSize > BaseOffset && "Cannot represent alloca access size using 64-bit integers!"
) ? static_cast<void> (0) : __assert_fail ("BaseOffset + LoadSize > BaseOffset && \"Cannot represent alloca access size using 64-bit integers!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 3981, __PRETTY_FUNCTION__))
;
3982
3983 Instruction *BasePtr = cast<Instruction>(LI->getPointerOperand());
3984 IRB.SetInsertPoint(LI);
3985
3986 LLVM_DEBUG(dbgs() << " Splitting load: " << *LI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Splitting load: " << *LI
<< "\n"; } } while (false)
;
3987
3988 uint64_t PartOffset = 0, PartSize = Offsets.Splits.front();
3989 int Idx = 0, Size = Offsets.Splits.size();
3990 for (;;) {
3991 auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8);
3992 auto AS = LI->getPointerAddressSpace();
3993 auto *PartPtrTy = PartTy->getPointerTo(AS);
3994 LoadInst *PLoad = IRB.CreateAlignedLoad(
3995 PartTy,
3996 getAdjustedPtr(IRB, DL, BasePtr,
3997 APInt(DL.getIndexSizeInBits(AS), PartOffset),
3998 PartPtrTy, BasePtr->getName() + "."),
3999 getAdjustedAlignment(LI, PartOffset),
4000 /*IsVolatile*/ false, LI->getName());
4001 PLoad->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access,
4002 LLVMContext::MD_access_group});
4003
4004 // Append this load onto the list of split loads so we can find it later
4005 // to rewrite the stores.
4006 SplitLoads.push_back(PLoad);
4007
4008 // Now build a new slice for the alloca.
4009 NewSlices.push_back(
4010 Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize,
4011 &PLoad->getOperandUse(PLoad->getPointerOperandIndex()),
4012 /*IsSplittable*/ false));
4013 LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " new slice [" << NewSlices
.back().beginOffset() << ", " << NewSlices.back()
.endOffset() << "): " << *PLoad << "\n"; } }
while (false)
4014 << ", " << NewSlices.back().endOffset()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " new slice [" << NewSlices
.back().beginOffset() << ", " << NewSlices.back()
.endOffset() << "): " << *PLoad << "\n"; } }
while (false)
4015 << "): " << *PLoad << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " new slice [" << NewSlices
.back().beginOffset() << ", " << NewSlices.back()
.endOffset() << "): " << *PLoad << "\n"; } }
while (false)
;
4016
4017 // See if we've handled all the splits.
4018 if (Idx >= Size)
4019 break;
4020
4021 // Setup the next partition.
4022 PartOffset = Offsets.Splits[Idx];
4023 ++Idx;
4024 PartSize = (Idx < Size ? Offsets.Splits[Idx] : LoadSize) - PartOffset;
4025 }
4026
4027 // Now that we have the split loads, do the slow walk over all uses of the
4028 // load and rewrite them as split stores, or save the split loads to use
4029 // below if the store is going to be split there anyways.
4030 bool DeferredStores = false;
4031 for (User *LU : LI->users()) {
4032 StoreInst *SI = cast<StoreInst>(LU);
4033 if (!Stores.empty() && SplitOffsetsMap.count(SI)) {
4034 DeferredStores = true;
4035 LLVM_DEBUG(dbgs() << " Deferred splitting of store: " << *SIdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Deferred splitting of store: "
<< *SI << "\n"; } } while (false)
4036 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Deferred splitting of store: "
<< *SI << "\n"; } } while (false)
;
4037 continue;
4038 }
4039
4040 Value *StoreBasePtr = SI->getPointerOperand();
4041 IRB.SetInsertPoint(SI);
4042
4043 LLVM_DEBUG(dbgs() << " Splitting store of load: " << *SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Splitting store of load: " <<
*SI << "\n"; } } while (false)
;
4044
4045 for (int Idx = 0, Size = SplitLoads.size(); Idx < Size; ++Idx) {
4046 LoadInst *PLoad = SplitLoads[Idx];
4047 uint64_t PartOffset = Idx == 0 ? 0 : Offsets.Splits[Idx - 1];
4048 auto *PartPtrTy =
4049 PLoad->getType()->getPointerTo(SI->getPointerAddressSpace());
4050
4051 auto AS = SI->getPointerAddressSpace();
4052 StoreInst *PStore = IRB.CreateAlignedStore(
4053 PLoad,
4054 getAdjustedPtr(IRB, DL, StoreBasePtr,
4055 APInt(DL.getIndexSizeInBits(AS), PartOffset),
4056 PartPtrTy, StoreBasePtr->getName() + "."),
4057 getAdjustedAlignment(SI, PartOffset),
4058 /*IsVolatile*/ false);
4059 PStore->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access,
4060 LLVMContext::MD_access_group});
4061 LLVM_DEBUG(dbgs() << " +" << PartOffset << ":" << *PStore << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " +" << PartOffset <<
":" << *PStore << "\n"; } } while (false)
;
4062 }
4063
4064 // We want to immediately iterate on any allocas impacted by splitting
4065 // this store, and we have to track any promotable alloca (indicated by
4066 // a direct store) as needing to be resplit because it is no longer
4067 // promotable.
4068 if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(StoreBasePtr)) {
4069 ResplitPromotableAllocas.insert(OtherAI);
4070 Worklist.insert(OtherAI);
4071 } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(
4072 StoreBasePtr->stripInBoundsOffsets())) {
4073 Worklist.insert(OtherAI);
4074 }
4075
4076 // Mark the original store as dead.
4077 DeadInsts.push_back(SI);
4078 }
4079
4080 // Save the split loads if there are deferred stores among the users.
4081 if (DeferredStores)
4082 SplitLoadsMap.insert(std::make_pair(LI, std::move(SplitLoads)));
4083
4084 // Mark the original load as dead and kill the original slice.
4085 DeadInsts.push_back(LI);
4086 Offsets.S->kill();
4087 }
4088
4089 // Second, we rewrite all of the split stores. At this point, we know that
4090 // all loads from this alloca have been split already. For stores of such
4091 // loads, we can simply look up the pre-existing split loads. For stores of
4092 // other loads, we split those loads first and then write split stores of
4093 // them.
4094 for (StoreInst *SI : Stores) {
4095 auto *LI = cast<LoadInst>(SI->getValueOperand());
4096 IntegerType *Ty = cast<IntegerType>(LI->getType());
4097 uint64_t StoreSize = Ty->getBitWidth() / 8;
4098 assert(StoreSize > 0 && "Cannot have a zero-sized integer store!")((StoreSize > 0 && "Cannot have a zero-sized integer store!"
) ? static_cast<void> (0) : __assert_fail ("StoreSize > 0 && \"Cannot have a zero-sized integer store!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4098, __PRETTY_FUNCTION__))
;
4099
4100 auto &Offsets = SplitOffsetsMap[SI];
4101 assert(StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() &&((StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset
() && "Slice size should always match load size exactly!"
) ? static_cast<void> (0) : __assert_fail ("StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && \"Slice size should always match load size exactly!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4102, __PRETTY_FUNCTION__))
4102 "Slice size should always match load size exactly!")((StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset
() && "Slice size should always match load size exactly!"
) ? static_cast<void> (0) : __assert_fail ("StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && \"Slice size should always match load size exactly!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4102, __PRETTY_FUNCTION__))
;
4103 uint64_t BaseOffset = Offsets.S->beginOffset();
4104 assert(BaseOffset + StoreSize > BaseOffset &&((BaseOffset + StoreSize > BaseOffset && "Cannot represent alloca access size using 64-bit integers!"
) ? static_cast<void> (0) : __assert_fail ("BaseOffset + StoreSize > BaseOffset && \"Cannot represent alloca access size using 64-bit integers!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4105, __PRETTY_FUNCTION__))
4105 "Cannot represent alloca access size using 64-bit integers!")((BaseOffset + StoreSize > BaseOffset && "Cannot represent alloca access size using 64-bit integers!"
) ? static_cast<void> (0) : __assert_fail ("BaseOffset + StoreSize > BaseOffset && \"Cannot represent alloca access size using 64-bit integers!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4105, __PRETTY_FUNCTION__))
;
4106
4107 Value *LoadBasePtr = LI->getPointerOperand();
4108 Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand());
4109
4110 LLVM_DEBUG(dbgs() << " Splitting store: " << *SI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Splitting store: " << *SI
<< "\n"; } } while (false)
;
4111
4112 // Check whether we have an already split load.
4113 auto SplitLoadsMapI = SplitLoadsMap.find(LI);
4114 std::vector<LoadInst *> *SplitLoads = nullptr;
4115 if (SplitLoadsMapI != SplitLoadsMap.end()) {
4116 SplitLoads = &SplitLoadsMapI->second;
4117 assert(SplitLoads->size() == Offsets.Splits.size() + 1 &&((SplitLoads->size() == Offsets.Splits.size() + 1 &&
"Too few split loads for the number of splits in the store!"
) ? static_cast<void> (0) : __assert_fail ("SplitLoads->size() == Offsets.Splits.size() + 1 && \"Too few split loads for the number of splits in the store!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4118, __PRETTY_FUNCTION__))
4118 "Too few split loads for the number of splits in the store!")((SplitLoads->size() == Offsets.Splits.size() + 1 &&
"Too few split loads for the number of splits in the store!"
) ? static_cast<void> (0) : __assert_fail ("SplitLoads->size() == Offsets.Splits.size() + 1 && \"Too few split loads for the number of splits in the store!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4118, __PRETTY_FUNCTION__))
;
4119 } else {
4120 LLVM_DEBUG(dbgs() << " of load: " << *LI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " of load: " << *LI
<< "\n"; } } while (false)
;
4121 }
4122
4123 uint64_t PartOffset = 0, PartSize = Offsets.Splits.front();
4124 int Idx = 0, Size = Offsets.Splits.size();
4125 for (;;) {
4126 auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8);
4127 auto *LoadPartPtrTy = PartTy->getPointerTo(LI->getPointerAddressSpace());
4128 auto *StorePartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace());
4129
4130 // Either lookup a split load or create one.
4131 LoadInst *PLoad;
4132 if (SplitLoads) {
4133 PLoad = (*SplitLoads)[Idx];
4134 } else {
4135 IRB.SetInsertPoint(LI);
4136 auto AS = LI->getPointerAddressSpace();
4137 PLoad = IRB.CreateAlignedLoad(
4138 PartTy,
4139 getAdjustedPtr(IRB, DL, LoadBasePtr,
4140 APInt(DL.getIndexSizeInBits(AS), PartOffset),
4141 LoadPartPtrTy, LoadBasePtr->getName() + "."),
4142 getAdjustedAlignment(LI, PartOffset),
4143 /*IsVolatile*/ false, LI->getName());
4144 }
4145
4146 // And store this partition.
4147 IRB.SetInsertPoint(SI);
4148 auto AS = SI->getPointerAddressSpace();
4149 StoreInst *PStore = IRB.CreateAlignedStore(
4150 PLoad,
4151 getAdjustedPtr(IRB, DL, StoreBasePtr,
4152 APInt(DL.getIndexSizeInBits(AS), PartOffset),
4153 StorePartPtrTy, StoreBasePtr->getName() + "."),
4154 getAdjustedAlignment(SI, PartOffset),
4155 /*IsVolatile*/ false);
4156
4157 // Now build a new slice for the alloca.
4158 NewSlices.push_back(
4159 Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize,
4160 &PStore->getOperandUse(PStore->getPointerOperandIndex()),
4161 /*IsSplittable*/ false));
4162 LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " new slice [" << NewSlices
.back().beginOffset() << ", " << NewSlices.back()
.endOffset() << "): " << *PStore << "\n"; }
} while (false)
4163 << ", " << NewSlices.back().endOffset()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " new slice [" << NewSlices
.back().beginOffset() << ", " << NewSlices.back()
.endOffset() << "): " << *PStore << "\n"; }
} while (false)
4164 << "): " << *PStore << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " new slice [" << NewSlices
.back().beginOffset() << ", " << NewSlices.back()
.endOffset() << "): " << *PStore << "\n"; }
} while (false)
;
4165 if (!SplitLoads) {
4166 LLVM_DEBUG(dbgs() << " of split load: " << *PLoad << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " of split load: " << *
PLoad << "\n"; } } while (false)
;
4167 }
4168
4169 // See if we've finished all the splits.
4170 if (Idx >= Size)
4171 break;
4172
4173 // Setup the next partition.
4174 PartOffset = Offsets.Splits[Idx];
4175 ++Idx;
4176 PartSize = (Idx < Size ? Offsets.Splits[Idx] : StoreSize) - PartOffset;
4177 }
4178
4179 // We want to immediately iterate on any allocas impacted by splitting
4180 // this load, which is only relevant if it isn't a load of this alloca and
4181 // thus we didn't already split the loads above. We also have to keep track
4182 // of any promotable allocas we split loads on as they can no longer be
4183 // promoted.
4184 if (!SplitLoads) {
4185 if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(LoadBasePtr)) {
4186 assert(OtherAI != &AI && "We can't re-split our own alloca!")((OtherAI != &AI && "We can't re-split our own alloca!"
) ? static_cast<void> (0) : __assert_fail ("OtherAI != &AI && \"We can't re-split our own alloca!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4186, __PRETTY_FUNCTION__))
;
4187 ResplitPromotableAllocas.insert(OtherAI);
4188 Worklist.insert(OtherAI);
4189 } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(
4190 LoadBasePtr->stripInBoundsOffsets())) {
4191 assert(OtherAI != &AI && "We can't re-split our own alloca!")((OtherAI != &AI && "We can't re-split our own alloca!"
) ? static_cast<void> (0) : __assert_fail ("OtherAI != &AI && \"We can't re-split our own alloca!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4191, __PRETTY_FUNCTION__))
;
4192 Worklist.insert(OtherAI);
4193 }
4194 }
4195
4196 // Mark the original store as dead now that we've split it up and kill its
4197 // slice. Note that we leave the original load in place unless this store
4198 // was its only use. It may in turn be split up if it is an alloca load
4199 // for some other alloca, but it may be a normal load. This may introduce
4200 // redundant loads, but where those can be merged the rest of the optimizer
4201 // should handle the merging, and this uncovers SSA splits which is more
4202 // important. In practice, the original loads will almost always be fully
4203 // split and removed eventually, and the splits will be merged by any
4204 // trivial CSE, including instcombine.
4205 if (LI->hasOneUse()) {
4206 assert(*LI->user_begin() == SI && "Single use isn't this store!")((*LI->user_begin() == SI && "Single use isn't this store!"
) ? static_cast<void> (0) : __assert_fail ("*LI->user_begin() == SI && \"Single use isn't this store!\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4206, __PRETTY_FUNCTION__))
;
4207 DeadInsts.push_back(LI);
4208 }
4209 DeadInsts.push_back(SI);
4210 Offsets.S->kill();
4211 }
4212
4213 // Remove the killed slices that have ben pre-split.
4214 llvm::erase_if(AS, [](const Slice &S) { return S.isDead(); });
4215
4216 // Insert our new slices. This will sort and merge them into the sorted
4217 // sequence.
4218 AS.insert(NewSlices);
4219
4220 LLVM_DEBUG(dbgs() << " Pre-split slices:\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Pre-split slices:\n"; } } while
(false)
;
4221#ifndef NDEBUG
4222 for (auto I = AS.begin(), E = AS.end(); I != E; ++I)
4223 LLVM_DEBUG(AS.print(dbgs(), I, " "))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { AS.print(dbgs(), I, " "); } } while (false)
;
4224#endif
4225
4226 // Finally, don't try to promote any allocas that new require re-splitting.
4227 // They have already been added to the worklist above.
4228 llvm::erase_if(PromotableAllocas, [&](AllocaInst *AI) {
4229 return ResplitPromotableAllocas.count(AI);
4230 });
4231
4232 return true;
4233}
4234
4235/// Rewrite an alloca partition's users.
4236///
4237/// This routine drives both of the rewriting goals of the SROA pass. It tries
4238/// to rewrite uses of an alloca partition to be conducive for SSA value
4239/// promotion. If the partition needs a new, more refined alloca, this will
4240/// build that new alloca, preserving as much type information as possible, and
4241/// rewrite the uses of the old alloca to point at the new one and have the
4242/// appropriate new offsets. It also evaluates how successful the rewrite was
4243/// at enabling promotion and if it was successful queues the alloca to be
4244/// promoted.
4245AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
4246 Partition &P) {
4247 // Try to compute a friendly type for this partition of the alloca. This
4248 // won't always succeed, in which case we fall back to a legal integer type
4249 // or an i8 array of an appropriate size.
4250 Type *SliceTy = nullptr;
4251 const DataLayout &DL = AI.getModule()->getDataLayout();
4252 std::pair<Type *, IntegerType *> CommonUseTy =
4253 findCommonType(P.begin(), P.end(), P.endOffset());
4254 // Do all uses operate on the same type?
4255 if (CommonUseTy.first)
4256 if (DL.getTypeAllocSize(CommonUseTy.first).getFixedSize() >= P.size())
4257 SliceTy = CommonUseTy.first;
4258 // If not, can we find an appropriate subtype in the original allocated type?
4259 if (!SliceTy)
4260 if (Type *TypePartitionTy = getTypePartition(DL, AI.getAllocatedType(),
4261 P.beginOffset(), P.size()))
4262 SliceTy = TypePartitionTy;
4263 // If still not, can we use the largest bitwidth integer type used?
4264 if (!SliceTy && CommonUseTy.second)
4265 if (DL.getTypeAllocSize(CommonUseTy.second).getFixedSize() >= P.size())
4266 SliceTy = CommonUseTy.second;
4267 if ((!SliceTy || (SliceTy->isArrayTy() &&
4268 SliceTy->getArrayElementType()->isIntegerTy())) &&
4269 DL.isLegalInteger(P.size() * 8))
4270 SliceTy = Type::getIntNTy(*C, P.size() * 8);
4271 if (!SliceTy)
4272 SliceTy = ArrayType::get(Type::getInt8Ty(*C), P.size());
4273 assert(DL.getTypeAllocSize(SliceTy).getFixedSize() >= P.size())((DL.getTypeAllocSize(SliceTy).getFixedSize() >= P.size())
? static_cast<void> (0) : __assert_fail ("DL.getTypeAllocSize(SliceTy).getFixedSize() >= P.size()"
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4273, __PRETTY_FUNCTION__))
;
4274
4275 bool IsIntegerPromotable = isIntegerWideningViable(P, SliceTy, DL);
4276
4277 VectorType *VecTy =
4278 IsIntegerPromotable ? nullptr : isVectorPromotionViable(P, DL);
4279 if (VecTy)
4280 SliceTy = VecTy;
4281
4282 // Check for the case where we're going to rewrite to a new alloca of the
4283 // exact same type as the original, and with the same access offsets. In that
4284 // case, re-use the existing alloca, but still run through the rewriter to
4285 // perform phi and select speculation.
4286 // P.beginOffset() can be non-zero even with the same type in a case with
4287 // out-of-bounds access (e.g. @PR35657 function in SROA/basictest.ll).
4288 AllocaInst *NewAI;
4289 if (SliceTy == AI.getAllocatedType() && P.beginOffset() == 0) {
4290 NewAI = &AI;
4291 // FIXME: We should be able to bail at this point with "nothing changed".
4292 // FIXME: We might want to defer PHI speculation until after here.
4293 // FIXME: return nullptr;
4294 } else {
4295 // Make sure the alignment is compatible with P.beginOffset().
4296 const Align Alignment = commonAlignment(AI.getAlign(), P.beginOffset());
4297 // If we will get at least this much alignment from the type alone, leave
4298 // the alloca's alignment unconstrained.
4299 const bool IsUnconstrained = Alignment <= DL.getABITypeAlign(SliceTy);
4300 NewAI = new AllocaInst(
4301 SliceTy, AI.getType()->getAddressSpace(), nullptr,
4302 IsUnconstrained ? DL.getPrefTypeAlign(SliceTy) : Alignment,
4303 AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()), &AI);
4304 // Copy the old AI debug location over to the new one.
4305 NewAI->setDebugLoc(AI.getDebugLoc());
4306 ++NumNewAllocas;
4307 }
4308
4309 LLVM_DEBUG(dbgs() << "Rewriting alloca partition "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "Rewriting alloca partition " <<
"[" << P.beginOffset() << "," << P.endOffset
() << ") to: " << *NewAI << "\n"; } } while
(false)
4310 << "[" << P.beginOffset() << "," << P.endOffset()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "Rewriting alloca partition " <<
"[" << P.beginOffset() << "," << P.endOffset
() << ") to: " << *NewAI << "\n"; } } while
(false)
4311 << ") to: " << *NewAI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "Rewriting alloca partition " <<
"[" << P.beginOffset() << "," << P.endOffset
() << ") to: " << *NewAI << "\n"; } } while
(false)
;
4312
4313 // Track the high watermark on the worklist as it is only relevant for
4314 // promoted allocas. We will reset it to this point if the alloca is not in
4315 // fact scheduled for promotion.
4316 unsigned PPWOldSize = PostPromotionWorklist.size();
4317 unsigned NumUses = 0;
4318 SmallSetVector<PHINode *, 8> PHIUsers;
4319 SmallSetVector<SelectInst *, 8> SelectUsers;
4320
4321 AllocaSliceRewriter Rewriter(DL, AS, *this, AI, *NewAI, P.beginOffset(),
4322 P.endOffset(), IsIntegerPromotable, VecTy,
4323 PHIUsers, SelectUsers);
4324 bool Promotable = true;
4325 for (Slice *S : P.splitSliceTails()) {
4326 Promotable &= Rewriter.visit(S);
4327 ++NumUses;
4328 }
4329 for (Slice &S : P) {
4330 Promotable &= Rewriter.visit(&S);
4331 ++NumUses;
4332 }
4333
4334 NumAllocaPartitionUses += NumUses;
4335 MaxUsesPerAllocaPartition.updateMax(NumUses);
4336
4337 // Now that we've processed all the slices in the new partition, check if any
4338 // PHIs or Selects would block promotion.
4339 for (PHINode *PHI : PHIUsers)
4340 if (!isSafePHIToSpeculate(*PHI)) {
4341 Promotable = false;
4342 PHIUsers.clear();
4343 SelectUsers.clear();
4344 break;
4345 }
4346
4347 for (SelectInst *Sel : SelectUsers)
4348 if (!isSafeSelectToSpeculate(*Sel)) {
4349 Promotable = false;
4350 PHIUsers.clear();
4351 SelectUsers.clear();
4352 break;
4353 }
4354
4355 if (Promotable) {
4356 for (Use *U : AS.getDeadUsesIfPromotable()) {
4357 auto *OldInst = dyn_cast<Instruction>(U->get());
4358 Value::dropDroppableUse(*U);
4359 if (OldInst)
4360 if (isInstructionTriviallyDead(OldInst))
4361 DeadInsts.push_back(OldInst);
4362 }
4363 if (PHIUsers.empty() && SelectUsers.empty()) {
4364 // Promote the alloca.
4365 PromotableAllocas.push_back(NewAI);
4366 } else {
4367 // If we have either PHIs or Selects to speculate, add them to those
4368 // worklists and re-queue the new alloca so that we promote in on the
4369 // next iteration.
4370 for (PHINode *PHIUser : PHIUsers)
4371 SpeculatablePHIs.insert(PHIUser);
4372 for (SelectInst *SelectUser : SelectUsers)
4373 SpeculatableSelects.insert(SelectUser);
4374 Worklist.insert(NewAI);
4375 }
4376 } else {
4377 // Drop any post-promotion work items if promotion didn't happen.
4378 while (PostPromotionWorklist.size() > PPWOldSize)
4379 PostPromotionWorklist.pop_back();
4380
4381 // We couldn't promote and we didn't create a new partition, nothing
4382 // happened.
4383 if (NewAI == &AI)
4384 return nullptr;
4385
4386 // If we can't promote the alloca, iterate on it to check for new
4387 // refinements exposed by splitting the current alloca. Don't iterate on an
4388 // alloca which didn't actually change and didn't get promoted.
4389 Worklist.insert(NewAI);
4390 }
4391
4392 return NewAI;
4393}
4394
4395/// Walks the slices of an alloca and form partitions based on them,
4396/// rewriting each of their uses.
4397bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
4398 if (AS.begin() == AS.end())
4399 return false;
4400
4401 unsigned NumPartitions = 0;
4402 bool Changed = false;
4403 const DataLayout &DL = AI.getModule()->getDataLayout();
4404
4405 // First try to pre-split loads and stores.
4406 Changed |= presplitLoadsAndStores(AI, AS);
4407
4408 // Now that we have identified any pre-splitting opportunities,
4409 // mark loads and stores unsplittable except for the following case.
4410 // We leave a slice splittable if all other slices are disjoint or fully
4411 // included in the slice, such as whole-alloca loads and stores.
4412 // If we fail to split these during pre-splitting, we want to force them
4413 // to be rewritten into a partition.
4414 bool IsSorted = true;
4415
4416 uint64_t AllocaSize =
4417 DL.getTypeAllocSize(AI.getAllocatedType()).getFixedSize();
4418 const uint64_t MaxBitVectorSize = 1024;
4419 if (AllocaSize <= MaxBitVectorSize) {
4420 // If a byte boundary is included in any load or store, a slice starting or
4421 // ending at the boundary is not splittable.
4422 SmallBitVector SplittableOffset(AllocaSize + 1, true);
4423 for (Slice &S : AS)
4424 for (unsigned O = S.beginOffset() + 1;
4425 O < S.endOffset() && O < AllocaSize; O++)
4426 SplittableOffset.reset(O);
4427
4428 for (Slice &S : AS) {
4429 if (!S.isSplittable())
4430 continue;
4431
4432 if ((S.beginOffset() > AllocaSize || SplittableOffset[S.beginOffset()]) &&
4433 (S.endOffset() > AllocaSize || SplittableOffset[S.endOffset()]))
4434 continue;
4435
4436 if (isa<LoadInst>(S.getUse()->getUser()) ||
4437 isa<StoreInst>(S.getUse()->getUser())) {
4438 S.makeUnsplittable();
4439 IsSorted = false;
4440 }
4441 }
4442 }
4443 else {
4444 // We only allow whole-alloca splittable loads and stores
4445 // for a large alloca to avoid creating too large BitVector.
4446 for (Slice &S : AS) {
4447 if (!S.isSplittable())
4448 continue;
4449
4450 if (S.beginOffset() == 0 && S.endOffset() >= AllocaSize)
4451 continue;
4452
4453 if (isa<LoadInst>(S.getUse()->getUser()) ||
4454 isa<StoreInst>(S.getUse()->getUser())) {
4455 S.makeUnsplittable();
4456 IsSorted = false;
4457 }
4458 }
4459 }
4460
4461 if (!IsSorted)
4462 llvm::sort(AS);
4463
4464 /// Describes the allocas introduced by rewritePartition in order to migrate
4465 /// the debug info.
4466 struct Fragment {
4467 AllocaInst *Alloca;
4468 uint64_t Offset;
4469 uint64_t Size;
4470 Fragment(AllocaInst *AI, uint64_t O, uint64_t S)
4471 : Alloca(AI), Offset(O), Size(S) {}
4472 };
4473 SmallVector<Fragment, 4> Fragments;
4474
4475 // Rewrite each partition.
4476 for (auto &P : AS.partitions()) {
4477 if (AllocaInst *NewAI = rewritePartition(AI, AS, P)) {
4478 Changed = true;
4479 if (NewAI != &AI) {
4480 uint64_t SizeOfByte = 8;
4481 uint64_t AllocaSize =
4482 DL.getTypeSizeInBits(NewAI->getAllocatedType()).getFixedSize();
4483 // Don't include any padding.
4484 uint64_t Size = std::min(AllocaSize, P.size() * SizeOfByte);
4485 Fragments.push_back(Fragment(NewAI, P.beginOffset() * SizeOfByte, Size));
4486 }
4487 }
4488 ++NumPartitions;
4489 }
4490
4491 NumAllocaPartitions += NumPartitions;
4492 MaxPartitionsPerAlloca.updateMax(NumPartitions);
4493
4494 // Migrate debug information from the old alloca to the new alloca(s)
4495 // and the individual partitions.
4496 TinyPtrVector<DbgVariableIntrinsic *> DbgDeclares = FindDbgAddrUses(&AI);
4497 for (DbgVariableIntrinsic *DbgDeclare : DbgDeclares) {
4498 auto *Expr = DbgDeclare->getExpression();
4499 DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
4500 uint64_t AllocaSize =
4501 DL.getTypeSizeInBits(AI.getAllocatedType()).getFixedSize();
4502 for (auto Fragment : Fragments) {
4503 // Create a fragment expression describing the new partition or reuse AI's
4504 // expression if there is only one partition.
4505 auto *FragmentExpr = Expr;
4506 if (Fragment.Size < AllocaSize || Expr->isFragment()) {
4507 // If this alloca is already a scalar replacement of a larger aggregate,
4508 // Fragment.Offset describes the offset inside the scalar.
4509 auto ExprFragment = Expr->getFragmentInfo();
4510 uint64_t Offset = ExprFragment ? ExprFragment->OffsetInBits : 0;
4511 uint64_t Start = Offset + Fragment.Offset;
4512 uint64_t Size = Fragment.Size;
4513 if (ExprFragment) {
4514 uint64_t AbsEnd =
4515 ExprFragment->OffsetInBits + ExprFragment->SizeInBits;
4516 if (Start >= AbsEnd)
4517 // No need to describe a SROAed padding.
4518 continue;
4519 Size = std::min(Size, AbsEnd - Start);
4520 }
4521 // The new, smaller fragment is stenciled out from the old fragment.
4522 if (auto OrigFragment = FragmentExpr->getFragmentInfo()) {
4523 assert(Start >= OrigFragment->OffsetInBits &&((Start >= OrigFragment->OffsetInBits && "new fragment is outside of original fragment"
) ? static_cast<void> (0) : __assert_fail ("Start >= OrigFragment->OffsetInBits && \"new fragment is outside of original fragment\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4524, __PRETTY_FUNCTION__))
4524 "new fragment is outside of original fragment")((Start >= OrigFragment->OffsetInBits && "new fragment is outside of original fragment"
) ? static_cast<void> (0) : __assert_fail ("Start >= OrigFragment->OffsetInBits && \"new fragment is outside of original fragment\""
, "/build/llvm-toolchain-snapshot-12~++20210124100612+2afaf072f5c1/llvm/lib/Transforms/Scalar/SROA.cpp"
, 4524, __PRETTY_FUNCTION__))
;
4525 Start -= OrigFragment->OffsetInBits;
4526 }
4527
4528 // The alloca may be larger than the variable.
4529 auto VarSize = DbgDeclare->getVariable()->getSizeInBits();
4530 if (VarSize) {
4531 if (Size > *VarSize)
4532 Size = *VarSize;
4533 if (Size == 0 || Start + Size > *VarSize)
4534 continue;
4535 }
4536
4537 // Avoid creating a fragment expression that covers the entire variable.
4538 if (!VarSize || *VarSize != Size) {
4539 if (auto E =
4540 DIExpression::createFragmentExpression(Expr, Start, Size))
4541 FragmentExpr = *E;
4542 else
4543 continue;
4544 }
4545 }
4546
4547 // Remove any existing intrinsics on the new alloca describing
4548 // the variable fragment.
4549 for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(Fragment.Alloca)) {
4550 auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS,
4551 const DbgVariableIntrinsic *RHS) {
4552 return LHS->getVariable() == RHS->getVariable() &&
4553 LHS->getDebugLoc()->getInlinedAt() ==
4554 RHS->getDebugLoc()->getInlinedAt();
4555 };
4556 if (SameVariableFragment(OldDII, DbgDeclare))
4557 OldDII->eraseFromParent();
4558 }
4559
4560 DIB.insertDeclare(Fragment.Alloca, DbgDeclare->getVariable(), FragmentExpr,
4561 DbgDeclare->getDebugLoc(), &AI);
4562 }
4563 }
4564 return Changed;
4565}
4566
4567/// Clobber a use with undef, deleting the used value if it becomes dead.
4568void SROA::clobberUse(Use &U) {
4569 Value *OldV = U;
4570 // Replace the use with an undef value.
4571 U = UndefValue::get(OldV->getType());
4572
4573 // Check for this making an instruction dead. We have to garbage collect
4574 // all the dead instructions to ensure the uses of any alloca end up being
4575 // minimal.
4576 if (Instruction *OldI = dyn_cast<Instruction>(OldV))
4577 if (isInstructionTriviallyDead(OldI)) {
4578 DeadInsts.push_back(OldI);
4579 }
4580}
4581
4582/// Analyze an alloca for SROA.
4583///
4584/// This analyzes the alloca to ensure we can reason about it, builds
4585/// the slices of the alloca, and then hands it off to be split and
4586/// rewritten as needed.
4587bool SROA::runOnAlloca(AllocaInst &AI) {
4588 LLVM_DEBUG(dbgs() << "SROA alloca: " << AI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "SROA alloca: " << AI <<
"\n"; } } while (false)
;
4589 ++NumAllocasAnalyzed;
4590
4591 // Special case dead allocas, as they're trivial.
4592 if (AI.use_empty()) {
4593 AI.eraseFromParent();
4594 return true;
4595 }
4596 const DataLayout &DL = AI.getModule()->getDataLayout();
4597
4598 // Skip alloca forms that this analysis can't handle.
4599 auto *AT = AI.getAllocatedType();
4600 if (AI.isArrayAllocation() || !AT->isSized() || isa<ScalableVectorType>(AT) ||
4601 DL.getTypeAllocSize(AT).getFixedSize() == 0)
4602 return false;
4603
4604 bool Changed = false;
4605
4606 // First, split any FCA loads and stores touching this alloca to promote
4607 // better splitting and promotion opportunities.
4608 AggLoadStoreRewriter AggRewriter(DL);
4609 Changed |= AggRewriter.rewrite(AI);
4610
4611 // Build the slices using a recursive instruction-visiting builder.
4612 AllocaSlices AS(DL, AI);
4613 LLVM_DEBUG(AS.print(dbgs()))do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { AS.print(dbgs()); } } while (false)
;
4614 if (AS.isEscaped())
4615 return Changed;
4616
4617 // Delete all the dead users of this alloca before splitting and rewriting it.
4618 for (Instruction *DeadUser : AS.getDeadUsers()) {
4619 // Free up everything used by this instruction.
4620 for (Use &DeadOp : DeadUser->operands())
4621 clobberUse(DeadOp);
4622
4623 // Now replace the uses of this instruction.
4624 DeadUser->replaceAllUsesWith(UndefValue::get(DeadUser->getType()));
4625
4626 // And mark it for deletion.
4627 DeadInsts.push_back(DeadUser);
4628 Changed = true;
4629 }
4630 for (Use *DeadOp : AS.getDeadOperands()) {
4631 clobberUse(*DeadOp);
4632 Changed = true;
4633 }
4634
4635 // No slices to split. Leave the dead alloca for a later pass to clean up.
4636 if (AS.begin() == AS.end())
4637 return Changed;
4638
4639 Changed |= splitAlloca(AI, AS);
4640
4641 LLVM_DEBUG(dbgs() << " Speculating PHIs\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Speculating PHIs\n"; } } while
(false)
;
4642 while (!SpeculatablePHIs.empty())
4643 speculatePHINodeLoads(*SpeculatablePHIs.pop_back_val());
4644
4645 LLVM_DEBUG(dbgs() << " Speculating Selects\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << " Speculating Selects\n"; } } while
(false)
;
4646 while (!SpeculatableSelects.empty())
4647 speculateSelectInstLoads(*SpeculatableSelects.pop_back_val());
4648
4649 return Changed;
4650}
4651
4652/// Delete the dead instructions accumulated in this run.
4653///
4654/// Recursively deletes the dead instructions we've accumulated. This is done
4655/// at the very end to maximize locality of the recursive delete and to
4656/// minimize the problems of invalidated instruction pointers as such pointers
4657/// are used heavily in the intermediate stages of the algorithm.
4658///
4659/// We also record the alloca instructions deleted here so that they aren't
4660/// subsequently handed to mem2reg to promote.
4661bool SROA::deleteDeadInstructions(
4662 SmallPtrSetImpl<AllocaInst *> &DeletedAllocas) {
4663 bool Changed = false;
4664 while (!DeadInsts.empty()) {
4665 Instruction *I = dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val());
4666 if (!I) continue;
4667 LLVM_DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "Deleting dead instruction: " <<
*I << "\n"; } } while (false)
;
4668
4669 // If the instruction is an alloca, find the possible dbg.declare connected
4670 // to it, and remove it too. We must do this before calling RAUW or we will
4671 // not be able to find it.
4672 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
4673 DeletedAllocas.insert(AI);
4674 for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(AI))
4675 OldDII->eraseFromParent();
4676 }
4677
4678 I->replaceAllUsesWith(UndefValue::get(I->getType()));
4679
4680 for (Use &Operand : I->operands())
4681 if (Instruction *U = dyn_cast<Instruction>(Operand)) {
4682 // Zero out the operand and see if it becomes trivially dead.
4683 Operand = nullptr;
4684 if (isInstructionTriviallyDead(U))
4685 DeadInsts.push_back(U);
4686 }
4687
4688 ++NumDeleted;
4689 I->eraseFromParent();
4690 Changed = true;
4691 }
4692 return Changed;
4693}
4694
4695/// Promote the allocas, using the best available technique.
4696///
4697/// This attempts to promote whatever allocas have been identified as viable in
4698/// the PromotableAllocas list. If that list is empty, there is nothing to do.
4699/// This function returns whether any promotion occurred.
4700bool SROA::promoteAllocas(Function &F) {
4701 if (PromotableAllocas.empty())
4702 return false;
4703
4704 NumPromoted += PromotableAllocas.size();
4705
4706 LLVM_DEBUG(dbgs() << "Promoting allocas with mem2reg...\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "Promoting allocas with mem2reg...\n"
; } } while (false)
;
4707 PromoteMemToReg(PromotableAllocas, *DT, AC);
4708 PromotableAllocas.clear();
4709 return true;
4710}
4711
4712PreservedAnalyses SROA::runImpl(Function &F, DominatorTree &RunDT,
4713 AssumptionCache &RunAC) {
4714 LLVM_DEBUG(dbgs() << "SROA function: " << F.getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("sroa")) { dbgs() << "SROA function: " << F.getName
() << "\n"; } } while (false)
;
4715 C = &F.getContext();
4716 DT = &RunDT;
4717 AC = &RunAC;
4718
4719 BasicBlock &EntryBB = F.getEntryBlock();
4720 for (BasicBlock::iterator I = EntryBB.begin(), E = std::prev(EntryBB.end());
4721 I != E; ++I) {
4722 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
4723 if (isa<ScalableVectorType>(AI->getAllocatedType())) {
4724 if (isAllocaPromotable(AI))
4725 PromotableAllocas.push_back(AI);
4726 } else {
4727 Worklist.insert(AI);
4728 }
4729 }
4730 }
4731
4732 bool Changed = false;
4733 // A set of deleted alloca instruction pointers which should be removed from
4734 // the list of promotable allocas.
4735 SmallPtrSet<AllocaInst *, 4> DeletedAllocas;
4736
4737 do {
4738 while (!Worklist.empty()) {
4739 Changed |= runOnAlloca(*Worklist.pop_back_val());
4740 Changed |= deleteDeadInstructions(DeletedAllocas);
4741
4742 // Remove the deleted allocas from various lists so that we don't try to
4743 // continue processing them.
4744 if (!DeletedAllocas.empty()) {
4745 auto IsInSet = [&](AllocaInst *AI) { return DeletedAllocas.count(AI); };
4746 Worklist.remove_if(IsInSet);
4747 PostPromotionWorklist.remove_if(IsInSet);
4748 llvm::erase_if(PromotableAllocas, IsInSet);
4749 DeletedAllocas.clear();
4750 }
4751 }
4752
4753 Changed |= promoteAllocas(F);
4754
4755 Worklist = PostPromotionWorklist;
4756 PostPromotionWorklist.clear();
4757 } while (!Worklist.empty());
4758
4759 if (!Changed)
4760 return PreservedAnalyses::all();
4761
4762 PreservedAnalyses PA;
4763 PA.preserveSet<CFGAnalyses>();
4764 PA.preserve<GlobalsAA>();
4765 return PA;
4766}
4767
4768PreservedAnalyses SROA::run(Function &F, FunctionAnalysisManager &AM) {
4769 return runImpl(F, AM.getResult<DominatorTreeAnalysis>(F),
4770 AM.getResult<AssumptionAnalysis>(F));
4771}
4772
4773/// A legacy pass for the legacy pass manager that wraps the \c SROA pass.
4774///
4775/// This is in the llvm namespace purely to allow it to be a friend of the \c
4776/// SROA pass.
4777class llvm::sroa::SROALegacyPass : public FunctionPass {
4778 /// The SROA implementation.
4779 SROA Impl;
4780
4781public:
4782 static char ID;
4783
4784 SROALegacyPass() : FunctionPass(ID) {
4785 initializeSROALegacyPassPass(*PassRegistry::getPassRegistry());
4786 }
4787
4788 bool runOnFunction(Function &F) override {
4789 if (skipFunction(F))
4790 return false;
4791
4792 auto PA = Impl.runImpl(
4793 F, getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
4794 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F));
4795 return !PA.areAllPreserved();
4796 }
4797
4798 void getAnalysisUsage(AnalysisUsage &AU) const override {
4799 AU.addRequired<AssumptionCacheTracker>();
4800 AU.addRequired<DominatorTreeWrapperPass>();
4801 AU.addPreserved<GlobalsAAWrapperPass>();
4802 AU.setPreservesCFG();
4803 }
4804
4805 StringRef getPassName() const override { return "SROA"; }
4806};
4807
4808char SROALegacyPass::ID = 0;
4809
4810FunctionPass *llvm::createSROAPass() { return new SROALegacyPass(); }
4811
4812INITIALIZE_PASS_BEGIN(SROALegacyPass, "sroa",static void *initializeSROALegacyPassPassOnce(PassRegistry &
Registry) {
4813 "Scalar Replacement Of Aggregates", false, false)static void *initializeSROALegacyPassPassOnce(PassRegistry &
Registry) {
4814INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)initializeAssumptionCacheTrackerPass(Registry);
4815INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)initializeDominatorTreeWrapperPassPass(Registry);
4816INITIALIZE_PASS_END(SROALegacyPass, "sroa", "Scalar Replacement Of Aggregates",PassInfo *PI = new PassInfo( "Scalar Replacement Of Aggregates"
, "sroa", &SROALegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<SROALegacyPass>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeSROALegacyPassPassFlag
; void llvm::initializeSROALegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeSROALegacyPassPassFlag, initializeSROALegacyPassPassOnce
, std::ref(Registry)); }
4817 false, false)PassInfo *PI = new PassInfo( "Scalar Replacement Of Aggregates"
, "sroa", &SROALegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor
<SROALegacyPass>), false, false); Registry.registerPass
(*PI, true); return PI; } static llvm::once_flag InitializeSROALegacyPassPassFlag
; void llvm::initializeSROALegacyPassPass(PassRegistry &Registry
) { llvm::call_once(InitializeSROALegacyPassPassFlag, initializeSROALegacyPassPassOnce
, std::ref(Registry)); }