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