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