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