1 //===- SROA.cpp - Scalar Replacement Of Aggregates ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This transformation implements the well known scalar replacement of
11 /// aggregates transformation. It tries to identify promotable elements of an
12 /// aggregate alloca, and promote them to registers. It will also try to
13 /// convert uses of an element (or set of elements) of an alloca into a vector
14 /// or bitfield-style integer scalar if appropriate.
16 /// It works to do this with minimal slicing of the alloca so that regions
17 /// which are merely transferred in and out of external memory remain unchanged
18 /// and are not decomposed to scalar code.
20 /// Because this also performs alloca promotion, it can be thought of as also
21 /// serving the purpose of SSA formation. The algorithm iterates on the
22 /// function until all opportunities for promotion have been realized.
24 //===----------------------------------------------------------------------===//
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Analysis/AssumptionTracker.h"
32 #include "llvm/Analysis/Loads.h"
33 #include "llvm/Analysis/PtrUseVisitor.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/IR/Constants.h"
36 #include "llvm/IR/DIBuilder.h"
37 #include "llvm/IR/DataLayout.h"
38 #include "llvm/IR/DebugInfo.h"
39 #include "llvm/IR/DerivedTypes.h"
40 #include "llvm/IR/Dominators.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/IRBuilder.h"
43 #include "llvm/IR/InstVisitor.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/IntrinsicInst.h"
46 #include "llvm/IR/LLVMContext.h"
47 #include "llvm/IR/Operator.h"
48 #include "llvm/Pass.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/Compiler.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/MathExtras.h"
54 #include "llvm/Support/TimeValue.h"
55 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Transforms/Utils/Local.h"
57 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
58 #include "llvm/Transforms/Utils/SSAUpdater.h"
60 #if __cplusplus >= 201103L && !defined(NDEBUG)
61 // We only use this for a debug check in C++11
67 #define DEBUG_TYPE "sroa"
69 STATISTIC(NumAllocasAnalyzed, "Number of allocas analyzed for replacement");
70 STATISTIC(NumAllocaPartitions, "Number of alloca partitions formed");
71 STATISTIC(MaxPartitionsPerAlloca, "Maximum number of partitions per alloca");
72 STATISTIC(NumAllocaPartitionUses, "Number of alloca partition uses rewritten");
73 STATISTIC(MaxUsesPerAllocaPartition, "Maximum number of uses of a partition");
74 STATISTIC(NumNewAllocas, "Number of new, smaller allocas introduced");
75 STATISTIC(NumPromoted, "Number of allocas promoted to SSA values");
76 STATISTIC(NumLoadsSpeculated, "Number of loads speculated to allow promotion");
77 STATISTIC(NumDeleted, "Number of instructions deleted");
78 STATISTIC(NumVectorized, "Number of vectorized aggregates");
80 /// Hidden option to force the pass to not use DomTree and mem2reg, instead
81 /// forming SSA values through the SSAUpdater infrastructure.
82 static cl::opt<bool> ForceSSAUpdater("force-ssa-updater", cl::init(false),
85 /// Hidden option to enable randomly shuffling the slices to help uncover
86 /// instability in their order.
87 static cl::opt<bool> SROARandomShuffleSlices("sroa-random-shuffle-slices",
88 cl::init(false), cl::Hidden);
90 /// Hidden option to experiment with completely strict handling of inbounds
92 static cl::opt<bool> SROAStrictInbounds("sroa-strict-inbounds", cl::init(false),
96 /// \brief A custom IRBuilder inserter which prefixes all names if they are
98 template <bool preserveNames = true>
99 class IRBuilderPrefixedInserter
100 : public IRBuilderDefaultInserter<preserveNames> {
104 void SetNamePrefix(const Twine &P) { Prefix = P.str(); }
107 void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
108 BasicBlock::iterator InsertPt) const {
109 IRBuilderDefaultInserter<preserveNames>::InsertHelper(
110 I, Name.isTriviallyEmpty() ? Name : Prefix + Name, BB, InsertPt);
114 // Specialization for not preserving the name is trivial.
116 class IRBuilderPrefixedInserter<false>
117 : public IRBuilderDefaultInserter<false> {
119 void SetNamePrefix(const Twine &P) {}
122 /// \brief Provide a typedef for IRBuilder that drops names in release builds.
124 typedef llvm::IRBuilder<true, ConstantFolder, IRBuilderPrefixedInserter<true>>
127 typedef llvm::IRBuilder<false, ConstantFolder, IRBuilderPrefixedInserter<false>>
133 /// \brief A used slice of an alloca.
135 /// This structure represents a slice of an alloca used by some instruction. It
136 /// stores both the begin and end offsets of this use, a pointer to the use
137 /// itself, and a flag indicating whether we can classify the use as splittable
138 /// or not when forming partitions of the alloca.
140 /// \brief The beginning offset of the range.
141 uint64_t BeginOffset;
143 /// \brief The ending offset, not included in the range.
146 /// \brief Storage for both the use of this slice and whether it can be
148 PointerIntPair<Use *, 1, bool> UseAndIsSplittable;
151 Slice() : BeginOffset(), EndOffset() {}
152 Slice(uint64_t BeginOffset, uint64_t EndOffset, Use *U, bool IsSplittable)
153 : BeginOffset(BeginOffset), EndOffset(EndOffset),
154 UseAndIsSplittable(U, IsSplittable) {}
156 uint64_t beginOffset() const { return BeginOffset; }
157 uint64_t endOffset() const { return EndOffset; }
159 bool isSplittable() const { return UseAndIsSplittable.getInt(); }
160 void makeUnsplittable() { UseAndIsSplittable.setInt(false); }
162 Use *getUse() const { return UseAndIsSplittable.getPointer(); }
164 bool isDead() const { return getUse() == nullptr; }
165 void kill() { UseAndIsSplittable.setPointer(nullptr); }
167 /// \brief Support for ordering ranges.
169 /// This provides an ordering over ranges such that start offsets are
170 /// always increasing, and within equal start offsets, the end offsets are
171 /// decreasing. Thus the spanning range comes first in a cluster with the
172 /// same start position.
173 bool operator<(const Slice &RHS) const {
174 if (beginOffset() < RHS.beginOffset())
176 if (beginOffset() > RHS.beginOffset())
178 if (isSplittable() != RHS.isSplittable())
179 return !isSplittable();
180 if (endOffset() > RHS.endOffset())
185 /// \brief Support comparison with a single offset to allow binary searches.
186 friend LLVM_ATTRIBUTE_UNUSED bool operator<(const Slice &LHS,
187 uint64_t RHSOffset) {
188 return LHS.beginOffset() < RHSOffset;
190 friend LLVM_ATTRIBUTE_UNUSED bool operator<(uint64_t LHSOffset,
192 return LHSOffset < RHS.beginOffset();
195 bool operator==(const Slice &RHS) const {
196 return isSplittable() == RHS.isSplittable() &&
197 beginOffset() == RHS.beginOffset() && endOffset() == RHS.endOffset();
199 bool operator!=(const Slice &RHS) const { return !operator==(RHS); }
201 } // end anonymous namespace
204 template <typename T> struct isPodLike;
205 template <> struct isPodLike<Slice> { static const bool value = true; };
209 /// \brief Representation of the alloca slices.
211 /// This class represents the slices of an alloca which are formed by its
212 /// various uses. If a pointer escapes, we can't fully build a representation
213 /// for the slices used and we reflect that in this structure. The uses are
214 /// stored, sorted by increasing beginning offset and with unsplittable slices
215 /// starting at a particular offset before splittable slices.
218 /// \brief Construct the slices of a particular alloca.
219 AllocaSlices(const DataLayout &DL, AllocaInst &AI);
221 /// \brief Test whether a pointer to the allocation escapes our analysis.
223 /// If this is true, the slices are never fully built and should be
225 bool isEscaped() const { return PointerEscapingInstr; }
227 /// \brief Support for iterating over the slices.
229 typedef SmallVectorImpl<Slice>::iterator iterator;
230 typedef iterator_range<iterator> range;
231 iterator begin() { return Slices.begin(); }
232 iterator end() { return Slices.end(); }
234 typedef SmallVectorImpl<Slice>::const_iterator const_iterator;
235 typedef iterator_range<const_iterator> const_range;
236 const_iterator begin() const { return Slices.begin(); }
237 const_iterator end() const { return Slices.end(); }
240 /// \brief Erase a range of slices.
241 void erase(iterator Start, iterator Stop) { Slices.erase(Start, Stop); }
243 /// \brief Insert new slices for this alloca.
245 /// This moves the slices into the alloca's slices collection, and re-sorts
246 /// everything so that the usual ordering properties of the alloca's slices
248 void insert(ArrayRef<Slice> NewSlices) {
249 int OldSize = Slices.size();
250 std::move(NewSlices.begin(), NewSlices.end(), std::back_inserter(Slices));
251 auto SliceI = Slices.begin() + OldSize;
252 std::sort(SliceI, Slices.end());
253 std::inplace_merge(Slices.begin(), SliceI, Slices.end());
256 // Forward declare an iterator to befriend it.
257 class partition_iterator;
259 /// \brief A partition of the slices.
261 /// An ephemeral representation for a range of slices which can be viewed as
262 /// a partition of the alloca. This range represents a span of the alloca's
263 /// memory which cannot be split, and provides access to all of the slices
264 /// overlapping some part of the partition.
266 /// Objects of this type are produced by traversing the alloca's slices, but
267 /// are only ephemeral and not persistent.
270 friend class AllocaSlices;
271 friend class AllocaSlices::partition_iterator;
273 /// \brief The begining and ending offsets of the alloca for this partition.
274 uint64_t BeginOffset, EndOffset;
276 /// \brief The start end end iterators of this partition.
279 /// \brief A collection of split slice tails overlapping the partition.
280 SmallVector<Slice *, 4> SplitTails;
282 /// \brief Raw constructor builds an empty partition starting and ending at
283 /// the given iterator.
284 Partition(iterator SI) : SI(SI), SJ(SI) {}
287 /// \brief The start offset of this partition.
289 /// All of the contained slices start at or after this offset.
290 uint64_t beginOffset() const { return BeginOffset; }
292 /// \brief The end offset of this partition.
294 /// All of the contained slices end at or before this offset.
295 uint64_t endOffset() const { return EndOffset; }
297 /// \brief The size of the partition.
299 /// Note that this can never be zero.
300 uint64_t size() const {
301 assert(BeginOffset < EndOffset && "Partitions must span some bytes!");
302 return EndOffset - BeginOffset;
305 /// \brief Test whether this partition contains no slices, and merely spans
306 /// a region occupied by split slices.
307 bool empty() const { return SI == SJ; }
309 /// \name Iterate slices that start within the partition.
310 /// These may be splittable or unsplittable. They have a begin offset >= the
311 /// partition begin offset.
313 // FIXME: We should probably define a "concat_iterator" helper and use that
314 // to stitch together pointee_iterators over the split tails and the
315 // contiguous iterators of the partition. That would give a much nicer
316 // interface here. We could then additionally expose filtered iterators for
317 // split, unsplit, and unsplittable splices based on the usage patterns.
318 iterator begin() const { return SI; }
319 iterator end() const { return SJ; }
322 /// \brief Get the sequence of split slice tails.
324 /// These tails are of slices which start before this partition but are
325 /// split and overlap into the partition. We accumulate these while forming
327 ArrayRef<Slice *> splitSliceTails() const { return SplitTails; }
330 /// \brief An iterator over partitions of the alloca's slices.
332 /// This iterator implements the core algorithm for partitioning the alloca's
333 /// slices. It is a forward iterator as we don't support backtracking for
334 /// efficiency reasons, and re-use a single storage area to maintain the
335 /// current set of split slices.
337 /// It is templated on the slice iterator type to use so that it can operate
338 /// with either const or non-const slice iterators.
339 class partition_iterator
340 : public iterator_facade_base<partition_iterator,
341 std::forward_iterator_tag, Partition> {
342 friend class AllocaSlices;
344 /// \brief Most of the state for walking the partitions is held in a class
345 /// with a nice interface for examining them.
348 /// \brief We need to keep the end of the slices to know when to stop.
349 AllocaSlices::iterator SE;
351 /// \brief We also need to keep track of the maximum split end offset seen.
352 /// FIXME: Do we really?
353 uint64_t MaxSplitSliceEndOffset;
355 /// \brief Sets the partition to be empty at given iterator, and sets the
357 partition_iterator(AllocaSlices::iterator SI, AllocaSlices::iterator SE)
358 : P(SI), SE(SE), MaxSplitSliceEndOffset(0) {
359 // If not already at the end, advance our state to form the initial
365 /// \brief Advance the iterator to the next partition.
367 /// Requires that the iterator not be at the end of the slices.
369 assert((P.SI != SE || !P.SplitTails.empty()) &&
370 "Cannot advance past the end of the slices!");
372 // Clear out any split uses which have ended.
373 if (!P.SplitTails.empty()) {
374 if (P.EndOffset >= MaxSplitSliceEndOffset) {
375 // If we've finished all splits, this is easy.
376 P.SplitTails.clear();
377 MaxSplitSliceEndOffset = 0;
379 // Remove the uses which have ended in the prior partition. This
380 // cannot change the max split slice end because we just checked that
381 // the prior partition ended prior to that max.
384 P.SplitTails.begin(), P.SplitTails.end(),
385 [&](Slice *S) { return S->endOffset() <= P.EndOffset; }),
387 assert(std::any_of(P.SplitTails.begin(), P.SplitTails.end(),
389 return S->endOffset() == MaxSplitSliceEndOffset;
391 "Could not find the current max split slice offset!");
392 assert(std::all_of(P.SplitTails.begin(), P.SplitTails.end(),
394 return S->endOffset() <= MaxSplitSliceEndOffset;
396 "Max split slice end offset is not actually the max!");
400 // If P.SI is already at the end, then we've cleared the split tail and
401 // now have an end iterator.
403 assert(P.SplitTails.empty() && "Failed to clear the split slices!");
407 // If we had a non-empty partition previously, set up the state for
408 // subsequent partitions.
410 // Accumulate all the splittable slices which started in the old
411 // partition into the split list.
413 if (S.isSplittable() && S.endOffset() > P.EndOffset) {
414 P.SplitTails.push_back(&S);
415 MaxSplitSliceEndOffset =
416 std::max(S.endOffset(), MaxSplitSliceEndOffset);
419 // Start from the end of the previous partition.
422 // If P.SI is now at the end, we at most have a tail of split slices.
424 P.BeginOffset = P.EndOffset;
425 P.EndOffset = MaxSplitSliceEndOffset;
429 // If the we have split slices and the next slice is after a gap and is
430 // not splittable immediately form an empty partition for the split
431 // slices up until the next slice begins.
432 if (!P.SplitTails.empty() && P.SI->beginOffset() != P.EndOffset &&
433 !P.SI->isSplittable()) {
434 P.BeginOffset = P.EndOffset;
435 P.EndOffset = P.SI->beginOffset();
440 // OK, we need to consume new slices. Set the end offset based on the
441 // current slice, and step SJ past it. The beginning offset of the
442 // parttion is the beginning offset of the next slice unless we have
443 // pre-existing split slices that are continuing, in which case we begin
444 // at the prior end offset.
445 P.BeginOffset = P.SplitTails.empty() ? P.SI->beginOffset() : P.EndOffset;
446 P.EndOffset = P.SI->endOffset();
449 // There are two strategies to form a partition based on whether the
450 // partition starts with an unsplittable slice or a splittable slice.
451 if (!P.SI->isSplittable()) {
452 // When we're forming an unsplittable region, it must always start at
453 // the first slice and will extend through its end.
454 assert(P.BeginOffset == P.SI->beginOffset());
456 // Form a partition including all of the overlapping slices with this
457 // unsplittable slice.
458 while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) {
459 if (!P.SJ->isSplittable())
460 P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset());
464 // We have a partition across a set of overlapping unsplittable
469 // If we're starting with a splittable slice, then we need to form
470 // a synthetic partition spanning it and any other overlapping splittable
472 assert(P.SI->isSplittable() && "Forming a splittable partition!");
474 // Collect all of the overlapping splittable slices.
475 while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset &&
476 P.SJ->isSplittable()) {
477 P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset());
481 // Back upiP.EndOffset if we ended the span early when encountering an
482 // unsplittable slice. This synthesizes the early end offset of
483 // a partition spanning only splittable slices.
484 if (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) {
485 assert(!P.SJ->isSplittable());
486 P.EndOffset = P.SJ->beginOffset();
491 bool operator==(const partition_iterator &RHS) const {
492 assert(SE == RHS.SE &&
493 "End iterators don't match between compared partition iterators!");
495 // The observed positions of partitions is marked by the P.SI iterator and
496 // the emptyness of the split slices. The latter is only relevant when
497 // P.SI == SE, as the end iterator will additionally have an empty split
498 // slices list, but the prior may have the same P.SI and a tail of split
500 if (P.SI == RHS.P.SI &&
501 P.SplitTails.empty() == RHS.P.SplitTails.empty()) {
502 assert(P.SJ == RHS.P.SJ &&
503 "Same set of slices formed two different sized partitions!");
504 assert(P.SplitTails.size() == RHS.P.SplitTails.size() &&
505 "Same slice position with differently sized non-empty split "
512 partition_iterator &operator++() {
517 Partition &operator*() { return P; }
520 /// \brief A forward range over the partitions of the alloca's slices.
522 /// This accesses an iterator range over the partitions of the alloca's
523 /// slices. It computes these partitions on the fly based on the overlapping
524 /// offsets of the slices and the ability to split them. It will visit "empty"
525 /// partitions to cover regions of the alloca only accessed via split
527 iterator_range<partition_iterator> partitions() {
528 return make_range(partition_iterator(begin(), end()),
529 partition_iterator(end(), end()));
532 /// \brief Access the dead users for this alloca.
533 ArrayRef<Instruction *> getDeadUsers() const { return DeadUsers; }
535 /// \brief Access the dead operands referring to this alloca.
537 /// These are operands which have cannot actually be used to refer to the
538 /// alloca as they are outside its range and the user doesn't correct for
539 /// that. These mostly consist of PHI node inputs and the like which we just
540 /// need to replace with undef.
541 ArrayRef<Use *> getDeadOperands() const { return DeadOperands; }
543 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
544 void print(raw_ostream &OS, const_iterator I, StringRef Indent = " ") const;
545 void printSlice(raw_ostream &OS, const_iterator I,
546 StringRef Indent = " ") const;
547 void printUse(raw_ostream &OS, const_iterator I,
548 StringRef Indent = " ") const;
549 void print(raw_ostream &OS) const;
550 void dump(const_iterator I) const;
555 template <typename DerivedT, typename RetT = void> class BuilderBase;
557 friend class AllocaSlices::SliceBuilder;
559 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
560 /// \brief Handle to alloca instruction to simplify method interfaces.
564 /// \brief The instruction responsible for this alloca not having a known set
567 /// When an instruction (potentially) escapes the pointer to the alloca, we
568 /// store a pointer to that here and abort trying to form slices of the
569 /// alloca. This will be null if the alloca slices are analyzed successfully.
570 Instruction *PointerEscapingInstr;
572 /// \brief The slices of the alloca.
574 /// We store a vector of the slices formed by uses of the alloca here. This
575 /// vector is sorted by increasing begin offset, and then the unsplittable
576 /// slices before the splittable ones. See the Slice inner class for more
578 SmallVector<Slice, 8> Slices;
580 /// \brief Instructions which will become dead if we rewrite the alloca.
582 /// Note that these are not separated by slice. This is because we expect an
583 /// alloca to be completely rewritten or not rewritten at all. If rewritten,
584 /// all these instructions can simply be removed and replaced with undef as
585 /// they come from outside of the allocated space.
586 SmallVector<Instruction *, 8> DeadUsers;
588 /// \brief Operands which will become dead if we rewrite the alloca.
590 /// These are operands that in their particular use can be replaced with
591 /// undef when we rewrite the alloca. These show up in out-of-bounds inputs
592 /// to PHI nodes and the like. They aren't entirely dead (there might be
593 /// a GEP back into the bounds using it elsewhere) and nor is the PHI, but we
594 /// want to swap this particular input for undef to simplify the use lists of
596 SmallVector<Use *, 8> DeadOperands;
600 static Value *foldSelectInst(SelectInst &SI) {
601 // If the condition being selected on is a constant or the same value is
602 // being selected between, fold the select. Yes this does (rarely) happen
604 if (ConstantInt *CI = dyn_cast<ConstantInt>(SI.getCondition()))
605 return SI.getOperand(1 + CI->isZero());
606 if (SI.getOperand(1) == SI.getOperand(2))
607 return SI.getOperand(1);
612 /// \brief A helper that folds a PHI node or a select.
613 static Value *foldPHINodeOrSelectInst(Instruction &I) {
614 if (PHINode *PN = dyn_cast<PHINode>(&I)) {
615 // If PN merges together the same value, return that value.
616 return PN->hasConstantValue();
618 return foldSelectInst(cast<SelectInst>(I));
621 /// \brief Builder for the alloca slices.
623 /// This class builds a set of alloca slices by recursively visiting the uses
624 /// of an alloca and making a slice for each load and store at each offset.
625 class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
626 friend class PtrUseVisitor<SliceBuilder>;
627 friend class InstVisitor<SliceBuilder>;
628 typedef PtrUseVisitor<SliceBuilder> Base;
630 const uint64_t AllocSize;
633 SmallDenseMap<Instruction *, unsigned> MemTransferSliceMap;
634 SmallDenseMap<Instruction *, uint64_t> PHIOrSelectSizes;
636 /// \brief Set to de-duplicate dead instructions found in the use walk.
637 SmallPtrSet<Instruction *, 4> VisitedDeadInsts;
640 SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS)
641 : PtrUseVisitor<SliceBuilder>(DL),
642 AllocSize(DL.getTypeAllocSize(AI.getAllocatedType())), AS(AS) {}
645 void markAsDead(Instruction &I) {
646 if (VisitedDeadInsts.insert(&I).second)
647 AS.DeadUsers.push_back(&I);
650 void insertUse(Instruction &I, const APInt &Offset, uint64_t Size,
651 bool IsSplittable = false) {
652 // Completely skip uses which have a zero size or start either before or
653 // past the end of the allocation.
654 if (Size == 0 || Offset.uge(AllocSize)) {
655 DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte use @" << Offset
656 << " which has zero size or starts outside of the "
657 << AllocSize << " byte alloca:\n"
658 << " alloca: " << AS.AI << "\n"
659 << " use: " << I << "\n");
660 return markAsDead(I);
663 uint64_t BeginOffset = Offset.getZExtValue();
664 uint64_t EndOffset = BeginOffset + Size;
666 // Clamp the end offset to the end of the allocation. Note that this is
667 // formulated to handle even the case where "BeginOffset + Size" overflows.
668 // This may appear superficially to be something we could ignore entirely,
669 // but that is not so! There may be widened loads or PHI-node uses where
670 // some instructions are dead but not others. We can't completely ignore
671 // them, and so have to record at least the information here.
672 assert(AllocSize >= BeginOffset); // Established above.
673 if (Size > AllocSize - BeginOffset) {
674 DEBUG(dbgs() << "WARNING: Clamping a " << Size << " byte use @" << Offset
675 << " to remain within the " << AllocSize << " byte alloca:\n"
676 << " alloca: " << AS.AI << "\n"
677 << " use: " << I << "\n");
678 EndOffset = AllocSize;
681 AS.Slices.push_back(Slice(BeginOffset, EndOffset, U, IsSplittable));
684 void visitBitCastInst(BitCastInst &BC) {
686 return markAsDead(BC);
688 return Base::visitBitCastInst(BC);
691 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
692 if (GEPI.use_empty())
693 return markAsDead(GEPI);
695 if (SROAStrictInbounds && GEPI.isInBounds()) {
696 // FIXME: This is a manually un-factored variant of the basic code inside
697 // of GEPs with checking of the inbounds invariant specified in the
698 // langref in a very strict sense. If we ever want to enable
699 // SROAStrictInbounds, this code should be factored cleanly into
700 // PtrUseVisitor, but it is easier to experiment with SROAStrictInbounds
701 // by writing out the code here where we have tho underlying allocation
702 // size readily available.
703 APInt GEPOffset = Offset;
704 for (gep_type_iterator GTI = gep_type_begin(GEPI),
705 GTE = gep_type_end(GEPI);
707 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
711 // Handle a struct index, which adds its field offset to the pointer.
712 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
713 unsigned ElementIdx = OpC->getZExtValue();
714 const StructLayout *SL = DL.getStructLayout(STy);
716 APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx));
718 // For array or vector indices, scale the index by the size of the
720 APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth());
721 GEPOffset += Index * APInt(Offset.getBitWidth(),
722 DL.getTypeAllocSize(GTI.getIndexedType()));
725 // If this index has computed an intermediate pointer which is not
726 // inbounds, then the result of the GEP is a poison value and we can
727 // delete it and all uses.
728 if (GEPOffset.ugt(AllocSize))
729 return markAsDead(GEPI);
733 return Base::visitGetElementPtrInst(GEPI);
736 void handleLoadOrStore(Type *Ty, Instruction &I, const APInt &Offset,
737 uint64_t Size, bool IsVolatile) {
738 // We allow splitting of loads and stores where the type is an integer type
739 // and cover the entire alloca. This prevents us from splitting over
741 // FIXME: In the great blue eventually, we should eagerly split all integer
742 // loads and stores, and then have a separate step that merges adjacent
743 // alloca partitions into a single partition suitable for integer widening.
744 // Or we should skip the merge step and rely on GVN and other passes to
745 // merge adjacent loads and stores that survive mem2reg.
747 Ty->isIntegerTy() && !IsVolatile && Offset == 0 && Size >= AllocSize;
749 insertUse(I, Offset, Size, IsSplittable);
752 void visitLoadInst(LoadInst &LI) {
753 assert((!LI.isSimple() || LI.getType()->isSingleValueType()) &&
754 "All simple FCA loads should have been pre-split");
757 return PI.setAborted(&LI);
759 uint64_t Size = DL.getTypeStoreSize(LI.getType());
760 return handleLoadOrStore(LI.getType(), LI, Offset, Size, LI.isVolatile());
763 void visitStoreInst(StoreInst &SI) {
764 Value *ValOp = SI.getValueOperand();
766 return PI.setEscapedAndAborted(&SI);
768 return PI.setAborted(&SI);
770 uint64_t Size = DL.getTypeStoreSize(ValOp->getType());
772 // If this memory access can be shown to *statically* extend outside the
773 // bounds of of the allocation, it's behavior is undefined, so simply
774 // ignore it. Note that this is more strict than the generic clamping
775 // behavior of insertUse. We also try to handle cases which might run the
777 // FIXME: We should instead consider the pointer to have escaped if this
778 // function is being instrumented for addressing bugs or race conditions.
779 if (Size > AllocSize || Offset.ugt(AllocSize - Size)) {
780 DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte store @" << Offset
781 << " which extends past the end of the " << AllocSize
783 << " alloca: " << AS.AI << "\n"
784 << " use: " << SI << "\n");
785 return markAsDead(SI);
788 assert((!SI.isSimple() || ValOp->getType()->isSingleValueType()) &&
789 "All simple FCA stores should have been pre-split");
790 handleLoadOrStore(ValOp->getType(), SI, Offset, Size, SI.isVolatile());
793 void visitMemSetInst(MemSetInst &II) {
794 assert(II.getRawDest() == *U && "Pointer use is not the destination?");
795 ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength());
796 if ((Length && Length->getValue() == 0) ||
797 (IsOffsetKnown && Offset.uge(AllocSize)))
798 // Zero-length mem transfer intrinsics can be ignored entirely.
799 return markAsDead(II);
802 return PI.setAborted(&II);
804 insertUse(II, Offset, Length ? Length->getLimitedValue()
805 : AllocSize - Offset.getLimitedValue(),
809 void visitMemTransferInst(MemTransferInst &II) {
810 ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength());
811 if (Length && Length->getValue() == 0)
812 // Zero-length mem transfer intrinsics can be ignored entirely.
813 return markAsDead(II);
815 // Because we can visit these intrinsics twice, also check to see if the
816 // first time marked this instruction as dead. If so, skip it.
817 if (VisitedDeadInsts.count(&II))
821 return PI.setAborted(&II);
823 // This side of the transfer is completely out-of-bounds, and so we can
824 // nuke the entire transfer. However, we also need to nuke the other side
825 // if already added to our partitions.
826 // FIXME: Yet another place we really should bypass this when
827 // instrumenting for ASan.
828 if (Offset.uge(AllocSize)) {
829 SmallDenseMap<Instruction *, unsigned>::iterator MTPI =
830 MemTransferSliceMap.find(&II);
831 if (MTPI != MemTransferSliceMap.end())
832 AS.Slices[MTPI->second].kill();
833 return markAsDead(II);
836 uint64_t RawOffset = Offset.getLimitedValue();
837 uint64_t Size = Length ? Length->getLimitedValue() : AllocSize - RawOffset;
839 // Check for the special case where the same exact value is used for both
841 if (*U == II.getRawDest() && *U == II.getRawSource()) {
842 // For non-volatile transfers this is a no-op.
843 if (!II.isVolatile())
844 return markAsDead(II);
846 return insertUse(II, Offset, Size, /*IsSplittable=*/false);
849 // If we have seen both source and destination for a mem transfer, then
850 // they both point to the same alloca.
852 SmallDenseMap<Instruction *, unsigned>::iterator MTPI;
853 std::tie(MTPI, Inserted) =
854 MemTransferSliceMap.insert(std::make_pair(&II, AS.Slices.size()));
855 unsigned PrevIdx = MTPI->second;
857 Slice &PrevP = AS.Slices[PrevIdx];
859 // Check if the begin offsets match and this is a non-volatile transfer.
860 // In that case, we can completely elide the transfer.
861 if (!II.isVolatile() && PrevP.beginOffset() == RawOffset) {
863 return markAsDead(II);
866 // Otherwise we have an offset transfer within the same alloca. We can't
868 PrevP.makeUnsplittable();
871 // Insert the use now that we've fixed up the splittable nature.
872 insertUse(II, Offset, Size, /*IsSplittable=*/Inserted && Length);
874 // Check that we ended up with a valid index in the map.
875 assert(AS.Slices[PrevIdx].getUse()->getUser() == &II &&
876 "Map index doesn't point back to a slice with this user.");
879 // Disable SRoA for any intrinsics except for lifetime invariants.
880 // FIXME: What about debug intrinsics? This matches old behavior, but
881 // doesn't make sense.
882 void visitIntrinsicInst(IntrinsicInst &II) {
884 return PI.setAborted(&II);
886 if (II.getIntrinsicID() == Intrinsic::lifetime_start ||
887 II.getIntrinsicID() == Intrinsic::lifetime_end) {
888 ConstantInt *Length = cast<ConstantInt>(II.getArgOperand(0));
889 uint64_t Size = std::min(AllocSize - Offset.getLimitedValue(),
890 Length->getLimitedValue());
891 insertUse(II, Offset, Size, true);
895 Base::visitIntrinsicInst(II);
898 Instruction *hasUnsafePHIOrSelectUse(Instruction *Root, uint64_t &Size) {
899 // We consider any PHI or select that results in a direct load or store of
900 // the same offset to be a viable use for slicing purposes. These uses
901 // are considered unsplittable and the size is the maximum loaded or stored
903 SmallPtrSet<Instruction *, 4> Visited;
904 SmallVector<std::pair<Instruction *, Instruction *>, 4> Uses;
905 Visited.insert(Root);
906 Uses.push_back(std::make_pair(cast<Instruction>(*U), Root));
907 // If there are no loads or stores, the access is dead. We mark that as
908 // a size zero access.
911 Instruction *I, *UsedI;
912 std::tie(UsedI, I) = Uses.pop_back_val();
914 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
915 Size = std::max(Size, DL.getTypeStoreSize(LI->getType()));
918 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
919 Value *Op = SI->getOperand(0);
922 Size = std::max(Size, DL.getTypeStoreSize(Op->getType()));
926 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
927 if (!GEP->hasAllZeroIndices())
929 } else if (!isa<BitCastInst>(I) && !isa<PHINode>(I) &&
930 !isa<SelectInst>(I)) {
934 for (User *U : I->users())
935 if (Visited.insert(cast<Instruction>(U)).second)
936 Uses.push_back(std::make_pair(I, cast<Instruction>(U)));
937 } while (!Uses.empty());
942 void visitPHINodeOrSelectInst(Instruction &I) {
943 assert(isa<PHINode>(I) || isa<SelectInst>(I));
945 return markAsDead(I);
947 // TODO: We could use SimplifyInstruction here to fold PHINodes and
948 // SelectInsts. However, doing so requires to change the current
949 // dead-operand-tracking mechanism. For instance, suppose neither loading
950 // from %U nor %other traps. Then "load (select undef, %U, %other)" does not
951 // trap either. However, if we simply replace %U with undef using the
952 // current dead-operand-tracking mechanism, "load (select undef, undef,
953 // %other)" may trap because the select may return the first operand
955 if (Value *Result = foldPHINodeOrSelectInst(I)) {
957 // If the result of the constant fold will be the pointer, recurse
958 // through the PHI/select as if we had RAUW'ed it.
961 // Otherwise the operand to the PHI/select is dead, and we can replace
963 AS.DeadOperands.push_back(U);
969 return PI.setAborted(&I);
971 // See if we already have computed info on this node.
972 uint64_t &Size = PHIOrSelectSizes[&I];
974 // This is a new PHI/Select, check for an unsafe use of it.
975 if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&I, Size))
976 return PI.setAborted(UnsafeI);
979 // For PHI and select operands outside the alloca, we can't nuke the entire
980 // phi or select -- the other side might still be relevant, so we special
981 // case them here and use a separate structure to track the operands
982 // themselves which should be replaced with undef.
983 // FIXME: This should instead be escaped in the event we're instrumenting
984 // for address sanitization.
985 if (Offset.uge(AllocSize)) {
986 AS.DeadOperands.push_back(U);
990 insertUse(I, Offset, Size);
993 void visitPHINode(PHINode &PN) { visitPHINodeOrSelectInst(PN); }
995 void visitSelectInst(SelectInst &SI) { visitPHINodeOrSelectInst(SI); }
997 /// \brief Disable SROA entirely if there are unhandled users of the alloca.
998 void visitInstruction(Instruction &I) { PI.setAborted(&I); }
1001 AllocaSlices::AllocaSlices(const DataLayout &DL, AllocaInst &AI)
1003 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1006 PointerEscapingInstr(nullptr) {
1007 SliceBuilder PB(DL, AI, *this);
1008 SliceBuilder::PtrInfo PtrI = PB.visitPtr(AI);
1009 if (PtrI.isEscaped() || PtrI.isAborted()) {
1010 // FIXME: We should sink the escape vs. abort info into the caller nicely,
1011 // possibly by just storing the PtrInfo in the AllocaSlices.
1012 PointerEscapingInstr = PtrI.getEscapingInst() ? PtrI.getEscapingInst()
1013 : PtrI.getAbortingInst();
1014 assert(PointerEscapingInstr && "Did not track a bad instruction");
1018 Slices.erase(std::remove_if(Slices.begin(), Slices.end(),
1019 [](const Slice &S) {
1024 #if __cplusplus >= 201103L && !defined(NDEBUG)
1025 if (SROARandomShuffleSlices) {
1026 std::mt19937 MT(static_cast<unsigned>(sys::TimeValue::now().msec()));
1027 std::shuffle(Slices.begin(), Slices.end(), MT);
1031 // Sort the uses. This arranges for the offsets to be in ascending order,
1032 // and the sizes to be in descending order.
1033 std::sort(Slices.begin(), Slices.end());
1036 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1038 void AllocaSlices::print(raw_ostream &OS, const_iterator I,
1039 StringRef Indent) const {
1040 printSlice(OS, I, Indent);
1042 printUse(OS, I, Indent);
1045 void AllocaSlices::printSlice(raw_ostream &OS, const_iterator I,
1046 StringRef Indent) const {
1047 OS << Indent << "[" << I->beginOffset() << "," << I->endOffset() << ")"
1048 << " slice #" << (I - begin())
1049 << (I->isSplittable() ? " (splittable)" : "");
1052 void AllocaSlices::printUse(raw_ostream &OS, const_iterator I,
1053 StringRef Indent) const {
1054 OS << Indent << " used by: " << *I->getUse()->getUser() << "\n";
1057 void AllocaSlices::print(raw_ostream &OS) const {
1058 if (PointerEscapingInstr) {
1059 OS << "Can't analyze slices for alloca: " << AI << "\n"
1060 << " A pointer to this alloca escaped by:\n"
1061 << " " << *PointerEscapingInstr << "\n";
1065 OS << "Slices of alloca: " << AI << "\n";
1066 for (const_iterator I = begin(), E = end(); I != E; ++I)
1070 LLVM_DUMP_METHOD void AllocaSlices::dump(const_iterator I) const {
1073 LLVM_DUMP_METHOD void AllocaSlices::dump() const { print(dbgs()); }
1075 #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1078 /// \brief Implementation of LoadAndStorePromoter for promoting allocas.
1080 /// This subclass of LoadAndStorePromoter adds overrides to handle promoting
1081 /// the loads and stores of an alloca instruction, as well as updating its
1082 /// debug information. This is used when a domtree is unavailable and thus
1083 /// mem2reg in its full form can't be used to handle promotion of allocas to
1085 class AllocaPromoter : public LoadAndStorePromoter {
1089 SmallVector<DbgDeclareInst *, 4> DDIs;
1090 SmallVector<DbgValueInst *, 4> DVIs;
1093 AllocaPromoter(const SmallVectorImpl<Instruction *> &Insts, SSAUpdater &S,
1094 AllocaInst &AI, DIBuilder &DIB)
1095 : LoadAndStorePromoter(Insts, S), AI(AI), DIB(DIB) {}
1097 void run(const SmallVectorImpl<Instruction *> &Insts) {
1098 // Retain the debug information attached to the alloca for use when
1099 // rewriting loads and stores.
1100 if (auto *L = LocalAsMetadata::getIfExists(&AI)) {
1101 if (auto *DebugNode = MetadataAsValue::getIfExists(AI.getContext(), L)) {
1102 for (User *U : DebugNode->users())
1103 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
1104 DDIs.push_back(DDI);
1105 else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
1106 DVIs.push_back(DVI);
1110 LoadAndStorePromoter::run(Insts);
1112 // While we have the debug information, clear it off of the alloca. The
1113 // caller takes care of deleting the alloca.
1114 while (!DDIs.empty())
1115 DDIs.pop_back_val()->eraseFromParent();
1116 while (!DVIs.empty())
1117 DVIs.pop_back_val()->eraseFromParent();
1121 isInstInList(Instruction *I,
1122 const SmallVectorImpl<Instruction *> &Insts) const override {
1124 if (LoadInst *LI = dyn_cast<LoadInst>(I))
1125 Ptr = LI->getOperand(0);
1127 Ptr = cast<StoreInst>(I)->getPointerOperand();
1129 // Only used to detect cycles, which will be rare and quickly found as
1130 // we're walking up a chain of defs rather than down through uses.
1131 SmallPtrSet<Value *, 4> Visited;
1137 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Ptr))
1138 Ptr = BCI->getOperand(0);
1139 else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr))
1140 Ptr = GEPI->getPointerOperand();
1144 } while (Visited.insert(Ptr).second);
1149 void updateDebugInfo(Instruction *Inst) const override {
1150 for (DbgDeclareInst *DDI : DDIs)
1151 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
1152 ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
1153 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
1154 ConvertDebugDeclareToDebugValue(DDI, LI, DIB);
1155 for (DbgValueInst *DVI : DVIs) {
1156 Value *Arg = nullptr;
1157 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1158 // If an argument is zero extended then use argument directly. The ZExt
1159 // may be zapped by an optimization pass in future.
1160 if (ZExtInst *ZExt = dyn_cast<ZExtInst>(SI->getOperand(0)))
1161 Arg = dyn_cast<Argument>(ZExt->getOperand(0));
1162 else if (SExtInst *SExt = dyn_cast<SExtInst>(SI->getOperand(0)))
1163 Arg = dyn_cast<Argument>(SExt->getOperand(0));
1165 Arg = SI->getValueOperand();
1166 } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
1167 Arg = LI->getPointerOperand();
1171 Instruction *DbgVal =
1172 DIB.insertDbgValueIntrinsic(Arg, 0, DIVariable(DVI->getVariable()),
1173 DIExpression(DVI->getExpression()), Inst);
1174 DbgVal->setDebugLoc(DVI->getDebugLoc());
1178 } // end anon namespace
1181 /// \brief An optimization pass providing Scalar Replacement of Aggregates.
1183 /// This pass takes allocations which can be completely analyzed (that is, they
1184 /// don't escape) and tries to turn them into scalar SSA values. There are
1185 /// a few steps to this process.
1187 /// 1) It takes allocations of aggregates and analyzes the ways in which they
1188 /// are used to try to split them into smaller allocations, ideally of
1189 /// a single scalar data type. It will split up memcpy and memset accesses
1190 /// as necessary and try to isolate individual scalar accesses.
1191 /// 2) It will transform accesses into forms which are suitable for SSA value
1192 /// promotion. This can be replacing a memset with a scalar store of an
1193 /// integer value, or it can involve speculating operations on a PHI or
1194 /// select to be a PHI or select of the results.
1195 /// 3) Finally, this will try to detect a pattern of accesses which map cleanly
1196 /// onto insert and extract operations on a vector value, and convert them to
1197 /// this form. By doing so, it will enable promotion of vector aggregates to
1198 /// SSA vector values.
1199 class SROA : public FunctionPass {
1200 const bool RequiresDomTree;
1203 const DataLayout *DL;
1205 AssumptionTracker *AT;
1207 /// \brief Worklist of alloca instructions to simplify.
1209 /// Each alloca in the function is added to this. Each new alloca formed gets
1210 /// added to it as well to recursively simplify unless that alloca can be
1211 /// directly promoted. Finally, each time we rewrite a use of an alloca other
1212 /// the one being actively rewritten, we add it back onto the list if not
1213 /// already present to ensure it is re-visited.
1214 SetVector<AllocaInst *, SmallVector<AllocaInst *, 16>> Worklist;
1216 /// \brief A collection of instructions to delete.
1217 /// We try to batch deletions to simplify code and make things a bit more
1219 SetVector<Instruction *, SmallVector<Instruction *, 8>> DeadInsts;
1221 /// \brief Post-promotion worklist.
1223 /// Sometimes we discover an alloca which has a high probability of becoming
1224 /// viable for SROA after a round of promotion takes place. In those cases,
1225 /// the alloca is enqueued here for re-processing.
1227 /// Note that we have to be very careful to clear allocas out of this list in
1228 /// the event they are deleted.
1229 SetVector<AllocaInst *, SmallVector<AllocaInst *, 16>> PostPromotionWorklist;
1231 /// \brief A collection of alloca instructions we can directly promote.
1232 std::vector<AllocaInst *> PromotableAllocas;
1234 /// \brief A worklist of PHIs to speculate prior to promoting allocas.
1236 /// All of these PHIs have been checked for the safety of speculation and by
1237 /// being speculated will allow promoting allocas currently in the promotable
1239 SetVector<PHINode *, SmallVector<PHINode *, 2>> SpeculatablePHIs;
1241 /// \brief A worklist of select instructions to speculate prior to promoting
1244 /// All of these select instructions have been checked for the safety of
1245 /// speculation and by being speculated will allow promoting allocas
1246 /// currently in the promotable queue.
1247 SetVector<SelectInst *, SmallVector<SelectInst *, 2>> SpeculatableSelects;
1250 SROA(bool RequiresDomTree = true)
1251 : FunctionPass(ID), RequiresDomTree(RequiresDomTree), C(nullptr),
1252 DL(nullptr), DT(nullptr) {
1253 initializeSROAPass(*PassRegistry::getPassRegistry());
1255 bool runOnFunction(Function &F) override;
1256 void getAnalysisUsage(AnalysisUsage &AU) const override;
1258 const char *getPassName() const override { return "SROA"; }
1262 friend class PHIOrSelectSpeculator;
1263 friend class AllocaSliceRewriter;
1265 bool presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS);
1266 bool rewritePartition(AllocaInst &AI, AllocaSlices &AS,
1267 AllocaSlices::Partition &P);
1268 bool splitAlloca(AllocaInst &AI, AllocaSlices &AS);
1269 bool runOnAlloca(AllocaInst &AI);
1270 void clobberUse(Use &U);
1271 void deleteDeadInstructions(SmallPtrSetImpl<AllocaInst *> &DeletedAllocas);
1272 bool promoteAllocas(Function &F);
1278 FunctionPass *llvm::createSROAPass(bool RequiresDomTree) {
1279 return new SROA(RequiresDomTree);
1282 INITIALIZE_PASS_BEGIN(SROA, "sroa", "Scalar Replacement Of Aggregates", false,
1284 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
1285 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1286 INITIALIZE_PASS_END(SROA, "sroa", "Scalar Replacement Of Aggregates", false,
1289 /// Walk the range of a partitioning looking for a common type to cover this
1290 /// sequence of slices.
1291 static Type *findCommonType(AllocaSlices::const_iterator B,
1292 AllocaSlices::const_iterator E,
1293 uint64_t EndOffset) {
1295 bool TyIsCommon = true;
1296 IntegerType *ITy = nullptr;
1298 // Note that we need to look at *every* alloca slice's Use to ensure we
1299 // always get consistent results regardless of the order of slices.
1300 for (AllocaSlices::const_iterator I = B; I != E; ++I) {
1301 Use *U = I->getUse();
1302 if (isa<IntrinsicInst>(*U->getUser()))
1304 if (I->beginOffset() != B->beginOffset() || I->endOffset() != EndOffset)
1307 Type *UserTy = nullptr;
1308 if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
1309 UserTy = LI->getType();
1310 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
1311 UserTy = SI->getValueOperand()->getType();
1314 if (IntegerType *UserITy = dyn_cast_or_null<IntegerType>(UserTy)) {
1315 // If the type is larger than the partition, skip it. We only encounter
1316 // this for split integer operations where we want to use the type of the
1317 // entity causing the split. Also skip if the type is not a byte width
1319 if (UserITy->getBitWidth() % 8 != 0 ||
1320 UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset()))
1323 // Track the largest bitwidth integer type used in this way in case there
1324 // is no common type.
1325 if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth())
1329 // To avoid depending on the order of slices, Ty and TyIsCommon must not
1330 // depend on types skipped above.
1331 if (!UserTy || (Ty && Ty != UserTy))
1332 TyIsCommon = false; // Give up on anything but an iN type.
1337 return TyIsCommon ? Ty : ITy;
1340 /// PHI instructions that use an alloca and are subsequently loaded can be
1341 /// rewritten to load both input pointers in the pred blocks and then PHI the
1342 /// results, allowing the load of the alloca to be promoted.
1344 /// %P2 = phi [i32* %Alloca, i32* %Other]
1345 /// %V = load i32* %P2
1347 /// %V1 = load i32* %Alloca -> will be mem2reg'd
1349 /// %V2 = load i32* %Other
1351 /// %V = phi [i32 %V1, i32 %V2]
1353 /// We can do this to a select if its only uses are loads and if the operands
1354 /// to the select can be loaded unconditionally.
1356 /// FIXME: This should be hoisted into a generic utility, likely in
1357 /// Transforms/Util/Local.h
1358 static bool isSafePHIToSpeculate(PHINode &PN, const DataLayout *DL = nullptr) {
1359 // For now, we can only do this promotion if the load is in the same block
1360 // as the PHI, and if there are no stores between the phi and load.
1361 // TODO: Allow recursive phi users.
1362 // TODO: Allow stores.
1363 BasicBlock *BB = PN.getParent();
1364 unsigned MaxAlign = 0;
1365 bool HaveLoad = false;
1366 for (User *U : PN.users()) {
1367 LoadInst *LI = dyn_cast<LoadInst>(U);
1368 if (!LI || !LI->isSimple())
1371 // For now we only allow loads in the same block as the PHI. This is
1372 // a common case that happens when instcombine merges two loads through
1374 if (LI->getParent() != BB)
1377 // Ensure that there are no instructions between the PHI and the load that
1379 for (BasicBlock::iterator BBI = &PN; &*BBI != LI; ++BBI)
1380 if (BBI->mayWriteToMemory())
1383 MaxAlign = std::max(MaxAlign, LI->getAlignment());
1390 // We can only transform this if it is safe to push the loads into the
1391 // predecessor blocks. The only thing to watch out for is that we can't put
1392 // a possibly trapping load in the predecessor if it is a critical edge.
1393 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1394 TerminatorInst *TI = PN.getIncomingBlock(Idx)->getTerminator();
1395 Value *InVal = PN.getIncomingValue(Idx);
1397 // If the value is produced by the terminator of the predecessor (an
1398 // invoke) or it has side-effects, there is no valid place to put a load
1399 // in the predecessor.
1400 if (TI == InVal || TI->mayHaveSideEffects())
1403 // If the predecessor has a single successor, then the edge isn't
1405 if (TI->getNumSuccessors() == 1)
1408 // If this pointer is always safe to load, or if we can prove that there
1409 // is already a load in the block, then we can move the load to the pred
1411 if (InVal->isDereferenceablePointer(DL) ||
1412 isSafeToLoadUnconditionally(InVal, TI, MaxAlign, DL))
1421 static void speculatePHINodeLoads(PHINode &PN) {
1422 DEBUG(dbgs() << " original: " << PN << "\n");
1424 Type *LoadTy = cast<PointerType>(PN.getType())->getElementType();
1425 IRBuilderTy PHIBuilder(&PN);
1426 PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(),
1427 PN.getName() + ".sroa.speculated");
1429 // Get the AA tags and alignment to use from one of the loads. It doesn't
1430 // matter which one we get and if any differ.
1431 LoadInst *SomeLoad = cast<LoadInst>(PN.user_back());
1434 SomeLoad->getAAMetadata(AATags);
1435 unsigned Align = SomeLoad->getAlignment();
1437 // Rewrite all loads of the PN to use the new PHI.
1438 while (!PN.use_empty()) {
1439 LoadInst *LI = cast<LoadInst>(PN.user_back());
1440 LI->replaceAllUsesWith(NewPN);
1441 LI->eraseFromParent();
1444 // Inject loads into all of the pred blocks.
1445 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1446 BasicBlock *Pred = PN.getIncomingBlock(Idx);
1447 TerminatorInst *TI = Pred->getTerminator();
1448 Value *InVal = PN.getIncomingValue(Idx);
1449 IRBuilderTy PredBuilder(TI);
1451 LoadInst *Load = PredBuilder.CreateLoad(
1452 InVal, (PN.getName() + ".sroa.speculate.load." + Pred->getName()));
1453 ++NumLoadsSpeculated;
1454 Load->setAlignment(Align);
1456 Load->setAAMetadata(AATags);
1457 NewPN->addIncoming(Load, Pred);
1460 DEBUG(dbgs() << " speculated to: " << *NewPN << "\n");
1461 PN.eraseFromParent();
1464 /// Select instructions that use an alloca and are subsequently loaded can be
1465 /// rewritten to load both input pointers and then select between the result,
1466 /// allowing the load of the alloca to be promoted.
1468 /// %P2 = select i1 %cond, i32* %Alloca, i32* %Other
1469 /// %V = load i32* %P2
1471 /// %V1 = load i32* %Alloca -> will be mem2reg'd
1472 /// %V2 = load i32* %Other
1473 /// %V = select i1 %cond, i32 %V1, i32 %V2
1475 /// We can do this to a select if its only uses are loads and if the operand
1476 /// to the select can be loaded unconditionally.
1477 static bool isSafeSelectToSpeculate(SelectInst &SI,
1478 const DataLayout *DL = nullptr) {
1479 Value *TValue = SI.getTrueValue();
1480 Value *FValue = SI.getFalseValue();
1481 bool TDerefable = TValue->isDereferenceablePointer(DL);
1482 bool FDerefable = FValue->isDereferenceablePointer(DL);
1484 for (User *U : SI.users()) {
1485 LoadInst *LI = dyn_cast<LoadInst>(U);
1486 if (!LI || !LI->isSimple())
1489 // Both operands to the select need to be dereferencable, either
1490 // absolutely (e.g. allocas) or at this point because we can see other
1493 !isSafeToLoadUnconditionally(TValue, LI, LI->getAlignment(), DL))
1496 !isSafeToLoadUnconditionally(FValue, LI, LI->getAlignment(), DL))
1503 static void speculateSelectInstLoads(SelectInst &SI) {
1504 DEBUG(dbgs() << " original: " << SI << "\n");
1506 IRBuilderTy IRB(&SI);
1507 Value *TV = SI.getTrueValue();
1508 Value *FV = SI.getFalseValue();
1509 // Replace the loads of the select with a select of two loads.
1510 while (!SI.use_empty()) {
1511 LoadInst *LI = cast<LoadInst>(SI.user_back());
1512 assert(LI->isSimple() && "We only speculate simple loads");
1514 IRB.SetInsertPoint(LI);
1516 IRB.CreateLoad(TV, LI->getName() + ".sroa.speculate.load.true");
1518 IRB.CreateLoad(FV, LI->getName() + ".sroa.speculate.load.false");
1519 NumLoadsSpeculated += 2;
1521 // Transfer alignment and AA info if present.
1522 TL->setAlignment(LI->getAlignment());
1523 FL->setAlignment(LI->getAlignment());
1526 LI->getAAMetadata(Tags);
1528 TL->setAAMetadata(Tags);
1529 FL->setAAMetadata(Tags);
1532 Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL,
1533 LI->getName() + ".sroa.speculated");
1535 DEBUG(dbgs() << " speculated to: " << *V << "\n");
1536 LI->replaceAllUsesWith(V);
1537 LI->eraseFromParent();
1539 SI.eraseFromParent();
1542 /// \brief Build a GEP out of a base pointer and indices.
1544 /// This will return the BasePtr if that is valid, or build a new GEP
1545 /// instruction using the IRBuilder if GEP-ing is needed.
1546 static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr,
1547 SmallVectorImpl<Value *> &Indices, Twine NamePrefix) {
1548 if (Indices.empty())
1551 // A single zero index is a no-op, so check for this and avoid building a GEP
1553 if (Indices.size() == 1 && cast<ConstantInt>(Indices.back())->isZero())
1556 return IRB.CreateInBoundsGEP(BasePtr, Indices, NamePrefix + "sroa_idx");
1559 /// \brief Get a natural GEP off of the BasePtr walking through Ty toward
1560 /// TargetTy without changing the offset of the pointer.
1562 /// This routine assumes we've already established a properly offset GEP with
1563 /// Indices, and arrived at the Ty type. The goal is to continue to GEP with
1564 /// zero-indices down through type layers until we find one the same as
1565 /// TargetTy. If we can't find one with the same type, we at least try to use
1566 /// one with the same size. If none of that works, we just produce the GEP as
1567 /// indicated by Indices to have the correct offset.
1568 static Value *getNaturalGEPWithType(IRBuilderTy &IRB, const DataLayout &DL,
1569 Value *BasePtr, Type *Ty, Type *TargetTy,
1570 SmallVectorImpl<Value *> &Indices,
1573 return buildGEP(IRB, BasePtr, Indices, NamePrefix);
1575 // Pointer size to use for the indices.
1576 unsigned PtrSize = DL.getPointerTypeSizeInBits(BasePtr->getType());
1578 // See if we can descend into a struct and locate a field with the correct
1580 unsigned NumLayers = 0;
1581 Type *ElementTy = Ty;
1583 if (ElementTy->isPointerTy())
1586 if (ArrayType *ArrayTy = dyn_cast<ArrayType>(ElementTy)) {
1587 ElementTy = ArrayTy->getElementType();
1588 Indices.push_back(IRB.getIntN(PtrSize, 0));
1589 } else if (VectorType *VectorTy = dyn_cast<VectorType>(ElementTy)) {
1590 ElementTy = VectorTy->getElementType();
1591 Indices.push_back(IRB.getInt32(0));
1592 } else if (StructType *STy = dyn_cast<StructType>(ElementTy)) {
1593 if (STy->element_begin() == STy->element_end())
1594 break; // Nothing left to descend into.
1595 ElementTy = *STy->element_begin();
1596 Indices.push_back(IRB.getInt32(0));
1601 } while (ElementTy != TargetTy);
1602 if (ElementTy != TargetTy)
1603 Indices.erase(Indices.end() - NumLayers, Indices.end());
1605 return buildGEP(IRB, BasePtr, Indices, NamePrefix);
1608 /// \brief Recursively compute indices for a natural GEP.
1610 /// This is the recursive step for getNaturalGEPWithOffset that walks down the
1611 /// element types adding appropriate indices for the GEP.
1612 static Value *getNaturalGEPRecursively(IRBuilderTy &IRB, const DataLayout &DL,
1613 Value *Ptr, Type *Ty, APInt &Offset,
1615 SmallVectorImpl<Value *> &Indices,
1618 return getNaturalGEPWithType(IRB, DL, Ptr, Ty, TargetTy, Indices,
1621 // We can't recurse through pointer types.
1622 if (Ty->isPointerTy())
1625 // We try to analyze GEPs over vectors here, but note that these GEPs are
1626 // extremely poorly defined currently. The long-term goal is to remove GEPing
1627 // over a vector from the IR completely.
1628 if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) {
1629 unsigned ElementSizeInBits = DL.getTypeSizeInBits(VecTy->getScalarType());
1630 if (ElementSizeInBits % 8 != 0) {
1631 // GEPs over non-multiple of 8 size vector elements are invalid.
1634 APInt ElementSize(Offset.getBitWidth(), ElementSizeInBits / 8);
1635 APInt NumSkippedElements = Offset.sdiv(ElementSize);
1636 if (NumSkippedElements.ugt(VecTy->getNumElements()))
1638 Offset -= NumSkippedElements * ElementSize;
1639 Indices.push_back(IRB.getInt(NumSkippedElements));
1640 return getNaturalGEPRecursively(IRB, DL, Ptr, VecTy->getElementType(),
1641 Offset, TargetTy, Indices, NamePrefix);
1644 if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) {
1645 Type *ElementTy = ArrTy->getElementType();
1646 APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy));
1647 APInt NumSkippedElements = Offset.sdiv(ElementSize);
1648 if (NumSkippedElements.ugt(ArrTy->getNumElements()))
1651 Offset -= NumSkippedElements * ElementSize;
1652 Indices.push_back(IRB.getInt(NumSkippedElements));
1653 return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy,
1654 Indices, NamePrefix);
1657 StructType *STy = dyn_cast<StructType>(Ty);
1661 const StructLayout *SL = DL.getStructLayout(STy);
1662 uint64_t StructOffset = Offset.getZExtValue();
1663 if (StructOffset >= SL->getSizeInBytes())
1665 unsigned Index = SL->getElementContainingOffset(StructOffset);
1666 Offset -= APInt(Offset.getBitWidth(), SL->getElementOffset(Index));
1667 Type *ElementTy = STy->getElementType(Index);
1668 if (Offset.uge(DL.getTypeAllocSize(ElementTy)))
1669 return nullptr; // The offset points into alignment padding.
1671 Indices.push_back(IRB.getInt32(Index));
1672 return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy,
1673 Indices, NamePrefix);
1676 /// \brief Get a natural GEP from a base pointer to a particular offset and
1677 /// resulting in a particular type.
1679 /// The goal is to produce a "natural" looking GEP that works with the existing
1680 /// composite types to arrive at the appropriate offset and element type for
1681 /// a pointer. TargetTy is the element type the returned GEP should point-to if
1682 /// possible. We recurse by decreasing Offset, adding the appropriate index to
1683 /// Indices, and setting Ty to the result subtype.
1685 /// If no natural GEP can be constructed, this function returns null.
1686 static Value *getNaturalGEPWithOffset(IRBuilderTy &IRB, const DataLayout &DL,
1687 Value *Ptr, APInt Offset, Type *TargetTy,
1688 SmallVectorImpl<Value *> &Indices,
1690 PointerType *Ty = cast<PointerType>(Ptr->getType());
1692 // Don't consider any GEPs through an i8* as natural unless the TargetTy is
1694 if (Ty == IRB.getInt8PtrTy(Ty->getAddressSpace()) && TargetTy->isIntegerTy(8))
1697 Type *ElementTy = Ty->getElementType();
1698 if (!ElementTy->isSized())
1699 return nullptr; // We can't GEP through an unsized element.
1700 APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy));
1701 if (ElementSize == 0)
1702 return nullptr; // Zero-length arrays can't help us build a natural GEP.
1703 APInt NumSkippedElements = Offset.sdiv(ElementSize);
1705 Offset -= NumSkippedElements * ElementSize;
1706 Indices.push_back(IRB.getInt(NumSkippedElements));
1707 return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy,
1708 Indices, NamePrefix);
1711 /// \brief Compute an adjusted pointer from Ptr by Offset bytes where the
1712 /// resulting pointer has PointerTy.
1714 /// This tries very hard to compute a "natural" GEP which arrives at the offset
1715 /// and produces the pointer type desired. Where it cannot, it will try to use
1716 /// the natural GEP to arrive at the offset and bitcast to the type. Where that
1717 /// fails, it will try to use an existing i8* and GEP to the byte offset and
1718 /// bitcast to the type.
1720 /// The strategy for finding the more natural GEPs is to peel off layers of the
1721 /// pointer, walking back through bit casts and GEPs, searching for a base
1722 /// pointer from which we can compute a natural GEP with the desired
1723 /// properties. The algorithm tries to fold as many constant indices into
1724 /// a single GEP as possible, thus making each GEP more independent of the
1725 /// surrounding code.
1726 static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
1727 APInt Offset, Type *PointerTy, Twine NamePrefix) {
1728 // Even though we don't look through PHI nodes, we could be called on an
1729 // instruction in an unreachable block, which may be on a cycle.
1730 SmallPtrSet<Value *, 4> Visited;
1731 Visited.insert(Ptr);
1732 SmallVector<Value *, 4> Indices;
1734 // We may end up computing an offset pointer that has the wrong type. If we
1735 // never are able to compute one directly that has the correct type, we'll
1736 // fall back to it, so keep it around here.
1737 Value *OffsetPtr = nullptr;
1739 // Remember any i8 pointer we come across to re-use if we need to do a raw
1741 Value *Int8Ptr = nullptr;
1742 APInt Int8PtrOffset(Offset.getBitWidth(), 0);
1744 Type *TargetTy = PointerTy->getPointerElementType();
1747 // First fold any existing GEPs into the offset.
1748 while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
1749 APInt GEPOffset(Offset.getBitWidth(), 0);
1750 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
1752 Offset += GEPOffset;
1753 Ptr = GEP->getPointerOperand();
1754 if (!Visited.insert(Ptr).second)
1758 // See if we can perform a natural GEP here.
1760 if (Value *P = getNaturalGEPWithOffset(IRB, DL, Ptr, Offset, TargetTy,
1761 Indices, NamePrefix)) {
1762 if (P->getType() == PointerTy) {
1763 // Zap any offset pointer that we ended up computing in previous rounds.
1764 if (OffsetPtr && OffsetPtr->use_empty())
1765 if (Instruction *I = dyn_cast<Instruction>(OffsetPtr))
1766 I->eraseFromParent();
1774 // Stash this pointer if we've found an i8*.
1775 if (Ptr->getType()->isIntegerTy(8)) {
1777 Int8PtrOffset = Offset;
1780 // Peel off a layer of the pointer and update the offset appropriately.
1781 if (Operator::getOpcode(Ptr) == Instruction::BitCast) {
1782 Ptr = cast<Operator>(Ptr)->getOperand(0);
1783 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) {
1784 if (GA->mayBeOverridden())
1786 Ptr = GA->getAliasee();
1790 assert(Ptr->getType()->isPointerTy() && "Unexpected operand type!");
1791 } while (Visited.insert(Ptr).second);
1795 Int8Ptr = IRB.CreateBitCast(
1796 Ptr, IRB.getInt8PtrTy(PointerTy->getPointerAddressSpace()),
1797 NamePrefix + "sroa_raw_cast");
1798 Int8PtrOffset = Offset;
1801 OffsetPtr = Int8PtrOffset == 0
1803 : IRB.CreateInBoundsGEP(Int8Ptr, IRB.getInt(Int8PtrOffset),
1804 NamePrefix + "sroa_raw_idx");
1808 // On the off chance we were targeting i8*, guard the bitcast here.
1809 if (Ptr->getType() != PointerTy)
1810 Ptr = IRB.CreateBitCast(Ptr, PointerTy, NamePrefix + "sroa_cast");
1815 /// \brief Compute the adjusted alignment for a load or store from an offset.
1816 static unsigned getAdjustedAlignment(Instruction *I, uint64_t Offset,
1817 const DataLayout &DL) {
1820 if (auto *LI = dyn_cast<LoadInst>(I)) {
1821 Alignment = LI->getAlignment();
1823 } else if (auto *SI = dyn_cast<StoreInst>(I)) {
1824 Alignment = SI->getAlignment();
1825 Ty = SI->getValueOperand()->getType();
1827 llvm_unreachable("Only loads and stores are allowed!");
1831 Alignment = DL.getABITypeAlignment(Ty);
1833 return MinAlign(Alignment, Offset);
1836 /// \brief Test whether we can convert a value from the old to the new type.
1838 /// This predicate should be used to guard calls to convertValue in order to
1839 /// ensure that we only try to convert viable values. The strategy is that we
1840 /// will peel off single element struct and array wrappings to get to an
1841 /// underlying value, and convert that value.
1842 static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy) {
1845 if (IntegerType *OldITy = dyn_cast<IntegerType>(OldTy))
1846 if (IntegerType *NewITy = dyn_cast<IntegerType>(NewTy))
1847 if (NewITy->getBitWidth() >= OldITy->getBitWidth())
1849 if (DL.getTypeSizeInBits(NewTy) != DL.getTypeSizeInBits(OldTy))
1851 if (!NewTy->isSingleValueType() || !OldTy->isSingleValueType())
1854 // We can convert pointers to integers and vice-versa. Same for vectors
1855 // of pointers and integers.
1856 OldTy = OldTy->getScalarType();
1857 NewTy = NewTy->getScalarType();
1858 if (NewTy->isPointerTy() || OldTy->isPointerTy()) {
1859 if (NewTy->isPointerTy() && OldTy->isPointerTy())
1861 if (NewTy->isIntegerTy() || OldTy->isIntegerTy())
1869 /// \brief Generic routine to convert an SSA value to a value of a different
1872 /// This will try various different casting techniques, such as bitcasts,
1873 /// inttoptr, and ptrtoint casts. Use the \c canConvertValue predicate to test
1874 /// two types for viability with this routine.
1875 static Value *convertValue(const DataLayout &DL, IRBuilderTy &IRB, Value *V,
1877 Type *OldTy = V->getType();
1878 assert(canConvertValue(DL, OldTy, NewTy) && "Value not convertable to type");
1883 if (IntegerType *OldITy = dyn_cast<IntegerType>(OldTy))
1884 if (IntegerType *NewITy = dyn_cast<IntegerType>(NewTy))
1885 if (NewITy->getBitWidth() > OldITy->getBitWidth())
1886 return IRB.CreateZExt(V, NewITy);
1888 // See if we need inttoptr for this type pair. A cast involving both scalars
1889 // and vectors requires and additional bitcast.
1890 if (OldTy->getScalarType()->isIntegerTy() &&
1891 NewTy->getScalarType()->isPointerTy()) {
1892 // Expand <2 x i32> to i8* --> <2 x i32> to i64 to i8*
1893 if (OldTy->isVectorTy() && !NewTy->isVectorTy())
1894 return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)),
1897 // Expand i128 to <2 x i8*> --> i128 to <2 x i64> to <2 x i8*>
1898 if (!OldTy->isVectorTy() && NewTy->isVectorTy())
1899 return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)),
1902 return IRB.CreateIntToPtr(V, NewTy);
1905 // See if we need ptrtoint for this type pair. A cast involving both scalars
1906 // and vectors requires and additional bitcast.
1907 if (OldTy->getScalarType()->isPointerTy() &&
1908 NewTy->getScalarType()->isIntegerTy()) {
1909 // Expand <2 x i8*> to i128 --> <2 x i8*> to <2 x i64> to i128
1910 if (OldTy->isVectorTy() && !NewTy->isVectorTy())
1911 return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)),
1914 // Expand i8* to <2 x i32> --> i8* to i64 to <2 x i32>
1915 if (!OldTy->isVectorTy() && NewTy->isVectorTy())
1916 return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)),
1919 return IRB.CreatePtrToInt(V, NewTy);
1922 return IRB.CreateBitCast(V, NewTy);
1925 /// \brief Test whether the given slice use can be promoted to a vector.
1927 /// This function is called to test each entry in a partioning which is slated
1928 /// for a single slice.
1929 static bool isVectorPromotionViableForSlice(AllocaSlices::Partition &P,
1930 const Slice &S, VectorType *Ty,
1931 uint64_t ElementSize,
1932 const DataLayout &DL) {
1933 // First validate the slice offsets.
1934 uint64_t BeginOffset =
1935 std::max(S.beginOffset(), P.beginOffset()) - P.beginOffset();
1936 uint64_t BeginIndex = BeginOffset / ElementSize;
1937 if (BeginIndex * ElementSize != BeginOffset ||
1938 BeginIndex >= Ty->getNumElements())
1940 uint64_t EndOffset =
1941 std::min(S.endOffset(), P.endOffset()) - P.beginOffset();
1942 uint64_t EndIndex = EndOffset / ElementSize;
1943 if (EndIndex * ElementSize != EndOffset || EndIndex > Ty->getNumElements())
1946 assert(EndIndex > BeginIndex && "Empty vector!");
1947 uint64_t NumElements = EndIndex - BeginIndex;
1948 Type *SliceTy = (NumElements == 1)
1949 ? Ty->getElementType()
1950 : VectorType::get(Ty->getElementType(), NumElements);
1953 Type::getIntNTy(Ty->getContext(), NumElements * ElementSize * 8);
1955 Use *U = S.getUse();
1957 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) {
1958 if (MI->isVolatile())
1960 if (!S.isSplittable())
1961 return false; // Skip any unsplittable intrinsics.
1962 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) {
1963 if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
1964 II->getIntrinsicID() != Intrinsic::lifetime_end)
1966 } else if (U->get()->getType()->getPointerElementType()->isStructTy()) {
1967 // Disable vector promotion when there are loads or stores of an FCA.
1969 } else if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
1970 if (LI->isVolatile())
1972 Type *LTy = LI->getType();
1973 if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) {
1974 assert(LTy->isIntegerTy());
1977 if (!canConvertValue(DL, SliceTy, LTy))
1979 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
1980 if (SI->isVolatile())
1982 Type *STy = SI->getValueOperand()->getType();
1983 if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) {
1984 assert(STy->isIntegerTy());
1987 if (!canConvertValue(DL, STy, SliceTy))
1996 /// \brief Test whether the given alloca partitioning and range of slices can be
1997 /// promoted to a vector.
1999 /// This is a quick test to check whether we can rewrite a particular alloca
2000 /// partition (and its newly formed alloca) into a vector alloca with only
2001 /// whole-vector loads and stores such that it could be promoted to a vector
2002 /// SSA value. We only can ensure this for a limited set of operations, and we
2003 /// don't want to do the rewrites unless we are confident that the result will
2004 /// be promotable, so we have an early test here.
2005 static VectorType *isVectorPromotionViable(AllocaSlices::Partition &P,
2006 const DataLayout &DL) {
2007 // Collect the candidate types for vector-based promotion. Also track whether
2008 // we have different element types.
2009 SmallVector<VectorType *, 4> CandidateTys;
2010 Type *CommonEltTy = nullptr;
2011 bool HaveCommonEltTy = true;
2012 auto CheckCandidateType = [&](Type *Ty) {
2013 if (auto *VTy = dyn_cast<VectorType>(Ty)) {
2014 CandidateTys.push_back(VTy);
2016 CommonEltTy = VTy->getElementType();
2017 else if (CommonEltTy != VTy->getElementType())
2018 HaveCommonEltTy = false;
2021 // Consider any loads or stores that are the exact size of the slice.
2022 for (const Slice &S : P)
2023 if (S.beginOffset() == P.beginOffset() &&
2024 S.endOffset() == P.endOffset()) {
2025 if (auto *LI = dyn_cast<LoadInst>(S.getUse()->getUser()))
2026 CheckCandidateType(LI->getType());
2027 else if (auto *SI = dyn_cast<StoreInst>(S.getUse()->getUser()))
2028 CheckCandidateType(SI->getValueOperand()->getType());
2031 // If we didn't find a vector type, nothing to do here.
2032 if (CandidateTys.empty())
2035 // Remove non-integer vector types if we had multiple common element types.
2036 // FIXME: It'd be nice to replace them with integer vector types, but we can't
2037 // do that until all the backends are known to produce good code for all
2038 // integer vector types.
2039 if (!HaveCommonEltTy) {
2040 CandidateTys.erase(std::remove_if(CandidateTys.begin(), CandidateTys.end(),
2041 [](VectorType *VTy) {
2042 return !VTy->getElementType()->isIntegerTy();
2044 CandidateTys.end());
2046 // If there were no integer vector types, give up.
2047 if (CandidateTys.empty())
2050 // Rank the remaining candidate vector types. This is easy because we know
2051 // they're all integer vectors. We sort by ascending number of elements.
2052 auto RankVectorTypes = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2053 assert(DL.getTypeSizeInBits(RHSTy) == DL.getTypeSizeInBits(LHSTy) &&
2054 "Cannot have vector types of different sizes!");
2055 assert(RHSTy->getElementType()->isIntegerTy() &&
2056 "All non-integer types eliminated!");
2057 assert(LHSTy->getElementType()->isIntegerTy() &&
2058 "All non-integer types eliminated!");
2059 return RHSTy->getNumElements() < LHSTy->getNumElements();
2061 std::sort(CandidateTys.begin(), CandidateTys.end(), RankVectorTypes);
2063 std::unique(CandidateTys.begin(), CandidateTys.end(), RankVectorTypes),
2064 CandidateTys.end());
2066 // The only way to have the same element type in every vector type is to
2067 // have the same vector type. Check that and remove all but one.
2069 for (VectorType *VTy : CandidateTys) {
2070 assert(VTy->getElementType() == CommonEltTy &&
2071 "Unaccounted for element type!");
2072 assert(VTy == CandidateTys[0] &&
2073 "Different vector types with the same element type!");
2076 CandidateTys.resize(1);
2079 // Try each vector type, and return the one which works.
2080 auto CheckVectorTypeForPromotion = [&](VectorType *VTy) {
2081 uint64_t ElementSize = DL.getTypeSizeInBits(VTy->getElementType());
2083 // While the definition of LLVM vectors is bitpacked, we don't support sizes
2084 // that aren't byte sized.
2085 if (ElementSize % 8)
2087 assert((DL.getTypeSizeInBits(VTy) % 8) == 0 &&
2088 "vector size not a multiple of element size?");
2091 for (const Slice &S : P)
2092 if (!isVectorPromotionViableForSlice(P, S, VTy, ElementSize, DL))
2095 for (const Slice *S : P.splitSliceTails())
2096 if (!isVectorPromotionViableForSlice(P, *S, VTy, ElementSize, DL))
2101 for (VectorType *VTy : CandidateTys)
2102 if (CheckVectorTypeForPromotion(VTy))
2108 /// \brief Test whether a slice of an alloca is valid for integer widening.
2110 /// This implements the necessary checking for the \c isIntegerWideningViable
2111 /// test below on a single slice of the alloca.
2112 static bool isIntegerWideningViableForSlice(const Slice &S,
2113 uint64_t AllocBeginOffset,
2115 const DataLayout &DL,
2116 bool &WholeAllocaOp) {
2117 uint64_t Size = DL.getTypeStoreSize(AllocaTy);
2119 uint64_t RelBegin = S.beginOffset() - AllocBeginOffset;
2120 uint64_t RelEnd = S.endOffset() - AllocBeginOffset;
2122 // We can't reasonably handle cases where the load or store extends past
2123 // the end of the aloca's type and into its padding.
2127 Use *U = S.getUse();
2129 if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
2130 if (LI->isVolatile())
2132 // Note that we don't count vector loads or stores as whole-alloca
2133 // operations which enable integer widening because we would prefer to use
2134 // vector widening instead.
2135 if (!isa<VectorType>(LI->getType()) && RelBegin == 0 && RelEnd == Size)
2136 WholeAllocaOp = true;
2137 if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) {
2138 if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy))
2140 } else if (RelBegin != 0 || RelEnd != Size ||
2141 !canConvertValue(DL, AllocaTy, LI->getType())) {
2142 // Non-integer loads need to be convertible from the alloca type so that
2143 // they are promotable.
2146 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
2147 Type *ValueTy = SI->getValueOperand()->getType();
2148 if (SI->isVolatile())
2150 // Note that we don't count vector loads or stores as whole-alloca
2151 // operations which enable integer widening because we would prefer to use
2152 // vector widening instead.
2153 if (!isa<VectorType>(ValueTy) && RelBegin == 0 && RelEnd == Size)
2154 WholeAllocaOp = true;
2155 if (IntegerType *ITy = dyn_cast<IntegerType>(ValueTy)) {
2156 if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy))
2158 } else if (RelBegin != 0 || RelEnd != Size ||
2159 !canConvertValue(DL, ValueTy, AllocaTy)) {
2160 // Non-integer stores need to be convertible to the alloca type so that
2161 // they are promotable.
2164 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) {
2165 if (MI->isVolatile() || !isa<Constant>(MI->getLength()))
2167 if (!S.isSplittable())
2168 return false; // Skip any unsplittable intrinsics.
2169 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) {
2170 if (II->getIntrinsicID() != Intrinsic::lifetime_start &&
2171 II->getIntrinsicID() != Intrinsic::lifetime_end)
2180 /// \brief Test whether the given alloca partition's integer operations can be
2181 /// widened to promotable ones.
2183 /// This is a quick test to check whether we can rewrite the integer loads and
2184 /// stores to a particular alloca into wider loads and stores and be able to
2185 /// promote the resulting alloca.
2186 static bool isIntegerWideningViable(AllocaSlices::Partition &P, Type *AllocaTy,
2187 const DataLayout &DL) {
2188 uint64_t SizeInBits = DL.getTypeSizeInBits(AllocaTy);
2189 // Don't create integer types larger than the maximum bitwidth.
2190 if (SizeInBits > IntegerType::MAX_INT_BITS)
2193 // Don't try to handle allocas with bit-padding.
2194 if (SizeInBits != DL.getTypeStoreSizeInBits(AllocaTy))
2197 // We need to ensure that an integer type with the appropriate bitwidth can
2198 // be converted to the alloca type, whatever that is. We don't want to force
2199 // the alloca itself to have an integer type if there is a more suitable one.
2200 Type *IntTy = Type::getIntNTy(AllocaTy->getContext(), SizeInBits);
2201 if (!canConvertValue(DL, AllocaTy, IntTy) ||
2202 !canConvertValue(DL, IntTy, AllocaTy))
2205 // While examining uses, we ensure that the alloca has a covering load or
2206 // store. We don't want to widen the integer operations only to fail to
2207 // promote due to some other unsplittable entry (which we may make splittable
2208 // later). However, if there are only splittable uses, go ahead and assume
2209 // that we cover the alloca.
2210 // FIXME: We shouldn't consider split slices that happen to start in the
2211 // partition here...
2212 bool WholeAllocaOp =
2213 P.begin() != P.end() ? false : DL.isLegalInteger(SizeInBits);
2215 for (const Slice &S : P)
2216 if (!isIntegerWideningViableForSlice(S, P.beginOffset(), AllocaTy, DL,
2220 for (const Slice *S : P.splitSliceTails())
2221 if (!isIntegerWideningViableForSlice(*S, P.beginOffset(), AllocaTy, DL,
2225 return WholeAllocaOp;
2228 static Value *extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V,
2229 IntegerType *Ty, uint64_t Offset,
2230 const Twine &Name) {
2231 DEBUG(dbgs() << " start: " << *V << "\n");
2232 IntegerType *IntTy = cast<IntegerType>(V->getType());
2233 assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) &&
2234 "Element extends past full value");
2235 uint64_t ShAmt = 8 * Offset;
2236 if (DL.isBigEndian())
2237 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset);
2239 V = IRB.CreateLShr(V, ShAmt, Name + ".shift");
2240 DEBUG(dbgs() << " shifted: " << *V << "\n");
2242 assert(Ty->getBitWidth() <= IntTy->getBitWidth() &&
2243 "Cannot extract to a larger integer!");
2245 V = IRB.CreateTrunc(V, Ty, Name + ".trunc");
2246 DEBUG(dbgs() << " trunced: " << *V << "\n");
2251 static Value *insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old,
2252 Value *V, uint64_t Offset, const Twine &Name) {
2253 IntegerType *IntTy = cast<IntegerType>(Old->getType());
2254 IntegerType *Ty = cast<IntegerType>(V->getType());
2255 assert(Ty->getBitWidth() <= IntTy->getBitWidth() &&
2256 "Cannot insert a larger integer!");
2257 DEBUG(dbgs() << " start: " << *V << "\n");
2259 V = IRB.CreateZExt(V, IntTy, Name + ".ext");
2260 DEBUG(dbgs() << " extended: " << *V << "\n");
2262 assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) &&
2263 "Element store outside of alloca store");
2264 uint64_t ShAmt = 8 * Offset;
2265 if (DL.isBigEndian())
2266 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset);
2268 V = IRB.CreateShl(V, ShAmt, Name + ".shift");
2269 DEBUG(dbgs() << " shifted: " << *V << "\n");
2272 if (ShAmt || Ty->getBitWidth() < IntTy->getBitWidth()) {
2273 APInt Mask = ~Ty->getMask().zext(IntTy->getBitWidth()).shl(ShAmt);
2274 Old = IRB.CreateAnd(Old, Mask, Name + ".mask");
2275 DEBUG(dbgs() << " masked: " << *Old << "\n");
2276 V = IRB.CreateOr(Old, V, Name + ".insert");
2277 DEBUG(dbgs() << " inserted: " << *V << "\n");
2282 static Value *extractVector(IRBuilderTy &IRB, Value *V, unsigned BeginIndex,
2283 unsigned EndIndex, const Twine &Name) {
2284 VectorType *VecTy = cast<VectorType>(V->getType());
2285 unsigned NumElements = EndIndex - BeginIndex;
2286 assert(NumElements <= VecTy->getNumElements() && "Too many elements!");
2288 if (NumElements == VecTy->getNumElements())
2291 if (NumElements == 1) {
2292 V = IRB.CreateExtractElement(V, IRB.getInt32(BeginIndex),
2294 DEBUG(dbgs() << " extract: " << *V << "\n");
2298 SmallVector<Constant *, 8> Mask;
2299 Mask.reserve(NumElements);
2300 for (unsigned i = BeginIndex; i != EndIndex; ++i)
2301 Mask.push_back(IRB.getInt32(i));
2302 V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()),
2303 ConstantVector::get(Mask), Name + ".extract");
2304 DEBUG(dbgs() << " shuffle: " << *V << "\n");
2308 static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V,
2309 unsigned BeginIndex, const Twine &Name) {
2310 VectorType *VecTy = cast<VectorType>(Old->getType());
2311 assert(VecTy && "Can only insert a vector into a vector");
2313 VectorType *Ty = dyn_cast<VectorType>(V->getType());
2315 // Single element to insert.
2316 V = IRB.CreateInsertElement(Old, V, IRB.getInt32(BeginIndex),
2318 DEBUG(dbgs() << " insert: " << *V << "\n");
2322 assert(Ty->getNumElements() <= VecTy->getNumElements() &&
2323 "Too many elements!");
2324 if (Ty->getNumElements() == VecTy->getNumElements()) {
2325 assert(V->getType() == VecTy && "Vector type mismatch");
2328 unsigned EndIndex = BeginIndex + Ty->getNumElements();
2330 // When inserting a smaller vector into the larger to store, we first
2331 // use a shuffle vector to widen it with undef elements, and then
2332 // a second shuffle vector to select between the loaded vector and the
2334 SmallVector<Constant *, 8> Mask;
2335 Mask.reserve(VecTy->getNumElements());
2336 for (unsigned i = 0; i != VecTy->getNumElements(); ++i)
2337 if (i >= BeginIndex && i < EndIndex)
2338 Mask.push_back(IRB.getInt32(i - BeginIndex));
2340 Mask.push_back(UndefValue::get(IRB.getInt32Ty()));
2341 V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()),
2342 ConstantVector::get(Mask), Name + ".expand");
2343 DEBUG(dbgs() << " shuffle: " << *V << "\n");
2346 for (unsigned i = 0; i != VecTy->getNumElements(); ++i)
2347 Mask.push_back(IRB.getInt1(i >= BeginIndex && i < EndIndex));
2349 V = IRB.CreateSelect(ConstantVector::get(Mask), V, Old, Name + "blend");
2351 DEBUG(dbgs() << " blend: " << *V << "\n");
2356 /// \brief Visitor to rewrite instructions using p particular slice of an alloca
2357 /// to use a new alloca.
2359 /// Also implements the rewriting to vector-based accesses when the partition
2360 /// passes the isVectorPromotionViable predicate. Most of the rewriting logic
2362 class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
2363 // Befriend the base class so it can delegate to private visit methods.
2364 friend class llvm::InstVisitor<AllocaSliceRewriter, bool>;
2365 typedef llvm::InstVisitor<AllocaSliceRewriter, bool> Base;
2367 const DataLayout &DL;
2370 AllocaInst &OldAI, &NewAI;
2371 const uint64_t NewAllocaBeginOffset, NewAllocaEndOffset;
2374 // This is a convenience and flag variable that will be null unless the new
2375 // alloca's integer operations should be widened to this integer type due to
2376 // passing isIntegerWideningViable above. If it is non-null, the desired
2377 // integer type will be stored here for easy access during rewriting.
2380 // If we are rewriting an alloca partition which can be written as pure
2381 // vector operations, we stash extra information here. When VecTy is
2382 // non-null, we have some strict guarantees about the rewritten alloca:
2383 // - The new alloca is exactly the size of the vector type here.
2384 // - The accesses all either map to the entire vector or to a single
2386 // - The set of accessing instructions is only one of those handled above
2387 // in isVectorPromotionViable. Generally these are the same access kinds
2388 // which are promotable via mem2reg.
2391 uint64_t ElementSize;
2393 // The original offset of the slice currently being rewritten relative to
2394 // the original alloca.
2395 uint64_t BeginOffset, EndOffset;
2396 // The new offsets of the slice currently being rewritten relative to the
2398 uint64_t NewBeginOffset, NewEndOffset;
2404 Instruction *OldPtr;
2406 // Track post-rewrite users which are PHI nodes and Selects.
2407 SmallPtrSetImpl<PHINode *> &PHIUsers;
2408 SmallPtrSetImpl<SelectInst *> &SelectUsers;
2410 // Utility IR builder, whose name prefix is setup for each visited use, and
2411 // the insertion point is set to point to the user.
2415 AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &AS, SROA &Pass,
2416 AllocaInst &OldAI, AllocaInst &NewAI,
2417 uint64_t NewAllocaBeginOffset,
2418 uint64_t NewAllocaEndOffset, bool IsIntegerPromotable,
2419 VectorType *PromotableVecTy,
2420 SmallPtrSetImpl<PHINode *> &PHIUsers,
2421 SmallPtrSetImpl<SelectInst *> &SelectUsers)
2422 : DL(DL), AS(AS), Pass(Pass), OldAI(OldAI), NewAI(NewAI),
2423 NewAllocaBeginOffset(NewAllocaBeginOffset),
2424 NewAllocaEndOffset(NewAllocaEndOffset),
2425 NewAllocaTy(NewAI.getAllocatedType()),
2426 IntTy(IsIntegerPromotable
2429 DL.getTypeSizeInBits(NewAI.getAllocatedType()))
2431 VecTy(PromotableVecTy),
2432 ElementTy(VecTy ? VecTy->getElementType() : nullptr),
2433 ElementSize(VecTy ? DL.getTypeSizeInBits(ElementTy) / 8 : 0),
2434 BeginOffset(), EndOffset(), IsSplittable(), IsSplit(), OldUse(),
2435 OldPtr(), PHIUsers(PHIUsers), SelectUsers(SelectUsers),
2436 IRB(NewAI.getContext(), ConstantFolder()) {
2438 assert((DL.getTypeSizeInBits(ElementTy) % 8) == 0 &&
2439 "Only multiple-of-8 sized vector elements are viable");
2442 assert((!IntTy && !VecTy) || (IntTy && !VecTy) || (!IntTy && VecTy));
2445 bool visit(AllocaSlices::const_iterator I) {
2446 bool CanSROA = true;
2447 BeginOffset = I->beginOffset();
2448 EndOffset = I->endOffset();
2449 IsSplittable = I->isSplittable();
2451 BeginOffset < NewAllocaBeginOffset || EndOffset > NewAllocaEndOffset;
2452 DEBUG(dbgs() << " rewriting " << (IsSplit ? "split " : ""));
2453 DEBUG(AS.printSlice(dbgs(), I, ""));
2454 DEBUG(dbgs() << "\n");
2456 // Compute the intersecting offset range.
2457 assert(BeginOffset < NewAllocaEndOffset);
2458 assert(EndOffset > NewAllocaBeginOffset);
2459 NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset);
2460 NewEndOffset = std::min(EndOffset, NewAllocaEndOffset);
2462 SliceSize = NewEndOffset - NewBeginOffset;
2464 OldUse = I->getUse();
2465 OldPtr = cast<Instruction>(OldUse->get());
2467 Instruction *OldUserI = cast<Instruction>(OldUse->getUser());
2468 IRB.SetInsertPoint(OldUserI);
2469 IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc());
2470 IRB.SetNamePrefix(Twine(NewAI.getName()) + "." + Twine(BeginOffset) + ".");
2472 CanSROA &= visit(cast<Instruction>(OldUse->getUser()));
2479 // Make sure the other visit overloads are visible.
2482 // Every instruction which can end up as a user must have a rewrite rule.
2483 bool visitInstruction(Instruction &I) {
2484 DEBUG(dbgs() << " !!!! Cannot rewrite: " << I << "\n");
2485 llvm_unreachable("No rewrite rule for this instruction!");
2488 Value *getNewAllocaSlicePtr(IRBuilderTy &IRB, Type *PointerTy) {
2489 // Note that the offset computation can use BeginOffset or NewBeginOffset
2490 // interchangeably for unsplit slices.
2491 assert(IsSplit || BeginOffset == NewBeginOffset);
2492 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
2495 StringRef OldName = OldPtr->getName();
2496 // Skip through the last '.sroa.' component of the name.
2497 size_t LastSROAPrefix = OldName.rfind(".sroa.");
2498 if (LastSROAPrefix != StringRef::npos) {
2499 OldName = OldName.substr(LastSROAPrefix + strlen(".sroa."));
2500 // Look for an SROA slice index.
2501 size_t IndexEnd = OldName.find_first_not_of("0123456789");
2502 if (IndexEnd != StringRef::npos && OldName[IndexEnd] == '.') {
2503 // Strip the index and look for the offset.
2504 OldName = OldName.substr(IndexEnd + 1);
2505 size_t OffsetEnd = OldName.find_first_not_of("0123456789");
2506 if (OffsetEnd != StringRef::npos && OldName[OffsetEnd] == '.')
2507 // Strip the offset.
2508 OldName = OldName.substr(OffsetEnd + 1);
2511 // Strip any SROA suffixes as well.
2512 OldName = OldName.substr(0, OldName.find(".sroa_"));
2515 return getAdjustedPtr(IRB, DL, &NewAI,
2516 APInt(DL.getPointerSizeInBits(), Offset), PointerTy,
2518 Twine(OldName) + "."
2525 /// \brief Compute suitable alignment to access this slice of the *new*
2528 /// You can optionally pass a type to this routine and if that type's ABI
2529 /// alignment is itself suitable, this will return zero.
2530 unsigned getSliceAlign(Type *Ty = nullptr) {
2531 unsigned NewAIAlign = NewAI.getAlignment();
2533 NewAIAlign = DL.getABITypeAlignment(NewAI.getAllocatedType());
2535 MinAlign(NewAIAlign, NewBeginOffset - NewAllocaBeginOffset);
2536 return (Ty && Align == DL.getABITypeAlignment(Ty)) ? 0 : Align;
2539 unsigned getIndex(uint64_t Offset) {
2540 assert(VecTy && "Can only call getIndex when rewriting a vector");
2541 uint64_t RelOffset = Offset - NewAllocaBeginOffset;
2542 assert(RelOffset / ElementSize < UINT32_MAX && "Index out of bounds");
2543 uint32_t Index = RelOffset / ElementSize;
2544 assert(Index * ElementSize == RelOffset);
2548 void deleteIfTriviallyDead(Value *V) {
2549 Instruction *I = cast<Instruction>(V);
2550 if (isInstructionTriviallyDead(I))
2551 Pass.DeadInsts.insert(I);
2554 Value *rewriteVectorizedLoadInst() {
2555 unsigned BeginIndex = getIndex(NewBeginOffset);
2556 unsigned EndIndex = getIndex(NewEndOffset);
2557 assert(EndIndex > BeginIndex && "Empty vector!");
2559 Value *V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load");
2560 return extractVector(IRB, V, BeginIndex, EndIndex, "vec");
2563 Value *rewriteIntegerLoad(LoadInst &LI) {
2564 assert(IntTy && "We cannot insert an integer to the alloca");
2565 assert(!LI.isVolatile());
2566 Value *V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load");
2567 V = convertValue(DL, IRB, V, IntTy);
2568 assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset");
2569 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
2570 if (Offset > 0 || NewEndOffset < NewAllocaEndOffset)
2571 V = extractInteger(DL, IRB, V, cast<IntegerType>(LI.getType()), Offset,
2576 bool visitLoadInst(LoadInst &LI) {
2577 DEBUG(dbgs() << " original: " << LI << "\n");
2578 Value *OldOp = LI.getOperand(0);
2579 assert(OldOp == OldPtr);
2581 Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), SliceSize * 8)
2583 bool IsPtrAdjusted = false;
2586 V = rewriteVectorizedLoadInst();
2587 } else if (IntTy && LI.getType()->isIntegerTy()) {
2588 V = rewriteIntegerLoad(LI);
2589 } else if (NewBeginOffset == NewAllocaBeginOffset &&
2590 canConvertValue(DL, NewAllocaTy, LI.getType())) {
2591 V = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), LI.isVolatile(),
2594 Type *LTy = TargetTy->getPointerTo();
2595 V = IRB.CreateAlignedLoad(getNewAllocaSlicePtr(IRB, LTy),
2596 getSliceAlign(TargetTy), LI.isVolatile(),
2598 IsPtrAdjusted = true;
2600 V = convertValue(DL, IRB, V, TargetTy);
2603 assert(!LI.isVolatile());
2604 assert(LI.getType()->isIntegerTy() &&
2605 "Only integer type loads and stores are split");
2606 assert(SliceSize < DL.getTypeStoreSize(LI.getType()) &&
2607 "Split load isn't smaller than original load");
2608 assert(LI.getType()->getIntegerBitWidth() ==
2609 DL.getTypeStoreSizeInBits(LI.getType()) &&
2610 "Non-byte-multiple bit width");
2611 // Move the insertion point just past the load so that we can refer to it.
2612 IRB.SetInsertPoint(std::next(BasicBlock::iterator(&LI)));
2613 // Create a placeholder value with the same type as LI to use as the
2614 // basis for the new value. This allows us to replace the uses of LI with
2615 // the computed value, and then replace the placeholder with LI, leaving
2616 // LI only used for this computation.
2617 Value *Placeholder =
2618 new LoadInst(UndefValue::get(LI.getType()->getPointerTo()));
2619 V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset, "insert");
2620 LI.replaceAllUsesWith(V);
2621 Placeholder->replaceAllUsesWith(&LI);
2624 LI.replaceAllUsesWith(V);
2627 Pass.DeadInsts.insert(&LI);
2628 deleteIfTriviallyDead(OldOp);
2629 DEBUG(dbgs() << " to: " << *V << "\n");
2630 return !LI.isVolatile() && !IsPtrAdjusted;
2633 bool rewriteVectorizedStoreInst(Value *V, StoreInst &SI, Value *OldOp) {
2634 if (V->getType() != VecTy) {
2635 unsigned BeginIndex = getIndex(NewBeginOffset);
2636 unsigned EndIndex = getIndex(NewEndOffset);
2637 assert(EndIndex > BeginIndex && "Empty vector!");
2638 unsigned NumElements = EndIndex - BeginIndex;
2639 assert(NumElements <= VecTy->getNumElements() && "Too many elements!");
2640 Type *SliceTy = (NumElements == 1)
2642 : VectorType::get(ElementTy, NumElements);
2643 if (V->getType() != SliceTy)
2644 V = convertValue(DL, IRB, V, SliceTy);
2646 // Mix in the existing elements.
2647 Value *Old = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load");
2648 V = insertVector(IRB, Old, V, BeginIndex, "vec");
2650 StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment());
2651 Pass.DeadInsts.insert(&SI);
2654 DEBUG(dbgs() << " to: " << *Store << "\n");
2658 bool rewriteIntegerStore(Value *V, StoreInst &SI) {
2659 assert(IntTy && "We cannot extract an integer from the alloca");
2660 assert(!SI.isVolatile());
2661 if (DL.getTypeSizeInBits(V->getType()) != IntTy->getBitWidth()) {
2663 IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload");
2664 Old = convertValue(DL, IRB, Old, IntTy);
2665 assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset");
2666 uint64_t Offset = BeginOffset - NewAllocaBeginOffset;
2667 V = insertInteger(DL, IRB, Old, SI.getValueOperand(), Offset, "insert");
2669 V = convertValue(DL, IRB, V, NewAllocaTy);
2670 StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment());
2671 Pass.DeadInsts.insert(&SI);
2673 DEBUG(dbgs() << " to: " << *Store << "\n");
2677 bool visitStoreInst(StoreInst &SI) {
2678 DEBUG(dbgs() << " original: " << SI << "\n");
2679 Value *OldOp = SI.getOperand(1);
2680 assert(OldOp == OldPtr);
2682 Value *V = SI.getValueOperand();
2684 // Strip all inbounds GEPs and pointer casts to try to dig out any root
2685 // alloca that should be re-examined after promoting this alloca.
2686 if (V->getType()->isPointerTy())
2687 if (AllocaInst *AI = dyn_cast<AllocaInst>(V->stripInBoundsOffsets()))
2688 Pass.PostPromotionWorklist.insert(AI);
2690 if (SliceSize < DL.getTypeStoreSize(V->getType())) {
2691 assert(!SI.isVolatile());
2692 assert(V->getType()->isIntegerTy() &&
2693 "Only integer type loads and stores are split");
2694 assert(V->getType()->getIntegerBitWidth() ==
2695 DL.getTypeStoreSizeInBits(V->getType()) &&
2696 "Non-byte-multiple bit width");
2697 IntegerType *NarrowTy = Type::getIntNTy(SI.getContext(), SliceSize * 8);
2698 V = extractInteger(DL, IRB, V, NarrowTy, NewBeginOffset, "extract");
2702 return rewriteVectorizedStoreInst(V, SI, OldOp);
2703 if (IntTy && V->getType()->isIntegerTy())
2704 return rewriteIntegerStore(V, SI);
2707 if (NewBeginOffset == NewAllocaBeginOffset &&
2708 NewEndOffset == NewAllocaEndOffset &&
2709 canConvertValue(DL, V->getType(), NewAllocaTy)) {
2710 V = convertValue(DL, IRB, V, NewAllocaTy);
2711 NewSI = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(),
2714 Value *NewPtr = getNewAllocaSlicePtr(IRB, V->getType()->getPointerTo());
2715 NewSI = IRB.CreateAlignedStore(V, NewPtr, getSliceAlign(V->getType()),
2719 Pass.DeadInsts.insert(&SI);
2720 deleteIfTriviallyDead(OldOp);
2722 DEBUG(dbgs() << " to: " << *NewSI << "\n");
2723 return NewSI->getPointerOperand() == &NewAI && !SI.isVolatile();
2726 /// \brief Compute an integer value from splatting an i8 across the given
2727 /// number of bytes.
2729 /// Note that this routine assumes an i8 is a byte. If that isn't true, don't
2730 /// call this routine.
2731 /// FIXME: Heed the advice above.
2733 /// \param V The i8 value to splat.
2734 /// \param Size The number of bytes in the output (assuming i8 is one byte)
2735 Value *getIntegerSplat(Value *V, unsigned Size) {
2736 assert(Size > 0 && "Expected a positive number of bytes.");
2737 IntegerType *VTy = cast<IntegerType>(V->getType());
2738 assert(VTy->getBitWidth() == 8 && "Expected an i8 value for the byte");
2742 Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8);
2744 IRB.CreateZExt(V, SplatIntTy, "zext"),
2745 ConstantExpr::getUDiv(
2746 Constant::getAllOnesValue(SplatIntTy),
2747 ConstantExpr::getZExt(Constant::getAllOnesValue(V->getType()),
2753 /// \brief Compute a vector splat for a given element value.
2754 Value *getVectorSplat(Value *V, unsigned NumElements) {
2755 V = IRB.CreateVectorSplat(NumElements, V, "vsplat");
2756 DEBUG(dbgs() << " splat: " << *V << "\n");
2760 bool visitMemSetInst(MemSetInst &II) {
2761 DEBUG(dbgs() << " original: " << II << "\n");
2762 assert(II.getRawDest() == OldPtr);
2764 // If the memset has a variable size, it cannot be split, just adjust the
2765 // pointer to the new alloca.
2766 if (!isa<Constant>(II.getLength())) {
2768 assert(NewBeginOffset == BeginOffset);
2769 II.setDest(getNewAllocaSlicePtr(IRB, OldPtr->getType()));
2770 Type *CstTy = II.getAlignmentCst()->getType();
2771 II.setAlignment(ConstantInt::get(CstTy, getSliceAlign()));
2773 deleteIfTriviallyDead(OldPtr);
2777 // Record this instruction for deletion.
2778 Pass.DeadInsts.insert(&II);
2780 Type *AllocaTy = NewAI.getAllocatedType();
2781 Type *ScalarTy = AllocaTy->getScalarType();
2783 // If this doesn't map cleanly onto the alloca type, and that type isn't
2784 // a single value type, just emit a memset.
2785 if (!VecTy && !IntTy &&
2786 (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset ||
2787 SliceSize != DL.getTypeStoreSize(AllocaTy) ||
2788 !AllocaTy->isSingleValueType() ||
2789 !DL.isLegalInteger(DL.getTypeSizeInBits(ScalarTy)) ||
2790 DL.getTypeSizeInBits(ScalarTy) % 8 != 0)) {
2791 Type *SizeTy = II.getLength()->getType();
2792 Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset);
2793 CallInst *New = IRB.CreateMemSet(
2794 getNewAllocaSlicePtr(IRB, OldPtr->getType()), II.getValue(), Size,
2795 getSliceAlign(), II.isVolatile());
2797 DEBUG(dbgs() << " to: " << *New << "\n");
2801 // If we can represent this as a simple value, we have to build the actual
2802 // value to store, which requires expanding the byte present in memset to
2803 // a sensible representation for the alloca type. This is essentially
2804 // splatting the byte to a sufficiently wide integer, splatting it across
2805 // any desired vector width, and bitcasting to the final type.
2809 // If this is a memset of a vectorized alloca, insert it.
2810 assert(ElementTy == ScalarTy);
2812 unsigned BeginIndex = getIndex(NewBeginOffset);
2813 unsigned EndIndex = getIndex(NewEndOffset);
2814 assert(EndIndex > BeginIndex && "Empty vector!");
2815 unsigned NumElements = EndIndex - BeginIndex;
2816 assert(NumElements <= VecTy->getNumElements() && "Too many elements!");
2819 getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ElementTy) / 8);
2820 Splat = convertValue(DL, IRB, Splat, ElementTy);
2821 if (NumElements > 1)
2822 Splat = getVectorSplat(Splat, NumElements);
2825 IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload");
2826 V = insertVector(IRB, Old, Splat, BeginIndex, "vec");
2828 // If this is a memset on an alloca where we can widen stores, insert the
2830 assert(!II.isVolatile());
2832 uint64_t Size = NewEndOffset - NewBeginOffset;
2833 V = getIntegerSplat(II.getValue(), Size);
2835 if (IntTy && (BeginOffset != NewAllocaBeginOffset ||
2836 EndOffset != NewAllocaBeginOffset)) {
2838 IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload");
2839 Old = convertValue(DL, IRB, Old, IntTy);
2840 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
2841 V = insertInteger(DL, IRB, Old, V, Offset, "insert");
2843 assert(V->getType() == IntTy &&
2844 "Wrong type for an alloca wide integer!");
2846 V = convertValue(DL, IRB, V, AllocaTy);
2848 // Established these invariants above.
2849 assert(NewBeginOffset == NewAllocaBeginOffset);
2850 assert(NewEndOffset == NewAllocaEndOffset);
2852 V = getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ScalarTy) / 8);
2853 if (VectorType *AllocaVecTy = dyn_cast<VectorType>(AllocaTy))
2854 V = getVectorSplat(V, AllocaVecTy->getNumElements());
2856 V = convertValue(DL, IRB, V, AllocaTy);
2859 Value *New = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(),
2862 DEBUG(dbgs() << " to: " << *New << "\n");
2863 return !II.isVolatile();
2866 bool visitMemTransferInst(MemTransferInst &II) {
2867 // Rewriting of memory transfer instructions can be a bit tricky. We break
2868 // them into two categories: split intrinsics and unsplit intrinsics.
2870 DEBUG(dbgs() << " original: " << II << "\n");
2872 bool IsDest = &II.getRawDestUse() == OldUse;
2873 assert((IsDest && II.getRawDest() == OldPtr) ||
2874 (!IsDest && II.getRawSource() == OldPtr));
2876 unsigned SliceAlign = getSliceAlign();
2878 // For unsplit intrinsics, we simply modify the source and destination
2879 // pointers in place. This isn't just an optimization, it is a matter of
2880 // correctness. With unsplit intrinsics we may be dealing with transfers
2881 // within a single alloca before SROA ran, or with transfers that have
2882 // a variable length. We may also be dealing with memmove instead of
2883 // memcpy, and so simply updating the pointers is the necessary for us to
2884 // update both source and dest of a single call.
2885 if (!IsSplittable) {
2886 Value *AdjustedPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
2888 II.setDest(AdjustedPtr);
2890 II.setSource(AdjustedPtr);
2892 if (II.getAlignment() > SliceAlign) {
2893 Type *CstTy = II.getAlignmentCst()->getType();
2895 ConstantInt::get(CstTy, MinAlign(II.getAlignment(), SliceAlign)));
2898 DEBUG(dbgs() << " to: " << II << "\n");
2899 deleteIfTriviallyDead(OldPtr);
2902 // For split transfer intrinsics we have an incredibly useful assurance:
2903 // the source and destination do not reside within the same alloca, and at
2904 // least one of them does not escape. This means that we can replace
2905 // memmove with memcpy, and we don't need to worry about all manner of
2906 // downsides to splitting and transforming the operations.
2908 // If this doesn't map cleanly onto the alloca type, and that type isn't
2909 // a single value type, just emit a memcpy.
2912 (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset ||
2913 SliceSize != DL.getTypeStoreSize(NewAI.getAllocatedType()) ||
2914 !NewAI.getAllocatedType()->isSingleValueType());
2916 // If we're just going to emit a memcpy, the alloca hasn't changed, and the
2917 // size hasn't been shrunk based on analysis of the viable range, this is
2919 if (EmitMemCpy && &OldAI == &NewAI) {
2920 // Ensure the start lines up.
2921 assert(NewBeginOffset == BeginOffset);
2923 // Rewrite the size as needed.
2924 if (NewEndOffset != EndOffset)
2925 II.setLength(ConstantInt::get(II.getLength()->getType(),
2926 NewEndOffset - NewBeginOffset));
2929 // Record this instruction for deletion.
2930 Pass.DeadInsts.insert(&II);
2932 // Strip all inbounds GEPs and pointer casts to try to dig out any root
2933 // alloca that should be re-examined after rewriting this instruction.
2934 Value *OtherPtr = IsDest ? II.getRawSource() : II.getRawDest();
2935 if (AllocaInst *AI =
2936 dyn_cast<AllocaInst>(OtherPtr->stripInBoundsOffsets())) {
2937 assert(AI != &OldAI && AI != &NewAI &&
2938 "Splittable transfers cannot reach the same alloca on both ends.");
2939 Pass.Worklist.insert(AI);
2942 Type *OtherPtrTy = OtherPtr->getType();
2943 unsigned OtherAS = OtherPtrTy->getPointerAddressSpace();
2945 // Compute the relative offset for the other pointer within the transfer.
2946 unsigned IntPtrWidth = DL.getPointerSizeInBits(OtherAS);
2947 APInt OtherOffset(IntPtrWidth, NewBeginOffset - BeginOffset);
2948 unsigned OtherAlign = MinAlign(II.getAlignment() ? II.getAlignment() : 1,
2949 OtherOffset.zextOrTrunc(64).getZExtValue());
2952 // Compute the other pointer, folding as much as possible to produce
2953 // a single, simple GEP in most cases.
2954 OtherPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy,
2955 OtherPtr->getName() + ".");
2957 Value *OurPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
2958 Type *SizeTy = II.getLength()->getType();
2959 Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset);
2961 CallInst *New = IRB.CreateMemCpy(
2962 IsDest ? OurPtr : OtherPtr, IsDest ? OtherPtr : OurPtr, Size,
2963 MinAlign(SliceAlign, OtherAlign), II.isVolatile());
2965 DEBUG(dbgs() << " to: " << *New << "\n");
2969 bool IsWholeAlloca = NewBeginOffset == NewAllocaBeginOffset &&
2970 NewEndOffset == NewAllocaEndOffset;
2971 uint64_t Size = NewEndOffset - NewBeginOffset;
2972 unsigned BeginIndex = VecTy ? getIndex(NewBeginOffset) : 0;
2973 unsigned EndIndex = VecTy ? getIndex(NewEndOffset) : 0;
2974 unsigned NumElements = EndIndex - BeginIndex;
2975 IntegerType *SubIntTy =
2976 IntTy ? Type::getIntNTy(IntTy->getContext(), Size * 8) : nullptr;
2978 // Reset the other pointer type to match the register type we're going to
2979 // use, but using the address space of the original other pointer.
2980 if (VecTy && !IsWholeAlloca) {
2981 if (NumElements == 1)
2982 OtherPtrTy = VecTy->getElementType();
2984 OtherPtrTy = VectorType::get(VecTy->getElementType(), NumElements);
2986 OtherPtrTy = OtherPtrTy->getPointerTo(OtherAS);
2987 } else if (IntTy && !IsWholeAlloca) {
2988 OtherPtrTy = SubIntTy->getPointerTo(OtherAS);
2990 OtherPtrTy = NewAllocaTy->getPointerTo(OtherAS);
2993 Value *SrcPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy,
2994 OtherPtr->getName() + ".");
2995 unsigned SrcAlign = OtherAlign;
2996 Value *DstPtr = &NewAI;
2997 unsigned DstAlign = SliceAlign;
2999 std::swap(SrcPtr, DstPtr);
3000 std::swap(SrcAlign, DstAlign);
3004 if (VecTy && !IsWholeAlloca && !IsDest) {
3005 Src = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load");
3006 Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec");
3007 } else if (IntTy && !IsWholeAlloca && !IsDest) {
3008 Src = IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "load");
3009 Src = convertValue(DL, IRB, Src, IntTy);
3010 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3011 Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract");
3014 IRB.CreateAlignedLoad(SrcPtr, SrcAlign, II.isVolatile(), "copyload");
3017 if (VecTy && !IsWholeAlloca && IsDest) {
3019 IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload");
3020 Src = insertVector(IRB, Old, Src, BeginIndex, "vec");
3021 } else if (IntTy && !IsWholeAlloca && IsDest) {
3023 IRB.CreateAlignedLoad(&NewAI, NewAI.getAlignment(), "oldload");
3024 Old = convertValue(DL, IRB, Old, IntTy);
3025 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3026 Src = insertInteger(DL, IRB, Old, Src, Offset, "insert");
3027 Src = convertValue(DL, IRB, Src, NewAllocaTy);
3030 StoreInst *Store = cast<StoreInst>(
3031 IRB.CreateAlignedStore(Src, DstPtr, DstAlign, II.isVolatile()));
3033 DEBUG(dbgs() << " to: " << *Store << "\n");
3034 return !II.isVolatile();
3037 bool visitIntrinsicInst(IntrinsicInst &II) {
3038 assert(II.getIntrinsicID() == Intrinsic::lifetime_start ||
3039 II.getIntrinsicID() == Intrinsic::lifetime_end);
3040 DEBUG(dbgs() << " original: " << II << "\n");
3041 assert(II.getArgOperand(1) == OldPtr);
3043 // Record this instruction for deletion.
3044 Pass.DeadInsts.insert(&II);
3047 ConstantInt::get(cast<IntegerType>(II.getArgOperand(0)->getType()),
3048 NewEndOffset - NewBeginOffset);
3049 Value *Ptr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
3051 if (II.getIntrinsicID() == Intrinsic::lifetime_start)
3052 New = IRB.CreateLifetimeStart(Ptr, Size);
3054 New = IRB.CreateLifetimeEnd(Ptr, Size);
3057 DEBUG(dbgs() << " to: " << *New << "\n");
3061 bool visitPHINode(PHINode &PN) {
3062 DEBUG(dbgs() << " original: " << PN << "\n");
3063 assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable");
3064 assert(EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable");
3066 // We would like to compute a new pointer in only one place, but have it be
3067 // as local as possible to the PHI. To do that, we re-use the location of
3068 // the old pointer, which necessarily must be in the right position to
3069 // dominate the PHI.
3070 IRBuilderTy PtrBuilder(IRB);
3071 if (isa<PHINode>(OldPtr))
3072 PtrBuilder.SetInsertPoint(OldPtr->getParent()->getFirstInsertionPt());
3074 PtrBuilder.SetInsertPoint(OldPtr);
3075 PtrBuilder.SetCurrentDebugLocation(OldPtr->getDebugLoc());
3077 Value *NewPtr = getNewAllocaSlicePtr(PtrBuilder, OldPtr->getType());
3078 // Replace the operands which were using the old pointer.
3079 std::replace(PN.op_begin(), PN.op_end(), cast<Value>(OldPtr), NewPtr);
3081 DEBUG(dbgs() << " to: " << PN << "\n");
3082 deleteIfTriviallyDead(OldPtr);
3084 // PHIs can't be promoted on their own, but often can be speculated. We
3085 // check the speculation outside of the rewriter so that we see the
3086 // fully-rewritten alloca.
3087 PHIUsers.insert(&PN);
3091 bool visitSelectInst(SelectInst &SI) {
3092 DEBUG(dbgs() << " original: " << SI << "\n");
3093 assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) &&
3094 "Pointer isn't an operand!");
3095 assert(BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable");
3096 assert(EndOffset <= NewAllocaEndOffset && "Selects are unsplittable");
3098 Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
3099 // Replace the operands which were using the old pointer.
3100 if (SI.getOperand(1) == OldPtr)
3101 SI.setOperand(1, NewPtr);
3102 if (SI.getOperand(2) == OldPtr)
3103 SI.setOperand(2, NewPtr);
3105 DEBUG(dbgs() << " to: " << SI << "\n");
3106 deleteIfTriviallyDead(OldPtr);
3108 // Selects can't be promoted on their own, but often can be speculated. We
3109 // check the speculation outside of the rewriter so that we see the
3110 // fully-rewritten alloca.
3111 SelectUsers.insert(&SI);
3118 /// \brief Visitor to rewrite aggregate loads and stores as scalar.
3120 /// This pass aggressively rewrites all aggregate loads and stores on
3121 /// a particular pointer (or any pointer derived from it which we can identify)
3122 /// with scalar loads and stores.
3123 class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
3124 // Befriend the base class so it can delegate to private visit methods.
3125 friend class llvm::InstVisitor<AggLoadStoreRewriter, bool>;
3127 const DataLayout &DL;
3129 /// Queue of pointer uses to analyze and potentially rewrite.
3130 SmallVector<Use *, 8> Queue;
3132 /// Set to prevent us from cycling with phi nodes and loops.
3133 SmallPtrSet<User *, 8> Visited;
3135 /// The current pointer use being rewritten. This is used to dig up the used
3136 /// value (as opposed to the user).
3140 AggLoadStoreRewriter(const DataLayout &DL) : DL(DL) {}
3142 /// Rewrite loads and stores through a pointer and all pointers derived from
3144 bool rewrite(Instruction &I) {
3145 DEBUG(dbgs() << " Rewriting FCA loads and stores...\n");
3147 bool Changed = false;
3148 while (!Queue.empty()) {
3149 U = Queue.pop_back_val();
3150 Changed |= visit(cast<Instruction>(U->getUser()));
3156 /// Enqueue all the users of the given instruction for further processing.
3157 /// This uses a set to de-duplicate users.
3158 void enqueueUsers(Instruction &I) {
3159 for (Use &U : I.uses())
3160 if (Visited.insert(U.getUser()).second)
3161 Queue.push_back(&U);
3164 // Conservative default is to not rewrite anything.
3165 bool visitInstruction(Instruction &I) { return false; }
3167 /// \brief Generic recursive split emission class.
3168 template <typename Derived> class OpSplitter {
3170 /// The builder used to form new instructions.
3172 /// The indices which to be used with insert- or extractvalue to select the
3173 /// appropriate value within the aggregate.
3174 SmallVector<unsigned, 4> Indices;
3175 /// The indices to a GEP instruction which will move Ptr to the correct slot
3176 /// within the aggregate.
3177 SmallVector<Value *, 4> GEPIndices;
3178 /// The base pointer of the original op, used as a base for GEPing the
3179 /// split operations.
3182 /// Initialize the splitter with an insertion point, Ptr and start with a
3183 /// single zero GEP index.
3184 OpSplitter(Instruction *InsertionPoint, Value *Ptr)
3185 : IRB(InsertionPoint), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr) {}
3188 /// \brief Generic recursive split emission routine.
3190 /// This method recursively splits an aggregate op (load or store) into
3191 /// scalar or vector ops. It splits recursively until it hits a single value
3192 /// and emits that single value operation via the template argument.
3194 /// The logic of this routine relies on GEPs and insertvalue and
3195 /// extractvalue all operating with the same fundamental index list, merely
3196 /// formatted differently (GEPs need actual values).
3198 /// \param Ty The type being split recursively into smaller ops.
3199 /// \param Agg The aggregate value being built up or stored, depending on
3200 /// whether this is splitting a load or a store respectively.
3201 void emitSplitOps(Type *Ty, Value *&Agg, const Twine &Name) {
3202 if (Ty->isSingleValueType())
3203 return static_cast<Derived *>(this)->emitFunc(Ty, Agg, Name);
3205 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
3206 unsigned OldSize = Indices.size();
3208 for (unsigned Idx = 0, Size = ATy->getNumElements(); Idx != Size;
3210 assert(Indices.size() == OldSize && "Did not return to the old size");
3211 Indices.push_back(Idx);
3212 GEPIndices.push_back(IRB.getInt32(Idx));
3213 emitSplitOps(ATy->getElementType(), Agg, Name + "." + Twine(Idx));
3214 GEPIndices.pop_back();
3220 if (StructType *STy = dyn_cast<StructType>(Ty)) {
3221 unsigned OldSize = Indices.size();
3223 for (unsigned Idx = 0, Size = STy->getNumElements(); Idx != Size;
3225 assert(Indices.size() == OldSize && "Did not return to the old size");
3226 Indices.push_back(Idx);
3227 GEPIndices.push_back(IRB.getInt32(Idx));
3228 emitSplitOps(STy->getElementType(Idx), Agg, Name + "." + Twine(Idx));
3229 GEPIndices.pop_back();
3235 llvm_unreachable("Only arrays and structs are aggregate loadable types");
3239 struct LoadOpSplitter : public OpSplitter<LoadOpSplitter> {
3240 LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr)
3241 : OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr) {}
3243 /// Emit a leaf load of a single value. This is called at the leaves of the
3244 /// recursive emission to actually load values.
3245 void emitFunc(Type *Ty, Value *&Agg, const Twine &Name) {
3246 assert(Ty->isSingleValueType());
3247 // Load the single value and insert it using the indices.
3248 Value *GEP = IRB.CreateInBoundsGEP(Ptr, GEPIndices, Name + ".gep");
3249 Value *Load = IRB.CreateLoad(GEP, Name + ".load");
3250 Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert");
3251 DEBUG(dbgs() << " to: " << *Load << "\n");
3255 bool visitLoadInst(LoadInst &LI) {
3256 assert(LI.getPointerOperand() == *U);
3257 if (!LI.isSimple() || LI.getType()->isSingleValueType())
3260 // We have an aggregate being loaded, split it apart.
3261 DEBUG(dbgs() << " original: " << LI << "\n");
3262 LoadOpSplitter Splitter(&LI, *U);
3263 Value *V = UndefValue::get(LI.getType());
3264 Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca");
3265 LI.replaceAllUsesWith(V);
3266 LI.eraseFromParent();
3270 struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> {
3271 StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr)
3272 : OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr) {}
3274 /// Emit a leaf store of a single value. This is called at the leaves of the
3275 /// recursive emission to actually produce stores.
3276 void emitFunc(Type *Ty, Value *&Agg, const Twine &Name) {
3277 assert(Ty->isSingleValueType());
3278 // Extract the single value and store it using the indices.
3279 Value *Store = IRB.CreateStore(
3280 IRB.CreateExtractValue(Agg, Indices, Name + ".extract"),
3281 IRB.CreateInBoundsGEP(Ptr, GEPIndices, Name + ".gep"));
3283 DEBUG(dbgs() << " to: " << *Store << "\n");
3287 bool visitStoreInst(StoreInst &SI) {
3288 if (!SI.isSimple() || SI.getPointerOperand() != *U)
3290 Value *V = SI.getValueOperand();
3291 if (V->getType()->isSingleValueType())
3294 // We have an aggregate being stored, split it apart.
3295 DEBUG(dbgs() << " original: " << SI << "\n");
3296 StoreOpSplitter Splitter(&SI, *U);
3297 Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca");
3298 SI.eraseFromParent();
3302 bool visitBitCastInst(BitCastInst &BC) {
3307 bool visitGetElementPtrInst(GetElementPtrInst &GEPI) {
3312 bool visitPHINode(PHINode &PN) {
3317 bool visitSelectInst(SelectInst &SI) {
3324 /// \brief Strip aggregate type wrapping.
3326 /// This removes no-op aggregate types wrapping an underlying type. It will
3327 /// strip as many layers of types as it can without changing either the type
3328 /// size or the allocated size.
3329 static Type *stripAggregateTypeWrapping(const DataLayout &DL, Type *Ty) {
3330 if (Ty->isSingleValueType())
3333 uint64_t AllocSize = DL.getTypeAllocSize(Ty);
3334 uint64_t TypeSize = DL.getTypeSizeInBits(Ty);
3337 if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) {
3338 InnerTy = ArrTy->getElementType();
3339 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
3340 const StructLayout *SL = DL.getStructLayout(STy);
3341 unsigned Index = SL->getElementContainingOffset(0);
3342 InnerTy = STy->getElementType(Index);
3347 if (AllocSize > DL.getTypeAllocSize(InnerTy) ||
3348 TypeSize > DL.getTypeSizeInBits(InnerTy))
3351 return stripAggregateTypeWrapping(DL, InnerTy);
3354 /// \brief Try to find a partition of the aggregate type passed in for a given
3355 /// offset and size.
3357 /// This recurses through the aggregate type and tries to compute a subtype
3358 /// based on the offset and size. When the offset and size span a sub-section
3359 /// of an array, it will even compute a new array type for that sub-section,
3360 /// and the same for structs.
3362 /// Note that this routine is very strict and tries to find a partition of the
3363 /// type which produces the *exact* right offset and size. It is not forgiving
3364 /// when the size or offset cause either end of type-based partition to be off.
3365 /// Also, this is a best-effort routine. It is reasonable to give up and not
3366 /// return a type if necessary.
3367 static Type *getTypePartition(const DataLayout &DL, Type *Ty, uint64_t Offset,
3369 if (Offset == 0 && DL.getTypeAllocSize(Ty) == Size)
3370 return stripAggregateTypeWrapping(DL, Ty);
3371 if (Offset > DL.getTypeAllocSize(Ty) ||
3372 (DL.getTypeAllocSize(Ty) - Offset) < Size)
3375 if (SequentialType *SeqTy = dyn_cast<SequentialType>(Ty)) {
3376 // We can't partition pointers...
3377 if (SeqTy->isPointerTy())
3380 Type *ElementTy = SeqTy->getElementType();
3381 uint64_t ElementSize = DL.getTypeAllocSize(ElementTy);
3382 uint64_t NumSkippedElements = Offset / ElementSize;
3383 if (ArrayType *ArrTy = dyn_cast<ArrayType>(SeqTy)) {
3384 if (NumSkippedElements >= ArrTy->getNumElements())
3386 } else if (VectorType *VecTy = dyn_cast<VectorType>(SeqTy)) {
3387 if (NumSkippedElements >= VecTy->getNumElements())
3390 Offset -= NumSkippedElements * ElementSize;
3392 // First check if we need to recurse.
3393 if (Offset > 0 || Size < ElementSize) {
3394 // Bail if the partition ends in a different array element.
3395 if ((Offset + Size) > ElementSize)
3397 // Recurse through the element type trying to peel off offset bytes.
3398 return getTypePartition(DL, ElementTy, Offset, Size);
3400 assert(Offset == 0);
3402 if (Size == ElementSize)
3403 return stripAggregateTypeWrapping(DL, ElementTy);
3404 assert(Size > ElementSize);
3405 uint64_t NumElements = Size / ElementSize;
3406 if (NumElements * ElementSize != Size)
3408 return ArrayType::get(ElementTy, NumElements);
3411 StructType *STy = dyn_cast<StructType>(Ty);
3415 const StructLayout *SL = DL.getStructLayout(STy);
3416 if (Offset >= SL->getSizeInBytes())
3418 uint64_t EndOffset = Offset + Size;
3419 if (EndOffset > SL->getSizeInBytes())
3422 unsigned Index = SL->getElementContainingOffset(Offset);
3423 Offset -= SL->getElementOffset(Index);
3425 Type *ElementTy = STy->getElementType(Index);
3426 uint64_t ElementSize = DL.getTypeAllocSize(ElementTy);
3427 if (Offset >= ElementSize)
3428 return nullptr; // The offset points into alignment padding.
3430 // See if any partition must be contained by the element.
3431 if (Offset > 0 || Size < ElementSize) {
3432 if ((Offset + Size) > ElementSize)
3434 return getTypePartition(DL, ElementTy, Offset, Size);
3436 assert(Offset == 0);
3438 if (Size == ElementSize)
3439 return stripAggregateTypeWrapping(DL, ElementTy);
3441 StructType::element_iterator EI = STy->element_begin() + Index,
3442 EE = STy->element_end();
3443 if (EndOffset < SL->getSizeInBytes()) {
3444 unsigned EndIndex = SL->getElementContainingOffset(EndOffset);
3445 if (Index == EndIndex)
3446 return nullptr; // Within a single element and its padding.
3448 // Don't try to form "natural" types if the elements don't line up with the
3450 // FIXME: We could potentially recurse down through the last element in the
3451 // sub-struct to find a natural end point.
3452 if (SL->getElementOffset(EndIndex) != EndOffset)
3455 assert(Index < EndIndex);
3456 EE = STy->element_begin() + EndIndex;
3459 // Try to build up a sub-structure.
3461 StructType::get(STy->getContext(), makeArrayRef(EI, EE), STy->isPacked());
3462 const StructLayout *SubSL = DL.getStructLayout(SubTy);
3463 if (Size != SubSL->getSizeInBytes())
3464 return nullptr; // The sub-struct doesn't have quite the size needed.
3469 /// \brief Pre-split loads and stores to simplify rewriting.
3471 /// We want to break up the splittable load+store pairs as much as
3472 /// possible. This is important to do as a preprocessing step, as once we
3473 /// start rewriting the accesses to partitions of the alloca we lose the
3474 /// necessary information to correctly split apart paired loads and stores
3475 /// which both point into this alloca. The case to consider is something like
3478 /// %a = alloca [12 x i8]
3479 /// %gep1 = getelementptr [12 x i8]* %a, i32 0, i32 0
3480 /// %gep2 = getelementptr [12 x i8]* %a, i32 0, i32 4
3481 /// %gep3 = getelementptr [12 x i8]* %a, i32 0, i32 8
3482 /// %iptr1 = bitcast i8* %gep1 to i64*
3483 /// %iptr2 = bitcast i8* %gep2 to i64*
3484 /// %fptr1 = bitcast i8* %gep1 to float*
3485 /// %fptr2 = bitcast i8* %gep2 to float*
3486 /// %fptr3 = bitcast i8* %gep3 to float*
3487 /// store float 0.0, float* %fptr1
3488 /// store float 1.0, float* %fptr2
3489 /// %v = load i64* %iptr1
3490 /// store i64 %v, i64* %iptr2
3491 /// %f1 = load float* %fptr2
3492 /// %f2 = load float* %fptr3
3494 /// Here we want to form 3 partitions of the alloca, each 4 bytes large, and
3495 /// promote everything so we recover the 2 SSA values that should have been
3496 /// there all along.
3498 /// \returns true if any changes are made.
3499 bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
3500 DEBUG(dbgs() << "Pre-splitting loads and stores\n");
3502 // Track the loads and stores which are candidates for pre-splitting here, in
3503 // the order they first appear during the partition scan. These give stable
3504 // iteration order and a basis for tracking which loads and stores we
3506 SmallVector<LoadInst *, 4> Loads;
3507 SmallVector<StoreInst *, 4> Stores;
3509 // We need to accumulate the splits required of each load or store where we
3510 // can find them via a direct lookup. This is important to cross-check loads
3511 // and stores against each other. We also track the slice so that we can kill
3512 // all the slices that end up split.
3513 struct SplitOffsets {
3515 std::vector<uint64_t> Splits;
3517 SmallDenseMap<Instruction *, SplitOffsets, 8> SplitOffsetsMap;
3519 DEBUG(dbgs() << " Searching for candidate loads and stores\n");
3520 for (auto &P : AS.partitions()) {
3521 for (Slice &S : P) {
3522 if (!S.isSplittable())
3524 if (S.endOffset() <= P.endOffset())
3526 assert(P.endOffset() > S.beginOffset() &&
3527 "Empty or backwards partition!");
3529 // Determine if this is a pre-splittable slice.
3530 Instruction *I = cast<Instruction>(S.getUse()->getUser());
3531 if (auto *LI = dyn_cast<LoadInst>(I)) {
3532 assert(!LI->isVolatile() && "Cannot split volatile loads!");
3534 // The load must be used exclusively to store into other pointers for
3535 // us to be able to arbitrarily pre-split it. The stores must also be
3536 // simple to avoid changing semantics.
3537 auto IsLoadSimplyStored = [](LoadInst *LI) {
3538 for (User *LU : LI->users()) {
3539 auto *SI = dyn_cast<StoreInst>(LU);
3540 if (!SI || !SI->isSimple())
3545 if (!IsLoadSimplyStored(LI))
3548 Loads.push_back(LI);
3549 } else if (auto *SI = dyn_cast<StoreInst>(S.getUse()->getUser())) {
3551 S.getUse() != &SI->getOperandUse(SI->getPointerOperandIndex()))
3553 auto *StoredLoad = dyn_cast<LoadInst>(SI->getValueOperand());
3554 if (!StoredLoad || !StoredLoad->isSimple())
3556 assert(!SI->isVolatile() && "Cannot split volatile stores!");
3558 Stores.push_back(SI);
3560 // Other uses cannot be pre-split.
3564 // Record the initial split.
3565 DEBUG(dbgs() << " Candidate: " << *I << "\n");
3566 auto &Offsets = SplitOffsetsMap[I];
3567 assert(Offsets.Splits.empty() &&
3568 "Should not have splits the first time we see an instruction!");
3570 Offsets.Splits.push_back(P.endOffset());
3573 // Now scan the already split slices, and add a split for any of them which
3574 // we're going to pre-split.
3575 for (Slice *S : P.splitSliceTails()) {
3576 auto SplitOffsetsMapI =
3577 SplitOffsetsMap.find(cast<Instruction>(S->getUse()->getUser()));
3578 if (SplitOffsetsMapI == SplitOffsetsMap.end())
3580 auto &Offsets = SplitOffsetsMapI->second;
3582 assert(Offsets.S == S && "Found a mismatched slice!");
3583 assert(!Offsets.Splits.empty() &&
3584 "Cannot have an empty set of splits on the second partition!");
3585 assert(Offsets.Splits.back() == P.beginOffset() &&
3586 "Previous split does not end where this one begins!");
3588 // Record each split. The last partition's end isn't needed as the size
3589 // of the slice dictates that.
3590 if (S->endOffset() > P.endOffset())
3591 Offsets.Splits.push_back(P.endOffset());
3595 // We may have split loads where some of their stores are split stores. For
3596 // such loads and stores, we can only pre-split them if their splits exactly
3597 // match relative to their starting offset. We have to verify this prior to
3599 SmallPtrSet<LoadInst *, 4> BadSplitLoads;
3601 std::remove_if(Stores.begin(), Stores.end(),
3602 [&BadSplitLoads, &SplitOffsetsMap](StoreInst *SI) {
3603 // Lookup the load we are storing in our map of split
3605 auto *LI = cast<LoadInst>(SI->getValueOperand());
3606 auto LoadOffsetsI = SplitOffsetsMap.find(LI);
3607 if (LoadOffsetsI == SplitOffsetsMap.end())
3608 return false; // Unrelated loads are always safe.
3609 auto &LoadOffsets = LoadOffsetsI->second;
3611 // Now lookup the store's offsets.
3612 auto &StoreOffsets = SplitOffsetsMap[SI];
3614 // If the relative offsets of each split in the load and
3615 // store match exactly, then we can split them and we
3616 // don't need to remove them here.
3617 if (LoadOffsets.Splits == StoreOffsets.Splits)
3621 << " Mismatched splits for load and store:\n"
3622 << " " << *LI << "\n"
3623 << " " << *SI << "\n");
3625 // We've found a store and load that we need to split
3626 // with mismatched relative splits. Just give up on them
3627 // and remove both instructions from our list of
3629 BadSplitLoads.insert(LI);
3633 Loads.erase(std::remove_if(Loads.begin(), Loads.end(),
3634 [&BadSplitLoads](LoadInst *LI) {
3635 return BadSplitLoads.count(LI);
3639 // If no loads or stores are left, there is no pre-splitting to be done for
3641 if (Loads.empty() && Stores.empty())
3644 // From here on, we can't fail and will be building new accesses, so rig up
3646 IRBuilderTy IRB(&AI);
3648 // Collect the new slices which we will merge into the alloca slices.
3649 SmallVector<Slice, 4> NewSlices;
3651 // Track any allocas we end up splitting loads and stores for so we iterate
3653 SmallPtrSet<AllocaInst *, 4> ResplitPromotableAllocas;
3655 // At this point, we have collected all of the loads and stores we can
3656 // pre-split, and the specific splits needed for them. We actually do the
3657 // splitting in a specific order in order to handle when one of the loads in
3658 // the value operand to one of the stores.
3660 // First, we rewrite all of the split loads, and just accumulate each split
3661 // load in a parallel structure. We also build the slices for them and append
3662 // them to the alloca slices.
3663 SmallDenseMap<LoadInst *, std::vector<LoadInst *>, 1> SplitLoadsMap;
3664 std::vector<LoadInst *> SplitLoads;
3665 for (LoadInst *LI : Loads) {
3668 IntegerType *Ty = cast<IntegerType>(LI->getType());
3669 uint64_t LoadSize = Ty->getBitWidth() / 8;
3670 assert(LoadSize > 0 && "Cannot have a zero-sized integer load!");
3672 auto &Offsets = SplitOffsetsMap[LI];
3673 assert(LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset() &&
3674 "Slice size should always match load size exactly!");
3675 uint64_t BaseOffset = Offsets.S->beginOffset();
3676 assert(BaseOffset + LoadSize > BaseOffset &&
3677 "Cannot represent alloca access size using 64-bit integers!");
3679 Instruction *BasePtr = cast<Instruction>(LI->getPointerOperand());
3680 IRB.SetInsertPoint(BasicBlock::iterator(LI));
3682 DEBUG(dbgs() << " Splitting load: " << *LI << "\n");
3684 uint64_t PartOffset = 0, PartSize = Offsets.Splits.front();
3685 int Idx = 0, Size = Offsets.Splits.size();
3687 auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8);
3688 auto *PartPtrTy = PartTy->getPointerTo(LI->getPointerAddressSpace());
3689 LoadInst *PLoad = IRB.CreateAlignedLoad(
3690 getAdjustedPtr(IRB, *DL, BasePtr,
3691 APInt(DL->getPointerSizeInBits(), PartOffset),
3692 PartPtrTy, BasePtr->getName() + "."),
3693 getAdjustedAlignment(LI, PartOffset, *DL), /*IsVolatile*/ false,
3696 // Append this load onto the list of split loads so we can find it later
3697 // to rewrite the stores.
3698 SplitLoads.push_back(PLoad);
3700 // Now build a new slice for the alloca.
3701 NewSlices.push_back(
3702 Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize,
3703 &PLoad->getOperandUse(PLoad->getPointerOperandIndex()),
3704 /*IsSplittable*/ true));
3705 DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset()
3706 << ", " << NewSlices.back().endOffset() << "): " << *PLoad
3709 // See if we've handled all the splits.
3713 // Setup the next partition.
3714 PartOffset = Offsets.Splits[Idx];
3716 PartSize = (Idx < Size ? Offsets.Splits[Idx] : LoadSize) - PartOffset;
3719 // Now that we have the split loads, do the slow walk over all uses of the
3720 // load and rewrite them as split stores, or save the split loads to use
3721 // below if the store is going to be split there anyways.
3722 bool DeferredStores = false;
3723 for (User *LU : LI->users()) {
3724 StoreInst *SI = cast<StoreInst>(LU);
3725 if (!Stores.empty() && SplitOffsetsMap.count(SI)) {
3726 DeferredStores = true;
3727 DEBUG(dbgs() << " Deferred splitting of store: " << *SI << "\n");
3731 Value *StoreBasePtr = SI->getPointerOperand();
3732 IRB.SetInsertPoint(BasicBlock::iterator(SI));
3734 DEBUG(dbgs() << " Splitting store of load: " << *SI << "\n");
3736 for (int Idx = 0, Size = SplitLoads.size(); Idx < Size; ++Idx) {
3737 LoadInst *PLoad = SplitLoads[Idx];
3738 uint64_t PartOffset = Idx == 0 ? 0 : Offsets.Splits[Idx - 1];
3740 PLoad->getType()->getPointerTo(SI->getPointerAddressSpace());
3742 StoreInst *PStore = IRB.CreateAlignedStore(
3743 PLoad, getAdjustedPtr(IRB, *DL, StoreBasePtr,
3744 APInt(DL->getPointerSizeInBits(), PartOffset),
3745 PartPtrTy, StoreBasePtr->getName() + "."),
3746 getAdjustedAlignment(SI, PartOffset, *DL), /*IsVolatile*/ false);
3748 DEBUG(dbgs() << " +" << PartOffset << ":" << *PStore << "\n");
3751 // We want to immediately iterate on any allocas impacted by splitting
3752 // this store, and we have to track any promotable alloca (indicated by
3753 // a direct store) as needing to be resplit because it is no longer
3755 if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(StoreBasePtr)) {
3756 ResplitPromotableAllocas.insert(OtherAI);
3757 Worklist.insert(OtherAI);
3758 } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(
3759 StoreBasePtr->stripInBoundsOffsets())) {
3760 Worklist.insert(OtherAI);
3763 // Mark the original store as dead.
3764 DeadInsts.insert(SI);
3767 // Save the split loads if there are deferred stores among the users.
3769 SplitLoadsMap.insert(std::make_pair(LI, std::move(SplitLoads)));
3771 // Mark the original load as dead and kill the original slice.
3772 DeadInsts.insert(LI);
3776 // Second, we rewrite all of the split stores. At this point, we know that
3777 // all loads from this alloca have been split already. For stores of such
3778 // loads, we can simply look up the pre-existing split loads. For stores of
3779 // other loads, we split those loads first and then write split stores of
3781 for (StoreInst *SI : Stores) {
3782 auto *LI = cast<LoadInst>(SI->getValueOperand());
3783 IntegerType *Ty = cast<IntegerType>(LI->getType());
3784 uint64_t StoreSize = Ty->getBitWidth() / 8;
3785 assert(StoreSize > 0 && "Cannot have a zero-sized integer store!");
3787 auto &Offsets = SplitOffsetsMap[SI];
3788 assert(StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() &&
3789 "Slice size should always match load size exactly!");
3790 uint64_t BaseOffset = Offsets.S->beginOffset();
3791 assert(BaseOffset + StoreSize > BaseOffset &&
3792 "Cannot represent alloca access size using 64-bit integers!");
3794 Value *LoadBasePtr = LI->getPointerOperand();
3795 Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand());
3797 DEBUG(dbgs() << " Splitting store: " << *SI << "\n");
3799 // Check whether we have an already split load.
3800 auto SplitLoadsMapI = SplitLoadsMap.find(LI);
3801 std::vector<LoadInst *> *SplitLoads = nullptr;
3802 if (SplitLoadsMapI != SplitLoadsMap.end()) {
3803 SplitLoads = &SplitLoadsMapI->second;
3804 assert(SplitLoads->size() == Offsets.Splits.size() + 1 &&
3805 "Too few split loads for the number of splits in the store!");
3807 DEBUG(dbgs() << " of load: " << *LI << "\n");
3810 uint64_t PartOffset = 0, PartSize = Offsets.Splits.front();
3811 int Idx = 0, Size = Offsets.Splits.size();
3813 auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8);
3814 auto *PartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace());
3816 // Either lookup a split load or create one.
3819 PLoad = (*SplitLoads)[Idx];
3821 IRB.SetInsertPoint(BasicBlock::iterator(LI));
3822 PLoad = IRB.CreateAlignedLoad(
3823 getAdjustedPtr(IRB, *DL, LoadBasePtr,
3824 APInt(DL->getPointerSizeInBits(), PartOffset),
3825 PartPtrTy, LoadBasePtr->getName() + "."),
3826 getAdjustedAlignment(LI, PartOffset, *DL), /*IsVolatile*/ false,
3830 // And store this partition.
3831 IRB.SetInsertPoint(BasicBlock::iterator(SI));
3832 StoreInst *PStore = IRB.CreateAlignedStore(
3833 PLoad, getAdjustedPtr(IRB, *DL, StoreBasePtr,
3834 APInt(DL->getPointerSizeInBits(), PartOffset),
3835 PartPtrTy, StoreBasePtr->getName() + "."),
3836 getAdjustedAlignment(SI, PartOffset, *DL), /*IsVolatile*/ false);
3838 // Now build a new slice for the alloca.
3839 NewSlices.push_back(
3840 Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize,
3841 &PStore->getOperandUse(PStore->getPointerOperandIndex()),
3842 /*IsSplittable*/ true));
3843 DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset()
3844 << ", " << NewSlices.back().endOffset() << "): " << *PStore
3847 DEBUG(dbgs() << " of split load: " << *PLoad << "\n");
3850 // See if we've finished all the splits.
3854 // Setup the next partition.
3855 PartOffset = Offsets.Splits[Idx];
3857 PartSize = (Idx < Size ? Offsets.Splits[Idx] : StoreSize) - PartOffset;
3860 // We want to immediately iterate on any allocas impacted by splitting
3861 // this load, which is only relevant if it isn't a load of this alloca and
3862 // thus we didn't already split the loads above. We also have to keep track
3863 // of any promotable allocas we split loads on as they can no longer be
3866 if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(LoadBasePtr)) {
3867 assert(OtherAI != &AI && "We can't re-split our own alloca!");
3868 ResplitPromotableAllocas.insert(OtherAI);
3869 Worklist.insert(OtherAI);
3870 } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(
3871 LoadBasePtr->stripInBoundsOffsets())) {
3872 assert(OtherAI != &AI && "We can't re-split our own alloca!");
3873 Worklist.insert(OtherAI);
3877 // Mark the original store as dead now that we've split it up and kill its
3878 // slice. Note that we leave the original load in place. It may in turn be
3879 // split up if it is an alloca load for some other alloca, but it may be
3880 // a normal load. This may introduce redundant loads, but where those can
3881 // be merged the rest of the optimizer should handle the merging, and this
3882 // uncovers SSA splits which is more important. In practice, the original
3883 // loads will almost always be fully split and removed eventually, and the
3884 // splits will be merged by any trivial CSE, including instcombine.
3885 DeadInsts.insert(SI);
3889 // Now we need to remove the killed slices, sort the newly added slices, and
3890 // merge the two sorted ranges of slices so that the entire range is sorted
3891 // properly for us to re-compute the partitions.
3892 AS.erase(std::remove_if(AS.begin(), AS.end(), [](const Slice &S) {
3896 AS.insert(NewSlices);
3898 DEBUG(dbgs() << " Pre-split slices:\n");
3900 for (auto I = AS.begin(), E = AS.end(); I != E; ++I)
3901 DEBUG(AS.print(dbgs(), I, " "));
3904 // Finally, don't try to promote any allocas that new require re-splitting.
3905 // They have already been added to the worklist above.
3906 PromotableAllocas.erase(
3908 PromotableAllocas.begin(), PromotableAllocas.end(),
3909 [&](AllocaInst *AI) { return ResplitPromotableAllocas.count(AI); }),
3910 PromotableAllocas.end());
3915 /// \brief Rewrite an alloca partition's users.
3917 /// This routine drives both of the rewriting goals of the SROA pass. It tries
3918 /// to rewrite uses of an alloca partition to be conducive for SSA value
3919 /// promotion. If the partition needs a new, more refined alloca, this will
3920 /// build that new alloca, preserving as much type information as possible, and
3921 /// rewrite the uses of the old alloca to point at the new one and have the
3922 /// appropriate new offsets. It also evaluates how successful the rewrite was
3923 /// at enabling promotion and if it was successful queues the alloca to be
3925 bool SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
3926 AllocaSlices::Partition &P) {
3927 // Try to compute a friendly type for this partition of the alloca. This
3928 // won't always succeed, in which case we fall back to a legal integer type
3929 // or an i8 array of an appropriate size.
3930 Type *SliceTy = nullptr;
3931 if (Type *CommonUseTy = findCommonType(P.begin(), P.end(), P.endOffset()))
3932 if (DL->getTypeAllocSize(CommonUseTy) >= P.size())
3933 SliceTy = CommonUseTy;
3935 if (Type *TypePartitionTy = getTypePartition(*DL, AI.getAllocatedType(),
3936 P.beginOffset(), P.size()))
3937 SliceTy = TypePartitionTy;
3938 if ((!SliceTy || (SliceTy->isArrayTy() &&
3939 SliceTy->getArrayElementType()->isIntegerTy())) &&
3940 DL->isLegalInteger(P.size() * 8))
3941 SliceTy = Type::getIntNTy(*C, P.size() * 8);
3943 SliceTy = ArrayType::get(Type::getInt8Ty(*C), P.size());
3944 assert(DL->getTypeAllocSize(SliceTy) >= P.size());
3946 bool IsIntegerPromotable = isIntegerWideningViable(P, SliceTy, *DL);
3949 IsIntegerPromotable ? nullptr : isVectorPromotionViable(P, *DL);
3953 // Check for the case where we're going to rewrite to a new alloca of the
3954 // exact same type as the original, and with the same access offsets. In that
3955 // case, re-use the existing alloca, but still run through the rewriter to
3956 // perform phi and select speculation.
3958 if (SliceTy == AI.getAllocatedType()) {
3959 assert(P.beginOffset() == 0 &&
3960 "Non-zero begin offset but same alloca type");
3962 // FIXME: We should be able to bail at this point with "nothing changed".
3963 // FIXME: We might want to defer PHI speculation until after here.
3965 unsigned Alignment = AI.getAlignment();
3967 // The minimum alignment which users can rely on when the explicit
3968 // alignment is omitted or zero is that required by the ABI for this
3970 Alignment = DL->getABITypeAlignment(AI.getAllocatedType());
3972 Alignment = MinAlign(Alignment, P.beginOffset());
3973 // If we will get at least this much alignment from the type alone, leave
3974 // the alloca's alignment unconstrained.
3975 if (Alignment <= DL->getABITypeAlignment(SliceTy))
3977 NewAI = new AllocaInst(
3978 SliceTy, nullptr, Alignment,
3979 AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()), &AI);
3983 DEBUG(dbgs() << "Rewriting alloca partition "
3984 << "[" << P.beginOffset() << "," << P.endOffset()
3985 << ") to: " << *NewAI << "\n");
3987 // Track the high watermark on the worklist as it is only relevant for
3988 // promoted allocas. We will reset it to this point if the alloca is not in
3989 // fact scheduled for promotion.
3990 unsigned PPWOldSize = PostPromotionWorklist.size();
3991 unsigned NumUses = 0;
3992 SmallPtrSet<PHINode *, 8> PHIUsers;
3993 SmallPtrSet<SelectInst *, 8> SelectUsers;
3995 AllocaSliceRewriter Rewriter(*DL, AS, *this, AI, *NewAI, P.beginOffset(),
3996 P.endOffset(), IsIntegerPromotable, VecTy,
3997 PHIUsers, SelectUsers);
3998 bool Promotable = true;
3999 for (Slice *S : P.splitSliceTails()) {
4000 Promotable &= Rewriter.visit(S);
4003 for (Slice &S : P) {
4004 Promotable &= Rewriter.visit(&S);
4008 NumAllocaPartitionUses += NumUses;
4009 MaxUsesPerAllocaPartition =
4010 std::max<unsigned>(NumUses, MaxUsesPerAllocaPartition);
4012 // Now that we've processed all the slices in the new partition, check if any
4013 // PHIs or Selects would block promotion.
4014 for (SmallPtrSetImpl<PHINode *>::iterator I = PHIUsers.begin(),
4017 if (!isSafePHIToSpeculate(**I, DL)) {
4020 SelectUsers.clear();
4023 for (SmallPtrSetImpl<SelectInst *>::iterator I = SelectUsers.begin(),
4024 E = SelectUsers.end();
4026 if (!isSafeSelectToSpeculate(**I, DL)) {
4029 SelectUsers.clear();
4034 if (PHIUsers.empty() && SelectUsers.empty()) {
4035 // Promote the alloca.
4036 PromotableAllocas.push_back(NewAI);
4038 // If we have either PHIs or Selects to speculate, add them to those
4039 // worklists and re-queue the new alloca so that we promote in on the
4041 for (PHINode *PHIUser : PHIUsers)
4042 SpeculatablePHIs.insert(PHIUser);
4043 for (SelectInst *SelectUser : SelectUsers)
4044 SpeculatableSelects.insert(SelectUser);
4045 Worklist.insert(NewAI);
4048 // If we can't promote the alloca, iterate on it to check for new
4049 // refinements exposed by splitting the current alloca. Don't iterate on an
4050 // alloca which didn't actually change and didn't get promoted.
4052 Worklist.insert(NewAI);
4054 // Drop any post-promotion work items if promotion didn't happen.
4055 while (PostPromotionWorklist.size() > PPWOldSize)
4056 PostPromotionWorklist.pop_back();
4062 /// \brief Walks the slices of an alloca and form partitions based on them,
4063 /// rewriting each of their uses.
4064 bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
4065 if (AS.begin() == AS.end())
4068 unsigned NumPartitions = 0;
4069 bool Changed = false;
4071 Changed |= presplitLoadsAndStores(AI, AS);
4073 // Rewrite each partition.
4074 for (auto &P : AS.partitions()) {
4075 Changed |= rewritePartition(AI, AS, P);
4079 NumAllocaPartitions += NumPartitions;
4080 MaxPartitionsPerAlloca =
4081 std::max<unsigned>(NumPartitions, MaxPartitionsPerAlloca);
4086 /// \brief Clobber a use with undef, deleting the used value if it becomes dead.
4087 void SROA::clobberUse(Use &U) {
4089 // Replace the use with an undef value.
4090 U = UndefValue::get(OldV->getType());
4092 // Check for this making an instruction dead. We have to garbage collect
4093 // all the dead instructions to ensure the uses of any alloca end up being
4095 if (Instruction *OldI = dyn_cast<Instruction>(OldV))
4096 if (isInstructionTriviallyDead(OldI)) {
4097 DeadInsts.insert(OldI);
4101 /// \brief Analyze an alloca for SROA.
4103 /// This analyzes the alloca to ensure we can reason about it, builds
4104 /// the slices of the alloca, and then hands it off to be split and
4105 /// rewritten as needed.
4106 bool SROA::runOnAlloca(AllocaInst &AI) {
4107 DEBUG(dbgs() << "SROA alloca: " << AI << "\n");
4108 ++NumAllocasAnalyzed;
4110 // Special case dead allocas, as they're trivial.
4111 if (AI.use_empty()) {
4112 AI.eraseFromParent();
4116 // Skip alloca forms that this analysis can't handle.
4117 if (AI.isArrayAllocation() || !AI.getAllocatedType()->isSized() ||
4118 DL->getTypeAllocSize(AI.getAllocatedType()) == 0)
4121 bool Changed = false;
4123 // First, split any FCA loads and stores touching this alloca to promote
4124 // better splitting and promotion opportunities.
4125 AggLoadStoreRewriter AggRewriter(*DL);
4126 Changed |= AggRewriter.rewrite(AI);
4128 // Build the slices using a recursive instruction-visiting builder.
4129 AllocaSlices AS(*DL, AI);
4130 DEBUG(AS.print(dbgs()));
4134 // Delete all the dead users of this alloca before splitting and rewriting it.
4135 for (Instruction *DeadUser : AS.getDeadUsers()) {
4136 // Free up everything used by this instruction.
4137 for (Use &DeadOp : DeadUser->operands())
4140 // Now replace the uses of this instruction.
4141 DeadUser->replaceAllUsesWith(UndefValue::get(DeadUser->getType()));
4143 // And mark it for deletion.
4144 DeadInsts.insert(DeadUser);
4147 for (Use *DeadOp : AS.getDeadOperands()) {
4148 clobberUse(*DeadOp);
4152 // No slices to split. Leave the dead alloca for a later pass to clean up.
4153 if (AS.begin() == AS.end())
4156 Changed |= splitAlloca(AI, AS);
4158 DEBUG(dbgs() << " Speculating PHIs\n");
4159 while (!SpeculatablePHIs.empty())
4160 speculatePHINodeLoads(*SpeculatablePHIs.pop_back_val());
4162 DEBUG(dbgs() << " Speculating Selects\n");
4163 while (!SpeculatableSelects.empty())
4164 speculateSelectInstLoads(*SpeculatableSelects.pop_back_val());
4169 /// \brief Delete the dead instructions accumulated in this run.
4171 /// Recursively deletes the dead instructions we've accumulated. This is done
4172 /// at the very end to maximize locality of the recursive delete and to
4173 /// minimize the problems of invalidated instruction pointers as such pointers
4174 /// are used heavily in the intermediate stages of the algorithm.
4176 /// We also record the alloca instructions deleted here so that they aren't
4177 /// subsequently handed to mem2reg to promote.
4178 void SROA::deleteDeadInstructions(
4179 SmallPtrSetImpl<AllocaInst *> &DeletedAllocas) {
4180 while (!DeadInsts.empty()) {
4181 Instruction *I = DeadInsts.pop_back_val();
4182 DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n");
4184 I->replaceAllUsesWith(UndefValue::get(I->getType()));
4186 for (Use &Operand : I->operands())
4187 if (Instruction *U = dyn_cast<Instruction>(Operand)) {
4188 // Zero out the operand and see if it becomes trivially dead.
4190 if (isInstructionTriviallyDead(U))
4191 DeadInsts.insert(U);
4194 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
4195 DeletedAllocas.insert(AI);
4198 I->eraseFromParent();
4202 static void enqueueUsersInWorklist(Instruction &I,
4203 SmallVectorImpl<Instruction *> &Worklist,
4204 SmallPtrSetImpl<Instruction *> &Visited) {
4205 for (User *U : I.users())
4206 if (Visited.insert(cast<Instruction>(U)).second)
4207 Worklist.push_back(cast<Instruction>(U));
4210 /// \brief Promote the allocas, using the best available technique.
4212 /// This attempts to promote whatever allocas have been identified as viable in
4213 /// the PromotableAllocas list. If that list is empty, there is nothing to do.
4214 /// If there is a domtree available, we attempt to promote using the full power
4215 /// of mem2reg. Otherwise, we build and use the AllocaPromoter above which is
4216 /// based on the SSAUpdater utilities. This function returns whether any
4217 /// promotion occurred.
4218 bool SROA::promoteAllocas(Function &F) {
4219 if (PromotableAllocas.empty())
4222 NumPromoted += PromotableAllocas.size();
4224 if (DT && !ForceSSAUpdater) {
4225 DEBUG(dbgs() << "Promoting allocas with mem2reg...\n");
4226 PromoteMemToReg(PromotableAllocas, *DT, nullptr, AT);
4227 PromotableAllocas.clear();
4231 DEBUG(dbgs() << "Promoting allocas with SSAUpdater...\n");
4233 DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
4234 SmallVector<Instruction *, 64> Insts;
4236 // We need a worklist to walk the uses of each alloca.
4237 SmallVector<Instruction *, 8> Worklist;
4238 SmallPtrSet<Instruction *, 8> Visited;
4239 SmallVector<Instruction *, 32> DeadInsts;
4241 for (unsigned Idx = 0, Size = PromotableAllocas.size(); Idx != Size; ++Idx) {
4242 AllocaInst *AI = PromotableAllocas[Idx];
4247 enqueueUsersInWorklist(*AI, Worklist, Visited);
4249 while (!Worklist.empty()) {
4250 Instruction *I = Worklist.pop_back_val();
4252 // FIXME: Currently the SSAUpdater infrastructure doesn't reason about
4253 // lifetime intrinsics and so we strip them (and the bitcasts+GEPs
4254 // leading to them) here. Eventually it should use them to optimize the
4255 // scalar values produced.
4256 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
4257 assert(II->getIntrinsicID() == Intrinsic::lifetime_start ||
4258 II->getIntrinsicID() == Intrinsic::lifetime_end);
4259 II->eraseFromParent();
4263 // Push the loads and stores we find onto the list. SROA will already
4264 // have validated that all loads and stores are viable candidates for
4266 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
4267 assert(LI->getType() == AI->getAllocatedType());
4268 Insts.push_back(LI);
4271 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
4272 assert(SI->getValueOperand()->getType() == AI->getAllocatedType());
4273 Insts.push_back(SI);
4277 // For everything else, we know that only no-op bitcasts and GEPs will
4278 // make it this far, just recurse through them and recall them for later
4280 DeadInsts.push_back(I);
4281 enqueueUsersInWorklist(*I, Worklist, Visited);
4283 AllocaPromoter(Insts, SSA, *AI, DIB).run(Insts);
4284 while (!DeadInsts.empty())
4285 DeadInsts.pop_back_val()->eraseFromParent();
4286 AI->eraseFromParent();
4289 PromotableAllocas.clear();
4293 bool SROA::runOnFunction(Function &F) {
4294 if (skipOptnoneFunction(F))
4297 DEBUG(dbgs() << "SROA function: " << F.getName() << "\n");
4298 C = &F.getContext();
4299 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
4301 DEBUG(dbgs() << " Skipping SROA -- no target data!\n");
4304 DL = &DLP->getDataLayout();
4305 DominatorTreeWrapperPass *DTWP =
4306 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
4307 DT = DTWP ? &DTWP->getDomTree() : nullptr;
4308 AT = &getAnalysis<AssumptionTracker>();
4310 BasicBlock &EntryBB = F.getEntryBlock();
4311 for (BasicBlock::iterator I = EntryBB.begin(), E = std::prev(EntryBB.end());
4313 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
4314 Worklist.insert(AI);
4316 bool Changed = false;
4317 // A set of deleted alloca instruction pointers which should be removed from
4318 // the list of promotable allocas.
4319 SmallPtrSet<AllocaInst *, 4> DeletedAllocas;
4322 while (!Worklist.empty()) {
4323 Changed |= runOnAlloca(*Worklist.pop_back_val());
4324 deleteDeadInstructions(DeletedAllocas);
4326 // Remove the deleted allocas from various lists so that we don't try to
4327 // continue processing them.
4328 if (!DeletedAllocas.empty()) {
4329 auto IsInSet = [&](AllocaInst *AI) { return DeletedAllocas.count(AI); };
4330 Worklist.remove_if(IsInSet);
4331 PostPromotionWorklist.remove_if(IsInSet);
4332 PromotableAllocas.erase(std::remove_if(PromotableAllocas.begin(),
4333 PromotableAllocas.end(),
4335 PromotableAllocas.end());
4336 DeletedAllocas.clear();
4340 Changed |= promoteAllocas(F);
4342 Worklist = PostPromotionWorklist;
4343 PostPromotionWorklist.clear();
4344 } while (!Worklist.empty());
4349 void SROA::getAnalysisUsage(AnalysisUsage &AU) const {
4350 AU.addRequired<AssumptionTracker>();
4351 if (RequiresDomTree)
4352 AU.addRequired<DominatorTreeWrapperPass>();
4353 AU.setPreservesCFG();