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