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