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