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