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