Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1 | //===- SROA.cpp - Scalar Replacement Of Aggregates ------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// This transformation implements the well known scalar replacement of |
| 10 | /// aggregates transformation. It tries to identify promotable elements of an |
| 11 | /// aggregate alloca, and promote them to registers. It will also try to |
| 12 | /// convert uses of an element (or set of elements) of an alloca into a vector |
| 13 | /// or bitfield-style integer scalar if appropriate. |
| 14 | /// |
| 15 | /// It works to do this with minimal slicing of the alloca so that regions |
| 16 | /// which are merely transferred in and out of external memory remain unchanged |
| 17 | /// and are not decomposed to scalar code. |
| 18 | /// |
| 19 | /// Because this also performs alloca promotion, it can be thought of as also |
| 20 | /// serving the purpose of SSA formation. The algorithm iterates on the |
| 21 | /// function until all opportunities for promotion have been realized. |
| 22 | /// |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar/SROA.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/APInt.h" |
| 27 | #include "llvm/ADT/ArrayRef.h" |
| 28 | #include "llvm/ADT/DenseMap.h" |
| 29 | #include "llvm/ADT/PointerIntPair.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SetVector.h" |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallBitVector.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
| 35 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringRef.h" |
| 37 | #include "llvm/ADT/Twine.h" |
| 38 | #include "llvm/ADT/iterator.h" |
| 39 | #include "llvm/ADT/iterator_range.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/Loads.h" |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/PtrUseVisitor.h" |
David Blaikie | 31b98d2 | 2018-06-04 21:23:21 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/Local.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 45 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 46 | #include "llvm/IR/BasicBlock.h" |
| 47 | #include "llvm/IR/Constant.h" |
| 48 | #include "llvm/IR/ConstantFolder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 12664a0 | 2014-03-06 00:22:06 +0000 | [diff] [blame] | 50 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 51 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 52 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 53 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 54 | #include "llvm/IR/Dominators.h" |
| 55 | #include "llvm/IR/Function.h" |
| 56 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 57 | #include "llvm/IR/GlobalAlias.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 58 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 59 | #include "llvm/IR/InstVisitor.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 60 | #include "llvm/IR/InstrTypes.h" |
| 61 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 62 | #include "llvm/IR/Instructions.h" |
| 63 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 64 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 65 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 66 | #include "llvm/IR/Metadata.h" |
| 67 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 68 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 69 | #include "llvm/IR/PassManager.h" |
| 70 | #include "llvm/IR/Type.h" |
| 71 | #include "llvm/IR/Use.h" |
| 72 | #include "llvm/IR/User.h" |
| 73 | #include "llvm/IR/Value.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 74 | #include "llvm/Pass.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 75 | #include "llvm/Support/Casting.h" |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 76 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 77 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 78 | #include "llvm/Support/Debug.h" |
| 79 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 80 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 81 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 82 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 83 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 84 | #include <algorithm> |
| 85 | #include <cassert> |
| 86 | #include <chrono> |
| 87 | #include <cstddef> |
| 88 | #include <cstdint> |
| 89 | #include <cstring> |
| 90 | #include <iterator> |
| 91 | #include <string> |
| 92 | #include <tuple> |
| 93 | #include <utility> |
| 94 | #include <vector> |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 95 | |
Hal Finkel | 29f5131 | 2016-03-28 11:13:03 +0000 | [diff] [blame] | 96 | #ifndef NDEBUG |
| 97 | // We only use this for a debug check. |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 98 | #include <random> |
| 99 | #endif |
| 100 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 101 | using namespace llvm; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 102 | using namespace llvm::sroa; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 103 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 104 | #define DEBUG_TYPE "sroa" |
| 105 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 106 | STATISTIC(NumAllocasAnalyzed, "Number of allocas analyzed for replacement"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 107 | STATISTIC(NumAllocaPartitions, "Number of alloca partitions formed"); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 108 | STATISTIC(MaxPartitionsPerAlloca, "Maximum number of partitions per alloca"); |
| 109 | STATISTIC(NumAllocaPartitionUses, "Number of alloca partition uses rewritten"); |
| 110 | STATISTIC(MaxUsesPerAllocaPartition, "Maximum number of uses of a partition"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 111 | STATISTIC(NumNewAllocas, "Number of new, smaller allocas introduced"); |
| 112 | STATISTIC(NumPromoted, "Number of allocas promoted to SSA values"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 113 | STATISTIC(NumLoadsSpeculated, "Number of loads speculated to allow promotion"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 114 | STATISTIC(NumDeleted, "Number of instructions deleted"); |
| 115 | STATISTIC(NumVectorized, "Number of vectorized aggregates"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 116 | |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 117 | /// Hidden option to enable randomly shuffling the slices to help uncover |
| 118 | /// instability in their order. |
| 119 | static cl::opt<bool> SROARandomShuffleSlices("sroa-random-shuffle-slices", |
| 120 | cl::init(false), cl::Hidden); |
| 121 | |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 122 | /// Hidden option to experiment with completely strict handling of inbounds |
| 123 | /// GEPs. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 124 | static cl::opt<bool> SROAStrictInbounds("sroa-strict-inbounds", cl::init(false), |
| 125 | cl::Hidden); |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 126 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 127 | namespace { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 128 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 129 | /// A custom IRBuilder inserter which prefixes all names, but only in |
Mehdi Amini | 1e9c925 | 2016-03-11 17:15:34 +0000 | [diff] [blame] | 130 | /// Assert builds. |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 131 | class IRBuilderPrefixedInserter : public IRBuilderDefaultInserter { |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 132 | std::string Prefix; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 133 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 134 | const Twine getNameWithPrefix(const Twine &Name) const { |
| 135 | return Name.isTriviallyEmpty() ? Name : Prefix + Name; |
| 136 | } |
| 137 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 138 | public: |
| 139 | void SetNamePrefix(const Twine &P) { Prefix = P.str(); } |
| 140 | |
| 141 | protected: |
| 142 | void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, |
| 143 | BasicBlock::iterator InsertPt) const { |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 144 | IRBuilderDefaultInserter::InsertHelper(I, getNameWithPrefix(Name), BB, |
| 145 | InsertPt); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 149 | /// Provide a type for IRBuilder that drops names in release builds. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 150 | using IRBuilderTy = IRBuilder<ConstantFolder, IRBuilderPrefixedInserter>; |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 151 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 152 | /// A used slice of an alloca. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 153 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 154 | /// This structure represents a slice of an alloca used by some instruction. It |
| 155 | /// stores both the begin and end offsets of this use, a pointer to the use |
| 156 | /// itself, and a flag indicating whether we can classify the use as splittable |
| 157 | /// or not when forming partitions of the alloca. |
| 158 | class Slice { |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 159 | /// The beginning offset of the range. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 160 | uint64_t BeginOffset = 0; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 161 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 162 | /// The ending offset, not included in the range. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 163 | uint64_t EndOffset = 0; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 164 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 165 | /// 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] | 166 | /// split. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 167 | PointerIntPair<Use *, 1, bool> UseAndIsSplittable; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 168 | |
| 169 | public: |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 170 | Slice() = default; |
| 171 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 172 | Slice(uint64_t BeginOffset, uint64_t EndOffset, Use *U, bool IsSplittable) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 173 | : BeginOffset(BeginOffset), EndOffset(EndOffset), |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 174 | UseAndIsSplittable(U, IsSplittable) {} |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 175 | |
| 176 | uint64_t beginOffset() const { return BeginOffset; } |
| 177 | uint64_t endOffset() const { return EndOffset; } |
| 178 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 179 | bool isSplittable() const { return UseAndIsSplittable.getInt(); } |
| 180 | void makeUnsplittable() { UseAndIsSplittable.setInt(false); } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 181 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 182 | Use *getUse() const { return UseAndIsSplittable.getPointer(); } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 183 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 184 | bool isDead() const { return getUse() == nullptr; } |
| 185 | void kill() { UseAndIsSplittable.setPointer(nullptr); } |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 186 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 187 | /// Support for ordering ranges. |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 188 | /// |
| 189 | /// This provides an ordering over ranges such that start offsets are |
| 190 | /// always increasing, and within equal start offsets, the end offsets are |
| 191 | /// decreasing. Thus the spanning range comes first in a cluster with the |
| 192 | /// same start position. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 193 | bool operator<(const Slice &RHS) const { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 194 | if (beginOffset() < RHS.beginOffset()) |
| 195 | return true; |
| 196 | if (beginOffset() > RHS.beginOffset()) |
| 197 | return false; |
| 198 | if (isSplittable() != RHS.isSplittable()) |
| 199 | return !isSplittable(); |
| 200 | if (endOffset() > RHS.endOffset()) |
| 201 | return true; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 205 | /// Support comparison with a single offset to allow binary searches. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 206 | friend LLVM_ATTRIBUTE_UNUSED bool operator<(const Slice &LHS, |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 207 | uint64_t RHSOffset) { |
| 208 | return LHS.beginOffset() < RHSOffset; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 209 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 210 | friend LLVM_ATTRIBUTE_UNUSED bool operator<(uint64_t LHSOffset, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 211 | const Slice &RHS) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 212 | return LHSOffset < RHS.beginOffset(); |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 213 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 214 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 215 | bool operator==(const Slice &RHS) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 216 | return isSplittable() == RHS.isSplittable() && |
| 217 | beginOffset() == RHS.beginOffset() && endOffset() == RHS.endOffset(); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 218 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 219 | bool operator!=(const Slice &RHS) const { return !operator==(RHS); } |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 220 | }; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 221 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 222 | } // end anonymous namespace |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 223 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 224 | /// Representation of the alloca slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 225 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 226 | /// This class represents the slices of an alloca which are formed by its |
| 227 | /// various uses. If a pointer escapes, we can't fully build a representation |
| 228 | /// for the slices used and we reflect that in this structure. The uses are |
| 229 | /// stored, sorted by increasing beginning offset and with unsplittable slices |
| 230 | /// starting at a particular offset before splittable slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 231 | class llvm::sroa::AllocaSlices { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 232 | public: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 233 | /// Construct the slices of a particular alloca. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 234 | AllocaSlices(const DataLayout &DL, AllocaInst &AI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 235 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 236 | /// Test whether a pointer to the allocation escapes our analysis. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 237 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 238 | /// 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] | 239 | /// ignored. |
| 240 | bool isEscaped() const { return PointerEscapingInstr; } |
| 241 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 242 | /// Support for iterating over the slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 243 | /// @{ |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 244 | using iterator = SmallVectorImpl<Slice>::iterator; |
| 245 | using range = iterator_range<iterator>; |
| 246 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 247 | iterator begin() { return Slices.begin(); } |
| 248 | iterator end() { return Slices.end(); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 249 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 250 | using const_iterator = SmallVectorImpl<Slice>::const_iterator; |
| 251 | using const_range = iterator_range<const_iterator>; |
| 252 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 253 | const_iterator begin() const { return Slices.begin(); } |
| 254 | const_iterator end() const { return Slices.end(); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 255 | /// @} |
| 256 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 257 | /// Erase a range of slices. |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 258 | void erase(iterator Start, iterator Stop) { Slices.erase(Start, Stop); } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 259 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 260 | /// Insert new slices for this alloca. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 261 | /// |
| 262 | /// This moves the slices into the alloca's slices collection, and re-sorts |
| 263 | /// everything so that the usual ordering properties of the alloca's slices |
| 264 | /// hold. |
| 265 | void insert(ArrayRef<Slice> NewSlices) { |
| 266 | int OldSize = Slices.size(); |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 267 | Slices.append(NewSlices.begin(), NewSlices.end()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 268 | auto SliceI = Slices.begin() + OldSize; |
Mandeep Singh Grang | 636d94d | 2018-04-13 19:47:57 +0000 | [diff] [blame] | 269 | llvm::sort(SliceI, Slices.end()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 270 | std::inplace_merge(Slices.begin(), SliceI, Slices.end()); |
| 271 | } |
| 272 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 273 | // Forward declare the iterator and range accessor for walking the |
| 274 | // partitions. |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 275 | class partition_iterator; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 276 | iterator_range<partition_iterator> partitions(); |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 277 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 278 | /// Access the dead users for this alloca. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 279 | ArrayRef<Instruction *> getDeadUsers() const { return DeadUsers; } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 280 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 281 | /// Access the dead operands referring to this alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 282 | /// |
| 283 | /// These are operands which have cannot actually be used to refer to the |
| 284 | /// alloca as they are outside its range and the user doesn't correct for |
| 285 | /// that. These mostly consist of PHI node inputs and the like which we just |
| 286 | /// need to replace with undef. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 287 | ArrayRef<Use *> getDeadOperands() const { return DeadOperands; } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 288 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 289 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 290 | void print(raw_ostream &OS, const_iterator I, StringRef Indent = " ") const; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 291 | void printSlice(raw_ostream &OS, const_iterator I, |
| 292 | StringRef Indent = " ") const; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 293 | void printUse(raw_ostream &OS, const_iterator I, |
| 294 | StringRef Indent = " ") const; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 295 | void print(raw_ostream &OS) const; |
Alp Toker | f929e09 | 2014-01-04 22:47:48 +0000 | [diff] [blame] | 296 | void dump(const_iterator I) const; |
| 297 | void dump() const; |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 298 | #endif |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 299 | |
| 300 | private: |
| 301 | template <typename DerivedT, typename RetT = void> class BuilderBase; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 302 | class SliceBuilder; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 303 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 304 | friend class AllocaSlices::SliceBuilder; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 305 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 306 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 307 | /// Handle to alloca instruction to simplify method interfaces. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 308 | AllocaInst &AI; |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 309 | #endif |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 310 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 311 | /// The instruction responsible for this alloca not having a known set |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 312 | /// of slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 313 | /// |
| 314 | /// When an instruction (potentially) escapes the pointer to the alloca, we |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 315 | /// store a pointer to that here and abort trying to form slices of the |
| 316 | /// alloca. This will be null if the alloca slices are analyzed successfully. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 317 | Instruction *PointerEscapingInstr; |
| 318 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 319 | /// The slices of the alloca. |
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 | /// We store a vector of the slices formed by uses of the alloca here. This |
| 322 | /// vector is sorted by increasing begin offset, and then the unsplittable |
| 323 | /// slices before the splittable ones. See the Slice inner class for more |
| 324 | /// details. |
| 325 | SmallVector<Slice, 8> Slices; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 326 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 327 | /// Instructions which will become dead if we rewrite the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 328 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 329 | /// Note that these are not separated by slice. This is because we expect an |
| 330 | /// alloca to be completely rewritten or not rewritten at all. If rewritten, |
| 331 | /// all these instructions can simply be removed and replaced with undef as |
| 332 | /// they come from outside of the allocated space. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 333 | SmallVector<Instruction *, 8> DeadUsers; |
| 334 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 335 | /// Operands which will become dead if we rewrite the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 336 | /// |
| 337 | /// These are operands that in their particular use can be replaced with |
| 338 | /// undef when we rewrite the alloca. These show up in out-of-bounds inputs |
| 339 | /// to PHI nodes and the like. They aren't entirely dead (there might be |
| 340 | /// a GEP back into the bounds using it elsewhere) and nor is the PHI, but we |
| 341 | /// want to swap this particular input for undef to simplify the use lists of |
| 342 | /// the alloca. |
| 343 | SmallVector<Use *, 8> DeadOperands; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 344 | }; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 345 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 346 | /// A partition of the slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 347 | /// |
| 348 | /// An ephemeral representation for a range of slices which can be viewed as |
| 349 | /// a partition of the alloca. This range represents a span of the alloca's |
| 350 | /// memory which cannot be split, and provides access to all of the slices |
| 351 | /// overlapping some part of the partition. |
| 352 | /// |
| 353 | /// Objects of this type are produced by traversing the alloca's slices, but |
| 354 | /// are only ephemeral and not persistent. |
| 355 | class llvm::sroa::Partition { |
| 356 | private: |
| 357 | friend class AllocaSlices; |
| 358 | friend class AllocaSlices::partition_iterator; |
| 359 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 360 | using iterator = AllocaSlices::iterator; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 361 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 362 | /// The beginning and ending offsets of the alloca for this |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 363 | /// partition. |
| 364 | uint64_t BeginOffset, EndOffset; |
| 365 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 366 | /// The start and end iterators of this partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 367 | iterator SI, SJ; |
| 368 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 369 | /// A collection of split slice tails overlapping the partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 370 | SmallVector<Slice *, 4> SplitTails; |
| 371 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 372 | /// Raw constructor builds an empty partition starting and ending at |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 373 | /// the given iterator. |
| 374 | Partition(iterator SI) : SI(SI), SJ(SI) {} |
| 375 | |
| 376 | public: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 377 | /// The start offset of this partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 378 | /// |
| 379 | /// All of the contained slices start at or after this offset. |
| 380 | uint64_t beginOffset() const { return BeginOffset; } |
| 381 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 382 | /// The end offset of this partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 383 | /// |
| 384 | /// All of the contained slices end at or before this offset. |
| 385 | uint64_t endOffset() const { return EndOffset; } |
| 386 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 387 | /// The size of the partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 388 | /// |
| 389 | /// Note that this can never be zero. |
| 390 | uint64_t size() const { |
| 391 | assert(BeginOffset < EndOffset && "Partitions must span some bytes!"); |
| 392 | return EndOffset - BeginOffset; |
| 393 | } |
| 394 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 395 | /// Test whether this partition contains no slices, and merely spans |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 396 | /// a region occupied by split slices. |
| 397 | bool empty() const { return SI == SJ; } |
| 398 | |
| 399 | /// \name Iterate slices that start within the partition. |
| 400 | /// These may be splittable or unsplittable. They have a begin offset >= the |
| 401 | /// partition begin offset. |
| 402 | /// @{ |
| 403 | // FIXME: We should probably define a "concat_iterator" helper and use that |
| 404 | // to stitch together pointee_iterators over the split tails and the |
| 405 | // contiguous iterators of the partition. That would give a much nicer |
| 406 | // interface here. We could then additionally expose filtered iterators for |
| 407 | // split, unsplit, and unsplittable splices based on the usage patterns. |
| 408 | iterator begin() const { return SI; } |
| 409 | iterator end() const { return SJ; } |
| 410 | /// @} |
| 411 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 412 | /// Get the sequence of split slice tails. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 413 | /// |
| 414 | /// These tails are of slices which start before this partition but are |
| 415 | /// split and overlap into the partition. We accumulate these while forming |
| 416 | /// partitions. |
| 417 | ArrayRef<Slice *> splitSliceTails() const { return SplitTails; } |
| 418 | }; |
| 419 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 420 | /// An iterator over partitions of the alloca's slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 421 | /// |
| 422 | /// This iterator implements the core algorithm for partitioning the alloca's |
| 423 | /// slices. It is a forward iterator as we don't support backtracking for |
| 424 | /// efficiency reasons, and re-use a single storage area to maintain the |
| 425 | /// current set of split slices. |
| 426 | /// |
| 427 | /// It is templated on the slice iterator type to use so that it can operate |
| 428 | /// with either const or non-const slice iterators. |
| 429 | class AllocaSlices::partition_iterator |
| 430 | : public iterator_facade_base<partition_iterator, std::forward_iterator_tag, |
| 431 | Partition> { |
| 432 | friend class AllocaSlices; |
| 433 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 434 | /// Most of the state for walking the partitions is held in a class |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 435 | /// with a nice interface for examining them. |
| 436 | Partition P; |
| 437 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 438 | /// We need to keep the end of the slices to know when to stop. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 439 | AllocaSlices::iterator SE; |
| 440 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 441 | /// We also need to keep track of the maximum split end offset seen. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 442 | /// FIXME: Do we really? |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 443 | uint64_t MaxSplitSliceEndOffset = 0; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 444 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 445 | /// Sets the partition to be empty at given iterator, and sets the |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 446 | /// end iterator. |
| 447 | partition_iterator(AllocaSlices::iterator SI, AllocaSlices::iterator SE) |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 448 | : P(SI), SE(SE) { |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 449 | // If not already at the end, advance our state to form the initial |
| 450 | // partition. |
| 451 | if (SI != SE) |
| 452 | advance(); |
| 453 | } |
| 454 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 455 | /// Advance the iterator to the next partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 456 | /// |
| 457 | /// Requires that the iterator not be at the end of the slices. |
| 458 | void advance() { |
| 459 | assert((P.SI != SE || !P.SplitTails.empty()) && |
| 460 | "Cannot advance past the end of the slices!"); |
| 461 | |
| 462 | // Clear out any split uses which have ended. |
| 463 | if (!P.SplitTails.empty()) { |
| 464 | if (P.EndOffset >= MaxSplitSliceEndOffset) { |
| 465 | // If we've finished all splits, this is easy. |
| 466 | P.SplitTails.clear(); |
| 467 | MaxSplitSliceEndOffset = 0; |
| 468 | } else { |
| 469 | // Remove the uses which have ended in the prior partition. This |
| 470 | // cannot change the max split slice end because we just checked that |
| 471 | // the prior partition ended prior to that max. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 472 | P.SplitTails.erase(llvm::remove_if(P.SplitTails, |
| 473 | [&](Slice *S) { |
| 474 | return S->endOffset() <= |
| 475 | P.EndOffset; |
| 476 | }), |
| 477 | P.SplitTails.end()); |
| 478 | assert(llvm::any_of(P.SplitTails, |
| 479 | [&](Slice *S) { |
| 480 | return S->endOffset() == MaxSplitSliceEndOffset; |
| 481 | }) && |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 482 | "Could not find the current max split slice offset!"); |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 483 | assert(llvm::all_of(P.SplitTails, |
| 484 | [&](Slice *S) { |
| 485 | return S->endOffset() <= MaxSplitSliceEndOffset; |
| 486 | }) && |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 487 | "Max split slice end offset is not actually the max!"); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // If P.SI is already at the end, then we've cleared the split tail and |
| 492 | // now have an end iterator. |
| 493 | if (P.SI == SE) { |
| 494 | assert(P.SplitTails.empty() && "Failed to clear the split slices!"); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | // If we had a non-empty partition previously, set up the state for |
| 499 | // subsequent partitions. |
| 500 | if (P.SI != P.SJ) { |
| 501 | // Accumulate all the splittable slices which started in the old |
| 502 | // partition into the split list. |
| 503 | for (Slice &S : P) |
| 504 | if (S.isSplittable() && S.endOffset() > P.EndOffset) { |
| 505 | P.SplitTails.push_back(&S); |
| 506 | MaxSplitSliceEndOffset = |
| 507 | std::max(S.endOffset(), MaxSplitSliceEndOffset); |
| 508 | } |
| 509 | |
| 510 | // Start from the end of the previous partition. |
| 511 | P.SI = P.SJ; |
| 512 | |
| 513 | // If P.SI is now at the end, we at most have a tail of split slices. |
| 514 | if (P.SI == SE) { |
| 515 | P.BeginOffset = P.EndOffset; |
| 516 | P.EndOffset = MaxSplitSliceEndOffset; |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | // If the we have split slices and the next slice is after a gap and is |
| 521 | // not splittable immediately form an empty partition for the split |
| 522 | // slices up until the next slice begins. |
| 523 | if (!P.SplitTails.empty() && P.SI->beginOffset() != P.EndOffset && |
| 524 | !P.SI->isSplittable()) { |
| 525 | P.BeginOffset = P.EndOffset; |
| 526 | P.EndOffset = P.SI->beginOffset(); |
| 527 | return; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // OK, we need to consume new slices. Set the end offset based on the |
| 532 | // current slice, and step SJ past it. The beginning offset of the |
| 533 | // partition is the beginning offset of the next slice unless we have |
| 534 | // pre-existing split slices that are continuing, in which case we begin |
| 535 | // at the prior end offset. |
| 536 | P.BeginOffset = P.SplitTails.empty() ? P.SI->beginOffset() : P.EndOffset; |
| 537 | P.EndOffset = P.SI->endOffset(); |
| 538 | ++P.SJ; |
| 539 | |
| 540 | // There are two strategies to form a partition based on whether the |
| 541 | // partition starts with an unsplittable slice or a splittable slice. |
| 542 | if (!P.SI->isSplittable()) { |
| 543 | // When we're forming an unsplittable region, it must always start at |
| 544 | // the first slice and will extend through its end. |
| 545 | assert(P.BeginOffset == P.SI->beginOffset()); |
| 546 | |
| 547 | // Form a partition including all of the overlapping slices with this |
| 548 | // unsplittable slice. |
| 549 | while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) { |
| 550 | if (!P.SJ->isSplittable()) |
| 551 | P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset()); |
| 552 | ++P.SJ; |
| 553 | } |
| 554 | |
| 555 | // We have a partition across a set of overlapping unsplittable |
| 556 | // partitions. |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | // If we're starting with a splittable slice, then we need to form |
| 561 | // a synthetic partition spanning it and any other overlapping splittable |
| 562 | // splices. |
| 563 | assert(P.SI->isSplittable() && "Forming a splittable partition!"); |
| 564 | |
| 565 | // Collect all of the overlapping splittable slices. |
| 566 | while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset && |
| 567 | P.SJ->isSplittable()) { |
| 568 | P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset()); |
| 569 | ++P.SJ; |
| 570 | } |
| 571 | |
| 572 | // Back upiP.EndOffset if we ended the span early when encountering an |
| 573 | // unsplittable slice. This synthesizes the early end offset of |
| 574 | // a partition spanning only splittable slices. |
| 575 | if (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) { |
| 576 | assert(!P.SJ->isSplittable()); |
| 577 | P.EndOffset = P.SJ->beginOffset(); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | public: |
| 582 | bool operator==(const partition_iterator &RHS) const { |
| 583 | assert(SE == RHS.SE && |
| 584 | "End iterators don't match between compared partition iterators!"); |
| 585 | |
| 586 | // The observed positions of partitions is marked by the P.SI iterator and |
| 587 | // the emptiness of the split slices. The latter is only relevant when |
| 588 | // P.SI == SE, as the end iterator will additionally have an empty split |
| 589 | // slices list, but the prior may have the same P.SI and a tail of split |
| 590 | // slices. |
| 591 | if (P.SI == RHS.P.SI && P.SplitTails.empty() == RHS.P.SplitTails.empty()) { |
| 592 | assert(P.SJ == RHS.P.SJ && |
| 593 | "Same set of slices formed two different sized partitions!"); |
| 594 | assert(P.SplitTails.size() == RHS.P.SplitTails.size() && |
| 595 | "Same slice position with differently sized non-empty split " |
| 596 | "slice tails!"); |
| 597 | return true; |
| 598 | } |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | partition_iterator &operator++() { |
| 603 | advance(); |
| 604 | return *this; |
| 605 | } |
| 606 | |
| 607 | Partition &operator*() { return P; } |
| 608 | }; |
| 609 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 610 | /// A forward range over the partitions of the alloca's slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 611 | /// |
| 612 | /// This accesses an iterator range over the partitions of the alloca's |
| 613 | /// slices. It computes these partitions on the fly based on the overlapping |
| 614 | /// offsets of the slices and the ability to split them. It will visit "empty" |
| 615 | /// partitions to cover regions of the alloca only accessed via split |
| 616 | /// slices. |
| 617 | iterator_range<AllocaSlices::partition_iterator> AllocaSlices::partitions() { |
| 618 | return make_range(partition_iterator(begin(), end()), |
| 619 | partition_iterator(end(), end())); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 620 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 621 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 622 | static Value *foldSelectInst(SelectInst &SI) { |
| 623 | // If the condition being selected on is a constant or the same value is |
| 624 | // being selected between, fold the select. Yes this does (rarely) happen |
| 625 | // early on. |
| 626 | if (ConstantInt *CI = dyn_cast<ConstantInt>(SI.getCondition())) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 627 | return SI.getOperand(1 + CI->isZero()); |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 628 | if (SI.getOperand(1) == SI.getOperand(2)) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 629 | return SI.getOperand(1); |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 630 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 631 | return nullptr; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 632 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 633 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 634 | /// A helper that folds a PHI node or a select. |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 635 | static Value *foldPHINodeOrSelectInst(Instruction &I) { |
| 636 | if (PHINode *PN = dyn_cast<PHINode>(&I)) { |
| 637 | // If PN merges together the same value, return that value. |
| 638 | return PN->hasConstantValue(); |
| 639 | } |
| 640 | return foldSelectInst(cast<SelectInst>(I)); |
| 641 | } |
| 642 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 643 | /// Builder for the alloca slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 644 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 645 | /// This class builds a set of alloca slices by recursively visiting the uses |
| 646 | /// of an alloca and making a slice for each load and store at each offset. |
| 647 | class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> { |
| 648 | friend class PtrUseVisitor<SliceBuilder>; |
| 649 | friend class InstVisitor<SliceBuilder>; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 650 | |
| 651 | using Base = PtrUseVisitor<SliceBuilder>; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 652 | |
| 653 | const uint64_t AllocSize; |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 654 | AllocaSlices &AS; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 655 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 656 | SmallDenseMap<Instruction *, unsigned> MemTransferSliceMap; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 657 | SmallDenseMap<Instruction *, uint64_t> PHIOrSelectSizes; |
| 658 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 659 | /// Set to de-duplicate dead instructions found in the use walk. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 660 | SmallPtrSet<Instruction *, 4> VisitedDeadInsts; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 661 | |
| 662 | public: |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 663 | SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 664 | : PtrUseVisitor<SliceBuilder>(DL), |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 665 | AllocSize(DL.getTypeAllocSize(AI.getAllocatedType())), AS(AS) {} |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 666 | |
| 667 | private: |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 668 | void markAsDead(Instruction &I) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 669 | if (VisitedDeadInsts.insert(&I).second) |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 670 | AS.DeadUsers.push_back(&I); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 673 | void insertUse(Instruction &I, const APInt &Offset, uint64_t Size, |
Chandler Carruth | 9712117 | 2012-09-16 19:39:50 +0000 | [diff] [blame] | 674 | bool IsSplittable = false) { |
Chandler Carruth | f02b8bf | 2012-12-03 10:59:55 +0000 | [diff] [blame] | 675 | // Completely skip uses which have a zero size or start either before or |
| 676 | // past the end of the allocation. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 677 | if (Size == 0 || Offset.uge(AllocSize)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 678 | LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte use @" |
| 679 | << Offset |
| 680 | << " which has zero size or starts outside of the " |
| 681 | << AllocSize << " byte alloca:\n" |
| 682 | << " alloca: " << AS.AI << "\n" |
| 683 | << " use: " << I << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 684 | return markAsDead(I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 687 | uint64_t BeginOffset = Offset.getZExtValue(); |
| 688 | uint64_t EndOffset = BeginOffset + Size; |
Chandler Carruth | e7a1ba5 | 2012-09-23 11:43:14 +0000 | [diff] [blame] | 689 | |
| 690 | // Clamp the end offset to the end of the allocation. Note that this is |
| 691 | // formulated to handle even the case where "BeginOffset + Size" overflows. |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 692 | // This may appear superficially to be something we could ignore entirely, |
| 693 | // but that is not so! There may be widened loads or PHI-node uses where |
| 694 | // some instructions are dead but not others. We can't completely ignore |
| 695 | // them, and so have to record at least the information here. |
Chandler Carruth | e7a1ba5 | 2012-09-23 11:43:14 +0000 | [diff] [blame] | 696 | assert(AllocSize >= BeginOffset); // Established above. |
| 697 | if (Size > AllocSize - BeginOffset) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 698 | LLVM_DEBUG(dbgs() << "WARNING: Clamping a " << Size << " byte use @" |
| 699 | << Offset << " to remain within the " << AllocSize |
| 700 | << " byte alloca:\n" |
| 701 | << " alloca: " << AS.AI << "\n" |
| 702 | << " use: " << I << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 703 | EndOffset = AllocSize; |
| 704 | } |
| 705 | |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 706 | AS.Slices.push_back(Slice(BeginOffset, EndOffset, U, IsSplittable)); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void visitBitCastInst(BitCastInst &BC) { |
| 710 | if (BC.use_empty()) |
| 711 | return markAsDead(BC); |
| 712 | |
| 713 | return Base::visitBitCastInst(BC); |
| 714 | } |
| 715 | |
| 716 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 717 | if (GEPI.use_empty()) |
| 718 | return markAsDead(GEPI); |
| 719 | |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 720 | if (SROAStrictInbounds && GEPI.isInBounds()) { |
| 721 | // FIXME: This is a manually un-factored variant of the basic code inside |
| 722 | // of GEPs with checking of the inbounds invariant specified in the |
| 723 | // langref in a very strict sense. If we ever want to enable |
| 724 | // SROAStrictInbounds, this code should be factored cleanly into |
| 725 | // PtrUseVisitor, but it is easier to experiment with SROAStrictInbounds |
Hal Finkel | 5c83a09 | 2016-03-28 11:23:21 +0000 | [diff] [blame] | 726 | // by writing out the code here where we have the underlying allocation |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 727 | // size readily available. |
| 728 | APInt GEPOffset = Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 729 | const DataLayout &DL = GEPI.getModule()->getDataLayout(); |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 730 | for (gep_type_iterator GTI = gep_type_begin(GEPI), |
| 731 | GTE = gep_type_end(GEPI); |
| 732 | GTI != GTE; ++GTI) { |
| 733 | ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); |
| 734 | if (!OpC) |
| 735 | break; |
| 736 | |
| 737 | // Handle a struct index, which adds its field offset to the pointer. |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 738 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 739 | unsigned ElementIdx = OpC->getZExtValue(); |
| 740 | const StructLayout *SL = DL.getStructLayout(STy); |
| 741 | GEPOffset += |
| 742 | APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)); |
| 743 | } else { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 744 | // For array or vector indices, scale the index by the size of the |
| 745 | // type. |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 746 | APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth()); |
| 747 | GEPOffset += Index * APInt(Offset.getBitWidth(), |
| 748 | DL.getTypeAllocSize(GTI.getIndexedType())); |
| 749 | } |
| 750 | |
| 751 | // If this index has computed an intermediate pointer which is not |
| 752 | // inbounds, then the result of the GEP is a poison value and we can |
| 753 | // delete it and all uses. |
| 754 | if (GEPOffset.ugt(AllocSize)) |
| 755 | return markAsDead(GEPI); |
| 756 | } |
| 757 | } |
| 758 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 759 | return Base::visitGetElementPtrInst(GEPI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 762 | void handleLoadOrStore(Type *Ty, Instruction &I, const APInt &Offset, |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 763 | uint64_t Size, bool IsVolatile) { |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 764 | // We allow splitting of non-volatile loads and stores where the type is an |
| 765 | // integer type. These may be used to implement 'memcpy' or other "transfer |
| 766 | // of bits" patterns. |
| 767 | bool IsSplittable = Ty->isIntegerTy() && !IsVolatile; |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 768 | |
| 769 | insertUse(I, Offset, Size, IsSplittable); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 772 | void visitLoadInst(LoadInst &LI) { |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 773 | assert((!LI.isSimple() || LI.getType()->isSingleValueType()) && |
| 774 | "All simple FCA loads should have been pre-split"); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 775 | |
| 776 | if (!IsOffsetKnown) |
| 777 | return PI.setAborted(&LI); |
| 778 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 779 | const DataLayout &DL = LI.getModule()->getDataLayout(); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 780 | uint64_t Size = DL.getTypeStoreSize(LI.getType()); |
| 781 | return handleLoadOrStore(LI.getType(), LI, Offset, Size, LI.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 784 | void visitStoreInst(StoreInst &SI) { |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 785 | Value *ValOp = SI.getValueOperand(); |
| 786 | if (ValOp == *U) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 787 | return PI.setEscapedAndAborted(&SI); |
| 788 | if (!IsOffsetKnown) |
| 789 | return PI.setAborted(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 790 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 791 | const DataLayout &DL = SI.getModule()->getDataLayout(); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 792 | uint64_t Size = DL.getTypeStoreSize(ValOp->getType()); |
| 793 | |
| 794 | // If this memory access can be shown to *statically* extend outside the |
Hiroshi Inoue | 0909ca1 | 2018-01-26 08:15:29 +0000 | [diff] [blame] | 795 | // bounds of the allocation, it's behavior is undefined, so simply |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 796 | // ignore it. Note that this is more strict than the generic clamping |
| 797 | // behavior of insertUse. We also try to handle cases which might run the |
| 798 | // risk of overflow. |
| 799 | // FIXME: We should instead consider the pointer to have escaped if this |
| 800 | // function is being instrumented for addressing bugs or race conditions. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 801 | if (Size > AllocSize || Offset.ugt(AllocSize - Size)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 802 | LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte store @" |
| 803 | << Offset << " which extends past the end of the " |
| 804 | << AllocSize << " byte alloca:\n" |
| 805 | << " alloca: " << AS.AI << "\n" |
| 806 | << " use: " << SI << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 807 | return markAsDead(SI); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 810 | assert((!SI.isSimple() || ValOp->getType()->isSingleValueType()) && |
| 811 | "All simple FCA stores should have been pre-split"); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 812 | handleLoadOrStore(ValOp->getType(), SI, Offset, Size, SI.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 815 | void visitMemSetInst(MemSetInst &II) { |
Chandler Carruth | b0de6dd | 2012-09-14 10:26:34 +0000 | [diff] [blame] | 816 | assert(II.getRawDest() == *U && "Pointer use is not the destination?"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 817 | ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength()); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 818 | if ((Length && Length->getValue() == 0) || |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 819 | (IsOffsetKnown && Offset.uge(AllocSize))) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 820 | // Zero-length mem transfer intrinsics can be ignored entirely. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 821 | return markAsDead(II); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 822 | |
| 823 | if (!IsOffsetKnown) |
| 824 | return PI.setAborted(&II); |
| 825 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 826 | insertUse(II, Offset, Length ? Length->getLimitedValue() |
| 827 | : AllocSize - Offset.getLimitedValue(), |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 828 | (bool)Length); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 831 | void visitMemTransferInst(MemTransferInst &II) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 832 | ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength()); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 833 | if (Length && Length->getValue() == 0) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 834 | // Zero-length mem transfer intrinsics can be ignored entirely. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 835 | return markAsDead(II); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 836 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 837 | // Because we can visit these intrinsics twice, also check to see if the |
| 838 | // first time marked this instruction as dead. If so, skip it. |
| 839 | if (VisitedDeadInsts.count(&II)) |
| 840 | return; |
| 841 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 842 | if (!IsOffsetKnown) |
| 843 | return PI.setAborted(&II); |
| 844 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 845 | // This side of the transfer is completely out-of-bounds, and so we can |
| 846 | // nuke the entire transfer. However, we also need to nuke the other side |
| 847 | // if already added to our partitions. |
| 848 | // FIXME: Yet another place we really should bypass this when |
| 849 | // instrumenting for ASan. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 850 | if (Offset.uge(AllocSize)) { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 851 | SmallDenseMap<Instruction *, unsigned>::iterator MTPI = |
| 852 | MemTransferSliceMap.find(&II); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 853 | if (MTPI != MemTransferSliceMap.end()) |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 854 | AS.Slices[MTPI->second].kill(); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 855 | return markAsDead(II); |
| 856 | } |
| 857 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 858 | uint64_t RawOffset = Offset.getLimitedValue(); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 859 | uint64_t Size = Length ? Length->getLimitedValue() : AllocSize - RawOffset; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 860 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 861 | // Check for the special case where the same exact value is used for both |
| 862 | // source and dest. |
| 863 | if (*U == II.getRawDest() && *U == II.getRawSource()) { |
| 864 | // For non-volatile transfers this is a no-op. |
| 865 | if (!II.isVolatile()) |
| 866 | return markAsDead(II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 867 | |
Nick Lewycky | 6ab9d93 | 2013-07-22 23:38:27 +0000 | [diff] [blame] | 868 | return insertUse(II, Offset, Size, /*IsSplittable=*/false); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 869 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 870 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 871 | // If we have seen both source and destination for a mem transfer, then |
| 872 | // they both point to the same alloca. |
| 873 | bool Inserted; |
| 874 | SmallDenseMap<Instruction *, unsigned>::iterator MTPI; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 875 | std::tie(MTPI, Inserted) = |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 876 | MemTransferSliceMap.insert(std::make_pair(&II, AS.Slices.size())); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 877 | unsigned PrevIdx = MTPI->second; |
| 878 | if (!Inserted) { |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 879 | Slice &PrevP = AS.Slices[PrevIdx]; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 880 | |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 881 | // Check if the begin offsets match and this is a non-volatile transfer. |
| 882 | // In that case, we can completely elide the transfer. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 883 | if (!II.isVolatile() && PrevP.beginOffset() == RawOffset) { |
| 884 | PrevP.kill(); |
| 885 | return markAsDead(II); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // Otherwise we have an offset transfer within the same alloca. We can't |
| 889 | // split those. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 890 | PrevP.makeUnsplittable(); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 893 | // Insert the use now that we've fixed up the splittable nature. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 894 | insertUse(II, Offset, Size, /*IsSplittable=*/Inserted && Length); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 895 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 896 | // Check that we ended up with a valid index in the map. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 897 | assert(AS.Slices[PrevIdx].getUse()->getUser() == &II && |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 898 | "Map index doesn't point back to a slice with this user."); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // Disable SRoA for any intrinsics except for lifetime invariants. |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 902 | // FIXME: What about debug intrinsics? This matches old behavior, but |
Chandler Carruth | 4b40e00 | 2012-09-14 10:26:36 +0000 | [diff] [blame] | 903 | // doesn't make sense. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 904 | void visitIntrinsicInst(IntrinsicInst &II) { |
| 905 | if (!IsOffsetKnown) |
| 906 | return PI.setAborted(&II); |
| 907 | |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 908 | if (II.isLifetimeStartOrEnd()) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 909 | ConstantInt *Length = cast<ConstantInt>(II.getArgOperand(0)); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 910 | uint64_t Size = std::min(AllocSize - Offset.getLimitedValue(), |
| 911 | Length->getLimitedValue()); |
Chandler Carruth | 9712117 | 2012-09-16 19:39:50 +0000 | [diff] [blame] | 912 | insertUse(II, Offset, Size, true); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 913 | return; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 916 | Base::visitIntrinsicInst(II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | Instruction *hasUnsafePHIOrSelectUse(Instruction *Root, uint64_t &Size) { |
| 920 | // 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] | 921 | // 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] | 922 | // are considered unsplittable and the size is the maximum loaded or stored |
| 923 | // size. |
| 924 | SmallPtrSet<Instruction *, 4> Visited; |
| 925 | SmallVector<std::pair<Instruction *, Instruction *>, 4> Uses; |
| 926 | Visited.insert(Root); |
| 927 | Uses.push_back(std::make_pair(cast<Instruction>(*U), Root)); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 928 | const DataLayout &DL = Root->getModule()->getDataLayout(); |
Chandler Carruth | 8b907e8 | 2012-09-25 10:03:40 +0000 | [diff] [blame] | 929 | // If there are no loads or stores, the access is dead. We mark that as |
| 930 | // a size zero access. |
| 931 | Size = 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 932 | do { |
| 933 | Instruction *I, *UsedI; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 934 | std::tie(UsedI, I) = Uses.pop_back_val(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 935 | |
| 936 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 937 | Size = std::max(Size, DL.getTypeStoreSize(LI->getType())); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 938 | continue; |
| 939 | } |
| 940 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 941 | Value *Op = SI->getOperand(0); |
| 942 | if (Op == UsedI) |
| 943 | return SI; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 944 | Size = std::max(Size, DL.getTypeStoreSize(Op->getType())); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 945 | continue; |
| 946 | } |
| 947 | |
| 948 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 949 | if (!GEP->hasAllZeroIndices()) |
| 950 | return GEP; |
| 951 | } else if (!isa<BitCastInst>(I) && !isa<PHINode>(I) && |
| 952 | !isa<SelectInst>(I)) { |
| 953 | return I; |
| 954 | } |
| 955 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 956 | for (User *U : I->users()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 957 | if (Visited.insert(cast<Instruction>(U)).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 958 | Uses.push_back(std::make_pair(I, cast<Instruction>(U))); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 959 | } while (!Uses.empty()); |
| 960 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 961 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 964 | void visitPHINodeOrSelectInst(Instruction &I) { |
| 965 | assert(isa<PHINode>(I) || isa<SelectInst>(I)); |
| 966 | if (I.use_empty()) |
| 967 | return markAsDead(I); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 968 | |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 969 | // TODO: We could use SimplifyInstruction here to fold PHINodes and |
| 970 | // SelectInsts. However, doing so requires to change the current |
| 971 | // dead-operand-tracking mechanism. For instance, suppose neither loading |
| 972 | // from %U nor %other traps. Then "load (select undef, %U, %other)" does not |
| 973 | // trap either. However, if we simply replace %U with undef using the |
| 974 | // current dead-operand-tracking mechanism, "load (select undef, undef, |
| 975 | // %other)" may trap because the select may return the first operand |
| 976 | // "undef". |
| 977 | if (Value *Result = foldPHINodeOrSelectInst(I)) { |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 978 | if (Result == *U) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 979 | // If the result of the constant fold will be the pointer, recurse |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 980 | // through the PHI/select as if we had RAUW'ed it. |
| 981 | enqueueUsers(I); |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 982 | else |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 983 | // Otherwise the operand to the PHI/select is dead, and we can replace |
| 984 | // it with undef. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 985 | AS.DeadOperands.push_back(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 986 | |
| 987 | return; |
| 988 | } |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 989 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 990 | if (!IsOffsetKnown) |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 991 | return PI.setAborted(&I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 992 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 993 | // See if we already have computed info on this node. |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 994 | uint64_t &Size = PHIOrSelectSizes[&I]; |
| 995 | if (!Size) { |
| 996 | // This is a new PHI/Select, check for an unsafe use of it. |
| 997 | if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&I, Size)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 998 | return PI.setAborted(UnsafeI); |
| 999 | } |
| 1000 | |
| 1001 | // For PHI and select operands outside the alloca, we can't nuke the entire |
| 1002 | // phi or select -- the other side might still be relevant, so we special |
| 1003 | // case them here and use a separate structure to track the operands |
| 1004 | // themselves which should be replaced with undef. |
| 1005 | // FIXME: This should instead be escaped in the event we're instrumenting |
| 1006 | // for address sanitization. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 1007 | if (Offset.uge(AllocSize)) { |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 1008 | AS.DeadOperands.push_back(U); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1009 | return; |
| 1010 | } |
| 1011 | |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1012 | insertUse(I, Offset, Size); |
| 1013 | } |
| 1014 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1015 | void visitPHINode(PHINode &PN) { visitPHINodeOrSelectInst(PN); } |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1016 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1017 | void visitSelectInst(SelectInst &SI) { visitPHINodeOrSelectInst(SI); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1018 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1019 | /// Disable SROA entirely if there are unhandled users of the alloca. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1020 | void visitInstruction(Instruction &I) { PI.setAborted(&I); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1021 | }; |
| 1022 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1023 | AllocaSlices::AllocaSlices(const DataLayout &DL, AllocaInst &AI) |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1024 | : |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1025 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1026 | AI(AI), |
| 1027 | #endif |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1028 | PointerEscapingInstr(nullptr) { |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1029 | SliceBuilder PB(DL, AI, *this); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1030 | SliceBuilder::PtrInfo PtrI = PB.visitPtr(AI); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 1031 | if (PtrI.isEscaped() || PtrI.isAborted()) { |
| 1032 | // 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] | 1033 | // possibly by just storing the PtrInfo in the AllocaSlices. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 1034 | PointerEscapingInstr = PtrI.getEscapingInst() ? PtrI.getEscapingInst() |
| 1035 | : PtrI.getAbortingInst(); |
| 1036 | assert(PointerEscapingInstr && "Did not track a bad instruction"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1037 | return; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 1038 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1039 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 1040 | Slices.erase( |
| 1041 | llvm::remove_if(Slices, [](const Slice &S) { return S.isDead(); }), |
| 1042 | Slices.end()); |
Benjamin Kramer | 08e5070 | 2013-07-20 08:38:34 +0000 | [diff] [blame] | 1043 | |
Hal Finkel | 29f5131 | 2016-03-28 11:13:03 +0000 | [diff] [blame] | 1044 | #ifndef NDEBUG |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 1045 | if (SROARandomShuffleSlices) { |
Pavel Labath | c207bec | 2016-11-09 12:07:12 +0000 | [diff] [blame] | 1046 | std::mt19937 MT(static_cast<unsigned>( |
| 1047 | std::chrono::system_clock::now().time_since_epoch().count())); |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 1048 | std::shuffle(Slices.begin(), Slices.end(), MT); |
| 1049 | } |
| 1050 | #endif |
| 1051 | |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 1052 | // Sort the uses. This arranges for the offsets to be in ascending order, |
| 1053 | // and the sizes to be in descending order. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1054 | llvm::sort(Slices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1057 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 1058 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1059 | void AllocaSlices::print(raw_ostream &OS, const_iterator I, |
| 1060 | StringRef Indent) const { |
| 1061 | printSlice(OS, I, Indent); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 1062 | OS << "\n"; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1063 | printUse(OS, I, Indent); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1066 | void AllocaSlices::printSlice(raw_ostream &OS, const_iterator I, |
| 1067 | StringRef Indent) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1068 | OS << Indent << "[" << I->beginOffset() << "," << I->endOffset() << ")" |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1069 | << " slice #" << (I - begin()) |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 1070 | << (I->isSplittable() ? " (splittable)" : ""); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1073 | void AllocaSlices::printUse(raw_ostream &OS, const_iterator I, |
| 1074 | StringRef Indent) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1075 | OS << Indent << " used by: " << *I->getUse()->getUser() << "\n"; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1078 | void AllocaSlices::print(raw_ostream &OS) const { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1079 | if (PointerEscapingInstr) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1080 | OS << "Can't analyze slices for alloca: " << AI << "\n" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1081 | << " A pointer to this alloca escaped by:\n" |
| 1082 | << " " << *PointerEscapingInstr << "\n"; |
| 1083 | return; |
| 1084 | } |
| 1085 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1086 | OS << "Slices of alloca: " << AI << "\n"; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1087 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1088 | print(OS, I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Alp Toker | f929e09 | 2014-01-04 22:47:48 +0000 | [diff] [blame] | 1091 | LLVM_DUMP_METHOD void AllocaSlices::dump(const_iterator I) const { |
| 1092 | print(dbgs(), I); |
| 1093 | } |
| 1094 | LLVM_DUMP_METHOD void AllocaSlices::dump() const { print(dbgs()); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1095 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1096 | #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 1097 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1098 | /// Walk the range of a partitioning looking for a common type to cover this |
| 1099 | /// sequence of slices. |
| 1100 | static Type *findCommonType(AllocaSlices::const_iterator B, |
| 1101 | AllocaSlices::const_iterator E, |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1102 | uint64_t EndOffset) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1103 | Type *Ty = nullptr; |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1104 | bool TyIsCommon = true; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1105 | IntegerType *ITy = nullptr; |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Note that we need to look at *every* alloca slice's Use to ensure we |
| 1108 | // always get consistent results regardless of the order of slices. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1109 | for (AllocaSlices::const_iterator I = B; I != E; ++I) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1110 | Use *U = I->getUse(); |
| 1111 | if (isa<IntrinsicInst>(*U->getUser())) |
| 1112 | continue; |
| 1113 | if (I->beginOffset() != B->beginOffset() || I->endOffset() != EndOffset) |
| 1114 | continue; |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1115 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1116 | Type *UserTy = nullptr; |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 1117 | if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1118 | UserTy = LI->getType(); |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 1119 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1120 | UserTy = SI->getValueOperand()->getType(); |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 1121 | } |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1122 | |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1123 | if (IntegerType *UserITy = dyn_cast_or_null<IntegerType>(UserTy)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1124 | // 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] | 1125 | // 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] | 1126 | // entity causing the split. Also skip if the type is not a byte width |
| 1127 | // multiple. |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1128 | if (UserITy->getBitWidth() % 8 != 0 || |
| 1129 | UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset())) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1130 | continue; |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1131 | |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1132 | // Track the largest bitwidth integer type used in this way in case there |
| 1133 | // is no common type. |
| 1134 | if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth()) |
| 1135 | ITy = UserITy; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1136 | } |
Duncan P. N. Exon Smith | 73686d3 | 2014-06-17 00:19:35 +0000 | [diff] [blame] | 1137 | |
| 1138 | // To avoid depending on the order of slices, Ty and TyIsCommon must not |
| 1139 | // depend on types skipped above. |
| 1140 | if (!UserTy || (Ty && Ty != UserTy)) |
| 1141 | TyIsCommon = false; // Give up on anything but an iN type. |
| 1142 | else |
| 1143 | Ty = UserTy; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1144 | } |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1145 | |
| 1146 | return TyIsCommon ? Ty : ITy; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1147 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1148 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1149 | /// PHI instructions that use an alloca and are subsequently loaded can be |
| 1150 | /// rewritten to load both input pointers in the pred blocks and then PHI the |
| 1151 | /// results, allowing the load of the alloca to be promoted. |
| 1152 | /// From this: |
| 1153 | /// %P2 = phi [i32* %Alloca, i32* %Other] |
| 1154 | /// %V = load i32* %P2 |
| 1155 | /// to: |
| 1156 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1157 | /// ... |
| 1158 | /// %V2 = load i32* %Other |
| 1159 | /// ... |
| 1160 | /// %V = phi [i32 %V1, i32 %V2] |
| 1161 | /// |
| 1162 | /// We can do this to a select if its only uses are loads and if the operands |
| 1163 | /// to the select can be loaded unconditionally. |
| 1164 | /// |
| 1165 | /// FIXME: This should be hoisted into a generic utility, likely in |
| 1166 | /// Transforms/Util/Local.h |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1167 | static bool isSafePHIToSpeculate(PHINode &PN) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1168 | // For now, we can only do this promotion if the load is in the same block |
| 1169 | // as the PHI, and if there are no stores between the phi and load. |
| 1170 | // TODO: Allow recursive phi users. |
| 1171 | // TODO: Allow stores. |
| 1172 | BasicBlock *BB = PN.getParent(); |
| 1173 | unsigned MaxAlign = 0; |
| 1174 | bool HaveLoad = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1175 | for (User *U : PN.users()) { |
| 1176 | LoadInst *LI = dyn_cast<LoadInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1177 | if (!LI || !LI->isSimple()) |
Chandler Carruth | e74ff4c | 2013-07-15 10:30:19 +0000 | [diff] [blame] | 1178 | return false; |
Chandler Carruth | e74ff4c | 2013-07-15 10:30:19 +0000 | [diff] [blame] | 1179 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1180 | // For now we only allow loads in the same block as the PHI. This is |
| 1181 | // a common case that happens when instcombine merges two loads through |
| 1182 | // a PHI. |
| 1183 | if (LI->getParent() != BB) |
| 1184 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1185 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1186 | // Ensure that there are no instructions between the PHI and the load that |
| 1187 | // could store. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1188 | for (BasicBlock::iterator BBI(PN); &*BBI != LI; ++BBI) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1189 | if (BBI->mayWriteToMemory()) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1190 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1191 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1192 | MaxAlign = std::max(MaxAlign, LI->getAlignment()); |
| 1193 | HaveLoad = true; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1196 | if (!HaveLoad) |
| 1197 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1198 | |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1199 | const DataLayout &DL = PN.getModule()->getDataLayout(); |
| 1200 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1201 | // We can only transform this if it is safe to push the loads into the |
| 1202 | // predecessor blocks. The only thing to watch out for is that we can't put |
| 1203 | // a possibly trapping load in the predecessor if it is a critical edge. |
| 1204 | for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 1205 | Instruction *TI = PN.getIncomingBlock(Idx)->getTerminator(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1206 | Value *InVal = PN.getIncomingValue(Idx); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1207 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1208 | // If the value is produced by the terminator of the predecessor (an |
| 1209 | // invoke) or it has side-effects, there is no valid place to put a load |
| 1210 | // in the predecessor. |
| 1211 | if (TI == InVal || TI->mayHaveSideEffects()) |
| 1212 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1213 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1214 | // If the predecessor has a single successor, then the edge isn't |
| 1215 | // critical. |
| 1216 | if (TI->getNumSuccessors() == 1) |
| 1217 | continue; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1218 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1219 | // If this pointer is always safe to load, or if we can prove that there |
| 1220 | // is already a load in the block, then we can move the load to the pred |
| 1221 | // block. |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1222 | if (isSafeToLoadUnconditionally(InVal, MaxAlign, DL, TI)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1223 | continue; |
| 1224 | |
| 1225 | return false; |
| 1226 | } |
| 1227 | |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
| 1231 | static void speculatePHINodeLoads(PHINode &PN) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1232 | LLVM_DEBUG(dbgs() << " original: " << PN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1233 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 1234 | LoadInst *SomeLoad = cast<LoadInst>(PN.user_back()); |
| 1235 | Type *LoadTy = SomeLoad->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1236 | IRBuilderTy PHIBuilder(&PN); |
| 1237 | PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(), |
| 1238 | PN.getName() + ".sroa.speculated"); |
| 1239 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1240 | // Get the AA tags and alignment to use from one of the loads. It doesn't |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1241 | // matter which one we get and if any differ. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1242 | AAMDNodes AATags; |
| 1243 | SomeLoad->getAAMetadata(AATags); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1244 | unsigned Align = SomeLoad->getAlignment(); |
| 1245 | |
| 1246 | // Rewrite all loads of the PN to use the new PHI. |
| 1247 | while (!PN.use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1248 | LoadInst *LI = cast<LoadInst>(PN.user_back()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1249 | LI->replaceAllUsesWith(NewPN); |
| 1250 | LI->eraseFromParent(); |
| 1251 | } |
| 1252 | |
| 1253 | // Inject loads into all of the pred blocks. |
Bjorn Pettersson | 81a76a3 | 2018-05-17 07:21:41 +0000 | [diff] [blame] | 1254 | DenseMap<BasicBlock*, Value*> InjectedLoads; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1255 | for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) { |
| 1256 | BasicBlock *Pred = PN.getIncomingBlock(Idx); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1257 | Value *InVal = PN.getIncomingValue(Idx); |
Bjorn Pettersson | 81a76a3 | 2018-05-17 07:21:41 +0000 | [diff] [blame] | 1258 | |
| 1259 | // A PHI node is allowed to have multiple (duplicated) entries for the same |
| 1260 | // basic block, as long as the value is the same. So if we already injected |
| 1261 | // a load in the predecessor, then we should reuse the same load for all |
| 1262 | // duplicated entries. |
| 1263 | if (Value* V = InjectedLoads.lookup(Pred)) { |
| 1264 | NewPN->addIncoming(V, Pred); |
| 1265 | continue; |
| 1266 | } |
| 1267 | |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 1268 | Instruction *TI = Pred->getTerminator(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1269 | IRBuilderTy PredBuilder(TI); |
| 1270 | |
| 1271 | LoadInst *Load = PredBuilder.CreateLoad( |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 1272 | LoadTy, InVal, |
| 1273 | (PN.getName() + ".sroa.speculate.load." + Pred->getName())); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1274 | ++NumLoadsSpeculated; |
| 1275 | Load->setAlignment(Align); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1276 | if (AATags) |
| 1277 | Load->setAAMetadata(AATags); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1278 | NewPN->addIncoming(Load, Pred); |
Bjorn Pettersson | 81a76a3 | 2018-05-17 07:21:41 +0000 | [diff] [blame] | 1279 | InjectedLoads[Pred] = Load; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1282 | LLVM_DEBUG(dbgs() << " speculated to: " << *NewPN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1283 | PN.eraseFromParent(); |
| 1284 | } |
| 1285 | |
| 1286 | /// Select instructions that use an alloca and are subsequently loaded can be |
| 1287 | /// rewritten to load both input pointers and then select between the result, |
| 1288 | /// allowing the load of the alloca to be promoted. |
| 1289 | /// From this: |
| 1290 | /// %P2 = select i1 %cond, i32* %Alloca, i32* %Other |
| 1291 | /// %V = load i32* %P2 |
| 1292 | /// to: |
| 1293 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1294 | /// %V2 = load i32* %Other |
| 1295 | /// %V = select i1 %cond, i32 %V1, i32 %V2 |
| 1296 | /// |
| 1297 | /// We can do this to a select if its only uses are loads and if the operand |
| 1298 | /// to the select can be loaded unconditionally. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1299 | static bool isSafeSelectToSpeculate(SelectInst &SI) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1300 | Value *TValue = SI.getTrueValue(); |
| 1301 | Value *FValue = SI.getFalseValue(); |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1302 | const DataLayout &DL = SI.getModule()->getDataLayout(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1303 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1304 | for (User *U : SI.users()) { |
| 1305 | LoadInst *LI = dyn_cast<LoadInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1306 | if (!LI || !LI->isSimple()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1307 | return false; |
| 1308 | |
Hiroshi Inoue | b300824 | 2017-06-24 15:43:33 +0000 | [diff] [blame] | 1309 | // Both operands to the select need to be dereferenceable, either |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1310 | // absolutely (e.g. allocas) or at this point because we can see other |
| 1311 | // accesses to it. |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1312 | if (!isSafeToLoadUnconditionally(TValue, LI->getAlignment(), DL, LI)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1313 | return false; |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1314 | if (!isSafeToLoadUnconditionally(FValue, LI->getAlignment(), DL, LI)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1315 | return false; |
| 1316 | } |
| 1317 | |
| 1318 | return true; |
| 1319 | } |
| 1320 | |
| 1321 | static void speculateSelectInstLoads(SelectInst &SI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1322 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1323 | |
| 1324 | IRBuilderTy IRB(&SI); |
| 1325 | Value *TV = SI.getTrueValue(); |
| 1326 | Value *FV = SI.getFalseValue(); |
| 1327 | // Replace the loads of the select with a select of two loads. |
| 1328 | while (!SI.use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1329 | LoadInst *LI = cast<LoadInst>(SI.user_back()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1330 | assert(LI->isSimple() && "We only speculate simple loads"); |
| 1331 | |
| 1332 | IRB.SetInsertPoint(LI); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 1333 | LoadInst *TL = IRB.CreateLoad(LI->getType(), TV, |
| 1334 | LI->getName() + ".sroa.speculate.load.true"); |
| 1335 | LoadInst *FL = IRB.CreateLoad(LI->getType(), FV, |
| 1336 | LI->getName() + ".sroa.speculate.load.false"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1337 | NumLoadsSpeculated += 2; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1338 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1339 | // Transfer alignment and AA info if present. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1340 | TL->setAlignment(LI->getAlignment()); |
| 1341 | FL->setAlignment(LI->getAlignment()); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1342 | |
| 1343 | AAMDNodes Tags; |
| 1344 | LI->getAAMetadata(Tags); |
| 1345 | if (Tags) { |
| 1346 | TL->setAAMetadata(Tags); |
| 1347 | FL->setAAMetadata(Tags); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1348 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1349 | |
| 1350 | Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL, |
| 1351 | LI->getName() + ".sroa.speculated"); |
| 1352 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1353 | LLVM_DEBUG(dbgs() << " speculated to: " << *V << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1354 | LI->replaceAllUsesWith(V); |
| 1355 | LI->eraseFromParent(); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1356 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1357 | SI.eraseFromParent(); |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1360 | /// Build a GEP out of a base pointer and indices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1361 | /// |
| 1362 | /// This will return the BasePtr if that is valid, or build a new GEP |
| 1363 | /// instruction using the IRBuilder if GEP-ing is needed. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1364 | static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1365 | SmallVectorImpl<Value *> &Indices, Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1366 | if (Indices.empty()) |
| 1367 | return BasePtr; |
| 1368 | |
| 1369 | // A single zero index is a no-op, so check for this and avoid building a GEP |
| 1370 | // in that case. |
| 1371 | if (Indices.size() == 1 && cast<ConstantInt>(Indices.back())->isZero()) |
| 1372 | return BasePtr; |
| 1373 | |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 1374 | return IRB.CreateInBoundsGEP(nullptr, BasePtr, Indices, |
| 1375 | NamePrefix + "sroa_idx"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1378 | /// Get a natural GEP off of the BasePtr walking through Ty toward |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1379 | /// TargetTy without changing the offset of the pointer. |
| 1380 | /// |
| 1381 | /// This routine assumes we've already established a properly offset GEP with |
| 1382 | /// Indices, and arrived at the Ty type. The goal is to continue to GEP with |
| 1383 | /// zero-indices down through type layers until we find one the same as |
| 1384 | /// TargetTy. If we can't find one with the same type, we at least try to use |
| 1385 | /// one with the same size. If none of that works, we just produce the GEP as |
| 1386 | /// indicated by Indices to have the correct offset. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1387 | static Value *getNaturalGEPWithType(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1388 | Value *BasePtr, Type *Ty, Type *TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1389 | SmallVectorImpl<Value *> &Indices, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1390 | Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1391 | if (Ty == TargetTy) |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1392 | return buildGEP(IRB, BasePtr, Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1393 | |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 1394 | // Offset size to use for the indices. |
| 1395 | unsigned OffsetSize = DL.getIndexTypeSizeInBits(BasePtr->getType()); |
Chandler Carruth | dfb2efd | 2014-02-26 10:08:16 +0000 | [diff] [blame] | 1396 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1397 | // See if we can descend into a struct and locate a field with the correct |
| 1398 | // type. |
| 1399 | unsigned NumLayers = 0; |
| 1400 | Type *ElementTy = Ty; |
| 1401 | do { |
| 1402 | if (ElementTy->isPointerTy()) |
| 1403 | break; |
Chandler Carruth | dfb2efd | 2014-02-26 10:08:16 +0000 | [diff] [blame] | 1404 | |
| 1405 | if (ArrayType *ArrayTy = dyn_cast<ArrayType>(ElementTy)) { |
| 1406 | ElementTy = ArrayTy->getElementType(); |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 1407 | Indices.push_back(IRB.getIntN(OffsetSize, 0)); |
Chandler Carruth | dfb2efd | 2014-02-26 10:08:16 +0000 | [diff] [blame] | 1408 | } else if (VectorType *VectorTy = dyn_cast<VectorType>(ElementTy)) { |
| 1409 | ElementTy = VectorTy->getElementType(); |
| 1410 | Indices.push_back(IRB.getInt32(0)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1411 | } else if (StructType *STy = dyn_cast<StructType>(ElementTy)) { |
Chandler Carruth | 503eb2b | 2012-10-09 01:58:35 +0000 | [diff] [blame] | 1412 | if (STy->element_begin() == STy->element_end()) |
| 1413 | break; // Nothing left to descend into. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1414 | ElementTy = *STy->element_begin(); |
| 1415 | Indices.push_back(IRB.getInt32(0)); |
| 1416 | } else { |
| 1417 | break; |
| 1418 | } |
| 1419 | ++NumLayers; |
| 1420 | } while (ElementTy != TargetTy); |
| 1421 | if (ElementTy != TargetTy) |
| 1422 | Indices.erase(Indices.end() - NumLayers, Indices.end()); |
| 1423 | |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1424 | return buildGEP(IRB, BasePtr, Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1427 | /// Recursively compute indices for a natural GEP. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1428 | /// |
| 1429 | /// This is the recursive step for getNaturalGEPWithOffset that walks down the |
| 1430 | /// element types adding appropriate indices for the GEP. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1431 | static Value *getNaturalGEPRecursively(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1432 | Value *Ptr, Type *Ty, APInt &Offset, |
| 1433 | Type *TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1434 | SmallVectorImpl<Value *> &Indices, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1435 | Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1436 | if (Offset == 0) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1437 | return getNaturalGEPWithType(IRB, DL, Ptr, Ty, TargetTy, Indices, |
| 1438 | NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1439 | |
| 1440 | // We can't recurse through pointer types. |
| 1441 | if (Ty->isPointerTy()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1442 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1443 | |
Chandler Carruth | dd3cea8 | 2012-09-14 10:30:40 +0000 | [diff] [blame] | 1444 | // We try to analyze GEPs over vectors here, but note that these GEPs are |
| 1445 | // extremely poorly defined currently. The long-term goal is to remove GEPing |
| 1446 | // over a vector from the IR completely. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1447 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1448 | unsigned ElementSizeInBits = DL.getTypeSizeInBits(VecTy->getScalarType()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1449 | if (ElementSizeInBits % 8 != 0) { |
| 1450 | // GEPs over non-multiple of 8 size vector elements are invalid. |
| 1451 | return nullptr; |
| 1452 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1453 | APInt ElementSize(Offset.getBitWidth(), ElementSizeInBits / 8); |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1454 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1455 | if (NumSkippedElements.ugt(VecTy->getNumElements())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1456 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1457 | Offset -= NumSkippedElements * ElementSize; |
| 1458 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1459 | return getNaturalGEPRecursively(IRB, DL, Ptr, VecTy->getElementType(), |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1460 | Offset, TargetTy, Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 1464 | Type *ElementTy = ArrTy->getElementType(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1465 | APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy)); |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1466 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1467 | if (NumSkippedElements.ugt(ArrTy->getNumElements())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1468 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1469 | |
| 1470 | Offset -= NumSkippedElements * ElementSize; |
| 1471 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1472 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1473 | Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | StructType *STy = dyn_cast<StructType>(Ty); |
| 1477 | if (!STy) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1478 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1479 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1480 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1481 | uint64_t StructOffset = Offset.getZExtValue(); |
Chandler Carruth | cabd96c | 2012-09-14 10:30:42 +0000 | [diff] [blame] | 1482 | if (StructOffset >= SL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1483 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1484 | unsigned Index = SL->getElementContainingOffset(StructOffset); |
| 1485 | Offset -= APInt(Offset.getBitWidth(), SL->getElementOffset(Index)); |
| 1486 | Type *ElementTy = STy->getElementType(Index); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1487 | if (Offset.uge(DL.getTypeAllocSize(ElementTy))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1488 | return nullptr; // The offset points into alignment padding. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1489 | |
| 1490 | Indices.push_back(IRB.getInt32(Index)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1491 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1492 | Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1495 | /// Get a natural GEP from a base pointer to a particular offset and |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1496 | /// resulting in a particular type. |
| 1497 | /// |
| 1498 | /// The goal is to produce a "natural" looking GEP that works with the existing |
| 1499 | /// composite types to arrive at the appropriate offset and element type for |
| 1500 | /// a pointer. TargetTy is the element type the returned GEP should point-to if |
| 1501 | /// possible. We recurse by decreasing Offset, adding the appropriate index to |
| 1502 | /// Indices, and setting Ty to the result subtype. |
| 1503 | /// |
Chandler Carruth | 93a21e7 | 2012-09-14 10:18:49 +0000 | [diff] [blame] | 1504 | /// If no natural GEP can be constructed, this function returns null. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1505 | static Value *getNaturalGEPWithOffset(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1506 | Value *Ptr, APInt Offset, Type *TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1507 | SmallVectorImpl<Value *> &Indices, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1508 | Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1509 | PointerType *Ty = cast<PointerType>(Ptr->getType()); |
| 1510 | |
| 1511 | // Don't consider any GEPs through an i8* as natural unless the TargetTy is |
| 1512 | // an i8. |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 1513 | if (Ty == IRB.getInt8PtrTy(Ty->getAddressSpace()) && TargetTy->isIntegerTy(8)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1514 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1515 | |
| 1516 | Type *ElementTy = Ty->getElementType(); |
Chandler Carruth | 3f882d4 | 2012-09-18 22:37:19 +0000 | [diff] [blame] | 1517 | if (!ElementTy->isSized()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1518 | return nullptr; // We can't GEP through an unsized element. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1519 | APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1520 | if (ElementSize == 0) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1521 | return nullptr; // Zero-length arrays can't help us build a natural GEP. |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1522 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1523 | |
| 1524 | Offset -= NumSkippedElements * ElementSize; |
| 1525 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1526 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1527 | Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1530 | /// Compute an adjusted pointer from Ptr by Offset bytes where the |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1531 | /// resulting pointer has PointerTy. |
| 1532 | /// |
| 1533 | /// This tries very hard to compute a "natural" GEP which arrives at the offset |
| 1534 | /// and produces the pointer type desired. Where it cannot, it will try to use |
| 1535 | /// the natural GEP to arrive at the offset and bitcast to the type. Where that |
| 1536 | /// fails, it will try to use an existing i8* and GEP to the byte offset and |
| 1537 | /// bitcast to the type. |
| 1538 | /// |
| 1539 | /// The strategy for finding the more natural GEPs is to peel off layers of the |
| 1540 | /// pointer, walking back through bit casts and GEPs, searching for a base |
| 1541 | /// pointer from which we can compute a natural GEP with the desired |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 1542 | /// properties. The algorithm tries to fold as many constant indices into |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1543 | /// a single GEP as possible, thus making each GEP more independent of the |
| 1544 | /// surrounding code. |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1545 | static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1546 | APInt Offset, Type *PointerTy, Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1547 | // Even though we don't look through PHI nodes, we could be called on an |
| 1548 | // instruction in an unreachable block, which may be on a cycle. |
| 1549 | SmallPtrSet<Value *, 4> Visited; |
| 1550 | Visited.insert(Ptr); |
| 1551 | SmallVector<Value *, 4> Indices; |
| 1552 | |
| 1553 | // We may end up computing an offset pointer that has the wrong type. If we |
| 1554 | // never are able to compute one directly that has the correct type, we'll |
Chandler Carruth | 5986b54 | 2015-01-02 02:47:38 +0000 | [diff] [blame] | 1555 | // fall back to it, so keep it and the base it was computed from around here. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1556 | Value *OffsetPtr = nullptr; |
Chandler Carruth | 5986b54 | 2015-01-02 02:47:38 +0000 | [diff] [blame] | 1557 | Value *OffsetBasePtr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1558 | |
| 1559 | // Remember any i8 pointer we come across to re-use if we need to do a raw |
| 1560 | // byte offset. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1561 | Value *Int8Ptr = nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1562 | APInt Int8PtrOffset(Offset.getBitWidth(), 0); |
| 1563 | |
| 1564 | Type *TargetTy = PointerTy->getPointerElementType(); |
| 1565 | |
| 1566 | do { |
| 1567 | // First fold any existing GEPs into the offset. |
| 1568 | while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
| 1569 | APInt GEPOffset(Offset.getBitWidth(), 0); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1570 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1571 | break; |
| 1572 | Offset += GEPOffset; |
| 1573 | Ptr = GEP->getPointerOperand(); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1574 | if (!Visited.insert(Ptr).second) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | |
| 1578 | // See if we can perform a natural GEP here. |
| 1579 | Indices.clear(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1580 | if (Value *P = getNaturalGEPWithOffset(IRB, DL, Ptr, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1581 | Indices, NamePrefix)) { |
Chandler Carruth | 5986b54 | 2015-01-02 02:47:38 +0000 | [diff] [blame] | 1582 | // If we have a new natural pointer at the offset, clear out any old |
| 1583 | // offset pointer we computed. Unless it is the base pointer or |
| 1584 | // a non-instruction, we built a GEP we don't need. Zap it. |
| 1585 | if (OffsetPtr && OffsetPtr != OffsetBasePtr) |
| 1586 | if (Instruction *I = dyn_cast<Instruction>(OffsetPtr)) { |
| 1587 | assert(I->use_empty() && "Built a GEP with uses some how!"); |
| 1588 | I->eraseFromParent(); |
| 1589 | } |
| 1590 | OffsetPtr = P; |
| 1591 | OffsetBasePtr = Ptr; |
| 1592 | // If we also found a pointer of the right type, we're done. |
| 1593 | if (P->getType() == PointerTy) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1594 | return P; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | // Stash this pointer if we've found an i8*. |
| 1598 | if (Ptr->getType()->isIntegerTy(8)) { |
| 1599 | Int8Ptr = Ptr; |
| 1600 | Int8PtrOffset = Offset; |
| 1601 | } |
| 1602 | |
| 1603 | // Peel off a layer of the pointer and update the offset appropriately. |
| 1604 | if (Operator::getOpcode(Ptr) == Instruction::BitCast) { |
| 1605 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 1606 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 1607 | if (GA->isInterposable()) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1608 | break; |
| 1609 | Ptr = GA->getAliasee(); |
| 1610 | } else { |
| 1611 | break; |
| 1612 | } |
| 1613 | assert(Ptr->getType()->isPointerTy() && "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1614 | } while (Visited.insert(Ptr).second); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (!OffsetPtr) { |
| 1617 | if (!Int8Ptr) { |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 1618 | Int8Ptr = IRB.CreateBitCast( |
| 1619 | Ptr, IRB.getInt8PtrTy(PointerTy->getPointerAddressSpace()), |
| 1620 | NamePrefix + "sroa_raw_cast"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1621 | Int8PtrOffset = Offset; |
| 1622 | } |
| 1623 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1624 | OffsetPtr = Int8PtrOffset == 0 |
| 1625 | ? Int8Ptr |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 1626 | : IRB.CreateInBoundsGEP(IRB.getInt8Ty(), Int8Ptr, |
| 1627 | IRB.getInt(Int8PtrOffset), |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1628 | NamePrefix + "sroa_raw_idx"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1629 | } |
| 1630 | Ptr = OffsetPtr; |
| 1631 | |
| 1632 | // On the off chance we were targeting i8*, guard the bitcast here. |
| 1633 | if (Ptr->getType() != PointerTy) |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1634 | Ptr = IRB.CreateBitCast(Ptr, PointerTy, NamePrefix + "sroa_cast"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1635 | |
| 1636 | return Ptr; |
| 1637 | } |
| 1638 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1639 | /// Compute the adjusted alignment for a load or store from an offset. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 1640 | static unsigned getAdjustedAlignment(Instruction *I, uint64_t Offset, |
| 1641 | const DataLayout &DL) { |
| 1642 | unsigned Alignment; |
| 1643 | Type *Ty; |
| 1644 | if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 1645 | Alignment = LI->getAlignment(); |
| 1646 | Ty = LI->getType(); |
| 1647 | } else if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 1648 | Alignment = SI->getAlignment(); |
| 1649 | Ty = SI->getValueOperand()->getType(); |
| 1650 | } else { |
| 1651 | llvm_unreachable("Only loads and stores are allowed!"); |
| 1652 | } |
| 1653 | |
| 1654 | if (!Alignment) |
| 1655 | Alignment = DL.getABITypeAlignment(Ty); |
| 1656 | |
| 1657 | return MinAlign(Alignment, Offset); |
| 1658 | } |
| 1659 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1660 | /// Test whether we can convert a value from the old to the new type. |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1661 | /// |
| 1662 | /// This predicate should be used to guard calls to convertValue in order to |
| 1663 | /// ensure that we only try to convert viable values. The strategy is that we |
| 1664 | /// will peel off single element struct and array wrappings to get to an |
| 1665 | /// underlying value, and convert that value. |
| 1666 | static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy) { |
| 1667 | if (OldTy == NewTy) |
| 1668 | return true; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 1669 | |
| 1670 | // For integer types, we can't handle any bit-width differences. This would |
| 1671 | // break both vector conversions with extension and introduce endianness |
| 1672 | // issues when in conjunction with loads and stores. |
| 1673 | if (isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) { |
| 1674 | assert(cast<IntegerType>(OldTy)->getBitWidth() != |
| 1675 | cast<IntegerType>(NewTy)->getBitWidth() && |
| 1676 | "We can't have the same bitwidth for different int types"); |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1680 | if (DL.getTypeSizeInBits(NewTy) != DL.getTypeSizeInBits(OldTy)) |
| 1681 | return false; |
| 1682 | if (!NewTy->isSingleValueType() || !OldTy->isSingleValueType()) |
| 1683 | return false; |
| 1684 | |
Benjamin Kramer | 5626259 | 2013-09-22 11:24:58 +0000 | [diff] [blame] | 1685 | // We can convert pointers to integers and vice-versa. Same for vectors |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1686 | // of pointers and integers. |
| 1687 | OldTy = OldTy->getScalarType(); |
| 1688 | NewTy = NewTy->getScalarType(); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1689 | if (NewTy->isPointerTy() || OldTy->isPointerTy()) { |
Jack Liu | f101c0f | 2016-05-03 19:30:48 +0000 | [diff] [blame] | 1690 | if (NewTy->isPointerTy() && OldTy->isPointerTy()) { |
| 1691 | return cast<PointerType>(NewTy)->getPointerAddressSpace() == |
| 1692 | cast<PointerType>(OldTy)->getPointerAddressSpace(); |
| 1693 | } |
Sanjoy Das | b70ddd8 | 2017-06-17 20:28:13 +0000 | [diff] [blame] | 1694 | |
| 1695 | // We can convert integers to integral pointers, but not to non-integral |
| 1696 | // pointers. |
| 1697 | if (OldTy->isIntegerTy()) |
| 1698 | return !DL.isNonIntegralPointerType(NewTy); |
| 1699 | |
| 1700 | // We can convert integral pointers to integers, but non-integral pointers |
| 1701 | // need to remain pointers. |
| 1702 | if (!DL.isNonIntegralPointerType(OldTy)) |
| 1703 | return NewTy->isIntegerTy(); |
| 1704 | |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1705 | return false; |
| 1706 | } |
| 1707 | |
| 1708 | return true; |
| 1709 | } |
| 1710 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1711 | /// Generic routine to convert an SSA value to a value of a different |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1712 | /// type. |
| 1713 | /// |
| 1714 | /// This will try various different casting techniques, such as bitcasts, |
| 1715 | /// inttoptr, and ptrtoint casts. Use the \c canConvertValue predicate to test |
| 1716 | /// two types for viability with this routine. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1717 | static Value *convertValue(const DataLayout &DL, IRBuilderTy &IRB, Value *V, |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1718 | Type *NewTy) { |
| 1719 | Type *OldTy = V->getType(); |
| 1720 | assert(canConvertValue(DL, OldTy, NewTy) && "Value not convertable to type"); |
| 1721 | |
| 1722 | if (OldTy == NewTy) |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1723 | return V; |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1724 | |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 1725 | assert(!(isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) && |
| 1726 | "Integer types must be the exact same to convert."); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1727 | |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1728 | // See if we need inttoptr for this type pair. A cast involving both scalars |
| 1729 | // and vectors requires and additional bitcast. |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1730 | if (OldTy->isIntOrIntVectorTy() && NewTy->isPtrOrPtrVectorTy()) { |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1731 | // Expand <2 x i32> to i8* --> <2 x i32> to i64 to i8* |
| 1732 | if (OldTy->isVectorTy() && !NewTy->isVectorTy()) |
| 1733 | return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)), |
| 1734 | NewTy); |
| 1735 | |
| 1736 | // Expand i128 to <2 x i8*> --> i128 to <2 x i64> to <2 x i8*> |
| 1737 | if (!OldTy->isVectorTy() && NewTy->isVectorTy()) |
| 1738 | return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)), |
| 1739 | NewTy); |
| 1740 | |
| 1741 | return IRB.CreateIntToPtr(V, NewTy); |
| 1742 | } |
| 1743 | |
| 1744 | // See if we need ptrtoint for this type pair. A cast involving both scalars |
| 1745 | // and vectors requires and additional bitcast. |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1746 | if (OldTy->isPtrOrPtrVectorTy() && NewTy->isIntOrIntVectorTy()) { |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1747 | // Expand <2 x i8*> to i128 --> <2 x i8*> to <2 x i64> to i128 |
| 1748 | if (OldTy->isVectorTy() && !NewTy->isVectorTy()) |
| 1749 | return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)), |
| 1750 | NewTy); |
| 1751 | |
| 1752 | // Expand i8* to <2 x i32> --> i8* to i64 to <2 x i32> |
| 1753 | if (!OldTy->isVectorTy() && NewTy->isVectorTy()) |
| 1754 | return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)), |
| 1755 | NewTy); |
| 1756 | |
| 1757 | return IRB.CreatePtrToInt(V, NewTy); |
| 1758 | } |
| 1759 | |
| 1760 | return IRB.CreateBitCast(V, NewTy); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1763 | /// Test whether the given slice use can be promoted to a vector. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1764 | /// |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1765 | /// This function is called to test each entry in a partition which is slated |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1766 | /// for a single slice. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 1767 | static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S, |
| 1768 | VectorType *Ty, |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1769 | uint64_t ElementSize, |
| 1770 | const DataLayout &DL) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1771 | // First validate the slice offsets. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1772 | uint64_t BeginOffset = |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1773 | std::max(S.beginOffset(), P.beginOffset()) - P.beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1774 | uint64_t BeginIndex = BeginOffset / ElementSize; |
| 1775 | if (BeginIndex * ElementSize != BeginOffset || |
| 1776 | BeginIndex >= Ty->getNumElements()) |
| 1777 | return false; |
| 1778 | uint64_t EndOffset = |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1779 | std::min(S.endOffset(), P.endOffset()) - P.beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1780 | uint64_t EndIndex = EndOffset / ElementSize; |
| 1781 | if (EndIndex * ElementSize != EndOffset || EndIndex > Ty->getNumElements()) |
| 1782 | return false; |
| 1783 | |
| 1784 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 1785 | uint64_t NumElements = EndIndex - BeginIndex; |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1786 | Type *SliceTy = (NumElements == 1) |
| 1787 | ? Ty->getElementType() |
| 1788 | : VectorType::get(Ty->getElementType(), NumElements); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1789 | |
| 1790 | Type *SplitIntTy = |
| 1791 | Type::getIntNTy(Ty->getContext(), NumElements * ElementSize * 8); |
| 1792 | |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1793 | Use *U = S.getUse(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1794 | |
| 1795 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) { |
| 1796 | if (MI->isVolatile()) |
| 1797 | return false; |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1798 | if (!S.isSplittable()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1799 | return false; // Skip any unsplittable intrinsics. |
Owen Anderson | 6c19ab1 | 2014-08-07 21:07:35 +0000 | [diff] [blame] | 1800 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) { |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 1801 | if (!II->isLifetimeStartOrEnd()) |
Owen Anderson | 6c19ab1 | 2014-08-07 21:07:35 +0000 | [diff] [blame] | 1802 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1803 | } else if (U->get()->getType()->getPointerElementType()->isStructTy()) { |
| 1804 | // Disable vector promotion when there are loads or stores of an FCA. |
| 1805 | return false; |
| 1806 | } else if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
| 1807 | if (LI->isVolatile()) |
| 1808 | return false; |
| 1809 | Type *LTy = LI->getType(); |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1810 | if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1811 | assert(LTy->isIntegerTy()); |
| 1812 | LTy = SplitIntTy; |
| 1813 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1814 | if (!canConvertValue(DL, SliceTy, LTy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1815 | return false; |
| 1816 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
| 1817 | if (SI->isVolatile()) |
| 1818 | return false; |
| 1819 | Type *STy = SI->getValueOperand()->getType(); |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1820 | if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1821 | assert(STy->isIntegerTy()); |
| 1822 | STy = SplitIntTy; |
| 1823 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1824 | if (!canConvertValue(DL, STy, SliceTy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1825 | return false; |
Chandler Carruth | 1ed848d | 2013-07-19 10:57:32 +0000 | [diff] [blame] | 1826 | } else { |
| 1827 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | return true; |
| 1831 | } |
| 1832 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1833 | /// Test whether the given alloca partitioning and range of slices can be |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1834 | /// promoted to a vector. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1835 | /// |
| 1836 | /// This is a quick test to check whether we can rewrite a particular alloca |
| 1837 | /// partition (and its newly formed alloca) into a vector alloca with only |
| 1838 | /// whole-vector loads and stores such that it could be promoted to a vector |
| 1839 | /// SSA value. We only can ensure this for a limited set of operations, and we |
| 1840 | /// don't want to do the rewrites unless we are confident that the result will |
| 1841 | /// be promotable, so we have an early test here. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 1842 | static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) { |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1843 | // Collect the candidate types for vector-based promotion. Also track whether |
| 1844 | // we have different element types. |
| 1845 | SmallVector<VectorType *, 4> CandidateTys; |
| 1846 | Type *CommonEltTy = nullptr; |
| 1847 | bool HaveCommonEltTy = true; |
| 1848 | auto CheckCandidateType = [&](Type *Ty) { |
| 1849 | if (auto *VTy = dyn_cast<VectorType>(Ty)) { |
| 1850 | CandidateTys.push_back(VTy); |
| 1851 | if (!CommonEltTy) |
| 1852 | CommonEltTy = VTy->getElementType(); |
| 1853 | else if (CommonEltTy != VTy->getElementType()) |
| 1854 | HaveCommonEltTy = false; |
| 1855 | } |
| 1856 | }; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1857 | // Consider any loads or stores that are the exact size of the slice. |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1858 | for (const Slice &S : P) |
| 1859 | if (S.beginOffset() == P.beginOffset() && |
| 1860 | S.endOffset() == P.endOffset()) { |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1861 | if (auto *LI = dyn_cast<LoadInst>(S.getUse()->getUser())) |
| 1862 | CheckCandidateType(LI->getType()); |
| 1863 | else if (auto *SI = dyn_cast<StoreInst>(S.getUse()->getUser())) |
| 1864 | CheckCandidateType(SI->getValueOperand()->getType()); |
| 1865 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1866 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1867 | // If we didn't find a vector type, nothing to do here. |
| 1868 | if (CandidateTys.empty()) |
| 1869 | return nullptr; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1870 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1871 | // Remove non-integer vector types if we had multiple common element types. |
| 1872 | // FIXME: It'd be nice to replace them with integer vector types, but we can't |
| 1873 | // do that until all the backends are known to produce good code for all |
| 1874 | // integer vector types. |
| 1875 | if (!HaveCommonEltTy) { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 1876 | CandidateTys.erase( |
| 1877 | llvm::remove_if(CandidateTys, |
| 1878 | [](VectorType *VTy) { |
| 1879 | return !VTy->getElementType()->isIntegerTy(); |
| 1880 | }), |
| 1881 | CandidateTys.end()); |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1882 | |
| 1883 | // If there were no integer vector types, give up. |
| 1884 | if (CandidateTys.empty()) |
| 1885 | return nullptr; |
| 1886 | |
| 1887 | // Rank the remaining candidate vector types. This is easy because we know |
| 1888 | // they're all integer vectors. We sort by ascending number of elements. |
| 1889 | auto RankVectorTypes = [&DL](VectorType *RHSTy, VectorType *LHSTy) { |
David L. Jones | 41cecba | 2017-01-13 21:02:41 +0000 | [diff] [blame] | 1890 | (void)DL; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1891 | assert(DL.getTypeSizeInBits(RHSTy) == DL.getTypeSizeInBits(LHSTy) && |
| 1892 | "Cannot have vector types of different sizes!"); |
| 1893 | assert(RHSTy->getElementType()->isIntegerTy() && |
| 1894 | "All non-integer types eliminated!"); |
| 1895 | assert(LHSTy->getElementType()->isIntegerTy() && |
| 1896 | "All non-integer types eliminated!"); |
| 1897 | return RHSTy->getNumElements() < LHSTy->getNumElements(); |
| 1898 | }; |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1899 | llvm::sort(CandidateTys, RankVectorTypes); |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1900 | CandidateTys.erase( |
| 1901 | std::unique(CandidateTys.begin(), CandidateTys.end(), RankVectorTypes), |
| 1902 | CandidateTys.end()); |
| 1903 | } else { |
| 1904 | // The only way to have the same element type in every vector type is to |
| 1905 | // have the same vector type. Check that and remove all but one. |
| 1906 | #ifndef NDEBUG |
| 1907 | for (VectorType *VTy : CandidateTys) { |
| 1908 | assert(VTy->getElementType() == CommonEltTy && |
| 1909 | "Unaccounted for element type!"); |
| 1910 | assert(VTy == CandidateTys[0] && |
| 1911 | "Different vector types with the same element type!"); |
| 1912 | } |
| 1913 | #endif |
| 1914 | CandidateTys.resize(1); |
| 1915 | } |
| 1916 | |
| 1917 | // Try each vector type, and return the one which works. |
| 1918 | auto CheckVectorTypeForPromotion = [&](VectorType *VTy) { |
| 1919 | uint64_t ElementSize = DL.getTypeSizeInBits(VTy->getElementType()); |
| 1920 | |
| 1921 | // While the definition of LLVM vectors is bitpacked, we don't support sizes |
| 1922 | // that aren't byte sized. |
| 1923 | if (ElementSize % 8) |
| 1924 | return false; |
| 1925 | assert((DL.getTypeSizeInBits(VTy) % 8) == 0 && |
| 1926 | "vector size not a multiple of element size?"); |
| 1927 | ElementSize /= 8; |
| 1928 | |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1929 | for (const Slice &S : P) |
| 1930 | if (!isVectorPromotionViableForSlice(P, S, VTy, ElementSize, DL)) |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1931 | return false; |
| 1932 | |
Chandler Carruth | ffb7ce5 | 2014-12-24 01:48:09 +0000 | [diff] [blame] | 1933 | for (const Slice *S : P.splitSliceTails()) |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1934 | if (!isVectorPromotionViableForSlice(P, *S, VTy, ElementSize, DL)) |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1935 | return false; |
| 1936 | |
| 1937 | return true; |
| 1938 | }; |
| 1939 | for (VectorType *VTy : CandidateTys) |
| 1940 | if (CheckVectorTypeForPromotion(VTy)) |
| 1941 | return VTy; |
| 1942 | |
| 1943 | return nullptr; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1946 | /// Test whether a slice of an alloca is valid for integer widening. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1947 | /// |
| 1948 | /// This implements the necessary checking for the \c isIntegerWideningViable |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1949 | /// test below on a single slice of the alloca. |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1950 | static bool isIntegerWideningViableForSlice(const Slice &S, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1951 | uint64_t AllocBeginOffset, |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1952 | Type *AllocaTy, |
| 1953 | const DataLayout &DL, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1954 | bool &WholeAllocaOp) { |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1955 | uint64_t Size = DL.getTypeStoreSize(AllocaTy); |
| 1956 | |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1957 | uint64_t RelBegin = S.beginOffset() - AllocBeginOffset; |
| 1958 | uint64_t RelEnd = S.endOffset() - AllocBeginOffset; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1959 | |
| 1960 | // We can't reasonably handle cases where the load or store extends past |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1961 | // the end of the alloca's type and into its padding. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1962 | if (RelEnd > Size) |
| 1963 | return false; |
| 1964 | |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1965 | Use *U = S.getUse(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1966 | |
| 1967 | if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
| 1968 | if (LI->isVolatile()) |
| 1969 | return false; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 1970 | // We can't handle loads that extend past the allocated memory. |
| 1971 | if (DL.getTypeStoreSize(LI->getType()) > Size) |
| 1972 | return false; |
Hiroshi Inoue | f5c0e6c | 2018-05-17 06:32:17 +0000 | [diff] [blame] | 1973 | // So far, AllocaSliceRewriter does not support widening split slice tails |
| 1974 | // in rewriteIntegerLoad. |
| 1975 | if (S.beginOffset() < AllocBeginOffset) |
| 1976 | return false; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1977 | // Note that we don't count vector loads or stores as whole-alloca |
| 1978 | // operations which enable integer widening because we would prefer to use |
| 1979 | // vector widening instead. |
| 1980 | if (!isa<VectorType>(LI->getType()) && RelBegin == 0 && RelEnd == Size) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1981 | WholeAllocaOp = true; |
| 1982 | if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1983 | if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy)) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1984 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1985 | } else if (RelBegin != 0 || RelEnd != Size || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1986 | !canConvertValue(DL, AllocaTy, LI->getType())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1987 | // Non-integer loads need to be convertible from the alloca type so that |
| 1988 | // they are promotable. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1989 | return false; |
| 1990 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1991 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
| 1992 | Type *ValueTy = SI->getValueOperand()->getType(); |
| 1993 | if (SI->isVolatile()) |
| 1994 | return false; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 1995 | // We can't handle stores that extend past the allocated memory. |
| 1996 | if (DL.getTypeStoreSize(ValueTy) > Size) |
| 1997 | return false; |
Hiroshi Inoue | f5c0e6c | 2018-05-17 06:32:17 +0000 | [diff] [blame] | 1998 | // So far, AllocaSliceRewriter does not support widening split slice tails |
| 1999 | // in rewriteIntegerStore. |
| 2000 | if (S.beginOffset() < AllocBeginOffset) |
| 2001 | return false; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2002 | // Note that we don't count vector loads or stores as whole-alloca |
| 2003 | // operations which enable integer widening because we would prefer to use |
| 2004 | // vector widening instead. |
| 2005 | if (!isa<VectorType>(ValueTy) && RelBegin == 0 && RelEnd == Size) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2006 | WholeAllocaOp = true; |
| 2007 | if (IntegerType *ITy = dyn_cast<IntegerType>(ValueTy)) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2008 | if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2009 | return false; |
| 2010 | } else if (RelBegin != 0 || RelEnd != Size || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2011 | !canConvertValue(DL, ValueTy, AllocaTy)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2012 | // Non-integer stores need to be convertible to the alloca type so that |
| 2013 | // they are promotable. |
| 2014 | return false; |
| 2015 | } |
| 2016 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) { |
| 2017 | if (MI->isVolatile() || !isa<Constant>(MI->getLength())) |
| 2018 | return false; |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 2019 | if (!S.isSplittable()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2020 | return false; // Skip any unsplittable intrinsics. |
| 2021 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) { |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 2022 | if (!II->isLifetimeStartOrEnd()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2023 | return false; |
| 2024 | } else { |
| 2025 | return false; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2026 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2027 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2028 | return true; |
| 2029 | } |
| 2030 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2031 | /// Test whether the given alloca partition's integer operations can be |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2032 | /// widened to promotable ones. |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2033 | /// |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2034 | /// This is a quick test to check whether we can rewrite the integer loads and |
| 2035 | /// stores to a particular alloca into wider loads and stores and be able to |
| 2036 | /// promote the resulting alloca. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 2037 | static bool isIntegerWideningViable(Partition &P, Type *AllocaTy, |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2038 | const DataLayout &DL) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2039 | uint64_t SizeInBits = DL.getTypeSizeInBits(AllocaTy); |
Benjamin Kramer | 47534c7 | 2012-12-01 11:53:32 +0000 | [diff] [blame] | 2040 | // Don't create integer types larger than the maximum bitwidth. |
| 2041 | if (SizeInBits > IntegerType::MAX_INT_BITS) |
| 2042 | return false; |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Don't try to handle allocas with bit-padding. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2045 | if (SizeInBits != DL.getTypeStoreSizeInBits(AllocaTy)) |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2046 | return false; |
| 2047 | |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2048 | // We need to ensure that an integer type with the appropriate bitwidth can |
| 2049 | // be converted to the alloca type, whatever that is. We don't want to force |
| 2050 | // the alloca itself to have an integer type if there is a more suitable one. |
| 2051 | Type *IntTy = Type::getIntNTy(AllocaTy->getContext(), SizeInBits); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2052 | if (!canConvertValue(DL, AllocaTy, IntTy) || |
| 2053 | !canConvertValue(DL, IntTy, AllocaTy)) |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2054 | return false; |
| 2055 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2056 | // While examining uses, we ensure that the alloca has a covering load or |
| 2057 | // store. We don't want to widen the integer operations only to fail to |
| 2058 | // promote due to some other unsplittable entry (which we may make splittable |
Chandler Carruth | 5955c9e | 2013-07-19 07:12:23 +0000 | [diff] [blame] | 2059 | // later). However, if there are only splittable uses, go ahead and assume |
| 2060 | // that we cover the alloca. |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2061 | // FIXME: We shouldn't consider split slices that happen to start in the |
| 2062 | // partition here... |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 2063 | bool WholeAllocaOp = |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2064 | P.begin() != P.end() ? false : DL.isLegalInteger(SizeInBits); |
Chandler Carruth | 43c8b46 | 2012-10-04 10:39:28 +0000 | [diff] [blame] | 2065 | |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2066 | for (const Slice &S : P) |
| 2067 | if (!isIntegerWideningViableForSlice(S, P.beginOffset(), AllocaTy, DL, |
| 2068 | WholeAllocaOp)) |
Chandler Carruth | 43c8b46 | 2012-10-04 10:39:28 +0000 | [diff] [blame] | 2069 | return false; |
| 2070 | |
Chandler Carruth | ffb7ce5 | 2014-12-24 01:48:09 +0000 | [diff] [blame] | 2071 | for (const Slice *S : P.splitSliceTails()) |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2072 | if (!isIntegerWideningViableForSlice(*S, P.beginOffset(), AllocaTy, DL, |
| 2073 | WholeAllocaOp)) |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2074 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2075 | |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2076 | return WholeAllocaOp; |
| 2077 | } |
| 2078 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2079 | static Value *extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V, |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2080 | IntegerType *Ty, uint64_t Offset, |
| 2081 | const Twine &Name) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2082 | LLVM_DEBUG(dbgs() << " start: " << *V << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2083 | IntegerType *IntTy = cast<IntegerType>(V->getType()); |
| 2084 | assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) && |
| 2085 | "Element extends past full value"); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2086 | uint64_t ShAmt = 8 * Offset; |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2087 | if (DL.isBigEndian()) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2088 | ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2089 | if (ShAmt) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2090 | V = IRB.CreateLShr(V, ShAmt, Name + ".shift"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2091 | LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2092 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2093 | assert(Ty->getBitWidth() <= IntTy->getBitWidth() && |
| 2094 | "Cannot extract to a larger integer!"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2095 | if (Ty != IntTy) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2096 | V = IRB.CreateTrunc(V, Ty, Name + ".trunc"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2097 | LLVM_DEBUG(dbgs() << " trunced: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2098 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2099 | return V; |
| 2100 | } |
| 2101 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2102 | static Value *insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old, |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2103 | Value *V, uint64_t Offset, const Twine &Name) { |
| 2104 | IntegerType *IntTy = cast<IntegerType>(Old->getType()); |
| 2105 | IntegerType *Ty = cast<IntegerType>(V->getType()); |
| 2106 | assert(Ty->getBitWidth() <= IntTy->getBitWidth() && |
| 2107 | "Cannot insert a larger integer!"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2108 | LLVM_DEBUG(dbgs() << " start: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2109 | if (Ty != IntTy) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2110 | V = IRB.CreateZExt(V, IntTy, Name + ".ext"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2111 | LLVM_DEBUG(dbgs() << " extended: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2112 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2113 | assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) && |
| 2114 | "Element store outside of alloca store"); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2115 | uint64_t ShAmt = 8 * Offset; |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2116 | if (DL.isBigEndian()) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2117 | ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2118 | if (ShAmt) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2119 | V = IRB.CreateShl(V, ShAmt, Name + ".shift"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2120 | LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2121 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2122 | |
| 2123 | if (ShAmt || Ty->getBitWidth() < IntTy->getBitWidth()) { |
| 2124 | APInt Mask = ~Ty->getMask().zext(IntTy->getBitWidth()).shl(ShAmt); |
| 2125 | Old = IRB.CreateAnd(Old, Mask, Name + ".mask"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2126 | LLVM_DEBUG(dbgs() << " masked: " << *Old << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2127 | V = IRB.CreateOr(Old, V, Name + ".insert"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2128 | LLVM_DEBUG(dbgs() << " inserted: " << *V << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2129 | } |
| 2130 | return V; |
| 2131 | } |
| 2132 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2133 | static Value *extractVector(IRBuilderTy &IRB, Value *V, unsigned BeginIndex, |
| 2134 | unsigned EndIndex, const Twine &Name) { |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2135 | VectorType *VecTy = cast<VectorType>(V->getType()); |
| 2136 | unsigned NumElements = EndIndex - BeginIndex; |
| 2137 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
| 2138 | |
| 2139 | if (NumElements == VecTy->getNumElements()) |
| 2140 | return V; |
| 2141 | |
| 2142 | if (NumElements == 1) { |
| 2143 | V = IRB.CreateExtractElement(V, IRB.getInt32(BeginIndex), |
| 2144 | Name + ".extract"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2145 | LLVM_DEBUG(dbgs() << " extract: " << *V << "\n"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2146 | return V; |
| 2147 | } |
| 2148 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2149 | SmallVector<Constant *, 8> Mask; |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2150 | Mask.reserve(NumElements); |
| 2151 | for (unsigned i = BeginIndex; i != EndIndex; ++i) |
| 2152 | Mask.push_back(IRB.getInt32(i)); |
| 2153 | V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()), |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2154 | ConstantVector::get(Mask), Name + ".extract"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2155 | LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2156 | return V; |
| 2157 | } |
| 2158 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2159 | static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V, |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2160 | unsigned BeginIndex, const Twine &Name) { |
| 2161 | VectorType *VecTy = cast<VectorType>(Old->getType()); |
| 2162 | assert(VecTy && "Can only insert a vector into a vector"); |
| 2163 | |
| 2164 | VectorType *Ty = dyn_cast<VectorType>(V->getType()); |
| 2165 | if (!Ty) { |
| 2166 | // Single element to insert. |
| 2167 | V = IRB.CreateInsertElement(Old, V, IRB.getInt32(BeginIndex), |
| 2168 | Name + ".insert"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2169 | LLVM_DEBUG(dbgs() << " insert: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2170 | return V; |
| 2171 | } |
| 2172 | |
| 2173 | assert(Ty->getNumElements() <= VecTy->getNumElements() && |
| 2174 | "Too many elements!"); |
| 2175 | if (Ty->getNumElements() == VecTy->getNumElements()) { |
| 2176 | assert(V->getType() == VecTy && "Vector type mismatch"); |
| 2177 | return V; |
| 2178 | } |
| 2179 | unsigned EndIndex = BeginIndex + Ty->getNumElements(); |
| 2180 | |
| 2181 | // When inserting a smaller vector into the larger to store, we first |
| 2182 | // use a shuffle vector to widen it with undef elements, and then |
| 2183 | // a second shuffle vector to select between the loaded vector and the |
| 2184 | // incoming vector. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2185 | SmallVector<Constant *, 8> Mask; |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2186 | Mask.reserve(VecTy->getNumElements()); |
| 2187 | for (unsigned i = 0; i != VecTy->getNumElements(); ++i) |
| 2188 | if (i >= BeginIndex && i < EndIndex) |
| 2189 | Mask.push_back(IRB.getInt32(i - BeginIndex)); |
| 2190 | else |
| 2191 | Mask.push_back(UndefValue::get(IRB.getInt32Ty())); |
| 2192 | V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()), |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2193 | ConstantVector::get(Mask), Name + ".expand"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2194 | LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2195 | |
| 2196 | Mask.clear(); |
| 2197 | for (unsigned i = 0; i != VecTy->getNumElements(); ++i) |
Nadav Rotem | 1e21191 | 2013-05-01 19:53:30 +0000 | [diff] [blame] | 2198 | Mask.push_back(IRB.getInt1(i >= BeginIndex && i < EndIndex)); |
| 2199 | |
| 2200 | V = IRB.CreateSelect(ConstantVector::get(Mask), V, Old, Name + "blend"); |
| 2201 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2202 | LLVM_DEBUG(dbgs() << " blend: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2203 | return V; |
| 2204 | } |
| 2205 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2206 | /// Visitor to rewrite instructions using p particular slice of an alloca |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2207 | /// to use a new alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2208 | /// |
| 2209 | /// Also implements the rewriting to vector-based accesses when the partition |
| 2210 | /// passes the isVectorPromotionViable predicate. Most of the rewriting logic |
| 2211 | /// lives here. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 2212 | class llvm::sroa::AllocaSliceRewriter |
| 2213 | : public InstVisitor<AllocaSliceRewriter, bool> { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2214 | // Befriend the base class so it can delegate to private visit methods. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2215 | friend class InstVisitor<AllocaSliceRewriter, bool>; |
| 2216 | |
| 2217 | using Base = InstVisitor<AllocaSliceRewriter, bool>; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2218 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2219 | const DataLayout &DL; |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 2220 | AllocaSlices &AS; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2221 | SROA &Pass; |
| 2222 | AllocaInst &OldAI, &NewAI; |
| 2223 | const uint64_t NewAllocaBeginOffset, NewAllocaEndOffset; |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2224 | Type *NewAllocaTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2225 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2226 | // This is a convenience and flag variable that will be null unless the new |
| 2227 | // alloca's integer operations should be widened to this integer type due to |
| 2228 | // passing isIntegerWideningViable above. If it is non-null, the desired |
| 2229 | // integer type will be stored here for easy access during rewriting. |
| 2230 | IntegerType *IntTy; |
| 2231 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2232 | // If we are rewriting an alloca partition which can be written as pure |
| 2233 | // vector operations, we stash extra information here. When VecTy is |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 2234 | // non-null, we have some strict guarantees about the rewritten alloca: |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2235 | // - The new alloca is exactly the size of the vector type here. |
| 2236 | // - The accesses all either map to the entire vector or to a single |
| 2237 | // element. |
| 2238 | // - The set of accessing instructions is only one of those handled above |
| 2239 | // in isVectorPromotionViable. Generally these are the same access kinds |
| 2240 | // which are promotable via mem2reg. |
| 2241 | VectorType *VecTy; |
| 2242 | Type *ElementTy; |
| 2243 | uint64_t ElementSize; |
| 2244 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2245 | // The original offset of the slice currently being rewritten relative to |
| 2246 | // the original alloca. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2247 | uint64_t BeginOffset = 0; |
| 2248 | uint64_t EndOffset = 0; |
| 2249 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2250 | // The new offsets of the slice currently being rewritten relative to the |
| 2251 | // original alloca. |
| 2252 | uint64_t NewBeginOffset, NewEndOffset; |
| 2253 | |
| 2254 | uint64_t SliceSize; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2255 | bool IsSplittable = false; |
| 2256 | bool IsSplit = false; |
| 2257 | Use *OldUse = nullptr; |
| 2258 | Instruction *OldPtr = nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2259 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 2260 | // Track post-rewrite users which are PHI nodes and Selects. |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 2261 | SmallSetVector<PHINode *, 8> &PHIUsers; |
| 2262 | SmallSetVector<SelectInst *, 8> &SelectUsers; |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2263 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2264 | // Utility IR builder, whose name prefix is setup for each visited use, and |
| 2265 | // the insertion point is set to point to the user. |
| 2266 | IRBuilderTy IRB; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2267 | |
| 2268 | public: |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 2269 | AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &AS, SROA &Pass, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2270 | AllocaInst &OldAI, AllocaInst &NewAI, |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2271 | uint64_t NewAllocaBeginOffset, |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2272 | uint64_t NewAllocaEndOffset, bool IsIntegerPromotable, |
| 2273 | VectorType *PromotableVecTy, |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 2274 | SmallSetVector<PHINode *, 8> &PHIUsers, |
| 2275 | SmallSetVector<SelectInst *, 8> &SelectUsers) |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 2276 | : DL(DL), AS(AS), Pass(Pass), OldAI(OldAI), NewAI(NewAI), |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2277 | NewAllocaBeginOffset(NewAllocaBeginOffset), |
| 2278 | NewAllocaEndOffset(NewAllocaEndOffset), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2279 | NewAllocaTy(NewAI.getAllocatedType()), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2280 | IntTy(IsIntegerPromotable |
| 2281 | ? Type::getIntNTy( |
| 2282 | NewAI.getContext(), |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2283 | DL.getTypeSizeInBits(NewAI.getAllocatedType())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2284 | : nullptr), |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2285 | VecTy(PromotableVecTy), |
| 2286 | ElementTy(VecTy ? VecTy->getElementType() : nullptr), |
| 2287 | ElementSize(VecTy ? DL.getTypeSizeInBits(ElementTy) / 8 : 0), |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2288 | PHIUsers(PHIUsers), SelectUsers(SelectUsers), |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2289 | IRB(NewAI.getContext(), ConstantFolder()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2290 | if (VecTy) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2291 | assert((DL.getTypeSizeInBits(ElementTy) % 8) == 0 && |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2292 | "Only multiple-of-8 sized vector elements are viable"); |
| 2293 | ++NumVectorized; |
| 2294 | } |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2295 | assert((!IntTy && !VecTy) || (IntTy && !VecTy) || (!IntTy && VecTy)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2296 | } |
| 2297 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2298 | bool visit(AllocaSlices::const_iterator I) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2299 | bool CanSROA = true; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2300 | BeginOffset = I->beginOffset(); |
| 2301 | EndOffset = I->endOffset(); |
| 2302 | IsSplittable = I->isSplittable(); |
| 2303 | IsSplit = |
| 2304 | BeginOffset < NewAllocaBeginOffset || EndOffset > NewAllocaEndOffset; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2305 | LLVM_DEBUG(dbgs() << " rewriting " << (IsSplit ? "split " : "")); |
| 2306 | LLVM_DEBUG(AS.printSlice(dbgs(), I, "")); |
| 2307 | LLVM_DEBUG(dbgs() << "\n"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2308 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2309 | // Compute the intersecting offset range. |
| 2310 | assert(BeginOffset < NewAllocaEndOffset); |
| 2311 | assert(EndOffset > NewAllocaBeginOffset); |
| 2312 | NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2313 | NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
| 2314 | |
| 2315 | SliceSize = NewEndOffset - NewBeginOffset; |
| 2316 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2317 | OldUse = I->getUse(); |
| 2318 | OldPtr = cast<Instruction>(OldUse->get()); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2319 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2320 | Instruction *OldUserI = cast<Instruction>(OldUse->getUser()); |
| 2321 | IRB.SetInsertPoint(OldUserI); |
| 2322 | IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc()); |
| 2323 | IRB.SetNamePrefix(Twine(NewAI.getName()) + "." + Twine(BeginOffset) + "."); |
| 2324 | |
| 2325 | CanSROA &= visit(cast<Instruction>(OldUse->getUser())); |
| 2326 | if (VecTy || IntTy) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2327 | assert(CanSROA); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2328 | return CanSROA; |
| 2329 | } |
| 2330 | |
| 2331 | private: |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2332 | // Make sure the other visit overloads are visible. |
| 2333 | using Base::visit; |
| 2334 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2335 | // Every instruction which can end up as a user must have a rewrite rule. |
| 2336 | bool visitInstruction(Instruction &I) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2337 | LLVM_DEBUG(dbgs() << " !!!! Cannot rewrite: " << I << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2338 | llvm_unreachable("No rewrite rule for this instruction!"); |
| 2339 | } |
| 2340 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2341 | Value *getNewAllocaSlicePtr(IRBuilderTy &IRB, Type *PointerTy) { |
| 2342 | // Note that the offset computation can use BeginOffset or NewBeginOffset |
| 2343 | // interchangeably for unsplit slices. |
| 2344 | assert(IsSplit || BeginOffset == NewBeginOffset); |
| 2345 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
| 2346 | |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2347 | #ifndef NDEBUG |
| 2348 | StringRef OldName = OldPtr->getName(); |
| 2349 | // Skip through the last '.sroa.' component of the name. |
| 2350 | size_t LastSROAPrefix = OldName.rfind(".sroa."); |
| 2351 | if (LastSROAPrefix != StringRef::npos) { |
| 2352 | OldName = OldName.substr(LastSROAPrefix + strlen(".sroa.")); |
| 2353 | // Look for an SROA slice index. |
| 2354 | size_t IndexEnd = OldName.find_first_not_of("0123456789"); |
| 2355 | if (IndexEnd != StringRef::npos && OldName[IndexEnd] == '.') { |
| 2356 | // Strip the index and look for the offset. |
| 2357 | OldName = OldName.substr(IndexEnd + 1); |
| 2358 | size_t OffsetEnd = OldName.find_first_not_of("0123456789"); |
| 2359 | if (OffsetEnd != StringRef::npos && OldName[OffsetEnd] == '.') |
| 2360 | // Strip the offset. |
| 2361 | OldName = OldName.substr(OffsetEnd + 1); |
| 2362 | } |
| 2363 | } |
| 2364 | // Strip any SROA suffixes as well. |
| 2365 | OldName = OldName.substr(0, OldName.find(".sroa_")); |
| 2366 | #endif |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2367 | |
| 2368 | return getAdjustedPtr(IRB, DL, &NewAI, |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 2369 | APInt(DL.getIndexTypeSizeInBits(PointerTy), Offset), |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2370 | PointerTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2371 | #ifndef NDEBUG |
| 2372 | Twine(OldName) + "." |
| 2373 | #else |
| 2374 | Twine() |
| 2375 | #endif |
| 2376 | ); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2379 | /// Compute suitable alignment to access this slice of the *new* |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2380 | /// alloca. |
Chandler Carruth | 2659e50 | 2014-02-26 05:02:19 +0000 | [diff] [blame] | 2381 | /// |
| 2382 | /// You can optionally pass a type to this routine and if that type's ABI |
| 2383 | /// alignment is itself suitable, this will return zero. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2384 | unsigned getSliceAlign(Type *Ty = nullptr) { |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2385 | unsigned NewAIAlign = NewAI.getAlignment(); |
| 2386 | if (!NewAIAlign) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2387 | NewAIAlign = DL.getABITypeAlignment(NewAI.getAllocatedType()); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2388 | unsigned Align = |
| 2389 | MinAlign(NewAIAlign, NewBeginOffset - NewAllocaBeginOffset); |
Chandler Carruth | 2659e50 | 2014-02-26 05:02:19 +0000 | [diff] [blame] | 2390 | return (Ty && Align == DL.getABITypeAlignment(Ty)) ? 0 : Align; |
Chandler Carruth | 4b2b38d | 2012-10-03 08:14:02 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2393 | unsigned getIndex(uint64_t Offset) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2394 | assert(VecTy && "Can only call getIndex when rewriting a vector"); |
| 2395 | uint64_t RelOffset = Offset - NewAllocaBeginOffset; |
| 2396 | assert(RelOffset / ElementSize < UINT32_MAX && "Index out of bounds"); |
| 2397 | uint32_t Index = RelOffset / ElementSize; |
| 2398 | assert(Index * ElementSize == RelOffset); |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2399 | return Index; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | void deleteIfTriviallyDead(Value *V) { |
| 2403 | Instruction *I = cast<Instruction>(V); |
| 2404 | if (isInstructionTriviallyDead(I)) |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2405 | Pass.DeadInsts.insert(I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2408 | Value *rewriteVectorizedLoadInst() { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2409 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2410 | unsigned EndIndex = getIndex(NewEndOffset); |
Chandler Carruth | 769445e | 2012-12-17 12:50:21 +0000 | [diff] [blame] | 2411 | assert(EndIndex > BeginIndex && "Empty vector!"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2412 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2413 | Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2414 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2415 | return extractVector(IRB, V, BeginIndex, EndIndex, "vec"); |
Chandler Carruth | 769445e | 2012-12-17 12:50:21 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2418 | Value *rewriteIntegerLoad(LoadInst &LI) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2419 | assert(IntTy && "We cannot insert an integer to the alloca"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2420 | assert(!LI.isVolatile()); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2421 | Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2422 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2423 | V = convertValue(DL, IRB, V, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2424 | assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); |
| 2425 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 4b682f6 | 2015-08-28 09:03:52 +0000 | [diff] [blame] | 2426 | if (Offset > 0 || NewEndOffset < NewAllocaEndOffset) { |
| 2427 | IntegerType *ExtractTy = Type::getIntNTy(LI.getContext(), SliceSize * 8); |
| 2428 | V = extractInteger(DL, IRB, V, ExtractTy, Offset, "extract"); |
| 2429 | } |
| 2430 | // It is possible that the extracted type is not the load type. This |
| 2431 | // happens if there is a load past the end of the alloca, and as |
| 2432 | // a consequence the slice is narrower but still a candidate for integer |
| 2433 | // lowering. To handle this case, we just zero extend the extracted |
| 2434 | // integer. |
| 2435 | assert(cast<IntegerType>(LI.getType())->getBitWidth() >= SliceSize * 8 && |
| 2436 | "Can only handle an extract for an overly wide load"); |
| 2437 | if (cast<IntegerType>(LI.getType())->getBitWidth() > SliceSize * 8) |
| 2438 | V = IRB.CreateZExt(V, LI.getType()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2439 | return V; |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2442 | bool visitLoadInst(LoadInst &LI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2443 | LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2444 | Value *OldOp = LI.getOperand(0); |
| 2445 | assert(OldOp == OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2446 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2447 | AAMDNodes AATags; |
| 2448 | LI.getAAMetadata(AATags); |
| 2449 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2450 | unsigned AS = LI.getPointerAddressSpace(); |
| 2451 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2452 | Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), SliceSize * 8) |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2453 | : LI.getType(); |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2454 | const bool IsLoadPastEnd = DL.getTypeStoreSize(TargetTy) > SliceSize; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2455 | bool IsPtrAdjusted = false; |
| 2456 | Value *V; |
| 2457 | if (VecTy) { |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2458 | V = rewriteVectorizedLoadInst(); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2459 | } else if (IntTy && LI.getType()->isIntegerTy()) { |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2460 | V = rewriteIntegerLoad(LI); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2461 | } else if (NewBeginOffset == NewAllocaBeginOffset && |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2462 | NewEndOffset == NewAllocaEndOffset && |
| 2463 | (canConvertValue(DL, NewAllocaTy, TargetTy) || |
| 2464 | (IsLoadPastEnd && NewAllocaTy->isIntegerTy() && |
| 2465 | TargetTy->isIntegerTy()))) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2466 | LoadInst *NewLI = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2467 | NewAI.getAlignment(), |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2468 | LI.isVolatile(), LI.getName()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2469 | if (AATags) |
| 2470 | NewLI->setAAMetadata(AATags); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2471 | if (LI.isVolatile()) |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 2472 | NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
Luqman Aden | 3f807c9 | 2017-03-22 19:16:39 +0000 | [diff] [blame] | 2473 | |
Chandler Carruth | 3f81d80 | 2017-06-27 08:32:03 +0000 | [diff] [blame] | 2474 | // Any !nonnull metadata or !range metadata on the old load is also valid |
| 2475 | // on the new load. This is even true in some cases even when the loads |
| 2476 | // are different types, for example by mapping !nonnull metadata to |
| 2477 | // !range metadata by modeling the null pointer constant converted to the |
| 2478 | // integer type. |
Rafael Espindola | c06f55e | 2017-11-28 01:25:38 +0000 | [diff] [blame] | 2479 | // FIXME: Add support for range metadata here. Currently the utilities |
| 2480 | // for this don't propagate range metadata in trivial cases from one |
| 2481 | // integer load to another, don't handle non-addrspace-0 null pointers |
| 2482 | // correctly, and don't have any support for mapping ranges as the |
| 2483 | // integer type becomes winder or narrower. |
Chandler Carruth | 3f81d80 | 2017-06-27 08:32:03 +0000 | [diff] [blame] | 2484 | if (MDNode *N = LI.getMetadata(LLVMContext::MD_nonnull)) |
| 2485 | copyNonnullMetadata(LI, N, *NewLI); |
Rafael Espindola | c06f55e | 2017-11-28 01:25:38 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Try to preserve nonnull metadata |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2488 | V = NewLI; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2489 | |
| 2490 | // If this is an integer load past the end of the slice (which means the |
| 2491 | // bytes outside the slice are undef or this load is dead) just forcibly |
| 2492 | // fix the integer size with correct handling of endianness. |
| 2493 | if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy)) |
| 2494 | if (auto *TITy = dyn_cast<IntegerType>(TargetTy)) |
| 2495 | if (AITy->getBitWidth() < TITy->getBitWidth()) { |
| 2496 | V = IRB.CreateZExt(V, TITy, "load.ext"); |
| 2497 | if (DL.isBigEndian()) |
| 2498 | V = IRB.CreateShl(V, TITy->getBitWidth() - AITy->getBitWidth(), |
| 2499 | "endian_shift"); |
| 2500 | } |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2501 | } else { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2502 | Type *LTy = TargetTy->getPointerTo(AS); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2503 | LoadInst *NewLI = IRB.CreateAlignedLoad( |
| 2504 | TargetTy, getNewAllocaSlicePtr(IRB, LTy), getSliceAlign(TargetTy), |
| 2505 | LI.isVolatile(), LI.getName()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2506 | if (AATags) |
| 2507 | NewLI->setAAMetadata(AATags); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2508 | if (LI.isVolatile()) |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 2509 | NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2510 | |
| 2511 | V = NewLI; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2512 | IsPtrAdjusted = true; |
| 2513 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2514 | V = convertValue(DL, IRB, V, TargetTy); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2515 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2516 | if (IsSplit) { |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2517 | assert(!LI.isVolatile()); |
| 2518 | assert(LI.getType()->isIntegerTy() && |
| 2519 | "Only integer type loads and stores are split"); |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2520 | assert(SliceSize < DL.getTypeStoreSize(LI.getType()) && |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2521 | "Split load isn't smaller than original load"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2522 | assert(LI.getType()->getIntegerBitWidth() == |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2523 | DL.getTypeStoreSizeInBits(LI.getType()) && |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2524 | "Non-byte-multiple bit width"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2525 | // Move the insertion point just past the load so that we can refer to it. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 2526 | IRB.SetInsertPoint(&*std::next(BasicBlock::iterator(&LI))); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2527 | // Create a placeholder value with the same type as LI to use as the |
| 2528 | // basis for the new value. This allows us to replace the uses of LI with |
| 2529 | // the computed value, and then replace the placeholder with LI, leaving |
| 2530 | // LI only used for this computation. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2531 | Value *Placeholder = new LoadInst( |
| 2532 | LI.getType(), UndefValue::get(LI.getType()->getPointerTo(AS))); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 2533 | V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset - BeginOffset, |
| 2534 | "insert"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2535 | LI.replaceAllUsesWith(V); |
| 2536 | Placeholder->replaceAllUsesWith(&LI); |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 2537 | Placeholder->deleteValue(); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2538 | } else { |
| 2539 | LI.replaceAllUsesWith(V); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2542 | Pass.DeadInsts.insert(&LI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2543 | deleteIfTriviallyDead(OldOp); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2544 | LLVM_DEBUG(dbgs() << " to: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2545 | return !LI.isVolatile() && !IsPtrAdjusted; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2548 | bool rewriteVectorizedStoreInst(Value *V, StoreInst &SI, Value *OldOp, |
| 2549 | AAMDNodes AATags) { |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2550 | if (V->getType() != VecTy) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2551 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2552 | unsigned EndIndex = getIndex(NewEndOffset); |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2553 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 2554 | unsigned NumElements = EndIndex - BeginIndex; |
| 2555 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2556 | Type *SliceTy = (NumElements == 1) |
| 2557 | ? ElementTy |
| 2558 | : VectorType::get(ElementTy, NumElements); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2559 | if (V->getType() != SliceTy) |
| 2560 | V = convertValue(DL, IRB, V, SliceTy); |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2561 | |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2562 | // Mix in the existing elements. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2563 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2564 | NewAI.getAlignment(), "load"); |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2565 | V = insertVector(IRB, Old, V, BeginIndex, "vec"); |
| 2566 | } |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2567 | StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2568 | if (AATags) |
| 2569 | Store->setAAMetadata(AATags); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2570 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2571 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2572 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2573 | return true; |
| 2574 | } |
| 2575 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2576 | bool rewriteIntegerStore(Value *V, StoreInst &SI, AAMDNodes AATags) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2577 | assert(IntTy && "We cannot extract an integer from the alloca"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2578 | assert(!SI.isVolatile()); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2579 | if (DL.getTypeSizeInBits(V->getType()) != IntTy->getBitWidth()) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2580 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2581 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2582 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2583 | assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); |
| 2584 | uint64_t Offset = BeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2585 | V = insertInteger(DL, IRB, Old, SI.getValueOperand(), Offset, "insert"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2586 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2587 | V = convertValue(DL, IRB, V, NewAllocaTy); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2588 | StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 2589 | Store->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access, |
| 2590 | LLVMContext::MD_access_group}); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2591 | if (AATags) |
| 2592 | Store->setAAMetadata(AATags); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2593 | Pass.DeadInsts.insert(&SI); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2594 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2595 | return true; |
| 2596 | } |
| 2597 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2598 | bool visitStoreInst(StoreInst &SI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2599 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2600 | Value *OldOp = SI.getOperand(1); |
| 2601 | assert(OldOp == OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2602 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2603 | AAMDNodes AATags; |
| 2604 | SI.getAAMetadata(AATags); |
| 2605 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2606 | Value *V = SI.getValueOperand(); |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2607 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 2608 | // Strip all inbounds GEPs and pointer casts to try to dig out any root |
| 2609 | // alloca that should be re-examined after promoting this alloca. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2610 | if (V->getType()->isPointerTy()) |
| 2611 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V->stripInBoundsOffsets())) |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 2612 | Pass.PostPromotionWorklist.insert(AI); |
| 2613 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2614 | if (SliceSize < DL.getTypeStoreSize(V->getType())) { |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2615 | assert(!SI.isVolatile()); |
| 2616 | assert(V->getType()->isIntegerTy() && |
| 2617 | "Only integer type loads and stores are split"); |
| 2618 | assert(V->getType()->getIntegerBitWidth() == |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2619 | DL.getTypeStoreSizeInBits(V->getType()) && |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2620 | "Non-byte-multiple bit width"); |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2621 | IntegerType *NarrowTy = Type::getIntNTy(SI.getContext(), SliceSize * 8); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 2622 | V = extractInteger(DL, IRB, V, NarrowTy, NewBeginOffset - BeginOffset, |
| 2623 | "extract"); |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2626 | if (VecTy) |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2627 | return rewriteVectorizedStoreInst(V, SI, OldOp, AATags); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2628 | if (IntTy && V->getType()->isIntegerTy()) |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2629 | return rewriteIntegerStore(V, SI, AATags); |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2630 | |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2631 | const bool IsStorePastEnd = DL.getTypeStoreSize(V->getType()) > SliceSize; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2632 | StoreInst *NewSI; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2633 | if (NewBeginOffset == NewAllocaBeginOffset && |
| 2634 | NewEndOffset == NewAllocaEndOffset && |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2635 | (canConvertValue(DL, V->getType(), NewAllocaTy) || |
| 2636 | (IsStorePastEnd && NewAllocaTy->isIntegerTy() && |
| 2637 | V->getType()->isIntegerTy()))) { |
| 2638 | // If this is an integer store past the end of slice (and thus the bytes |
| 2639 | // past that point are irrelevant or this is unreachable), truncate the |
| 2640 | // value prior to storing. |
| 2641 | if (auto *VITy = dyn_cast<IntegerType>(V->getType())) |
| 2642 | if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy)) |
| 2643 | if (VITy->getBitWidth() > AITy->getBitWidth()) { |
| 2644 | if (DL.isBigEndian()) |
| 2645 | V = IRB.CreateLShr(V, VITy->getBitWidth() - AITy->getBitWidth(), |
| 2646 | "endian_shift"); |
| 2647 | V = IRB.CreateTrunc(V, AITy, "load.trunc"); |
| 2648 | } |
| 2649 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2650 | V = convertValue(DL, IRB, V, NewAllocaTy); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2651 | NewSI = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), |
| 2652 | SI.isVolatile()); |
| 2653 | } else { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2654 | unsigned AS = SI.getPointerAddressSpace(); |
| 2655 | Value *NewPtr = getNewAllocaSlicePtr(IRB, V->getType()->getPointerTo(AS)); |
Chandler Carruth | 2659e50 | 2014-02-26 05:02:19 +0000 | [diff] [blame] | 2656 | NewSI = IRB.CreateAlignedStore(V, NewPtr, getSliceAlign(V->getType()), |
| 2657 | SI.isVolatile()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2658 | } |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 2659 | NewSI->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access, |
| 2660 | LLVMContext::MD_access_group}); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2661 | if (AATags) |
| 2662 | NewSI->setAAMetadata(AATags); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2663 | if (SI.isVolatile()) |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 2664 | NewSI->setAtomic(SI.getOrdering(), SI.getSyncScopeID()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2665 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2666 | deleteIfTriviallyDead(OldOp); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2667 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2668 | LLVM_DEBUG(dbgs() << " to: " << *NewSI << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2669 | return NewSI->getPointerOperand() == &NewAI && !SI.isVolatile(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2672 | /// Compute an integer value from splatting an i8 across the given |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2673 | /// number of bytes. |
| 2674 | /// |
| 2675 | /// Note that this routine assumes an i8 is a byte. If that isn't true, don't |
| 2676 | /// call this routine. |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 2677 | /// FIXME: Heed the advice above. |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2678 | /// |
| 2679 | /// \param V The i8 value to splat. |
| 2680 | /// \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] | 2681 | Value *getIntegerSplat(Value *V, unsigned Size) { |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2682 | assert(Size > 0 && "Expected a positive number of bytes."); |
| 2683 | IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 2684 | assert(VTy->getBitWidth() == 8 && "Expected an i8 value for the byte"); |
| 2685 | if (Size == 1) |
| 2686 | return V; |
| 2687 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2688 | Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8); |
| 2689 | V = IRB.CreateMul( |
| 2690 | IRB.CreateZExt(V, SplatIntTy, "zext"), |
| 2691 | ConstantExpr::getUDiv( |
| 2692 | Constant::getAllOnesValue(SplatIntTy), |
| 2693 | ConstantExpr::getZExt(Constant::getAllOnesValue(V->getType()), |
| 2694 | SplatIntTy)), |
| 2695 | "isplat"); |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2696 | return V; |
| 2697 | } |
| 2698 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2699 | /// Compute a vector splat for a given element value. |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2700 | Value *getVectorSplat(Value *V, unsigned NumElements) { |
| 2701 | V = IRB.CreateVectorSplat(NumElements, V, "vsplat"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2702 | LLVM_DEBUG(dbgs() << " splat: " << *V << "\n"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2703 | return V; |
| 2704 | } |
| 2705 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2706 | bool visitMemSetInst(MemSetInst &II) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2707 | LLVM_DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2708 | assert(II.getRawDest() == OldPtr); |
| 2709 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2710 | AAMDNodes AATags; |
| 2711 | II.getAAMetadata(AATags); |
| 2712 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2713 | // If the memset has a variable size, it cannot be split, just adjust the |
| 2714 | // pointer to the new alloca. |
| 2715 | if (!isa<Constant>(II.getLength())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2716 | assert(!IsSplit); |
Chandler Carruth | 735d5be | 2014-02-26 04:45:24 +0000 | [diff] [blame] | 2717 | assert(NewBeginOffset == BeginOffset); |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2718 | II.setDest(getNewAllocaSlicePtr(IRB, OldPtr->getType())); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2719 | II.setDestAlignment(getSliceAlign()); |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2720 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2721 | deleteIfTriviallyDead(OldPtr); |
| 2722 | return false; |
| 2723 | } |
| 2724 | |
| 2725 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2726 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2727 | |
| 2728 | Type *AllocaTy = NewAI.getAllocatedType(); |
| 2729 | Type *ScalarTy = AllocaTy->getScalarType(); |
| 2730 | |
| 2731 | // If this doesn't map cleanly onto the alloca type, and that type isn't |
| 2732 | // a single value type, just emit a memset. |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2733 | if (!VecTy && !IntTy && |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2734 | (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset || |
Reid Kleckner | c36f48f | 2014-08-22 00:09:56 +0000 | [diff] [blame] | 2735 | SliceSize != DL.getTypeStoreSize(AllocaTy) || |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2736 | !AllocaTy->isSingleValueType() || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2737 | !DL.isLegalInteger(DL.getTypeSizeInBits(ScalarTy)) || |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2738 | DL.getTypeSizeInBits(ScalarTy) % 8 != 0)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2739 | Type *SizeTy = II.getLength()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2740 | Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset); |
| 2741 | CallInst *New = IRB.CreateMemSet( |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2742 | getNewAllocaSlicePtr(IRB, OldPtr->getType()), II.getValue(), Size, |
| 2743 | getSliceAlign(), II.isVolatile()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2744 | if (AATags) |
| 2745 | New->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2746 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2747 | return false; |
| 2748 | } |
| 2749 | |
| 2750 | // If we can represent this as a simple value, we have to build the actual |
| 2751 | // value to store, which requires expanding the byte present in memset to |
| 2752 | // a sensible representation for the alloca type. This is essentially |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2753 | // splatting the byte to a sufficiently wide integer, splatting it across |
| 2754 | // any desired vector width, and bitcasting to the final type. |
Benjamin Kramer | c003a45 | 2013-01-01 16:13:35 +0000 | [diff] [blame] | 2755 | Value *V; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2756 | |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2757 | if (VecTy) { |
| 2758 | // If this is a memset of a vectorized alloca, insert it. |
| 2759 | assert(ElementTy == ScalarTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2760 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2761 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2762 | unsigned EndIndex = getIndex(NewEndOffset); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2763 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 2764 | unsigned NumElements = EndIndex - BeginIndex; |
| 2765 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
| 2766 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2767 | Value *Splat = |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2768 | getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ElementTy) / 8); |
| 2769 | Splat = convertValue(DL, IRB, Splat, ElementTy); |
Chandler Carruth | cacda25 | 2012-12-17 14:03:01 +0000 | [diff] [blame] | 2770 | if (NumElements > 1) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2771 | Splat = getVectorSplat(Splat, NumElements); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2772 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2773 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2774 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2775 | V = insertVector(IRB, Old, Splat, BeginIndex, "vec"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2776 | } else if (IntTy) { |
| 2777 | // If this is a memset on an alloca where we can widen stores, insert the |
| 2778 | // set integer. |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2779 | assert(!II.isVolatile()); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2780 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2781 | uint64_t Size = NewEndOffset - NewBeginOffset; |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2782 | V = getIntegerSplat(II.getValue(), Size); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2783 | |
| 2784 | if (IntTy && (BeginOffset != NewAllocaBeginOffset || |
| 2785 | EndOffset != NewAllocaBeginOffset)) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2786 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2787 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2788 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2789 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2790 | V = insertInteger(DL, IRB, Old, V, Offset, "insert"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2791 | } else { |
| 2792 | assert(V->getType() == IntTy && |
| 2793 | "Wrong type for an alloca wide integer!"); |
| 2794 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2795 | V = convertValue(DL, IRB, V, AllocaTy); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2796 | } else { |
| 2797 | // Established these invariants above. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2798 | assert(NewBeginOffset == NewAllocaBeginOffset); |
| 2799 | assert(NewEndOffset == NewAllocaEndOffset); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2800 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2801 | V = getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ScalarTy) / 8); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2802 | if (VectorType *AllocaVecTy = dyn_cast<VectorType>(AllocaTy)) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2803 | V = getVectorSplat(V, AllocaVecTy->getNumElements()); |
Chandler Carruth | 95e1fb8 | 2012-12-17 13:51:03 +0000 | [diff] [blame] | 2804 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2805 | V = convertValue(DL, IRB, V, AllocaTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2808 | StoreInst *New = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), |
| 2809 | II.isVolatile()); |
| 2810 | if (AATags) |
| 2811 | New->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2812 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2813 | return !II.isVolatile(); |
| 2814 | } |
| 2815 | |
| 2816 | bool visitMemTransferInst(MemTransferInst &II) { |
| 2817 | // Rewriting of memory transfer instructions can be a bit tricky. We break |
| 2818 | // them into two categories: split intrinsics and unsplit intrinsics. |
| 2819 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2820 | LLVM_DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2821 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2822 | AAMDNodes AATags; |
| 2823 | II.getAAMetadata(AATags); |
| 2824 | |
Chandler Carruth | bb2a932 | 2014-02-25 03:50:14 +0000 | [diff] [blame] | 2825 | bool IsDest = &II.getRawDestUse() == OldUse; |
Alexey Samsonov | 26af6f7 | 2014-02-25 07:56:00 +0000 | [diff] [blame] | 2826 | assert((IsDest && II.getRawDest() == OldPtr) || |
Chandler Carruth | bb2a932 | 2014-02-25 03:50:14 +0000 | [diff] [blame] | 2827 | (!IsDest && II.getRawSource() == OldPtr)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2828 | |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 2829 | unsigned SliceAlign = getSliceAlign(); |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2830 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2831 | // For unsplit intrinsics, we simply modify the source and destination |
| 2832 | // pointers in place. This isn't just an optimization, it is a matter of |
| 2833 | // correctness. With unsplit intrinsics we may be dealing with transfers |
| 2834 | // within a single alloca before SROA ran, or with transfers that have |
| 2835 | // a variable length. We may also be dealing with memmove instead of |
| 2836 | // memcpy, and so simply updating the pointers is the necessary for us to |
| 2837 | // update both source and dest of a single call. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2838 | if (!IsSplittable) { |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2839 | Value *AdjustedPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType()); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2840 | if (IsDest) { |
Chandler Carruth | 8183a50 | 2014-02-25 11:08:02 +0000 | [diff] [blame] | 2841 | II.setDest(AdjustedPtr); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2842 | II.setDestAlignment(SliceAlign); |
| 2843 | } |
| 2844 | else { |
Chandler Carruth | 8183a50 | 2014-02-25 11:08:02 +0000 | [diff] [blame] | 2845 | II.setSource(AdjustedPtr); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2846 | II.setSourceAlignment(SliceAlign); |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2847 | } |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2848 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2849 | LLVM_DEBUG(dbgs() << " to: " << II << "\n"); |
Chandler Carruth | 8183a50 | 2014-02-25 11:08:02 +0000 | [diff] [blame] | 2850 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2851 | return false; |
| 2852 | } |
| 2853 | // For split transfer intrinsics we have an incredibly useful assurance: |
| 2854 | // the source and destination do not reside within the same alloca, and at |
| 2855 | // least one of them does not escape. This means that we can replace |
| 2856 | // memmove with memcpy, and we don't need to worry about all manner of |
| 2857 | // downsides to splitting and transforming the operations. |
| 2858 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2859 | // If this doesn't map cleanly onto the alloca type, and that type isn't |
| 2860 | // a single value type, just emit a memcpy. |
Reid Kleckner | c36f48f | 2014-08-22 00:09:56 +0000 | [diff] [blame] | 2861 | bool EmitMemCpy = |
| 2862 | !VecTy && !IntTy && |
| 2863 | (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset || |
| 2864 | SliceSize != DL.getTypeStoreSize(NewAI.getAllocatedType()) || |
| 2865 | !NewAI.getAllocatedType()->isSingleValueType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2866 | |
| 2867 | // If we're just going to emit a memcpy, the alloca hasn't changed, and the |
| 2868 | // size hasn't been shrunk based on analysis of the viable range, this is |
| 2869 | // a no-op. |
| 2870 | if (EmitMemCpy && &OldAI == &NewAI) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2871 | // Ensure the start lines up. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2872 | assert(NewBeginOffset == BeginOffset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2873 | |
| 2874 | // Rewrite the size as needed. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2875 | if (NewEndOffset != EndOffset) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2876 | II.setLength(ConstantInt::get(II.getLength()->getType(), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2877 | NewEndOffset - NewBeginOffset)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2878 | return false; |
| 2879 | } |
| 2880 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2881 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2882 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2883 | // Strip all inbounds GEPs and pointer casts to try to dig out any root |
| 2884 | // alloca that should be re-examined after rewriting this instruction. |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2885 | Value *OtherPtr = IsDest ? II.getRawSource() : II.getRawDest(); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2886 | if (AllocaInst *AI = |
| 2887 | dyn_cast<AllocaInst>(OtherPtr->stripInBoundsOffsets())) { |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 2888 | assert(AI != &OldAI && AI != &NewAI && |
| 2889 | "Splittable transfers cannot reach the same alloca on both ends."); |
Chandler Carruth | 4bd8f66 | 2012-09-26 07:41:40 +0000 | [diff] [blame] | 2890 | Pass.Worklist.insert(AI); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 2891 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2892 | |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 2893 | Type *OtherPtrTy = OtherPtr->getType(); |
| 2894 | unsigned OtherAS = OtherPtrTy->getPointerAddressSpace(); |
| 2895 | |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2896 | // Compute the relative offset for the other pointer within the transfer. |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 2897 | unsigned OffsetWidth = DL.getIndexSizeInBits(OtherAS); |
| 2898 | APInt OtherOffset(OffsetWidth, NewBeginOffset - BeginOffset); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2899 | unsigned OtherAlign = |
| 2900 | IsDest ? II.getSourceAlignment() : II.getDestAlignment(); |
| 2901 | OtherAlign = MinAlign(OtherAlign ? OtherAlign : 1, |
| 2902 | OtherOffset.zextOrTrunc(64).getZExtValue()); |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2903 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2904 | if (EmitMemCpy) { |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2905 | // Compute the other pointer, folding as much as possible to produce |
| 2906 | // a single, simple GEP in most cases. |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2907 | OtherPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2908 | OtherPtr->getName() + "."); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2909 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2910 | Value *OurPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2911 | Type *SizeTy = II.getLength()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2912 | Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2913 | |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2914 | Value *DestPtr, *SrcPtr; |
| 2915 | unsigned DestAlign, SrcAlign; |
| 2916 | // Note: IsDest is true iff we're copying into the new alloca slice |
| 2917 | if (IsDest) { |
| 2918 | DestPtr = OurPtr; |
| 2919 | DestAlign = SliceAlign; |
| 2920 | SrcPtr = OtherPtr; |
| 2921 | SrcAlign = OtherAlign; |
| 2922 | } else { |
| 2923 | DestPtr = OtherPtr; |
| 2924 | DestAlign = OtherAlign; |
| 2925 | SrcPtr = OurPtr; |
| 2926 | SrcAlign = SliceAlign; |
| 2927 | } |
| 2928 | CallInst *New = IRB.CreateMemCpy(DestPtr, DestAlign, SrcPtr, SrcAlign, |
| 2929 | Size, II.isVolatile()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2930 | if (AATags) |
| 2931 | New->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2932 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2933 | return false; |
| 2934 | } |
| 2935 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2936 | bool IsWholeAlloca = NewBeginOffset == NewAllocaBeginOffset && |
| 2937 | NewEndOffset == NewAllocaEndOffset; |
| 2938 | uint64_t Size = NewEndOffset - NewBeginOffset; |
| 2939 | unsigned BeginIndex = VecTy ? getIndex(NewBeginOffset) : 0; |
| 2940 | unsigned EndIndex = VecTy ? getIndex(NewEndOffset) : 0; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2941 | unsigned NumElements = EndIndex - BeginIndex; |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2942 | IntegerType *SubIntTy = |
| 2943 | IntTy ? Type::getIntNTy(IntTy->getContext(), Size * 8) : nullptr; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2944 | |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 2945 | // Reset the other pointer type to match the register type we're going to |
| 2946 | // use, but using the address space of the original other pointer. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2947 | Type *OtherTy; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2948 | if (VecTy && !IsWholeAlloca) { |
| 2949 | if (NumElements == 1) |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2950 | OtherTy = VecTy->getElementType(); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2951 | else |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2952 | OtherTy = VectorType::get(VecTy->getElementType(), NumElements); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2953 | } else if (IntTy && !IsWholeAlloca) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2954 | OtherTy = SubIntTy; |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 2955 | } else { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2956 | OtherTy = NewAllocaTy; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2957 | } |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2958 | OtherPtrTy = OtherTy->getPointerTo(OtherAS); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2959 | |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2960 | Value *SrcPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2961 | OtherPtr->getName() + "."); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 2962 | unsigned SrcAlign = OtherAlign; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2963 | Value *DstPtr = &NewAI; |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 2964 | unsigned DstAlign = SliceAlign; |
| 2965 | if (!IsDest) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2966 | std::swap(SrcPtr, DstPtr); |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 2967 | std::swap(SrcAlign, DstAlign); |
| 2968 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2969 | |
| 2970 | Value *Src; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2971 | if (VecTy && !IsWholeAlloca && !IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2972 | Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2973 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2974 | Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec"); |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 2975 | } else if (IntTy && !IsWholeAlloca && !IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2976 | Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2977 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2978 | Src = convertValue(DL, IRB, Src, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2979 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2980 | Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2981 | } else { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2982 | LoadInst *Load = IRB.CreateAlignedLoad(OtherTy, SrcPtr, SrcAlign, |
| 2983 | II.isVolatile(), "copyload"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2984 | if (AATags) |
| 2985 | Load->setAAMetadata(AATags); |
| 2986 | Src = Load; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2989 | if (VecTy && !IsWholeAlloca && IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2990 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2991 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2992 | Src = insertVector(IRB, Old, Src, BeginIndex, "vec"); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2993 | } else if (IntTy && !IsWholeAlloca && IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 2994 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2995 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2996 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2997 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2998 | Src = insertInteger(DL, IRB, Old, Src, Offset, "insert"); |
| 2999 | Src = convertValue(DL, IRB, Src, NewAllocaTy); |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 3002 | StoreInst *Store = cast<StoreInst>( |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 3003 | IRB.CreateAlignedStore(Src, DstPtr, DstAlign, II.isVolatile())); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3004 | if (AATags) |
| 3005 | Store->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3006 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3007 | return !II.isVolatile(); |
| 3008 | } |
| 3009 | |
| 3010 | bool visitIntrinsicInst(IntrinsicInst &II) { |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 3011 | assert(II.isLifetimeStartOrEnd()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3012 | LLVM_DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3013 | assert(II.getArgOperand(1) == OldPtr); |
| 3014 | |
| 3015 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 3016 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3017 | |
Eli Friedman | 5096775 | 2016-11-28 21:50:34 +0000 | [diff] [blame] | 3018 | // Lifetime intrinsics are only promotable if they cover the whole alloca. |
| 3019 | // Therefore, we drop lifetime intrinsics which don't cover the whole |
| 3020 | // alloca. |
| 3021 | // (In theory, intrinsics which partially cover an alloca could be |
| 3022 | // promoted, but PromoteMemToReg doesn't handle that case.) |
| 3023 | // FIXME: Check whether the alloca is promotable before dropping the |
| 3024 | // lifetime intrinsics? |
| 3025 | if (NewBeginOffset != NewAllocaBeginOffset || |
| 3026 | NewEndOffset != NewAllocaEndOffset) |
| 3027 | return true; |
| 3028 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3029 | ConstantInt *Size = |
| 3030 | ConstantInt::get(cast<IntegerType>(II.getArgOperand(0)->getType()), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3031 | NewEndOffset - NewBeginOffset); |
Gabor Buella | 3ec170c | 2019-01-16 12:06:17 +0000 | [diff] [blame] | 3032 | // Lifetime intrinsics always expect an i8* so directly get such a pointer |
| 3033 | // for the new alloca slice. |
| 3034 | Type *PointerTy = IRB.getInt8PtrTy(OldPtr->getType()->getPointerAddressSpace()); |
| 3035 | Value *Ptr = getNewAllocaSlicePtr(IRB, PointerTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3036 | Value *New; |
| 3037 | if (II.getIntrinsicID() == Intrinsic::lifetime_start) |
| 3038 | New = IRB.CreateLifetimeStart(Ptr, Size); |
| 3039 | else |
| 3040 | New = IRB.CreateLifetimeEnd(Ptr, Size); |
| 3041 | |
Edwin Vane | 82f80d4 | 2013-01-29 17:42:24 +0000 | [diff] [blame] | 3042 | (void)New; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3043 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Eli Friedman | 2a65dd1 | 2016-08-08 01:30:53 +0000 | [diff] [blame] | 3044 | |
Eli Friedman | 5096775 | 2016-11-28 21:50:34 +0000 | [diff] [blame] | 3045 | return true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3048 | void fixLoadStoreAlign(Instruction &Root) { |
| 3049 | // This algorithm implements the same visitor loop as |
| 3050 | // hasUnsafePHIOrSelectUse, and fixes the alignment of each load |
| 3051 | // or store found. |
| 3052 | SmallPtrSet<Instruction *, 4> Visited; |
| 3053 | SmallVector<Instruction *, 4> Uses; |
| 3054 | Visited.insert(&Root); |
| 3055 | Uses.push_back(&Root); |
| 3056 | do { |
| 3057 | Instruction *I = Uses.pop_back_val(); |
| 3058 | |
| 3059 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 3060 | unsigned LoadAlign = LI->getAlignment(); |
| 3061 | if (!LoadAlign) |
| 3062 | LoadAlign = DL.getABITypeAlignment(LI->getType()); |
| 3063 | LI->setAlignment(std::min(LoadAlign, getSliceAlign())); |
| 3064 | continue; |
| 3065 | } |
| 3066 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 3067 | unsigned StoreAlign = SI->getAlignment(); |
| 3068 | if (!StoreAlign) { |
| 3069 | Value *Op = SI->getOperand(0); |
| 3070 | StoreAlign = DL.getABITypeAlignment(Op->getType()); |
| 3071 | } |
| 3072 | SI->setAlignment(std::min(StoreAlign, getSliceAlign())); |
| 3073 | continue; |
| 3074 | } |
| 3075 | |
| 3076 | assert(isa<BitCastInst>(I) || isa<PHINode>(I) || |
| 3077 | isa<SelectInst>(I) || isa<GetElementPtrInst>(I)); |
| 3078 | for (User *U : I->users()) |
| 3079 | if (Visited.insert(cast<Instruction>(U)).second) |
| 3080 | Uses.push_back(cast<Instruction>(U)); |
| 3081 | } while (!Uses.empty()); |
| 3082 | } |
| 3083 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3084 | bool visitPHINode(PHINode &PN) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3085 | LLVM_DEBUG(dbgs() << " original: " << PN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3086 | assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable"); |
| 3087 | assert(EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3088 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3089 | // We would like to compute a new pointer in only one place, but have it be |
| 3090 | // as local as possible to the PHI. To do that, we re-use the location of |
| 3091 | // the old pointer, which necessarily must be in the right position to |
| 3092 | // dominate the PHI. |
Chandler Carruth | 5117553 | 2014-02-25 11:12:04 +0000 | [diff] [blame] | 3093 | IRBuilderTy PtrBuilder(IRB); |
David Majnemer | d4cffcf | 2014-09-01 21:20:14 +0000 | [diff] [blame] | 3094 | if (isa<PHINode>(OldPtr)) |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3095 | PtrBuilder.SetInsertPoint(&*OldPtr->getParent()->getFirstInsertionPt()); |
David Majnemer | d4cffcf | 2014-09-01 21:20:14 +0000 | [diff] [blame] | 3096 | else |
| 3097 | PtrBuilder.SetInsertPoint(OldPtr); |
Chandler Carruth | 5117553 | 2014-02-25 11:12:04 +0000 | [diff] [blame] | 3098 | PtrBuilder.SetCurrentDebugLocation(OldPtr->getDebugLoc()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3099 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 3100 | Value *NewPtr = getNewAllocaSlicePtr(PtrBuilder, OldPtr->getType()); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3101 | // Replace the operands which were using the old pointer. |
Benjamin Kramer | 7ddd705 | 2012-10-20 12:04:57 +0000 | [diff] [blame] | 3102 | std::replace(PN.op_begin(), PN.op_end(), cast<Value>(OldPtr), NewPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3103 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3104 | LLVM_DEBUG(dbgs() << " to: " << PN << "\n"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3105 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3106 | |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3107 | // Fix the alignment of any loads or stores using this PHI node. |
| 3108 | fixLoadStoreAlign(PN); |
| 3109 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 3110 | // PHIs can't be promoted on their own, but often can be speculated. We |
| 3111 | // check the speculation outside of the rewriter so that we see the |
| 3112 | // fully-rewritten alloca. |
| 3113 | PHIUsers.insert(&PN); |
| 3114 | return true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | bool visitSelectInst(SelectInst &SI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3118 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Benjamin Kramer | 0212dc2 | 2013-04-21 17:48:39 +0000 | [diff] [blame] | 3119 | assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) && |
| 3120 | "Pointer isn't an operand!"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3121 | assert(BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable"); |
| 3122 | assert(EndOffset <= NewAllocaEndOffset && "Selects are unsplittable"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3123 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 3124 | Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType()); |
Benjamin Kramer | 0212dc2 | 2013-04-21 17:48:39 +0000 | [diff] [blame] | 3125 | // Replace the operands which were using the old pointer. |
| 3126 | if (SI.getOperand(1) == OldPtr) |
| 3127 | SI.setOperand(1, NewPtr); |
| 3128 | if (SI.getOperand(2) == OldPtr) |
| 3129 | SI.setOperand(2, NewPtr); |
| 3130 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3131 | LLVM_DEBUG(dbgs() << " to: " << SI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3132 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3133 | |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3134 | // Fix the alignment of any loads or stores using this select. |
| 3135 | fixLoadStoreAlign(SI); |
| 3136 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 3137 | // Selects can't be promoted on their own, but often can be speculated. We |
| 3138 | // check the speculation outside of the rewriter so that we see the |
| 3139 | // fully-rewritten alloca. |
| 3140 | SelectUsers.insert(&SI); |
| 3141 | return true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3142 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3143 | }; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3144 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3145 | namespace { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3146 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3147 | /// Visitor to rewrite aggregate loads and stores as scalar. |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3148 | /// |
| 3149 | /// This pass aggressively rewrites all aggregate loads and stores on |
| 3150 | /// a particular pointer (or any pointer derived from it which we can identify) |
| 3151 | /// with scalar loads and stores. |
| 3152 | class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> { |
| 3153 | // Befriend the base class so it can delegate to private visit methods. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3154 | friend class InstVisitor<AggLoadStoreRewriter, bool>; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3155 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3156 | /// Queue of pointer uses to analyze and potentially rewrite. |
| 3157 | SmallVector<Use *, 8> Queue; |
| 3158 | |
| 3159 | /// Set to prevent us from cycling with phi nodes and loops. |
| 3160 | SmallPtrSet<User *, 8> Visited; |
| 3161 | |
| 3162 | /// The current pointer use being rewritten. This is used to dig up the used |
| 3163 | /// value (as opposed to the user). |
| 3164 | Use *U; |
| 3165 | |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3166 | /// Used to calculate offsets, and hence alignment, of subobjects. |
| 3167 | const DataLayout &DL; |
| 3168 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3169 | public: |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3170 | AggLoadStoreRewriter(const DataLayout &DL) : DL(DL) {} |
| 3171 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3172 | /// Rewrite loads and stores through a pointer and all pointers derived from |
| 3173 | /// it. |
| 3174 | bool rewrite(Instruction &I) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3175 | LLVM_DEBUG(dbgs() << " Rewriting FCA loads and stores...\n"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3176 | enqueueUsers(I); |
| 3177 | bool Changed = false; |
| 3178 | while (!Queue.empty()) { |
| 3179 | U = Queue.pop_back_val(); |
| 3180 | Changed |= visit(cast<Instruction>(U->getUser())); |
| 3181 | } |
| 3182 | return Changed; |
| 3183 | } |
| 3184 | |
| 3185 | private: |
| 3186 | /// Enqueue all the users of the given instruction for further processing. |
| 3187 | /// This uses a set to de-duplicate users. |
| 3188 | void enqueueUsers(Instruction &I) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3189 | for (Use &U : I.uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3190 | if (Visited.insert(U.getUser()).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3191 | Queue.push_back(&U); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | // Conservative default is to not rewrite anything. |
| 3195 | bool visitInstruction(Instruction &I) { return false; } |
| 3196 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3197 | /// Generic recursive split emission class. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3198 | template <typename Derived> class OpSplitter { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3199 | protected: |
| 3200 | /// The builder used to form new instructions. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 3201 | IRBuilderTy IRB; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3202 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3203 | /// The indices which to be used with insert- or extractvalue to select the |
| 3204 | /// appropriate value within the aggregate. |
| 3205 | SmallVector<unsigned, 4> Indices; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3206 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3207 | /// The indices to a GEP instruction which will move Ptr to the correct slot |
| 3208 | /// within the aggregate. |
| 3209 | SmallVector<Value *, 4> GEPIndices; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3210 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3211 | /// The base pointer of the original op, used as a base for GEPing the |
| 3212 | /// split operations. |
| 3213 | Value *Ptr; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3214 | |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3215 | /// The base pointee type being GEPed into. |
| 3216 | Type *BaseTy; |
| 3217 | |
| 3218 | /// Known alignment of the base pointer. |
| 3219 | unsigned BaseAlign; |
| 3220 | |
| 3221 | /// To calculate offset of each component so we can correctly deduce |
| 3222 | /// alignments. |
| 3223 | const DataLayout &DL; |
| 3224 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3225 | /// Initialize the splitter with an insertion point, Ptr and start with a |
| 3226 | /// single zero GEP index. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3227 | OpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy, |
| 3228 | unsigned BaseAlign, const DataLayout &DL) |
| 3229 | : IRB(InsertionPoint), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr), |
| 3230 | BaseTy(BaseTy), BaseAlign(BaseAlign), DL(DL) {} |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3231 | |
| 3232 | public: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3233 | /// Generic recursive split emission routine. |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3234 | /// |
| 3235 | /// This method recursively splits an aggregate op (load or store) into |
| 3236 | /// scalar or vector ops. It splits recursively until it hits a single value |
| 3237 | /// and emits that single value operation via the template argument. |
| 3238 | /// |
| 3239 | /// The logic of this routine relies on GEPs and insertvalue and |
| 3240 | /// extractvalue all operating with the same fundamental index list, merely |
| 3241 | /// formatted differently (GEPs need actual values). |
| 3242 | /// |
| 3243 | /// \param Ty The type being split recursively into smaller ops. |
| 3244 | /// \param Agg The aggregate value being built up or stored, depending on |
| 3245 | /// whether this is splitting a load or a store respectively. |
| 3246 | void emitSplitOps(Type *Ty, Value *&Agg, const Twine &Name) { |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3247 | if (Ty->isSingleValueType()) { |
| 3248 | unsigned Offset = DL.getIndexedOffsetInType(BaseTy, GEPIndices); |
| 3249 | return static_cast<Derived *>(this)->emitFunc( |
| 3250 | Ty, Agg, MinAlign(BaseAlign, Offset), Name); |
| 3251 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3252 | |
| 3253 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 3254 | unsigned OldSize = Indices.size(); |
| 3255 | (void)OldSize; |
| 3256 | for (unsigned Idx = 0, Size = ATy->getNumElements(); Idx != Size; |
| 3257 | ++Idx) { |
| 3258 | assert(Indices.size() == OldSize && "Did not return to the old size"); |
| 3259 | Indices.push_back(Idx); |
| 3260 | GEPIndices.push_back(IRB.getInt32(Idx)); |
| 3261 | emitSplitOps(ATy->getElementType(), Agg, Name + "." + Twine(Idx)); |
| 3262 | GEPIndices.pop_back(); |
| 3263 | Indices.pop_back(); |
| 3264 | } |
| 3265 | return; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3266 | } |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3267 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3268 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 3269 | unsigned OldSize = Indices.size(); |
| 3270 | (void)OldSize; |
| 3271 | for (unsigned Idx = 0, Size = STy->getNumElements(); Idx != Size; |
| 3272 | ++Idx) { |
| 3273 | assert(Indices.size() == OldSize && "Did not return to the old size"); |
| 3274 | Indices.push_back(Idx); |
| 3275 | GEPIndices.push_back(IRB.getInt32(Idx)); |
| 3276 | emitSplitOps(STy->getElementType(Idx), Agg, Name + "." + Twine(Idx)); |
| 3277 | GEPIndices.pop_back(); |
| 3278 | Indices.pop_back(); |
| 3279 | } |
| 3280 | return; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3281 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3282 | |
| 3283 | llvm_unreachable("Only arrays and structs are aggregate loadable types"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3284 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3285 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3286 | |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 3287 | struct LoadOpSplitter : public OpSplitter<LoadOpSplitter> { |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3288 | AAMDNodes AATags; |
| 3289 | |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3290 | LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy, |
| 3291 | AAMDNodes AATags, unsigned BaseAlign, const DataLayout &DL) |
| 3292 | : OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign, |
| 3293 | DL), AATags(AATags) {} |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3294 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3295 | /// Emit a leaf load of a single value. This is called at the leaves of the |
| 3296 | /// recursive emission to actually load values. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3297 | void emitFunc(Type *Ty, Value *&Agg, unsigned Align, const Twine &Name) { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3298 | assert(Ty->isSingleValueType()); |
| 3299 | // Load the single value and insert it using the indices. |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 3300 | Value *GEP = |
| 3301 | IRB.CreateInBoundsGEP(nullptr, Ptr, GEPIndices, Name + ".gep"); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 3302 | LoadInst *Load = IRB.CreateAlignedLoad(Ty, GEP, Align, Name + ".load"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3303 | if (AATags) |
| 3304 | Load->setAAMetadata(AATags); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3305 | Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3306 | LLVM_DEBUG(dbgs() << " to: " << *Load << "\n"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3307 | } |
| 3308 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3309 | |
| 3310 | bool visitLoadInst(LoadInst &LI) { |
| 3311 | assert(LI.getPointerOperand() == *U); |
| 3312 | if (!LI.isSimple() || LI.getType()->isSingleValueType()) |
| 3313 | return false; |
| 3314 | |
| 3315 | // We have an aggregate being loaded, split it apart. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3316 | LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3317 | AAMDNodes AATags; |
| 3318 | LI.getAAMetadata(AATags); |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3319 | LoadOpSplitter Splitter(&LI, *U, LI.getType(), AATags, |
| 3320 | getAdjustedAlignment(&LI, 0, DL), DL); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3321 | Value *V = UndefValue::get(LI.getType()); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3322 | Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3323 | LI.replaceAllUsesWith(V); |
| 3324 | LI.eraseFromParent(); |
| 3325 | return true; |
| 3326 | } |
| 3327 | |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 3328 | struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> { |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3329 | StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy, |
| 3330 | AAMDNodes AATags, unsigned BaseAlign, const DataLayout &DL) |
| 3331 | : OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign, |
| 3332 | DL), |
| 3333 | AATags(AATags) {} |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3334 | AAMDNodes AATags; |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3335 | /// Emit a leaf store of a single value. This is called at the leaves of the |
| 3336 | /// recursive emission to actually produce stores. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3337 | void emitFunc(Type *Ty, Value *&Agg, unsigned Align, const Twine &Name) { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3338 | assert(Ty->isSingleValueType()); |
| 3339 | // Extract the single value and store it using the indices. |
Patrik Hagglund | a83706e | 2016-06-20 10:19:00 +0000 | [diff] [blame] | 3340 | // |
| 3341 | // The gep and extractvalue values are factored out of the CreateStore |
| 3342 | // call to make the output independent of the argument evaluation order. |
Patrik Hagglund | 4e0bd84 | 2016-06-20 11:19:58 +0000 | [diff] [blame] | 3343 | Value *ExtractValue = |
| 3344 | IRB.CreateExtractValue(Agg, Indices, Name + ".extract"); |
| 3345 | Value *InBoundsGEP = |
| 3346 | IRB.CreateInBoundsGEP(nullptr, Ptr, GEPIndices, Name + ".gep"); |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3347 | StoreInst *Store = |
| 3348 | IRB.CreateAlignedStore(ExtractValue, InBoundsGEP, Align); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3349 | if (AATags) |
| 3350 | Store->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3351 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3352 | } |
| 3353 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3354 | |
| 3355 | bool visitStoreInst(StoreInst &SI) { |
| 3356 | if (!SI.isSimple() || SI.getPointerOperand() != *U) |
| 3357 | return false; |
| 3358 | Value *V = SI.getValueOperand(); |
| 3359 | if (V->getType()->isSingleValueType()) |
| 3360 | return false; |
| 3361 | |
| 3362 | // We have an aggregate being stored, split it apart. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3363 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3364 | AAMDNodes AATags; |
| 3365 | SI.getAAMetadata(AATags); |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3366 | StoreOpSplitter Splitter(&SI, *U, V->getType(), AATags, |
| 3367 | getAdjustedAlignment(&SI, 0, DL), DL); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3368 | Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3369 | SI.eraseFromParent(); |
| 3370 | return true; |
| 3371 | } |
| 3372 | |
| 3373 | bool visitBitCastInst(BitCastInst &BC) { |
| 3374 | enqueueUsers(BC); |
| 3375 | return false; |
| 3376 | } |
| 3377 | |
| 3378 | bool visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 3379 | enqueueUsers(GEPI); |
| 3380 | return false; |
| 3381 | } |
| 3382 | |
| 3383 | bool visitPHINode(PHINode &PN) { |
| 3384 | enqueueUsers(PN); |
| 3385 | return false; |
| 3386 | } |
| 3387 | |
| 3388 | bool visitSelectInst(SelectInst &SI) { |
| 3389 | enqueueUsers(SI); |
| 3390 | return false; |
| 3391 | } |
| 3392 | }; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3393 | |
| 3394 | } // end anonymous namespace |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3395 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3396 | /// Strip aggregate type wrapping. |
Chandler Carruth | ba93199 | 2012-10-13 10:49:33 +0000 | [diff] [blame] | 3397 | /// |
| 3398 | /// This removes no-op aggregate types wrapping an underlying type. It will |
| 3399 | /// strip as many layers of types as it can without changing either the type |
| 3400 | /// size or the allocated size. |
| 3401 | static Type *stripAggregateTypeWrapping(const DataLayout &DL, Type *Ty) { |
| 3402 | if (Ty->isSingleValueType()) |
| 3403 | return Ty; |
| 3404 | |
| 3405 | uint64_t AllocSize = DL.getTypeAllocSize(Ty); |
| 3406 | uint64_t TypeSize = DL.getTypeSizeInBits(Ty); |
| 3407 | |
| 3408 | Type *InnerTy; |
| 3409 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 3410 | InnerTy = ArrTy->getElementType(); |
| 3411 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 3412 | const StructLayout *SL = DL.getStructLayout(STy); |
| 3413 | unsigned Index = SL->getElementContainingOffset(0); |
| 3414 | InnerTy = STy->getElementType(Index); |
| 3415 | } else { |
| 3416 | return Ty; |
| 3417 | } |
| 3418 | |
| 3419 | if (AllocSize > DL.getTypeAllocSize(InnerTy) || |
| 3420 | TypeSize > DL.getTypeSizeInBits(InnerTy)) |
| 3421 | return Ty; |
| 3422 | |
| 3423 | return stripAggregateTypeWrapping(DL, InnerTy); |
| 3424 | } |
| 3425 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3426 | /// Try to find a partition of the aggregate type passed in for a given |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3427 | /// offset and size. |
| 3428 | /// |
| 3429 | /// This recurses through the aggregate type and tries to compute a subtype |
| 3430 | /// 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] | 3431 | /// of an array, it will even compute a new array type for that sub-section, |
| 3432 | /// and the same for structs. |
| 3433 | /// |
| 3434 | /// Note that this routine is very strict and tries to find a partition of the |
| 3435 | /// type which produces the *exact* right offset and size. It is not forgiving |
| 3436 | /// when the size or offset cause either end of type-based partition to be off. |
| 3437 | /// Also, this is a best-effort routine. It is reasonable to give up and not |
| 3438 | /// return a type if necessary. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3439 | static Type *getTypePartition(const DataLayout &DL, Type *Ty, uint64_t Offset, |
| 3440 | uint64_t Size) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3441 | if (Offset == 0 && DL.getTypeAllocSize(Ty) == Size) |
| 3442 | return stripAggregateTypeWrapping(DL, Ty); |
| 3443 | if (Offset > DL.getTypeAllocSize(Ty) || |
| 3444 | (DL.getTypeAllocSize(Ty) - Offset) < Size) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3445 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3446 | |
| 3447 | if (SequentialType *SeqTy = dyn_cast<SequentialType>(Ty)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3448 | Type *ElementTy = SeqTy->getElementType(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3449 | uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3450 | uint64_t NumSkippedElements = Offset / ElementSize; |
Peter Collingbourne | bc07052 | 2016-12-02 03:20:58 +0000 | [diff] [blame] | 3451 | if (NumSkippedElements >= SeqTy->getNumElements()) |
| 3452 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3453 | Offset -= NumSkippedElements * ElementSize; |
| 3454 | |
| 3455 | // First check if we need to recurse. |
| 3456 | if (Offset > 0 || Size < ElementSize) { |
| 3457 | // Bail if the partition ends in a different array element. |
| 3458 | if ((Offset + Size) > ElementSize) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3459 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3460 | // Recurse through the element type trying to peel off offset bytes. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3461 | return getTypePartition(DL, ElementTy, Offset, Size); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3462 | } |
| 3463 | assert(Offset == 0); |
| 3464 | |
| 3465 | if (Size == ElementSize) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3466 | return stripAggregateTypeWrapping(DL, ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3467 | assert(Size > ElementSize); |
| 3468 | uint64_t NumElements = Size / ElementSize; |
| 3469 | if (NumElements * ElementSize != Size) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3470 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3471 | return ArrayType::get(ElementTy, NumElements); |
| 3472 | } |
| 3473 | |
| 3474 | StructType *STy = dyn_cast<StructType>(Ty); |
| 3475 | if (!STy) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3476 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3477 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3478 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3479 | if (Offset >= SL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3480 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3481 | uint64_t EndOffset = Offset + Size; |
| 3482 | if (EndOffset > SL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3483 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3484 | |
| 3485 | unsigned Index = SL->getElementContainingOffset(Offset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3486 | Offset -= SL->getElementOffset(Index); |
| 3487 | |
| 3488 | Type *ElementTy = STy->getElementType(Index); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3489 | uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3490 | if (Offset >= ElementSize) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3491 | return nullptr; // The offset points into alignment padding. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3492 | |
| 3493 | // See if any partition must be contained by the element. |
| 3494 | if (Offset > 0 || Size < ElementSize) { |
| 3495 | if ((Offset + Size) > ElementSize) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3496 | return nullptr; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3497 | return getTypePartition(DL, ElementTy, Offset, Size); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3498 | } |
| 3499 | assert(Offset == 0); |
| 3500 | |
| 3501 | if (Size == ElementSize) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3502 | return stripAggregateTypeWrapping(DL, ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3503 | |
| 3504 | StructType::element_iterator EI = STy->element_begin() + Index, |
| 3505 | EE = STy->element_end(); |
| 3506 | if (EndOffset < SL->getSizeInBytes()) { |
| 3507 | unsigned EndIndex = SL->getElementContainingOffset(EndOffset); |
| 3508 | if (Index == EndIndex) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3509 | return nullptr; // Within a single element and its padding. |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3510 | |
| 3511 | // Don't try to form "natural" types if the elements don't line up with the |
| 3512 | // expected size. |
| 3513 | // FIXME: We could potentially recurse down through the last element in the |
| 3514 | // sub-struct to find a natural end point. |
| 3515 | if (SL->getElementOffset(EndIndex) != EndOffset) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3516 | return nullptr; |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3517 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3518 | assert(Index < EndIndex); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3519 | EE = STy->element_begin() + EndIndex; |
| 3520 | } |
| 3521 | |
| 3522 | // Try to build up a sub-structure. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3523 | StructType *SubTy = |
| 3524 | StructType::get(STy->getContext(), makeArrayRef(EI, EE), STy->isPacked()); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3525 | const StructLayout *SubSL = DL.getStructLayout(SubTy); |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3526 | if (Size != SubSL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3527 | return nullptr; // The sub-struct doesn't have quite the size needed. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3528 | |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3529 | return SubTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3532 | /// Pre-split loads and stores to simplify rewriting. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3533 | /// |
| 3534 | /// We want to break up the splittable load+store pairs as much as |
| 3535 | /// possible. This is important to do as a preprocessing step, as once we |
| 3536 | /// start rewriting the accesses to partitions of the alloca we lose the |
| 3537 | /// necessary information to correctly split apart paired loads and stores |
| 3538 | /// which both point into this alloca. The case to consider is something like |
| 3539 | /// the following: |
| 3540 | /// |
| 3541 | /// %a = alloca [12 x i8] |
| 3542 | /// %gep1 = getelementptr [12 x i8]* %a, i32 0, i32 0 |
| 3543 | /// %gep2 = getelementptr [12 x i8]* %a, i32 0, i32 4 |
| 3544 | /// %gep3 = getelementptr [12 x i8]* %a, i32 0, i32 8 |
| 3545 | /// %iptr1 = bitcast i8* %gep1 to i64* |
| 3546 | /// %iptr2 = bitcast i8* %gep2 to i64* |
| 3547 | /// %fptr1 = bitcast i8* %gep1 to float* |
| 3548 | /// %fptr2 = bitcast i8* %gep2 to float* |
| 3549 | /// %fptr3 = bitcast i8* %gep3 to float* |
| 3550 | /// store float 0.0, float* %fptr1 |
| 3551 | /// store float 1.0, float* %fptr2 |
| 3552 | /// %v = load i64* %iptr1 |
| 3553 | /// store i64 %v, i64* %iptr2 |
| 3554 | /// %f1 = load float* %fptr2 |
| 3555 | /// %f2 = load float* %fptr3 |
| 3556 | /// |
| 3557 | /// Here we want to form 3 partitions of the alloca, each 4 bytes large, and |
| 3558 | /// promote everything so we recover the 2 SSA values that should have been |
| 3559 | /// there all along. |
| 3560 | /// |
| 3561 | /// \returns true if any changes are made. |
| 3562 | bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3563 | LLVM_DEBUG(dbgs() << "Pre-splitting loads and stores\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3564 | |
| 3565 | // Track the loads and stores which are candidates for pre-splitting here, in |
| 3566 | // the order they first appear during the partition scan. These give stable |
| 3567 | // iteration order and a basis for tracking which loads and stores we |
| 3568 | // actually split. |
| 3569 | SmallVector<LoadInst *, 4> Loads; |
| 3570 | SmallVector<StoreInst *, 4> Stores; |
| 3571 | |
| 3572 | // We need to accumulate the splits required of each load or store where we |
| 3573 | // can find them via a direct lookup. This is important to cross-check loads |
| 3574 | // and stores against each other. We also track the slice so that we can kill |
| 3575 | // all the slices that end up split. |
| 3576 | struct SplitOffsets { |
| 3577 | Slice *S; |
| 3578 | std::vector<uint64_t> Splits; |
| 3579 | }; |
| 3580 | SmallDenseMap<Instruction *, SplitOffsets, 8> SplitOffsetsMap; |
| 3581 | |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3582 | // Track loads out of this alloca which cannot, for any reason, be pre-split. |
| 3583 | // This is important as we also cannot pre-split stores of those loads! |
| 3584 | // FIXME: This is all pretty gross. It means that we can be more aggressive |
| 3585 | // in pre-splitting when the load feeding the store happens to come from |
| 3586 | // a separate alloca. Put another way, the effectiveness of SROA would be |
| 3587 | // decreased by a frontend which just concatenated all of its local allocas |
| 3588 | // into one big flat alloca. But defeating such patterns is exactly the job |
| 3589 | // SROA is tasked with! Sadly, to not have this discrepancy we would have |
| 3590 | // change store pre-splitting to actually force pre-splitting of the load |
| 3591 | // that feeds it *and all stores*. That makes pre-splitting much harder, but |
| 3592 | // maybe it would make it more principled? |
| 3593 | SmallPtrSet<LoadInst *, 8> UnsplittableLoads; |
| 3594 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3595 | LLVM_DEBUG(dbgs() << " Searching for candidate loads and stores\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3596 | for (auto &P : AS.partitions()) { |
| 3597 | for (Slice &S : P) { |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3598 | Instruction *I = cast<Instruction>(S.getUse()->getUser()); |
Chandler Carruth | 37f1f12 | 2016-03-10 15:31:17 +0000 | [diff] [blame] | 3599 | if (!S.isSplittable() || S.endOffset() <= P.endOffset()) { |
| 3600 | // If this is a load we have to track that it can't participate in any |
| 3601 | // pre-splitting. If this is a store of a load we have to track that |
| 3602 | // that load also can't participate in any pre-splitting. |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3603 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 3604 | UnsplittableLoads.insert(LI); |
Chandler Carruth | 37f1f12 | 2016-03-10 15:31:17 +0000 | [diff] [blame] | 3605 | else if (auto *SI = dyn_cast<StoreInst>(I)) |
| 3606 | if (auto *LI = dyn_cast<LoadInst>(SI->getValueOperand())) |
| 3607 | UnsplittableLoads.insert(LI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3608 | continue; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3609 | } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3610 | assert(P.endOffset() > S.beginOffset() && |
| 3611 | "Empty or backwards partition!"); |
| 3612 | |
| 3613 | // Determine if this is a pre-splittable slice. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3614 | if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 3615 | assert(!LI->isVolatile() && "Cannot split volatile loads!"); |
| 3616 | |
| 3617 | // The load must be used exclusively to store into other pointers for |
| 3618 | // us to be able to arbitrarily pre-split it. The stores must also be |
| 3619 | // simple to avoid changing semantics. |
| 3620 | auto IsLoadSimplyStored = [](LoadInst *LI) { |
| 3621 | for (User *LU : LI->users()) { |
| 3622 | auto *SI = dyn_cast<StoreInst>(LU); |
| 3623 | if (!SI || !SI->isSimple()) |
| 3624 | return false; |
| 3625 | } |
| 3626 | return true; |
| 3627 | }; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3628 | if (!IsLoadSimplyStored(LI)) { |
| 3629 | UnsplittableLoads.insert(LI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3630 | continue; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3631 | } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3632 | |
| 3633 | Loads.push_back(LI); |
Chandler Carruth | d94a596 | 2016-03-10 14:16:18 +0000 | [diff] [blame] | 3634 | } else if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 3635 | if (S.getUse() != &SI->getOperandUse(SI->getPointerOperandIndex())) |
| 3636 | // Skip stores *of* pointers. FIXME: This shouldn't even be possible! |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3637 | continue; |
| 3638 | auto *StoredLoad = dyn_cast<LoadInst>(SI->getValueOperand()); |
| 3639 | if (!StoredLoad || !StoredLoad->isSimple()) |
| 3640 | continue; |
| 3641 | assert(!SI->isVolatile() && "Cannot split volatile stores!"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3642 | |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3643 | Stores.push_back(SI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3644 | } else { |
| 3645 | // Other uses cannot be pre-split. |
| 3646 | continue; |
| 3647 | } |
| 3648 | |
| 3649 | // Record the initial split. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3650 | LLVM_DEBUG(dbgs() << " Candidate: " << *I << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3651 | auto &Offsets = SplitOffsetsMap[I]; |
| 3652 | assert(Offsets.Splits.empty() && |
| 3653 | "Should not have splits the first time we see an instruction!"); |
| 3654 | Offsets.S = &S; |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3655 | Offsets.Splits.push_back(P.endOffset() - S.beginOffset()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | // Now scan the already split slices, and add a split for any of them which |
| 3659 | // we're going to pre-split. |
| 3660 | for (Slice *S : P.splitSliceTails()) { |
| 3661 | auto SplitOffsetsMapI = |
| 3662 | SplitOffsetsMap.find(cast<Instruction>(S->getUse()->getUser())); |
| 3663 | if (SplitOffsetsMapI == SplitOffsetsMap.end()) |
| 3664 | continue; |
| 3665 | auto &Offsets = SplitOffsetsMapI->second; |
| 3666 | |
| 3667 | assert(Offsets.S == S && "Found a mismatched slice!"); |
| 3668 | assert(!Offsets.Splits.empty() && |
| 3669 | "Cannot have an empty set of splits on the second partition!"); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3670 | assert(Offsets.Splits.back() == |
| 3671 | P.beginOffset() - Offsets.S->beginOffset() && |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3672 | "Previous split does not end where this one begins!"); |
| 3673 | |
| 3674 | // Record each split. The last partition's end isn't needed as the size |
| 3675 | // of the slice dictates that. |
| 3676 | if (S->endOffset() > P.endOffset()) |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3677 | Offsets.Splits.push_back(P.endOffset() - Offsets.S->beginOffset()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | // We may have split loads where some of their stores are split stores. For |
| 3682 | // such loads and stores, we can only pre-split them if their splits exactly |
| 3683 | // match relative to their starting offset. We have to verify this prior to |
| 3684 | // any rewriting. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3685 | Stores.erase( |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3686 | llvm::remove_if(Stores, |
| 3687 | [&UnsplittableLoads, &SplitOffsetsMap](StoreInst *SI) { |
| 3688 | // Lookup the load we are storing in our map of split |
| 3689 | // offsets. |
| 3690 | auto *LI = cast<LoadInst>(SI->getValueOperand()); |
| 3691 | // If it was completely unsplittable, then we're done, |
| 3692 | // and this store can't be pre-split. |
| 3693 | if (UnsplittableLoads.count(LI)) |
| 3694 | return true; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3695 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3696 | auto LoadOffsetsI = SplitOffsetsMap.find(LI); |
| 3697 | if (LoadOffsetsI == SplitOffsetsMap.end()) |
| 3698 | return false; // Unrelated loads are definitely safe. |
| 3699 | auto &LoadOffsets = LoadOffsetsI->second; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3700 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3701 | // Now lookup the store's offsets. |
| 3702 | auto &StoreOffsets = SplitOffsetsMap[SI]; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3703 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3704 | // If the relative offsets of each split in the load and |
| 3705 | // store match exactly, then we can split them and we |
| 3706 | // don't need to remove them here. |
| 3707 | if (LoadOffsets.Splits == StoreOffsets.Splits) |
| 3708 | return false; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3709 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3710 | LLVM_DEBUG( |
| 3711 | dbgs() |
| 3712 | << " Mismatched splits for load and store:\n" |
| 3713 | << " " << *LI << "\n" |
| 3714 | << " " << *SI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3715 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3716 | // We've found a store and load that we need to split |
| 3717 | // with mismatched relative splits. Just give up on them |
| 3718 | // and remove both instructions from our list of |
| 3719 | // candidates. |
| 3720 | UnsplittableLoads.insert(LI); |
| 3721 | return true; |
| 3722 | }), |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3723 | Stores.end()); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3724 | // Now we have to go *back* through all the stores, because a later store may |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3725 | // have caused an earlier store's load to become unsplittable and if it is |
| 3726 | // unsplittable for the later store, then we can't rely on it being split in |
| 3727 | // the earlier store either. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3728 | Stores.erase(llvm::remove_if(Stores, |
| 3729 | [&UnsplittableLoads](StoreInst *SI) { |
| 3730 | auto *LI = |
| 3731 | cast<LoadInst>(SI->getValueOperand()); |
| 3732 | return UnsplittableLoads.count(LI); |
| 3733 | }), |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3734 | Stores.end()); |
| 3735 | // Once we've established all the loads that can't be split for some reason, |
| 3736 | // filter any that made it into our list out. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3737 | Loads.erase(llvm::remove_if(Loads, |
| 3738 | [&UnsplittableLoads](LoadInst *LI) { |
| 3739 | return UnsplittableLoads.count(LI); |
| 3740 | }), |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3741 | Loads.end()); |
| 3742 | |
| 3743 | // If no loads or stores are left, there is no pre-splitting to be done for |
| 3744 | // this alloca. |
| 3745 | if (Loads.empty() && Stores.empty()) |
| 3746 | return false; |
| 3747 | |
| 3748 | // From here on, we can't fail and will be building new accesses, so rig up |
| 3749 | // an IR builder. |
| 3750 | IRBuilderTy IRB(&AI); |
| 3751 | |
| 3752 | // Collect the new slices which we will merge into the alloca slices. |
| 3753 | SmallVector<Slice, 4> NewSlices; |
| 3754 | |
| 3755 | // Track any allocas we end up splitting loads and stores for so we iterate |
| 3756 | // on them. |
| 3757 | SmallPtrSet<AllocaInst *, 4> ResplitPromotableAllocas; |
| 3758 | |
| 3759 | // At this point, we have collected all of the loads and stores we can |
| 3760 | // pre-split, and the specific splits needed for them. We actually do the |
| 3761 | // splitting in a specific order in order to handle when one of the loads in |
| 3762 | // the value operand to one of the stores. |
| 3763 | // |
| 3764 | // First, we rewrite all of the split loads, and just accumulate each split |
| 3765 | // load in a parallel structure. We also build the slices for them and append |
| 3766 | // them to the alloca slices. |
| 3767 | SmallDenseMap<LoadInst *, std::vector<LoadInst *>, 1> SplitLoadsMap; |
| 3768 | std::vector<LoadInst *> SplitLoads; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3769 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3770 | for (LoadInst *LI : Loads) { |
| 3771 | SplitLoads.clear(); |
| 3772 | |
| 3773 | IntegerType *Ty = cast<IntegerType>(LI->getType()); |
| 3774 | uint64_t LoadSize = Ty->getBitWidth() / 8; |
| 3775 | assert(LoadSize > 0 && "Cannot have a zero-sized integer load!"); |
| 3776 | |
| 3777 | auto &Offsets = SplitOffsetsMap[LI]; |
| 3778 | assert(LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && |
| 3779 | "Slice size should always match load size exactly!"); |
| 3780 | uint64_t BaseOffset = Offsets.S->beginOffset(); |
| 3781 | assert(BaseOffset + LoadSize > BaseOffset && |
| 3782 | "Cannot represent alloca access size using 64-bit integers!"); |
| 3783 | |
| 3784 | Instruction *BasePtr = cast<Instruction>(LI->getPointerOperand()); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3785 | IRB.SetInsertPoint(LI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3786 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3787 | LLVM_DEBUG(dbgs() << " Splitting load: " << *LI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3788 | |
| 3789 | uint64_t PartOffset = 0, PartSize = Offsets.Splits.front(); |
| 3790 | int Idx = 0, Size = Offsets.Splits.size(); |
| 3791 | for (;;) { |
| 3792 | auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8); |
Yaxun Liu | 7c44f34 | 2017-06-27 18:26:06 +0000 | [diff] [blame] | 3793 | auto AS = LI->getPointerAddressSpace(); |
| 3794 | auto *PartPtrTy = PartTy->getPointerTo(AS); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3795 | LoadInst *PLoad = IRB.CreateAlignedLoad( |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 3796 | PartTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3797 | getAdjustedPtr(IRB, DL, BasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3798 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3799 | PartPtrTy, BasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3800 | getAdjustedAlignment(LI, PartOffset, DL), /*IsVolatile*/ false, |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3801 | LI->getName()); |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 3802 | PLoad->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access, |
| 3803 | LLVMContext::MD_access_group}); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3804 | |
| 3805 | // Append this load onto the list of split loads so we can find it later |
| 3806 | // to rewrite the stores. |
| 3807 | SplitLoads.push_back(PLoad); |
| 3808 | |
| 3809 | // Now build a new slice for the alloca. |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3810 | NewSlices.push_back( |
| 3811 | Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize, |
| 3812 | &PLoad->getOperandUse(PLoad->getPointerOperandIndex()), |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3813 | /*IsSplittable*/ false)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3814 | LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset() |
| 3815 | << ", " << NewSlices.back().endOffset() |
| 3816 | << "): " << *PLoad << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3817 | |
Chandler Carruth | 29c22fa | 2015-01-02 00:10:22 +0000 | [diff] [blame] | 3818 | // See if we've handled all the splits. |
| 3819 | if (Idx >= Size) |
| 3820 | break; |
| 3821 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3822 | // Setup the next partition. |
| 3823 | PartOffset = Offsets.Splits[Idx]; |
| 3824 | ++Idx; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3825 | PartSize = (Idx < Size ? Offsets.Splits[Idx] : LoadSize) - PartOffset; |
| 3826 | } |
| 3827 | |
| 3828 | // Now that we have the split loads, do the slow walk over all uses of the |
| 3829 | // load and rewrite them as split stores, or save the split loads to use |
| 3830 | // below if the store is going to be split there anyways. |
| 3831 | bool DeferredStores = false; |
| 3832 | for (User *LU : LI->users()) { |
| 3833 | StoreInst *SI = cast<StoreInst>(LU); |
| 3834 | if (!Stores.empty() && SplitOffsetsMap.count(SI)) { |
| 3835 | DeferredStores = true; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3836 | LLVM_DEBUG(dbgs() << " Deferred splitting of store: " << *SI |
| 3837 | << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3838 | continue; |
| 3839 | } |
| 3840 | |
Chandler Carruth | c39eaa5 | 2015-01-01 23:26:16 +0000 | [diff] [blame] | 3841 | Value *StoreBasePtr = SI->getPointerOperand(); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3842 | IRB.SetInsertPoint(SI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3843 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3844 | LLVM_DEBUG(dbgs() << " Splitting store of load: " << *SI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3845 | |
| 3846 | for (int Idx = 0, Size = SplitLoads.size(); Idx < Size; ++Idx) { |
| 3847 | LoadInst *PLoad = SplitLoads[Idx]; |
| 3848 | uint64_t PartOffset = Idx == 0 ? 0 : Offsets.Splits[Idx - 1]; |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3849 | auto *PartPtrTy = |
| 3850 | PLoad->getType()->getPointerTo(SI->getPointerAddressSpace()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3851 | |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3852 | auto AS = SI->getPointerAddressSpace(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3853 | StoreInst *PStore = IRB.CreateAlignedStore( |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3854 | PLoad, |
| 3855 | getAdjustedPtr(IRB, DL, StoreBasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3856 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3857 | PartPtrTy, StoreBasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3858 | getAdjustedAlignment(SI, PartOffset, DL), /*IsVolatile*/ false); |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 3859 | PStore->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access, |
| 3860 | LLVMContext::MD_access_group}); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3861 | LLVM_DEBUG(dbgs() << " +" << PartOffset << ":" << *PStore << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | // We want to immediately iterate on any allocas impacted by splitting |
| 3865 | // this store, and we have to track any promotable alloca (indicated by |
| 3866 | // a direct store) as needing to be resplit because it is no longer |
| 3867 | // promotable. |
| 3868 | if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(StoreBasePtr)) { |
| 3869 | ResplitPromotableAllocas.insert(OtherAI); |
| 3870 | Worklist.insert(OtherAI); |
| 3871 | } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>( |
| 3872 | StoreBasePtr->stripInBoundsOffsets())) { |
| 3873 | Worklist.insert(OtherAI); |
| 3874 | } |
| 3875 | |
| 3876 | // Mark the original store as dead. |
| 3877 | DeadInsts.insert(SI); |
| 3878 | } |
| 3879 | |
| 3880 | // Save the split loads if there are deferred stores among the users. |
| 3881 | if (DeferredStores) |
| 3882 | SplitLoadsMap.insert(std::make_pair(LI, std::move(SplitLoads))); |
| 3883 | |
| 3884 | // Mark the original load as dead and kill the original slice. |
| 3885 | DeadInsts.insert(LI); |
| 3886 | Offsets.S->kill(); |
| 3887 | } |
| 3888 | |
| 3889 | // Second, we rewrite all of the split stores. At this point, we know that |
| 3890 | // all loads from this alloca have been split already. For stores of such |
| 3891 | // loads, we can simply look up the pre-existing split loads. For stores of |
| 3892 | // other loads, we split those loads first and then write split stores of |
| 3893 | // them. |
| 3894 | for (StoreInst *SI : Stores) { |
| 3895 | auto *LI = cast<LoadInst>(SI->getValueOperand()); |
| 3896 | IntegerType *Ty = cast<IntegerType>(LI->getType()); |
| 3897 | uint64_t StoreSize = Ty->getBitWidth() / 8; |
| 3898 | assert(StoreSize > 0 && "Cannot have a zero-sized integer store!"); |
| 3899 | |
| 3900 | auto &Offsets = SplitOffsetsMap[SI]; |
| 3901 | assert(StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && |
| 3902 | "Slice size should always match load size exactly!"); |
| 3903 | uint64_t BaseOffset = Offsets.S->beginOffset(); |
| 3904 | assert(BaseOffset + StoreSize > BaseOffset && |
| 3905 | "Cannot represent alloca access size using 64-bit integers!"); |
| 3906 | |
Chandler Carruth | c39eaa5 | 2015-01-01 23:26:16 +0000 | [diff] [blame] | 3907 | Value *LoadBasePtr = LI->getPointerOperand(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3908 | Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand()); |
| 3909 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3910 | LLVM_DEBUG(dbgs() << " Splitting store: " << *SI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3911 | |
| 3912 | // Check whether we have an already split load. |
| 3913 | auto SplitLoadsMapI = SplitLoadsMap.find(LI); |
| 3914 | std::vector<LoadInst *> *SplitLoads = nullptr; |
| 3915 | if (SplitLoadsMapI != SplitLoadsMap.end()) { |
| 3916 | SplitLoads = &SplitLoadsMapI->second; |
| 3917 | assert(SplitLoads->size() == Offsets.Splits.size() + 1 && |
| 3918 | "Too few split loads for the number of splits in the store!"); |
| 3919 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3920 | LLVM_DEBUG(dbgs() << " of load: " << *LI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3923 | uint64_t PartOffset = 0, PartSize = Offsets.Splits.front(); |
| 3924 | int Idx = 0, Size = Offsets.Splits.size(); |
| 3925 | for (;;) { |
| 3926 | auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8); |
Keno Fischer | 514a6a5 | 2017-06-02 19:04:17 +0000 | [diff] [blame] | 3927 | auto *LoadPartPtrTy = PartTy->getPointerTo(LI->getPointerAddressSpace()); |
| 3928 | auto *StorePartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3929 | |
| 3930 | // Either lookup a split load or create one. |
| 3931 | LoadInst *PLoad; |
| 3932 | if (SplitLoads) { |
| 3933 | PLoad = (*SplitLoads)[Idx]; |
| 3934 | } else { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3935 | IRB.SetInsertPoint(LI); |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3936 | auto AS = LI->getPointerAddressSpace(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3937 | PLoad = IRB.CreateAlignedLoad( |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame^] | 3938 | PartTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3939 | getAdjustedPtr(IRB, DL, LoadBasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3940 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Keno Fischer | 514a6a5 | 2017-06-02 19:04:17 +0000 | [diff] [blame] | 3941 | LoadPartPtrTy, LoadBasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3942 | getAdjustedAlignment(LI, PartOffset, DL), /*IsVolatile*/ false, |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3943 | LI->getName()); |
| 3944 | } |
| 3945 | |
| 3946 | // And store this partition. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3947 | IRB.SetInsertPoint(SI); |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3948 | auto AS = SI->getPointerAddressSpace(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3949 | StoreInst *PStore = IRB.CreateAlignedStore( |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3950 | PLoad, |
| 3951 | getAdjustedPtr(IRB, DL, StoreBasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3952 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3953 | StorePartPtrTy, StoreBasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3954 | getAdjustedAlignment(SI, PartOffset, DL), /*IsVolatile*/ false); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3955 | |
| 3956 | // Now build a new slice for the alloca. |
| 3957 | NewSlices.push_back( |
| 3958 | Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize, |
| 3959 | &PStore->getOperandUse(PStore->getPointerOperandIndex()), |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3960 | /*IsSplittable*/ false)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3961 | LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset() |
| 3962 | << ", " << NewSlices.back().endOffset() |
| 3963 | << "): " << *PStore << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3964 | if (!SplitLoads) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3965 | LLVM_DEBUG(dbgs() << " of split load: " << *PLoad << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Chandler Carruth | 29c22fa | 2015-01-02 00:10:22 +0000 | [diff] [blame] | 3968 | // See if we've finished all the splits. |
| 3969 | if (Idx >= Size) |
| 3970 | break; |
| 3971 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3972 | // Setup the next partition. |
| 3973 | PartOffset = Offsets.Splits[Idx]; |
| 3974 | ++Idx; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3975 | PartSize = (Idx < Size ? Offsets.Splits[Idx] : StoreSize) - PartOffset; |
| 3976 | } |
| 3977 | |
| 3978 | // We want to immediately iterate on any allocas impacted by splitting |
| 3979 | // this load, which is only relevant if it isn't a load of this alloca and |
| 3980 | // thus we didn't already split the loads above. We also have to keep track |
| 3981 | // of any promotable allocas we split loads on as they can no longer be |
| 3982 | // promoted. |
| 3983 | if (!SplitLoads) { |
| 3984 | if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(LoadBasePtr)) { |
| 3985 | assert(OtherAI != &AI && "We can't re-split our own alloca!"); |
| 3986 | ResplitPromotableAllocas.insert(OtherAI); |
| 3987 | Worklist.insert(OtherAI); |
| 3988 | } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>( |
| 3989 | LoadBasePtr->stripInBoundsOffsets())) { |
| 3990 | assert(OtherAI != &AI && "We can't re-split our own alloca!"); |
| 3991 | Worklist.insert(OtherAI); |
| 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | // Mark the original store as dead now that we've split it up and kill its |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3996 | // slice. Note that we leave the original load in place unless this store |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3997 | // was its only use. It may in turn be split up if it is an alloca load |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3998 | // for some other alloca, but it may be a normal load. This may introduce |
| 3999 | // redundant loads, but where those can be merged the rest of the optimizer |
| 4000 | // should handle the merging, and this uncovers SSA splits which is more |
| 4001 | // important. In practice, the original loads will almost always be fully |
| 4002 | // split and removed eventually, and the splits will be merged by any |
| 4003 | // trivial CSE, including instcombine. |
| 4004 | if (LI->hasOneUse()) { |
| 4005 | assert(*LI->user_begin() == SI && "Single use isn't this store!"); |
| 4006 | DeadInsts.insert(LI); |
| 4007 | } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4008 | DeadInsts.insert(SI); |
| 4009 | Offsets.S->kill(); |
| 4010 | } |
| 4011 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4012 | // Remove the killed slices that have ben pre-split. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4013 | AS.erase(llvm::remove_if(AS, [](const Slice &S) { return S.isDead(); }), |
| 4014 | AS.end()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4015 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4016 | // Insert our new slices. This will sort and merge them into the sorted |
| 4017 | // sequence. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4018 | AS.insert(NewSlices); |
| 4019 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4020 | LLVM_DEBUG(dbgs() << " Pre-split slices:\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4021 | #ifndef NDEBUG |
| 4022 | for (auto I = AS.begin(), E = AS.end(); I != E; ++I) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4023 | LLVM_DEBUG(AS.print(dbgs(), I, " ")); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4024 | #endif |
| 4025 | |
| 4026 | // Finally, don't try to promote any allocas that new require re-splitting. |
| 4027 | // They have already been added to the worklist above. |
| 4028 | PromotableAllocas.erase( |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4029 | llvm::remove_if( |
David Majnemer | c700490 | 2016-08-12 04:32:37 +0000 | [diff] [blame] | 4030 | PromotableAllocas, |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4031 | [&](AllocaInst *AI) { return ResplitPromotableAllocas.count(AI); }), |
| 4032 | PromotableAllocas.end()); |
| 4033 | |
| 4034 | return true; |
| 4035 | } |
| 4036 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4037 | /// Rewrite an alloca partition's users. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4038 | /// |
| 4039 | /// This routine drives both of the rewriting goals of the SROA pass. It tries |
| 4040 | /// to rewrite uses of an alloca partition to be conducive for SSA value |
| 4041 | /// promotion. If the partition needs a new, more refined alloca, this will |
| 4042 | /// build that new alloca, preserving as much type information as possible, and |
| 4043 | /// rewrite the uses of the old alloca to point at the new one and have the |
| 4044 | /// appropriate new offsets. It also evaluates how successful the rewrite was |
| 4045 | /// at enabling promotion and if it was successful queues the alloca to be |
| 4046 | /// promoted. |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4047 | AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS, |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4048 | Partition &P) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4049 | // Try to compute a friendly type for this partition of the alloca. This |
| 4050 | // won't always succeed, in which case we fall back to a legal integer type |
| 4051 | // or an i8 array of an appropriate size. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4052 | Type *SliceTy = nullptr; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4053 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4054 | if (Type *CommonUseTy = findCommonType(P.begin(), P.end(), P.endOffset())) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4055 | if (DL.getTypeAllocSize(CommonUseTy) >= P.size()) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4056 | SliceTy = CommonUseTy; |
| 4057 | if (!SliceTy) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4058 | if (Type *TypePartitionTy = getTypePartition(DL, AI.getAllocatedType(), |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4059 | P.beginOffset(), P.size())) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4060 | SliceTy = TypePartitionTy; |
| 4061 | if ((!SliceTy || (SliceTy->isArrayTy() && |
| 4062 | SliceTy->getArrayElementType()->isIntegerTy())) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4063 | DL.isLegalInteger(P.size() * 8)) |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4064 | SliceTy = Type::getIntNTy(*C, P.size() * 8); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4065 | if (!SliceTy) |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4066 | SliceTy = ArrayType::get(Type::getInt8Ty(*C), P.size()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4067 | assert(DL.getTypeAllocSize(SliceTy) >= P.size()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4068 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4069 | bool IsIntegerPromotable = isIntegerWideningViable(P, SliceTy, DL); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4070 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 4071 | VectorType *VecTy = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4072 | IsIntegerPromotable ? nullptr : isVectorPromotionViable(P, DL); |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 4073 | if (VecTy) |
| 4074 | SliceTy = VecTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4075 | |
| 4076 | // Check for the case where we're going to rewrite to a new alloca of the |
| 4077 | // exact same type as the original, and with the same access offsets. In that |
| 4078 | // 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] | 4079 | // perform phi and select speculation. |
Hiroshi Inoue | 99a8faa | 2018-01-16 06:23:05 +0000 | [diff] [blame] | 4080 | // P.beginOffset() can be non-zero even with the same type in a case with |
| 4081 | // out-of-bounds access (e.g. @PR35657 function in SROA/basictest.ll). |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4082 | AllocaInst *NewAI; |
Hiroshi Inoue | 99a8faa | 2018-01-16 06:23:05 +0000 | [diff] [blame] | 4083 | if (SliceTy == AI.getAllocatedType() && P.beginOffset() == 0) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4084 | NewAI = &AI; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4085 | // FIXME: We should be able to bail at this point with "nothing changed". |
| 4086 | // FIXME: We might want to defer PHI speculation until after here. |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4087 | // FIXME: return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4088 | } else { |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 4089 | unsigned Alignment = AI.getAlignment(); |
| 4090 | if (!Alignment) { |
| 4091 | // The minimum alignment which users can rely on when the explicit |
| 4092 | // alignment is omitted or zero is that required by the ABI for this |
| 4093 | // type. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4094 | Alignment = DL.getABITypeAlignment(AI.getAllocatedType()); |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 4095 | } |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4096 | Alignment = MinAlign(Alignment, P.beginOffset()); |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 4097 | // If we will get at least this much alignment from the type alone, leave |
| 4098 | // the alloca's alignment unconstrained. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4099 | if (Alignment <= DL.getABITypeAlignment(SliceTy)) |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 4100 | Alignment = 0; |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4101 | NewAI = new AllocaInst( |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 4102 | SliceTy, AI.getType()->getAddressSpace(), nullptr, Alignment, |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4103 | AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()), &AI); |
Anastasis Grammenos | 425df22 | 2018-06-28 18:58:30 +0000 | [diff] [blame] | 4104 | // Copy the old AI debug location over to the new one. |
| 4105 | NewAI->setDebugLoc(AI.getDebugLoc()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4106 | ++NumNewAllocas; |
| 4107 | } |
| 4108 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4109 | LLVM_DEBUG(dbgs() << "Rewriting alloca partition " |
| 4110 | << "[" << P.beginOffset() << "," << P.endOffset() |
| 4111 | << ") to: " << *NewAI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4112 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4113 | // 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] | 4114 | // promoted allocas. We will reset it to this point if the alloca is not in |
| 4115 | // fact scheduled for promotion. |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4116 | unsigned PPWOldSize = PostPromotionWorklist.size(); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4117 | unsigned NumUses = 0; |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 4118 | SmallSetVector<PHINode *, 8> PHIUsers; |
| 4119 | SmallSetVector<SelectInst *, 8> SelectUsers; |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4120 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4121 | AllocaSliceRewriter Rewriter(DL, AS, *this, AI, *NewAI, P.beginOffset(), |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4122 | P.endOffset(), IsIntegerPromotable, VecTy, |
| 4123 | PHIUsers, SelectUsers); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4124 | bool Promotable = true; |
Chandler Carruth | ffb7ce5 | 2014-12-24 01:48:09 +0000 | [diff] [blame] | 4125 | for (Slice *S : P.splitSliceTails()) { |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4126 | Promotable &= Rewriter.visit(S); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4127 | ++NumUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4128 | } |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4129 | for (Slice &S : P) { |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4130 | Promotable &= Rewriter.visit(&S); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4131 | ++NumUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4134 | NumAllocaPartitionUses += NumUses; |
Craig Topper | 8a95027 | 2017-05-18 00:51:39 +0000 | [diff] [blame] | 4135 | MaxUsesPerAllocaPartition.updateMax(NumUses); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4136 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4137 | // Now that we've processed all the slices in the new partition, check if any |
| 4138 | // PHIs or Selects would block promotion. |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 4139 | for (PHINode *PHI : PHIUsers) |
| 4140 | if (!isSafePHIToSpeculate(*PHI)) { |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4141 | Promotable = false; |
| 4142 | PHIUsers.clear(); |
| 4143 | SelectUsers.clear(); |
Chandler Carruth | a8c4cc6 | 2014-02-25 09:45:27 +0000 | [diff] [blame] | 4144 | break; |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4145 | } |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 4146 | |
| 4147 | for (SelectInst *Sel : SelectUsers) |
| 4148 | if (!isSafeSelectToSpeculate(*Sel)) { |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4149 | Promotable = false; |
| 4150 | PHIUsers.clear(); |
| 4151 | SelectUsers.clear(); |
Chandler Carruth | a8c4cc6 | 2014-02-25 09:45:27 +0000 | [diff] [blame] | 4152 | break; |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | if (Promotable) { |
| 4156 | if (PHIUsers.empty() && SelectUsers.empty()) { |
| 4157 | // Promote the alloca. |
| 4158 | PromotableAllocas.push_back(NewAI); |
| 4159 | } else { |
| 4160 | // If we have either PHIs or Selects to speculate, add them to those |
| 4161 | // worklists and re-queue the new alloca so that we promote in on the |
| 4162 | // next iteration. |
Chandler Carruth | 6174704 | 2014-10-16 21:05:14 +0000 | [diff] [blame] | 4163 | for (PHINode *PHIUser : PHIUsers) |
| 4164 | SpeculatablePHIs.insert(PHIUser); |
| 4165 | for (SelectInst *SelectUser : SelectUsers) |
| 4166 | SpeculatableSelects.insert(SelectUser); |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4167 | Worklist.insert(NewAI); |
| 4168 | } |
| 4169 | } else { |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4170 | // Drop any post-promotion work items if promotion didn't happen. |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4171 | while (PostPromotionWorklist.size() > PPWOldSize) |
| 4172 | PostPromotionWorklist.pop_back(); |
David Majnemer | 30ffc4c | 2016-04-26 01:05:00 +0000 | [diff] [blame] | 4173 | |
| 4174 | // We couldn't promote and we didn't create a new partition, nothing |
| 4175 | // happened. |
| 4176 | if (NewAI == &AI) |
| 4177 | return nullptr; |
| 4178 | |
| 4179 | // If we can't promote the alloca, iterate on it to check for new |
| 4180 | // refinements exposed by splitting the current alloca. Don't iterate on an |
| 4181 | // alloca which didn't actually change and didn't get promoted. |
| 4182 | Worklist.insert(NewAI); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4183 | } |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4184 | |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4185 | return NewAI; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4188 | /// Walks the slices of an alloca and form partitions based on them, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4189 | /// rewriting each of their uses. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4190 | bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { |
| 4191 | if (AS.begin() == AS.end()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4192 | return false; |
| 4193 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4194 | unsigned NumPartitions = 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4195 | bool Changed = false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4196 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4197 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4198 | // First try to pre-split loads and stores. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4199 | Changed |= presplitLoadsAndStores(AI, AS); |
| 4200 | |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4201 | // Now that we have identified any pre-splitting opportunities, |
| 4202 | // mark loads and stores unsplittable except for the following case. |
| 4203 | // We leave a slice splittable if all other slices are disjoint or fully |
| 4204 | // included in the slice, such as whole-alloca loads and stores. |
| 4205 | // If we fail to split these during pre-splitting, we want to force them |
| 4206 | // to be rewritten into a partition. |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4207 | bool IsSorted = true; |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4208 | |
| 4209 | uint64_t AllocaSize = DL.getTypeAllocSize(AI.getAllocatedType()); |
| 4210 | const uint64_t MaxBitVectorSize = 1024; |
Hiroshi Inoue | 99a8faa | 2018-01-16 06:23:05 +0000 | [diff] [blame] | 4211 | if (AllocaSize <= MaxBitVectorSize) { |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4212 | // If a byte boundary is included in any load or store, a slice starting or |
| 4213 | // ending at the boundary is not splittable. |
| 4214 | SmallBitVector SplittableOffset(AllocaSize + 1, true); |
| 4215 | for (Slice &S : AS) |
| 4216 | for (unsigned O = S.beginOffset() + 1; |
| 4217 | O < S.endOffset() && O < AllocaSize; O++) |
| 4218 | SplittableOffset.reset(O); |
| 4219 | |
| 4220 | for (Slice &S : AS) { |
| 4221 | if (!S.isSplittable()) |
| 4222 | continue; |
| 4223 | |
| 4224 | if ((S.beginOffset() > AllocaSize || SplittableOffset[S.beginOffset()]) && |
| 4225 | (S.endOffset() > AllocaSize || SplittableOffset[S.endOffset()])) |
| 4226 | continue; |
| 4227 | |
| 4228 | if (isa<LoadInst>(S.getUse()->getUser()) || |
| 4229 | isa<StoreInst>(S.getUse()->getUser())) { |
| 4230 | S.makeUnsplittable(); |
| 4231 | IsSorted = false; |
| 4232 | } |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4233 | } |
| 4234 | } |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4235 | else { |
| 4236 | // We only allow whole-alloca splittable loads and stores |
| 4237 | // for a large alloca to avoid creating too large BitVector. |
| 4238 | for (Slice &S : AS) { |
| 4239 | if (!S.isSplittable()) |
| 4240 | continue; |
| 4241 | |
| 4242 | if (S.beginOffset() == 0 && S.endOffset() >= AllocaSize) |
| 4243 | continue; |
| 4244 | |
| 4245 | if (isa<LoadInst>(S.getUse()->getUser()) || |
| 4246 | isa<StoreInst>(S.getUse()->getUser())) { |
| 4247 | S.makeUnsplittable(); |
| 4248 | IsSorted = false; |
| 4249 | } |
| 4250 | } |
| 4251 | } |
| 4252 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4253 | if (!IsSorted) |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 4254 | llvm::sort(AS); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4255 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4256 | /// Describes the allocas introduced by rewritePartition in order to migrate |
| 4257 | /// the debug info. |
| 4258 | struct Fragment { |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4259 | AllocaInst *Alloca; |
| 4260 | uint64_t Offset; |
| 4261 | uint64_t Size; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4262 | Fragment(AllocaInst *AI, uint64_t O, uint64_t S) |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4263 | : Alloca(AI), Offset(O), Size(S) {} |
| 4264 | }; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4265 | SmallVector<Fragment, 4> Fragments; |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4266 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4267 | // Rewrite each partition. |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4268 | for (auto &P : AS.partitions()) { |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4269 | if (AllocaInst *NewAI = rewritePartition(AI, AS, P)) { |
| 4270 | Changed = true; |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4271 | if (NewAI != &AI) { |
| 4272 | uint64_t SizeOfByte = 8; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4273 | uint64_t AllocaSize = DL.getTypeSizeInBits(NewAI->getAllocatedType()); |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4274 | // Don't include any padding. |
| 4275 | uint64_t Size = std::min(AllocaSize, P.size() * SizeOfByte); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4276 | Fragments.push_back(Fragment(NewAI, P.beginOffset() * SizeOfByte, Size)); |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4277 | } |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4278 | } |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4279 | ++NumPartitions; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4280 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4281 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4282 | NumAllocaPartitions += NumPartitions; |
Craig Topper | 8a95027 | 2017-05-18 00:51:39 +0000 | [diff] [blame] | 4283 | MaxPartitionsPerAlloca.updateMax(NumPartitions); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4284 | |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4285 | // Migrate debug information from the old alloca to the new alloca(s) |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 4286 | // and the individual partitions. |
Hsiangkai Wang | ef72e48 | 2018-08-06 03:59:47 +0000 | [diff] [blame] | 4287 | TinyPtrVector<DbgVariableIntrinsic *> DbgDeclares = FindDbgAddrUses(&AI); |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4288 | if (!DbgDeclares.empty()) { |
| 4289 | auto *Var = DbgDeclares.front()->getVariable(); |
| 4290 | auto *Expr = DbgDeclares.front()->getExpression(); |
Adrian Prantl | d7f6f16 | 2017-11-28 00:57:53 +0000 | [diff] [blame] | 4291 | auto VarSize = Var->getSizeInBits(); |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 4292 | DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false); |
Keno Fischer | d5354fd | 2016-01-14 20:06:34 +0000 | [diff] [blame] | 4293 | uint64_t AllocaSize = DL.getTypeSizeInBits(AI.getAllocatedType()); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4294 | for (auto Fragment : Fragments) { |
| 4295 | // Create a fragment expression describing the new partition or reuse AI's |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4296 | // expression if there is only one partition. |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4297 | auto *FragmentExpr = Expr; |
| 4298 | if (Fragment.Size < AllocaSize || Expr->isFragment()) { |
Adrian Prantl | 152ac39 | 2015-02-01 00:58:04 +0000 | [diff] [blame] | 4299 | // If this alloca is already a scalar replacement of a larger aggregate, |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4300 | // Fragment.Offset describes the offset inside the scalar. |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 4301 | auto ExprFragment = Expr->getFragmentInfo(); |
| 4302 | uint64_t Offset = ExprFragment ? ExprFragment->OffsetInBits : 0; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4303 | uint64_t Start = Offset + Fragment.Offset; |
| 4304 | uint64_t Size = Fragment.Size; |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 4305 | if (ExprFragment) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4306 | uint64_t AbsEnd = |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 4307 | ExprFragment->OffsetInBits + ExprFragment->SizeInBits; |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4308 | if (Start >= AbsEnd) |
| 4309 | // No need to describe a SROAed padding. |
| 4310 | continue; |
| 4311 | Size = std::min(Size, AbsEnd - Start); |
| 4312 | } |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 4313 | // The new, smaller fragment is stenciled out from the old fragment. |
| 4314 | if (auto OrigFragment = FragmentExpr->getFragmentInfo()) { |
| 4315 | assert(Start >= OrigFragment->OffsetInBits && |
| 4316 | "new fragment is outside of original fragment"); |
| 4317 | Start -= OrigFragment->OffsetInBits; |
| 4318 | } |
Adrian Prantl | 77d90b0 | 2017-11-28 21:30:38 +0000 | [diff] [blame] | 4319 | |
| 4320 | // The alloca may be larger than the variable. |
| 4321 | if (VarSize) { |
| 4322 | if (Size > *VarSize) |
| 4323 | Size = *VarSize; |
| 4324 | if (Size == 0 || Start + Size > *VarSize) |
| 4325 | continue; |
| 4326 | } |
| 4327 | |
Adrian Prantl | d7f6f16 | 2017-11-28 00:57:53 +0000 | [diff] [blame] | 4328 | // Avoid creating a fragment expression that covers the entire variable. |
| 4329 | if (!VarSize || *VarSize != Size) { |
| 4330 | if (auto E = |
| 4331 | DIExpression::createFragmentExpression(Expr, Start, Size)) |
| 4332 | FragmentExpr = *E; |
| 4333 | else |
| 4334 | continue; |
| 4335 | } |
Adrian Prantl | 152ac39 | 2015-02-01 00:58:04 +0000 | [diff] [blame] | 4336 | } |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4337 | |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4338 | // Remove any existing intrinsics describing the same alloca. |
Hsiangkai Wang | ef72e48 | 2018-08-06 03:59:47 +0000 | [diff] [blame] | 4339 | for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(Fragment.Alloca)) |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4340 | OldDII->eraseFromParent(); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4341 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4342 | DIB.insertDeclare(Fragment.Alloca, Var, FragmentExpr, |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4343 | DbgDeclares.front()->getDebugLoc(), &AI); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4344 | } |
| 4345 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4346 | return Changed; |
| 4347 | } |
| 4348 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4349 | /// Clobber a use with undef, deleting the used value if it becomes dead. |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4350 | void SROA::clobberUse(Use &U) { |
| 4351 | Value *OldV = U; |
| 4352 | // Replace the use with an undef value. |
| 4353 | U = UndefValue::get(OldV->getType()); |
| 4354 | |
| 4355 | // Check for this making an instruction dead. We have to garbage collect |
| 4356 | // all the dead instructions to ensure the uses of any alloca end up being |
| 4357 | // minimal. |
| 4358 | if (Instruction *OldI = dyn_cast<Instruction>(OldV)) |
| 4359 | if (isInstructionTriviallyDead(OldI)) { |
| 4360 | DeadInsts.insert(OldI); |
| 4361 | } |
| 4362 | } |
| 4363 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4364 | /// Analyze an alloca for SROA. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4365 | /// |
| 4366 | /// This analyzes the alloca to ensure we can reason about it, builds |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4367 | /// 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] | 4368 | /// rewritten as needed. |
| 4369 | bool SROA::runOnAlloca(AllocaInst &AI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4370 | LLVM_DEBUG(dbgs() << "SROA alloca: " << AI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4371 | ++NumAllocasAnalyzed; |
| 4372 | |
| 4373 | // Special case dead allocas, as they're trivial. |
| 4374 | if (AI.use_empty()) { |
| 4375 | AI.eraseFromParent(); |
| 4376 | return true; |
| 4377 | } |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4378 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4379 | |
| 4380 | // Skip alloca forms that this analysis can't handle. |
| 4381 | if (AI.isArrayAllocation() || !AI.getAllocatedType()->isSized() || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4382 | DL.getTypeAllocSize(AI.getAllocatedType()) == 0) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4383 | return false; |
| 4384 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 4385 | bool Changed = false; |
| 4386 | |
| 4387 | // First, split any FCA loads and stores touching this alloca to promote |
| 4388 | // better splitting and promotion opportunities. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 4389 | AggLoadStoreRewriter AggRewriter(DL); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 4390 | Changed |= AggRewriter.rewrite(AI); |
| 4391 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4392 | // Build the slices using a recursive instruction-visiting builder. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4393 | AllocaSlices AS(DL, AI); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4394 | LLVM_DEBUG(AS.print(dbgs())); |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4395 | if (AS.isEscaped()) |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 4396 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4397 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4398 | // Delete all the dead users of this alloca before splitting and rewriting it. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4399 | for (Instruction *DeadUser : AS.getDeadUsers()) { |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4400 | // Free up everything used by this instruction. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4401 | for (Use &DeadOp : DeadUser->operands()) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 4402 | clobberUse(DeadOp); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4403 | |
| 4404 | // Now replace the uses of this instruction. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4405 | DeadUser->replaceAllUsesWith(UndefValue::get(DeadUser->getType())); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4406 | |
| 4407 | // And mark it for deletion. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4408 | DeadInsts.insert(DeadUser); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4409 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4410 | } |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4411 | for (Use *DeadOp : AS.getDeadOperands()) { |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4412 | clobberUse(*DeadOp); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4413 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4416 | // No slices to split. Leave the dead alloca for a later pass to clean up. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4417 | if (AS.begin() == AS.end()) |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 4418 | return Changed; |
| 4419 | |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4420 | Changed |= splitAlloca(AI, AS); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4421 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4422 | LLVM_DEBUG(dbgs() << " Speculating PHIs\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4423 | while (!SpeculatablePHIs.empty()) |
| 4424 | speculatePHINodeLoads(*SpeculatablePHIs.pop_back_val()); |
| 4425 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4426 | LLVM_DEBUG(dbgs() << " Speculating Selects\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4427 | while (!SpeculatableSelects.empty()) |
| 4428 | speculateSelectInstLoads(*SpeculatableSelects.pop_back_val()); |
| 4429 | |
| 4430 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4433 | /// Delete the dead instructions accumulated in this run. |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 4434 | /// |
| 4435 | /// Recursively deletes the dead instructions we've accumulated. This is done |
| 4436 | /// at the very end to maximize locality of the recursive delete and to |
| 4437 | /// minimize the problems of invalidated instruction pointers as such pointers |
| 4438 | /// are used heavily in the intermediate stages of the algorithm. |
| 4439 | /// |
| 4440 | /// We also record the alloca instructions deleted here so that they aren't |
| 4441 | /// subsequently handed to mem2reg to promote. |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4442 | bool SROA::deleteDeadInstructions( |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 4443 | SmallPtrSetImpl<AllocaInst *> &DeletedAllocas) { |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4444 | bool Changed = false; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4445 | while (!DeadInsts.empty()) { |
| 4446 | Instruction *I = DeadInsts.pop_back_val(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4447 | LLVM_DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4448 | |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4449 | // If the instruction is an alloca, find the possible dbg.declare connected |
| 4450 | // to it, and remove it too. We must do this before calling RAUW or we will |
| 4451 | // not be able to find it. |
| 4452 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 4453 | DeletedAllocas.insert(AI); |
Hsiangkai Wang | ef72e48 | 2018-08-06 03:59:47 +0000 | [diff] [blame] | 4454 | for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(AI)) |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4455 | OldDII->eraseFromParent(); |
| 4456 | } |
| 4457 | |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 4458 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 4459 | |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 4460 | for (Use &Operand : I->operands()) |
| 4461 | if (Instruction *U = dyn_cast<Instruction>(Operand)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4462 | // Zero out the operand and see if it becomes trivially dead. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4463 | Operand = nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4464 | if (isInstructionTriviallyDead(U)) |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 4465 | DeadInsts.insert(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4468 | ++NumDeleted; |
| 4469 | I->eraseFromParent(); |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4470 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4471 | } |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4472 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4475 | /// Promote the allocas, using the best available technique. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 4476 | /// |
| 4477 | /// This attempts to promote whatever allocas have been identified as viable in |
| 4478 | /// the PromotableAllocas list. If that list is empty, there is nothing to do. |
Chandler Carruth | 748d095 | 2015-08-26 09:09:29 +0000 | [diff] [blame] | 4479 | /// This function returns whether any promotion occurred. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 4480 | bool SROA::promoteAllocas(Function &F) { |
| 4481 | if (PromotableAllocas.empty()) |
| 4482 | return false; |
| 4483 | |
| 4484 | NumPromoted += PromotableAllocas.size(); |
| 4485 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4486 | LLVM_DEBUG(dbgs() << "Promoting allocas with mem2reg...\n"); |
Davide Italiano | 612d5a9 | 2017-04-09 20:47:14 +0000 | [diff] [blame] | 4487 | PromoteMemToReg(PromotableAllocas, *DT, AC); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 4488 | PromotableAllocas.clear(); |
| 4489 | return true; |
| 4490 | } |
| 4491 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4492 | PreservedAnalyses SROA::runImpl(Function &F, DominatorTree &RunDT, |
| 4493 | AssumptionCache &RunAC) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4494 | LLVM_DEBUG(dbgs() << "SROA function: " << F.getName() << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4495 | C = &F.getContext(); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4496 | DT = &RunDT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4497 | AC = &RunAC; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4498 | |
| 4499 | BasicBlock &EntryBB = F.getEntryBlock(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 4500 | for (BasicBlock::iterator I = EntryBB.begin(), E = std::prev(EntryBB.end()); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4501 | I != E; ++I) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4502 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 4503 | Worklist.insert(AI); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4504 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4505 | |
| 4506 | bool Changed = false; |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 4507 | // A set of deleted alloca instruction pointers which should be removed from |
| 4508 | // the list of promotable allocas. |
| 4509 | SmallPtrSet<AllocaInst *, 4> DeletedAllocas; |
| 4510 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4511 | do { |
| 4512 | while (!Worklist.empty()) { |
| 4513 | Changed |= runOnAlloca(*Worklist.pop_back_val()); |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4514 | Changed |= deleteDeadInstructions(DeletedAllocas); |
Chandler Carruth | b09f0a3 | 2012-10-02 22:46:45 +0000 | [diff] [blame] | 4515 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4516 | // Remove the deleted allocas from various lists so that we don't try to |
| 4517 | // continue processing them. |
| 4518 | if (!DeletedAllocas.empty()) { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 4519 | auto IsInSet = [&](AllocaInst *AI) { return DeletedAllocas.count(AI); }; |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 4520 | Worklist.remove_if(IsInSet); |
| 4521 | PostPromotionWorklist.remove_if(IsInSet); |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4522 | PromotableAllocas.erase(llvm::remove_if(PromotableAllocas, IsInSet), |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4523 | PromotableAllocas.end()); |
| 4524 | DeletedAllocas.clear(); |
| 4525 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4526 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4527 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4528 | Changed |= promoteAllocas(F); |
| 4529 | |
| 4530 | Worklist = PostPromotionWorklist; |
| 4531 | PostPromotionWorklist.clear(); |
| 4532 | } while (!Worklist.empty()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4533 | |
Davide Italiano | 16e96d4 | 2016-06-07 13:21:17 +0000 | [diff] [blame] | 4534 | if (!Changed) |
| 4535 | return PreservedAnalyses::all(); |
| 4536 | |
Davide Italiano | 16e96d4 | 2016-06-07 13:21:17 +0000 | [diff] [blame] | 4537 | PreservedAnalyses PA; |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 4538 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | 16e96d4 | 2016-06-07 13:21:17 +0000 | [diff] [blame] | 4539 | PA.preserve<GlobalsAA>(); |
| 4540 | return PA; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 4543 | PreservedAnalyses SROA::run(Function &F, FunctionAnalysisManager &AM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4544 | return runImpl(F, AM.getResult<DominatorTreeAnalysis>(F), |
| 4545 | AM.getResult<AssumptionAnalysis>(F)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4546 | } |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4547 | |
| 4548 | /// A legacy pass for the legacy pass manager that wraps the \c SROA pass. |
| 4549 | /// |
| 4550 | /// This is in the llvm namespace purely to allow it to be a friend of the \c |
| 4551 | /// SROA pass. |
| 4552 | class llvm::sroa::SROALegacyPass : public FunctionPass { |
| 4553 | /// The SROA implementation. |
| 4554 | SROA Impl; |
| 4555 | |
| 4556 | public: |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4557 | static char ID; |
| 4558 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4559 | SROALegacyPass() : FunctionPass(ID) { |
| 4560 | initializeSROALegacyPassPass(*PassRegistry::getPassRegistry()); |
| 4561 | } |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4562 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4563 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 4564 | if (skipFunction(F)) |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4565 | return false; |
| 4566 | |
| 4567 | auto PA = Impl.runImpl( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4568 | F, getAnalysis<DominatorTreeWrapperPass>().getDomTree(), |
| 4569 | getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F)); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4570 | return !PA.areAllPreserved(); |
| 4571 | } |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4572 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4573 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4574 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4575 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4576 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 4577 | AU.setPreservesCFG(); |
| 4578 | } |
| 4579 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 4580 | StringRef getPassName() const override { return "SROA"; } |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4581 | }; |
| 4582 | |
| 4583 | char SROALegacyPass::ID = 0; |
| 4584 | |
| 4585 | FunctionPass *llvm::createSROAPass() { return new SROALegacyPass(); } |
| 4586 | |
| 4587 | INITIALIZE_PASS_BEGIN(SROALegacyPass, "sroa", |
| 4588 | "Scalar Replacement Of Aggregates", false, false) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4589 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4590 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 4591 | INITIALIZE_PASS_END(SROALegacyPass, "sroa", "Scalar Replacement Of Aggregates", |
| 4592 | false, false) |