LLVM 24.0.0git
SROA.cpp
Go to the documentation of this file.
1//===- SROA.cpp - Scalar Replacement Of Aggregates ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This transformation implements the well known scalar replacement of
10/// aggregates transformation. It tries to identify promotable elements of an
11/// aggregate alloca, and promote them to registers. It will also try to
12/// convert uses of an element (or set of elements) of an alloca into a vector
13/// or bitfield-style integer scalar if appropriate.
14///
15/// It works to do this with minimal slicing of the alloca so that regions
16/// which are merely transferred in and out of external memory remain unchanged
17/// and are not decomposed to scalar code.
18///
19/// Because this also performs alloca promotion, it can be thought of as also
20/// serving the purpose of SSA formation. The algorithm iterates on the
21/// function until all opportunities for promotion have been realized.
22///
23//===----------------------------------------------------------------------===//
24
26#include "llvm/ADT/APInt.h"
27#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/MapVector.h"
31#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/SetVector.h"
36#include "llvm/ADT/Statistic.h"
37#include "llvm/ADT/StringRef.h"
38#include "llvm/ADT/Twine.h"
39#include "llvm/ADT/iterator.h"
44#include "llvm/Analysis/Loads.h"
48#include "llvm/IR/BasicBlock.h"
49#include "llvm/IR/Constant.h"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DIBuilder.h"
53#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/DebugInfo.h"
57#include "llvm/IR/Dominators.h"
58#include "llvm/IR/Function.h"
59#include "llvm/IR/GlobalAlias.h"
60#include "llvm/IR/IRBuilder.h"
61#include "llvm/IR/InstVisitor.h"
62#include "llvm/IR/Instruction.h"
65#include "llvm/IR/LLVMContext.h"
66#include "llvm/IR/Metadata.h"
67#include "llvm/IR/Module.h"
68#include "llvm/IR/Operator.h"
69#include "llvm/IR/PassManager.h"
70#include "llvm/IR/Type.h"
71#include "llvm/IR/Use.h"
72#include "llvm/IR/User.h"
73#include "llvm/IR/Value.h"
74#include "llvm/IR/ValueHandle.h"
76#include "llvm/Pass.h"
80#include "llvm/Support/Debug.h"
88#include <algorithm>
89#include <cassert>
90#include <cstddef>
91#include <cstdint>
92#include <cstring>
93#include <iterator>
94#include <string>
95#include <tuple>
96#include <utility>
97#include <variant>
98#include <vector>
99
100using namespace llvm;
101
102#define DEBUG_TYPE "sroa"
103
104STATISTIC(NumAllocasAnalyzed, "Number of allocas analyzed for replacement");
105STATISTIC(NumAllocaPartitions, "Number of alloca partitions formed");
106STATISTIC(MaxPartitionsPerAlloca, "Maximum number of partitions per alloca");
107STATISTIC(NumAllocaPartitionUses, "Number of alloca partition uses rewritten");
108STATISTIC(MaxUsesPerAllocaPartition, "Maximum number of uses of a partition");
109STATISTIC(NumNewAllocas, "Number of new, smaller allocas introduced");
110STATISTIC(NumPromoted, "Number of allocas promoted to SSA values");
111STATISTIC(NumLoadsSpeculated, "Number of loads speculated to allow promotion");
112STATISTIC(NumLoadsPredicated,
113 "Number of loads rewritten into predicated loads to allow promotion");
115 NumStoresPredicated,
116 "Number of stores rewritten into predicated loads to allow promotion");
117STATISTIC(NumDeleted, "Number of instructions deleted");
118STATISTIC(NumVectorized, "Number of vectorized aggregates");
119
120namespace llvm {
121/// Disable running mem2reg during SROA in order to test or debug SROA.
122static cl::opt<bool> SROASkipMem2Reg("sroa-skip-mem2reg", cl::init(false),
123 cl::Hidden);
124} // namespace llvm
125
126namespace {
127
128class AllocaSliceRewriter;
129class AllocaSlices;
130class Partition;
131
132class SelectHandSpeculativity {
133 unsigned char Storage = 0; // None are speculatable by default.
134 using TrueVal = Bitfield::Element<bool, 0, 1>; // Low 0'th bit.
135 using FalseVal = Bitfield::Element<bool, 1, 1>; // Low 1'th bit.
136public:
137 SelectHandSpeculativity() = default;
138 SelectHandSpeculativity &setAsSpeculatable(bool isTrueVal);
139 bool isSpeculatable(bool isTrueVal) const;
140 bool areAllSpeculatable() const;
141 bool areAnySpeculatable() const;
142 bool areNoneSpeculatable() const;
143 // For interop as int half of PointerIntPair.
144 explicit operator intptr_t() const { return static_cast<intptr_t>(Storage); }
145 explicit SelectHandSpeculativity(intptr_t Storage_) : Storage(Storage_) {}
146};
147static_assert(sizeof(SelectHandSpeculativity) == sizeof(unsigned char));
148
149using PossiblySpeculatableLoad =
151using UnspeculatableStore = StoreInst *;
152using RewriteableMemOp =
153 std::variant<PossiblySpeculatableLoad, UnspeculatableStore>;
154using RewriteableMemOps = SmallVector<RewriteableMemOp, 2>;
155
156/// An optimization pass providing Scalar Replacement of Aggregates.
157///
158/// This pass takes allocations which can be completely analyzed (that is, they
159/// don't escape) and tries to turn them into scalar SSA values. There are
160/// a few steps to this process.
161///
162/// 1) It takes allocations of aggregates and analyzes the ways in which they
163/// are used to try to split them into smaller allocations, ideally of
164/// a single scalar data type. It will split up memcpy and memset accesses
165/// as necessary and try to isolate individual scalar accesses.
166/// 2) It will transform accesses into forms which are suitable for SSA value
167/// promotion. This can be replacing a memset with a scalar store of an
168/// integer value, or it can involve speculating operations on a PHI or
169/// select to be a PHI or select of the results.
170/// 3) Finally, this will try to detect a pattern of accesses which map cleanly
171/// onto insert and extract operations on a vector value, and convert them to
172/// this form. By doing so, it will enable promotion of vector aggregates to
173/// SSA vector values.
174class SROA {
175 LLVMContext *const C;
176 DomTreeUpdater *const DTU;
177 AssumptionCache *const AC;
178 const bool PreserveCFG;
179 const bool AggregateToVector;
180
181 /// Worklist of alloca instructions to simplify.
182 ///
183 /// Each alloca in the function is added to this. Each new alloca formed gets
184 /// added to it as well to recursively simplify unless that alloca can be
185 /// directly promoted. Finally, each time we rewrite a use of an alloca other
186 /// the one being actively rewritten, we add it back onto the list if not
187 /// already present to ensure it is re-visited.
188 SmallSetVector<AllocaInst *, 16> Worklist;
189
190 /// A collection of instructions to delete.
191 /// We try to batch deletions to simplify code and make things a bit more
192 /// efficient. We also make sure there is no dangling pointers.
193 SmallVector<WeakVH, 8> DeadInsts;
194
195 /// Post-promotion worklist.
196 ///
197 /// Sometimes we discover an alloca which has a high probability of becoming
198 /// viable for SROA after a round of promotion takes place. In those cases,
199 /// the alloca is enqueued here for re-processing.
200 ///
201 /// Note that we have to be very careful to clear allocas out of this list in
202 /// the event they are deleted.
203 SmallSetVector<AllocaInst *, 16> PostPromotionWorklist;
204
205 /// A collection of alloca instructions we can directly promote.
206 SetVector<AllocaInst *, SmallVector<AllocaInst *>,
207 SmallPtrSet<AllocaInst *, 16>, 16>
208 PromotableAllocas;
209
210 /// A worklist of PHIs to speculate prior to promoting allocas.
211 ///
212 /// All of these PHIs have been checked for the safety of speculation and by
213 /// being speculated will allow promoting allocas currently in the promotable
214 /// queue.
215 SmallSetVector<PHINode *, 8> SpeculatablePHIs;
216
217 /// A worklist of select instructions to rewrite prior to promoting
218 /// allocas.
219 SmallMapVector<SelectInst *, RewriteableMemOps, 8> SelectsToRewrite;
220
221 /// Select instructions that use an alloca and are subsequently loaded can be
222 /// rewritten to load both input pointers and then select between the result,
223 /// allowing the load of the alloca to be promoted.
224 /// From this:
225 /// %P2 = select i1 %cond, ptr %Alloca, ptr %Other
226 /// %V = load <type>, ptr %P2
227 /// to:
228 /// %V1 = load <type>, ptr %Alloca -> will be mem2reg'd
229 /// %V2 = load <type>, ptr %Other
230 /// %V = select i1 %cond, <type> %V1, <type> %V2
231 ///
232 /// We can do this to a select if its only uses are loads
233 /// and if either the operand to the select can be loaded unconditionally,
234 /// or if we are allowed to perform CFG modifications.
235 static std::optional<RewriteableMemOps>
236 isSafeSelectToSpeculate(SelectInst &SI, bool PreserveCFG);
237
238public:
239 SROA(LLVMContext *C, DomTreeUpdater *DTU, AssumptionCache *AC,
240 SROAOptions Options)
241 : C(C), DTU(DTU), AC(AC),
242 PreserveCFG(Options.CFG == SROAOptions::PreserveCFG),
243 AggregateToVector(Options.AggregateToVector) {}
244
245 /// Main run method used by both the SROAPass and by the legacy pass.
246 std::pair<bool /*Changed*/, bool /*CFGChanged*/> runSROA(Function &F);
247
248private:
249 friend class AllocaSliceRewriter;
250
251 bool presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS);
252 std::pair<AllocaInst *, uint64_t>
253 rewritePartition(AllocaInst &AI, AllocaSlices &AS, Partition &P);
254 bool splitAlloca(AllocaInst &AI, AllocaSlices &AS);
255 bool propagateStoredValuesToLoads(AllocaInst &AI, AllocaSlices &AS);
256 std::pair<bool /*Changed*/, bool /*CFGChanged*/> runOnAlloca(AllocaInst &AI);
257 void clobberUse(Use &U);
258 bool deleteDeadInstructions(SmallPtrSetImpl<AllocaInst *> &DeletedAllocas);
259 bool promoteAllocas();
260};
261
262} // end anonymous namespace
263
264/// Calculate the fragment of a variable to use when slicing a store
265/// based on the slice dimensions, existing fragment, and base storage
266/// fragment.
267/// Results:
268/// UseFrag - Use Target as the new fragment.
269/// UseNoFrag - The new slice already covers the whole variable.
270/// Skip - The new alloca slice doesn't include this variable.
271/// FIXME: Can we use calculateFragmentIntersect instead?
272namespace {
273enum FragCalcResult { UseFrag, UseNoFrag, Skip };
274}
275static FragCalcResult
277 uint64_t NewStorageSliceOffsetInBits,
278 uint64_t NewStorageSliceSizeInBits,
279 std::optional<DIExpression::FragmentInfo> StorageFragment,
280 std::optional<DIExpression::FragmentInfo> CurrentFragment,
282 // If the base storage describes part of the variable apply the offset and
283 // the size constraint.
284 if (StorageFragment) {
285 Target.SizeInBits =
286 std::min(NewStorageSliceSizeInBits, StorageFragment->SizeInBits);
287 Target.OffsetInBits =
288 NewStorageSliceOffsetInBits + StorageFragment->OffsetInBits;
289 } else {
290 Target.SizeInBits = NewStorageSliceSizeInBits;
291 Target.OffsetInBits = NewStorageSliceOffsetInBits;
292 }
293
294 // If this slice extracts the entirety of an independent variable from a
295 // larger alloca, do not produce a fragment expression, as the variable is
296 // not fragmented.
297 if (!CurrentFragment) {
298 if (auto Size = Variable->getSizeInBits()) {
299 // Treat the current fragment as covering the whole variable.
300 CurrentFragment = DIExpression::FragmentInfo(*Size, 0);
301 if (Target == CurrentFragment)
302 return UseNoFrag;
303 }
304 }
305
306 // No additional work to do if there isn't a fragment already, or there is
307 // but it already exactly describes the new assignment.
308 if (!CurrentFragment || *CurrentFragment == Target)
309 return UseFrag;
310
311 // Reject the target fragment if it doesn't fit wholly within the current
312 // fragment. TODO: We could instead chop up the target to fit in the case of
313 // a partial overlap.
314 if (Target.startInBits() < CurrentFragment->startInBits() ||
315 Target.endInBits() > CurrentFragment->endInBits())
316 return Skip;
317
318 // Target fits within the current fragment, return it.
319 return UseFrag;
320}
321
323 return DebugVariable(DVR->getVariable(), std::nullopt,
324 DVR->getDebugLoc().getInlinedAt());
325}
326
327/// Find linked dbg.assign and generate a new one with the correct
328/// FragmentInfo. Link Inst to the new dbg.assign. If Value is nullptr the
329/// value component is copied from the old dbg.assign to the new.
330/// \param OldAlloca Alloca for the variable before splitting.
331/// \param IsSplit True if the store (not necessarily alloca)
332/// is being split.
333/// \param OldAllocaOffsetInBits Offset of the slice taken from OldAlloca.
334/// \param SliceSizeInBits New number of bits being written to.
335/// \param OldInst Instruction that is being split.
336/// \param Inst New instruction performing this part of the
337/// split store.
338/// \param Dest Store destination.
339/// \param Value Stored value.
340/// \param DL Datalayout.
341static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit,
342 uint64_t OldAllocaOffsetInBits,
343 uint64_t SliceSizeInBits, Instruction *OldInst,
344 Instruction *Inst, Value *Dest, Value *Value,
345 const DataLayout &DL) {
346 // If we want allocas to be migrated using this helper then we need to ensure
347 // that the BaseFragments map code still works. A simple solution would be
348 // to choose to always clone alloca dbg_assigns (rather than sometimes
349 // "stealing" them).
350 assert(!isa<AllocaInst>(Inst) && "Unexpected alloca");
351
352 auto DVRAssignMarkerRange = at::getDVRAssignmentMarkers(OldInst);
353 // Nothing to do if OldInst has no linked dbg.assign intrinsics.
354 if (DVRAssignMarkerRange.empty())
355 return;
356
357 LLVM_DEBUG(dbgs() << " migrateDebugInfo\n");
358 LLVM_DEBUG(dbgs() << " OldAlloca: " << *OldAlloca << "\n");
359 LLVM_DEBUG(dbgs() << " IsSplit: " << IsSplit << "\n");
360 LLVM_DEBUG(dbgs() << " OldAllocaOffsetInBits: " << OldAllocaOffsetInBits
361 << "\n");
362 LLVM_DEBUG(dbgs() << " SliceSizeInBits: " << SliceSizeInBits << "\n");
363 LLVM_DEBUG(dbgs() << " OldInst: " << *OldInst << "\n");
364 LLVM_DEBUG(dbgs() << " Inst: " << *Inst << "\n");
365 LLVM_DEBUG(dbgs() << " Dest: " << *Dest << "\n");
366 if (Value)
367 LLVM_DEBUG(dbgs() << " Value: " << *Value << "\n");
368
369 /// Map of aggregate variables to their fragment associated with OldAlloca.
371 BaseFragments;
372 for (auto *DVR : at::getDVRAssignmentMarkers(OldAlloca))
373 BaseFragments[getAggregateVariable(DVR)] =
374 DVR->getExpression()->getFragmentInfo();
375
376 // The new inst needs a DIAssignID unique metadata tag (if OldInst has
377 // one). It shouldn't already have one: assert this assumption.
378 assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID));
379 DIAssignID *NewID = nullptr;
380 auto &Ctx = Inst->getContext();
381 DIBuilder DIB(*OldInst->getModule(), /*AllowUnresolved*/ false);
382 assert(OldAlloca->isStaticAlloca());
383
384 auto MigrateDbgAssign = [&](DbgVariableRecord *DbgAssign) {
385 LLVM_DEBUG(dbgs() << " existing dbg.assign is: " << *DbgAssign
386 << "\n");
387 auto *Expr = DbgAssign->getExpression();
388 bool SetKillLocation = false;
389
390 if (IsSplit) {
391 std::optional<DIExpression::FragmentInfo> BaseFragment;
392 {
393 auto R = BaseFragments.find(getAggregateVariable(DbgAssign));
394 if (R == BaseFragments.end())
395 return;
396 BaseFragment = R->second;
397 }
398 std::optional<DIExpression::FragmentInfo> CurrentFragment =
399 Expr->getFragmentInfo();
400 DIExpression::FragmentInfo NewFragment;
401 FragCalcResult Result = calculateFragment(
402 DbgAssign->getVariable(), OldAllocaOffsetInBits, SliceSizeInBits,
403 BaseFragment, CurrentFragment, NewFragment);
404
405 if (Result == Skip)
406 return;
407 if (Result == UseFrag && !(NewFragment == CurrentFragment)) {
408 if (CurrentFragment) {
409 // Rewrite NewFragment to be relative to the existing one (this is
410 // what createFragmentExpression wants). CalculateFragment has
411 // already resolved the size for us. FIXME: Should it return the
412 // relative fragment too?
413 NewFragment.OffsetInBits -= CurrentFragment->OffsetInBits;
414 }
415 // Add the new fragment info to the existing expression if possible.
417 Expr, NewFragment.OffsetInBits, NewFragment.SizeInBits)) {
418 Expr = *E;
419 } else {
420 // Otherwise, add the new fragment info to an empty expression and
421 // discard the value component of this dbg.assign as the value cannot
422 // be computed with the new fragment.
424 DIExpression::get(Expr->getContext(), {}),
425 NewFragment.OffsetInBits, NewFragment.SizeInBits);
426 SetKillLocation = true;
427 }
428 }
429 }
430
431 // If we haven't created a DIAssignID ID do that now and attach it to Inst.
432 if (!NewID) {
433 NewID = DIAssignID::getDistinct(Ctx);
434 Inst->setMetadata(LLVMContext::MD_DIAssignID, NewID);
435 }
436
437 DbgVariableRecord *NewAssign;
438 if (IsSplit) {
439 ::Value *NewValue = Value ? Value : DbgAssign->getValue();
441 Inst, NewValue, DbgAssign->getVariable(), Expr, Dest,
442 DIExpression::get(Expr->getContext(), {}), DbgAssign->getDebugLoc()));
443 } else {
444 // The store is not split, simply steal the existing dbg_assign.
445 NewAssign = DbgAssign;
446 NewAssign->setAssignId(NewID); // FIXME: Can we avoid generating new IDs?
447 NewAssign->setAddress(Dest);
448 if (Value)
449 NewAssign->replaceVariableLocationOp(0u, Value);
450 assert(Expr == NewAssign->getExpression());
451 }
452
453 // If we've updated the value but the original dbg.assign has an arglist
454 // then kill it now - we can't use the requested new value.
455 // We can't replace the DIArgList with the new value as it'd leave
456 // the DIExpression in an invalid state (DW_OP_LLVM_arg operands without
457 // an arglist). And we can't keep the DIArgList in case the linked store
458 // is being split - in which case the DIArgList + expression may no longer
459 // be computing the correct value.
460 // This should be a very rare situation as it requires the value being
461 // stored to differ from the dbg.assign (i.e., the value has been
462 // represented differently in the debug intrinsic for some reason).
463 SetKillLocation |=
464 Value && (DbgAssign->hasArgList() ||
465 !DbgAssign->getExpression()->isSingleLocationExpression());
466 if (SetKillLocation)
467 NewAssign->setKillLocation();
468
469 // We could use more precision here at the cost of some additional (code)
470 // complexity - if the original dbg.assign was adjacent to its store, we
471 // could position this new dbg.assign adjacent to its store rather than the
472 // old dbg.assgn. That would result in interleaved dbg.assigns rather than
473 // what we get now:
474 // split store !1
475 // split store !2
476 // dbg.assign !1
477 // dbg.assign !2
478 // This (current behaviour) results results in debug assignments being
479 // noted as slightly offset (in code) from the store. In practice this
480 // should have little effect on the debugging experience due to the fact
481 // that all the split stores should get the same line number.
482 if (NewAssign != DbgAssign) {
483 NewAssign->moveBefore(DbgAssign->getIterator());
484 NewAssign->setDebugLoc(DbgAssign->getDebugLoc());
485 }
486 LLVM_DEBUG(dbgs() << "Created new assign: " << *NewAssign << "\n");
487 };
488
489 for_each(DVRAssignMarkerRange, MigrateDbgAssign);
490}
491
492namespace {
493
494/// A custom IRBuilder inserter which prefixes all names, but only in
495/// Assert builds.
496class IRBuilderPrefixedInserter final : public IRBuilderDefaultInserter {
497 std::string Prefix;
498
499 Twine getNameWithPrefix(const Twine &Name) const {
500 return Name.isTriviallyEmpty() ? Name : Prefix + Name;
501 }
502
503public:
504 void SetNamePrefix(const Twine &P) { Prefix = P.str(); }
505
506 void InsertHelper(Instruction *I, const Twine &Name,
507 BasicBlock::iterator InsertPt) const override {
508 IRBuilderDefaultInserter::InsertHelper(I, getNameWithPrefix(Name),
509 InsertPt);
510 }
511};
512
513/// Provide a type for IRBuilder that drops names in release builds.
515
516/// A used slice of an alloca.
517///
518/// This structure represents a slice of an alloca used by some instruction. It
519/// stores both the begin and end offsets of this use, a pointer to the use
520/// itself, and a flag indicating whether we can classify the use as splittable
521/// or not when forming partitions of the alloca.
522class Slice {
523 /// The beginning offset of the range.
524 uint64_t BeginOffset = 0;
525
526 /// The ending offset, not included in the range.
527 uint64_t EndOffset = 0;
528
529 /// Storage for both the use of this slice and whether it can be
530 /// split.
531 PointerIntPair<Use *, 1, bool> UseAndIsSplittable;
532
533public:
534 Slice() = default;
535
536 Slice(uint64_t BeginOffset, uint64_t EndOffset, Use *U, bool IsSplittable)
537 : BeginOffset(BeginOffset), EndOffset(EndOffset),
538 UseAndIsSplittable(U, IsSplittable) {}
539
540 uint64_t beginOffset() const { return BeginOffset; }
541 uint64_t endOffset() const { return EndOffset; }
542
543 bool isSplittable() const { return UseAndIsSplittable.getInt(); }
544 void makeUnsplittable() { UseAndIsSplittable.setInt(false); }
545
546 Use *getUse() const { return UseAndIsSplittable.getPointer(); }
547
548 bool isDead() const { return getUse() == nullptr; }
549 void kill() { UseAndIsSplittable.setPointer(nullptr); }
550
551 /// Support for ordering ranges.
552 ///
553 /// This provides an ordering over ranges such that start offsets are
554 /// always increasing, and within equal start offsets, the end offsets are
555 /// decreasing. Thus the spanning range comes first in a cluster with the
556 /// same start position.
557 bool operator<(const Slice &RHS) const {
558 if (beginOffset() < RHS.beginOffset())
559 return true;
560 if (beginOffset() > RHS.beginOffset())
561 return false;
562 if (isSplittable() != RHS.isSplittable())
563 return !isSplittable();
564 if (endOffset() > RHS.endOffset())
565 return true;
566 return false;
567 }
568
569 /// Support comparison with a single offset to allow binary searches.
570 [[maybe_unused]] friend bool operator<(const Slice &LHS, uint64_t RHSOffset) {
571 return LHS.beginOffset() < RHSOffset;
572 }
573 [[maybe_unused]] friend bool operator<(uint64_t LHSOffset, const Slice &RHS) {
574 return LHSOffset < RHS.beginOffset();
575 }
576
577 bool operator==(const Slice &RHS) const {
578 return isSplittable() == RHS.isSplittable() &&
579 beginOffset() == RHS.beginOffset() && endOffset() == RHS.endOffset();
580 }
581 bool operator!=(const Slice &RHS) const { return !operator==(RHS); }
582};
583
584/// Representation of the alloca slices.
585///
586/// This class represents the slices of an alloca which are formed by its
587/// various uses. If a pointer escapes, we can't fully build a representation
588/// for the slices used and we reflect that in this structure. The uses are
589/// stored, sorted by increasing beginning offset and with unsplittable slices
590/// starting at a particular offset before splittable slices.
591class AllocaSlices {
592public:
593 /// Construct the slices of a particular alloca.
594 AllocaSlices(const DataLayout &DL, AllocaInst &AI);
595
596 /// Test whether a pointer to the allocation escapes our analysis.
597 ///
598 /// If this is true, the slices are never fully built and should be
599 /// ignored.
600 bool isEscaped() const { return PointerEscapingInstr; }
601 bool isEscapedReadOnly() const { return PointerEscapingInstrReadOnly; }
602
603 /// Support for iterating over the slices.
604 /// @{
605 using iterator = SmallVectorImpl<Slice>::iterator;
606 using range = iterator_range<iterator>;
607
608 iterator begin() { return Slices.begin(); }
609 iterator end() { return Slices.end(); }
610
611 using const_iterator = SmallVectorImpl<Slice>::const_iterator;
612 using const_range = iterator_range<const_iterator>;
613
614 const_iterator begin() const { return Slices.begin(); }
615 const_iterator end() const { return Slices.end(); }
616 /// @}
617
618 /// Erase a range of slices.
619 void erase(iterator Start, iterator Stop) { Slices.erase(Start, Stop); }
620
621 /// Insert new slices for this alloca.
622 ///
623 /// This moves the slices into the alloca's slices collection, and re-sorts
624 /// everything so that the usual ordering properties of the alloca's slices
625 /// hold.
626 void insert(ArrayRef<Slice> NewSlices) {
627 int OldSize = Slices.size();
628 Slices.append(NewSlices.begin(), NewSlices.end());
629 auto SliceI = Slices.begin() + OldSize;
630 std::stable_sort(SliceI, Slices.end());
631 std::inplace_merge(Slices.begin(), SliceI, Slices.end());
632 }
633
634 // Forward declare the iterator and range accessor for walking the
635 // partitions.
636 class partition_iterator;
638
639 /// Access the dead users for this alloca.
640 ArrayRef<Instruction *> getDeadUsers() const { return DeadUsers; }
641
642 /// Access Uses that should be dropped if the alloca is promotable.
643 ArrayRef<Use *> getDeadUsesIfPromotable() const {
644 return DeadUseIfPromotable;
645 }
646
647 /// Access the dead operands referring to this alloca.
648 ///
649 /// These are operands which have cannot actually be used to refer to the
650 /// alloca as they are outside its range and the user doesn't correct for
651 /// that. These mostly consist of PHI node inputs and the like which we just
652 /// need to replace with undef.
653 ArrayRef<Use *> getDeadOperands() const { return DeadOperands; }
654
655#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
656 void print(raw_ostream &OS, const_iterator I, StringRef Indent = " ") const;
657 void printSlice(raw_ostream &OS, const_iterator I,
658 StringRef Indent = " ") const;
659 void printUse(raw_ostream &OS, const_iterator I,
660 StringRef Indent = " ") const;
661 void print(raw_ostream &OS) const;
662 void dump(const_iterator I) const;
663 void dump() const;
664#endif
665
666private:
667 template <typename DerivedT, typename RetT = void> class BuilderBase;
668 class SliceBuilder;
669
670 friend class AllocaSlices::SliceBuilder;
671
672#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
673 /// Handle to alloca instruction to simplify method interfaces.
674 AllocaInst &AI;
675#endif
676
677 /// The instruction responsible for this alloca not having a known set
678 /// of slices.
679 ///
680 /// When an instruction (potentially) escapes the pointer to the alloca, we
681 /// store a pointer to that here and abort trying to form slices of the
682 /// alloca. This will be null if the alloca slices are analyzed successfully.
683 Instruction *PointerEscapingInstr;
684 Instruction *PointerEscapingInstrReadOnly;
685
686 /// The slices of the alloca.
687 ///
688 /// We store a vector of the slices formed by uses of the alloca here. This
689 /// vector is sorted by increasing begin offset, and then the unsplittable
690 /// slices before the splittable ones. See the Slice inner class for more
691 /// details.
693
694 /// Instructions which will become dead if we rewrite the alloca.
695 ///
696 /// Note that these are not separated by slice. This is because we expect an
697 /// alloca to be completely rewritten or not rewritten at all. If rewritten,
698 /// all these instructions can simply be removed and replaced with poison as
699 /// they come from outside of the allocated space.
700 SmallVector<Instruction *, 8> DeadUsers;
701
702 /// Uses which will become dead if can promote the alloca.
703 SmallVector<Use *, 8> DeadUseIfPromotable;
704
705 /// Operands which will become dead if we rewrite the alloca.
706 ///
707 /// These are operands that in their particular use can be replaced with
708 /// poison when we rewrite the alloca. These show up in out-of-bounds inputs
709 /// to PHI nodes and the like. They aren't entirely dead (there might be
710 /// a GEP back into the bounds using it elsewhere) and nor is the PHI, but we
711 /// want to swap this particular input for poison to simplify the use lists of
712 /// the alloca.
713 SmallVector<Use *, 8> DeadOperands;
714};
715
716/// A partition of the slices.
717///
718/// An ephemeral representation for a range of slices which can be viewed as
719/// a partition of the alloca. This range represents a span of the alloca's
720/// memory which cannot be split, and provides access to all of the slices
721/// overlapping some part of the partition.
722///
723/// Objects of this type are produced by traversing the alloca's slices, but
724/// are only ephemeral and not persistent.
725class Partition {
726private:
727 friend class AllocaSlices;
728 friend class AllocaSlices::partition_iterator;
729
730 using iterator = AllocaSlices::iterator;
731
732 /// The beginning and ending offsets of the alloca for this
733 /// partition.
734 uint64_t BeginOffset = 0, EndOffset = 0;
735
736 /// The start and end iterators of this partition.
737 iterator SI, SJ;
738
739 /// A collection of split slice tails overlapping the partition.
740 SmallVector<Slice *, 4> SplitTails;
741
742 /// Raw constructor builds an empty partition starting and ending at
743 /// the given iterator.
744 Partition(iterator SI) : SI(SI), SJ(SI) {}
745
746public:
747 /// The start offset of this partition.
748 ///
749 /// All of the contained slices start at or after this offset.
750 uint64_t beginOffset() const { return BeginOffset; }
751
752 /// The end offset of this partition.
753 ///
754 /// All of the contained slices end at or before this offset.
755 uint64_t endOffset() const { return EndOffset; }
756
757 /// The size of the partition.
758 ///
759 /// Note that this can never be zero.
760 uint64_t size() const {
761 assert(BeginOffset < EndOffset && "Partitions must span some bytes!");
762 return EndOffset - BeginOffset;
763 }
764
765 /// Test whether this partition contains no slices, and merely spans
766 /// a region occupied by split slices.
767 bool empty() const { return SI == SJ; }
768
769 /// \name Iterate slices that start within the partition.
770 /// These may be splittable or unsplittable. They have a begin offset >= the
771 /// partition begin offset.
772 /// @{
773 // FIXME: We should probably define a "concat_iterator" helper and use that
774 // to stitch together pointee_iterators over the split tails and the
775 // contiguous iterators of the partition. That would give a much nicer
776 // interface here. We could then additionally expose filtered iterators for
777 // split, unsplit, and unsplittable splices based on the usage patterns.
778 iterator begin() const { return SI; }
779 iterator end() const { return SJ; }
780 /// @}
781
782 /// Get the sequence of split slice tails.
783 ///
784 /// These tails are of slices which start before this partition but are
785 /// split and overlap into the partition. We accumulate these while forming
786 /// partitions.
787 ArrayRef<Slice *> splitSliceTails() const { return SplitTails; }
788};
789
790} // end anonymous namespace
791
792/// An iterator over partitions of the alloca's slices.
793///
794/// This iterator implements the core algorithm for partitioning the alloca's
795/// slices. It is a forward iterator as we don't support backtracking for
796/// efficiency reasons, and re-use a single storage area to maintain the
797/// current set of split slices.
798///
799/// It is templated on the slice iterator type to use so that it can operate
800/// with either const or non-const slice iterators.
802 : public iterator_facade_base<partition_iterator, std::forward_iterator_tag,
803 Partition> {
804 friend class AllocaSlices;
805
806 /// Most of the state for walking the partitions is held in a class
807 /// with a nice interface for examining them.
808 Partition P;
809
810 /// We need to keep the end of the slices to know when to stop.
811 AllocaSlices::iterator SE;
812
813 /// We also need to keep track of the maximum split end offset seen.
814 /// FIXME: Do we really?
815 uint64_t MaxSplitSliceEndOffset = 0;
816
817 /// Sets the partition to be empty at given iterator, and sets the
818 /// end iterator.
819 partition_iterator(AllocaSlices::iterator SI, AllocaSlices::iterator SE)
820 : P(SI), SE(SE) {
821 // If not already at the end, advance our state to form the initial
822 // partition.
823 if (SI != SE)
824 advance();
825 }
826
827 /// Advance the iterator to the next partition.
828 ///
829 /// Requires that the iterator not be at the end of the slices.
830 void advance() {
831 assert((P.SI != SE || !P.SplitTails.empty()) &&
832 "Cannot advance past the end of the slices!");
833
834 // Clear out any split uses which have ended.
835 if (!P.SplitTails.empty()) {
836 if (P.EndOffset >= MaxSplitSliceEndOffset) {
837 // If we've finished all splits, this is easy.
838 P.SplitTails.clear();
839 MaxSplitSliceEndOffset = 0;
840 } else {
841 // Remove the uses which have ended in the prior partition. This
842 // cannot change the max split slice end because we just checked that
843 // the prior partition ended prior to that max.
844 llvm::erase_if(P.SplitTails,
845 [&](Slice *S) { return S->endOffset() <= P.EndOffset; });
846 assert(llvm::any_of(P.SplitTails,
847 [&](Slice *S) {
848 return S->endOffset() == MaxSplitSliceEndOffset;
849 }) &&
850 "Could not find the current max split slice offset!");
851 assert(llvm::all_of(P.SplitTails,
852 [&](Slice *S) {
853 return S->endOffset() <= MaxSplitSliceEndOffset;
854 }) &&
855 "Max split slice end offset is not actually the max!");
856 }
857 }
858
859 // If P.SI is already at the end, then we've cleared the split tail and
860 // now have an end iterator.
861 if (P.SI == SE) {
862 assert(P.SplitTails.empty() && "Failed to clear the split slices!");
863 return;
864 }
865
866 // If we had a non-empty partition previously, set up the state for
867 // subsequent partitions.
868 if (P.SI != P.SJ) {
869 // Accumulate all the splittable slices which started in the old
870 // partition into the split list.
871 for (Slice &S : P)
872 if (S.isSplittable() && S.endOffset() > P.EndOffset) {
873 P.SplitTails.push_back(&S);
874 MaxSplitSliceEndOffset =
875 std::max(S.endOffset(), MaxSplitSliceEndOffset);
876 }
877
878 // Start from the end of the previous partition.
879 P.SI = P.SJ;
880
881 // If P.SI is now at the end, we at most have a tail of split slices.
882 if (P.SI == SE) {
883 P.BeginOffset = P.EndOffset;
884 P.EndOffset = MaxSplitSliceEndOffset;
885 return;
886 }
887
888 // If the we have split slices and the next slice is after a gap and is
889 // not splittable immediately form an empty partition for the split
890 // slices up until the next slice begins.
891 if (!P.SplitTails.empty() && P.SI->beginOffset() != P.EndOffset &&
892 !P.SI->isSplittable()) {
893 P.BeginOffset = P.EndOffset;
894 P.EndOffset = P.SI->beginOffset();
895 return;
896 }
897 }
898
899 // OK, we need to consume new slices. Set the end offset based on the
900 // current slice, and step SJ past it. The beginning offset of the
901 // partition is the beginning offset of the next slice unless we have
902 // pre-existing split slices that are continuing, in which case we begin
903 // at the prior end offset.
904 P.BeginOffset = P.SplitTails.empty() ? P.SI->beginOffset() : P.EndOffset;
905 P.EndOffset = P.SI->endOffset();
906 ++P.SJ;
907
908 // There are two strategies to form a partition based on whether the
909 // partition starts with an unsplittable slice or a splittable slice.
910 if (!P.SI->isSplittable()) {
911 // When we're forming an unsplittable region, it must always start at
912 // the first slice and will extend through its end.
913 assert(P.BeginOffset == P.SI->beginOffset());
914
915 // Form a partition including all of the overlapping slices with this
916 // unsplittable slice.
917 while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) {
918 if (!P.SJ->isSplittable())
919 P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset());
920 ++P.SJ;
921 }
922
923 // We have a partition across a set of overlapping unsplittable
924 // partitions.
925 return;
926 }
927
928 // If we're starting with a splittable slice, then we need to form
929 // a synthetic partition spanning it and any other overlapping splittable
930 // splices.
931 assert(P.SI->isSplittable() && "Forming a splittable partition!");
932
933 // Collect all of the overlapping splittable slices.
934 while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset &&
935 P.SJ->isSplittable()) {
936 P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset());
937 ++P.SJ;
938 }
939
940 // Back upiP.EndOffset if we ended the span early when encountering an
941 // unsplittable slice. This synthesizes the early end offset of
942 // a partition spanning only splittable slices.
943 if (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) {
944 assert(!P.SJ->isSplittable());
945 P.EndOffset = P.SJ->beginOffset();
946 }
947 }
948
949public:
950 bool operator==(const partition_iterator &RHS) const {
951 assert(SE == RHS.SE &&
952 "End iterators don't match between compared partition iterators!");
953
954 // The observed positions of partitions is marked by the P.SI iterator and
955 // the emptiness of the split slices. The latter is only relevant when
956 // P.SI == SE, as the end iterator will additionally have an empty split
957 // slices list, but the prior may have the same P.SI and a tail of split
958 // slices.
959 if (P.SI == RHS.P.SI && P.SplitTails.empty() == RHS.P.SplitTails.empty()) {
960 assert(P.SJ == RHS.P.SJ &&
961 "Same set of slices formed two different sized partitions!");
962 assert(P.SplitTails.size() == RHS.P.SplitTails.size() &&
963 "Same slice position with differently sized non-empty split "
964 "slice tails!");
965 return true;
966 }
967 return false;
968 }
969
970 partition_iterator &operator++() {
971 advance();
972 return *this;
973 }
974
975 Partition &operator*() { return P; }
976};
977
978/// A forward range over the partitions of the alloca's slices.
979///
980/// This accesses an iterator range over the partitions of the alloca's
981/// slices. It computes these partitions on the fly based on the overlapping
982/// offsets of the slices and the ability to split them. It will visit "empty"
983/// partitions to cover regions of the alloca only accessed via split
984/// slices.
985iterator_range<AllocaSlices::partition_iterator> AllocaSlices::partitions() {
986 return make_range(partition_iterator(begin(), end()),
987 partition_iterator(end(), end()));
988}
989
991 // If the condition being selected on is a constant or the same value is
992 // being selected between, fold the select. Yes this does (rarely) happen
993 // early on.
994 if (ConstantInt *CI = dyn_cast<ConstantInt>(SI.getCondition()))
995 return SI.getOperand(1 + CI->isZero());
996 if (SI.getOperand(1) == SI.getOperand(2))
997 return SI.getOperand(1);
998
999 return nullptr;
1000}
1001
1002/// A helper that folds a PHI node or a select.
1004 if (PHINode *PN = dyn_cast<PHINode>(&I)) {
1005 // If PN merges together the same value, return that value.
1006 return PN->hasConstantValue();
1007 }
1009}
1010
1011/// Builder for the alloca slices.
1012///
1013/// This class builds a set of alloca slices by recursively visiting the uses
1014/// of an alloca and making a slice for each load and store at each offset.
1015class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> {
1016 friend class PtrUseVisitor<SliceBuilder>;
1017 friend class InstVisitor<SliceBuilder>;
1018
1019 using Base = PtrUseVisitor<SliceBuilder>;
1020
1021 const uint64_t AllocSize;
1022 AllocaSlices &AS;
1023
1024 SmallDenseMap<Instruction *, unsigned> MemTransferSliceMap;
1026
1027 /// Set to de-duplicate dead instructions found in the use walk.
1028 SmallPtrSet<Instruction *, 4> VisitedDeadInsts;
1029
1030public:
1031 SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS)
1033 AllocSize(AI.getAllocationSize(DL)->getFixedValue()), AS(AS) {}
1034
1035private:
1036 void markAsDead(Instruction &I) {
1037 if (VisitedDeadInsts.insert(&I).second)
1038 AS.DeadUsers.push_back(&I);
1039 }
1040
1041 void insertUse(Instruction &I, const APInt &Offset, uint64_t Size,
1042 bool IsSplittable = false) {
1043 // Completely skip uses which have a zero size or start either before or
1044 // past the end of the allocation.
1045 if (Size == 0 || Offset.uge(AllocSize)) {
1046 LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte use @"
1047 << Offset
1048 << " which has zero size or starts outside of the "
1049 << AllocSize << " byte alloca:\n"
1050 << " alloca: " << AS.AI << "\n"
1051 << " use: " << I << "\n");
1052 return markAsDead(I);
1053 }
1054
1055 uint64_t BeginOffset = Offset.getZExtValue();
1056 uint64_t EndOffset = BeginOffset + Size;
1057
1058 // Clamp the end offset to the end of the allocation. Note that this is
1059 // formulated to handle even the case where "BeginOffset + Size" overflows.
1060 // This may appear superficially to be something we could ignore entirely,
1061 // but that is not so! There may be widened loads or PHI-node uses where
1062 // some instructions are dead but not others. We can't completely ignore
1063 // them, and so have to record at least the information here.
1064 assert(AllocSize >= BeginOffset); // Established above.
1065 if (Size > AllocSize - BeginOffset) {
1066 LLVM_DEBUG(dbgs() << "WARNING: Clamping a " << Size << " byte use @"
1067 << Offset << " to remain within the " << AllocSize
1068 << " byte alloca:\n"
1069 << " alloca: " << AS.AI << "\n"
1070 << " use: " << I << "\n");
1071 EndOffset = AllocSize;
1072 }
1073
1074 AS.Slices.push_back(Slice(BeginOffset, EndOffset, U, IsSplittable));
1075 }
1076
1077 void visitBitCastInst(BitCastInst &BC) {
1078 if (BC.use_empty())
1079 return markAsDead(BC);
1080
1081 return Base::visitBitCastInst(BC);
1082 }
1083
1084 void visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) {
1085 if (ASC.use_empty())
1086 return markAsDead(ASC);
1087
1088 return Base::visitAddrSpaceCastInst(ASC);
1089 }
1090
1091 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
1092 if (GEPI.use_empty())
1093 return markAsDead(GEPI);
1094
1095 return Base::visitGetElementPtrInst(GEPI);
1096 }
1097
1098 void handleLoadOrStore(Type *Ty, Instruction &I, const APInt &Offset,
1099 uint64_t Size, bool IsVolatile) {
1100 // We allow splitting of non-volatile loads and stores where the type is an
1101 // integer type. These may be used to implement 'memcpy' or other "transfer
1102 // of bits" patterns.
1103 bool IsSplittable =
1104 Ty->isIntegerTy() && !IsVolatile && DL.typeSizeEqualsStoreSize(Ty);
1105
1106 insertUse(I, Offset, Size, IsSplittable);
1107 }
1108
1109 void visitLoadInst(LoadInst &LI) {
1110 assert((!LI.isSimple() || LI.getType()->isSingleValueType()) &&
1111 "All simple FCA loads should have been pre-split");
1112
1113 // If there is a load with an unknown offset, we can still perform store
1114 // to load forwarding for other known-offset loads.
1115 if (!IsOffsetKnown)
1116 return PI.setEscapedReadOnly(&LI);
1117
1118 TypeSize Size = DL.getTypeStoreSize(LI.getType());
1119 if (Size.isScalable()) {
1120 unsigned VScale = LI.getFunction()->getVScaleValue();
1121 if (!VScale)
1122 return PI.setAborted(&LI);
1123
1124 Size = TypeSize::getFixed(Size.getKnownMinValue() * VScale);
1125 }
1126
1127 return handleLoadOrStore(LI.getType(), LI, Offset, Size.getFixedValue(),
1128 LI.isVolatile());
1129 }
1130
1131 void visitStoreInst(StoreInst &SI) {
1132 Value *ValOp = SI.getValueOperand();
1133 if (ValOp == *U)
1134 return PI.setEscapedAndAborted(&SI);
1135 if (!IsOffsetKnown)
1136 return PI.setAborted(&SI);
1137
1138 TypeSize StoreSize = DL.getTypeStoreSize(ValOp->getType());
1139 if (StoreSize.isScalable()) {
1140 unsigned VScale = SI.getFunction()->getVScaleValue();
1141 if (!VScale)
1142 return PI.setAborted(&SI);
1143
1144 StoreSize = TypeSize::getFixed(StoreSize.getKnownMinValue() * VScale);
1145 }
1146
1147 uint64_t Size = StoreSize.getFixedValue();
1148
1149 // If this memory access can be shown to *statically* extend outside the
1150 // bounds of the allocation, it's behavior is undefined, so simply
1151 // ignore it. Note that this is more strict than the generic clamping
1152 // behavior of insertUse. We also try to handle cases which might run the
1153 // risk of overflow.
1154 // FIXME: We should instead consider the pointer to have escaped if this
1155 // function is being instrumented for addressing bugs or race conditions.
1156 if (Size > AllocSize || Offset.ugt(AllocSize - Size)) {
1157 LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte store @"
1158 << Offset << " which extends past the end of the "
1159 << AllocSize << " byte alloca:\n"
1160 << " alloca: " << AS.AI << "\n"
1161 << " use: " << SI << "\n");
1162 return markAsDead(SI);
1163 }
1164
1165 assert((!SI.isSimple() || ValOp->getType()->isSingleValueType()) &&
1166 "All simple FCA stores should have been pre-split");
1167 handleLoadOrStore(ValOp->getType(), SI, Offset, Size, SI.isVolatile());
1168 }
1169
1170 void visitMemSetInst(MemSetInst &II) {
1171 assert(II.getRawDest() == *U && "Pointer use is not the destination?");
1172 ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength());
1173 if ((Length && Length->getValue() == 0) ||
1174 (IsOffsetKnown && Offset.uge(AllocSize)))
1175 // Zero-length mem transfer intrinsics can be ignored entirely.
1176 return markAsDead(II);
1177
1178 if (!IsOffsetKnown)
1179 return PI.setAborted(&II);
1180
1181 insertUse(II, Offset,
1182 Length ? Length->getLimitedValue()
1183 : AllocSize - Offset.getLimitedValue(),
1184 (bool)Length);
1185 }
1186
1187 void visitMemTransferInst(MemTransferInst &II) {
1188 ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength());
1189 if (Length && Length->getValue() == 0)
1190 // Zero-length mem transfer intrinsics can be ignored entirely.
1191 return markAsDead(II);
1192
1193 // Because we can visit these intrinsics twice, also check to see if the
1194 // first time marked this instruction as dead. If so, skip it.
1195 if (VisitedDeadInsts.count(&II))
1196 return;
1197
1198 if (!IsOffsetKnown)
1199 return PI.setAborted(&II);
1200
1201 // This side of the transfer is completely out-of-bounds, and so we can
1202 // nuke the entire transfer. However, we also need to nuke the other side
1203 // if already added to our partitions.
1204 // FIXME: Yet another place we really should bypass this when
1205 // instrumenting for ASan.
1206 if (Offset.uge(AllocSize)) {
1207 auto MTPI = MemTransferSliceMap.find(&II);
1208 if (MTPI != MemTransferSliceMap.end())
1209 AS.Slices[MTPI->second].kill();
1210 return markAsDead(II);
1211 }
1212
1213 uint64_t RawOffset = Offset.getLimitedValue();
1214 uint64_t Size = Length ? Length->getLimitedValue() : AllocSize - RawOffset;
1215
1216 // Check for the special case where the same exact value is used for both
1217 // source and dest.
1218 if (*U == II.getRawDest() && *U == II.getRawSource()) {
1219 // For non-volatile transfers this is a no-op.
1220 if (!II.isVolatile())
1221 return markAsDead(II);
1222
1223 return insertUse(II, Offset, Size, /*IsSplittable=*/false);
1224 }
1225
1226 // If we have seen both source and destination for a mem transfer, then
1227 // they both point to the same alloca.
1228 bool Inserted;
1229 SmallDenseMap<Instruction *, unsigned>::iterator MTPI;
1230 std::tie(MTPI, Inserted) =
1231 MemTransferSliceMap.insert(std::make_pair(&II, AS.Slices.size()));
1232 unsigned PrevIdx = MTPI->second;
1233 if (!Inserted) {
1234 Slice &PrevP = AS.Slices[PrevIdx];
1235
1236 // Check if the begin offsets match and this is a non-volatile transfer.
1237 // In that case, we can completely elide the transfer.
1238 if (!II.isVolatile() && PrevP.beginOffset() == RawOffset) {
1239 PrevP.kill();
1240 return markAsDead(II);
1241 }
1242
1243 // Otherwise we have an offset transfer within the same alloca. We can't
1244 // split those.
1245 PrevP.makeUnsplittable();
1246 }
1247
1248 // Insert the use now that we've fixed up the splittable nature.
1249 insertUse(II, Offset, Size, /*IsSplittable=*/Inserted && Length);
1250
1251 // Check that we ended up with a valid index in the map.
1252 assert(AS.Slices[PrevIdx].getUse()->getUser() == &II &&
1253 "Map index doesn't point back to a slice with this user.");
1254 }
1255
1256 // Disable SRoA for any intrinsics except for lifetime invariants.
1257 // FIXME: What about debug intrinsics? This matches old behavior, but
1258 // doesn't make sense.
1259 void visitIntrinsicInst(IntrinsicInst &II) {
1260 if (II.isDroppable()) {
1261 AS.DeadUseIfPromotable.push_back(U);
1262 return;
1263 }
1264
1265 if (!IsOffsetKnown)
1266 return PI.setAborted(&II);
1267
1268 if (II.isLifetimeStartOrEnd()) {
1269 insertUse(II, Offset, AllocSize, true);
1270 return;
1271 }
1272
1273 Base::visitIntrinsicInst(II);
1274 }
1275
1276 Instruction *hasUnsafePHIOrSelectUse(Instruction *Root, uint64_t &Size) {
1277 // We consider any PHI or select that results in a direct load or store of
1278 // the same offset to be a viable use for slicing purposes. These uses
1279 // are considered unsplittable and the size is the maximum loaded or stored
1280 // size.
1281 SmallPtrSet<Instruction *, 4> Visited;
1283 Visited.insert(Root);
1284 Uses.push_back(std::make_pair(cast<Instruction>(*U), Root));
1285 const DataLayout &DL = Root->getDataLayout();
1286 // If there are no loads or stores, the access is dead. We mark that as
1287 // a size zero access.
1288 Size = 0;
1289 do {
1290 Instruction *I, *UsedI;
1291 std::tie(UsedI, I) = Uses.pop_back_val();
1292
1293 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1294 TypeSize LoadSize = DL.getTypeStoreSize(LI->getType());
1295 if (LoadSize.isScalable()) {
1296 PI.setAborted(LI);
1297 return nullptr;
1298 }
1299 Size = std::max(Size, LoadSize.getFixedValue());
1300 continue;
1301 }
1302 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1303 Value *Op = SI->getOperand(0);
1304 if (Op == UsedI)
1305 return SI;
1306 TypeSize StoreSize = DL.getTypeStoreSize(Op->getType());
1307 if (StoreSize.isScalable()) {
1308 PI.setAborted(SI);
1309 return nullptr;
1310 }
1311 Size = std::max(Size, StoreSize.getFixedValue());
1312 continue;
1313 }
1314
1315 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
1316 if (!GEP->hasAllZeroIndices())
1317 return GEP;
1318 } else if (!isa<BitCastInst>(I) && !isa<PHINode>(I) &&
1320 return I;
1321 }
1322
1323 for (User *U : I->users())
1324 if (Visited.insert(cast<Instruction>(U)).second)
1325 Uses.push_back(std::make_pair(I, cast<Instruction>(U)));
1326 } while (!Uses.empty());
1327
1328 return nullptr;
1329 }
1330
1331 void visitPHINodeOrSelectInst(Instruction &I) {
1333 if (I.use_empty())
1334 return markAsDead(I);
1335
1336 // If this is a PHI node before a catchswitch, we cannot insert any non-PHI
1337 // instructions in this BB, which may be required during rewriting. Bail out
1338 // on these cases.
1339 if (isa<PHINode>(I) && !I.getParent()->hasInsertionPt())
1340 return PI.setAborted(&I);
1341
1342 // TODO: We could use simplifyInstruction here to fold PHINodes and
1343 // SelectInsts. However, doing so requires to change the current
1344 // dead-operand-tracking mechanism. For instance, suppose neither loading
1345 // from %U nor %other traps. Then "load (select undef, %U, %other)" does not
1346 // trap either. However, if we simply replace %U with undef using the
1347 // current dead-operand-tracking mechanism, "load (select undef, undef,
1348 // %other)" may trap because the select may return the first operand
1349 // "undef".
1350 if (Value *Result = foldPHINodeOrSelectInst(I)) {
1351 if (Result == *U)
1352 // If the result of the constant fold will be the pointer, recurse
1353 // through the PHI/select as if we had RAUW'ed it.
1354 enqueueUsers(I);
1355 else
1356 // Otherwise the operand to the PHI/select is dead, and we can replace
1357 // it with poison.
1358 AS.DeadOperands.push_back(U);
1359
1360 return;
1361 }
1362
1363 if (!IsOffsetKnown)
1364 return PI.setAborted(&I);
1365
1366 // See if we already have computed info on this node.
1367 uint64_t &Size = PHIOrSelectSizes[&I];
1368 if (!Size) {
1369 // This is a new PHI/Select, check for an unsafe use of it.
1370 if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&I, Size))
1371 return PI.setAborted(UnsafeI);
1372 }
1373
1374 // For PHI and select operands outside the alloca, we can't nuke the entire
1375 // phi or select -- the other side might still be relevant, so we special
1376 // case them here and use a separate structure to track the operands
1377 // themselves which should be replaced with poison.
1378 // FIXME: This should instead be escaped in the event we're instrumenting
1379 // for address sanitization.
1380 if (Offset.uge(AllocSize)) {
1381 AS.DeadOperands.push_back(U);
1382 return;
1383 }
1384
1385 insertUse(I, Offset, Size);
1386 }
1387
1388 void visitPHINode(PHINode &PN) { visitPHINodeOrSelectInst(PN); }
1389
1390 void visitSelectInst(SelectInst &SI) { visitPHINodeOrSelectInst(SI); }
1391
1392 /// Disable SROA entirely if there are unhandled users of the alloca.
1393 void visitInstruction(Instruction &I) { PI.setAborted(&I); }
1394
1395 void visitCallBase(CallBase &CB) {
1396 // If the call operand is read-only and only does a read-only or address
1397 // capture, then we mark it as EscapedReadOnly.
1398 if (CB.isDataOperand(U) &&
1399 !capturesFullProvenance(CB.getCaptureInfo(U->getOperandNo())) &&
1400 CB.onlyReadsMemory(U->getOperandNo())) {
1401 PI.setEscapedReadOnly(&CB);
1402 return;
1403 }
1404
1405 Base::visitCallBase(CB);
1406 }
1407};
1408
1409AllocaSlices::AllocaSlices(const DataLayout &DL, AllocaInst &AI)
1410 :
1411#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1412 AI(AI),
1413#endif
1414 PointerEscapingInstr(nullptr), PointerEscapingInstrReadOnly(nullptr) {
1415 SliceBuilder PB(DL, AI, *this);
1416 SliceBuilder::PtrInfo PtrI = PB.visitPtr(AI);
1417 if (PtrI.isEscaped() || PtrI.isAborted()) {
1418 // FIXME: We should sink the escape vs. abort info into the caller nicely,
1419 // possibly by just storing the PtrInfo in the AllocaSlices.
1420 PointerEscapingInstr = PtrI.getEscapingInst() ? PtrI.getEscapingInst()
1421 : PtrI.getAbortingInst();
1422 assert(PointerEscapingInstr && "Did not track a bad instruction");
1423 return;
1424 }
1425 PointerEscapingInstrReadOnly = PtrI.getEscapedReadOnlyInst();
1426
1427 llvm::erase_if(Slices, [](const Slice &S) { return S.isDead(); });
1428
1429 // Sort the uses. This arranges for the offsets to be in ascending order,
1430 // and the sizes to be in descending order.
1431 llvm::stable_sort(Slices);
1432}
1433
1434#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1435
1436void AllocaSlices::print(raw_ostream &OS, const_iterator I,
1437 StringRef Indent) const {
1438 printSlice(OS, I, Indent);
1439 OS << "\n";
1440 printUse(OS, I, Indent);
1441}
1442
1443void AllocaSlices::printSlice(raw_ostream &OS, const_iterator I,
1444 StringRef Indent) const {
1445 OS << Indent << "[" << I->beginOffset() << "," << I->endOffset() << ")"
1446 << " slice #" << (I - begin())
1447 << (I->isSplittable() ? " (splittable)" : "");
1448}
1449
1450void AllocaSlices::printUse(raw_ostream &OS, const_iterator I,
1451 StringRef Indent) const {
1452 OS << Indent << " used by: " << *I->getUse()->getUser() << "\n";
1453}
1454
1455void AllocaSlices::print(raw_ostream &OS) const {
1456 if (PointerEscapingInstr) {
1457 OS << "Can't analyze slices for alloca: " << AI << "\n"
1458 << " A pointer to this alloca escaped by:\n"
1459 << " " << *PointerEscapingInstr << "\n";
1460 return;
1461 }
1462
1463 if (PointerEscapingInstrReadOnly)
1464 OS << "Escapes into ReadOnly: " << *PointerEscapingInstrReadOnly << "\n";
1465
1466 OS << "Slices of alloca: " << AI << "\n";
1467 for (const_iterator I = begin(), E = end(); I != E; ++I)
1468 print(OS, I);
1469}
1470
1471LLVM_DUMP_METHOD void AllocaSlices::dump(const_iterator I) const {
1472 print(dbgs(), I);
1473}
1474LLVM_DUMP_METHOD void AllocaSlices::dump() const { print(dbgs()); }
1475
1476#endif // !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1477
1478/// Find a common load/store type used through a pointer PHI or select.
1479///
1480/// Look through a PHI or select to see if all of its users are loads or stores
1481/// of one common type. Whether those accesses can be speculated does not affect
1482/// the type they use and is checked separately when attempting promotion.
1484 assert((isa<PHINode, SelectInst>(I)) && "expected a PHI or select");
1485 Type *Ty = nullptr;
1486
1487 for (User *U : I.users()) {
1488 Type *UserTy = nullptr;
1489 if (auto *LI = dyn_cast<LoadInst>(U))
1490 UserTy = LI->getType();
1491 else if (auto *Store = dyn_cast<StoreInst>(U))
1492 // Slice building rejects stores of the PHI-or-select-derived pointer, so
1493 // it must be the store's pointer operand here.
1494 UserTy = Store->getValueOperand()->getType();
1495
1496 if (!UserTy || (Ty && Ty != UserTy))
1497 return nullptr;
1498 Ty = UserTy;
1499 }
1500
1501 return Ty;
1502}
1503
1504/// Walk the range of a partitioning looking for a common type to cover this
1505/// sequence of slices.
1506static std::pair<Type *, IntegerType *>
1507findCommonType(AllocaSlices::const_iterator B, AllocaSlices::const_iterator E,
1508 uint64_t EndOffset) {
1509 Type *Ty = nullptr;
1510 bool TyIsCommon = true;
1511 IntegerType *ITy = nullptr;
1512
1513 // Note that we need to look at *every* alloca slice's Use to ensure we
1514 // always get consistent results regardless of the order of slices.
1515 for (AllocaSlices::const_iterator I = B; I != E; ++I) {
1516 Use *U = I->getUse();
1517 if (isa<IntrinsicInst>(*U->getUser()))
1518 continue;
1519 if (I->beginOffset() != B->beginOffset() || I->endOffset() != EndOffset)
1520 continue;
1521
1522 Type *UserTy = nullptr;
1523 if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
1524 UserTy = LI->getType();
1525 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
1526 UserTy = SI->getValueOperand()->getType();
1527 } else if (isa<PHINode, SelectInst>(U->getUser())) {
1528 UserTy =
1530 }
1531
1532 if (IntegerType *UserITy = dyn_cast_or_null<IntegerType>(UserTy)) {
1533 // If the type is larger than the partition, skip it. We only encounter
1534 // this for split integer operations where we want to use the type of the
1535 // entity causing the split. Also skip if the type is not a byte width
1536 // multiple.
1537 if (UserITy->getBitWidth() % 8 != 0 ||
1538 UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset()))
1539 continue;
1540
1541 // Track the largest bitwidth integer type used in this way in case there
1542 // is no common type.
1543 if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth())
1544 ITy = UserITy;
1545 }
1546
1547 // To avoid depending on the order of slices, Ty and TyIsCommon must not
1548 // depend on types skipped above.
1549 if (!UserTy || (Ty && Ty != UserTy))
1550 TyIsCommon = false; // Give up on anything but an iN type.
1551 else
1552 Ty = UserTy;
1553 }
1554
1555 return {TyIsCommon ? Ty : nullptr, ITy};
1556}
1557
1558/// PHI instructions that use an alloca and are subsequently loaded can be
1559/// rewritten to load both input pointers in the pred blocks and then PHI the
1560/// results, allowing the load of the alloca to be promoted.
1561/// From this:
1562/// %P2 = phi [i32* %Alloca, i32* %Other]
1563/// %V = load i32* %P2
1564/// to:
1565/// %V1 = load i32* %Alloca -> will be mem2reg'd
1566/// ...
1567/// %V2 = load i32* %Other
1568/// ...
1569/// %V = phi [i32 %V1, i32 %V2]
1570///
1571/// We can do this to a select if its only uses are loads and if the operands
1572/// to the select can be loaded unconditionally.
1573///
1574/// FIXME: This should be hoisted into a generic utility, likely in
1575/// Transforms/Util/Local.h
1577 const DataLayout &DL = PN.getDataLayout();
1578
1579 // For now, we can only do this promotion if the load is in the same block
1580 // as the PHI, and if there are no stores between the phi and load.
1581 // TODO: Allow recursive phi users.
1582 // TODO: Allow stores.
1583 BasicBlock *BB = PN.getParent();
1584 Align MaxAlign;
1585 uint64_t APWidth = DL.getIndexTypeSizeInBits(PN.getType());
1586 Type *LoadType = nullptr;
1587 for (User *U : PN.users()) {
1589 if (!LI || !LI->isSimple())
1590 return false;
1591
1592 // For now we only allow loads in the same block as the PHI. This is
1593 // a common case that happens when instcombine merges two loads through
1594 // a PHI.
1595 if (LI->getParent() != BB)
1596 return false;
1597
1598 if (LoadType) {
1599 if (LoadType != LI->getType())
1600 return false;
1601 } else {
1602 LoadType = LI->getType();
1603 }
1604
1605 // Ensure that there are no instructions between the PHI and the load that
1606 // could store.
1607 for (BasicBlock::iterator BBI(PN); &*BBI != LI; ++BBI)
1608 if (BBI->mayWriteToMemory())
1609 return false;
1610
1611 MaxAlign = std::max(MaxAlign, LI->getAlign());
1612 }
1613
1614 if (!LoadType)
1615 return false;
1616
1617 APInt LoadSize =
1618 APInt(APWidth, DL.getTypeStoreSize(LoadType).getFixedValue());
1619
1620 // We can only transform this if it is safe to push the loads into the
1621 // predecessor blocks. The only thing to watch out for is that we can't put
1622 // a possibly trapping load in the predecessor if it is a critical edge.
1623 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1625 Value *InVal = PN.getIncomingValue(Idx);
1626
1627 // If the value is produced by the terminator of the predecessor (an
1628 // invoke) or it has side-effects, there is no valid place to put a load
1629 // in the predecessor.
1630 if (TI == InVal || TI->mayHaveSideEffects())
1631 return false;
1632
1633 // If the predecessor has a single successor, then the edge isn't
1634 // critical.
1635 if (TI->getNumSuccessors() == 1)
1636 continue;
1637
1638 // If this pointer is always safe to load, or if we can prove that there
1639 // is already a load in the block, then we can move the load to the pred
1640 // block.
1641 if (isSafeToLoadUnconditionally(InVal, MaxAlign, LoadSize,
1642 SimplifyQuery(DL, TI)))
1643 continue;
1644
1645 return false;
1646 }
1647
1648 return true;
1649}
1650
1651static void speculatePHINodeLoads(IRBuilderTy &IRB, PHINode &PN) {
1652 LLVM_DEBUG(dbgs() << " original: " << PN << "\n");
1653
1654 LoadInst *SomeLoad = cast<LoadInst>(PN.user_back());
1655 Type *LoadTy = SomeLoad->getType();
1656 IRB.SetInsertPoint(&PN);
1657 PHINode *NewPN = IRB.CreatePHI(LoadTy, PN.getNumIncomingValues(),
1658 PN.getName() + ".sroa.speculated");
1659
1660 // Get the AA tags and alignment to use from one of the loads. It does not
1661 // matter which one we get and if any differ.
1662 AAMDNodes AATags = SomeLoad->getAAMetadata();
1663 Align Alignment = SomeLoad->getAlign();
1664
1665 // Rewrite all loads of the PN to use the new PHI.
1666 while (!PN.use_empty()) {
1667 LoadInst *LI = cast<LoadInst>(PN.user_back());
1668 LI->replaceAllUsesWith(NewPN);
1669 LI->eraseFromParent();
1670 }
1671
1672 // Inject loads into all of the pred blocks.
1673 DenseMap<BasicBlock *, Value *> InjectedLoads;
1674 for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) {
1675 BasicBlock *Pred = PN.getIncomingBlock(Idx);
1676 Value *InVal = PN.getIncomingValue(Idx);
1677
1678 // A PHI node is allowed to have multiple (duplicated) entries for the same
1679 // basic block, as long as the value is the same. So if we already injected
1680 // a load in the predecessor, then we should reuse the same load for all
1681 // duplicated entries.
1682 if (Value *V = InjectedLoads.lookup(Pred)) {
1683 NewPN->addIncoming(V, Pred);
1684 continue;
1685 }
1686
1687 Instruction *TI = Pred->getTerminator();
1688 IRB.SetInsertPoint(TI);
1689
1690 LoadInst *Load = IRB.CreateAlignedLoad(
1691 LoadTy, InVal, Alignment,
1692 (PN.getName() + ".sroa.speculate.load." + Pred->getName()));
1693 ++NumLoadsSpeculated;
1694 if (AATags)
1695 Load->setAAMetadata(AATags);
1696 NewPN->addIncoming(Load, Pred);
1697 InjectedLoads[Pred] = Load;
1698 }
1699
1700 LLVM_DEBUG(dbgs() << " speculated to: " << *NewPN << "\n");
1701 PN.eraseFromParent();
1702}
1703
1704SelectHandSpeculativity &
1705SelectHandSpeculativity::setAsSpeculatable(bool isTrueVal) {
1706 if (isTrueVal)
1708 else
1710 return *this;
1711}
1712
1713bool SelectHandSpeculativity::isSpeculatable(bool isTrueVal) const {
1714 return isTrueVal ? Bitfield::get<SelectHandSpeculativity::TrueVal>(Storage)
1715 : Bitfield::get<SelectHandSpeculativity::FalseVal>(Storage);
1716}
1717
1718bool SelectHandSpeculativity::areAllSpeculatable() const {
1719 return isSpeculatable(/*isTrueVal=*/true) &&
1720 isSpeculatable(/*isTrueVal=*/false);
1721}
1722
1723bool SelectHandSpeculativity::areAnySpeculatable() const {
1724 return isSpeculatable(/*isTrueVal=*/true) ||
1725 isSpeculatable(/*isTrueVal=*/false);
1726}
1727bool SelectHandSpeculativity::areNoneSpeculatable() const {
1728 return !areAnySpeculatable();
1729}
1730
1731static SelectHandSpeculativity
1733 assert(LI.isSimple() && "Only for simple loads");
1734 SelectHandSpeculativity Spec;
1735
1736 const DataLayout &DL = SI.getDataLayout();
1737 for (Value *Value : {SI.getTrueValue(), SI.getFalseValue()})
1739 SimplifyQuery(DL, &LI)))
1740 Spec.setAsSpeculatable(/*isTrueVal=*/Value == SI.getTrueValue());
1741 else if (PreserveCFG)
1742 return Spec;
1743
1744 return Spec;
1745}
1746
1747std::optional<RewriteableMemOps>
1748SROA::isSafeSelectToSpeculate(SelectInst &SI, bool PreserveCFG) {
1749 RewriteableMemOps Ops;
1750
1751 for (User *U : SI.users()) {
1752 if (auto *Store = dyn_cast<StoreInst>(U)) {
1753 // Note that atomic stores can be transformed; atomic semantics do not
1754 // have any meaning for a local alloca. Stores are not speculatable,
1755 // however, so if we can't turn it into a predicated store, we are done.
1756 if (Store->isVolatile() || PreserveCFG)
1757 return {}; // Give up on this `select`.
1758 Ops.emplace_back(Store);
1759 continue;
1760 }
1761
1762 auto *LI = dyn_cast<LoadInst>(U);
1763
1764 // Note that atomic loads can be transformed;
1765 // atomic semantics do not have any meaning for a local alloca.
1766 if (!LI || LI->isVolatile())
1767 return {}; // Give up on this `select`.
1768
1769 PossiblySpeculatableLoad Load(LI);
1770 if (!LI->isSimple()) {
1771 // If the `load` is not simple, we can't speculatively execute it,
1772 // but we could handle this via a CFG modification. But can we?
1773 if (PreserveCFG)
1774 return {}; // Give up on this `select`.
1775 Ops.emplace_back(Load);
1776 continue;
1777 }
1778
1779 SelectHandSpeculativity Spec =
1780 isSafeLoadOfSelectToSpeculate(*LI, SI, PreserveCFG);
1781 if (PreserveCFG && !Spec.areAllSpeculatable())
1782 return {}; // Give up on this `select`.
1783
1784 Load.setInt(Spec);
1785 Ops.emplace_back(Load);
1786 }
1787
1788 return Ops;
1789}
1790
1792 IRBuilderTy &IRB) {
1793 LLVM_DEBUG(dbgs() << " original load: " << SI << "\n");
1794
1795 Value *TV = SI.getTrueValue();
1796 Value *FV = SI.getFalseValue();
1797 // Replace the given load of the select with a select of two loads.
1798
1799 assert(LI.isSimple() && "We only speculate simple loads");
1800
1801 IRB.SetInsertPoint(&LI);
1802
1803 LoadInst *TL =
1804 IRB.CreateAlignedLoad(LI.getType(), TV, LI.getAlign(),
1805 LI.getName() + ".sroa.speculate.load.true");
1806 LoadInst *FL =
1807 IRB.CreateAlignedLoad(LI.getType(), FV, LI.getAlign(),
1808 LI.getName() + ".sroa.speculate.load.false");
1809 NumLoadsSpeculated += 2;
1810
1811 // Transfer alignment and AA info if present.
1812 TL->setAlignment(LI.getAlign());
1813 FL->setAlignment(LI.getAlign());
1814
1815 AAMDNodes Tags = LI.getAAMetadata();
1816 if (Tags) {
1817 TL->setAAMetadata(Tags);
1818 FL->setAAMetadata(Tags);
1819 }
1820
1821 Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL,
1822 LI.getName() + ".sroa.speculated", &SI);
1823
1824 LLVM_DEBUG(dbgs() << " speculated to: " << *V << "\n");
1825 LI.replaceAllUsesWith(V);
1826}
1827
1828template <typename T>
1830 SelectHandSpeculativity Spec,
1831 DomTreeUpdater &DTU) {
1832 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Only for load and store!");
1833 LLVM_DEBUG(dbgs() << " original mem op: " << I << "\n");
1834 BasicBlock *Head = I.getParent();
1835 Instruction *ThenTerm = nullptr;
1836 Instruction *ElseTerm = nullptr;
1837 if (Spec.areNoneSpeculatable())
1838 SplitBlockAndInsertIfThenElse(SI.getCondition(), &I, &ThenTerm, &ElseTerm,
1839 SI.getMetadata(LLVMContext::MD_prof), &DTU);
1840 else {
1841 SplitBlockAndInsertIfThen(SI.getCondition(), &I, /*Unreachable=*/false,
1842 SI.getMetadata(LLVMContext::MD_prof), &DTU,
1843 /*LI=*/nullptr, /*ThenBlock=*/nullptr);
1844 if (Spec.isSpeculatable(/*isTrueVal=*/true))
1845 cast<CondBrInst>(Head->getTerminator())->swapSuccessors();
1846 }
1847 auto *HeadBI = cast<CondBrInst>(Head->getTerminator());
1848 Spec = {}; // Do not use `Spec` beyond this point.
1849 BasicBlock *Tail = I.getParent();
1850 Tail->setName(Head->getName() + ".cont");
1851 PHINode *PN;
1852 if (isa<LoadInst>(I))
1853 PN = PHINode::Create(I.getType(), 2, "", I.getIterator());
1854 for (BasicBlock *SuccBB : successors(Head)) {
1855 bool IsThen = SuccBB == HeadBI->getSuccessor(0);
1856 int SuccIdx = IsThen ? 0 : 1;
1857 auto *NewMemOpBB = SuccBB == Tail ? Head : SuccBB;
1858 auto &CondMemOp = cast<T>(*I.clone());
1859 if (NewMemOpBB != Head) {
1860 NewMemOpBB->setName(Head->getName() + (IsThen ? ".then" : ".else"));
1861 if (isa<LoadInst>(I))
1862 ++NumLoadsPredicated;
1863 else
1864 ++NumStoresPredicated;
1865 } else {
1866 CondMemOp.dropUBImplyingAttrsAndMetadata();
1867 ++NumLoadsSpeculated;
1868 }
1869 CondMemOp.insertBefore(NewMemOpBB->getTerminator()->getIterator());
1870 Value *Ptr = SI.getOperand(1 + SuccIdx);
1871 CondMemOp.setOperand(I.getPointerOperandIndex(), Ptr);
1872 if (isa<LoadInst>(I)) {
1873 CondMemOp.setName(I.getName() + (IsThen ? ".then" : ".else") + ".val");
1874 PN->addIncoming(&CondMemOp, NewMemOpBB);
1875 } else
1876 LLVM_DEBUG(dbgs() << " to: " << CondMemOp << "\n");
1877 }
1878 if (isa<LoadInst>(I)) {
1879 PN->takeName(&I);
1880 LLVM_DEBUG(dbgs() << " to: " << *PN << "\n");
1881 I.replaceAllUsesWith(PN);
1882 }
1883}
1884
1886 SelectHandSpeculativity Spec,
1887 DomTreeUpdater &DTU) {
1888 if (auto *LI = dyn_cast<LoadInst>(&I))
1889 rewriteMemOpOfSelect(SelInst, *LI, Spec, DTU);
1890 else if (auto *SI = dyn_cast<StoreInst>(&I))
1891 rewriteMemOpOfSelect(SelInst, *SI, Spec, DTU);
1892 else
1893 llvm_unreachable_internal("Only for load and store.");
1894}
1895
1897 const RewriteableMemOps &Ops,
1898 IRBuilderTy &IRB, DomTreeUpdater *DTU) {
1899 bool CFGChanged = false;
1900 LLVM_DEBUG(dbgs() << " original select: " << SI << "\n");
1901
1902 for (const RewriteableMemOp &Op : Ops) {
1903 SelectHandSpeculativity Spec;
1904 Instruction *I;
1905 if (auto *const *US = std::get_if<UnspeculatableStore>(&Op)) {
1906 I = *US;
1907 } else {
1908 auto PSL = std::get<PossiblySpeculatableLoad>(Op);
1909 I = PSL.getPointer();
1910 Spec = PSL.getInt();
1911 }
1912 if (Spec.areAllSpeculatable()) {
1914 } else {
1915 assert(DTU && "Should not get here when not allowed to modify the CFG!");
1916 rewriteMemOpOfSelect(SI, *I, Spec, *DTU);
1917 CFGChanged = true;
1918 }
1919 I->eraseFromParent();
1920 }
1921
1922 for (User *U : make_early_inc_range(SI.users()))
1923 cast<BitCastInst>(U)->eraseFromParent();
1924 SI.eraseFromParent();
1925 return CFGChanged;
1926}
1927
1928/// Compute an adjusted pointer from Ptr by Offset bytes where the
1929/// resulting pointer has PointerTy.
1930static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr,
1932 const Twine &NamePrefix) {
1933 if (Offset != 0)
1934 Ptr = IRB.CreateInBoundsPtrAdd(Ptr, IRB.getInt(Offset),
1935 NamePrefix + "sroa_idx");
1936 return IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, PointerTy,
1937 NamePrefix + "sroa_cast");
1938}
1939
1940/// Compute the adjusted alignment for a load or store from an offset.
1944
1945/// Test whether we can convert a value from the old to the new type.
1946///
1947/// This predicate should be used to guard calls to convertValue in order to
1948/// ensure that we only try to convert viable values. The strategy is that we
1949/// will peel off single element struct and array wrappings to get to an
1950/// underlying value, and convert that value.
1951static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy,
1952 unsigned VScale = 0) {
1953 if (OldTy == NewTy)
1954 return true;
1955
1956 // For integer types, we can't handle any bit-width differences. This would
1957 // break both vector conversions with extension and introduce endianness
1958 // issues when in conjunction with loads and stores.
1959 if (isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) {
1961 cast<IntegerType>(NewTy)->getBitWidth() &&
1962 "We can't have the same bitwidth for different int types");
1963 return false;
1964 }
1965
1966 TypeSize NewSize = DL.getTypeSizeInBits(NewTy);
1967 TypeSize OldSize = DL.getTypeSizeInBits(OldTy);
1968
1969 if ((isa<ScalableVectorType>(NewTy) && isa<FixedVectorType>(OldTy)) ||
1970 (isa<ScalableVectorType>(OldTy) && isa<FixedVectorType>(NewTy))) {
1971 // Conversion is only possible when the size of scalable vectors is known.
1972 if (!VScale)
1973 return false;
1974
1975 // For ptr-to-int and int-to-ptr casts, the pointer side is resolved within
1976 // a single domain (either fixed or scalable). Any additional conversion
1977 // between fixed and scalable types is handled through integer types.
1978 auto OldVTy = OldTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(OldTy) : OldTy;
1979 auto NewVTy = NewTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(NewTy) : NewTy;
1980
1981 if (isa<ScalableVectorType>(NewTy)) {
1983 return false;
1984
1985 NewSize = TypeSize::getFixed(NewSize.getKnownMinValue() * VScale);
1986 } else {
1988 return false;
1989
1990 OldSize = TypeSize::getFixed(OldSize.getKnownMinValue() * VScale);
1991 }
1992 }
1993
1994 if (NewSize != OldSize)
1995 return false;
1996 if (!NewTy->isSingleValueType() || !OldTy->isSingleValueType())
1997 return false;
1998
1999 // We can convert pointers to integers and vice-versa. Same for vectors
2000 // of pointers and integers.
2001 OldTy = OldTy->getScalarType();
2002 NewTy = NewTy->getScalarType();
2003 if (NewTy->isPointerTy() || OldTy->isPointerTy()) {
2004 if (NewTy->isPointerTy() && OldTy->isPointerTy()) {
2005 unsigned OldAS = OldTy->getPointerAddressSpace();
2006 unsigned NewAS = NewTy->getPointerAddressSpace();
2007 // Convert pointers if they are pointers from the same address space or
2008 // different integral (not non-integral) address spaces with the same
2009 // pointer size.
2010 return OldAS == NewAS ||
2011 (!DL.isNonIntegralAddressSpace(OldAS) &&
2012 !DL.isNonIntegralAddressSpace(NewAS) &&
2013 DL.getPointerSize(OldAS) == DL.getPointerSize(NewAS));
2014 }
2015
2016 // We can convert integers to integral pointers, but not to non-integral
2017 // pointers.
2018 if (OldTy->isIntegerTy())
2019 return !DL.isNonIntegralPointerType(NewTy);
2020
2021 // We can convert integral pointers to integers, but non-integral pointers
2022 // need to remain pointers.
2023 if (!DL.isNonIntegralPointerType(OldTy))
2024 return NewTy->isIntegerTy();
2025
2026 return false;
2027 }
2028
2029 if (OldTy->isTargetExtTy() || NewTy->isTargetExtTy())
2030 return false;
2031
2032 return true;
2033}
2034
2035/// Test whether the given slice use can be promoted to a vector.
2036///
2037/// This function is called to test each entry in a partition which is slated
2038/// for a single slice.
2039static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S,
2040 VectorType *Ty,
2041 uint64_t ElementSize,
2042 const DataLayout &DL,
2043 unsigned VScale) {
2044 // First validate the slice offsets.
2045 uint64_t BeginOffset =
2046 std::max(S.beginOffset(), P.beginOffset()) - P.beginOffset();
2047 uint64_t BeginIndex = BeginOffset / ElementSize;
2048 if (BeginIndex * ElementSize != BeginOffset ||
2049 BeginIndex >= cast<FixedVectorType>(Ty)->getNumElements())
2050 return false;
2051 uint64_t EndOffset = std::min(S.endOffset(), P.endOffset()) - P.beginOffset();
2052 uint64_t EndIndex = EndOffset / ElementSize;
2053 if (EndIndex * ElementSize != EndOffset ||
2054 EndIndex > cast<FixedVectorType>(Ty)->getNumElements())
2055 return false;
2056
2057 assert(EndIndex > BeginIndex && "Empty vector!");
2058 uint64_t NumElements = EndIndex - BeginIndex;
2059 Type *SliceTy = (NumElements == 1)
2060 ? Ty->getElementType()
2061 : FixedVectorType::get(Ty->getElementType(), NumElements);
2062
2063 Type *SplitIntTy =
2064 Type::getIntNTy(Ty->getContext(), NumElements * ElementSize * 8);
2065
2066 Use *U = S.getUse();
2067
2068 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) {
2069 if (MI->isVolatile())
2070 return false;
2071 if (!S.isSplittable())
2072 return false; // Skip any unsplittable intrinsics.
2073 if (isa<MemSetInst>(MI)) {
2074 Type *SplatTy = Type::getIntNTy(Ty->getContext(), ElementSize * 8);
2075 if (!canConvertValue(DL, SplatTy, Ty->getElementType(), VScale))
2076 return false;
2077 }
2078 } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) {
2079 if (!II->isLifetimeStartOrEnd() && !II->isDroppable())
2080 return false;
2081 } else if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
2082 if (LI->isVolatile())
2083 return false;
2084 Type *LTy = LI->getType();
2085 // Disable vector promotion when there are loads or stores of an FCA.
2086 if (LTy->isStructTy())
2087 return false;
2088 if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) {
2089 assert(LTy->isIntegerTy());
2090 LTy = SplitIntTy;
2091 }
2092 if (!canConvertValue(DL, SliceTy, LTy, VScale))
2093 return false;
2094 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
2095 if (SI->isVolatile())
2096 return false;
2097 Type *STy = SI->getValueOperand()->getType();
2098 // Disable vector promotion when there are loads or stores of an FCA.
2099 if (STy->isStructTy())
2100 return false;
2101 if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) {
2102 assert(STy->isIntegerTy());
2103 STy = SplitIntTy;
2104 }
2105 if (!canConvertValue(DL, STy, SliceTy, VScale))
2106 return false;
2107 } else {
2108 return false;
2109 }
2110
2111 return true;
2112}
2113
2114/// Test whether any vector type in \p CandidateTys is viable for promotion.
2115///
2116/// This implements the necessary checking for \c isVectorPromotionViable over
2117/// all slices of the alloca for the given VectorType.
2118static VectorType *
2120 SmallVectorImpl<VectorType *> &CandidateTys,
2121 bool HaveCommonEltTy, Type *CommonEltTy,
2122 bool HaveVecPtrTy, bool HaveCommonVecPtrTy,
2123 VectorType *CommonVecPtrTy, unsigned VScale) {
2124 // If we didn't find a vector type, nothing to do here.
2125 if (CandidateTys.empty())
2126 return nullptr;
2127
2128 // Pointer-ness is sticky, if we had a vector-of-pointers candidate type,
2129 // then we should choose it, not some other alternative.
2130 // But, we can't perform a no-op pointer address space change via bitcast,
2131 // so if we didn't have a common pointer element type, bail.
2132 if (HaveVecPtrTy && !HaveCommonVecPtrTy)
2133 return nullptr;
2134
2135 // Try to pick the "best" element type out of the choices.
2136 if (!HaveCommonEltTy && HaveVecPtrTy) {
2137 // If there was a pointer element type, there's really only one choice.
2138 CandidateTys.clear();
2139 CandidateTys.push_back(CommonVecPtrTy);
2140 } else if (!HaveCommonEltTy && !HaveVecPtrTy) {
2141 // Integer-ify vector types.
2142 for (VectorType *&VTy : CandidateTys) {
2143 if (!VTy->getElementType()->isIntegerTy())
2144 VTy = cast<VectorType>(VTy->getWithNewType(IntegerType::getIntNTy(
2145 VTy->getContext(), VTy->getScalarSizeInBits())));
2146 }
2147
2148 // Rank the remaining candidate vector types. This is easy because we know
2149 // they're all integer vectors. We sort by ascending number of elements.
2150 auto RankVectorTypesComp = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2151 (void)DL;
2152 assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2153 DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2154 "Cannot have vector types of different sizes!");
2155 assert(RHSTy->getElementType()->isIntegerTy() &&
2156 "All non-integer types eliminated!");
2157 assert(LHSTy->getElementType()->isIntegerTy() &&
2158 "All non-integer types eliminated!");
2159 return cast<FixedVectorType>(RHSTy)->getNumElements() <
2160 cast<FixedVectorType>(LHSTy)->getNumElements();
2161 };
2162 auto RankVectorTypesEq = [&DL](VectorType *RHSTy, VectorType *LHSTy) {
2163 (void)DL;
2164 assert(DL.getTypeSizeInBits(RHSTy).getFixedValue() ==
2165 DL.getTypeSizeInBits(LHSTy).getFixedValue() &&
2166 "Cannot have vector types of different sizes!");
2167 assert(RHSTy->getElementType()->isIntegerTy() &&
2168 "All non-integer types eliminated!");
2169 assert(LHSTy->getElementType()->isIntegerTy() &&
2170 "All non-integer types eliminated!");
2171 return cast<FixedVectorType>(RHSTy)->getNumElements() ==
2172 cast<FixedVectorType>(LHSTy)->getNumElements();
2173 };
2174 llvm::sort(CandidateTys, RankVectorTypesComp);
2175 CandidateTys.erase(llvm::unique(CandidateTys, RankVectorTypesEq),
2176 CandidateTys.end());
2177 } else {
2178// The only way to have the same element type in every vector type is to
2179// have the same vector type. Check that and remove all but one.
2180#ifndef NDEBUG
2181 for (VectorType *VTy : CandidateTys) {
2182 assert(VTy->getElementType() == CommonEltTy &&
2183 "Unaccounted for element type!");
2184 assert(VTy == CandidateTys[0] &&
2185 "Different vector types with the same element type!");
2186 }
2187#endif
2188 CandidateTys.resize(1);
2189 }
2190
2191 // FIXME: hack. Do we have a named constant for this?
2192 // SDAG SDNode can't have more than 65535 operands.
2193 llvm::erase_if(CandidateTys, [](VectorType *VTy) {
2194 return cast<FixedVectorType>(VTy)->getNumElements() >
2195 std::numeric_limits<unsigned short>::max();
2196 });
2197
2198 // Find a vector type viable for promotion by iterating over all slices.
2199 auto *VTy = llvm::find_if(CandidateTys, [&](VectorType *VTy) -> bool {
2200 uint64_t ElementSize =
2201 DL.getTypeSizeInBits(VTy->getElementType()).getFixedValue();
2202
2203 // While the definition of LLVM vectors is bitpacked, we don't support sizes
2204 // that aren't byte sized.
2205 if (ElementSize % 8)
2206 return false;
2207 assert((DL.getTypeSizeInBits(VTy).getFixedValue() % 8) == 0 &&
2208 "vector size not a multiple of element size?");
2209 ElementSize /= 8;
2210
2211 for (const Slice &S : P)
2212 if (!isVectorPromotionViableForSlice(P, S, VTy, ElementSize, DL, VScale))
2213 return false;
2214
2215 for (const Slice *S : P.splitSliceTails())
2216 if (!isVectorPromotionViableForSlice(P, *S, VTy, ElementSize, DL, VScale))
2217 return false;
2218
2219 return true;
2220 });
2221 return VTy != CandidateTys.end() ? *VTy : nullptr;
2222}
2223
2225 SetVector<Type *> &OtherTys, ArrayRef<VectorType *> CandidateTysCopy,
2226 function_ref<void(Type *)> CheckCandidateType, Partition &P,
2227 const DataLayout &DL, SmallVectorImpl<VectorType *> &CandidateTys,
2228 bool &HaveCommonEltTy, Type *&CommonEltTy, bool &HaveVecPtrTy,
2229 bool &HaveCommonVecPtrTy, VectorType *&CommonVecPtrTy, unsigned VScale) {
2230 [[maybe_unused]] VectorType *OriginalElt =
2231 CandidateTysCopy.size() ? CandidateTysCopy[0] : nullptr;
2232 // Consider additional vector types where the element type size is a
2233 // multiple of load/store element size.
2234 for (Type *Ty : OtherTys) {
2236 continue;
2237 unsigned TypeSize = DL.getTypeSizeInBits(Ty).getFixedValue();
2238 // Make a copy of CandidateTys and iterate through it, because we
2239 // might append to CandidateTys in the loop.
2240 for (VectorType *const VTy : CandidateTysCopy) {
2241 // The elements in the copy should remain invariant throughout the loop
2242 assert(CandidateTysCopy[0] == OriginalElt && "Different Element");
2243 unsigned VectorSize = DL.getTypeSizeInBits(VTy).getFixedValue();
2244 unsigned ElementSize =
2245 DL.getTypeSizeInBits(VTy->getElementType()).getFixedValue();
2246 if (TypeSize != VectorSize && TypeSize != ElementSize &&
2247 VectorSize % TypeSize == 0) {
2248 VectorType *NewVTy = VectorType::get(Ty, VectorSize / TypeSize, false);
2249 CheckCandidateType(NewVTy);
2250 }
2251 }
2252 }
2253
2255 P, DL, CandidateTys, HaveCommonEltTy, CommonEltTy, HaveVecPtrTy,
2256 HaveCommonVecPtrTy, CommonVecPtrTy, VScale);
2257}
2258
2259/// Test whether the given alloca partitioning and range of slices can be
2260/// promoted to a vector.
2261///
2262/// This is a quick test to check whether we can rewrite a particular alloca
2263/// partition (and its newly formed alloca) into a vector alloca with only
2264/// whole-vector loads and stores such that it could be promoted to a vector
2265/// SSA value. We only can ensure this for a limited set of operations, and we
2266/// don't want to do the rewrites unless we are confident that the result will
2267/// be promotable, so we have an early test here.
2269 unsigned VScale) {
2270 // Collect the candidate types for vector-based promotion. Also track whether
2271 // we have different element types.
2272 SmallVector<VectorType *, 4> CandidateTys;
2273 SetVector<Type *> LoadStoreTys;
2274 SetVector<Type *> DeferredTys;
2275 Type *CommonEltTy = nullptr;
2276 VectorType *CommonVecPtrTy = nullptr;
2277 bool HaveVecPtrTy = false;
2278 bool HaveCommonEltTy = true;
2279 bool HaveCommonVecPtrTy = true;
2280 auto CheckCandidateType = [&](Type *Ty) {
2281 if (auto *VTy = dyn_cast<FixedVectorType>(Ty)) {
2282 // Return if bitcast to vectors is different for total size in bits.
2283 if (!CandidateTys.empty()) {
2284 VectorType *V = CandidateTys[0];
2285 if (DL.getTypeSizeInBits(VTy).getFixedValue() !=
2286 DL.getTypeSizeInBits(V).getFixedValue()) {
2287 CandidateTys.clear();
2288 return;
2289 }
2290 }
2291 CandidateTys.push_back(VTy);
2292 Type *EltTy = VTy->getElementType();
2293
2294 if (!CommonEltTy)
2295 CommonEltTy = EltTy;
2296 else if (CommonEltTy != EltTy)
2297 HaveCommonEltTy = false;
2298
2299 if (EltTy->isPointerTy()) {
2300 HaveVecPtrTy = true;
2301 if (!CommonVecPtrTy)
2302 CommonVecPtrTy = VTy;
2303 else if (CommonVecPtrTy != VTy)
2304 HaveCommonVecPtrTy = false;
2305 }
2306 }
2307 };
2308
2309 // Put load and store types into a set for de-duplication.
2310 for (const Slice &S : P) {
2311 Type *Ty;
2312 if (auto *LI = dyn_cast<LoadInst>(S.getUse()->getUser()))
2313 Ty = LI->getType();
2314 else if (auto *SI = dyn_cast<StoreInst>(S.getUse()->getUser()))
2315 Ty = SI->getValueOperand()->getType();
2316 else
2317 continue;
2318
2319 auto CandTy = Ty->getScalarType();
2320 if (CandTy->isPointerTy() && (S.beginOffset() != P.beginOffset() ||
2321 S.endOffset() != P.endOffset())) {
2322 DeferredTys.insert(Ty);
2323 continue;
2324 }
2325
2326 LoadStoreTys.insert(Ty);
2327 // Consider any loads or stores that are the exact size of the slice.
2328 if (S.beginOffset() == P.beginOffset() && S.endOffset() == P.endOffset())
2329 CheckCandidateType(Ty);
2330 }
2331
2332 SmallVector<VectorType *, 4> CandidateTysCopy = CandidateTys;
2334 LoadStoreTys, CandidateTysCopy, CheckCandidateType, P, DL,
2335 CandidateTys, HaveCommonEltTy, CommonEltTy, HaveVecPtrTy,
2336 HaveCommonVecPtrTy, CommonVecPtrTy, VScale))
2337 return VTy;
2338
2339 CandidateTys.clear();
2341 DeferredTys, CandidateTysCopy, CheckCandidateType, P, DL, CandidateTys,
2342 HaveCommonEltTy, CommonEltTy, HaveVecPtrTy, HaveCommonVecPtrTy,
2343 CommonVecPtrTy, VScale);
2344}
2345
2346/// Test whether a slice of an alloca is valid for integer widening.
2347///
2348/// This implements the necessary checking for the \c isIntegerWideningViable
2349/// test below on a single slice of the alloca.
2350static bool isIntegerWideningViableForSlice(const Slice &S,
2351 uint64_t AllocBeginOffset,
2352 Type *AllocaTy,
2353 const DataLayout &DL,
2354 bool &WholeAllocaOp) {
2355 uint64_t Size = DL.getTypeStoreSize(AllocaTy).getFixedValue();
2356
2357 uint64_t RelBegin = S.beginOffset() - AllocBeginOffset;
2358 uint64_t RelEnd = S.endOffset() - AllocBeginOffset;
2359
2360 Use *U = S.getUse();
2361
2362 // Lifetime intrinsics operate over the whole alloca whose sizes are usually
2363 // larger than other load/store slices (RelEnd > Size). But lifetime are
2364 // always promotable and should not impact other slices' promotability of the
2365 // partition.
2366 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) {
2367 if (II->isLifetimeStartOrEnd() || II->isDroppable())
2368 return true;
2369 }
2370
2371 // We can't reasonably handle cases where the load or store extends past
2372 // the end of the alloca's type and into its padding.
2373 if (RelEnd > Size)
2374 return false;
2375
2376 if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) {
2377 if (LI->isVolatile())
2378 return false;
2379 // We can't handle loads that extend past the allocated memory.
2380 TypeSize LoadSize = DL.getTypeStoreSize(LI->getType());
2381 if (!LoadSize.isFixed() || LoadSize.getFixedValue() > Size)
2382 return false;
2383 // So far, AllocaSliceRewriter does not support widening split slice tails
2384 // in rewriteIntegerLoad.
2385 if (S.beginOffset() < AllocBeginOffset)
2386 return false;
2387 // Note that we don't count vector loads or stores as whole-alloca
2388 // operations which enable integer widening because we would prefer to use
2389 // vector widening instead.
2390 if (!isa<VectorType>(LI->getType()) && RelBegin == 0 && RelEnd == Size)
2391 WholeAllocaOp = true;
2392 if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) {
2393 if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy).getFixedValue())
2394 return false;
2395 } else if (RelBegin != 0 || RelEnd != Size ||
2396 !canConvertValue(DL, AllocaTy, LI->getType())) {
2397 // Non-integer loads need to be convertible from the alloca type so that
2398 // they are promotable.
2399 return false;
2400 }
2401 } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) {
2402 Type *ValueTy = SI->getValueOperand()->getType();
2403 if (SI->isVolatile())
2404 return false;
2405 // We can't handle stores that extend past the allocated memory.
2406 TypeSize StoreSize = DL.getTypeStoreSize(ValueTy);
2407 if (!StoreSize.isFixed() || StoreSize.getFixedValue() > Size)
2408 return false;
2409 // So far, AllocaSliceRewriter does not support widening split slice tails
2410 // in rewriteIntegerStore.
2411 if (S.beginOffset() < AllocBeginOffset)
2412 return false;
2413 // Note that we don't count vector loads or stores as whole-alloca
2414 // operations which enable integer widening because we would prefer to use
2415 // vector widening instead.
2416 if (!isa<VectorType>(ValueTy) && RelBegin == 0 && RelEnd == Size)
2417 WholeAllocaOp = true;
2418 if (IntegerType *ITy = dyn_cast<IntegerType>(ValueTy)) {
2419 if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy).getFixedValue())
2420 return false;
2421 } else if (RelBegin != 0 || RelEnd != Size ||
2422 !canConvertValue(DL, ValueTy, AllocaTy)) {
2423 // Non-integer stores need to be convertible to the alloca type so that
2424 // they are promotable.
2425 return false;
2426 }
2427 } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) {
2428 if (MI->isVolatile() || !isa<Constant>(MI->getLength()))
2429 return false;
2430 if (!S.isSplittable())
2431 return false; // Skip any unsplittable intrinsics.
2432 } else {
2433 return false;
2434 }
2435
2436 return true;
2437}
2438
2439/// Test whether the given alloca partition's integer operations can be
2440/// widened to promotable ones.
2441///
2442/// This is a quick test to check whether we can rewrite the integer loads and
2443/// stores to a particular alloca into wider loads and stores and be able to
2444/// promote the resulting alloca.
2445static bool isIntegerWideningViable(Partition &P, Type *AllocaTy,
2446 const DataLayout &DL) {
2447 uint64_t SizeInBits = DL.getTypeSizeInBits(AllocaTy).getFixedValue();
2448 // Don't create integer types larger than the maximum bitwidth.
2449 if (SizeInBits > IntegerType::MAX_INT_BITS)
2450 return false;
2451
2452 // Don't try to handle allocas with bit-padding.
2453 if (SizeInBits != DL.getTypeStoreSizeInBits(AllocaTy).getFixedValue())
2454 return false;
2455
2456 // We need to ensure that an integer type with the appropriate bitwidth can
2457 // be converted to the alloca type, whatever that is. We don't want to force
2458 // the alloca itself to have an integer type if there is a more suitable one.
2459 Type *IntTy = Type::getIntNTy(AllocaTy->getContext(), SizeInBits);
2460 if (!canConvertValue(DL, AllocaTy, IntTy) ||
2461 !canConvertValue(DL, IntTy, AllocaTy))
2462 return false;
2463
2464 // While examining uses, we ensure that the alloca has a covering load or
2465 // store. We don't want to widen the integer operations only to fail to
2466 // promote due to some other unsplittable entry (which we may make splittable
2467 // later). However, if there are only splittable uses, go ahead and assume
2468 // that we cover the alloca.
2469 // FIXME: We shouldn't consider split slices that happen to start in the
2470 // partition here...
2471 bool WholeAllocaOp = P.empty() && DL.isLegalInteger(SizeInBits);
2472
2473 for (const Slice &S : P)
2474 if (!isIntegerWideningViableForSlice(S, P.beginOffset(), AllocaTy, DL,
2475 WholeAllocaOp))
2476 return false;
2477
2478 for (const Slice *S : P.splitSliceTails())
2479 if (!isIntegerWideningViableForSlice(*S, P.beginOffset(), AllocaTy, DL,
2480 WholeAllocaOp))
2481 return false;
2482
2483 return WholeAllocaOp;
2484}
2485
2486static Value *extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V,
2488 const Twine &Name) {
2489 LLVM_DEBUG(dbgs() << " start: " << *V << "\n");
2490 IntegerType *IntTy = cast<IntegerType>(V->getType());
2491 assert(DL.getTypeStoreSize(Ty).getFixedValue() + Offset <=
2492 DL.getTypeStoreSize(IntTy).getFixedValue() &&
2493 "Element extends past full value");
2494 uint64_t ShAmt = 8 * Offset;
2495 if (DL.isBigEndian())
2496 ShAmt = 8 * (DL.getTypeStoreSize(IntTy).getFixedValue() -
2497 DL.getTypeStoreSize(Ty).getFixedValue() - Offset);
2498 if (ShAmt) {
2499 V = IRB.CreateLShr(V, ShAmt, Name + ".shift");
2500 LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n");
2501 }
2502 assert(Ty->getBitWidth() <= IntTy->getBitWidth() &&
2503 "Cannot extract to a larger integer!");
2504 if (Ty != IntTy) {
2505 V = IRB.CreateTrunc(V, Ty, Name + ".trunc");
2506 LLVM_DEBUG(dbgs() << " trunced: " << *V << "\n");
2507 }
2508 return V;
2509}
2510
2511static Value *insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old,
2512 Value *V, uint64_t Offset, const Twine &Name) {
2513 IntegerType *IntTy = cast<IntegerType>(Old->getType());
2514 IntegerType *Ty = cast<IntegerType>(V->getType());
2515 assert(Ty->getBitWidth() <= IntTy->getBitWidth() &&
2516 "Cannot insert a larger integer!");
2517 LLVM_DEBUG(dbgs() << " start: " << *V << "\n");
2518 if (Ty != IntTy) {
2519 V = IRB.CreateZExt(V, IntTy, Name + ".ext");
2520 LLVM_DEBUG(dbgs() << " extended: " << *V << "\n");
2521 }
2522 assert(DL.getTypeStoreSize(Ty).getFixedValue() + Offset <=
2523 DL.getTypeStoreSize(IntTy).getFixedValue() &&
2524 "Element store outside of alloca store");
2525 uint64_t ShAmt = 8 * Offset;
2526 if (DL.isBigEndian())
2527 ShAmt = 8 * (DL.getTypeStoreSize(IntTy).getFixedValue() -
2528 DL.getTypeStoreSize(Ty).getFixedValue() - Offset);
2529 if (ShAmt) {
2530 V = IRB.CreateShl(V, ShAmt, Name + ".shift");
2531 LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n");
2532 }
2533
2534 if (ShAmt || Ty->getBitWidth() < IntTy->getBitWidth()) {
2535 APInt Mask = ~Ty->getMask().zext(IntTy->getBitWidth()).shl(ShAmt);
2536 Old = IRB.CreateAnd(Old, Mask, Name + ".mask");
2537 LLVM_DEBUG(dbgs() << " masked: " << *Old << "\n");
2538 V = IRB.CreateOr(Old, V, Name + ".insert");
2539 LLVM_DEBUG(dbgs() << " inserted: " << *V << "\n");
2540 }
2541 return V;
2542}
2543
2544static Value *extractVector(IRBuilderTy &IRB, Value *V, unsigned BeginIndex,
2545 unsigned EndIndex, const Twine &Name) {
2546 auto *VecTy = cast<FixedVectorType>(V->getType());
2547 unsigned NumElements = EndIndex - BeginIndex;
2548 assert(NumElements <= VecTy->getNumElements() && "Too many elements!");
2549
2550 if (NumElements == VecTy->getNumElements())
2551 return V;
2552
2553 if (NumElements == 1) {
2554 V = IRB.CreateExtractElement(V, BeginIndex, Name + ".extract");
2555 LLVM_DEBUG(dbgs() << " extract: " << *V << "\n");
2556 return V;
2557 }
2558
2559 auto Mask = llvm::to_vector<8>(llvm::seq<int>(BeginIndex, EndIndex));
2560 V = IRB.CreateShuffleVector(V, Mask, Name + ".extract");
2561 LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n");
2562 return V;
2563}
2564
2565static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V,
2566 unsigned BeginIndex, const Twine &Name) {
2567 VectorType *VecTy = cast<VectorType>(Old->getType());
2568 assert(VecTy && "Can only insert a vector into a vector");
2569
2570 VectorType *Ty = dyn_cast<VectorType>(V->getType());
2571 if (!Ty) {
2572 // Single element to insert.
2573 V = IRB.CreateInsertElement(Old, V, BeginIndex, Name + ".insert");
2574 LLVM_DEBUG(dbgs() << " insert: " << *V << "\n");
2575 return V;
2576 }
2577
2578 unsigned NumSubElements = cast<FixedVectorType>(Ty)->getNumElements();
2579 unsigned NumElements = cast<FixedVectorType>(VecTy)->getNumElements();
2580
2581 assert(NumSubElements <= NumElements && "Too many elements!");
2582 if (NumSubElements == NumElements) {
2583 assert(V->getType() == VecTy && "Vector type mismatch");
2584 return V;
2585 }
2586 unsigned EndIndex = BeginIndex + NumSubElements;
2587
2588 // When inserting a smaller vector into the larger to store, we first
2589 // use a shuffle vector to widen it with undef elements, and then
2590 // a second shuffle vector to select between the loaded vector and the
2591 // incoming vector.
2593 Mask.reserve(NumElements);
2594 for (unsigned Idx = 0; Idx != NumElements; ++Idx)
2595 if (Idx >= BeginIndex && Idx < EndIndex)
2596 Mask.push_back(Idx - BeginIndex);
2597 else
2598 Mask.push_back(-1);
2599 V = IRB.CreateShuffleVector(V, Mask, Name + ".expand");
2600 LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n");
2601
2602 Mask.clear();
2603 for (unsigned Idx = 0; Idx != NumElements; ++Idx)
2604 if (Idx >= BeginIndex && Idx < EndIndex)
2605 Mask.push_back(Idx);
2606 else
2607 Mask.push_back(Idx + NumElements);
2608 V = IRB.CreateShuffleVector(V, Old, Mask, Name + "blend");
2609 LLVM_DEBUG(dbgs() << " blend: " << *V << "\n");
2610 return V;
2611}
2612
2613/// This function takes two vector values and combines them into a single vector
2614/// by concatenating their elements. The function handles:
2615///
2616/// 1. Element type mismatch: If either vector's element type differs from
2617/// NewAIEltType, the function bitcasts the vector to use NewAIEltType while
2618/// preserving the total bit width (adjusting the number of elements
2619/// accordingly).
2620///
2621/// 2. Size mismatch: After transforming the vectors to have the desired element
2622/// type, if the two vectors have different numbers of elements, the smaller
2623/// vector is extended with poison values to match the size of the larger
2624/// vector before concatenation.
2625///
2626/// 3. Concatenation: The vectors are merged using a shuffle operation that
2627/// places all elements of V0 first, followed by all elements of V1.
2628///
2629/// \param V0 The first vector to merge (must be a vector type)
2630/// \param V1 The second vector to merge (must be a vector type)
2631/// \param DL The data layout for size calculations
2632/// \param NewAIEltTy The desired element type for the result vector
2633/// \param Builder IRBuilder for creating new instructions
2634/// \return A new vector containing all elements from V0 followed by all
2635/// elements from V1
2637 Type *NewAIEltTy, IRBuilder<> &Builder) {
2638 // V0 and V1 are vectors
2639 // Create a new vector type with combined elements
2640 // Use ShuffleVector to concatenate the vectors
2641 auto *VecType0 = cast<FixedVectorType>(V0->getType());
2642 auto *VecType1 = cast<FixedVectorType>(V1->getType());
2643
2644 // If V0/V1 element types are different from NewAllocaElementType,
2645 // we need to introduce bitcasts before merging them
2646 auto BitcastIfNeeded = [&](Value *&V, FixedVectorType *&VecType,
2647 const char *DebugName) {
2648 Type *EltType = VecType->getElementType();
2649 if (EltType != NewAIEltTy) {
2650 // Calculate new number of elements to maintain same bit width
2651 unsigned TotalBits =
2652 VecType->getNumElements() * DL.getTypeSizeInBits(EltType);
2653 unsigned NewNumElts = TotalBits / DL.getTypeSizeInBits(NewAIEltTy);
2654
2655 auto *NewVecType = FixedVectorType::get(NewAIEltTy, NewNumElts);
2656 V = Builder.CreateBitCast(V, NewVecType);
2657 VecType = NewVecType;
2658 LLVM_DEBUG(dbgs() << " bitcast " << DebugName << ": " << *V << "\n");
2659 }
2660 };
2661
2662 BitcastIfNeeded(V0, VecType0, "V0");
2663 BitcastIfNeeded(V1, VecType1, "V1");
2664
2665 unsigned NumElts0 = VecType0->getNumElements();
2666 unsigned NumElts1 = VecType1->getNumElements();
2667
2668 SmallVector<int, 16> ShuffleMask;
2669
2670 if (NumElts0 == NumElts1) {
2671 for (unsigned i = 0; i < NumElts0 + NumElts1; ++i)
2672 ShuffleMask.push_back(i);
2673 } else {
2674 // If two vectors have different sizes, we need to extend
2675 // the smaller vector to the size of the larger vector.
2676 unsigned SmallSize = std::min(NumElts0, NumElts1);
2677 unsigned LargeSize = std::max(NumElts0, NumElts1);
2678 bool IsV0Smaller = NumElts0 < NumElts1;
2679 Value *&ExtendedVec = IsV0Smaller ? V0 : V1;
2680 SmallVector<int, 16> ExtendMask;
2681 for (unsigned i = 0; i < SmallSize; ++i)
2682 ExtendMask.push_back(i);
2683 for (unsigned i = SmallSize; i < LargeSize; ++i)
2684 ExtendMask.push_back(PoisonMaskElem);
2685 ExtendedVec = Builder.CreateShuffleVector(
2686 ExtendedVec, PoisonValue::get(ExtendedVec->getType()), ExtendMask);
2687 LLVM_DEBUG(dbgs() << " shufflevector: " << *ExtendedVec << "\n");
2688 for (unsigned i = 0; i < NumElts0; ++i)
2689 ShuffleMask.push_back(i);
2690 for (unsigned i = 0; i < NumElts1; ++i)
2691 ShuffleMask.push_back(LargeSize + i);
2692 }
2693
2694 return Builder.CreateShuffleVector(V0, V1, ShuffleMask);
2695}
2696
2697namespace {
2698
2699/// Visitor to rewrite instructions using p particular slice of an alloca
2700/// to use a new alloca.
2701///
2702/// Also implements the rewriting to vector-based accesses when the partition
2703/// passes the isVectorPromotionViable predicate. Most of the rewriting logic
2704/// lives here.
2705class AllocaSliceRewriter : public InstVisitor<AllocaSliceRewriter, bool> {
2706 // Befriend the base class so it can delegate to private visit methods.
2707 friend class InstVisitor<AllocaSliceRewriter, bool>;
2708
2709 using Base = InstVisitor<AllocaSliceRewriter, bool>;
2710
2711 const DataLayout &DL;
2712 AllocaSlices &AS;
2713 SROA &Pass;
2714 AllocaInst &OldAI, &NewAI;
2715 const uint64_t NewAllocaBeginOffset, NewAllocaEndOffset;
2716 Type *NewAllocaTy;
2717
2718 // This is a convenience and flag variable that will be null unless the new
2719 // alloca's integer operations should be widened to this integer type due to
2720 // passing isIntegerWideningViable above. If it is non-null, the desired
2721 // integer type will be stored here for easy access during rewriting.
2722 IntegerType *IntTy;
2723
2724 // If we are rewriting an alloca partition which can be written as pure
2725 // vector operations, we stash extra information here. When VecTy is
2726 // non-null, we have some strict guarantees about the rewritten alloca:
2727 // - The new alloca is exactly the size of the vector type here.
2728 // - The accesses all either map to the entire vector or to a single
2729 // element.
2730 // - The set of accessing instructions is only one of those handled above
2731 // in isVectorPromotionViable. Generally these are the same access kinds
2732 // which are promotable via mem2reg.
2733 VectorType *VecTy;
2734 Type *ElementTy;
2735 uint64_t ElementSize;
2736
2737 // The original offset of the slice currently being rewritten relative to
2738 // the original alloca.
2739 uint64_t BeginOffset = 0;
2740 uint64_t EndOffset = 0;
2741
2742 // The new offsets of the slice currently being rewritten relative to the
2743 // original alloca.
2744 uint64_t NewBeginOffset = 0, NewEndOffset = 0;
2745
2746 uint64_t SliceSize = 0;
2747 bool IsSplittable = false;
2748 bool IsSplit = false;
2749 Use *OldUse = nullptr;
2750 Instruction *OldPtr = nullptr;
2751
2752 // Track post-rewrite users which are PHI nodes and Selects.
2753 SmallSetVector<PHINode *, 8> &PHIUsers;
2754 SmallSetVector<SelectInst *, 8> &SelectUsers;
2755
2756 // Utility IR builder, whose name prefix is setup for each visited use, and
2757 // the insertion point is set to point to the user.
2758 IRBuilderTy IRB;
2759
2760 // Return the new alloca, addrspacecasted if required to avoid changing the
2761 // addrspace of a volatile access.
2762 Value *getPtrToNewAI(unsigned AddrSpace, bool IsVolatile) {
2763 if (!IsVolatile || AddrSpace == NewAI.getType()->getPointerAddressSpace())
2764 return &NewAI;
2765
2766 Type *AccessTy = IRB.getPtrTy(AddrSpace);
2767 return IRB.CreateAddrSpaceCast(&NewAI, AccessTy);
2768 }
2769
2770public:
2771 AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &AS, SROA &Pass,
2772 AllocaInst &OldAI, AllocaInst &NewAI, Type *NewAllocaTy,
2773 uint64_t NewAllocaBeginOffset,
2774 uint64_t NewAllocaEndOffset, bool IsIntegerPromotable,
2775 VectorType *PromotableVecTy,
2776 SmallSetVector<PHINode *, 8> &PHIUsers,
2777 SmallSetVector<SelectInst *, 8> &SelectUsers)
2778 : DL(DL), AS(AS), Pass(Pass), OldAI(OldAI), NewAI(NewAI),
2779 NewAllocaBeginOffset(NewAllocaBeginOffset),
2780 NewAllocaEndOffset(NewAllocaEndOffset), NewAllocaTy(NewAllocaTy),
2781 IntTy(IsIntegerPromotable
2782 ? Type::getIntNTy(
2783 NewAI.getContext(),
2784 DL.getTypeSizeInBits(NewAllocaTy).getFixedValue())
2785 : nullptr),
2786 VecTy(PromotableVecTy),
2787 ElementTy(VecTy ? VecTy->getElementType() : nullptr),
2788 ElementSize(VecTy ? DL.getTypeSizeInBits(ElementTy).getFixedValue() / 8
2789 : 0),
2790 PHIUsers(PHIUsers), SelectUsers(SelectUsers),
2791 IRB(NewAI.getContext(), ConstantFolder()) {
2792 if (VecTy) {
2793 assert((DL.getTypeSizeInBits(ElementTy).getFixedValue() % 8) == 0 &&
2794 "Only multiple-of-8 sized vector elements are viable");
2795 ++NumVectorized;
2796 }
2797 assert((!IntTy && !VecTy) || (IntTy && !VecTy) || (!IntTy && VecTy));
2798 }
2799
2800 bool visit(AllocaSlices::const_iterator I) {
2801 bool CanSROA = true;
2802 BeginOffset = I->beginOffset();
2803 EndOffset = I->endOffset();
2804 IsSplittable = I->isSplittable();
2805 IsSplit =
2806 BeginOffset < NewAllocaBeginOffset || EndOffset > NewAllocaEndOffset;
2807 LLVM_DEBUG(dbgs() << " rewriting " << (IsSplit ? "split " : ""));
2808 LLVM_DEBUG(AS.printSlice(dbgs(), I, ""));
2809 LLVM_DEBUG(dbgs() << "\n");
2810
2811 // Compute the intersecting offset range.
2812 assert(BeginOffset < NewAllocaEndOffset);
2813 assert(EndOffset > NewAllocaBeginOffset);
2814 NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset);
2815 NewEndOffset = std::min(EndOffset, NewAllocaEndOffset);
2816
2817 SliceSize = NewEndOffset - NewBeginOffset;
2818 LLVM_DEBUG(dbgs() << " Begin:(" << BeginOffset << ", " << EndOffset
2819 << ") NewBegin:(" << NewBeginOffset << ", "
2820 << NewEndOffset << ") NewAllocaBegin:("
2821 << NewAllocaBeginOffset << ", " << NewAllocaEndOffset
2822 << ")\n");
2823 assert(IsSplit || NewBeginOffset == BeginOffset);
2824 OldUse = I->getUse();
2825 OldPtr = cast<Instruction>(OldUse->get());
2826
2827 Instruction *OldUserI = cast<Instruction>(OldUse->getUser());
2828 IRB.SetInsertPoint(OldUserI);
2829 IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc());
2830 // Avoid materializing the name prefix when it is discarded anyway.
2831 if (!IRB.getContext().shouldDiscardValueNames())
2832 IRB.getInserter().SetNamePrefix(Twine(NewAI.getName()) + "." +
2833 Twine(BeginOffset) + ".");
2834
2835 CanSROA &= visit(cast<Instruction>(OldUse->getUser()));
2836 if (VecTy || IntTy)
2837 assert(CanSROA);
2838 return CanSROA;
2839 }
2840
2841 /// Attempts to rewrite a partition using tree-structured merge optimization.
2842 ///
2843 /// This function handles two patterns. Both produce an O(log n) tree of
2844 /// shufflevectors in place of the linear expand+blend chain that SROA would
2845 /// otherwise emit for each partial store.
2846 ///
2847 /// Pattern 1 (stores-only):
2848 /// Multiple non-overlapping partial stores completely fill the alloca
2849 /// and there is exactly one full-width load coming after the stores.
2850 /// The stores are tree-merged into a single vector and stored once.
2851 ///
2852 /// Example transformation:
2853 /// Before: (stores do not have to be in order)
2854 /// %alloca = alloca <8 x float>
2855 /// store <2 x float> %val0, ptr %alloca ; offset 0-1
2856 /// store <2 x float> %val2, ptr %alloca+16 ; offset 4-5
2857 /// store <2 x float> %val1, ptr %alloca+8 ; offset 2-3
2858 /// store <2 x float> %val3, ptr %alloca+24 ; offset 6-7
2859 /// %r = load <8 x float>, ptr %alloca
2860 ///
2861 /// After: tree of shufflevectors producing <8 x float> directly.
2862 ///
2863 /// Pattern 2 (init + RMW, possibly multi-round):
2864 /// A single full-width init store, followed by partial loads and
2865 /// partial stores that read-modify-write the alloca one or more
2866 /// times, optionally followed by a full-width load. The only
2867 /// structural requirement is that the distinct [begin, end) ranges
2868 /// touched by the partial loads and stores, taken together, tile
2869 /// the alloca disjointly.
2870 ///
2871 /// We keep a map from each slice range to the SSA value that
2872 /// currently lives there, `SliceValues[r] -> Value*`:
2873 /// - initialize each entry to the corresponding piece of the
2874 /// init store's value (via a shufflevector picking the
2875 /// range's elements out of the init value),
2876 /// - walk partial loads and stores in block order,
2877 /// - for a partial load at range r: RAUW with `SliceValues[r]`,
2878 /// - for a partial store at range r: update `SliceValues[r]` to
2879 /// the stored value and drop the store.
2880 /// At the end, the final `SliceValues[r]` entries are tree-merged
2881 /// (in range order) into a single store to the alloca, and the
2882 /// optional full-width load is replaced by a load of the alloca.
2883 ///
2884 /// Because the ranges are disjoint by construction, a store at one
2885 /// range cannot affect another range's tracked value, so a single
2886 /// block-order walk correctly tracks the memory state at each
2887 /// range. The algorithm handles multi-round RMW, partial loads
2888 /// and stores interleaved in any order, read-only slices (the
2889 /// tracked value stays at the init extract), and write-only
2890 /// slices (the tracked value never flows into a load).
2891 ///
2892 /// \param P The partition to analyze and potentially rewrite
2893 /// \return An optional vector of values that were deleted during the
2894 /// rewrite, or std::nullopt if the partition cannot be optimized.
2895 std::optional<SmallVector<Value *, 4>>
2896 rewriteTreeStructuredMerge(Partition &P) {
2897 // No tail slices that overlap with the partition
2898 if (P.splitSliceTails().size() > 0)
2899 return std::nullopt;
2900
2901 // Structure to hold store information
2902 struct StoreInfo {
2903 StoreInst *Store;
2904 uint64_t BeginOffset;
2905 uint64_t EndOffset;
2906 Value *StoredValue;
2907 StoreInfo(StoreInst *SI, uint64_t Begin, uint64_t End, Value *Val)
2908 : Store(SI), BeginOffset(Begin), EndOffset(End), StoredValue(Val) {}
2909 };
2910 struct LoadInfo {
2911 LoadInst *Load;
2912 uint64_t BeginOffset;
2913 uint64_t EndOffset;
2914 };
2915
2916 SmallVector<StoreInfo, 4> StoreInfos; // partial stores only
2917 SmallVector<LoadInfo, 4> LoadInfos; // partial loads only
2918 LoadInst *FullLoad = nullptr; // optional full-width load
2919 StoreInst *InitStore = nullptr; // optional full-width init store
2920
2921 // If the new alloca is a fixed vector type, we use its element type as the
2922 // allocated element type, otherwise we use i8 as the allocated element
2923 Type *AllocatedEltTy =
2924 isa<FixedVectorType>(NewAllocaTy)
2925 ? cast<FixedVectorType>(NewAllocaTy)->getElementType()
2926 : Type::getInt8Ty(NewAI.getContext());
2927 unsigned AllocatedEltTySize = DL.getTypeSizeInBits(AllocatedEltTy);
2928
2929 // Helper to check if a type is
2930 // 1. A fixed vector type
2931 // 2. The element type is not a pointer
2932 // 3. The element type size is byte-aligned
2933 // We only handle the cases that the ld/st meet these conditions
2934 auto IsTypeValidForTreeStructuredMerge = [&](Type *Ty) -> bool {
2935 auto *FixedVecTy = dyn_cast<FixedVectorType>(Ty);
2936 return FixedVecTy &&
2937 DL.getTypeSizeInBits(FixedVecTy->getElementType()) % 8 == 0 &&
2938 !FixedVecTy->getElementType()->isPointerTy();
2939 };
2940
2941 for (Slice &S : P) {
2942 auto *User = cast<Instruction>(S.getUse()->getUser());
2943 // A "full-width" slice spans the entire alloca; it's either the single
2944 // init store (Pattern 2) or the single final load (both patterns).
2945 bool IsFullWidth = (S.beginOffset() == NewAllocaBeginOffset &&
2946 S.endOffset() == NewAllocaEndOffset);
2947 if (auto *LI = dyn_cast<LoadInst>(User)) {
2948 // Only handle simple (non-volatile, non-atomic) loads.
2949 if (!LI->isSimple() ||
2950 !IsTypeValidForTreeStructuredMerge(LI->getType()))
2951 return std::nullopt;
2952 if (IsFullWidth) {
2953 // We accept at most one full-width load (the "final" load, after
2954 // all the partial stores).
2955 if (FullLoad)
2956 return std::nullopt;
2957 FullLoad = LI;
2958 } else {
2959 // Partial load (RMW pattern only).
2960 LoadInfos.push_back({LI, S.beginOffset(), S.endOffset()});
2961 }
2962 } else if (auto *SI = dyn_cast<StoreInst>(User)) {
2963 // Do not handle the case if
2964 // 1. The store does not meet the conditions in the helper function
2965 // 2. The store is not simple — we drop stores as part of the
2966 // rewrite, so volatile stores (which must be kept) and atomic
2967 // stores (which carry memory-ordering semantics) are unsound
2968 // to replace with SSA bookkeeping.
2969 // 3. The total store size is not a multiple of the allocated
2970 // element type size (required so the tree merge can produce a
2971 // vector whose element type matches the alloca).
2972 if (!SI->isSimple() || !IsTypeValidForTreeStructuredMerge(
2973 SI->getValueOperand()->getType()))
2974 return std::nullopt;
2975 auto *StVecTy = cast<FixedVectorType>(SI->getValueOperand()->getType());
2976 unsigned NumElts = StVecTy->getNumElements();
2977 unsigned EltSize = DL.getTypeSizeInBits(StVecTy->getElementType());
2978 if (NumElts * EltSize % AllocatedEltTySize != 0)
2979 return std::nullopt;
2980 if (IsFullWidth) {
2981 // At most one full-width store is allowed — it's the init store
2982 // for the RMW pattern.
2983 if (InitStore)
2984 return std::nullopt;
2985 InitStore = SI;
2986 } else {
2987 StoreInfos.emplace_back(SI, S.beginOffset(), S.endOffset(),
2988 SI->getValueOperand());
2989 }
2990 } else {
2991 // If we have instructions other than load and store, we cannot do
2992 // the tree structured merge.
2993 return std::nullopt;
2994 }
2995 }
2996
2997 // Need at least two partial stores to benefit from tree-merging; a
2998 // single store is already optimal as-is. This applies to both patterns
2999 // below, so check it before classifying.
3000 if (StoreInfos.size() < 2)
3001 return std::nullopt;
3002
3003 // Classify the pattern by looking at what we collected:
3004 // Pattern 1 (stores-only): only partial stores + exactly one full load.
3005 // Pattern 2 (RMW): one full init store + partial loads + partial stores
3006 // (+ optional full final load). RMW also needs VecTy to be set
3007 // because we use getIndex() to convert byte offsets to element
3008 // indices, which requires a promoted vector alloca.
3009 bool IsRMWPattern = InitStore && VecTy && !LoadInfos.empty();
3010 bool IsStoresOnlyPattern = !InitStore && FullLoad && LoadInfos.empty();
3011 if (!IsRMWPattern && !IsStoresOnlyPattern)
3012 return std::nullopt;
3013
3014 // All partial stores must live in the same basic block — the tree merge
3015 // is built in a single BB using block-order ordering (comesBefore).
3016 BasicBlock *StoreBB = StoreInfos[0].Store->getParent();
3017 for (auto &Info : StoreInfos)
3018 if (Info.Store->getParent() != StoreBB)
3019 return std::nullopt;
3020
3021 SmallVector<Value *, 4> DeletedValues;
3022
3023 // Helper: pairwise tree-merge a list of vectors into a single vector.
3024 // At each iteration we merge each adjacent pair via mergeTwoVectors,
3025 // collect the merged values into Next, and (if Vals had odd length)
3026 // carry the trailing element through unchanged. Loop until one value
3027 // remains — the fully-merged vector.
3028 auto TreeMerge = [&](SmallVectorImpl<Value *> &Vals,
3029 IRBuilder<> &B) -> Value * {
3030 LLVM_DEBUG(dbgs() << " Rewrite stores into shufflevectors:\n");
3031 while (Vals.size() > 1) {
3032 SmallVector<Value *, 8> Next;
3033 for (unsigned I = 0, E = Vals.size(); I + 1 < E; I += 2) {
3034 Value *M =
3035 mergeTwoVectors(Vals[I], Vals[I + 1], DL, AllocatedEltTy, B);
3036 LLVM_DEBUG(dbgs() << " shufflevector: " << *M << "\n");
3037 Next.push_back(M);
3038 }
3039 if (Vals.size() % 2 == 1)
3040 Next.push_back(Vals.back());
3041 Vals = std::move(Next);
3042 }
3043 return Vals[0];
3044 };
3045
3046 // Replace a full-width load with a load of the freshly-merged alloca.
3047 // The merge stored a value of type Merged->getType() into NewAI; we load
3048 // that same type back so every access to NewAI stays consistently typed
3049 // (otherwise the alloca is no longer promotable).
3050 auto ReplaceFullLoad = [&](LoadInst *LoadToReplace, Value *Merged) {
3051 IRBuilder<> LoadBuilder(LoadToReplace);
3052 Value *NewLoad = LoadBuilder.CreateAlignedLoad(
3053 Merged->getType(), &NewAI, getSliceAlign(),
3054 LoadToReplace->isVolatile(),
3055 LoadToReplace->getName() + ".sroa.new.load");
3056 if (NewLoad->getType() != LoadToReplace->getType())
3057 NewLoad = LoadBuilder.CreateBitCast(NewLoad, LoadToReplace->getType());
3058 LoadToReplace->replaceAllUsesWith(NewLoad);
3059 DeletedValues.push_back(LoadToReplace);
3060 };
3061
3062 if (IsStoresOnlyPattern) {
3063 // Stores should not overlap and should cover the whole alloca.
3064 // Sort by begin offset to verify this with a single linear scan.
3065 llvm::sort(StoreInfos, [](const StoreInfo &A, const StoreInfo &B) {
3066 return A.BeginOffset < B.BeginOffset;
3067 });
3068 // Check for gap or overlap: each begin offset must equal the previous
3069 // end offset, i.e. the store ranges must tile [NewAllocaBeginOffset,
3070 // NewAllocaEndOffset) exactly.
3071 uint64_t Expected = NewAllocaBeginOffset;
3072 for (auto &Info : StoreInfos) {
3073 if (Info.BeginOffset != Expected)
3074 return std::nullopt;
3075 Expected = Info.EndOffset;
3076 }
3077 // Stores cover the entire alloca (no trailing gap either).
3078 if (Expected != NewAllocaEndOffset)
3079 return std::nullopt;
3080
3081 // The load should not be in the middle of the stores.
3082 // Note:
3083 // If the load is in a different basic block from the stores, we can
3084 // still do the tree-structured merge. We don't have store->load
3085 // forwarding here — the merged vector is stored back to NewAI and
3086 // the new load loads from NewAI. The forwarding will be handled
3087 // later when NewAI is promoted.
3088 BasicBlock *LoadBB = FullLoad->getParent();
3089 if (LoadBB == StoreBB) {
3090 for (auto &Info : StoreInfos)
3091 if (!Info.Store->comesBefore(FullLoad))
3092 return std::nullopt;
3093 }
3094
3095 LLVM_DEBUG({
3096 dbgs() << "Tree structured merge rewrite (stores-only):\n";
3097 dbgs() << " Load: " << *FullLoad << "\n Ordered stores:\n";
3098 for (auto [I, Info] : enumerate(StoreInfos)) {
3099 dbgs() << " [" << I << "] Range[" << Info.BeginOffset << ", "
3100 << Info.EndOffset << ") \tStore: " << *Info.Store
3101 << "\tValue: " << *Info.StoredValue << "\n";
3102 }
3103 });
3104
3105 // StoreInfos is sorted by offset, not by block order. Anchoring to
3106 // StoreInfos.back().Store (last by offset) can place shuffles before
3107 // operands that appear later in the block (invalid SSA). Insert before
3108 // FullLoad when it shares the store block (after all stores, before
3109 // any later IR in that block). Otherwise insert before the store
3110 // block's terminator so the merge runs after every store and any
3111 // trailing instructions in that block.
3112 IRBuilder<> Builder(LoadBB == StoreBB ? cast<Instruction>(FullLoad)
3113 : StoreBB->getTerminator());
3114 SmallVector<Value *, 8> Vals;
3115 for (const auto &Info : StoreInfos) {
3116 DeletedValues.push_back(Info.Store);
3117 Vals.push_back(Info.StoredValue);
3118 }
3119 // Merge all stored values and store the merged value into the alloca.
3120 Value *Merged = TreeMerge(Vals, Builder);
3121 Builder.CreateAlignedStore(Merged, &NewAI, getSliceAlign());
3122
3123 // Replace the original load with a load of the newly-merged alloca.
3124 ReplaceFullLoad(FullLoad, Merged);
3125 return DeletedValues;
3126 }
3127
3128 // RMW pattern handling starts from here.
3129 // Like StoreBB above: keep the init store, all partial loads and all
3130 // partial stores in one basic block so we can reason about ordering
3131 // with comesBefore and build SSA without PHIs.
3132 if (InitStore->getParent() != StoreBB)
3133 return std::nullopt;
3134 if (any_of(LoadInfos, [&](const LoadInfo &I) {
3135 return I.Load->getParent() != StoreBB;
3136 }))
3137 return std::nullopt;
3138 // FullLoad (if any) is allowed to live in a different basic block. See
3139 // the note on the stores-only path: we don't do store->load forwarding
3140 // directly — the merged vector is stored to NewAI and the new load
3141 // loads from NewAI, so cross-BB ordering is resolved later when NewAI
3142 // is promoted.
3143
3144 // Collect the combined partial-load/partial-store accesses sorted
3145 // by block order. Used both for ordering checks and for the rewrite
3146 // walk below.
3147 struct Access {
3148 Instruction *Inst;
3149 uint64_t BeginOffset, EndOffset;
3150 bool IsStore;
3151 };
3153 Accesses.reserve(LoadInfos.size() + StoreInfos.size());
3154 for (const auto &L : LoadInfos)
3155 Accesses.push_back({L.Load, L.BeginOffset, L.EndOffset, false});
3156 for (const auto &S : StoreInfos)
3157 Accesses.push_back({S.Store, S.BeginOffset, S.EndOffset, true});
3158 llvm::sort(Accesses, [](const Access &A, const Access &B) {
3159 return A.Inst->comesBefore(B.Inst);
3160 });
3161
3162 // Ordering constraint 1: InitStore must come before every partial
3163 // access — they read/write the RMW state initialised by InitStore.
3164 // Accesses is sorted by block order, so the first element is the
3165 // earliest; checking it is enough.
3166 if (!InitStore->comesBefore(Accesses.front().Inst))
3167 return std::nullopt;
3168 // Ordering constraint 2: when FullLoad shares the block with the
3169 // partial accesses, it must come after every one of them — otherwise
3170 // it could read a stale value. Accesses is sorted, so the last
3171 // element is the latest; checking it is enough. If FullLoad is in
3172 // another block, mem2reg forwards the merged store to it.
3173 if (FullLoad && FullLoad->getParent() == StoreBB &&
3174 !Accesses.back().Inst->comesBefore(FullLoad))
3175 return std::nullopt;
3176
3177 // Coverage check: the distinct [begin, end) ranges touched by the
3178 // partial loads and stores must tile the alloca disjointly. That is
3179 // the only precondition the per-range SliceValues tracking below
3180 // needs — a disjoint tile guarantees the entries don't alias each
3181 // other. We don't check per-range load/store counts: a range with
3182 // only loads ends with SliceValues[r] = the init extract
3183 // (contributed to the final tree-merge), and a range with only
3184 // stores ends with SliceValues[r] = its last stored value. Both are
3185 // correct.
3186 using SliceRange = std::pair<uint64_t, uint64_t>;
3187 SmallVector<SliceRange, 8> SortedRanges;
3188 SortedRanges.reserve(Accesses.size());
3189 for (auto &Acc : Accesses)
3190 SortedRanges.emplace_back(Acc.BeginOffset, Acc.EndOffset);
3191 llvm::sort(SortedRanges);
3192 SortedRanges.erase(llvm::unique(SortedRanges), SortedRanges.end());
3193 // Disjoint + contiguous tile of the whole alloca.
3194 uint64_t Expected = NewAllocaBeginOffset;
3195 for (auto &Range : SortedRanges) {
3196 if (Range.first != Expected)
3197 return std::nullopt;
3198 Expected = Range.second;
3199 }
3200 if (Expected != NewAllocaEndOffset)
3201 return std::nullopt;
3202
3203 LLVM_DEBUG({
3204 dbgs() << "Tree structured merge rewrite (RMW):\n";
3205 dbgs() << " Init store: " << *InitStore << "\n";
3206 if (FullLoad)
3207 dbgs() << " Final load: " << *FullLoad << "\n";
3208 dbgs() << " Slice ranges (" << SortedRanges.size() << "):\n";
3209 for (auto &Range : SortedRanges)
3210 dbgs() << " [" << Range.first << ", " << Range.second << ")\n";
3211 });
3212
3213 // Initialize SliceValues: one SSA value per slice range, tracking
3214 // the value the alloca currently holds at that range. Each entry
3215 // starts at the corresponding piece of the init store, obtained by
3216 // bitcasting the init value to the alloca's vector type (if needed)
3217 // and extracting the slice's sub-range.
3218 IRB.SetInsertPoint(InitStore->getNextNode());
3219 Value *InitVec = InitStore->getValueOperand();
3220 if (InitVec->getType() != NewAllocaTy)
3221 InitVec = IRB.CreateBitCast(InitVec, NewAllocaTy, "init.cast");
3222 DenseMap<SliceRange, Value *> SliceValues;
3223 for (auto &Range : SortedRanges) {
3224 unsigned BeginIdx = getIndex(Range.first);
3225 unsigned EndIdx = getIndex(Range.second);
3226 SliceValues[Range] = IRB.CreateShuffleVector(
3227 InitVec, createSequentialMask(BeginIdx, EndIdx - BeginIdx, 0),
3228 "init.extract");
3229 }
3230 // The init store itself becomes dead — its value is consumed via the
3231 // extracts above.
3232 DeletedValues.push_back(InitStore);
3233
3234 // Walk accesses in block order:
3235 // - partial load at range r: replace with SliceValues[r] (bitcast
3236 // if the load's type differs from the current tracked value's
3237 // type, e.g. because a previous store wrote a vector with a
3238 // different element type);
3239 // - partial store at range r: update SliceValues[r] to the stored
3240 // value and drop the store.
3241 for (auto &Acc : Accesses) {
3242 SliceRange Range{Acc.BeginOffset, Acc.EndOffset};
3243 if (!Acc.IsStore) {
3244 Value *V = SliceValues[Range];
3245 if (V->getType() != Acc.Inst->getType()) {
3246 IRB.SetInsertPoint(cast<LoadInst>(Acc.Inst));
3247 V = IRB.CreateBitCast(V, Acc.Inst->getType());
3248 }
3249 Acc.Inst->replaceAllUsesWith(V);
3250 } else {
3251 SliceValues[Range] = cast<StoreInst>(Acc.Inst)->getValueOperand();
3252 }
3253 DeletedValues.push_back(Acc.Inst);
3254 }
3255
3256 // Tree-merge the final per-range values (in range order) into the
3257 // alloca's final vector value. Anchor the IRBuilder to FullLoad (when it
3258 // shares the partial-access block) or otherwise to the block's
3259 // terminator — never to a partial access, since those are queued for
3260 // deletion. Both anchors are guaranteed to dominate every SliceValues
3261 // entry: each one is either an init extract (before any access) or a
3262 // stored value defined before its (now-deleted) store.
3263 IRBuilder<> Builder(FullLoad && FullLoad->getParent() == StoreBB
3264 ? cast<Instruction>(FullLoad)
3265 : StoreBB->getTerminator());
3266 SmallVector<Value *, 8> Vals;
3267 for (auto &Range : SortedRanges)
3268 Vals.push_back(SliceValues[Range]);
3269 Value *Merged = TreeMerge(Vals, Builder);
3270 Builder.CreateAlignedStore(Merged, &NewAI, getSliceAlign());
3271
3272 // Replace the optional final full-width load with a load of the newly
3273 // merged alloca. Later promotion will forward the store above to it.
3274 if (FullLoad)
3275 ReplaceFullLoad(FullLoad, Merged);
3276
3277 return DeletedValues;
3278 }
3279
3280private:
3281 // Make sure the other visit overloads are visible.
3282 using Base::visit;
3283
3284 // Every instruction which can end up as a user must have a rewrite rule.
3285 bool visitInstruction(Instruction &I) {
3286 LLVM_DEBUG(dbgs() << " !!!! Cannot rewrite: " << I << "\n");
3287 llvm_unreachable("No rewrite rule for this instruction!");
3288 }
3289
3290 Value *getNewAllocaSlicePtr(IRBuilderTy &IRB, Type *PointerTy) {
3291 // Note that the offset computation can use BeginOffset or NewBeginOffset
3292 // interchangeably for unsplit slices.
3293 assert(IsSplit || BeginOffset == NewBeginOffset);
3294 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3295
3296 StringRef OldName = OldPtr->getName();
3297 // Skip through the last '.sroa.' component of the name.
3298 size_t LastSROAPrefix = OldName.rfind(".sroa.");
3299 if (LastSROAPrefix != StringRef::npos) {
3300 OldName = OldName.substr(LastSROAPrefix + strlen(".sroa."));
3301 // Look for an SROA slice index.
3302 size_t IndexEnd = OldName.find_first_not_of("0123456789");
3303 if (IndexEnd != StringRef::npos && OldName[IndexEnd] == '.') {
3304 // Strip the index and look for the offset.
3305 OldName = OldName.substr(IndexEnd + 1);
3306 size_t OffsetEnd = OldName.find_first_not_of("0123456789");
3307 if (OffsetEnd != StringRef::npos && OldName[OffsetEnd] == '.')
3308 // Strip the offset.
3309 OldName = OldName.substr(OffsetEnd + 1);
3310 }
3311 }
3312 // Strip any SROA suffixes as well.
3313 OldName = OldName.substr(0, OldName.find(".sroa_"));
3314
3315 return getAdjustedPtr(IRB, DL, &NewAI,
3316 APInt(DL.getIndexTypeSizeInBits(PointerTy), Offset),
3317 PointerTy, Twine(OldName) + ".");
3318 }
3319
3320 /// Compute suitable alignment to access this slice of the *new*
3321 /// alloca.
3322 ///
3323 /// You can optionally pass a type to this routine and if that type's ABI
3324 /// alignment is itself suitable, this will return zero.
3325 Align getSliceAlign() {
3326 return commonAlignment(NewAI.getAlign(),
3327 NewBeginOffset - NewAllocaBeginOffset);
3328 }
3329
3330 unsigned getIndex(uint64_t Offset) {
3331 assert(VecTy && "Can only call getIndex when rewriting a vector");
3332 uint64_t RelOffset = Offset - NewAllocaBeginOffset;
3333 assert(RelOffset / ElementSize < UINT32_MAX && "Index out of bounds");
3334 uint32_t Index = RelOffset / ElementSize;
3335 assert(Index * ElementSize == RelOffset);
3336 return Index;
3337 }
3338
3339 void deleteIfTriviallyDead(Value *V) {
3342 Pass.DeadInsts.push_back(I);
3343 }
3344
3345 Value *rewriteVectorizedLoadInst(LoadInst &LI) {
3346 unsigned BeginIndex = getIndex(NewBeginOffset);
3347 unsigned EndIndex = getIndex(NewEndOffset);
3348 assert(EndIndex > BeginIndex && "Empty vector!");
3349
3350 LoadInst *Load =
3351 IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(), "load");
3352
3353 Load->copyMetadata(LI, {LLVMContext::MD_mem_parallel_loop_access,
3354 LLVMContext::MD_access_group});
3355 return extractVector(IRB, Load, BeginIndex, EndIndex, "vec");
3356 }
3357
3358 Value *rewriteIntegerLoad(LoadInst &LI) {
3359 assert(IntTy && "We cannot insert an integer to the alloca");
3360 assert(!LI.isVolatile());
3361 Value *V =
3362 IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(), "load");
3363 V = IRB.CreateBitPreservingCastChain(DL, V, IntTy);
3364 assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset");
3365 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3366 if (Offset > 0 || NewEndOffset < NewAllocaEndOffset) {
3367 IntegerType *ExtractTy = Type::getIntNTy(LI.getContext(), SliceSize * 8);
3368 V = extractInteger(DL, IRB, V, ExtractTy, Offset, "extract");
3369 }
3370 // It is possible that the extracted type is not the load type. This
3371 // happens if there is a load past the end of the alloca, and as
3372 // a consequence the slice is narrower but still a candidate for integer
3373 // lowering. To handle this case, we just zero extend the extracted
3374 // integer.
3375 assert(cast<IntegerType>(LI.getType())->getBitWidth() >= SliceSize * 8 &&
3376 "Can only handle an extract for an overly wide load");
3377 if (cast<IntegerType>(LI.getType())->getBitWidth() > SliceSize * 8)
3378 V = IRB.CreateZExt(V, LI.getType());
3379 return V;
3380 }
3381
3382 bool visitLoadInst(LoadInst &LI) {
3383 LLVM_DEBUG(dbgs() << " original: " << LI << "\n");
3384 Value *OldOp = LI.getOperand(0);
3385 assert(OldOp == OldPtr);
3386
3387 AAMDNodes AATags = LI.getAAMetadata();
3388
3389 unsigned AS = LI.getPointerAddressSpace();
3390
3391 Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), SliceSize * 8)
3392 : LI.getType();
3393 bool IsPtrAdjusted = false;
3394 Value *V;
3395 if (VecTy) {
3396 V = rewriteVectorizedLoadInst(LI);
3397 } else if (IntTy && LI.getType()->isIntegerTy()) {
3398 V = rewriteIntegerLoad(LI);
3399 } else if (NewBeginOffset == NewAllocaBeginOffset &&
3400 NewEndOffset == NewAllocaEndOffset &&
3401 (canConvertValue(DL, NewAllocaTy, TargetTy) ||
3402 (NewAllocaTy->isIntegerTy() && TargetTy->isIntegerTy() &&
3403 DL.getTypeStoreSize(TargetTy).getFixedValue() > SliceSize &&
3404 !LI.isVolatile()))) {
3405 Value *NewPtr =
3406 getPtrToNewAI(LI.getPointerAddressSpace(), LI.isVolatile());
3407 LoadInst *NewLI = IRB.CreateAlignedLoad(
3408 NewAllocaTy, NewPtr, NewAI.getAlign(), LI.isVolatile(), LI.getName());
3409 if (LI.isVolatile())
3410 NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
3411 if (NewLI->isAtomic())
3412 NewLI->setAlignment(LI.getAlign());
3413
3414 // Copy any metadata that is valid for the new load. This may require
3415 // conversion to a different kind of metadata, e.g. !nonnull might change
3416 // to !range or vice versa.
3417 copyMetadataForLoad(*NewLI, LI);
3418
3419 // Do this after copyMetadataForLoad() to preserve the TBAA shift.
3420 if (AATags)
3421 NewLI->setAAMetadata(AATags.adjustForAccess(
3422 NewBeginOffset - BeginOffset, NewLI->getType(), DL));
3423
3424 // Try to preserve nonnull metadata
3425 V = NewLI;
3426
3427 // If this is an integer load past the end of the slice (which means the
3428 // bytes outside the slice are undef or this load is dead) just forcibly
3429 // fix the integer size with correct handling of endianness.
3430 if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy))
3431 if (auto *TITy = dyn_cast<IntegerType>(TargetTy))
3432 if (AITy->getBitWidth() < TITy->getBitWidth()) {
3433 V = IRB.CreateZExt(V, TITy, "load.ext");
3434 if (DL.isBigEndian())
3435 V = IRB.CreateShl(V, TITy->getBitWidth() - AITy->getBitWidth(),
3436 "endian_shift");
3437 }
3438 } else {
3439 Type *LTy = IRB.getPtrTy(AS);
3440 LoadInst *NewLI =
3441 IRB.CreateAlignedLoad(TargetTy, getNewAllocaSlicePtr(IRB, LTy),
3442 getSliceAlign(), LI.isVolatile(), LI.getName());
3443
3444 if (AATags)
3445 NewLI->setAAMetadata(AATags.adjustForAccess(
3446 NewBeginOffset - BeginOffset, NewLI->getType(), DL));
3447
3448 if (LI.isVolatile())
3449 NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID());
3450 NewLI->copyMetadata(LI, {LLVMContext::MD_mem_parallel_loop_access,
3451 LLVMContext::MD_access_group});
3452
3453 V = NewLI;
3454 IsPtrAdjusted = true;
3455 }
3456 V = IRB.CreateBitPreservingCastChain(DL, V, TargetTy);
3457
3458 if (IsSplit) {
3459 assert(!LI.isVolatile());
3460 assert(LI.getType()->isIntegerTy() &&
3461 "Only integer type loads and stores are split");
3462 assert(SliceSize < DL.getTypeStoreSize(LI.getType()).getFixedValue() &&
3463 "Split load isn't smaller than original load");
3464 assert(DL.typeSizeEqualsStoreSize(LI.getType()) &&
3465 "Non-byte-multiple bit width");
3466 // Move the insertion point just past the load so that we can refer to it.
3467 BasicBlock::iterator LIIt = std::next(LI.getIterator());
3468 // Ensure the insertion point comes before any debug-info immediately
3469 // after the load, so that variable values referring to the load are
3470 // dominated by it.
3471 LIIt.setHeadBit(true);
3472 IRB.SetInsertPoint(LI.getParent(), LIIt);
3473 // Create a placeholder value with the same type as LI to use as the
3474 // basis for the new value. This allows us to replace the uses of LI with
3475 // the computed value, and then replace the placeholder with LI, leaving
3476 // LI only used for this computation.
3477 Value *Placeholder =
3478 new LoadInst(LI.getType(), PoisonValue::get(IRB.getPtrTy(AS)), "",
3479 false, Align(1));
3480 V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset - BeginOffset,
3481 "insert");
3482 LI.replaceAllUsesWith(V);
3483 Placeholder->replaceAllUsesWith(&LI);
3484 Placeholder->deleteValue();
3485 } else {
3486 LI.replaceAllUsesWith(V);
3487 }
3488
3489 Pass.DeadInsts.push_back(&LI);
3490 deleteIfTriviallyDead(OldOp);
3491 LLVM_DEBUG(dbgs() << " to: " << *V << "\n");
3492 return !LI.isVolatile() && !IsPtrAdjusted;
3493 }
3494
3495 bool rewriteVectorizedStoreInst(Value *V, StoreInst &SI, Value *OldOp,
3496 AAMDNodes AATags) {
3497 // Capture V for the purpose of debug-info accounting once it's converted
3498 // to a vector store.
3499 Value *OrigV = V;
3500 if (V->getType() != VecTy) {
3501 unsigned BeginIndex = getIndex(NewBeginOffset);
3502 unsigned EndIndex = getIndex(NewEndOffset);
3503 assert(EndIndex > BeginIndex && "Empty vector!");
3504 unsigned NumElements = EndIndex - BeginIndex;
3505 assert(NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() &&
3506 "Too many elements!");
3507 Type *SliceTy = (NumElements == 1)
3508 ? ElementTy
3509 : FixedVectorType::get(ElementTy, NumElements);
3510 if (V->getType() != SliceTy)
3511 V = IRB.CreateBitPreservingCastChain(DL, V, SliceTy);
3512
3513 // Mix in the existing elements.
3514 Value *Old =
3515 IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(), "load");
3516 V = insertVector(IRB, Old, V, BeginIndex, "vec");
3517 }
3518 StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlign());
3519 Store->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access,
3520 LLVMContext::MD_access_group});
3521 if (AATags)
3522 Store->setAAMetadata(AATags.adjustForAccess(NewBeginOffset - BeginOffset,
3523 V->getType(), DL));
3524 Pass.DeadInsts.push_back(&SI);
3525
3526 // NOTE: Careful to use OrigV rather than V.
3527 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8, &SI,
3528 Store, Store->getPointerOperand(), OrigV, DL);
3529 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n");
3530 return true;
3531 }
3532
3533 bool rewriteIntegerStore(Value *V, StoreInst &SI, AAMDNodes AATags) {
3534 assert(IntTy && "We cannot extract an integer from the alloca");
3535 assert(!SI.isVolatile());
3536 if (DL.getTypeSizeInBits(V->getType()).getFixedValue() !=
3537 IntTy->getBitWidth()) {
3538 Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(),
3539 "oldload");
3540 Old = IRB.CreateBitPreservingCastChain(DL, Old, IntTy);
3541 assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset");
3542 uint64_t Offset = BeginOffset - NewAllocaBeginOffset;
3543 V = insertInteger(DL, IRB, Old, SI.getValueOperand(), Offset, "insert");
3544 }
3545 V = IRB.CreateBitPreservingCastChain(DL, V, NewAllocaTy);
3546 StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlign());
3547 Store->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access,
3548 LLVMContext::MD_access_group});
3549 if (AATags)
3550 Store->setAAMetadata(AATags.adjustForAccess(NewBeginOffset - BeginOffset,
3551 V->getType(), DL));
3552
3553 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8, &SI,
3554 Store, Store->getPointerOperand(),
3555 Store->getValueOperand(), DL);
3556
3557 Pass.DeadInsts.push_back(&SI);
3558 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n");
3559 return true;
3560 }
3561
3562 bool visitStoreInst(StoreInst &SI) {
3563 LLVM_DEBUG(dbgs() << " original: " << SI << "\n");
3564 Value *OldOp = SI.getOperand(1);
3565 assert(OldOp == OldPtr);
3566
3567 AAMDNodes AATags = SI.getAAMetadata();
3568 Value *V = SI.getValueOperand();
3569
3570 // Strip all inbounds GEPs and pointer casts to try to dig out any root
3571 // alloca that should be re-examined after promoting this alloca.
3572 if (V->getType()->isPointerTy())
3573 if (AllocaInst *AI = dyn_cast<AllocaInst>(V->stripInBoundsOffsets()))
3574 Pass.PostPromotionWorklist.insert(AI);
3575
3576 TypeSize StoreSize = DL.getTypeStoreSize(V->getType());
3577 if (StoreSize.isFixed() && SliceSize < StoreSize.getFixedValue()) {
3578 assert(!SI.isVolatile());
3579 assert(V->getType()->isIntegerTy() &&
3580 "Only integer type loads and stores are split");
3581 assert(DL.typeSizeEqualsStoreSize(V->getType()) &&
3582 "Non-byte-multiple bit width");
3583 IntegerType *NarrowTy = Type::getIntNTy(SI.getContext(), SliceSize * 8);
3584 V = extractInteger(DL, IRB, V, NarrowTy, NewBeginOffset - BeginOffset,
3585 "extract");
3586 }
3587
3588 if (VecTy)
3589 return rewriteVectorizedStoreInst(V, SI, OldOp, AATags);
3590 if (IntTy && V->getType()->isIntegerTy())
3591 return rewriteIntegerStore(V, SI, AATags);
3592
3593 StoreInst *NewSI;
3594 if (NewBeginOffset == NewAllocaBeginOffset &&
3595 NewEndOffset == NewAllocaEndOffset &&
3596 canConvertValue(DL, V->getType(), NewAllocaTy)) {
3597 V = IRB.CreateBitPreservingCastChain(DL, V, NewAllocaTy);
3598 Value *NewPtr =
3599 getPtrToNewAI(SI.getPointerAddressSpace(), SI.isVolatile());
3600
3601 NewSI =
3602 IRB.CreateAlignedStore(V, NewPtr, NewAI.getAlign(), SI.isVolatile());
3603 } else {
3604 unsigned AS = SI.getPointerAddressSpace();
3605 Value *NewPtr = getNewAllocaSlicePtr(IRB, IRB.getPtrTy(AS));
3606 NewSI =
3607 IRB.CreateAlignedStore(V, NewPtr, getSliceAlign(), SI.isVolatile());
3608 }
3609 NewSI->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access,
3610 LLVMContext::MD_access_group});
3611 if (AATags)
3612 NewSI->setAAMetadata(AATags.adjustForAccess(NewBeginOffset - BeginOffset,
3613 V->getType(), DL));
3614 if (SI.isVolatile())
3615 NewSI->setAtomic(SI.getOrdering(), SI.getSyncScopeID());
3616 if (NewSI->isAtomic())
3617 NewSI->setAlignment(SI.getAlign());
3618
3619 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8, &SI,
3620 NewSI, NewSI->getPointerOperand(),
3621 NewSI->getValueOperand(), DL);
3622
3623 Pass.DeadInsts.push_back(&SI);
3624 deleteIfTriviallyDead(OldOp);
3625
3626 LLVM_DEBUG(dbgs() << " to: " << *NewSI << "\n");
3627 return NewSI->getPointerOperand() == &NewAI &&
3628 NewSI->getValueOperand()->getType() == NewAllocaTy &&
3629 !SI.isVolatile();
3630 }
3631
3632 /// Compute an integer value from splatting an i8 across the given
3633 /// number of bytes.
3634 ///
3635 /// Note that this routine assumes an i8 is a byte. If that isn't true, don't
3636 /// call this routine.
3637 /// FIXME: Heed the advice above.
3638 ///
3639 /// \param V The i8 value to splat.
3640 /// \param Size The number of bytes in the output (assuming i8 is one byte)
3641 Value *getIntegerSplat(Value *V, unsigned Size) {
3642 assert(Size > 0 && "Expected a positive number of bytes.");
3643 IntegerType *VTy = cast<IntegerType>(V->getType());
3644 assert(VTy->getBitWidth() == 8 && "Expected an i8 value for the byte");
3645 if (Size == 1)
3646 return V;
3647
3648 Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8);
3649 V = IRB.CreateMul(
3650 IRB.CreateZExt(V, SplatIntTy, "zext"),
3651 IRB.CreateUDiv(Constant::getAllOnesValue(SplatIntTy),
3652 IRB.CreateZExt(Constant::getAllOnesValue(V->getType()),
3653 SplatIntTy)),
3654 "isplat");
3655 return V;
3656 }
3657
3658 /// Compute a vector splat for a given element value.
3659 Value *getVectorSplat(Value *V, unsigned NumElements) {
3660 V = IRB.CreateVectorSplat(NumElements, V, "vsplat");
3661 LLVM_DEBUG(dbgs() << " splat: " << *V << "\n");
3662 return V;
3663 }
3664
3665 bool visitMemSetInst(MemSetInst &II) {
3666 LLVM_DEBUG(dbgs() << " original: " << II << "\n");
3667 assert(II.getRawDest() == OldPtr);
3668
3669 AAMDNodes AATags = II.getAAMetadata();
3670
3671 // If the memset has a variable size, it cannot be split, just adjust the
3672 // pointer to the new alloca.
3673 if (!isa<ConstantInt>(II.getLength())) {
3674 assert(!IsSplit);
3675 assert(NewBeginOffset == BeginOffset);
3676 II.setDest(getNewAllocaSlicePtr(IRB, OldPtr->getType()));
3677 II.setDestAlignment(getSliceAlign());
3678 // In theory we should call migrateDebugInfo here. However, we do not
3679 // emit dbg.assign intrinsics for mem intrinsics storing through non-
3680 // constant geps, or storing a variable number of bytes.
3682 "AT: Unexpected link to non-const GEP");
3683 deleteIfTriviallyDead(OldPtr);
3684 return false;
3685 }
3686
3687 // Record this instruction for deletion.
3688 Pass.DeadInsts.push_back(&II);
3689
3690 Type *ScalarTy = NewAllocaTy->getScalarType();
3691
3692 const bool CanContinue = [&]() {
3693 if (VecTy || IntTy)
3694 return true;
3695 if (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset)
3696 return false;
3697 // Length must be in range for FixedVectorType.
3698 auto *C = cast<ConstantInt>(II.getLength());
3699 const uint64_t Len = C->getLimitedValue();
3700 if (Len > std::numeric_limits<unsigned>::max())
3701 return false;
3702 auto *Int8Ty = IntegerType::getInt8Ty(NewAI.getContext());
3703 auto *SrcTy = FixedVectorType::get(Int8Ty, Len);
3704 return canConvertValue(DL, SrcTy, NewAllocaTy) &&
3705 DL.isLegalInteger(DL.getTypeSizeInBits(ScalarTy).getFixedValue());
3706 }();
3707
3708 // If this doesn't map cleanly onto the alloca type, and that type isn't
3709 // a single value type, just emit a memset.
3710 if (!CanContinue) {
3711 Type *SizeTy = II.getLength()->getType();
3712 unsigned Sz = NewEndOffset - NewBeginOffset;
3713 Constant *Size = ConstantInt::get(SizeTy, Sz);
3714 MemIntrinsic *New = cast<MemIntrinsic>(IRB.CreateMemSet(
3715 getNewAllocaSlicePtr(IRB, OldPtr->getType()), II.getValue(), Size,
3716 MaybeAlign(getSliceAlign()), II.isVolatile()));
3717 if (AATags)
3718 New->setAAMetadata(
3719 AATags.adjustForAccess(NewBeginOffset - BeginOffset, Sz));
3720
3721 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8, &II,
3722 New, New->getRawDest(), nullptr, DL);
3723
3724 LLVM_DEBUG(dbgs() << " to: " << *New << "\n");
3725 return false;
3726 }
3727
3728 // If we can represent this as a simple value, we have to build the actual
3729 // value to store, which requires expanding the byte present in memset to
3730 // a sensible representation for the alloca type. This is essentially
3731 // splatting the byte to a sufficiently wide integer, splatting it across
3732 // any desired vector width, and bitcasting to the final type.
3733 Value *V;
3734
3735 if (VecTy) {
3736 // If this is a memset of a vectorized alloca, insert it.
3737 assert(ElementTy == ScalarTy);
3738
3739 unsigned BeginIndex = getIndex(NewBeginOffset);
3740 unsigned EndIndex = getIndex(NewEndOffset);
3741 assert(EndIndex > BeginIndex && "Empty vector!");
3742 unsigned NumElements = EndIndex - BeginIndex;
3743 assert(NumElements <= cast<FixedVectorType>(VecTy)->getNumElements() &&
3744 "Too many elements!");
3745
3746 Value *Splat = getIntegerSplat(
3747 II.getValue(), DL.getTypeSizeInBits(ElementTy).getFixedValue() / 8);
3748 Splat = IRB.CreateBitPreservingCastChain(DL, Splat, ElementTy);
3749 if (NumElements > 1)
3750 Splat = getVectorSplat(Splat, NumElements);
3751
3752 Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(),
3753 "oldload");
3754 V = insertVector(IRB, Old, Splat, BeginIndex, "vec");
3755 } else if (IntTy) {
3756 // If this is a memset on an alloca where we can widen stores, insert the
3757 // set integer.
3758 assert(!II.isVolatile());
3759
3760 uint64_t Size = NewEndOffset - NewBeginOffset;
3761 V = getIntegerSplat(II.getValue(), Size);
3762
3763 if (IntTy && (NewBeginOffset != NewAllocaBeginOffset ||
3764 NewEndOffset != NewAllocaEndOffset)) {
3765 Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI,
3766 NewAI.getAlign(), "oldload");
3767 Old = IRB.CreateBitPreservingCastChain(DL, Old, IntTy);
3768 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3769 V = insertInteger(DL, IRB, Old, V, Offset, "insert");
3770 } else {
3771 assert(V->getType() == IntTy &&
3772 "Wrong type for an alloca wide integer!");
3773 }
3774 V = IRB.CreateBitPreservingCastChain(DL, V, NewAllocaTy);
3775 } else {
3776 // Established these invariants above.
3777 assert(NewBeginOffset == NewAllocaBeginOffset);
3778 assert(NewEndOffset == NewAllocaEndOffset);
3779
3780 V = getIntegerSplat(II.getValue(),
3781 DL.getTypeSizeInBits(ScalarTy).getFixedValue() / 8);
3782 if (VectorType *AllocaVecTy = dyn_cast<VectorType>(NewAllocaTy))
3783 V = getVectorSplat(
3784 V, cast<FixedVectorType>(AllocaVecTy)->getNumElements());
3785
3786 V = IRB.CreateBitPreservingCastChain(DL, V, NewAllocaTy);
3787 }
3788
3789 Value *NewPtr = getPtrToNewAI(II.getDestAddressSpace(), II.isVolatile());
3790 StoreInst *New =
3791 IRB.CreateAlignedStore(V, NewPtr, NewAI.getAlign(), II.isVolatile());
3792 New->copyMetadata(II, {LLVMContext::MD_mem_parallel_loop_access,
3793 LLVMContext::MD_access_group});
3794 if (AATags)
3795 New->setAAMetadata(AATags.adjustForAccess(NewBeginOffset - BeginOffset,
3796 V->getType(), DL));
3797
3798 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8, &II,
3799 New, New->getPointerOperand(), V, DL);
3800
3801 LLVM_DEBUG(dbgs() << " to: " << *New << "\n");
3802 return !II.isVolatile();
3803 }
3804
3805 bool visitMemTransferInst(MemTransferInst &II) {
3806 // Rewriting of memory transfer instructions can be a bit tricky. We break
3807 // them into two categories: split intrinsics and unsplit intrinsics.
3808
3809 LLVM_DEBUG(dbgs() << " original: " << II << "\n");
3810
3811 AAMDNodes AATags = II.getAAMetadata();
3812
3813 bool IsDest = &II.getRawDestUse() == OldUse;
3814 assert((IsDest && II.getRawDest() == OldPtr) ||
3815 (!IsDest && II.getRawSource() == OldPtr));
3816
3817 Align SliceAlign = getSliceAlign();
3818 // For unsplit intrinsics, we simply modify the source and destination
3819 // pointers in place. This isn't just an optimization, it is a matter of
3820 // correctness. With unsplit intrinsics we may be dealing with transfers
3821 // within a single alloca before SROA ran, or with transfers that have
3822 // a variable length. We may also be dealing with memmove instead of
3823 // memcpy, and so simply updating the pointers is the necessary for us to
3824 // update both source and dest of a single call.
3825 if (!IsSplittable) {
3826 Value *AdjustedPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
3827 if (IsDest) {
3828 // Update the address component of linked dbg.assigns.
3829 for (DbgVariableRecord *DbgAssign : at::getDVRAssignmentMarkers(&II)) {
3830 if (llvm::is_contained(DbgAssign->location_ops(), II.getDest()) ||
3831 DbgAssign->getAddress() == II.getDest())
3832 DbgAssign->replaceVariableLocationOp(II.getDest(), AdjustedPtr);
3833 }
3834 II.setDest(AdjustedPtr);
3835 II.setDestAlignment(SliceAlign);
3836 } else {
3837 II.setSource(AdjustedPtr);
3838 II.setSourceAlignment(SliceAlign);
3839 }
3840
3841 LLVM_DEBUG(dbgs() << " to: " << II << "\n");
3842 deleteIfTriviallyDead(OldPtr);
3843 return false;
3844 }
3845 // For split transfer intrinsics we have an incredibly useful assurance:
3846 // the source and destination do not reside within the same alloca, and at
3847 // least one of them does not escape. This means that we can replace
3848 // memmove with memcpy, and we don't need to worry about all manner of
3849 // downsides to splitting and transforming the operations.
3850
3851 // If this doesn't map cleanly onto the alloca type, and that type isn't
3852 // a single value type, just emit a memcpy.
3853 bool EmitMemCpy =
3854 !VecTy && !IntTy &&
3855 (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset ||
3856 SliceSize != DL.getTypeStoreSize(NewAllocaTy).getFixedValue() ||
3857 !DL.typeSizeEqualsStoreSize(NewAllocaTy) ||
3858 !NewAllocaTy->isSingleValueType());
3859
3860 // If we're just going to emit a memcpy, the alloca hasn't changed, and the
3861 // size hasn't been shrunk based on analysis of the viable range, this is
3862 // a no-op.
3863 if (EmitMemCpy && &OldAI == &NewAI) {
3864 // Ensure the start lines up.
3865 assert(NewBeginOffset == BeginOffset);
3866
3867 // Rewrite the size as needed.
3868 if (NewEndOffset != EndOffset)
3869 II.setLength(NewEndOffset - NewBeginOffset);
3870 return false;
3871 }
3872 // Record this instruction for deletion.
3873 Pass.DeadInsts.push_back(&II);
3874
3875 // Strip all inbounds GEPs and pointer casts to try to dig out any root
3876 // alloca that should be re-examined after rewriting this instruction.
3877 Value *OtherPtr = IsDest ? II.getRawSource() : II.getRawDest();
3878 if (AllocaInst *AI =
3880 assert(AI != &OldAI && AI != &NewAI &&
3881 "Splittable transfers cannot reach the same alloca on both ends.");
3882 Pass.Worklist.insert(AI);
3883 }
3884
3885 Type *OtherPtrTy = OtherPtr->getType();
3886 unsigned OtherAS = OtherPtrTy->getPointerAddressSpace();
3887
3888 // Compute the relative offset for the other pointer within the transfer.
3889 unsigned OffsetWidth = DL.getIndexSizeInBits(OtherAS);
3890 APInt OtherOffset(OffsetWidth, NewBeginOffset - BeginOffset);
3891 Align OtherAlign =
3892 (IsDest ? II.getSourceAlign() : II.getDestAlign()).valueOrOne();
3893 OtherAlign =
3894 commonAlignment(OtherAlign, OtherOffset.zextOrTrunc(64).getZExtValue());
3895
3896 if (EmitMemCpy) {
3897 // Compute the other pointer, folding as much as possible to produce
3898 // a single, simple GEP in most cases.
3899 OtherPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy,
3900 OtherPtr->getName() + ".");
3901
3902 Value *OurPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
3903 Type *SizeTy = II.getLength()->getType();
3904 Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset);
3905
3906 Value *DestPtr, *SrcPtr;
3907 MaybeAlign DestAlign, SrcAlign;
3908 // Note: IsDest is true iff we're copying into the new alloca slice
3909 if (IsDest) {
3910 DestPtr = OurPtr;
3911 DestAlign = SliceAlign;
3912 SrcPtr = OtherPtr;
3913 SrcAlign = OtherAlign;
3914 } else {
3915 DestPtr = OtherPtr;
3916 DestAlign = OtherAlign;
3917 SrcPtr = OurPtr;
3918 SrcAlign = SliceAlign;
3919 }
3920 CallInst *New = IRB.CreateMemCpy(DestPtr, DestAlign, SrcPtr, SrcAlign,
3921 Size, II.isVolatile());
3922 if (AATags)
3923 New->setAAMetadata(AATags.shift(NewBeginOffset - BeginOffset));
3924
3925 APInt Offset(DL.getIndexTypeSizeInBits(DestPtr->getType()), 0);
3926 if (IsDest) {
3927 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8,
3928 &II, New, DestPtr, nullptr, DL);
3929 } else if (AllocaInst *Base = dyn_cast<AllocaInst>(
3931 DL, Offset, /*AllowNonInbounds*/ true))) {
3932 migrateDebugInfo(Base, IsSplit, Offset.getZExtValue() * 8,
3933 SliceSize * 8, &II, New, DestPtr, nullptr, DL);
3934 }
3935 LLVM_DEBUG(dbgs() << " to: " << *New << "\n");
3936 return false;
3937 }
3938
3939 bool IsWholeAlloca = NewBeginOffset == NewAllocaBeginOffset &&
3940 NewEndOffset == NewAllocaEndOffset;
3941 uint64_t Size = NewEndOffset - NewBeginOffset;
3942 unsigned BeginIndex = VecTy ? getIndex(NewBeginOffset) : 0;
3943 unsigned EndIndex = VecTy ? getIndex(NewEndOffset) : 0;
3944 unsigned NumElements = EndIndex - BeginIndex;
3945 IntegerType *SubIntTy =
3946 IntTy ? Type::getIntNTy(IntTy->getContext(), Size * 8) : nullptr;
3947
3948 // Reset the other pointer type to match the register type we're going to
3949 // use, but using the address space of the original other pointer.
3950 Type *OtherTy;
3951 if (VecTy && !IsWholeAlloca) {
3952 if (NumElements == 1)
3953 OtherTy = VecTy->getElementType();
3954 else
3955 OtherTy = FixedVectorType::get(VecTy->getElementType(), NumElements);
3956 } else if (IntTy && !IsWholeAlloca) {
3957 OtherTy = SubIntTy;
3958 } else {
3959 OtherTy = NewAllocaTy;
3960 }
3961
3962 Value *AdjPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy,
3963 OtherPtr->getName() + ".");
3964 MaybeAlign SrcAlign = OtherAlign;
3965 MaybeAlign DstAlign = SliceAlign;
3966 if (!IsDest)
3967 std::swap(SrcAlign, DstAlign);
3968
3969 Value *SrcPtr;
3970 Value *DstPtr;
3971
3972 if (IsDest) {
3973 DstPtr = getPtrToNewAI(II.getDestAddressSpace(), II.isVolatile());
3974 SrcPtr = AdjPtr;
3975 } else {
3976 DstPtr = AdjPtr;
3977 SrcPtr = getPtrToNewAI(II.getSourceAddressSpace(), II.isVolatile());
3978 }
3979
3980 Value *Src;
3981 if (VecTy && !IsWholeAlloca && !IsDest) {
3982 Src =
3983 IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(), "load");
3984 Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec");
3985 } else if (IntTy && !IsWholeAlloca && !IsDest) {
3986 Src =
3987 IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(), "load");
3988 Src = IRB.CreateBitPreservingCastChain(DL, Src, IntTy);
3989 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
3990 Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract");
3991 } else {
3992 LoadInst *Load = IRB.CreateAlignedLoad(OtherTy, SrcPtr, SrcAlign,
3993 II.isVolatile(), "copyload");
3994 Load->copyMetadata(II, {LLVMContext::MD_mem_parallel_loop_access,
3995 LLVMContext::MD_access_group});
3996 if (AATags)
3997 Load->setAAMetadata(AATags.adjustForAccess(NewBeginOffset - BeginOffset,
3998 Load->getType(), DL));
3999 Src = Load;
4000 }
4001
4002 if (VecTy && !IsWholeAlloca && IsDest) {
4003 Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(),
4004 "oldload");
4005 Src = insertVector(IRB, Old, Src, BeginIndex, "vec");
4006 } else if (IntTy && !IsWholeAlloca && IsDest) {
4007 Value *Old = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(),
4008 "oldload");
4009 Old = IRB.CreateBitPreservingCastChain(DL, Old, IntTy);
4010 uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset;
4011 Src = insertInteger(DL, IRB, Old, Src, Offset, "insert");
4012 Src = IRB.CreateBitPreservingCastChain(DL, Src, NewAllocaTy);
4013 }
4014
4015 StoreInst *Store = cast<StoreInst>(
4016 IRB.CreateAlignedStore(Src, DstPtr, DstAlign, II.isVolatile()));
4017 Store->copyMetadata(II, {LLVMContext::MD_mem_parallel_loop_access,
4018 LLVMContext::MD_access_group});
4019 if (AATags)
4020 Store->setAAMetadata(AATags.adjustForAccess(NewBeginOffset - BeginOffset,
4021 Src->getType(), DL));
4022
4023 APInt Offset(DL.getIndexTypeSizeInBits(DstPtr->getType()), 0);
4024 if (IsDest) {
4025
4026 migrateDebugInfo(&OldAI, IsSplit, NewBeginOffset * 8, SliceSize * 8, &II,
4027 Store, DstPtr, Src, DL);
4028 } else if (AllocaInst *Base = dyn_cast<AllocaInst>(
4030 DL, Offset, /*AllowNonInbounds*/ true))) {
4031 migrateDebugInfo(Base, IsSplit, Offset.getZExtValue() * 8, SliceSize * 8,
4032 &II, Store, DstPtr, Src, DL);
4033 }
4034
4035 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n");
4036 return !II.isVolatile();
4037 }
4038
4039 bool visitIntrinsicInst(IntrinsicInst &II) {
4040 assert((II.isLifetimeStartOrEnd() || II.isDroppable()) &&
4041 "Unexpected intrinsic!");
4042 LLVM_DEBUG(dbgs() << " original: " << II << "\n");
4043
4044 // Record this instruction for deletion.
4045 Pass.DeadInsts.push_back(&II);
4046
4047 if (II.isDroppable()) {
4048 assert(II.getIntrinsicID() == Intrinsic::assume && "Expected assume");
4049 // TODO For now we forget assumed information, this can be improved.
4050 OldPtr->dropDroppableUsesIn(II);
4051 return true;
4052 }
4053
4054 assert(II.getArgOperand(0) == OldPtr);
4055 Type *PointerTy = IRB.getPtrTy(OldPtr->getType()->getPointerAddressSpace());
4056 Value *Ptr = getNewAllocaSlicePtr(IRB, PointerTy);
4057 Value *New;
4058 if (II.getIntrinsicID() == Intrinsic::lifetime_start)
4059 New = IRB.CreateLifetimeStart(Ptr);
4060 else
4061 New = IRB.CreateLifetimeEnd(Ptr);
4062
4063 (void)New;
4064 LLVM_DEBUG(dbgs() << " to: " << *New << "\n");
4065
4066 return true;
4067 }
4068
4069 void fixLoadStoreAlign(Instruction &Root) {
4070 // This algorithm implements the same visitor loop as
4071 // hasUnsafePHIOrSelectUse, and fixes the alignment of each load
4072 // or store found.
4073 SmallPtrSet<Instruction *, 4> Visited;
4074 SmallVector<Instruction *, 4> Uses;
4075 Visited.insert(&Root);
4076 Uses.push_back(&Root);
4077 do {
4078 Instruction *I = Uses.pop_back_val();
4079
4080 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
4081 LI->setAlignment(std::min(LI->getAlign(), getSliceAlign()));
4082 continue;
4083 }
4084 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
4085 SI->setAlignment(std::min(SI->getAlign(), getSliceAlign()));
4086 continue;
4087 }
4088
4092 for (User *U : I->users())
4093 if (Visited.insert(cast<Instruction>(U)).second)
4094 Uses.push_back(cast<Instruction>(U));
4095 } while (!Uses.empty());
4096 }
4097
4098 bool visitPHINode(PHINode &PN) {
4099 LLVM_DEBUG(dbgs() << " original: " << PN << "\n");
4100 assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable");
4101 assert(EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable");
4102
4103 // We would like to compute a new pointer in only one place, but have it be
4104 // as local as possible to the PHI. To do that, we re-use the location of
4105 // the old pointer, which necessarily must be in the right position to
4106 // dominate the PHI.
4107 IRBuilderBase::InsertPointGuard Guard(IRB);
4108 if (isa<PHINode>(OldPtr))
4109 IRB.SetInsertPoint(OldPtr->getParent(),
4110 OldPtr->getParent()->getFirstInsertionPt());
4111 else
4112 IRB.SetInsertPoint(OldPtr);
4113 IRB.SetCurrentDebugLocation(OldPtr->getDebugLoc());
4114
4115 Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
4116 // Replace the operands which were using the old pointer.
4117 std::replace(PN.op_begin(), PN.op_end(), cast<Value>(OldPtr), NewPtr);
4118
4119 LLVM_DEBUG(dbgs() << " to: " << PN << "\n");
4120 deleteIfTriviallyDead(OldPtr);
4121
4122 // Fix the alignment of any loads or stores using this PHI node.
4123 fixLoadStoreAlign(PN);
4124
4125 // PHIs can't be promoted on their own, but often can be speculated. We
4126 // check the speculation outside of the rewriter so that we see the
4127 // fully-rewritten alloca.
4128 PHIUsers.insert(&PN);
4129 return true;
4130 }
4131
4132 bool visitSelectInst(SelectInst &SI) {
4133 LLVM_DEBUG(dbgs() << " original: " << SI << "\n");
4134 assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) &&
4135 "Pointer isn't an operand!");
4136 assert(BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable");
4137 assert(EndOffset <= NewAllocaEndOffset && "Selects are unsplittable");
4138
4139 Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType());
4140 // Replace the operands which were using the old pointer.
4141 if (SI.getOperand(1) == OldPtr)
4142 SI.setOperand(1, NewPtr);
4143 if (SI.getOperand(2) == OldPtr)
4144 SI.setOperand(2, NewPtr);
4145
4146 LLVM_DEBUG(dbgs() << " to: " << SI << "\n");
4147 deleteIfTriviallyDead(OldPtr);
4148
4149 // Fix the alignment of any loads or stores using this select.
4150 fixLoadStoreAlign(SI);
4151
4152 // Selects can't be promoted on their own, but often can be speculated. We
4153 // check the speculation outside of the rewriter so that we see the
4154 // fully-rewritten alloca.
4155 SelectUsers.insert(&SI);
4156 return true;
4157 }
4158};
4159
4160/// Visitor to rewrite aggregate loads and stores as scalar.
4161///
4162/// This pass aggressively rewrites all aggregate loads and stores on
4163/// a particular pointer (or any pointer derived from it which we can identify)
4164/// with scalar loads and stores.
4165class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> {
4166 // Befriend the base class so it can delegate to private visit methods.
4167 friend class InstVisitor<AggLoadStoreRewriter, bool>;
4168
4169 /// Queue of pointer uses to analyze and potentially rewrite.
4171
4172 /// Set to prevent us from cycling with phi nodes and loops.
4173 SmallPtrSet<User *, 8> Visited;
4174
4175 /// The current pointer use being rewritten. This is used to dig up the used
4176 /// value (as opposed to the user).
4177 Use *U = nullptr;
4178
4179 /// Used to calculate offsets, and hence alignment, of subobjects.
4180 const DataLayout &DL;
4181
4182 IRBuilderTy &IRB;
4183
4184public:
4185 AggLoadStoreRewriter(const DataLayout &DL, IRBuilderTy &IRB)
4186 : DL(DL), IRB(IRB) {}
4187
4188 /// Rewrite loads and stores through a pointer and all pointers derived from
4189 /// it.
4190 bool rewrite(Instruction &I) {
4191 LLVM_DEBUG(dbgs() << " Rewriting FCA loads and stores...\n");
4192 enqueueUsers(I);
4193 bool Changed = false;
4194 while (!Queue.empty()) {
4195 U = Queue.pop_back_val();
4196 Changed |= visit(cast<Instruction>(U->getUser()));
4197 }
4198 return Changed;
4199 }
4200
4201private:
4202 /// Enqueue all the users of the given instruction for further processing.
4203 /// This uses a set to de-duplicate users.
4204 void enqueueUsers(Instruction &I) {
4205 for (Use &U : I.uses())
4206 if (Visited.insert(U.getUser()).second)
4207 Queue.push_back(&U);
4208 }
4209
4210 // Conservative default is to not rewrite anything.
4211 bool visitInstruction(Instruction &I) { return false; }
4212
4213 /// Generic recursive split emission class.
4214 template <typename Derived> class OpSplitter {
4215 protected:
4216 /// The builder used to form new instructions.
4217 IRBuilderTy &IRB;
4218
4219 /// The indices which to be used with insert- or extractvalue to select the
4220 /// appropriate value within the aggregate.
4221 SmallVector<unsigned, 4> Indices;
4222
4223 /// The indices to a GEP instruction which will move Ptr to the correct slot
4224 /// within the aggregate.
4225 SmallVector<Value *, 4> GEPIndices;
4226
4227 /// The base pointer of the original op, used as a base for GEPing the
4228 /// split operations.
4229 Value *Ptr;
4230
4231 /// The base pointee type being GEPed into.
4232 Type *BaseTy;
4233
4234 /// Known alignment of the base pointer.
4235 Align BaseAlign;
4236
4237 /// To calculate offset of each component so we can correctly deduce
4238 /// alignments.
4239 const DataLayout &DL;
4240
4241 /// Initialize the splitter with an insertion point, Ptr and start with a
4242 /// single zero GEP index.
4243 OpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
4244 Align BaseAlign, const DataLayout &DL, IRBuilderTy &IRB)
4245 : IRB(IRB), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr), BaseTy(BaseTy),
4246 BaseAlign(BaseAlign), DL(DL) {
4247 IRB.SetInsertPoint(InsertionPoint);
4248 }
4249
4250 public:
4251 /// Generic recursive split emission routine.
4252 ///
4253 /// This method recursively splits an aggregate op (load or store) into
4254 /// scalar or vector ops. It splits recursively until it hits a single value
4255 /// and emits that single value operation via the template argument.
4256 ///
4257 /// The logic of this routine relies on GEPs and insertvalue and
4258 /// extractvalue all operating with the same fundamental index list, merely
4259 /// formatted differently (GEPs need actual values).
4260 ///
4261 /// \param Ty The type being split recursively into smaller ops.
4262 /// \param Agg The aggregate value being built up or stored, depending on
4263 /// whether this is splitting a load or a store respectively.
4264 void emitSplitOps(Type *Ty, Value *&Agg, const Twine &Name) {
4265 if (Ty->isSingleValueType()) {
4266 unsigned Offset = DL.getIndexedOffsetInType(BaseTy, GEPIndices);
4267 return static_cast<Derived *>(this)->emitFunc(
4268 Ty, Agg, commonAlignment(BaseAlign, Offset), Name);
4269 }
4270
4271 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
4272 unsigned OldSize = Indices.size();
4273 (void)OldSize;
4274 for (unsigned Idx = 0, Size = ATy->getNumElements(); Idx != Size;
4275 ++Idx) {
4276 assert(Indices.size() == OldSize && "Did not return to the old size");
4277 Indices.push_back(Idx);
4278 GEPIndices.push_back(IRB.getInt32(Idx));
4279 emitSplitOps(ATy->getElementType(), Agg, Name + "." + Twine(Idx));
4280 GEPIndices.pop_back();
4281 Indices.pop_back();
4282 }
4283 return;
4284 }
4285
4286 if (StructType *STy = dyn_cast<StructType>(Ty)) {
4287 unsigned OldSize = Indices.size();
4288 (void)OldSize;
4289 for (unsigned Idx = 0, Size = STy->getNumElements(); Idx != Size;
4290 ++Idx) {
4291 assert(Indices.size() == OldSize && "Did not return to the old size");
4292 Indices.push_back(Idx);
4293 GEPIndices.push_back(IRB.getInt32(Idx));
4294 emitSplitOps(STy->getElementType(Idx), Agg, Name + "." + Twine(Idx));
4295 GEPIndices.pop_back();
4296 Indices.pop_back();
4297 }
4298 return;
4299 }
4300
4301 llvm_unreachable("Only arrays and structs are aggregate loadable types");
4302 }
4303 };
4304
4305 struct LoadOpSplitter : public OpSplitter<LoadOpSplitter> {
4306 AAMDNodes AATags;
4307 // A vector to hold the split components that we want to emit
4308 // separate fake uses for.
4309 SmallVector<Value *, 4> Components;
4310 // A vector to hold all the fake uses of the struct that we are splitting.
4311 // Usually there should only be one, but we are handling the general case.
4313
4314 LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
4315 AAMDNodes AATags, Align BaseAlign, const DataLayout &DL,
4316 IRBuilderTy &IRB)
4317 : OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign, DL,
4318 IRB),
4319 AATags(AATags) {}
4320
4321 /// Emit a leaf load of a single value. This is called at the leaves of the
4322 /// recursive emission to actually load values.
4323 void emitFunc(Type *Ty, Value *&Agg, Align Alignment, const Twine &Name) {
4325 // Load the single value and insert it using the indices.
4326 Value *GEP =
4327 IRB.CreateInBoundsGEP(BaseTy, Ptr, GEPIndices, Name + ".gep");
4328 LoadInst *Load =
4329 IRB.CreateAlignedLoad(Ty, GEP, Alignment, Name + ".load");
4330
4331 APInt Offset(
4332 DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace()), 0);
4333 if (AATags &&
4334 GEPOperator::accumulateConstantOffset(BaseTy, GEPIndices, DL, Offset))
4335 Load->setAAMetadata(
4336 AATags.adjustForAccess(Offset.getZExtValue(), Load->getType(), DL));
4337 // Record the load so we can generate a fake use for this aggregate
4338 // component.
4339 Components.push_back(Load);
4340
4341 Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert");
4342 LLVM_DEBUG(dbgs() << " to: " << *Load << "\n");
4343 }
4344
4345 // Stash the fake uses that use the value generated by this instruction.
4346 void recordFakeUses(LoadInst &LI) {
4347 for (Use &U : LI.uses())
4348 if (auto *II = dyn_cast<IntrinsicInst>(U.getUser()))
4349 if (II->getIntrinsicID() == Intrinsic::fake_use)
4350 FakeUses.push_back(II);
4351 }
4352
4353 // Replace all fake uses of the aggregate with a series of fake uses, one
4354 // for each split component.
4355 void emitFakeUses() {
4356 for (Instruction *I : FakeUses) {
4357 IRB.SetInsertPoint(I);
4358 for (auto *V : Components)
4359 IRB.CreateIntrinsic(Intrinsic::fake_use, {V});
4360 I->eraseFromParent();
4361 }
4362 }
4363 };
4364
4365 bool visitLoadInst(LoadInst &LI) {
4366 assert(LI.getPointerOperand() == *U);
4367 if (!LI.isSimple() || LI.getType()->isSingleValueType())
4368 return false;
4369
4370 // We have an aggregate being loaded, split it apart.
4371 LLVM_DEBUG(dbgs() << " original: " << LI << "\n");
4372 LoadOpSplitter Splitter(&LI, *U, LI.getType(), LI.getAAMetadata(),
4373 getAdjustedAlignment(&LI, 0), DL, IRB);
4374 Splitter.recordFakeUses(LI);
4376 Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca");
4377 Splitter.emitFakeUses();
4378 Visited.erase(&LI);
4379 LI.replaceAllUsesWith(V);
4380 LI.eraseFromParent();
4381 return true;
4382 }
4383
4384 struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> {
4385 StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy,
4386 AAMDNodes AATags, StoreInst *AggStore, Align BaseAlign,
4387 const DataLayout &DL, IRBuilderTy &IRB)
4388 : OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign,
4389 DL, IRB),
4390 AATags(AATags), AggStore(AggStore) {}
4391 AAMDNodes AATags;
4392 StoreInst *AggStore;
4393 /// Emit a leaf store of a single value. This is called at the leaves of the
4394 /// recursive emission to actually produce stores.
4395 void emitFunc(Type *Ty, Value *&Agg, Align Alignment, const Twine &Name) {
4397 // Extract the single value and store it using the indices.
4398 //
4399 // The gep and extractvalue values are factored out of the CreateStore
4400 // call to make the output independent of the argument evaluation order.
4401 Value *ExtractValue =
4402 IRB.CreateExtractValue(Agg, Indices, Name + ".extract");
4403 Value *InBoundsGEP =
4404 IRB.CreateInBoundsGEP(BaseTy, Ptr, GEPIndices, Name + ".gep");
4405 StoreInst *Store =
4406 IRB.CreateAlignedStore(ExtractValue, InBoundsGEP, Alignment);
4407
4408 APInt Offset(
4409 DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace()), 0);
4410 GEPOperator::accumulateConstantOffset(BaseTy, GEPIndices, DL, Offset);
4411 if (AATags) {
4412 Store->setAAMetadata(AATags.adjustForAccess(
4413 Offset.getZExtValue(), ExtractValue->getType(), DL));
4414 }
4415
4416 // migrateDebugInfo requires the base Alloca. Walk to it from this gep.
4417 // If we cannot (because there's an intervening non-const or unbounded
4418 // gep) then we wouldn't expect to see dbg.assign intrinsics linked to
4419 // this instruction.
4421 if (auto *OldAI = dyn_cast<AllocaInst>(Base)) {
4422 uint64_t SizeInBits =
4423 DL.getTypeSizeInBits(Store->getValueOperand()->getType());
4424 migrateDebugInfo(OldAI, /*IsSplit*/ true, Offset.getZExtValue() * 8,
4425 SizeInBits, AggStore, Store,
4426 Store->getPointerOperand(), Store->getValueOperand(),
4427 DL);
4428 } else {
4430 "AT: unexpected debug.assign linked to store through "
4431 "unbounded GEP");
4432 }
4433 LLVM_DEBUG(dbgs() << " to: " << *Store << "\n");
4434 }
4435 };
4436
4437 bool visitStoreInst(StoreInst &SI) {
4438 if (!SI.isSimple() || SI.getPointerOperand() != *U)
4439 return false;
4440 Value *V = SI.getValueOperand();
4441 if (V->getType()->isSingleValueType())
4442 return false;
4443
4444 // We have an aggregate being stored, split it apart.
4445 LLVM_DEBUG(dbgs() << " original: " << SI << "\n");
4446 StoreOpSplitter Splitter(&SI, *U, V->getType(), SI.getAAMetadata(), &SI,
4447 getAdjustedAlignment(&SI, 0), DL, IRB);
4448 Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca");
4449 Visited.erase(&SI);
4450 // The stores replacing SI each have markers describing fragments of the
4451 // assignment so delete the assignment markers linked to SI.
4453 SI.eraseFromParent();
4454 return true;
4455 }
4456
4457 bool visitBitCastInst(BitCastInst &BC) {
4458 enqueueUsers(BC);
4459 return false;
4460 }
4461
4462 bool visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) {
4463 enqueueUsers(ASC);
4464 return false;
4465 }
4466
4467 // Unfold gep (select cond, ptr1, ptr2), idx
4468 // => select cond, gep(ptr1, idx), gep(ptr2, idx)
4469 // and gep ptr, (select cond, idx1, idx2)
4470 // => select cond, gep(ptr, idx1), gep(ptr, idx2)
4471 // We also allow for i1 zext indices, which are equivalent to selects.
4472 bool unfoldGEPSelect(GetElementPtrInst &GEPI) {
4473 // Check whether the GEP has exactly one select operand and all indices
4474 // will become constant after the transform.
4476 for (Value *Op : GEPI.indices()) {
4477 if (auto *SI = dyn_cast<SelectInst>(Op)) {
4478 if (Sel)
4479 return false;
4480
4481 Sel = SI;
4482 if (!isa<ConstantInt>(SI->getTrueValue()) ||
4483 !isa<ConstantInt>(SI->getFalseValue()))
4484 return false;
4485 continue;
4486 }
4487 if (auto *ZI = dyn_cast<ZExtInst>(Op)) {
4488 if (Sel)
4489 return false;
4490 Sel = ZI;
4491 if (!ZI->getSrcTy()->isIntegerTy(1))
4492 return false;
4493 continue;
4494 }
4495
4496 if (!isa<ConstantInt>(Op))
4497 return false;
4498 }
4499
4500 if (!Sel)
4501 return false;
4502
4503 LLVM_DEBUG(dbgs() << " Rewriting gep(select) -> select(gep):\n";
4504 dbgs() << " original: " << *Sel << "\n";
4505 dbgs() << " " << GEPI << "\n";);
4506
4507 auto GetNewOps = [&](Value *SelOp) {
4508 SmallVector<Value *> NewOps;
4509 for (Value *Op : GEPI.operands())
4510 if (Op == Sel)
4511 NewOps.push_back(SelOp);
4512 else
4513 NewOps.push_back(Op);
4514 return NewOps;
4515 };
4516
4517 Value *Cond, *True, *False;
4518 Instruction *MDFrom = nullptr;
4519 if (auto *SI = dyn_cast<SelectInst>(Sel)) {
4520 Cond = SI->getCondition();
4521 True = SI->getTrueValue();
4522 False = SI->getFalseValue();
4523 MDFrom = SI;
4524 } else {
4525 Cond = Sel->getOperand(0);
4526 True = ConstantInt::get(Sel->getType(), 1);
4527 False = ConstantInt::get(Sel->getType(), 0);
4528 }
4529 SmallVector<Value *> TrueOps = GetNewOps(True);
4530 SmallVector<Value *> FalseOps = GetNewOps(False);
4531
4532 IRB.SetInsertPoint(&GEPI);
4533 GEPNoWrapFlags NW = GEPI.getNoWrapFlags();
4534
4535 Type *Ty = GEPI.getSourceElementType();
4536 Value *NTrue = IRB.CreateGEP(Ty, TrueOps[0], ArrayRef(TrueOps).drop_front(),
4537 True->getName() + ".sroa.gep", NW);
4538
4539 Value *NFalse =
4540 IRB.CreateGEP(Ty, FalseOps[0], ArrayRef(FalseOps).drop_front(),
4541 False->getName() + ".sroa.gep", NW);
4542
4543 Value *NSel = MDFrom
4544 ? IRB.CreateSelect(Cond, NTrue, NFalse,
4545 Sel->getName() + ".sroa.sel", MDFrom)
4546 : IRB.CreateSelectWithUnknownProfile(
4547 Cond, NTrue, NFalse, DEBUG_TYPE,
4548 Sel->getName() + ".sroa.sel");
4549 Visited.erase(&GEPI);
4550 GEPI.replaceAllUsesWith(NSel);
4551 GEPI.eraseFromParent();
4552 Instruction *NSelI = cast<Instruction>(NSel);
4553 Visited.insert(NSelI);
4554 enqueueUsers(*NSelI);
4555
4556 LLVM_DEBUG(dbgs() << " to: " << *NTrue << "\n";
4557 dbgs() << " " << *NFalse << "\n";
4558 dbgs() << " " << *NSel << "\n";);
4559
4560 return true;
4561 }
4562
4563 // Unfold gep (phi ptr1, ptr2), idx
4564 // => phi ((gep ptr1, idx), (gep ptr2, idx))
4565 // and gep ptr, (phi idx1, idx2)
4566 // => phi ((gep ptr, idx1), (gep ptr, idx2))
4567 bool unfoldGEPPhi(GetElementPtrInst &GEPI) {
4568 // To prevent infinitely expanding recursive phis, bail if the GEP pointer
4569 // operand (looking through the phi if it is the phi we want to unfold) is
4570 // an instruction besides a static alloca.
4571 PHINode *Phi = dyn_cast<PHINode>(GEPI.getPointerOperand());
4572 auto IsInvalidPointerOperand = [](Value *V) {
4573 if (!isa<Instruction>(V))
4574 return false;
4575 if (auto *AI = dyn_cast<AllocaInst>(V))
4576 return !AI->isStaticAlloca();
4577 return true;
4578 };
4579 if (Phi) {
4580 if (any_of(Phi->operands(), IsInvalidPointerOperand))
4581 return false;
4582 } else {
4583 if (IsInvalidPointerOperand(GEPI.getPointerOperand()))
4584 return false;
4585 }
4586 // Check whether the GEP has exactly one phi operand (including the pointer
4587 // operand) and all indices will become constant after the transform.
4588 for (Value *Op : GEPI.indices()) {
4589 if (auto *SI = dyn_cast<PHINode>(Op)) {
4590 if (Phi)
4591 return false;
4592
4593 Phi = SI;
4594 if (!all_of(Phi->incoming_values(),
4595 [](Value *V) { return isa<ConstantInt>(V); }))
4596 return false;
4597 continue;
4598 }
4599
4600 if (!isa<ConstantInt>(Op))
4601 return false;
4602 }
4603
4604 if (!Phi)
4605 return false;
4606
4607 LLVM_DEBUG(dbgs() << " Rewriting gep(phi) -> phi(gep):\n";
4608 dbgs() << " original: " << *Phi << "\n";
4609 dbgs() << " " << GEPI << "\n";);
4610
4611 auto GetNewOps = [&](Value *PhiOp) {
4612 SmallVector<Value *> NewOps;
4613 for (Value *Op : GEPI.operands())
4614 if (Op == Phi)
4615 NewOps.push_back(PhiOp);
4616 else
4617 NewOps.push_back(Op);
4618 return NewOps;
4619 };
4620
4621 IRB.SetInsertPoint(Phi);
4622 PHINode *NewPhi = IRB.CreatePHI(GEPI.getType(), Phi->getNumIncomingValues(),
4623 Phi->getName() + ".sroa.phi");
4624
4625 Type *SourceTy = GEPI.getSourceElementType();
4626 // We only handle arguments, constants, and static allocas here, so we can
4627 // insert GEPs at the end of the entry block.
4628 IRB.SetInsertPoint(GEPI.getFunction()->getEntryBlock().getTerminator());
4629 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
4630 Value *Op = Phi->getIncomingValue(I);
4631 BasicBlock *BB = Phi->getIncomingBlock(I);
4632 Value *NewGEP;
4633 if (int NI = NewPhi->getBasicBlockIndex(BB); NI >= 0) {
4634 NewGEP = NewPhi->getIncomingValue(NI);
4635 } else {
4636 SmallVector<Value *> NewOps = GetNewOps(Op);
4637 NewGEP =
4638 IRB.CreateGEP(SourceTy, NewOps[0], ArrayRef(NewOps).drop_front(),
4639 Phi->getName() + ".sroa.gep", GEPI.getNoWrapFlags());
4640 }
4641 NewPhi->addIncoming(NewGEP, BB);
4642 }
4643
4644 Visited.erase(&GEPI);
4645 GEPI.replaceAllUsesWith(NewPhi);
4646 GEPI.eraseFromParent();
4647 Visited.insert(NewPhi);
4648 enqueueUsers(*NewPhi);
4649
4650 LLVM_DEBUG(dbgs() << " to: ";
4651 for (Value *In
4652 : NewPhi->incoming_values()) dbgs()
4653 << "\n " << *In;
4654 dbgs() << "\n " << *NewPhi << '\n');
4655
4656 return true;
4657 }
4658
4659 bool visitGetElementPtrInst(GetElementPtrInst &GEPI) {
4660 if (unfoldGEPSelect(GEPI))
4661 return true;
4662
4663 if (unfoldGEPPhi(GEPI))
4664 return true;
4665
4666 enqueueUsers(GEPI);
4667 return false;
4668 }
4669
4670 bool visitPHINode(PHINode &PN) {
4671 enqueueUsers(PN);
4672 return false;
4673 }
4674
4675 bool visitSelectInst(SelectInst &SI) {
4676 enqueueUsers(SI);
4677 return false;
4678 }
4679};
4680
4681} // end anonymous namespace
4682
4683/// Strip aggregate type wrapping.
4684///
4685/// This removes no-op aggregate types wrapping an underlying type. It will
4686/// strip as many layers of types as it can without changing either the type
4687/// size or the allocated size.
4689 if (Ty->isSingleValueType())
4690 return Ty;
4691
4692 uint64_t AllocSize = DL.getTypeAllocSize(Ty).getFixedValue();
4693 uint64_t TypeSize = DL.getTypeSizeInBits(Ty).getFixedValue();
4694
4695 Type *InnerTy;
4696 if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) {
4697 InnerTy = ArrTy->getElementType();
4698 } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
4699 const StructLayout *SL = DL.getStructLayout(STy);
4700 unsigned Index = SL->getElementContainingOffset(0);
4701 InnerTy = STy->getElementType(Index);
4702 } else {
4703 return Ty;
4704 }
4705
4706 if (AllocSize > DL.getTypeAllocSize(InnerTy).getFixedValue() ||
4707 TypeSize > DL.getTypeSizeInBits(InnerTy).getFixedValue())
4708 return Ty;
4709
4710 return stripAggregateTypeWrapping(DL, InnerTy);
4711}
4712
4713/// Try to find a partition of the aggregate type passed in for a given
4714/// offset and size.
4715///
4716/// This recurses through the aggregate type and tries to compute a subtype
4717/// based on the offset and size. When the offset and size span a sub-section
4718/// of an array, it will even compute a new array type for that sub-section,
4719/// and the same for structs.
4720///
4721/// Note that this routine is very strict and tries to find a partition of the
4722/// type which produces the *exact* right offset and size. It is not forgiving
4723/// when the size or offset cause either end of type-based partition to be off.
4724/// Also, this is a best-effort routine. It is reasonable to give up and not
4725/// return a type if necessary.
4727 uint64_t Size) {
4728 if (Offset == 0 && DL.getTypeAllocSize(Ty).getFixedValue() == Size)
4729 return stripAggregateTypeWrapping(DL, Ty);
4730 if (Offset > DL.getTypeAllocSize(Ty).getFixedValue() ||
4731 (DL.getTypeAllocSize(Ty).getFixedValue() - Offset) < Size)
4732 return nullptr;
4733
4734 if (isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
4735 Type *ElementTy;
4736 uint64_t TyNumElements;
4737 if (auto *AT = dyn_cast<ArrayType>(Ty)) {
4738 ElementTy = AT->getElementType();
4739 TyNumElements = AT->getNumElements();
4740 } else {
4741 // FIXME: This isn't right for vectors with non-byte-sized or
4742 // non-power-of-two sized elements.
4743 auto *VT = cast<FixedVectorType>(Ty);
4744 ElementTy = VT->getElementType();
4745 TyNumElements = VT->getNumElements();
4746 }
4747 uint64_t ElementSize = DL.getTypeAllocSize(ElementTy).getFixedValue();
4748 uint64_t NumSkippedElements = Offset / ElementSize;
4749 if (NumSkippedElements >= TyNumElements)
4750 return nullptr;
4751 Offset -= NumSkippedElements * ElementSize;
4752
4753 // First check if we need to recurse.
4754 if (Offset > 0 || Size < ElementSize) {
4755 // Bail if the partition ends in a different array element.
4756 if ((Offset + Size) > ElementSize)
4757 return nullptr;
4758 // Recurse through the element type trying to peel off offset bytes.
4759 return getTypePartition(DL, ElementTy, Offset, Size);
4760 }
4761 assert(Offset == 0);
4762
4763 if (Size == ElementSize)
4764 return stripAggregateTypeWrapping(DL, ElementTy);
4765 assert(Size > ElementSize);
4766 uint64_t NumElements = Size / ElementSize;
4767 if (NumElements * ElementSize != Size)
4768 return nullptr;
4769 return ArrayType::get(ElementTy, NumElements);
4770 }
4771
4773 if (!STy)
4774 return nullptr;
4775
4776 const StructLayout *SL = DL.getStructLayout(STy);
4777
4778 if (SL->getSizeInBits().isScalable())
4779 return nullptr;
4780
4781 if (Offset >= SL->getSizeInBytes())
4782 return nullptr;
4783 uint64_t EndOffset = Offset + Size;
4784 if (EndOffset > SL->getSizeInBytes())
4785 return nullptr;
4786
4787 unsigned Index = SL->getElementContainingOffset(Offset);
4788 Offset -= SL->getElementOffset(Index);
4789
4790 Type *ElementTy = STy->getElementType(Index);
4791 uint64_t ElementSize = DL.getTypeAllocSize(ElementTy).getFixedValue();
4792 if (Offset >= ElementSize)
4793 return nullptr; // The offset points into alignment padding.
4794
4795 // See if any partition must be contained by the element.
4796 if (Offset > 0 || Size < ElementSize) {
4797 if ((Offset + Size) > ElementSize)
4798 return nullptr;
4799 return getTypePartition(DL, ElementTy, Offset, Size);
4800 }
4801 assert(Offset == 0);
4802
4803 if (Size == ElementSize)
4804 return stripAggregateTypeWrapping(DL, ElementTy);
4805
4806 StructType::element_iterator EI = STy->element_begin() + Index,
4807 EE = STy->element_end();
4808 if (EndOffset < SL->getSizeInBytes()) {
4809 unsigned EndIndex = SL->getElementContainingOffset(EndOffset);
4810 if (Index == EndIndex)
4811 return nullptr; // Within a single element and its padding.
4812
4813 // Don't try to form "natural" types if the elements don't line up with the
4814 // expected size.
4815 // FIXME: We could potentially recurse down through the last element in the
4816 // sub-struct to find a natural end point.
4817 if (SL->getElementOffset(EndIndex) != EndOffset)
4818 return nullptr;
4819
4820 assert(Index < EndIndex);
4821 EE = STy->element_begin() + EndIndex;
4822 }
4823
4824 // Try to build up a sub-structure.
4825 StructType *SubTy =
4826 StructType::get(STy->getContext(), ArrayRef(EI, EE), STy->isPacked());
4827 const StructLayout *SubSL = DL.getStructLayout(SubTy);
4828 if (Size != SubSL->getSizeInBytes())
4829 return nullptr; // The sub-struct doesn't have quite the size needed.
4830
4831 return SubTy;
4832}
4833
4834/// Pre-split loads and stores to simplify rewriting.
4835///
4836/// We want to break up the splittable load+store pairs as much as
4837/// possible. This is important to do as a preprocessing step, as once we
4838/// start rewriting the accesses to partitions of the alloca we lose the
4839/// necessary information to correctly split apart paired loads and stores
4840/// which both point into this alloca. The case to consider is something like
4841/// the following:
4842///
4843/// %a = alloca [12 x i8]
4844/// %gep1 = getelementptr i8, ptr %a, i32 0
4845/// %gep2 = getelementptr i8, ptr %a, i32 4
4846/// %gep3 = getelementptr i8, ptr %a, i32 8
4847/// store float 0.0, ptr %gep1
4848/// store float 1.0, ptr %gep2
4849/// %v = load i64, ptr %gep1
4850/// store i64 %v, ptr %gep2
4851/// %f1 = load float, ptr %gep2
4852/// %f2 = load float, ptr %gep3
4853///
4854/// Here we want to form 3 partitions of the alloca, each 4 bytes large, and
4855/// promote everything so we recover the 2 SSA values that should have been
4856/// there all along.
4857///
4858/// \returns true if any changes are made.
4859bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) {
4860 LLVM_DEBUG(dbgs() << "Pre-splitting loads and stores\n");
4861
4862 // Track the loads and stores which are candidates for pre-splitting here, in
4863 // the order they first appear during the partition scan. These give stable
4864 // iteration order and a basis for tracking which loads and stores we
4865 // actually split.
4868
4869 // We need to accumulate the splits required of each load or store where we
4870 // can find them via a direct lookup. This is important to cross-check loads
4871 // and stores against each other. We also track the slice so that we can kill
4872 // all the slices that end up split.
4873 struct SplitOffsets {
4874 Slice *S;
4875 std::vector<uint64_t> Splits;
4876 };
4877 SmallDenseMap<Instruction *, SplitOffsets, 8> SplitOffsetsMap;
4878
4879 // Track loads out of this alloca which cannot, for any reason, be pre-split.
4880 // This is important as we also cannot pre-split stores of those loads!
4881 // FIXME: This is all pretty gross. It means that we can be more aggressive
4882 // in pre-splitting when the load feeding the store happens to come from
4883 // a separate alloca. Put another way, the effectiveness of SROA would be
4884 // decreased by a frontend which just concatenated all of its local allocas
4885 // into one big flat alloca. But defeating such patterns is exactly the job
4886 // SROA is tasked with! Sadly, to not have this discrepancy we would have
4887 // change store pre-splitting to actually force pre-splitting of the load
4888 // that feeds it *and all stores*. That makes pre-splitting much harder, but
4889 // maybe it would make it more principled?
4890 SmallPtrSet<LoadInst *, 8> UnsplittableLoads;
4891
4892 LLVM_DEBUG(dbgs() << " Searching for candidate loads and stores\n");
4893 for (auto &P : AS.partitions()) {
4894 for (Slice &S : P) {
4895 Instruction *I = cast<Instruction>(S.getUse()->getUser());
4896 if (!S.isSplittable() || S.endOffset() <= P.endOffset()) {
4897 // If this is a load we have to track that it can't participate in any
4898 // pre-splitting. If this is a store of a load we have to track that
4899 // that load also can't participate in any pre-splitting.
4900 if (auto *LI = dyn_cast<LoadInst>(I))
4901 UnsplittableLoads.insert(LI);
4902 else if (auto *SI = dyn_cast<StoreInst>(I))
4903 if (auto *LI = dyn_cast<LoadInst>(SI->getValueOperand()))
4904 UnsplittableLoads.insert(LI);
4905 continue;
4906 }
4907 assert(P.endOffset() > S.beginOffset() &&
4908 "Empty or backwards partition!");
4909
4910 // Determine if this is a pre-splittable slice.
4911 if (auto *LI = dyn_cast<LoadInst>(I)) {
4912 assert(!LI->isVolatile() && "Cannot split volatile loads!");
4913
4914 // The load must be used exclusively to store into other pointers for
4915 // us to be able to arbitrarily pre-split it. The stores must also be
4916 // simple to avoid changing semantics.
4917 auto IsLoadSimplyStored = [](LoadInst *LI) {
4918 for (User *LU : LI->users()) {
4919 auto *SI = dyn_cast<StoreInst>(LU);
4920 if (!SI || !SI->isSimple())
4921 return false;
4922 }
4923 return true;
4924 };
4925 if (!IsLoadSimplyStored(LI)) {
4926 UnsplittableLoads.insert(LI);
4927 continue;
4928 }
4929
4930 Loads.push_back(LI);
4931 } else if (auto *SI = dyn_cast<StoreInst>(I)) {
4932 if (S.getUse() != &SI->getOperandUse(SI->getPointerOperandIndex()))
4933 // Skip stores *of* pointers. FIXME: This shouldn't even be possible!
4934 continue;
4935 auto *StoredLoad = dyn_cast<LoadInst>(SI->getValueOperand());
4936 if (!StoredLoad || !StoredLoad->isSimple())
4937 continue;
4938 assert(!SI->isVolatile() && "Cannot split volatile stores!");
4939
4940 Stores.push_back(SI);
4941 } else {
4942 // Other uses cannot be pre-split.
4943 continue;
4944 }
4945
4946 // Record the initial split.
4947 LLVM_DEBUG(dbgs() << " Candidate: " << *I << "\n");
4948 auto &Offsets = SplitOffsetsMap[I];
4949 assert(Offsets.Splits.empty() &&
4950 "Should not have splits the first time we see an instruction!");
4951 Offsets.S = &S;
4952 Offsets.Splits.push_back(P.endOffset() - S.beginOffset());
4953 }
4954
4955 // Now scan the already split slices, and add a split for any of them which
4956 // we're going to pre-split.
4957 for (Slice *S : P.splitSliceTails()) {
4958 auto SplitOffsetsMapI =
4959 SplitOffsetsMap.find(cast<Instruction>(S->getUse()->getUser()));
4960 if (SplitOffsetsMapI == SplitOffsetsMap.end())
4961 continue;
4962 auto &Offsets = SplitOffsetsMapI->second;
4963
4964 assert(Offsets.S == S && "Found a mismatched slice!");
4965 assert(!Offsets.Splits.empty() &&
4966 "Cannot have an empty set of splits on the second partition!");
4967 assert(Offsets.Splits.back() ==
4968 P.beginOffset() - Offsets.S->beginOffset() &&
4969 "Previous split does not end where this one begins!");
4970
4971 // Record each split. The last partition's end isn't needed as the size
4972 // of the slice dictates that.
4973 if (S->endOffset() > P.endOffset())
4974 Offsets.Splits.push_back(P.endOffset() - Offsets.S->beginOffset());
4975 }
4976 }
4977
4978 // We may have split loads where some of their stores are split stores. For
4979 // such loads and stores, we can only pre-split them if their splits exactly
4980 // match relative to their starting offset. We have to verify this prior to
4981 // any rewriting.
4982 llvm::erase_if(Stores, [&UnsplittableLoads, &SplitOffsetsMap](StoreInst *SI) {
4983 // Lookup the load we are storing in our map of split
4984 // offsets.
4985 auto *LI = cast<LoadInst>(SI->getValueOperand());
4986 // If it was completely unsplittable, then we're done,
4987 // and this store can't be pre-split.
4988 if (UnsplittableLoads.count(LI))
4989 return true;
4990
4991 auto LoadOffsetsI = SplitOffsetsMap.find(LI);
4992 if (LoadOffsetsI == SplitOffsetsMap.end())
4993 return false; // Unrelated loads are definitely safe.
4994 auto &LoadOffsets = LoadOffsetsI->second;
4995
4996 // Now lookup the store's offsets.
4997 auto &StoreOffsets = SplitOffsetsMap[SI];
4998
4999 // If the relative offsets of each split in the load and
5000 // store match exactly, then we can split them and we
5001 // don't need to remove them here.
5002 if (LoadOffsets.Splits == StoreOffsets.Splits)
5003 return false;
5004
5005 LLVM_DEBUG(dbgs() << " Mismatched splits for load and store:\n"
5006 << " " << *LI << "\n"
5007 << " " << *SI << "\n");
5008
5009 // We've found a store and load that we need to split
5010 // with mismatched relative splits. Just give up on them
5011 // and remove both instructions from our list of
5012 // candidates.
5013 UnsplittableLoads.insert(LI);
5014 return true;
5015 });
5016 // Now we have to go *back* through all the stores, because a later store may
5017 // have caused an earlier store's load to become unsplittable and if it is
5018 // unsplittable for the later store, then we can't rely on it being split in
5019 // the earlier store either.
5020 llvm::erase_if(Stores, [&UnsplittableLoads](StoreInst *SI) {
5021 auto *LI = cast<LoadInst>(SI->getValueOperand());
5022 return UnsplittableLoads.count(LI);
5023 });
5024 // Once we've established all the loads that can't be split for some reason,
5025 // filter any that made it into our list out.
5026 llvm::erase_if(Loads, [&UnsplittableLoads](LoadInst *LI) {
5027 return UnsplittableLoads.count(LI);
5028 });
5029
5030 // If no loads or stores are left, there is no pre-splitting to be done for
5031 // this alloca.
5032 if (Loads.empty() && Stores.empty())
5033 return false;
5034
5035 // From here on, we can't fail and will be building new accesses, so rig up
5036 // an IR builder.
5037 IRBuilderTy IRB(&AI);
5038
5039 // Collect the new slices which we will merge into the alloca slices.
5040 SmallVector<Slice, 4> NewSlices;
5041
5042 // Track any allocas we end up splitting loads and stores for so we iterate
5043 // on them.
5044 SmallPtrSet<AllocaInst *, 4> ResplitPromotableAllocas;
5045
5046 // At this point, we have collected all of the loads and stores we can
5047 // pre-split, and the specific splits needed for them. We actually do the
5048 // splitting in a specific order in order to handle when one of the loads in
5049 // the value operand to one of the stores.
5050 //
5051 // First, we rewrite all of the split loads, and just accumulate each split
5052 // load in a parallel structure. We also build the slices for them and append
5053 // them to the alloca slices.
5054 SmallDenseMap<LoadInst *, std::vector<LoadInst *>, 1> SplitLoadsMap;
5055 std::vector<LoadInst *> SplitLoads;
5056 const DataLayout &DL = AI.getDataLayout();
5057 for (LoadInst *LI : Loads) {
5058 SplitLoads.clear();
5059
5060 auto &Offsets = SplitOffsetsMap[LI];
5061 unsigned SliceSize = Offsets.S->endOffset() - Offsets.S->beginOffset();
5062 assert(LI->getType()->getIntegerBitWidth() % 8 == 0 &&
5063 "Load must have type size equal to store size");
5064 assert(LI->getType()->getIntegerBitWidth() / 8 >= SliceSize &&
5065 "Load must be >= slice size");
5066
5067 uint64_t BaseOffset = Offsets.S->beginOffset();
5068 assert(BaseOffset + SliceSize > BaseOffset &&
5069 "Cannot represent alloca access size using 64-bit integers!");
5070
5072 IRB.SetInsertPoint(LI);
5073
5074 LLVM_DEBUG(dbgs() << " Splitting load: " << *LI << "\n");
5075
5076 uint64_t PartOffset = 0, PartSize = Offsets.Splits.front();
5077 int Idx = 0, Size = Offsets.Splits.size();
5078 for (;;) {
5079 auto *PartTy = Type::getIntNTy(LI->getContext(), PartSize * 8);
5080 auto AS = LI->getPointerAddressSpace();
5081 auto *PartPtrTy = LI->getPointerOperandType();
5082 LoadInst *PLoad = IRB.CreateAlignedLoad(
5083 PartTy,
5084 getAdjustedPtr(IRB, DL, BasePtr,
5085 APInt(DL.getIndexSizeInBits(AS), PartOffset),
5086 PartPtrTy, BasePtr->getName() + "."),
5087 getAdjustedAlignment(LI, PartOffset),
5088 /*IsVolatile*/ false, LI->getName());
5089 PLoad->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access,
5090 LLVMContext::MD_access_group});
5091
5092 // Append this load onto the list of split loads so we can find it later
5093 // to rewrite the stores.
5094 SplitLoads.push_back(PLoad);
5095
5096 // Now build a new slice for the alloca.
5097 NewSlices.push_back(
5098 Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize,
5099 &PLoad->getOperandUse(PLoad->getPointerOperandIndex()),
5100 /*IsSplittable*/ false));
5101 LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset()
5102 << ", " << NewSlices.back().endOffset()
5103 << "): " << *PLoad << "\n");
5104
5105 // See if we've handled all the splits.
5106 if (Idx >= Size)
5107 break;
5108
5109 // Setup the next partition.
5110 PartOffset = Offsets.Splits[Idx];
5111 ++Idx;
5112 PartSize = (Idx < Size ? Offsets.Splits[Idx] : SliceSize) - PartOffset;
5113 }
5114
5115 // Now that we have the split loads, do the slow walk over all uses of the
5116 // load and rewrite them as split stores, or save the split loads to use
5117 // below if the store is going to be split there anyways.
5118 bool DeferredStores = false;
5119 for (User *LU : LI->users()) {
5120 StoreInst *SI = cast<StoreInst>(LU);
5121 if (!Stores.empty() && SplitOffsetsMap.count(SI)) {
5122 DeferredStores = true;
5123 LLVM_DEBUG(dbgs() << " Deferred splitting of store: " << *SI
5124 << "\n");
5125 continue;
5126 }
5127
5128 Value *StoreBasePtr = SI->getPointerOperand();
5129 IRB.SetInsertPoint(SI);
5130 AAMDNodes AATags = SI->getAAMetadata();
5131
5132 LLVM_DEBUG(dbgs() << " Splitting store of load: " << *SI << "\n");
5133
5134 for (int Idx = 0, Size = SplitLoads.size(); Idx < Size; ++Idx) {
5135 LoadInst *PLoad = SplitLoads[Idx];
5136 uint64_t PartOffset = Idx == 0 ? 0 : Offsets.Splits[Idx - 1];
5137 auto *PartPtrTy = SI->getPointerOperandType();
5138
5139 auto AS = SI->getPointerAddressSpace();
5140 StoreInst *PStore = IRB.CreateAlignedStore(
5141 PLoad,
5142 getAdjustedPtr(IRB, DL, StoreBasePtr,
5143 APInt(DL.getIndexSizeInBits(AS), PartOffset),
5144 PartPtrTy, StoreBasePtr->getName() + "."),
5145 getAdjustedAlignment(SI, PartOffset),
5146 /*IsVolatile*/ false);
5147 PStore->copyMetadata(*SI, {LLVMContext::MD_mem_parallel_loop_access,
5148 LLVMContext::MD_access_group,
5149 LLVMContext::MD_DIAssignID});
5150
5151 if (AATags)
5152 PStore->setAAMetadata(
5153 AATags.adjustForAccess(PartOffset, PLoad->getType(), DL));
5154 LLVM_DEBUG(dbgs() << " +" << PartOffset << ":" << *PStore << "\n");
5155 }
5156
5157 // We want to immediately iterate on any allocas impacted by splitting
5158 // this store, and we have to track any promotable alloca (indicated by
5159 // a direct store) as needing to be resplit because it is no longer
5160 // promotable.
5161 if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(StoreBasePtr)) {
5162 ResplitPromotableAllocas.insert(OtherAI);
5163 Worklist.insert(OtherAI);
5164 } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(
5165 StoreBasePtr->stripInBoundsOffsets())) {
5166 Worklist.insert(OtherAI);
5167 }
5168
5169 // Mark the original store as dead.
5170 DeadInsts.push_back(SI);
5171 }
5172
5173 // Save the split loads if there are deferred stores among the users.
5174 if (DeferredStores)
5175 SplitLoadsMap.insert(std::make_pair(LI, std::move(SplitLoads)));
5176
5177 // Mark the original load as dead and kill the original slice.
5178 DeadInsts.push_back(LI);
5179 Offsets.S->kill();
5180 }
5181
5182 // Second, we rewrite all of the split stores. At this point, we know that
5183 // all loads from this alloca have been split already. For stores of such
5184 // loads, we can simply look up the pre-existing split loads. For stores of
5185 // other loads, we split those loads first and then write split stores of
5186 // them.
5187 for (StoreInst *SI : Stores) {
5188 auto *LI = cast<LoadInst>(SI->getValueOperand());
5189 IntegerType *Ty = cast<IntegerType>(LI->getType());
5190 assert(Ty->getBitWidth() % 8 == 0);
5191 uint64_t StoreSize = Ty->getBitWidth() / 8;
5192 assert(StoreSize > 0 && "Cannot have a zero-sized integer store!");
5193
5194 auto &Offsets = SplitOffsetsMap[SI];
5195 assert(StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() &&
5196 "Slice size should always match load size exactly!");
5197 uint64_t BaseOffset = Offsets.S->beginOffset();
5198 assert(BaseOffset + StoreSize > BaseOffset &&
5199 "Cannot represent alloca access size using 64-bit integers!");
5200
5201 Value *LoadBasePtr = LI->getPointerOperand();
5202 Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand());
5203
5204 LLVM_DEBUG(dbgs() << " Splitting store: " << *SI << "\n");
5205
5206 // Check whether we have an already split load.
5207 auto SplitLoadsMapI = SplitLoadsMap.find(LI);
5208 std::vector<LoadInst *> *SplitLoads = nullptr;
5209 if (SplitLoadsMapI != SplitLoadsMap.end()) {
5210 SplitLoads = &SplitLoadsMapI->second;
5211 assert(SplitLoads->size() == Offsets.Splits.size() + 1 &&
5212 "Too few split loads for the number of splits in the store!");
5213 } else {
5214 LLVM_DEBUG(dbgs() << " of load: " << *LI << "\n");
5215 }
5216
5217 uint64_t PartOffset = 0, PartSize = Offsets.Splits.front();
5218 int Idx = 0, Size = Offsets.Splits.size();
5219 for (;;) {
5220 auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8);
5221 auto *LoadPartPtrTy = LI->getPointerOperandType();
5222 auto *StorePartPtrTy = SI->getPointerOperandType();
5223
5224 // Either lookup a split load or create one.
5225 LoadInst *PLoad;
5226 if (SplitLoads) {
5227 PLoad = (*SplitLoads)[Idx];
5228 } else {
5229 IRB.SetInsertPoint(LI);
5230 auto AS = LI->getPointerAddressSpace();
5231 PLoad = IRB.CreateAlignedLoad(
5232 PartTy,
5233 getAdjustedPtr(IRB, DL, LoadBasePtr,
5234 APInt(DL.getIndexSizeInBits(AS), PartOffset),
5235 LoadPartPtrTy, LoadBasePtr->getName() + "."),
5236 getAdjustedAlignment(LI, PartOffset),
5237 /*IsVolatile*/ false, LI->getName());
5238 PLoad->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access,
5239 LLVMContext::MD_access_group});
5240 }
5241
5242 // And store this partition.
5243 IRB.SetInsertPoint(SI);
5244 auto AS = SI->getPointerAddressSpace();
5245 StoreInst *PStore = IRB.CreateAlignedStore(
5246 PLoad,
5247 getAdjustedPtr(IRB, DL, StoreBasePtr,
5248 APInt(DL.getIndexSizeInBits(AS), PartOffset),
5249 StorePartPtrTy, StoreBasePtr->getName() + "."),
5250 getAdjustedAlignment(SI, PartOffset),
5251 /*IsVolatile*/ false);
5252 PStore->copyMetadata(*SI, {LLVMContext::MD_mem_parallel_loop_access,
5253 LLVMContext::MD_access_group});
5254
5255 // Now build a new slice for the alloca.
5256 NewSlices.push_back(
5257 Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize,
5258 &PStore->getOperandUse(PStore->getPointerOperandIndex()),
5259 /*IsSplittable*/ false));
5260 LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset()
5261 << ", " << NewSlices.back().endOffset()
5262 << "): " << *PStore << "\n");
5263 if (!SplitLoads) {
5264 LLVM_DEBUG(dbgs() << " of split load: " << *PLoad << "\n");
5265 }
5266
5267 // See if we've finished all the splits.
5268 if (Idx >= Size)
5269 break;
5270
5271 // Setup the next partition.
5272 PartOffset = Offsets.Splits[Idx];
5273 ++Idx;
5274 PartSize = (Idx < Size ? Offsets.Splits[Idx] : StoreSize) - PartOffset;
5275 }
5276
5277 // We want to immediately iterate on any allocas impacted by splitting
5278 // this load, which is only relevant if it isn't a load of this alloca and
5279 // thus we didn't already split the loads above. We also have to keep track
5280 // of any promotable allocas we split loads on as they can no longer be
5281 // promoted.
5282 if (!SplitLoads) {
5283 if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(LoadBasePtr)) {
5284 assert(OtherAI != &AI && "We can't re-split our own alloca!");
5285 ResplitPromotableAllocas.insert(OtherAI);
5286 Worklist.insert(OtherAI);
5287 } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(
5288 LoadBasePtr->stripInBoundsOffsets())) {
5289 assert(OtherAI != &AI && "We can't re-split our own alloca!");
5290 Worklist.insert(OtherAI);
5291 }
5292 }
5293
5294 // Mark the original store as dead now that we've split it up and kill its
5295 // slice. Note that we leave the original load in place unless this store
5296 // was its only use. It may in turn be split up if it is an alloca load
5297 // for some other alloca, but it may be a normal load. This may introduce
5298 // redundant loads, but where those can be merged the rest of the optimizer
5299 // should handle the merging, and this uncovers SSA splits which is more
5300 // important. In practice, the original loads will almost always be fully
5301 // split and removed eventually, and the splits will be merged by any
5302 // trivial CSE, including instcombine.
5303 if (LI->hasOneUse()) {
5304 assert(*LI->user_begin() == SI && "Single use isn't this store!");
5305 DeadInsts.push_back(LI);
5306 }
5307 DeadInsts.push_back(SI);
5308 Offsets.S->kill();
5309 }
5310
5311 // Remove the killed slices that have ben pre-split.
5312 llvm::erase_if(AS, [](const Slice &S) { return S.isDead(); });
5313
5314 // Insert our new slices. This will sort and merge them into the sorted
5315 // sequence.
5316 AS.insert(NewSlices);
5317
5318 LLVM_DEBUG(dbgs() << " Pre-split slices:\n");
5319#ifndef NDEBUG
5320 for (auto I = AS.begin(), E = AS.end(); I != E; ++I)
5321 LLVM_DEBUG(AS.print(dbgs(), I, " "));
5322#endif
5323
5324 // Finally, don't try to promote any allocas that new require re-splitting.
5325 // They have already been added to the worklist above.
5326 PromotableAllocas.set_subtract(ResplitPromotableAllocas);
5327
5328 return true;
5329}
5330
5331/// Try to canonicalize a homogeneous struct partition to a vector type.
5332///
5333/// We can do this if all the elements of the struct are the same and the
5334/// corresponding vector has the same byte-level layout. This can sometimes
5335/// eliminate allocas because structs cannot get promoted to LLVM values, but
5336/// vectors can.
5337///
5338/// We only apply this transformation when all users of the partition are memory
5339/// intrinsics. Otherwise, if there is a load or store of some other type to the
5340/// partition, SROA would select that type.
5341///
5342/// Applying this transformation too early may hinder memcpyopt, which may
5343/// generate better code when eliminating allocas. For example, see
5344/// `struct-to-vector-fp-store-only-tail.ll`, which demonstrates that applying
5345/// this before memcpyopt can initialize previously uninitialized memory when
5346/// the alloca gets promoted to an SSA value. For another example, see
5347/// `struct-to-vector-before-memcpyopt.ll`, which demonstrates that applying
5348/// this before memcpyopt can result in promoting an alloca so that we load a
5349/// temporary value instead of copying the temporary value into memory, whereas
5350/// memcpyopt eliminates the temporary altogether.
5351///
5352/// As such, we only apply this transformation after memcpyopt has run. We gate
5353/// this transformation by the "AggregateToVector" pass option.
5355 Partition &P,
5356 const DataLayout &DL) {
5357 unsigned NumElts = STy->getNumElements();
5358
5359 Type *EltTy = STy->getElementType(0);
5360 if (!llvm::all_equal(STy->elements()))
5361 return nullptr;
5362
5363 bool IsIntegralPointerTy =
5364 EltTy->isPointerTy() && !DL.isNonIntegralPointerType(EltTy);
5365 if (!EltTy->isIntegerTy() && !EltTy->isFloatingPointTy() &&
5366 !IsIntegralPointerTy)
5367 return nullptr;
5368
5369 // Ensure the struct is tightly packed so that the bit-layout is the same as
5370 // the corresponding vector. For example, this prevents a miscompile for
5371 // { i5, i5 }, which has padding after each i5 field, whereas <i5, i5> has
5372 // tightly packed elements and trailing padding.
5373 if (DL.getTypeSizeInBits(EltTy) != DL.getTypeAllocSizeInBits(EltTy))
5374 return nullptr;
5375
5376 auto *VTy = FixedVectorType::get(EltTy, NumElts);
5377 TypeSize StructSize = DL.getStructLayout(STy)->getSizeInBytes();
5378 TypeSize VectorSize = DL.getTypeStoreSize(VTy);
5379 // After ruling out per-element padding, make sure a vector load/store
5380 // covers the same number of bytes as the struct layout.
5381 if (StructSize != VectorSize)
5382 return nullptr;
5383
5384 auto IsIgnorableOrMemIntrinsicSlice = [](const Slice &S) {
5385 if (S.isDead())
5386 return true;
5387 auto *U = S.getUse();
5388 if (!U)
5389 return true;
5390
5391 User *Usr = U->getUser();
5393 return true;
5394
5395 return isa<MemIntrinsic>(Usr);
5396 };
5397
5398 for (const Slice &S : P)
5399 if (!IsIgnorableOrMemIntrinsicSlice(S))
5400 return nullptr;
5401
5402 for (const Slice *S : P.splitSliceTails())
5403 if (!IsIgnorableOrMemIntrinsicSlice(*S))
5404 return nullptr;
5405
5406 return VTy;
5407}
5408
5409/// Select a partition type for an alloca partition.
5410///
5411/// Try to compute a friendly type for this partition of the alloca. This
5412/// won't always succeed, in which case we fall back to a legal integer type
5413/// or an i8 array of an appropriate size.
5414///
5415/// \returns A tuple with the following elements:
5416/// - PartitionType: The computed type for this partition.
5417/// - IsIntegerWideningViable: True if integer widening promotion is used.
5418/// - VectorType: The vector type if vector promotion is used, otherwise
5419/// nullptr.
5420static std::tuple<Type *, bool, VectorType *>
5422 LLVMContext &C, bool AggregateToVector) {
5423 auto LogSelection = [&](StringRef Path, Type *SelectedTy,
5424 VectorType *SelectedVecTy, bool SelectedIntWidening) {
5425 LLVM_DEBUG({
5426 dbgs() << "selectPartitionType path=" << Path
5427 << " func=" << AI.getFunction()->getName() << " alloca=";
5428 if (AI.hasName())
5429 dbgs() << AI.getName();
5430 else
5431 dbgs() << "<unnamed>";
5432 dbgs() << " partition=[" << P.beginOffset() << "," << P.endOffset()
5433 << ") size=" << P.size();
5434 if (std::optional<TypeSize> AllocSize = AI.getAllocationSize(DL))
5435 dbgs() << " alloc-size=" << AllocSize->getKnownMinValue();
5436 if (SelectedTy)
5437 dbgs() << " chosen=" << *SelectedTy;
5438 if (SelectedVecTy)
5439 dbgs() << " vec=" << *SelectedVecTy;
5440 dbgs() << " intwiden=" << SelectedIntWidening << "\n";
5441 });
5442 };
5443 // First check if the partition is viable for vector promotion.
5444 //
5445 // We prefer vector promotion over integer widening promotion when:
5446 // - The vector element type is a floating-point type.
5447 // - All the loads/stores to the alloca are vector loads/stores to the
5448 // entire alloca or load/store a single element of the vector.
5449 //
5450 // Otherwise when there is an integer vector with mixed type loads/stores we
5451 // prefer integer widening promotion because it's more likely the user is
5452 // doing bitwise arithmetic and we generate better code.
5453 VectorType *VecTy =
5455 // If the vector element type is a floating-point type, we prefer vector
5456 // promotion. If the vector has one element, let the below code select
5457 // whether we promote with the vector or scalar.
5458 if (VecTy && VecTy->getElementType()->isFloatingPointTy() &&
5459 VecTy->getElementCount().getFixedValue() > 1) {
5460 LogSelection("direct-fp-vecty", VecTy, VecTy, false);
5461 return {VecTy, false, VecTy};
5462 }
5463
5464 // Check if there is a common type that all slices of the partition use that
5465 // spans the partition.
5466 auto [CommonUseTy, LargestIntTy] =
5467 findCommonType(P.begin(), P.end(), P.endOffset());
5468 if (CommonUseTy) {
5469 TypeSize CommonUseSize = DL.getTypeAllocSize(CommonUseTy);
5470 if (CommonUseSize.isFixed() && CommonUseSize.getFixedValue() >= P.size()) {
5471 // We prefer vector promotion here because if vector promotion is viable
5472 // and there is a common type used, then it implies the second listed
5473 // condition for preferring vector promotion is true.
5474 if (VecTy) {
5475 LogSelection("common-type-vecty", VecTy, VecTy, false);
5476 return {VecTy, false, VecTy};
5477 }
5478 bool IntWiden = isIntegerWideningViable(P, CommonUseTy, DL);
5479 LogSelection("common-type", CommonUseTy, nullptr, IntWiden);
5480 return {CommonUseTy, IntWiden, nullptr};
5481 }
5482 }
5483
5484 // Can we find an appropriate subtype in the original allocated
5485 // type?
5486 if (Type *TypePartitionTy = getTypePartition(DL, AI.getAllocatedType(),
5487 P.beginOffset(), P.size())) {
5488 // If the partition is an integer array that can be spanned by a legal
5489 // integer type, prefer to represent it as a legal integer type because
5490 // it's more likely to be promotable.
5491 if (TypePartitionTy->isArrayTy() &&
5492 TypePartitionTy->getArrayElementType()->isIntegerTy() &&
5493 DL.isLegalInteger(P.size() * 8))
5494 TypePartitionTy = Type::getIntNTy(C, P.size() * 8);
5495 // There was no common type used, so we prefer integer widening promotion.
5496 if (isIntegerWideningViable(P, TypePartitionTy, DL)) {
5497 LogSelection("type-partition-int-widen", TypePartitionTy, nullptr, true);
5498 return {TypePartitionTy, true, nullptr};
5499 }
5500 if (VecTy) {
5501 LogSelection("type-partition-vecty", VecTy, VecTy, false);
5502 return {VecTy, false, VecTy};
5503 }
5504 // If we couldn't promote with TypePartitionTy, try with the largest
5505 // integer type used.
5506 if (LargestIntTy &&
5507 DL.getTypeAllocSize(LargestIntTy).getFixedValue() >= P.size() &&
5508 isIntegerWideningViable(P, LargestIntTy, DL)) {
5509 LogSelection("largest-int-int-widen", LargestIntTy, nullptr, true);
5510 return {LargestIntTy, true, nullptr};
5511 }
5512
5513 // Try homogeneous struct to vector canonicalization when requested. Running
5514 // this too early can hide memcpy chains from MemCpyOpt.
5515 if (AggregateToVector) {
5516 if (auto *STy = dyn_cast<StructType>(TypePartitionTy)) {
5517 if (auto *VTy = tryCanonicalizeStructToVector(STy, P, DL)) {
5518 LogSelection("struct-fallback-vecty", VTy, nullptr, false);
5519 return {VTy, false, nullptr};
5520 }
5521 }
5522 }
5523
5524 // Fallback to TypePartitionTy and we probably won't promote.
5525 LogSelection("type-partition-fallback", TypePartitionTy, nullptr, false);
5526 return {TypePartitionTy, false, nullptr};
5527 }
5528
5529 // Select the largest integer type used if it spans the partition.
5530 if (LargestIntTy &&
5531 DL.getTypeAllocSize(LargestIntTy).getFixedValue() >= P.size()) {
5532 LogSelection("largest-int-fallback", LargestIntTy, nullptr, false);
5533 return {LargestIntTy, false, nullptr};
5534 }
5535
5536 // Select a legal integer type if it spans the partition.
5537 if (DL.isLegalInteger(P.size() * 8)) {
5538 Type *IntTy = Type::getIntNTy(C, P.size() * 8);
5539 LogSelection("legal-int-fallback", IntTy, nullptr, false);
5540 return {IntTy, false, nullptr};
5541 }
5542
5543 // Fallback to an i8 array.
5544 Type *ArrayTy = ArrayType::get(Type::getInt8Ty(C), P.size());
5545 LogSelection("byte-array-fallback", ArrayTy, nullptr, false);
5546 return {ArrayTy, false, nullptr};
5547}
5548
5549/// Rewrite an alloca partition's users.
5550///
5551/// This routine drives both of the rewriting goals of the SROA pass. It tries
5552/// to rewrite uses of an alloca partition to be conducive for SSA value
5553/// promotion. If the partition needs a new, more refined alloca, this will
5554/// build that new alloca, preserving as much type information as possible, and
5555/// rewrite the uses of the old alloca to point at the new one and have the
5556/// appropriate new offsets. It also evaluates how successful the rewrite was
5557/// at enabling promotion and if it was successful queues the alloca to be
5558/// promoted.
5559std::pair<AllocaInst *, uint64_t>
5560SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS, Partition &P) {
5561 const DataLayout &DL = AI.getDataLayout();
5562 // Select the type for the new alloca that spans the partition.
5563 auto [PartitionTy, IsIntegerWideningViable, VecTy] =
5564 selectPartitionType(P, DL, AI, *C, AggregateToVector);
5565
5566 // Check for the case where we're going to rewrite to a new alloca of the
5567 // exact same type as the original, and with the same access offsets. In that
5568 // case, re-use the existing alloca, but still run through the rewriter to
5569 // perform phi and select speculation.
5570 // P.beginOffset() can be non-zero even with the same type in a case with
5571 // out-of-bounds access (e.g. @PR35657 function in SROA/basictest.ll).
5572 AllocaInst *NewAI;
5573 if (PartitionTy == AI.getAllocatedType() && P.beginOffset() == 0) {
5574 NewAI = &AI;
5575 // FIXME: We should be able to bail at this point with "nothing changed".
5576 // FIXME: We might want to defer PHI speculation until after here.
5577 // FIXME: return nullptr;
5578 } else {
5579 // Make sure the alignment is compatible with P.beginOffset().
5580 const Align Alignment = commonAlignment(AI.getAlign(), P.beginOffset());
5581 // If we will get at least this much alignment from the type alone, leave
5582 // the alloca's alignment unconstrained.
5583 const bool IsUnconstrained = Alignment <= DL.getABITypeAlign(PartitionTy);
5584 NewAI = new AllocaInst(
5585 PartitionTy, AI.getAddressSpace(), nullptr,
5586 IsUnconstrained ? DL.getPrefTypeAlign(PartitionTy) : Alignment,
5587 AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()),
5588 AI.getIterator());
5589 // Copy the old AI debug location over to the new one.
5590 NewAI->setDebugLoc(AI.getDebugLoc());
5591 ++NumNewAllocas;
5592 }
5593
5594 LLVM_DEBUG(dbgs() << "Rewriting alloca partition " << "[" << P.beginOffset()
5595 << "," << P.endOffset() << ") to: " << *NewAI << "\n");
5596
5597 // Track the high watermark on the worklist as it is only relevant for
5598 // promoted allocas. We will reset it to this point if the alloca is not in
5599 // fact scheduled for promotion.
5600 unsigned PPWOldSize = PostPromotionWorklist.size();
5601 unsigned NumUses = 0;
5602 SmallSetVector<PHINode *, 8> PHIUsers;
5603 SmallSetVector<SelectInst *, 8> SelectUsers;
5604
5605 AllocaSliceRewriter Rewriter(
5606 DL, AS, *this, AI, *NewAI, PartitionTy, P.beginOffset(), P.endOffset(),
5607 IsIntegerWideningViable, VecTy, PHIUsers, SelectUsers);
5608 bool Promotable = true;
5609 // Check whether we can have tree-structured merge.
5610 if (auto DeletedValues = Rewriter.rewriteTreeStructuredMerge(P)) {
5611 NumUses += DeletedValues->size() + 1;
5612 for (Value *V : *DeletedValues)
5613 DeadInsts.push_back(V);
5614 } else {
5615 for (Slice *S : P.splitSliceTails()) {
5616 Promotable &= Rewriter.visit(S);
5617 ++NumUses;
5618 }
5619 for (Slice &S : P) {
5620 Promotable &= Rewriter.visit(&S);
5621 ++NumUses;
5622 }
5623 }
5624
5625 NumAllocaPartitionUses += NumUses;
5626 MaxUsesPerAllocaPartition.updateMax(NumUses);
5627
5628 // Now that we've processed all the slices in the new partition, check if any
5629 // PHIs or Selects would block promotion.
5630 for (PHINode *PHI : PHIUsers)
5631 if (!isSafePHIToSpeculate(*PHI)) {
5632 Promotable = false;
5633 PHIUsers.clear();
5634 SelectUsers.clear();
5635 break;
5636 }
5637
5639 NewSelectsToRewrite;
5640 NewSelectsToRewrite.reserve(SelectUsers.size());
5641 for (SelectInst *Sel : SelectUsers) {
5642 std::optional<RewriteableMemOps> Ops =
5643 isSafeSelectToSpeculate(*Sel, PreserveCFG);
5644 if (!Ops) {
5645 Promotable = false;
5646 PHIUsers.clear();
5647 SelectUsers.clear();
5648 NewSelectsToRewrite.clear();
5649 break;
5650 }
5651 NewSelectsToRewrite.emplace_back(std::make_pair(Sel, *Ops));
5652 }
5653
5654 if (Promotable) {
5655 for (Use *U : AS.getDeadUsesIfPromotable()) {
5656 auto *OldInst = dyn_cast<Instruction>(U->get());
5657 Value::dropDroppableUse(*U);
5658 if (OldInst)
5659 if (isInstructionTriviallyDead(OldInst))
5660 DeadInsts.push_back(OldInst);
5661 }
5662 if (PHIUsers.empty() && SelectUsers.empty()) {
5663 // Promote the alloca.
5664 PromotableAllocas.insert(NewAI);
5665 } else {
5666 // If we have either PHIs or Selects to speculate, add them to those
5667 // worklists and re-queue the new alloca so that we promote in on the
5668 // next iteration.
5669 SpeculatablePHIs.insert_range(PHIUsers);
5670 SelectsToRewrite.reserve(SelectsToRewrite.size() +
5671 NewSelectsToRewrite.size());
5672 for (auto &&KV : llvm::make_range(
5673 std::make_move_iterator(NewSelectsToRewrite.begin()),
5674 std::make_move_iterator(NewSelectsToRewrite.end())))
5675 SelectsToRewrite.insert(std::move(KV));
5676 Worklist.insert(NewAI);
5677 }
5678 } else {
5679 // Drop any post-promotion work items if promotion didn't happen.
5680 while (PostPromotionWorklist.size() > PPWOldSize)
5681 PostPromotionWorklist.pop_back();
5682
5683 // We couldn't promote and we didn't create a new partition, nothing
5684 // happened.
5685 if (NewAI == &AI)
5686 return {nullptr, 0};
5687
5688 // If we can't promote the alloca, iterate on it to check for new
5689 // refinements exposed by splitting the current alloca. Don't iterate on an
5690 // alloca which didn't actually change and didn't get promoted.
5691 Worklist.insert(NewAI);
5692 }
5693
5694 return {NewAI, DL.getTypeSizeInBits(PartitionTy).getFixedValue()};
5695}
5696
5697// There isn't a shared interface to get the "address" parts out of a
5698// dbg.declare and dbg.assign, so provide some wrappers.
5701 return DVR->isKillAddress();
5702 return DVR->isKillLocation();
5703}
5704
5707 return DVR->getAddressExpression();
5708 return DVR->getExpression();
5709}
5710
5711/// Create or replace an existing fragment in a DIExpression with \p Frag.
5712/// If the expression already contains a DW_OP_LLVM_extract_bits_[sz]ext
5713/// operation, add \p BitExtractOffset to the offset part.
5714///
5715/// Returns the new expression, or nullptr if this fails (see details below).
5716///
5717/// This function is similar to DIExpression::createFragmentExpression except
5718/// for 3 important distinctions:
5719/// 1. The new fragment isn't relative to an existing fragment.
5720/// 2. It assumes the computed location is a memory location. This means we
5721/// don't need to perform checks that creating the fragment preserves the
5722/// expression semantics.
5723/// 3. Existing extract_bits are modified independently of fragment changes
5724/// using \p BitExtractOffset. A change to the fragment offset or size
5725/// may affect a bit extract. But a bit extract offset can change
5726/// independently of the fragment dimensions.
5727///
5728/// Returns the new expression, or nullptr if one couldn't be created.
5729/// Ideally this is only used to signal that a bit-extract has become
5730/// zero-sized (and thus the new debug record has no size and can be
5731/// dropped), however, it fails for other reasons too - see the FIXME below.
5732///
5733/// FIXME: To keep the change that introduces this function NFC it bails
5734/// in some situations unecessarily, e.g. when fragment and bit extract
5735/// sizes differ.
5738 int64_t BitExtractOffset) {
5740 bool HasFragment = false;
5741 bool HasBitExtract = false;
5742
5743 for (auto &Op : Expr->expr_ops()) {
5744 if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
5745 HasFragment = true;
5746 continue;
5747 }
5748 if (auto Extract = dyn_cast<DIExpression::ExtractBitsOp>(Op)) {
5749 HasBitExtract = true;
5750 int64_t ExtractOffsetInBits = Extract.getOffsetInBits();
5751 int64_t ExtractSizeInBits = Extract.getSizeInBits();
5752
5753 // DIExpression::createFragmentExpression doesn't know how to handle
5754 // a fragment that is smaller than the extract. Copy the behaviour
5755 // (bail) to avoid non-NFC changes.
5756 // FIXME: Don't do this.
5757 if (Frag.SizeInBits < uint64_t(ExtractSizeInBits))
5758 return nullptr;
5759
5760 assert(BitExtractOffset <= 0);
5761 int64_t AdjustedOffset = ExtractOffsetInBits + BitExtractOffset;
5762
5763 // DIExpression::createFragmentExpression doesn't know what to do
5764 // if the new extract starts "outside" the existing one. Copy the
5765 // behaviour (bail) to avoid non-NFC changes.
5766 // FIXME: Don't do this.
5767 if (AdjustedOffset < 0)
5768 return nullptr;
5769
5770 Ops.push_back(Op.getOp());
5771 Ops.push_back(std::max<int64_t>(0, AdjustedOffset));
5772 Ops.push_back(ExtractSizeInBits);
5773 continue;
5774 }
5775 Op.appendToVector(Ops);
5776 }
5777
5778 // Unsupported by createFragmentExpression, so don't support it here yet to
5779 // preserve NFC-ness.
5780 if (HasFragment && HasBitExtract)
5781 return nullptr;
5782
5783 if (!HasBitExtract) {
5785 Ops.push_back(Frag.OffsetInBits);
5786 Ops.push_back(Frag.SizeInBits);
5787 }
5788 return DIExpression::get(Expr->getContext(), Ops);
5789}
5790
5791/// Insert a new DbgRecord.
5792/// \p Orig Original to copy record type, debug loc and variable from, and
5793/// additionally value and value expression for dbg_assign records.
5794/// \p NewAddr Location's new base address.
5795/// \p NewAddrExpr New expression to apply to address.
5796/// \p BeforeInst Insert position.
5797/// \p NewFragment New fragment (absolute, non-relative).
5798/// \p BitExtractAdjustment Offset to apply to any extract_bits op.
5799static void
5801 DIExpression *NewAddrExpr, Instruction *BeforeInst,
5802 std::optional<DIExpression::FragmentInfo> NewFragment,
5803 int64_t BitExtractAdjustment) {
5804 (void)DIB;
5805
5806 // A dbg_assign puts fragment info in the value expression only. The address
5807 // expression has already been built: NewAddrExpr. A dbg_declare puts the
5808 // new fragment info into NewAddrExpr (as it only has one expression).
5809 DIExpression *NewFragmentExpr =
5810 Orig->isDbgAssign() ? Orig->getExpression() : NewAddrExpr;
5811 if (NewFragment)
5812 NewFragmentExpr = createOrReplaceFragment(NewFragmentExpr, *NewFragment,
5813 BitExtractAdjustment);
5814 if (!NewFragmentExpr)
5815 return;
5816
5817 if (Orig->isDbgDeclare()) {
5819 NewAddr, Orig->getVariable(), NewFragmentExpr, Orig->getDebugLoc());
5820 BeforeInst->getParent()->insertDbgRecordBefore(DVR,
5821 BeforeInst->getIterator());
5822 return;
5823 }
5824
5825 if (Orig->isDbgValue()) {
5827 NewAddr, Orig->getVariable(), NewFragmentExpr, Orig->getDebugLoc());
5828 // Drop debug information if the expression doesn't start with a
5829 // DW_OP_deref. This is because without a DW_OP_deref, the #dbg_value
5830 // describes the address of alloca rather than the value inside the alloca.
5831 if (!NewFragmentExpr->startsWithDeref())
5832 DVR->setKillAddress();
5833 BeforeInst->getParent()->insertDbgRecordBefore(DVR,
5834 BeforeInst->getIterator());
5835 return;
5836 }
5837
5838 // Apply a DIAssignID to the store if it doesn't already have it.
5839 if (!NewAddr->hasMetadata(LLVMContext::MD_DIAssignID)) {
5840 NewAddr->setMetadata(LLVMContext::MD_DIAssignID,
5842 }
5843
5845 NewAddr, Orig->getValue(), Orig->getVariable(), NewFragmentExpr, NewAddr,
5846 NewAddrExpr, Orig->getDebugLoc());
5847 LLVM_DEBUG(dbgs() << "Created new DVRAssign: " << *NewAssign << "\n");
5848 (void)NewAssign;
5849}
5850
5851/// Walks the slices of an alloca and form partitions based on them,
5852/// rewriting each of their uses.
5853bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
5854 if (AS.begin() == AS.end())
5855 return false;
5856
5857 unsigned NumPartitions = 0;
5858 bool Changed = false;
5859 const DataLayout &DL = AI.getModule()->getDataLayout();
5860
5861 // First try to pre-split loads and stores.
5862 Changed |= presplitLoadsAndStores(AI, AS);
5863
5864 // Now that we have identified any pre-splitting opportunities,
5865 // mark loads and stores unsplittable except for the following case.
5866 // We leave a slice splittable if all other slices are disjoint or fully
5867 // included in the slice, such as whole-alloca loads and stores.
5868 // If we fail to split these during pre-splitting, we want to force them
5869 // to be rewritten into a partition.
5870 bool IsSorted = true;
5871
5872 uint64_t AllocaSize = AI.getAllocationSize(DL)->getFixedValue();
5873 const uint64_t MaxBitVectorSize = 1024;
5874 if (AllocaSize <= MaxBitVectorSize) {
5875 // If a byte boundary is included in any load or store, a slice starting or
5876 // ending at the boundary is not splittable.
5877 SmallBitVector SplittableOffset(AllocaSize + 1, true);
5878 for (Slice &S : AS)
5879 for (unsigned O = S.beginOffset() + 1;
5880 O < S.endOffset() && O < AllocaSize; O++)
5881 SplittableOffset.reset(O);
5882
5883 for (Slice &S : AS) {
5884 if (!S.isSplittable())
5885 continue;
5886
5887 if ((S.beginOffset() > AllocaSize || SplittableOffset[S.beginOffset()]) &&
5888 (S.endOffset() > AllocaSize || SplittableOffset[S.endOffset()]))
5889 continue;
5890
5891 if (isa<LoadInst>(S.getUse()->getUser()) ||
5892 isa<StoreInst>(S.getUse()->getUser())) {
5893 S.makeUnsplittable();
5894 IsSorted = false;
5895 }
5896 }
5897 } else {
5898 // We only allow whole-alloca splittable loads and stores
5899 // for a large alloca to avoid creating too large BitVector.
5900 for (Slice &S : AS) {
5901 if (!S.isSplittable())
5902 continue;
5903
5904 if (S.beginOffset() == 0 && S.endOffset() >= AllocaSize)
5905 continue;
5906
5907 if (isa<LoadInst>(S.getUse()->getUser()) ||
5908 isa<StoreInst>(S.getUse()->getUser())) {
5909 S.makeUnsplittable();
5910 IsSorted = false;
5911 }
5912 }
5913 }
5914
5915 if (!IsSorted)
5917
5918 /// Describes the allocas introduced by rewritePartition in order to migrate
5919 /// the debug info.
5920 struct Fragment {
5921 AllocaInst *Alloca;
5923 uint64_t Size;
5924 Fragment(AllocaInst *AI, uint64_t O, uint64_t S)
5925 : Alloca(AI), Offset(O), Size(S) {}
5926 };
5927 SmallVector<Fragment, 4> Fragments;
5928
5929 // Rewrite each partition.
5930 for (auto &P : AS.partitions()) {
5931 auto [NewAI, ActiveBits] = rewritePartition(AI, AS, P);
5932 if (NewAI) {
5933 Changed = true;
5934 if (NewAI != &AI) {
5935 uint64_t SizeOfByte = 8;
5936 // Don't include any padding.
5937 uint64_t Size = std::min(ActiveBits, P.size() * SizeOfByte);
5938 Fragments.push_back(
5939 Fragment(NewAI, P.beginOffset() * SizeOfByte, Size));
5940 }
5941 }
5942 ++NumPartitions;
5943 }
5944
5945 NumAllocaPartitions += NumPartitions;
5946 MaxPartitionsPerAlloca.updateMax(NumPartitions);
5947
5948 // Migrate debug information from the old alloca to the new alloca(s)
5949 // and the individual partitions.
5950 auto MigrateOne = [&](DbgVariableRecord *DbgVariable) {
5951 // Can't overlap with undef memory.
5952 if (isKillAddress(DbgVariable))
5953 return;
5954
5955 const Value *DbgPtr = DbgVariable->getAddress();
5957 DbgVariable->getFragmentOrEntireVariable();
5958 // Get the address expression constant offset if one exists and the ops
5959 // that come after it.
5960 int64_t CurrentExprOffsetInBytes = 0;
5961 SmallVector<uint64_t> PostOffsetOps;
5962 if (!getAddressExpression(DbgVariable)
5963 ->extractLeadingOffset(CurrentExprOffsetInBytes, PostOffsetOps))
5964 return; // Couldn't interpret this DIExpression - drop the var.
5965
5966 // Offset defined by a DW_OP_LLVM_extract_bits_[sz]ext.
5967 int64_t ExtractOffsetInBits = 0;
5968 for (auto Op : getAddressExpression(DbgVariable)->expr_ops()) {
5969 if (auto Extract = dyn_cast<DIExpression::ExtractBitsOp>(Op)) {
5970 ExtractOffsetInBits = Extract.getOffsetInBits();
5971 break;
5972 }
5973 }
5974
5975 DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
5976 for (auto Fragment : Fragments) {
5977 int64_t OffsetFromLocationInBits;
5978 std::optional<DIExpression::FragmentInfo> NewDbgFragment;
5979 // Find the variable fragment that the new alloca slice covers.
5980 // Drop debug info for this variable fragment if we can't compute an
5981 // intersect between it and the alloca slice.
5983 DL, &AI, Fragment.Offset, Fragment.Size, DbgPtr,
5984 CurrentExprOffsetInBytes * 8, ExtractOffsetInBits, VarFrag,
5985 NewDbgFragment, OffsetFromLocationInBits))
5986 continue; // Do not migrate this fragment to this slice.
5987
5988 // Zero sized fragment indicates there's no intersect between the variable
5989 // fragment and the alloca slice. Skip this slice for this variable
5990 // fragment.
5991 if (NewDbgFragment && !NewDbgFragment->SizeInBits)
5992 continue; // Do not migrate this fragment to this slice.
5993
5994 // No fragment indicates DbgVariable's variable or fragment exactly
5995 // overlaps the slice; copy its fragment (or nullopt if there isn't one).
5996 if (!NewDbgFragment)
5997 NewDbgFragment = DbgVariable->getFragment();
5998
5999 // Reduce the new expression offset by the bit-extract offset since
6000 // we'll be keeping that.
6001 int64_t OffestFromNewAllocaInBits =
6002 OffsetFromLocationInBits - ExtractOffsetInBits;
6003 // We need to adjust an existing bit extract if the offset expression
6004 // can't eat the slack (i.e., if the new offset would be negative).
6005 int64_t BitExtractOffset =
6006 std::min<int64_t>(0, OffestFromNewAllocaInBits);
6007 // The magnitude of a negative value indicates the number of bits into
6008 // the existing variable fragment that the memory region begins. The new
6009 // variable fragment already excludes those bits - the new DbgPtr offset
6010 // only needs to be applied if it's positive.
6011 OffestFromNewAllocaInBits =
6012 std::max(int64_t(0), OffestFromNewAllocaInBits);
6013
6014 // Rebuild the expression:
6015 // {Offset(OffestFromNewAllocaInBits), PostOffsetOps, NewDbgFragment}
6016 // Add NewDbgFragment later, because dbg.assigns don't want it in the
6017 // address expression but the value expression instead.
6018 DIExpression *NewExpr = DIExpression::get(AI.getContext(), PostOffsetOps);
6019 if (OffestFromNewAllocaInBits > 0) {
6020 int64_t OffsetInBytes = (OffestFromNewAllocaInBits + 7) / 8;
6021 NewExpr = DIExpression::prepend(NewExpr, /*flags=*/0, OffsetInBytes);
6022 }
6023
6024 // Remove any existing intrinsics on the new alloca describing
6025 // the variable fragment.
6026 auto RemoveOne = [DbgVariable](auto *OldDII) {
6027 auto SameVariableFragment = [](const auto *LHS, const auto *RHS) {
6028 return LHS->getVariable() == RHS->getVariable() &&
6029 LHS->getDebugLoc()->getInlinedAt() ==
6030 RHS->getDebugLoc()->getInlinedAt();
6031 };
6032 if (SameVariableFragment(OldDII, DbgVariable))
6033 OldDII->eraseFromParent();
6034 };
6035 for_each(findDVRDeclares(Fragment.Alloca), RemoveOne);
6036 for_each(findDVRValues(Fragment.Alloca), RemoveOne);
6037 insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, NewExpr, &AI,
6038 NewDbgFragment, BitExtractOffset);
6039 }
6040 };
6041
6042 // Migrate debug information from the old alloca to the new alloca(s)
6043 // and the individual partitions.
6044 for_each(findDVRDeclares(&AI), MigrateOne);
6045 for_each(findDVRValues(&AI), MigrateOne);
6046 for_each(at::getDVRAssignmentMarkers(&AI), MigrateOne);
6047
6048 return Changed;
6049}
6050
6051/// Clobber a use with poison, deleting the used value if it becomes dead.
6052void SROA::clobberUse(Use &U) {
6053 Value *OldV = U;
6054 // Replace the use with an poison value.
6055 U = PoisonValue::get(OldV->getType());
6056
6057 // Check for this making an instruction dead. We have to garbage collect
6058 // all the dead instructions to ensure the uses of any alloca end up being
6059 // minimal.
6060 if (Instruction *OldI = dyn_cast<Instruction>(OldV))
6061 if (isInstructionTriviallyDead(OldI)) {
6062 DeadInsts.push_back(OldI);
6063 }
6064}
6065
6066/// A basic LoadAndStorePromoter that does not remove store nodes.
6068public:
6070 Type *ZeroType)
6071 : LoadAndStorePromoter(Insts, S), ZeroType(ZeroType) {}
6072 bool shouldDelete(Instruction *I) const override {
6073 return !isa<StoreInst>(I) && !isa<AllocaInst>(I);
6074 }
6075
6077 return UndefValue::get(ZeroType);
6078 }
6079
6080private:
6081 Type *ZeroType;
6082};
6083
6084bool SROA::propagateStoredValuesToLoads(AllocaInst &AI, AllocaSlices &AS) {
6085 // Look through each "partition", looking for slices with the same start/end
6086 // that do not overlap with any before them. The slices are sorted by
6087 // increasing beginOffset. We don't use AS.partitions(), as it will use a more
6088 // sophisticated algorithm that takes splittable slices into account.
6089 LLVM_DEBUG(dbgs() << "Attempting to propagate values on " << AI << "\n");
6090 bool AllSameAndValid = true;
6091 Type *PartitionType = nullptr;
6092 SmallVector<Instruction *> Insts;
6093 uint64_t BeginOffset = 0;
6094 uint64_t EndOffset = 0;
6095
6096 auto Flush = [&]() {
6097 if (AllSameAndValid && !Insts.empty()) {
6098 LLVM_DEBUG(dbgs() << "Propagate values on slice [" << BeginOffset << ", "
6099 << EndOffset << ")\n");
6101 SSAUpdater SSA(&NewPHIs);
6102 Insts.push_back(&AI);
6103 BasicLoadAndStorePromoter Promoter(Insts, SSA, PartitionType);
6104 Promoter.run(Insts);
6105 }
6106 AllSameAndValid = true;
6107 PartitionType = nullptr;
6108 Insts.clear();
6109 };
6110
6111 for (Slice &S : AS) {
6112 auto *User = cast<Instruction>(S.getUse()->getUser());
6113 if (isAssumeLikeIntrinsic(User)) {
6114 LLVM_DEBUG({
6115 dbgs() << "Ignoring slice: ";
6116 AS.print(dbgs(), &S);
6117 });
6118 continue;
6119 }
6120 if (S.beginOffset() >= EndOffset) {
6121 Flush();
6122 BeginOffset = S.beginOffset();
6123 EndOffset = S.endOffset();
6124 } else if (S.beginOffset() != BeginOffset || S.endOffset() != EndOffset) {
6125 if (AllSameAndValid) {
6126 LLVM_DEBUG({
6127 dbgs() << "Slice does not match range [" << BeginOffset << ", "
6128 << EndOffset << ")";
6129 AS.print(dbgs(), &S);
6130 });
6131 AllSameAndValid = false;
6132 }
6133 EndOffset = std::max(EndOffset, S.endOffset());
6134 continue;
6135 }
6136
6137 if (auto *LI = dyn_cast<LoadInst>(User)) {
6138 Type *UserTy = LI->getType();
6139 // LoadAndStorePromoter requires all the types to be the same.
6140 if (!LI->isSimple() || (PartitionType && UserTy != PartitionType))
6141 AllSameAndValid = false;
6142 PartitionType = UserTy;
6143 Insts.push_back(User);
6144 } else if (auto *SI = dyn_cast<StoreInst>(User)) {
6145 Type *UserTy = SI->getValueOperand()->getType();
6146 if (!SI->isSimple() || (PartitionType && UserTy != PartitionType))
6147 AllSameAndValid = false;
6148 PartitionType = UserTy;
6149 Insts.push_back(User);
6150 } else {
6151 AllSameAndValid = false;
6152 }
6153 }
6154
6155 Flush();
6156 return true;
6157}
6158
6159/// Analyze an alloca for SROA.
6160///
6161/// This analyzes the alloca to ensure we can reason about it, builds
6162/// the slices of the alloca, and then hands it off to be split and
6163/// rewritten as needed.
6164std::pair<bool /*Changed*/, bool /*CFGChanged*/>
6165SROA::runOnAlloca(AllocaInst &AI) {
6166 bool Changed = false;
6167 bool CFGChanged = false;
6168
6169 LLVM_DEBUG(dbgs() << "SROA alloca: " << AI << "\n");
6170 ++NumAllocasAnalyzed;
6171
6172 // Special case dead allocas, as they're trivial.
6173 if (AI.use_empty()) {
6174 AI.eraseFromParent();
6175 Changed = true;
6176 return {Changed, CFGChanged};
6177 }
6178 const DataLayout &DL = AI.getDataLayout();
6179
6180 // Skip alloca forms that this analysis can't handle.
6181 std::optional<TypeSize> Size = AI.getAllocationSize(DL);
6182 if (AI.isArrayAllocation() || !Size || Size->isScalable() || Size->isZero())
6183 return {Changed, CFGChanged};
6184
6185 // First, split any FCA loads and stores touching this alloca to promote
6186 // better splitting and promotion opportunities.
6187 IRBuilderTy IRB(&AI);
6188 AggLoadStoreRewriter AggRewriter(DL, IRB);
6189 Changed |= AggRewriter.rewrite(AI);
6190
6191 // Build the slices using a recursive instruction-visiting builder.
6192 AllocaSlices AS(DL, AI);
6193 LLVM_DEBUG(AS.print(dbgs()));
6194 if (AS.isEscaped())
6195 return {Changed, CFGChanged};
6196
6197 if (AS.isEscapedReadOnly()) {
6198 Changed |= propagateStoredValuesToLoads(AI, AS);
6199 return {Changed, CFGChanged};
6200 }
6201
6202 // Delete all the dead users of this alloca before splitting and rewriting it.
6203 for (Instruction *DeadUser : AS.getDeadUsers()) {
6204 // Free up everything used by this instruction.
6205 for (Use &DeadOp : DeadUser->operands())
6206 clobberUse(DeadOp);
6207
6208 // Now replace the uses of this instruction.
6209 DeadUser->replaceAllUsesWith(PoisonValue::get(DeadUser->getType()));
6210
6211 // And mark it for deletion.
6212 DeadInsts.push_back(DeadUser);
6213 Changed = true;
6214 }
6215 for (Use *DeadOp : AS.getDeadOperands()) {
6216 clobberUse(*DeadOp);
6217 Changed = true;
6218 }
6219
6220 // No slices to split. Leave the dead alloca for a later pass to clean up.
6221 if (AS.begin() == AS.end())
6222 return {Changed, CFGChanged};
6223
6224 Changed |= splitAlloca(AI, AS);
6225
6226 LLVM_DEBUG(dbgs() << " Speculating PHIs\n");
6227 while (!SpeculatablePHIs.empty())
6228 speculatePHINodeLoads(IRB, *SpeculatablePHIs.pop_back_val());
6229
6230 LLVM_DEBUG(dbgs() << " Rewriting Selects\n");
6231 auto RemainingSelectsToRewrite = SelectsToRewrite.takeVector();
6232 while (!RemainingSelectsToRewrite.empty()) {
6233 const auto [K, V] = RemainingSelectsToRewrite.pop_back_val();
6234 CFGChanged |=
6235 rewriteSelectInstMemOps(*K, V, IRB, PreserveCFG ? nullptr : DTU);
6236 }
6237
6238 return {Changed, CFGChanged};
6239}
6240
6241/// Delete the dead instructions accumulated in this run.
6242///
6243/// Recursively deletes the dead instructions we've accumulated. This is done
6244/// at the very end to maximize locality of the recursive delete and to
6245/// minimize the problems of invalidated instruction pointers as such pointers
6246/// are used heavily in the intermediate stages of the algorithm.
6247///
6248/// We also record the alloca instructions deleted here so that they aren't
6249/// subsequently handed to mem2reg to promote.
6250bool SROA::deleteDeadInstructions(
6251 SmallPtrSetImpl<AllocaInst *> &DeletedAllocas) {
6252 bool Changed = false;
6253 while (!DeadInsts.empty()) {
6254 Instruction *I = dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val());
6255 if (!I)
6256 continue;
6257 LLVM_DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n");
6258
6259 // If the instruction is an alloca, find the possible dbg.declare connected
6260 // to it, and remove it too. We must do this before calling RAUW or we will
6261 // not be able to find it.
6262 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
6263 DeletedAllocas.insert(AI);
6264 for (DbgVariableRecord *OldDII : findDVRDeclares(AI))
6265 OldDII->eraseFromParent();
6266 }
6267
6269 I->replaceAllUsesWith(UndefValue::get(I->getType()));
6270
6271 for (Use &Operand : I->operands())
6272 if (Instruction *U = dyn_cast<Instruction>(Operand)) {
6273 // Zero out the operand and see if it becomes trivially dead.
6274 Operand = nullptr;
6276 DeadInsts.push_back(U);
6277 }
6278
6279 ++NumDeleted;
6280 I->eraseFromParent();
6281 Changed = true;
6282 }
6283 return Changed;
6284}
6285/// Promote the allocas, using the best available technique.
6286///
6287/// This attempts to promote whatever allocas have been identified as viable in
6288/// the PromotableAllocas list. If that list is empty, there is nothing to do.
6289/// This function returns whether any promotion occurred.
6290bool SROA::promoteAllocas() {
6291 if (PromotableAllocas.empty())
6292 return false;
6293
6294 if (SROASkipMem2Reg) {
6295 LLVM_DEBUG(dbgs() << "Not promoting allocas with mem2reg!\n");
6296 } else {
6297 LLVM_DEBUG(dbgs() << "Promoting allocas with mem2reg...\n");
6298 NumPromoted += PromotableAllocas.size();
6299 PromoteMemToReg(PromotableAllocas.getArrayRef(), DTU->getDomTree(), AC);
6300 }
6301
6302 PromotableAllocas.clear();
6303 return true;
6304}
6305
6306std::pair<bool /*Changed*/, bool /*CFGChanged*/> SROA::runSROA(Function &F) {
6307 LLVM_DEBUG(dbgs() << "SROA function: " << F.getName() << "\n");
6308
6309 const DataLayout &DL = F.getDataLayout();
6310 BasicBlock &EntryBB = F.getEntryBlock();
6311 for (BasicBlock::iterator I = EntryBB.begin(), E = std::prev(EntryBB.end());
6312 I != E; ++I) {
6313 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
6314 std::optional<TypeSize> Size = AI->getAllocationSize(DL);
6315 if (Size && Size->isScalable() && isAllocaPromotable(AI))
6316 PromotableAllocas.insert(AI);
6317 else
6318 Worklist.insert(AI);
6319 }
6320 }
6321
6322 bool Changed = false;
6323 bool CFGChanged = false;
6324 // A set of deleted alloca instruction pointers which should be removed from
6325 // the list of promotable allocas.
6326 SmallPtrSet<AllocaInst *, 4> DeletedAllocas;
6327
6328 do {
6329 while (!Worklist.empty()) {
6330 auto [IterationChanged, IterationCFGChanged] =
6331 runOnAlloca(*Worklist.pop_back_val());
6332 Changed |= IterationChanged;
6333 CFGChanged |= IterationCFGChanged;
6334
6335 Changed |= deleteDeadInstructions(DeletedAllocas);
6336
6337 // Remove the deleted allocas from various lists so that we don't try to
6338 // continue processing them.
6339 if (!DeletedAllocas.empty()) {
6340 Worklist.set_subtract(DeletedAllocas);
6341 PostPromotionWorklist.set_subtract(DeletedAllocas);
6342 PromotableAllocas.set_subtract(DeletedAllocas);
6343 DeletedAllocas.clear();
6344 }
6345 }
6346
6347 Changed |= promoteAllocas();
6348
6349 Worklist = PostPromotionWorklist;
6350 PostPromotionWorklist.clear();
6351 } while (!Worklist.empty());
6352
6353 assert((!CFGChanged || Changed) && "Can not only modify the CFG.");
6354 assert((!CFGChanged || !PreserveCFG) &&
6355 "Should not have modified the CFG when told to preserve it.");
6356
6357 if (Changed && isAssignmentTrackingEnabled(*F.getParent())) {
6358 for (auto &BB : F) {
6360 }
6361 }
6362
6363 return {Changed, CFGChanged};
6364}
6365
6369 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
6370 auto [Changed, CFGChanged] =
6371 SROA(&F.getContext(), &DTU, &AC, Options).runSROA(F);
6372 if (!Changed)
6373 return PreservedAnalyses::all();
6375 if (!CFGChanged)
6378 return PA;
6379}
6380
6382 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
6383 static_cast<PassInfoMixin<SROAPass> *>(this)->printPipeline(
6384 OS, MapClassName2PassName);
6385 OS << '<'
6386 << (Options.CFG == SROAOptions::PreserveCFG ? "preserve-cfg"
6387 : "modify-cfg");
6388 if (Options.AggregateToVector)
6389 OS << ";aggregate-to-vector";
6390 OS << '>';
6391}
6392
6393SROAPass::SROAPass(SROAOptions Options) : Options(Options) {}
6394
6395namespace {
6396
6397/// A legacy pass for the legacy pass manager that wraps the \c SROA pass.
6398class SROALegacyPass : public FunctionPass {
6400
6401public:
6402 static char ID;
6403
6405 : FunctionPass(ID), Options(Options) {
6407 }
6408
6409 bool runOnFunction(Function &F) override {
6410 if (skipFunction(F))
6411 return false;
6412
6413 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
6414 AssumptionCache &AC =
6415 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
6416 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
6417 auto [Changed, _] = SROA(&F.getContext(), &DTU, &AC, Options).runSROA(F);
6418 return Changed;
6419 }
6420
6421 void getAnalysisUsage(AnalysisUsage &AU) const override {
6422 AU.addRequired<AssumptionCacheTracker>();
6423 AU.addRequired<DominatorTreeWrapperPass>();
6424 AU.addPreserved<GlobalsAAWrapperPass>();
6425 AU.addPreserved<DominatorTreeWrapperPass>();
6426 }
6427
6428 StringRef getPassName() const override { return "SROA"; }
6429};
6430
6431} // end anonymous namespace
6432
6433char SROALegacyPass::ID = 0;
6434
6435FunctionPass *llvm::createSROAPass(bool PreserveCFG, bool AggregateToVector) {
6436 return new SROALegacyPass(SROAOptions(PreserveCFG ? SROAOptions::PreserveCFG
6438 AggregateToVector));
6439}
6440
6441INITIALIZE_PASS_BEGIN(SROALegacyPass, "sroa",
6442 "Scalar Replacement Of Aggregates", false, false)
6445INITIALIZE_PASS_END(SROALegacyPass, "sroa", "Scalar Replacement Of Aggregates",
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Forward Handle Accesses
DXIL Resource Access
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
Flatten the CFG
#define DEBUG_TYPE
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:621
This file implements a map that provides insertion order iteration.
static std::optional< AllocFnsTy > getAllocationSize(const CallBase *CB, const TargetLibraryInfo *TLI)
static std::optional< uint64_t > getSizeInBytes(std::optional< uint64_t > SizeInBits)
Memory SSA
Definition MemorySSA.cpp:73
This file contains the declarations for metadata subclasses.
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
if(PassOpts->AAPipeline)
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the PointerIntPair class.
This file provides a collection of visitors which walk the (instruction) uses of a pointer.
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
static void migrateDebugInfo(AllocaInst *OldAlloca, bool IsSplit, uint64_t OldAllocaOffsetInBits, uint64_t SliceSizeInBits, Instruction *OldInst, Instruction *Inst, Value *Dest, Value *Value, const DataLayout &DL)
Find linked dbg.assign and generate a new one with the correct FragmentInfo.
Definition SROA.cpp:341
static VectorType * isVectorPromotionViable(Partition &P, const DataLayout &DL, unsigned VScale)
Test whether the given alloca partitioning and range of slices can be promoted to a vector.
Definition SROA.cpp:2268
static Align getAdjustedAlignment(Instruction *I, uint64_t Offset)
Compute the adjusted alignment for a load or store from an offset.
Definition SROA.cpp:1941
static VectorType * checkVectorTypesForPromotion(Partition &P, const DataLayout &DL, SmallVectorImpl< VectorType * > &CandidateTys, bool HaveCommonEltTy, Type *CommonEltTy, bool HaveVecPtrTy, bool HaveCommonVecPtrTy, VectorType *CommonVecPtrTy, unsigned VScale)
Test whether any vector type in CandidateTys is viable for promotion.
Definition SROA.cpp:2119
static std::pair< Type *, IntegerType * > findCommonType(AllocaSlices::const_iterator B, AllocaSlices::const_iterator E, uint64_t EndOffset)
Walk the range of a partitioning looking for a common type to cover this sequence of slices.
Definition SROA.cpp:1507
static Type * stripAggregateTypeWrapping(const DataLayout &DL, Type *Ty)
Strip aggregate type wrapping.
Definition SROA.cpp:4688
static FragCalcResult calculateFragment(DILocalVariable *Variable, uint64_t NewStorageSliceOffsetInBits, uint64_t NewStorageSliceSizeInBits, std::optional< DIExpression::FragmentInfo > StorageFragment, std::optional< DIExpression::FragmentInfo > CurrentFragment, DIExpression::FragmentInfo &Target)
Definition SROA.cpp:276
static DIExpression * createOrReplaceFragment(const DIExpression *Expr, DIExpression::FragmentInfo Frag, int64_t BitExtractOffset)
Create or replace an existing fragment in a DIExpression with Frag.
Definition SROA.cpp:5736
static Value * insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old, Value *V, uint64_t Offset, const Twine &Name)
Definition SROA.cpp:2511
static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S, VectorType *Ty, uint64_t ElementSize, const DataLayout &DL, unsigned VScale)
Test whether the given slice use can be promoted to a vector.
Definition SROA.cpp:2039
static Value * getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr, APInt Offset, Type *PointerTy, const Twine &NamePrefix)
Compute an adjusted pointer from Ptr by Offset bytes where the resulting pointer has PointerTy.
Definition SROA.cpp:1930
static bool isIntegerWideningViableForSlice(const Slice &S, uint64_t AllocBeginOffset, Type *AllocaTy, const DataLayout &DL, bool &WholeAllocaOp)
Test whether a slice of an alloca is valid for integer widening.
Definition SROA.cpp:2350
static Value * extractVector(IRBuilderTy &IRB, Value *V, unsigned BeginIndex, unsigned EndIndex, const Twine &Name)
Definition SROA.cpp:2544
static Value * foldPHINodeOrSelectInst(Instruction &I)
A helper that folds a PHI node or a select.
Definition SROA.cpp:1003
static bool rewriteSelectInstMemOps(SelectInst &SI, const RewriteableMemOps &Ops, IRBuilderTy &IRB, DomTreeUpdater *DTU)
Definition SROA.cpp:1896
static void rewriteMemOpOfSelect(SelectInst &SI, T &I, SelectHandSpeculativity Spec, DomTreeUpdater &DTU)
Definition SROA.cpp:1829
static Value * foldSelectInst(SelectInst &SI)
Definition SROA.cpp:990
bool isKillAddress(const DbgVariableRecord *DVR)
Definition SROA.cpp:5699
static Value * insertVector(IRBuilderTy &IRB, Value *Old, Value *V, unsigned BeginIndex, const Twine &Name)
Definition SROA.cpp:2565
static bool isIntegerWideningViable(Partition &P, Type *AllocaTy, const DataLayout &DL)
Test whether the given alloca partition's integer operations can be widened to promotable ones.
Definition SROA.cpp:2445
static void speculatePHINodeLoads(IRBuilderTy &IRB, PHINode &PN)
Definition SROA.cpp:1651
static VectorType * createAndCheckVectorTypesForPromotion(SetVector< Type * > &OtherTys, ArrayRef< VectorType * > CandidateTysCopy, function_ref< void(Type *)> CheckCandidateType, Partition &P, const DataLayout &DL, SmallVectorImpl< VectorType * > &CandidateTys, bool &HaveCommonEltTy, Type *&CommonEltTy, bool &HaveVecPtrTy, bool &HaveCommonVecPtrTy, VectorType *&CommonVecPtrTy, unsigned VScale)
Definition SROA.cpp:2224
static DebugVariable getAggregateVariable(DbgVariableRecord *DVR)
Definition SROA.cpp:322
static std::tuple< Type *, bool, VectorType * > selectPartitionType(Partition &P, const DataLayout &DL, AllocaInst &AI, LLVMContext &C, bool AggregateToVector)
Select a partition type for an alloca partition.
Definition SROA.cpp:5421
static bool isSafePHIToSpeculate(PHINode &PN)
PHI instructions that use an alloca and are subsequently loaded can be rewritten to load both input p...
Definition SROA.cpp:1576
static FixedVectorType * tryCanonicalizeStructToVector(StructType *STy, Partition &P, const DataLayout &DL)
Try to canonicalize a homogeneous struct partition to a vector type.
Definition SROA.cpp:5354
static Value * extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V, IntegerType *Ty, uint64_t Offset, const Twine &Name)
Definition SROA.cpp:2486
static void insertNewDbgInst(DIBuilder &DIB, DbgVariableRecord *Orig, AllocaInst *NewAddr, DIExpression *NewAddrExpr, Instruction *BeforeInst, std::optional< DIExpression::FragmentInfo > NewFragment, int64_t BitExtractAdjustment)
Insert a new DbgRecord.
Definition SROA.cpp:5800
static void speculateSelectInstLoads(SelectInst &SI, LoadInst &LI, IRBuilderTy &IRB)
Definition SROA.cpp:1791
static Value * mergeTwoVectors(Value *V0, Value *V1, const DataLayout &DL, Type *NewAIEltTy, IRBuilder<> &Builder)
This function takes two vector values and combines them into a single vector by concatenating their e...
Definition SROA.cpp:2636
const DIExpression * getAddressExpression(const DbgVariableRecord *DVR)
Definition SROA.cpp:5705
static Type * getTypePartition(const DataLayout &DL, Type *Ty, uint64_t Offset, uint64_t Size)
Try to find a partition of the aggregate type passed in for a given offset and size.
Definition SROA.cpp:4726
static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy, unsigned VScale=0)
Test whether we can convert a value from the old to the new type.
Definition SROA.cpp:1951
static SelectHandSpeculativity isSafeLoadOfSelectToSpeculate(LoadInst &LI, SelectInst &SI, bool PreserveCFG)
Definition SROA.cpp:1732
static Type * findCommonTypeThroughPHIOrSelect(Instruction &I)
Find a common load/store type used through a pointer PHI or select.
Definition SROA.cpp:1483
This file provides the interface for LLVM's Scalar Replacement of Aggregates pass.
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file implements the SmallBitVector class.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Virtual Register Rewriter
Value * RHS
Value * LHS
Builder for the alloca slices.
Definition SROA.cpp:1015
SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS)
Definition SROA.cpp:1031
An iterator over partitions of the alloca's slices.
Definition SROA.cpp:803
bool operator==(const partition_iterator &RHS) const
Definition SROA.cpp:950
partition_iterator & operator++()
Definition SROA.cpp:970
bool shouldDelete(Instruction *I) const override
Return false if a sub-class wants to keep one of the loads/stores after the SSA construction.
Definition SROA.cpp:6072
BasicLoadAndStorePromoter(ArrayRef< const Instruction * > Insts, SSAUpdater &S, Type *ZeroType)
Definition SROA.cpp:6069
Value * getValueToUseForAlloca(Instruction *I) const override
Return the value to use for the point in the code that the alloca is positioned.
Definition SROA.cpp:6076
Class for arbitrary precision integers.
Definition APInt.h:78
an instruction to allocate memory on the stack
LLVM_ABI bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
PointerType * getType() const
Overload to return most specific pointer type.
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
LLVM_ABI std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:459
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:446
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
LLVM_ABI CaptureInfo getCaptureInfo(unsigned OpNo) const
Return which pointer components this operand may capture.
bool onlyReadsMemory(unsigned OpNo) const
bool isDataOperand(const Use *U) const
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static DIAssignID * getDistinct(LLVMContext &Context)
LLVM_ABI DbgRecord * insertDbgAssign(Instruction *LinkedInstr, Value *Val, DILocalVariable *SrcVar, DIExpression *ValExpr, Value *Addr, DIExpression *AddrExpr, const DILocation *DL)
Insert a new dbg_assign record.
DWARF expression.
iterator_range< expr_op_iterator > expr_ops() const
DbgVariableFragmentInfo FragmentInfo
LLVM_ABI bool startsWithDeref() const
Return whether the first element a DW_OP_deref.
static LLVM_ABI bool calculateFragmentIntersect(const DataLayout &DL, const Value *SliceStart, uint64_t SliceOffsetInBits, uint64_t SliceSizeInBits, const Value *DbgPtr, int64_t DbgPtrOffsetInBits, int64_t DbgExtractOffsetInBits, DIExpression::FragmentInfo VarFrag, std::optional< DIExpression::FragmentInfo > &Result, int64_t &OffsetFromLocationInBits)
Computes a fragment, bit-extract operation if needed, and new constant offset to describe a part of a...
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
static LLVM_ABI DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI void moveBefore(DbgRecord *MoveBefore)
DebugLoc getDebugLoc() const
void setDebugLoc(DebugLoc Loc)
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI void setKillAddress()
Kill the address component.
LLVM_ABI bool isKillLocation() const
LLVM_ABI bool isKillAddress() const
Check whether this kills the address component.
LLVM_ABI void replaceVariableLocationOp(Value *OldValue, Value *NewValue, bool AllowEmpty=false)
Value * getValue(unsigned OpIdx=0) const
static LLVM_ABI DbgVariableRecord * createLinkedDVRAssign(Instruction *LinkedInstr, Value *Val, DILocalVariable *Variable, DIExpression *Expression, Value *Address, DIExpression *AddressExpression, const DILocation *DI)
LLVM_ABI void setAssignId(DIAssignID *New)
DIExpression * getExpression() const
static LLVM_ABI DbgVariableRecord * createDVRDeclare(Value *Address, DILocalVariable *DV, DIExpression *Expr, const DILocation *DI)
static LLVM_ABI DbgVariableRecord * createDbgVariableRecord(Value *Location, DILocalVariable *DV, DIExpression *Expr, const DILocation *DI)
DILocalVariable * getVariable() const
DIExpression * getAddressExpression() const
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
Identifies a unique instance of a variable.
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:278
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:251
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:247
iterator end()
Definition DenseMap.h:169
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:312
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
Class to represent fixed width SIMD vectors.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:843
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
unsigned getVScaleValue() const
Return the value for vscale based on the vscale_range attribute or 0 when unknown.
const BasicBlock & getEntryBlock() const
Definition Function.h:794
LLVM_ABI bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset, function_ref< bool(Value &, APInt &)> ExternalAnalysis=nullptr) const
Accumulate the constant address offset of this GEP if possible.
Definition Operator.cpp:130
iterator_range< op_iterator > indices()
Type * getSourceElementType() const
LLVM_ABI GEPNoWrapFlags getNoWrapFlags() const
Get the nowrap flags for the GEP instruction.
This provides the default implementation of the IRBuilder 'InsertHelper' method that is called whenev...
Definition IRBuilder.h:61
virtual void InsertHelper(Instruction *I, const Twine &Name, BasicBlock::iterator InsertPt) const
Definition IRBuilder.h:65
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2908
Base class for instruction visitors.
Definition InstVisitor.h:78
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Instruction * user_back()
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI bool mayHaveSideEffects() const LLVM_READONLY
Return true if the instruction may have side effects.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
iterator_range< user_iterator > users()
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
user_iterator user_begin()
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Class to represent integer types.
@ MAX_INT_BITS
Maximum number of bits that can be specified.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
LLVM_ABI LoadAndStorePromoter(ArrayRef< const Instruction * > Insts, SSAUpdater &S, StringRef Name=StringRef())
An instruction for reading from memory.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
void setAlignment(Align Align)
Value * getPointerOperand()
bool isVolatile() const
Return true if this is a load from a volatile memory location.
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this load instruction.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Type * getPointerOperandType() const
static unsigned getPointerOperandIndex()
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
bool isSimple() const
Align getAlign() const
Return the alignment of the access that is being performed.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
LLVMContext & getContext() const
Definition Metadata.h:1233
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
This is the common base class for memset/memcpy/memmove.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PointerIntPair - This class implements a pair of a pointer and small integer.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
PtrUseVisitor(const DataLayout &DL)
LLVM_ABI SROAPass(SROAOptions Options)
If PreserveCFG is set, then the pass is not allowed to modify CFG in any way, even if it would update...
Definition SROA.cpp:6393
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Run the pass over the function.
Definition SROA.cpp:6366
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition SROA.cpp:6381
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition SSAUpdater.h:39
This class represents the LLVM 'select' instruction.
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
void clear()
Completely clear the SetVector.
Definition SetVector.h:273
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
typename SuperClass::const_iterator const_iterator
typename SuperClass::iterator iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
void setAlignment(Align Align)
Value * getValueOperand()
static unsigned getPointerOperandIndex()
Value * getPointerOperand()
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this store instruction.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr size_t npos
Definition StringRef.h:58
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition StringRef.h:365
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:290
LLVM_ABI size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:743
TypeSize getSizeInBytes() const
Definition DataLayout.h:752
LLVM_ABI unsigned getElementContainingOffset(uint64_t FixedOffset) const
Given a valid byte offset into the structure, returns the structure index that contains it.
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
TypeSize getSizeInBits() const
Definition DataLayout.h:754
Class to represent struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:467
element_iterator element_end() const
ArrayRef< Type * > elements() const
element_iterator element_begin() const
bool isPacked() const
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Type::subtype_iterator element_iterator
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:339
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isSingleValueType() const
Return true if the type is a valid type for a register in codegen.
Definition Type.h:306
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:297
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:363
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:271
bool isTargetExtTy() const
Return true if this is a target extension type.
Definition Type.h:205
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:280
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:303
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:267
op_iterator op_begin()
Definition User.h:259
const Use & getOperandUse(unsigned i) const
Definition User.h:220
Value * getOperand(unsigned i) const
Definition User.h:207
op_iterator op_end()
Definition User.h:261
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:441
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI const Value * stripInBoundsOffsets(function_ref< void(const Value *)> Func=[](const Value *) {}) const
Strip off pointer casts and inbounds GEPs.
Definition Value.cpp:828
LLVM_ABI void dropDroppableUsesIn(User &Usr)
Remove every use of this value in User that can safely be removed.
Definition Value.cpp:215
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
bool use_empty() const
Definition Value.h:348
iterator_range< use_iterator > uses()
Definition Value.h:382
bool hasName() const
Definition Value.h:263
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static VectorType * getWithSizeAndScalar(VectorType *SizeTy, Type *EltTy)
This static method attempts to construct a VectorType with the same size-in-bits as SizeTy but with a...
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isFixed() const
Returns true if the quantity is not scaled by vscale.
Definition TypeSize.h:171
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterator.h:80
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
Offsets
Offsets in bytes from the start of the input buffer.
SmallVector< DbgVariableRecord * > getDVRAssignmentMarkers(const Instruction *Inst)
Return a range of dbg_assign records for which Inst performs the assignment they encode.
Definition DebugInfo.h:205
LLVM_ABI void deleteAssignmentMarkers(const Instruction *Inst)
Delete the llvm.dbg.assign intrinsics linked to Inst.
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_fragment
Only used in LLVM metadata.
Definition Dwarf.h:144
@ User
could "use" a pointer
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
bool empty() const
Definition BasicBlock.h:101
iterator end() const
Definition BasicBlock.h:89
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
LLVM_ABI iterator begin() const
unsigned getNumElements(Type *Ty)
Definition SLPUtils.cpp:86
This is an optimization pass for GlobalISel generic memory operations.
static cl::opt< bool > SROASkipMem2Reg("sroa-skip-mem2reg", cl::init(false), cl::Hidden)
Disable running mem2reg during SROA in order to test or debug SROA.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
@ Offset
Definition DWP.cpp:577
@ Length
Definition DWP.cpp:577
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
void stable_sort(R &&Range)
Definition STLExtras.h:2116
LLVM_ABI bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
LLVM_ABI void PromoteMemToReg(ArrayRef< AllocaInst * > Allocas, DominatorTree &DT, AssumptionCache *AC=nullptr)
Promote the specified list of alloca instructions into scalar registers, inserting PHI nodes as appro...
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2140
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI std::optional< RegOrConstant > getVectorSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Definition Utils.cpp:1447
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
void * PointerTy
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2134
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2200
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:402
bool capturesFullProvenance(CaptureComponents CC)
Definition ModRef.h:396
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI void SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void initializeSROALegacyPassPass(PassRegistry &)
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
LLVM_ABI TinyPtrVector< DbgVariableRecord * > findDVRValues(Value *V)
As above, for DVRValues.
Definition DebugInfo.cpp:82
LLVM_ABI void llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr, unsigned line=0)
This function calls abort(), and prints the optional message to stderr.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr int PoisonMaskElem
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
DWARFExpression::Operation Op
LLVM_ABI FunctionPass * createSROAPass(bool PreserveCFG=true, bool AggregateToVector=false)
Definition SROA.cpp:6435
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1772
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2192
LLVM_ABI TinyPtrVector< DbgVariableRecord * > findDVRDeclares(Value *V)
Finds dbg.declare records declaring local variables as living in the memory that 'V' points to.
Definition DebugInfo.cpp:48
LLVM_ABI bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size, const SimplifyQuery &SQ)
Return true if we know that executing a load from this value cannot trap.
Definition Loads.cpp:456
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2166
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI llvm::SmallVector< int, 16 > createSequentialMask(unsigned Start, unsigned NumInts, unsigned NumUndefs)
Create a sequential shuffle mask.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define NDEBUG
Definition regutils.h:48
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
AAMDNodes shift(size_t Offset) const
Create a new AAMDNode that describes this AAMDNode after applying a constant offset to the start of t...
Definition Metadata.h:822
LLVM_ABI AAMDNodes adjustForAccess(unsigned AccessSize)
Create a new AAMDNode for accessing AccessSize bytes of this AAMDNode.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Describes an element of a Bitfield.
Definition Bitfields.h:176
static Bitfield::Type get(StorageType Packed)
Unpacks the field from the Packed value.
Definition Bitfields.h:207
static void set(StorageType &Packed, typename Bitfield::Type Value)
Sets the typed value in the provided Packed value.
Definition Bitfields.h:223