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