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