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