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