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