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