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" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 44 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 45 | #include "llvm/IR/BasicBlock.h" |
| 46 | #include "llvm/IR/Constant.h" |
| 47 | #include "llvm/IR/ConstantFolder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 48 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 12664a0 | 2014-03-06 00:22:06 +0000 | [diff] [blame] | 49 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 50 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 51 | #include "llvm/IR/DebugInfoMetadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 52 | #include "llvm/IR/DerivedTypes.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 53 | #include "llvm/IR/Dominators.h" |
| 54 | #include "llvm/IR/Function.h" |
| 55 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
| 56 | #include "llvm/IR/GlobalAlias.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 57 | #include "llvm/IR/IRBuilder.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 58 | #include "llvm/IR/InstVisitor.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 59 | #include "llvm/IR/InstrTypes.h" |
| 60 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Instructions.h" |
| 62 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 63 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 64 | #include "llvm/IR/LLVMContext.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 65 | #include "llvm/IR/Metadata.h" |
| 66 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 67 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 68 | #include "llvm/IR/PassManager.h" |
| 69 | #include "llvm/IR/Type.h" |
| 70 | #include "llvm/IR/Use.h" |
| 71 | #include "llvm/IR/User.h" |
| 72 | #include "llvm/IR/Value.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 73 | #include "llvm/InitializePasses.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" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 83 | #include "llvm/Transforms/Utils/Local.h" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 84 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 85 | #include <algorithm> |
| 86 | #include <cassert> |
| 87 | #include <chrono> |
| 88 | #include <cstddef> |
| 89 | #include <cstdint> |
| 90 | #include <cstring> |
| 91 | #include <iterator> |
| 92 | #include <string> |
| 93 | #include <tuple> |
| 94 | #include <utility> |
| 95 | #include <vector> |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 96 | |
Hal Finkel | 29f5131 | 2016-03-28 11:13:03 +0000 | [diff] [blame] | 97 | #ifndef NDEBUG |
| 98 | // We only use this for a debug check. |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 99 | #include <random> |
| 100 | #endif |
| 101 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 102 | using namespace llvm; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 103 | using namespace llvm::sroa; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 104 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 105 | #define DEBUG_TYPE "sroa" |
| 106 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 107 | STATISTIC(NumAllocasAnalyzed, "Number of allocas analyzed for replacement"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 108 | STATISTIC(NumAllocaPartitions, "Number of alloca partitions formed"); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 109 | STATISTIC(MaxPartitionsPerAlloca, "Maximum number of partitions per alloca"); |
| 110 | STATISTIC(NumAllocaPartitionUses, "Number of alloca partition uses rewritten"); |
| 111 | STATISTIC(MaxUsesPerAllocaPartition, "Maximum number of uses of a partition"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 112 | STATISTIC(NumNewAllocas, "Number of new, smaller allocas introduced"); |
| 113 | STATISTIC(NumPromoted, "Number of allocas promoted to SSA values"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 114 | STATISTIC(NumLoadsSpeculated, "Number of loads speculated to allow promotion"); |
Chandler Carruth | 5f5b616 | 2013-03-20 06:30:46 +0000 | [diff] [blame] | 115 | STATISTIC(NumDeleted, "Number of instructions deleted"); |
| 116 | STATISTIC(NumVectorized, "Number of vectorized aggregates"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 117 | |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 118 | /// Hidden option to enable randomly shuffling the slices to help uncover |
| 119 | /// instability in their order. |
| 120 | static cl::opt<bool> SROARandomShuffleSlices("sroa-random-shuffle-slices", |
| 121 | cl::init(false), cl::Hidden); |
| 122 | |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 123 | /// Hidden option to experiment with completely strict handling of inbounds |
| 124 | /// GEPs. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 125 | static cl::opt<bool> SROAStrictInbounds("sroa-strict-inbounds", cl::init(false), |
| 126 | cl::Hidden); |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 127 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 128 | namespace { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 129 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 130 | /// A custom IRBuilder inserter which prefixes all names, but only in |
Mehdi Amini | 1e9c925 | 2016-03-11 17:15:34 +0000 | [diff] [blame] | 131 | /// Assert builds. |
Mehdi Amini | ba9fba8 | 2016-03-13 21:05:13 +0000 | [diff] [blame] | 132 | class IRBuilderPrefixedInserter : public IRBuilderDefaultInserter { |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 133 | std::string Prefix; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 134 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 135 | const Twine getNameWithPrefix(const Twine &Name) const { |
| 136 | return Name.isTriviallyEmpty() ? Name : Prefix + Name; |
| 137 | } |
| 138 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 139 | public: |
| 140 | void SetNamePrefix(const Twine &P) { Prefix = P.str(); } |
| 141 | |
| 142 | protected: |
| 143 | void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, |
| 144 | BasicBlock::iterator InsertPt) const { |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 145 | IRBuilderDefaultInserter::InsertHelper(I, getNameWithPrefix(Name), BB, |
| 146 | InsertPt); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 147 | } |
| 148 | }; |
| 149 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 150 | /// Provide a type for IRBuilder that drops names in release builds. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 151 | using IRBuilderTy = IRBuilder<ConstantFolder, IRBuilderPrefixedInserter>; |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 152 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 153 | /// A used slice of an alloca. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 154 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 155 | /// This structure represents a slice of an alloca used by some instruction. It |
| 156 | /// stores both the begin and end offsets of this use, a pointer to the use |
| 157 | /// itself, and a flag indicating whether we can classify the use as splittable |
| 158 | /// or not when forming partitions of the alloca. |
| 159 | class Slice { |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 160 | /// The beginning offset of the range. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 161 | uint64_t BeginOffset = 0; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 162 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 163 | /// The ending offset, not included in the range. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 164 | uint64_t EndOffset = 0; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 165 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 166 | /// 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] | 167 | /// split. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 168 | PointerIntPair<Use *, 1, bool> UseAndIsSplittable; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 169 | |
| 170 | public: |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 171 | Slice() = default; |
| 172 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 173 | Slice(uint64_t BeginOffset, uint64_t EndOffset, Use *U, bool IsSplittable) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 174 | : BeginOffset(BeginOffset), EndOffset(EndOffset), |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 175 | UseAndIsSplittable(U, IsSplittable) {} |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 176 | |
| 177 | uint64_t beginOffset() const { return BeginOffset; } |
| 178 | uint64_t endOffset() const { return EndOffset; } |
| 179 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 180 | bool isSplittable() const { return UseAndIsSplittable.getInt(); } |
| 181 | void makeUnsplittable() { UseAndIsSplittable.setInt(false); } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 182 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 183 | Use *getUse() const { return UseAndIsSplittable.getPointer(); } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 184 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 185 | bool isDead() const { return getUse() == nullptr; } |
| 186 | void kill() { UseAndIsSplittable.setPointer(nullptr); } |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 187 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 188 | /// Support for ordering ranges. |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 189 | /// |
| 190 | /// This provides an ordering over ranges such that start offsets are |
| 191 | /// always increasing, and within equal start offsets, the end offsets are |
| 192 | /// decreasing. Thus the spanning range comes first in a cluster with the |
| 193 | /// same start position. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 194 | bool operator<(const Slice &RHS) const { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 195 | if (beginOffset() < RHS.beginOffset()) |
| 196 | return true; |
| 197 | if (beginOffset() > RHS.beginOffset()) |
| 198 | return false; |
| 199 | if (isSplittable() != RHS.isSplittable()) |
| 200 | return !isSplittable(); |
| 201 | if (endOffset() > RHS.endOffset()) |
| 202 | return true; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 206 | /// Support comparison with a single offset to allow binary searches. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 207 | friend LLVM_ATTRIBUTE_UNUSED bool operator<(const Slice &LHS, |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 208 | uint64_t RHSOffset) { |
| 209 | return LHS.beginOffset() < RHSOffset; |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 210 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 211 | friend LLVM_ATTRIBUTE_UNUSED bool operator<(uint64_t LHSOffset, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 212 | const Slice &RHS) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 213 | return LHSOffset < RHS.beginOffset(); |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 214 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 215 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 216 | bool operator==(const Slice &RHS) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 217 | return isSplittable() == RHS.isSplittable() && |
| 218 | beginOffset() == RHS.beginOffset() && endOffset() == RHS.endOffset(); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 219 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 220 | bool operator!=(const Slice &RHS) const { return !operator==(RHS); } |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 221 | }; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 222 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 223 | } // end anonymous namespace |
Chandler Carruth | f74654d | 2013-03-18 08:36:46 +0000 | [diff] [blame] | 224 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 225 | /// Representation of the alloca slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 226 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 227 | /// This class represents the slices of an alloca which are formed by its |
| 228 | /// various uses. If a pointer escapes, we can't fully build a representation |
| 229 | /// for the slices used and we reflect that in this structure. The uses are |
| 230 | /// stored, sorted by increasing beginning offset and with unsplittable slices |
| 231 | /// starting at a particular offset before splittable slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 232 | class llvm::sroa::AllocaSlices { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 233 | public: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 234 | /// Construct the slices of a particular alloca. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 235 | AllocaSlices(const DataLayout &DL, AllocaInst &AI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 236 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 237 | /// Test whether a pointer to the allocation escapes our analysis. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 238 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 239 | /// 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] | 240 | /// ignored. |
| 241 | bool isEscaped() const { return PointerEscapingInstr; } |
| 242 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 243 | /// Support for iterating over the slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 244 | /// @{ |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 245 | using iterator = SmallVectorImpl<Slice>::iterator; |
| 246 | using range = iterator_range<iterator>; |
| 247 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 248 | iterator begin() { return Slices.begin(); } |
| 249 | iterator end() { return Slices.end(); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 250 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 251 | using const_iterator = SmallVectorImpl<Slice>::const_iterator; |
| 252 | using const_range = iterator_range<const_iterator>; |
| 253 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 254 | const_iterator begin() const { return Slices.begin(); } |
| 255 | const_iterator end() const { return Slices.end(); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 256 | /// @} |
| 257 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 258 | /// Erase a range of slices. |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 259 | void erase(iterator Start, iterator Stop) { Slices.erase(Start, Stop); } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 260 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 261 | /// Insert new slices for this alloca. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 262 | /// |
| 263 | /// This moves the slices into the alloca's slices collection, and re-sorts |
| 264 | /// everything so that the usual ordering properties of the alloca's slices |
| 265 | /// hold. |
| 266 | void insert(ArrayRef<Slice> NewSlices) { |
| 267 | int OldSize = Slices.size(); |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 268 | Slices.append(NewSlices.begin(), NewSlices.end()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 269 | auto SliceI = Slices.begin() + OldSize; |
Mandeep Singh Grang | 636d94d | 2018-04-13 19:47:57 +0000 | [diff] [blame] | 270 | llvm::sort(SliceI, Slices.end()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 271 | std::inplace_merge(Slices.begin(), SliceI, Slices.end()); |
| 272 | } |
| 273 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 274 | // Forward declare the iterator and range accessor for walking the |
| 275 | // partitions. |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 276 | class partition_iterator; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 277 | iterator_range<partition_iterator> partitions(); |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 278 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 279 | /// Access the dead users for this alloca. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 280 | ArrayRef<Instruction *> getDeadUsers() const { return DeadUsers; } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 281 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 282 | /// Access the dead operands referring to this alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 283 | /// |
| 284 | /// These are operands which have cannot actually be used to refer to the |
| 285 | /// alloca as they are outside its range and the user doesn't correct for |
| 286 | /// that. These mostly consist of PHI node inputs and the like which we just |
| 287 | /// need to replace with undef. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 288 | ArrayRef<Use *> getDeadOperands() const { return DeadOperands; } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 289 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 290 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 291 | void print(raw_ostream &OS, const_iterator I, StringRef Indent = " ") const; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 292 | void printSlice(raw_ostream &OS, const_iterator I, |
| 293 | StringRef Indent = " ") const; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 294 | void printUse(raw_ostream &OS, const_iterator I, |
| 295 | StringRef Indent = " ") const; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 296 | void print(raw_ostream &OS) const; |
Alp Toker | f929e09 | 2014-01-04 22:47:48 +0000 | [diff] [blame] | 297 | void dump(const_iterator I) const; |
| 298 | void dump() const; |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 299 | #endif |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 300 | |
| 301 | private: |
| 302 | template <typename DerivedT, typename RetT = void> class BuilderBase; |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 303 | class SliceBuilder; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 304 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 305 | friend class AllocaSlices::SliceBuilder; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 306 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 307 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 308 | /// Handle to alloca instruction to simplify method interfaces. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 309 | AllocaInst &AI; |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 310 | #endif |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 311 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 312 | /// The instruction responsible for this alloca not having a known set |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 313 | /// of slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 314 | /// |
| 315 | /// When an instruction (potentially) escapes the pointer to the alloca, we |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 316 | /// store a pointer to that here and abort trying to form slices of the |
| 317 | /// alloca. This will be null if the alloca slices are analyzed successfully. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 318 | Instruction *PointerEscapingInstr; |
| 319 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 320 | /// The slices of the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 321 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 322 | /// We store a vector of the slices formed by uses of the alloca here. This |
| 323 | /// vector is sorted by increasing begin offset, and then the unsplittable |
| 324 | /// slices before the splittable ones. See the Slice inner class for more |
| 325 | /// details. |
| 326 | SmallVector<Slice, 8> Slices; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 327 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 328 | /// Instructions which will become dead if we rewrite the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 329 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 330 | /// Note that these are not separated by slice. This is because we expect an |
| 331 | /// alloca to be completely rewritten or not rewritten at all. If rewritten, |
| 332 | /// all these instructions can simply be removed and replaced with undef as |
| 333 | /// they come from outside of the allocated space. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 334 | SmallVector<Instruction *, 8> DeadUsers; |
| 335 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 336 | /// Operands which will become dead if we rewrite the alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 337 | /// |
| 338 | /// These are operands that in their particular use can be replaced with |
| 339 | /// undef when we rewrite the alloca. These show up in out-of-bounds inputs |
| 340 | /// to PHI nodes and the like. They aren't entirely dead (there might be |
| 341 | /// a GEP back into the bounds using it elsewhere) and nor is the PHI, but we |
| 342 | /// want to swap this particular input for undef to simplify the use lists of |
| 343 | /// the alloca. |
| 344 | SmallVector<Use *, 8> DeadOperands; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 345 | }; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 346 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 347 | /// A partition of the slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 348 | /// |
| 349 | /// An ephemeral representation for a range of slices which can be viewed as |
| 350 | /// a partition of the alloca. This range represents a span of the alloca's |
| 351 | /// memory which cannot be split, and provides access to all of the slices |
| 352 | /// overlapping some part of the partition. |
| 353 | /// |
| 354 | /// Objects of this type are produced by traversing the alloca's slices, but |
| 355 | /// are only ephemeral and not persistent. |
| 356 | class llvm::sroa::Partition { |
| 357 | private: |
| 358 | friend class AllocaSlices; |
| 359 | friend class AllocaSlices::partition_iterator; |
| 360 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 361 | using iterator = AllocaSlices::iterator; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 362 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 363 | /// The beginning and ending offsets of the alloca for this |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 364 | /// partition. |
| 365 | uint64_t BeginOffset, EndOffset; |
| 366 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 367 | /// The start and end iterators of this partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 368 | iterator SI, SJ; |
| 369 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 370 | /// A collection of split slice tails overlapping the partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 371 | SmallVector<Slice *, 4> SplitTails; |
| 372 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 373 | /// Raw constructor builds an empty partition starting and ending at |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 374 | /// the given iterator. |
| 375 | Partition(iterator SI) : SI(SI), SJ(SI) {} |
| 376 | |
| 377 | public: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 378 | /// The start offset of this partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 379 | /// |
| 380 | /// All of the contained slices start at or after this offset. |
| 381 | uint64_t beginOffset() const { return BeginOffset; } |
| 382 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 383 | /// The end offset of this partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 384 | /// |
| 385 | /// All of the contained slices end at or before this offset. |
| 386 | uint64_t endOffset() const { return EndOffset; } |
| 387 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 388 | /// The size of the partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 389 | /// |
| 390 | /// Note that this can never be zero. |
| 391 | uint64_t size() const { |
| 392 | assert(BeginOffset < EndOffset && "Partitions must span some bytes!"); |
| 393 | return EndOffset - BeginOffset; |
| 394 | } |
| 395 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 396 | /// Test whether this partition contains no slices, and merely spans |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 397 | /// a region occupied by split slices. |
| 398 | bool empty() const { return SI == SJ; } |
| 399 | |
| 400 | /// \name Iterate slices that start within the partition. |
| 401 | /// These may be splittable or unsplittable. They have a begin offset >= the |
| 402 | /// partition begin offset. |
| 403 | /// @{ |
| 404 | // FIXME: We should probably define a "concat_iterator" helper and use that |
| 405 | // to stitch together pointee_iterators over the split tails and the |
| 406 | // contiguous iterators of the partition. That would give a much nicer |
| 407 | // interface here. We could then additionally expose filtered iterators for |
| 408 | // split, unsplit, and unsplittable splices based on the usage patterns. |
| 409 | iterator begin() const { return SI; } |
| 410 | iterator end() const { return SJ; } |
| 411 | /// @} |
| 412 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 413 | /// Get the sequence of split slice tails. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 414 | /// |
| 415 | /// These tails are of slices which start before this partition but are |
| 416 | /// split and overlap into the partition. We accumulate these while forming |
| 417 | /// partitions. |
| 418 | ArrayRef<Slice *> splitSliceTails() const { return SplitTails; } |
| 419 | }; |
| 420 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 421 | /// An iterator over partitions of the alloca's slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 422 | /// |
| 423 | /// This iterator implements the core algorithm for partitioning the alloca's |
| 424 | /// slices. It is a forward iterator as we don't support backtracking for |
| 425 | /// efficiency reasons, and re-use a single storage area to maintain the |
| 426 | /// current set of split slices. |
| 427 | /// |
| 428 | /// It is templated on the slice iterator type to use so that it can operate |
| 429 | /// with either const or non-const slice iterators. |
| 430 | class AllocaSlices::partition_iterator |
| 431 | : public iterator_facade_base<partition_iterator, std::forward_iterator_tag, |
| 432 | Partition> { |
| 433 | friend class AllocaSlices; |
| 434 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 435 | /// 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] | 436 | /// with a nice interface for examining them. |
| 437 | Partition P; |
| 438 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 439 | /// 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] | 440 | AllocaSlices::iterator SE; |
| 441 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 442 | /// 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] | 443 | /// FIXME: Do we really? |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 444 | uint64_t MaxSplitSliceEndOffset = 0; |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 445 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 446 | /// Sets the partition to be empty at given iterator, and sets the |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 447 | /// end iterator. |
| 448 | partition_iterator(AllocaSlices::iterator SI, AllocaSlices::iterator SE) |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 449 | : P(SI), SE(SE) { |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 450 | // If not already at the end, advance our state to form the initial |
| 451 | // partition. |
| 452 | if (SI != SE) |
| 453 | advance(); |
| 454 | } |
| 455 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 456 | /// Advance the iterator to the next partition. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 457 | /// |
| 458 | /// Requires that the iterator not be at the end of the slices. |
| 459 | void advance() { |
| 460 | assert((P.SI != SE || !P.SplitTails.empty()) && |
| 461 | "Cannot advance past the end of the slices!"); |
| 462 | |
| 463 | // Clear out any split uses which have ended. |
| 464 | if (!P.SplitTails.empty()) { |
| 465 | if (P.EndOffset >= MaxSplitSliceEndOffset) { |
| 466 | // If we've finished all splits, this is easy. |
| 467 | P.SplitTails.clear(); |
| 468 | MaxSplitSliceEndOffset = 0; |
| 469 | } else { |
| 470 | // Remove the uses which have ended in the prior partition. This |
| 471 | // cannot change the max split slice end because we just checked that |
| 472 | // the prior partition ended prior to that max. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 473 | P.SplitTails.erase(llvm::remove_if(P.SplitTails, |
| 474 | [&](Slice *S) { |
| 475 | return S->endOffset() <= |
| 476 | P.EndOffset; |
| 477 | }), |
| 478 | P.SplitTails.end()); |
| 479 | assert(llvm::any_of(P.SplitTails, |
| 480 | [&](Slice *S) { |
| 481 | return S->endOffset() == MaxSplitSliceEndOffset; |
| 482 | }) && |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 483 | "Could not find the current max split slice offset!"); |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 484 | assert(llvm::all_of(P.SplitTails, |
| 485 | [&](Slice *S) { |
| 486 | return S->endOffset() <= MaxSplitSliceEndOffset; |
| 487 | }) && |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 488 | "Max split slice end offset is not actually the max!"); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // If P.SI is already at the end, then we've cleared the split tail and |
| 493 | // now have an end iterator. |
| 494 | if (P.SI == SE) { |
| 495 | assert(P.SplitTails.empty() && "Failed to clear the split slices!"); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | // If we had a non-empty partition previously, set up the state for |
| 500 | // subsequent partitions. |
| 501 | if (P.SI != P.SJ) { |
| 502 | // Accumulate all the splittable slices which started in the old |
| 503 | // partition into the split list. |
| 504 | for (Slice &S : P) |
| 505 | if (S.isSplittable() && S.endOffset() > P.EndOffset) { |
| 506 | P.SplitTails.push_back(&S); |
| 507 | MaxSplitSliceEndOffset = |
| 508 | std::max(S.endOffset(), MaxSplitSliceEndOffset); |
| 509 | } |
| 510 | |
| 511 | // Start from the end of the previous partition. |
| 512 | P.SI = P.SJ; |
| 513 | |
| 514 | // If P.SI is now at the end, we at most have a tail of split slices. |
| 515 | if (P.SI == SE) { |
| 516 | P.BeginOffset = P.EndOffset; |
| 517 | P.EndOffset = MaxSplitSliceEndOffset; |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | // If the we have split slices and the next slice is after a gap and is |
| 522 | // not splittable immediately form an empty partition for the split |
| 523 | // slices up until the next slice begins. |
| 524 | if (!P.SplitTails.empty() && P.SI->beginOffset() != P.EndOffset && |
| 525 | !P.SI->isSplittable()) { |
| 526 | P.BeginOffset = P.EndOffset; |
| 527 | P.EndOffset = P.SI->beginOffset(); |
| 528 | return; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // OK, we need to consume new slices. Set the end offset based on the |
| 533 | // current slice, and step SJ past it. The beginning offset of the |
| 534 | // partition is the beginning offset of the next slice unless we have |
| 535 | // pre-existing split slices that are continuing, in which case we begin |
| 536 | // at the prior end offset. |
| 537 | P.BeginOffset = P.SplitTails.empty() ? P.SI->beginOffset() : P.EndOffset; |
| 538 | P.EndOffset = P.SI->endOffset(); |
| 539 | ++P.SJ; |
| 540 | |
| 541 | // There are two strategies to form a partition based on whether the |
| 542 | // partition starts with an unsplittable slice or a splittable slice. |
| 543 | if (!P.SI->isSplittable()) { |
| 544 | // When we're forming an unsplittable region, it must always start at |
| 545 | // the first slice and will extend through its end. |
| 546 | assert(P.BeginOffset == P.SI->beginOffset()); |
| 547 | |
| 548 | // Form a partition including all of the overlapping slices with this |
| 549 | // unsplittable slice. |
| 550 | while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) { |
| 551 | if (!P.SJ->isSplittable()) |
| 552 | P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset()); |
| 553 | ++P.SJ; |
| 554 | } |
| 555 | |
| 556 | // We have a partition across a set of overlapping unsplittable |
| 557 | // partitions. |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | // If we're starting with a splittable slice, then we need to form |
| 562 | // a synthetic partition spanning it and any other overlapping splittable |
| 563 | // splices. |
| 564 | assert(P.SI->isSplittable() && "Forming a splittable partition!"); |
| 565 | |
| 566 | // Collect all of the overlapping splittable slices. |
| 567 | while (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset && |
| 568 | P.SJ->isSplittable()) { |
| 569 | P.EndOffset = std::max(P.EndOffset, P.SJ->endOffset()); |
| 570 | ++P.SJ; |
| 571 | } |
| 572 | |
| 573 | // Back upiP.EndOffset if we ended the span early when encountering an |
| 574 | // unsplittable slice. This synthesizes the early end offset of |
| 575 | // a partition spanning only splittable slices. |
| 576 | if (P.SJ != SE && P.SJ->beginOffset() < P.EndOffset) { |
| 577 | assert(!P.SJ->isSplittable()); |
| 578 | P.EndOffset = P.SJ->beginOffset(); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | public: |
| 583 | bool operator==(const partition_iterator &RHS) const { |
| 584 | assert(SE == RHS.SE && |
| 585 | "End iterators don't match between compared partition iterators!"); |
| 586 | |
| 587 | // The observed positions of partitions is marked by the P.SI iterator and |
| 588 | // the emptiness of the split slices. The latter is only relevant when |
| 589 | // P.SI == SE, as the end iterator will additionally have an empty split |
| 590 | // slices list, but the prior may have the same P.SI and a tail of split |
| 591 | // slices. |
| 592 | if (P.SI == RHS.P.SI && P.SplitTails.empty() == RHS.P.SplitTails.empty()) { |
| 593 | assert(P.SJ == RHS.P.SJ && |
| 594 | "Same set of slices formed two different sized partitions!"); |
| 595 | assert(P.SplitTails.size() == RHS.P.SplitTails.size() && |
| 596 | "Same slice position with differently sized non-empty split " |
| 597 | "slice tails!"); |
| 598 | return true; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | partition_iterator &operator++() { |
| 604 | advance(); |
| 605 | return *this; |
| 606 | } |
| 607 | |
| 608 | Partition &operator*() { return P; } |
| 609 | }; |
| 610 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 611 | /// A forward range over the partitions of the alloca's slices. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 612 | /// |
| 613 | /// This accesses an iterator range over the partitions of the alloca's |
| 614 | /// slices. It computes these partitions on the fly based on the overlapping |
| 615 | /// offsets of the slices and the ability to split them. It will visit "empty" |
| 616 | /// partitions to cover regions of the alloca only accessed via split |
| 617 | /// slices. |
| 618 | iterator_range<AllocaSlices::partition_iterator> AllocaSlices::partitions() { |
| 619 | return make_range(partition_iterator(begin(), end()), |
| 620 | partition_iterator(end(), end())); |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 621 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 622 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 623 | static Value *foldSelectInst(SelectInst &SI) { |
| 624 | // If the condition being selected on is a constant or the same value is |
| 625 | // being selected between, fold the select. Yes this does (rarely) happen |
| 626 | // early on. |
| 627 | if (ConstantInt *CI = dyn_cast<ConstantInt>(SI.getCondition())) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 628 | return SI.getOperand(1 + CI->isZero()); |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 629 | if (SI.getOperand(1) == SI.getOperand(2)) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 630 | return SI.getOperand(1); |
Jakub Staszak | 3c6583a | 2013-02-19 22:14:45 +0000 | [diff] [blame] | 631 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 632 | return nullptr; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 633 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 634 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 635 | /// A helper that folds a PHI node or a select. |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 636 | static Value *foldPHINodeOrSelectInst(Instruction &I) { |
| 637 | if (PHINode *PN = dyn_cast<PHINode>(&I)) { |
| 638 | // If PN merges together the same value, return that value. |
| 639 | return PN->hasConstantValue(); |
| 640 | } |
| 641 | return foldSelectInst(cast<SelectInst>(I)); |
| 642 | } |
| 643 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 644 | /// Builder for the alloca slices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 645 | /// |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 646 | /// This class builds a set of alloca slices by recursively visiting the uses |
| 647 | /// of an alloca and making a slice for each load and store at each offset. |
| 648 | class AllocaSlices::SliceBuilder : public PtrUseVisitor<SliceBuilder> { |
| 649 | friend class PtrUseVisitor<SliceBuilder>; |
| 650 | friend class InstVisitor<SliceBuilder>; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 651 | |
| 652 | using Base = PtrUseVisitor<SliceBuilder>; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 653 | |
| 654 | const uint64_t AllocSize; |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 655 | AllocaSlices &AS; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 656 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 657 | SmallDenseMap<Instruction *, unsigned> MemTransferSliceMap; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 658 | SmallDenseMap<Instruction *, uint64_t> PHIOrSelectSizes; |
| 659 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 660 | /// Set to de-duplicate dead instructions found in the use walk. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 661 | SmallPtrSet<Instruction *, 4> VisitedDeadInsts; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 662 | |
| 663 | public: |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 664 | SliceBuilder(const DataLayout &DL, AllocaInst &AI, AllocaSlices &AS) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 665 | : PtrUseVisitor<SliceBuilder>(DL), |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 666 | AllocSize(DL.getTypeAllocSize(AI.getAllocatedType())), AS(AS) {} |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 667 | |
| 668 | private: |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 669 | void markAsDead(Instruction &I) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 670 | if (VisitedDeadInsts.insert(&I).second) |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 671 | AS.DeadUsers.push_back(&I); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 674 | void insertUse(Instruction &I, const APInt &Offset, uint64_t Size, |
Chandler Carruth | 9712117 | 2012-09-16 19:39:50 +0000 | [diff] [blame] | 675 | bool IsSplittable = false) { |
Chandler Carruth | f02b8bf | 2012-12-03 10:59:55 +0000 | [diff] [blame] | 676 | // Completely skip uses which have a zero size or start either before or |
| 677 | // past the end of the allocation. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 678 | if (Size == 0 || Offset.uge(AllocSize)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 679 | LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte use @" |
| 680 | << Offset |
| 681 | << " which has zero size or starts outside of the " |
| 682 | << AllocSize << " byte alloca:\n" |
| 683 | << " alloca: " << AS.AI << "\n" |
| 684 | << " use: " << I << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 685 | return markAsDead(I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 688 | uint64_t BeginOffset = Offset.getZExtValue(); |
| 689 | uint64_t EndOffset = BeginOffset + Size; |
Chandler Carruth | e7a1ba5 | 2012-09-23 11:43:14 +0000 | [diff] [blame] | 690 | |
| 691 | // Clamp the end offset to the end of the allocation. Note that this is |
| 692 | // formulated to handle even the case where "BeginOffset + Size" overflows. |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 693 | // This may appear superficially to be something we could ignore entirely, |
| 694 | // but that is not so! There may be widened loads or PHI-node uses where |
| 695 | // some instructions are dead but not others. We can't completely ignore |
| 696 | // them, and so have to record at least the information here. |
Chandler Carruth | e7a1ba5 | 2012-09-23 11:43:14 +0000 | [diff] [blame] | 697 | assert(AllocSize >= BeginOffset); // Established above. |
| 698 | if (Size > AllocSize - BeginOffset) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 699 | LLVM_DEBUG(dbgs() << "WARNING: Clamping a " << Size << " byte use @" |
| 700 | << Offset << " to remain within the " << AllocSize |
| 701 | << " byte alloca:\n" |
| 702 | << " alloca: " << AS.AI << "\n" |
| 703 | << " use: " << I << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 704 | EndOffset = AllocSize; |
| 705 | } |
| 706 | |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 707 | AS.Slices.push_back(Slice(BeginOffset, EndOffset, U, IsSplittable)); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | void visitBitCastInst(BitCastInst &BC) { |
| 711 | if (BC.use_empty()) |
| 712 | return markAsDead(BC); |
| 713 | |
| 714 | return Base::visitBitCastInst(BC); |
| 715 | } |
| 716 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 717 | void visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) { |
| 718 | if (ASC.use_empty()) |
| 719 | return markAsDead(ASC); |
| 720 | |
| 721 | return Base::visitAddrSpaceCastInst(ASC); |
| 722 | } |
| 723 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 724 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 725 | if (GEPI.use_empty()) |
| 726 | return markAsDead(GEPI); |
| 727 | |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 728 | if (SROAStrictInbounds && GEPI.isInBounds()) { |
| 729 | // FIXME: This is a manually un-factored variant of the basic code inside |
| 730 | // of GEPs with checking of the inbounds invariant specified in the |
| 731 | // langref in a very strict sense. If we ever want to enable |
| 732 | // SROAStrictInbounds, this code should be factored cleanly into |
| 733 | // PtrUseVisitor, but it is easier to experiment with SROAStrictInbounds |
Hal Finkel | 5c83a09 | 2016-03-28 11:23:21 +0000 | [diff] [blame] | 734 | // by writing out the code here where we have the underlying allocation |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 735 | // size readily available. |
| 736 | APInt GEPOffset = Offset; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 737 | const DataLayout &DL = GEPI.getModule()->getDataLayout(); |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 738 | for (gep_type_iterator GTI = gep_type_begin(GEPI), |
| 739 | GTE = gep_type_end(GEPI); |
| 740 | GTI != GTE; ++GTI) { |
| 741 | ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); |
| 742 | if (!OpC) |
| 743 | break; |
| 744 | |
| 745 | // Handle a struct index, which adds its field offset to the pointer. |
Peter Collingbourne | ab85225b | 2016-12-02 02:24:42 +0000 | [diff] [blame] | 746 | if (StructType *STy = GTI.getStructTypeOrNull()) { |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 747 | unsigned ElementIdx = OpC->getZExtValue(); |
| 748 | const StructLayout *SL = DL.getStructLayout(STy); |
| 749 | GEPOffset += |
| 750 | APInt(Offset.getBitWidth(), SL->getElementOffset(ElementIdx)); |
| 751 | } else { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 752 | // For array or vector indices, scale the index by the size of the |
| 753 | // type. |
Chandler Carruth | 3b79b2a | 2014-02-25 21:24:45 +0000 | [diff] [blame] | 754 | APInt Index = OpC->getValue().sextOrTrunc(Offset.getBitWidth()); |
| 755 | GEPOffset += Index * APInt(Offset.getBitWidth(), |
| 756 | DL.getTypeAllocSize(GTI.getIndexedType())); |
| 757 | } |
| 758 | |
| 759 | // If this index has computed an intermediate pointer which is not |
| 760 | // inbounds, then the result of the GEP is a poison value and we can |
| 761 | // delete it and all uses. |
| 762 | if (GEPOffset.ugt(AllocSize)) |
| 763 | return markAsDead(GEPI); |
| 764 | } |
| 765 | } |
| 766 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 767 | return Base::visitGetElementPtrInst(GEPI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 770 | void handleLoadOrStore(Type *Ty, Instruction &I, const APInt &Offset, |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 771 | uint64_t Size, bool IsVolatile) { |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 772 | // We allow splitting of non-volatile loads and stores where the type is an |
| 773 | // integer type. These may be used to implement 'memcpy' or other "transfer |
| 774 | // of bits" patterns. |
| 775 | bool IsSplittable = Ty->isIntegerTy() && !IsVolatile; |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 776 | |
| 777 | insertUse(I, Offset, Size, IsSplittable); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 780 | void visitLoadInst(LoadInst &LI) { |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 781 | assert((!LI.isSimple() || LI.getType()->isSingleValueType()) && |
| 782 | "All simple FCA loads should have been pre-split"); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 783 | |
| 784 | if (!IsOffsetKnown) |
| 785 | return PI.setAborted(&LI); |
| 786 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 787 | if (LI.isVolatile() && |
| 788 | LI.getPointerAddressSpace() != DL.getAllocaAddrSpace()) |
| 789 | return PI.setAborted(&LI); |
| 790 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 791 | uint64_t Size = DL.getTypeStoreSize(LI.getType()); |
| 792 | return handleLoadOrStore(LI.getType(), LI, Offset, Size, LI.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 795 | void visitStoreInst(StoreInst &SI) { |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 796 | Value *ValOp = SI.getValueOperand(); |
| 797 | if (ValOp == *U) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 798 | return PI.setEscapedAndAborted(&SI); |
| 799 | if (!IsOffsetKnown) |
| 800 | return PI.setAborted(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 801 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 802 | if (SI.isVolatile() && |
| 803 | SI.getPointerAddressSpace() != DL.getAllocaAddrSpace()) |
| 804 | return PI.setAborted(&SI); |
| 805 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 806 | uint64_t Size = DL.getTypeStoreSize(ValOp->getType()); |
| 807 | |
| 808 | // If this memory access can be shown to *statically* extend outside the |
Hiroshi Inoue | 0909ca1 | 2018-01-26 08:15:29 +0000 | [diff] [blame] | 809 | // bounds of the allocation, it's behavior is undefined, so simply |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 810 | // ignore it. Note that this is more strict than the generic clamping |
| 811 | // behavior of insertUse. We also try to handle cases which might run the |
| 812 | // risk of overflow. |
| 813 | // FIXME: We should instead consider the pointer to have escaped if this |
| 814 | // function is being instrumented for addressing bugs or race conditions. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 815 | if (Size > AllocSize || Offset.ugt(AllocSize - Size)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 816 | LLVM_DEBUG(dbgs() << "WARNING: Ignoring " << Size << " byte store @" |
| 817 | << Offset << " which extends past the end of the " |
| 818 | << AllocSize << " byte alloca:\n" |
| 819 | << " alloca: " << AS.AI << "\n" |
| 820 | << " use: " << SI << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 821 | return markAsDead(SI); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 824 | assert((!SI.isSimple() || ValOp->getType()->isSingleValueType()) && |
| 825 | "All simple FCA stores should have been pre-split"); |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 826 | handleLoadOrStore(ValOp->getType(), SI, Offset, Size, SI.isVolatile()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 829 | void visitMemSetInst(MemSetInst &II) { |
Chandler Carruth | b0de6dd | 2012-09-14 10:26:34 +0000 | [diff] [blame] | 830 | assert(II.getRawDest() == *U && "Pointer use is not the destination?"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 831 | ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength()); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 832 | if ((Length && Length->getValue() == 0) || |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 833 | (IsOffsetKnown && Offset.uge(AllocSize))) |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +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 | |
| 837 | if (!IsOffsetKnown) |
| 838 | return PI.setAborted(&II); |
| 839 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 840 | // Don't replace this with a store with a different address space. TODO: |
| 841 | // Use a store with the casted new alloca? |
| 842 | if (II.isVolatile() && II.getDestAddressSpace() != DL.getAllocaAddrSpace()) |
| 843 | return PI.setAborted(&II); |
| 844 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 845 | insertUse(II, Offset, Length ? Length->getLimitedValue() |
| 846 | : AllocSize - Offset.getLimitedValue(), |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 847 | (bool)Length); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 850 | void visitMemTransferInst(MemTransferInst &II) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 851 | ConstantInt *Length = dyn_cast<ConstantInt>(II.getLength()); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 852 | if (Length && Length->getValue() == 0) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 853 | // Zero-length mem transfer intrinsics can be ignored entirely. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 854 | return markAsDead(II); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 855 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 856 | // Because we can visit these intrinsics twice, also check to see if the |
| 857 | // first time marked this instruction as dead. If so, skip it. |
| 858 | if (VisitedDeadInsts.count(&II)) |
| 859 | return; |
| 860 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 861 | if (!IsOffsetKnown) |
| 862 | return PI.setAborted(&II); |
| 863 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 864 | // Don't replace this with a load/store with a different address space. |
| 865 | // TODO: Use a store with the casted new alloca? |
| 866 | if (II.isVolatile() && |
| 867 | (II.getDestAddressSpace() != DL.getAllocaAddrSpace() || |
| 868 | II.getSourceAddressSpace() != DL.getAllocaAddrSpace())) |
| 869 | return PI.setAborted(&II); |
| 870 | |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 871 | // This side of the transfer is completely out-of-bounds, and so we can |
| 872 | // nuke the entire transfer. However, we also need to nuke the other side |
| 873 | // if already added to our partitions. |
| 874 | // FIXME: Yet another place we really should bypass this when |
| 875 | // instrumenting for ASan. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 876 | if (Offset.uge(AllocSize)) { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 877 | SmallDenseMap<Instruction *, unsigned>::iterator MTPI = |
| 878 | MemTransferSliceMap.find(&II); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 879 | if (MTPI != MemTransferSliceMap.end()) |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 880 | AS.Slices[MTPI->second].kill(); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 881 | return markAsDead(II); |
| 882 | } |
| 883 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 884 | uint64_t RawOffset = Offset.getLimitedValue(); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 885 | uint64_t Size = Length ? Length->getLimitedValue() : AllocSize - RawOffset; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 886 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 887 | // Check for the special case where the same exact value is used for both |
| 888 | // source and dest. |
| 889 | if (*U == II.getRawDest() && *U == II.getRawSource()) { |
| 890 | // For non-volatile transfers this is a no-op. |
| 891 | if (!II.isVolatile()) |
| 892 | return markAsDead(II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 893 | |
Nick Lewycky | 6ab9d93 | 2013-07-22 23:38:27 +0000 | [diff] [blame] | 894 | return insertUse(II, Offset, Size, /*IsSplittable=*/false); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 895 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 896 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 897 | // If we have seen both source and destination for a mem transfer, then |
| 898 | // they both point to the same alloca. |
| 899 | bool Inserted; |
| 900 | SmallDenseMap<Instruction *, unsigned>::iterator MTPI; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 901 | std::tie(MTPI, Inserted) = |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 902 | MemTransferSliceMap.insert(std::make_pair(&II, AS.Slices.size())); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 903 | unsigned PrevIdx = MTPI->second; |
| 904 | if (!Inserted) { |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 905 | Slice &PrevP = AS.Slices[PrevIdx]; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 906 | |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 907 | // Check if the begin offsets match and this is a non-volatile transfer. |
| 908 | // In that case, we can completely elide the transfer. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 909 | if (!II.isVolatile() && PrevP.beginOffset() == RawOffset) { |
| 910 | PrevP.kill(); |
| 911 | return markAsDead(II); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | // Otherwise we have an offset transfer within the same alloca. We can't |
| 915 | // split those. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 916 | PrevP.makeUnsplittable(); |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 919 | // Insert the use now that we've fixed up the splittable nature. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 920 | insertUse(II, Offset, Size, /*IsSplittable=*/Inserted && Length); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 921 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 922 | // Check that we ended up with a valid index in the map. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 923 | assert(AS.Slices[PrevIdx].getUse()->getUser() == &II && |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 924 | "Map index doesn't point back to a slice with this user."); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // Disable SRoA for any intrinsics except for lifetime invariants. |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 928 | // FIXME: What about debug intrinsics? This matches old behavior, but |
Chandler Carruth | 4b40e00 | 2012-09-14 10:26:36 +0000 | [diff] [blame] | 929 | // doesn't make sense. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 930 | void visitIntrinsicInst(IntrinsicInst &II) { |
| 931 | if (!IsOffsetKnown) |
| 932 | return PI.setAborted(&II); |
| 933 | |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 934 | if (II.isLifetimeStartOrEnd()) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 935 | ConstantInt *Length = cast<ConstantInt>(II.getArgOperand(0)); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 936 | uint64_t Size = std::min(AllocSize - Offset.getLimitedValue(), |
| 937 | Length->getLimitedValue()); |
Chandler Carruth | 9712117 | 2012-09-16 19:39:50 +0000 | [diff] [blame] | 938 | insertUse(II, Offset, Size, true); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 939 | return; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 942 | Base::visitIntrinsicInst(II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | Instruction *hasUnsafePHIOrSelectUse(Instruction *Root, uint64_t &Size) { |
| 946 | // 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] | 947 | // 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] | 948 | // are considered unsplittable and the size is the maximum loaded or stored |
| 949 | // size. |
| 950 | SmallPtrSet<Instruction *, 4> Visited; |
| 951 | SmallVector<std::pair<Instruction *, Instruction *>, 4> Uses; |
| 952 | Visited.insert(Root); |
| 953 | Uses.push_back(std::make_pair(cast<Instruction>(*U), Root)); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 954 | const DataLayout &DL = Root->getModule()->getDataLayout(); |
Chandler Carruth | 8b907e8 | 2012-09-25 10:03:40 +0000 | [diff] [blame] | 955 | // If there are no loads or stores, the access is dead. We mark that as |
| 956 | // a size zero access. |
| 957 | Size = 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 958 | do { |
| 959 | Instruction *I, *UsedI; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 960 | std::tie(UsedI, I) = Uses.pop_back_val(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 961 | |
| 962 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Graham Hunter | b302561 | 2019-10-08 12:53:54 +0000 | [diff] [blame] | 963 | Size = std::max(Size, |
| 964 | DL.getTypeStoreSize(LI->getType()).getFixedSize()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 965 | continue; |
| 966 | } |
| 967 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 968 | Value *Op = SI->getOperand(0); |
| 969 | if (Op == UsedI) |
| 970 | return SI; |
Graham Hunter | b302561 | 2019-10-08 12:53:54 +0000 | [diff] [blame] | 971 | Size = std::max(Size, |
| 972 | DL.getTypeStoreSize(Op->getType()).getFixedSize()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 973 | continue; |
| 974 | } |
| 975 | |
| 976 | if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) { |
| 977 | if (!GEP->hasAllZeroIndices()) |
| 978 | return GEP; |
| 979 | } else if (!isa<BitCastInst>(I) && !isa<PHINode>(I) && |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 980 | !isa<SelectInst>(I) && !isa<AddrSpaceCastInst>(I)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 981 | return I; |
| 982 | } |
| 983 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 984 | for (User *U : I->users()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 985 | if (Visited.insert(cast<Instruction>(U)).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 986 | Uses.push_back(std::make_pair(I, cast<Instruction>(U))); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 987 | } while (!Uses.empty()); |
| 988 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 989 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 992 | void visitPHINodeOrSelectInst(Instruction &I) { |
| 993 | assert(isa<PHINode>(I) || isa<SelectInst>(I)); |
| 994 | if (I.use_empty()) |
| 995 | return markAsDead(I); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 996 | |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 997 | // TODO: We could use SimplifyInstruction here to fold PHINodes and |
| 998 | // SelectInsts. However, doing so requires to change the current |
| 999 | // dead-operand-tracking mechanism. For instance, suppose neither loading |
| 1000 | // from %U nor %other traps. Then "load (select undef, %U, %other)" does not |
| 1001 | // trap either. However, if we simply replace %U with undef using the |
| 1002 | // current dead-operand-tracking mechanism, "load (select undef, undef, |
| 1003 | // %other)" may trap because the select may return the first operand |
| 1004 | // "undef". |
| 1005 | if (Value *Result = foldPHINodeOrSelectInst(I)) { |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1006 | if (Result == *U) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1007 | // If the result of the constant fold will be the pointer, recurse |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1008 | // through the PHI/select as if we had RAUW'ed it. |
| 1009 | enqueueUsers(I); |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1010 | else |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1011 | // Otherwise the operand to the PHI/select is dead, and we can replace |
| 1012 | // it with undef. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 1013 | AS.DeadOperands.push_back(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1014 | |
| 1015 | return; |
| 1016 | } |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1017 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1018 | if (!IsOffsetKnown) |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1019 | return PI.setAborted(&I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1020 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1021 | // See if we already have computed info on this node. |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1022 | uint64_t &Size = PHIOrSelectSizes[&I]; |
| 1023 | if (!Size) { |
| 1024 | // This is a new PHI/Select, check for an unsafe use of it. |
| 1025 | if (Instruction *UnsafeI = hasUnsafePHIOrSelectUse(&I, Size)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1026 | return PI.setAborted(UnsafeI); |
| 1027 | } |
| 1028 | |
| 1029 | // For PHI and select operands outside the alloca, we can't nuke the entire |
| 1030 | // phi or select -- the other side might still be relevant, so we special |
| 1031 | // case them here and use a separate structure to track the operands |
| 1032 | // themselves which should be replaced with undef. |
| 1033 | // FIXME: This should instead be escaped in the event we're instrumenting |
| 1034 | // for address sanitization. |
Chandler Carruth | 6aedc10 | 2014-02-26 03:14:14 +0000 | [diff] [blame] | 1035 | if (Offset.uge(AllocSize)) { |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 1036 | AS.DeadOperands.push_back(U); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1037 | return; |
| 1038 | } |
| 1039 | |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1040 | insertUse(I, Offset, Size); |
| 1041 | } |
| 1042 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1043 | void visitPHINode(PHINode &PN) { visitPHINodeOrSelectInst(PN); } |
Jingyue Wu | ec33fa9 | 2014-08-22 22:45:57 +0000 | [diff] [blame] | 1044 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1045 | void visitSelectInst(SelectInst &SI) { visitPHINodeOrSelectInst(SI); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1046 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1047 | /// Disable SROA entirely if there are unhandled users of the alloca. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1048 | void visitInstruction(Instruction &I) { PI.setAborted(&I); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1049 | }; |
| 1050 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1051 | AllocaSlices::AllocaSlices(const DataLayout &DL, AllocaInst &AI) |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1052 | : |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1053 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1054 | AI(AI), |
| 1055 | #endif |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1056 | PointerEscapingInstr(nullptr) { |
Nick Lewycky | c7776f7 | 2013-08-13 22:51:58 +0000 | [diff] [blame] | 1057 | SliceBuilder PB(DL, AI, *this); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1058 | SliceBuilder::PtrInfo PtrI = PB.visitPtr(AI); |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 1059 | if (PtrI.isEscaped() || PtrI.isAborted()) { |
| 1060 | // 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] | 1061 | // possibly by just storing the PtrInfo in the AllocaSlices. |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 1062 | PointerEscapingInstr = PtrI.getEscapingInst() ? PtrI.getEscapingInst() |
| 1063 | : PtrI.getAbortingInst(); |
| 1064 | assert(PointerEscapingInstr && "Did not track a bad instruction"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1065 | return; |
Chandler Carruth | e41e7b7 | 2012-12-10 08:28:39 +0000 | [diff] [blame] | 1066 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1067 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 1068 | Slices.erase( |
| 1069 | llvm::remove_if(Slices, [](const Slice &S) { return S.isDead(); }), |
| 1070 | Slices.end()); |
Benjamin Kramer | 08e5070 | 2013-07-20 08:38:34 +0000 | [diff] [blame] | 1071 | |
Hal Finkel | 29f5131 | 2016-03-28 11:13:03 +0000 | [diff] [blame] | 1072 | #ifndef NDEBUG |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 1073 | if (SROARandomShuffleSlices) { |
Pavel Labath | c207bec | 2016-11-09 12:07:12 +0000 | [diff] [blame] | 1074 | std::mt19937 MT(static_cast<unsigned>( |
| 1075 | std::chrono::system_clock::now().time_since_epoch().count())); |
Chandler Carruth | 83cee77 | 2014-02-25 03:59:29 +0000 | [diff] [blame] | 1076 | std::shuffle(Slices.begin(), Slices.end(), MT); |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 1080 | // Sort the uses. This arranges for the offsets to be in ascending order, |
| 1081 | // and the sizes to be in descending order. |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1082 | llvm::sort(Slices); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1085 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 1086 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1087 | void AllocaSlices::print(raw_ostream &OS, const_iterator I, |
| 1088 | StringRef Indent) const { |
| 1089 | printSlice(OS, I, Indent); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 1090 | OS << "\n"; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1091 | printUse(OS, I, Indent); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1094 | void AllocaSlices::printSlice(raw_ostream &OS, const_iterator I, |
| 1095 | StringRef Indent) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1096 | OS << Indent << "[" << I->beginOffset() << "," << I->endOffset() << ")" |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1097 | << " slice #" << (I - begin()) |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 1098 | << (I->isSplittable() ? " (splittable)" : ""); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1101 | void AllocaSlices::printUse(raw_ostream &OS, const_iterator I, |
| 1102 | StringRef Indent) const { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1103 | OS << Indent << " used by: " << *I->getUse()->getUser() << "\n"; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1106 | void AllocaSlices::print(raw_ostream &OS) const { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1107 | if (PointerEscapingInstr) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1108 | OS << "Can't analyze slices for alloca: " << AI << "\n" |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1109 | << " A pointer to this alloca escaped by:\n" |
| 1110 | << " " << *PointerEscapingInstr << "\n"; |
| 1111 | return; |
| 1112 | } |
| 1113 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1114 | OS << "Slices of alloca: " << AI << "\n"; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1115 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1116 | print(OS, I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Alp Toker | f929e09 | 2014-01-04 22:47:48 +0000 | [diff] [blame] | 1119 | LLVM_DUMP_METHOD void AllocaSlices::dump(const_iterator I) const { |
| 1120 | print(dbgs(), I); |
| 1121 | } |
| 1122 | LLVM_DUMP_METHOD void AllocaSlices::dump() const { print(dbgs()); } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1123 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 1124 | #endif // !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 25fb23d | 2012-09-14 10:18:51 +0000 | [diff] [blame] | 1125 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1126 | /// Walk the range of a partitioning looking for a common type to cover this |
| 1127 | /// sequence of slices. |
| 1128 | static Type *findCommonType(AllocaSlices::const_iterator B, |
| 1129 | AllocaSlices::const_iterator E, |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1130 | uint64_t EndOffset) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1131 | Type *Ty = nullptr; |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1132 | bool TyIsCommon = true; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1133 | IntegerType *ITy = nullptr; |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1134 | |
| 1135 | // Note that we need to look at *every* alloca slice's Use to ensure we |
| 1136 | // always get consistent results regardless of the order of slices. |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1137 | for (AllocaSlices::const_iterator I = B; I != E; ++I) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1138 | Use *U = I->getUse(); |
| 1139 | if (isa<IntrinsicInst>(*U->getUser())) |
| 1140 | continue; |
| 1141 | if (I->beginOffset() != B->beginOffset() || I->endOffset() != EndOffset) |
| 1142 | continue; |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1143 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1144 | Type *UserTy = nullptr; |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 1145 | if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1146 | UserTy = LI->getType(); |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 1147 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1148 | UserTy = SI->getValueOperand()->getType(); |
Chandler Carruth | a126200 | 2013-11-19 09:03:18 +0000 | [diff] [blame] | 1149 | } |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1150 | |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1151 | if (IntegerType *UserITy = dyn_cast_or_null<IntegerType>(UserTy)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1152 | // 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] | 1153 | // 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] | 1154 | // entity causing the split. Also skip if the type is not a byte width |
| 1155 | // multiple. |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1156 | if (UserITy->getBitWidth() % 8 != 0 || |
| 1157 | UserITy->getBitWidth() / 8 > (EndOffset - B->beginOffset())) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1158 | continue; |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1159 | |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1160 | // Track the largest bitwidth integer type used in this way in case there |
| 1161 | // is no common type. |
| 1162 | if (!ITy || ITy->getBitWidth() < UserITy->getBitWidth()) |
| 1163 | ITy = UserITy; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1164 | } |
Duncan P. N. Exon Smith | 73686d3 | 2014-06-17 00:19:35 +0000 | [diff] [blame] | 1165 | |
| 1166 | // To avoid depending on the order of slices, Ty and TyIsCommon must not |
| 1167 | // depend on types skipped above. |
| 1168 | if (!UserTy || (Ty && Ty != UserTy)) |
| 1169 | TyIsCommon = false; // Give up on anything but an iN type. |
| 1170 | else |
| 1171 | Ty = UserTy; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1172 | } |
Chandler Carruth | 4de3154 | 2014-01-21 23:16:05 +0000 | [diff] [blame] | 1173 | |
| 1174 | return TyIsCommon ? Ty : ITy; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1175 | } |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1176 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1177 | /// PHI instructions that use an alloca and are subsequently loaded can be |
| 1178 | /// rewritten to load both input pointers in the pred blocks and then PHI the |
| 1179 | /// results, allowing the load of the alloca to be promoted. |
| 1180 | /// From this: |
| 1181 | /// %P2 = phi [i32* %Alloca, i32* %Other] |
| 1182 | /// %V = load i32* %P2 |
| 1183 | /// to: |
| 1184 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1185 | /// ... |
| 1186 | /// %V2 = load i32* %Other |
| 1187 | /// ... |
| 1188 | /// %V = phi [i32 %V1, i32 %V2] |
| 1189 | /// |
| 1190 | /// We can do this to a select if its only uses are loads and if the operands |
| 1191 | /// to the select can be loaded unconditionally. |
| 1192 | /// |
| 1193 | /// FIXME: This should be hoisted into a generic utility, likely in |
| 1194 | /// Transforms/Util/Local.h |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1195 | static bool isSafePHIToSpeculate(PHINode &PN) { |
Tim Northover | 60afa49 | 2019-07-09 11:35:35 +0000 | [diff] [blame] | 1196 | const DataLayout &DL = PN.getModule()->getDataLayout(); |
| 1197 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1198 | // For now, we can only do this promotion if the load is in the same block |
| 1199 | // as the PHI, and if there are no stores between the phi and load. |
| 1200 | // TODO: Allow recursive phi users. |
| 1201 | // TODO: Allow stores. |
| 1202 | BasicBlock *BB = PN.getParent(); |
Guillaume Chatelet | 301b412 | 2019-10-21 15:10:26 +0000 | [diff] [blame] | 1203 | MaybeAlign MaxAlign; |
Tim Northover | 60afa49 | 2019-07-09 11:35:35 +0000 | [diff] [blame] | 1204 | uint64_t APWidth = DL.getIndexTypeSizeInBits(PN.getType()); |
| 1205 | APInt MaxSize(APWidth, 0); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1206 | bool HaveLoad = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1207 | for (User *U : PN.users()) { |
| 1208 | LoadInst *LI = dyn_cast<LoadInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1209 | if (!LI || !LI->isSimple()) |
Chandler Carruth | e74ff4c | 2013-07-15 10:30:19 +0000 | [diff] [blame] | 1210 | return false; |
Chandler Carruth | e74ff4c | 2013-07-15 10:30:19 +0000 | [diff] [blame] | 1211 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1212 | // For now we only allow loads in the same block as the PHI. This is |
| 1213 | // a common case that happens when instcombine merges two loads through |
| 1214 | // a PHI. |
| 1215 | if (LI->getParent() != BB) |
| 1216 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1217 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1218 | // Ensure that there are no instructions between the PHI and the load that |
| 1219 | // could store. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 1220 | for (BasicBlock::iterator BBI(PN); &*BBI != LI; ++BBI) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1221 | if (BBI->mayWriteToMemory()) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1222 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1223 | |
Philip Reames | 2694522 | 2019-08-27 19:34:43 +0000 | [diff] [blame] | 1224 | uint64_t Size = DL.getTypeStoreSize(LI->getType()); |
Guillaume Chatelet | 301b412 | 2019-10-21 15:10:26 +0000 | [diff] [blame] | 1225 | MaxAlign = std::max(MaxAlign, MaybeAlign(LI->getAlignment())); |
Tim Northover | 60afa49 | 2019-07-09 11:35:35 +0000 | [diff] [blame] | 1226 | MaxSize = MaxSize.ult(Size) ? APInt(APWidth, Size) : MaxSize; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1227 | HaveLoad = true; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1230 | if (!HaveLoad) |
| 1231 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1232 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1233 | // We can only transform this if it is safe to push the loads into the |
| 1234 | // predecessor blocks. The only thing to watch out for is that we can't put |
| 1235 | // a possibly trapping load in the predecessor if it is a critical edge. |
| 1236 | for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) { |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 1237 | Instruction *TI = PN.getIncomingBlock(Idx)->getTerminator(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1238 | Value *InVal = PN.getIncomingValue(Idx); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1239 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1240 | // If the value is produced by the terminator of the predecessor (an |
| 1241 | // invoke) or it has side-effects, there is no valid place to put a load |
| 1242 | // in the predecessor. |
| 1243 | if (TI == InVal || TI->mayHaveSideEffects()) |
| 1244 | return false; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1245 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1246 | // If the predecessor has a single successor, then the edge isn't |
| 1247 | // critical. |
| 1248 | if (TI->getNumSuccessors() == 1) |
| 1249 | continue; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1250 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1251 | // If this pointer is always safe to load, or if we can prove that there |
| 1252 | // is already a load in the block, then we can move the load to the pred |
| 1253 | // block. |
Tim Northover | 60afa49 | 2019-07-09 11:35:35 +0000 | [diff] [blame] | 1254 | if (isSafeToLoadUnconditionally(InVal, MaxAlign, MaxSize, DL, TI)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1255 | continue; |
| 1256 | |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | static void speculatePHINodeLoads(PHINode &PN) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1264 | LLVM_DEBUG(dbgs() << " original: " << PN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1265 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1266 | LoadInst *SomeLoad = cast<LoadInst>(PN.user_back()); |
| 1267 | Type *LoadTy = SomeLoad->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1268 | IRBuilderTy PHIBuilder(&PN); |
| 1269 | PHINode *NewPN = PHIBuilder.CreatePHI(LoadTy, PN.getNumIncomingValues(), |
| 1270 | PN.getName() + ".sroa.speculated"); |
| 1271 | |
Suyog Sarda | c62136e | 2019-09-21 18:03:30 +0000 | [diff] [blame] | 1272 | // Get the AA tags and alignment to use from one of the loads. It does not |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1273 | // matter which one we get and if any differ. |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1274 | AAMDNodes AATags; |
| 1275 | SomeLoad->getAAMetadata(AATags); |
Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 1276 | const MaybeAlign Align = MaybeAlign(SomeLoad->getAlignment()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Rewrite all loads of the PN to use the new PHI. |
| 1279 | while (!PN.use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1280 | LoadInst *LI = cast<LoadInst>(PN.user_back()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1281 | LI->replaceAllUsesWith(NewPN); |
| 1282 | LI->eraseFromParent(); |
| 1283 | } |
| 1284 | |
| 1285 | // Inject loads into all of the pred blocks. |
Bjorn Pettersson | 81a76a3 | 2018-05-17 07:21:41 +0000 | [diff] [blame] | 1286 | DenseMap<BasicBlock*, Value*> InjectedLoads; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1287 | for (unsigned Idx = 0, Num = PN.getNumIncomingValues(); Idx != Num; ++Idx) { |
| 1288 | BasicBlock *Pred = PN.getIncomingBlock(Idx); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1289 | Value *InVal = PN.getIncomingValue(Idx); |
Bjorn Pettersson | 81a76a3 | 2018-05-17 07:21:41 +0000 | [diff] [blame] | 1290 | |
| 1291 | // A PHI node is allowed to have multiple (duplicated) entries for the same |
| 1292 | // basic block, as long as the value is the same. So if we already injected |
| 1293 | // a load in the predecessor, then we should reuse the same load for all |
| 1294 | // duplicated entries. |
| 1295 | if (Value* V = InjectedLoads.lookup(Pred)) { |
| 1296 | NewPN->addIncoming(V, Pred); |
| 1297 | continue; |
| 1298 | } |
| 1299 | |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 1300 | Instruction *TI = Pred->getTerminator(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1301 | IRBuilderTy PredBuilder(TI); |
| 1302 | |
| 1303 | LoadInst *Load = PredBuilder.CreateLoad( |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1304 | LoadTy, InVal, |
| 1305 | (PN.getName() + ".sroa.speculate.load." + Pred->getName())); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1306 | ++NumLoadsSpeculated; |
| 1307 | Load->setAlignment(Align); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1308 | if (AATags) |
| 1309 | Load->setAAMetadata(AATags); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1310 | NewPN->addIncoming(Load, Pred); |
Bjorn Pettersson | 81a76a3 | 2018-05-17 07:21:41 +0000 | [diff] [blame] | 1311 | InjectedLoads[Pred] = Load; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1314 | LLVM_DEBUG(dbgs() << " speculated to: " << *NewPN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1315 | PN.eraseFromParent(); |
| 1316 | } |
| 1317 | |
| 1318 | /// Select instructions that use an alloca and are subsequently loaded can be |
| 1319 | /// rewritten to load both input pointers and then select between the result, |
| 1320 | /// allowing the load of the alloca to be promoted. |
| 1321 | /// From this: |
| 1322 | /// %P2 = select i1 %cond, i32* %Alloca, i32* %Other |
| 1323 | /// %V = load i32* %P2 |
| 1324 | /// to: |
| 1325 | /// %V1 = load i32* %Alloca -> will be mem2reg'd |
| 1326 | /// %V2 = load i32* %Other |
| 1327 | /// %V = select i1 %cond, i32 %V1, i32 %V2 |
| 1328 | /// |
| 1329 | /// We can do this to a select if its only uses are loads and if the operand |
| 1330 | /// to the select can be loaded unconditionally. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1331 | static bool isSafeSelectToSpeculate(SelectInst &SI) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1332 | Value *TValue = SI.getTrueValue(); |
| 1333 | Value *FValue = SI.getFalseValue(); |
Artur Pilipenko | 9bb6bea | 2016-04-27 11:00:48 +0000 | [diff] [blame] | 1334 | const DataLayout &DL = SI.getModule()->getDataLayout(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1335 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1336 | for (User *U : SI.users()) { |
| 1337 | LoadInst *LI = dyn_cast<LoadInst>(U); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1338 | if (!LI || !LI->isSimple()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1339 | return false; |
| 1340 | |
Hiroshi Inoue | b300824 | 2017-06-24 15:43:33 +0000 | [diff] [blame] | 1341 | // Both operands to the select need to be dereferenceable, either |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1342 | // absolutely (e.g. allocas) or at this point because we can see other |
| 1343 | // accesses to it. |
Guillaume Chatelet | 301b412 | 2019-10-21 15:10:26 +0000 | [diff] [blame] | 1344 | if (!isSafeToLoadUnconditionally(TValue, LI->getType(), |
| 1345 | MaybeAlign(LI->getAlignment()), DL, LI)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1346 | return false; |
Guillaume Chatelet | 301b412 | 2019-10-21 15:10:26 +0000 | [diff] [blame] | 1347 | if (!isSafeToLoadUnconditionally(FValue, LI->getType(), |
| 1348 | MaybeAlign(LI->getAlignment()), DL, LI)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1349 | return false; |
| 1350 | } |
| 1351 | |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
| 1355 | static void speculateSelectInstLoads(SelectInst &SI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1356 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1357 | |
| 1358 | IRBuilderTy IRB(&SI); |
| 1359 | Value *TV = SI.getTrueValue(); |
| 1360 | Value *FV = SI.getFalseValue(); |
| 1361 | // Replace the loads of the select with a select of two loads. |
| 1362 | while (!SI.use_empty()) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1363 | LoadInst *LI = cast<LoadInst>(SI.user_back()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1364 | assert(LI->isSimple() && "We only speculate simple loads"); |
| 1365 | |
| 1366 | IRB.SetInsertPoint(LI); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 1367 | LoadInst *TL = IRB.CreateLoad(LI->getType(), TV, |
| 1368 | LI->getName() + ".sroa.speculate.load.true"); |
| 1369 | LoadInst *FL = IRB.CreateLoad(LI->getType(), FV, |
| 1370 | LI->getName() + ".sroa.speculate.load.false"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1371 | NumLoadsSpeculated += 2; |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1372 | |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1373 | // Transfer alignment and AA info if present. |
Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 1374 | TL->setAlignment(MaybeAlign(LI->getAlignment())); |
| 1375 | FL->setAlignment(MaybeAlign(LI->getAlignment())); |
Hal Finkel | cc39b67 | 2014-07-24 12:16:19 +0000 | [diff] [blame] | 1376 | |
| 1377 | AAMDNodes Tags; |
| 1378 | LI->getAAMetadata(Tags); |
| 1379 | if (Tags) { |
| 1380 | TL->setAAMetadata(Tags); |
| 1381 | FL->setAAMetadata(Tags); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1382 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1383 | |
| 1384 | Value *V = IRB.CreateSelect(SI.getCondition(), TL, FL, |
| 1385 | LI->getName() + ".sroa.speculated"); |
| 1386 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1387 | LLVM_DEBUG(dbgs() << " speculated to: " << *V << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1388 | LI->replaceAllUsesWith(V); |
| 1389 | LI->eraseFromParent(); |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 1390 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1391 | SI.eraseFromParent(); |
Chandler Carruth | 90c4a3a | 2012-10-05 01:29:06 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1394 | /// Build a GEP out of a base pointer and indices. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1395 | /// |
| 1396 | /// This will return the BasePtr if that is valid, or build a new GEP |
| 1397 | /// instruction using the IRBuilder if GEP-ing is needed. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1398 | static Value *buildGEP(IRBuilderTy &IRB, Value *BasePtr, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1399 | SmallVectorImpl<Value *> &Indices, Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1400 | if (Indices.empty()) |
| 1401 | return BasePtr; |
| 1402 | |
| 1403 | // A single zero index is a no-op, so check for this and avoid building a GEP |
| 1404 | // in that case. |
| 1405 | if (Indices.size() == 1 && cast<ConstantInt>(Indices.back())->isZero()) |
| 1406 | return BasePtr; |
| 1407 | |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 1408 | return IRB.CreateInBoundsGEP(BasePtr->getType()->getPointerElementType(), |
| 1409 | BasePtr, Indices, NamePrefix + "sroa_idx"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1412 | /// Get a natural GEP off of the BasePtr walking through Ty toward |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1413 | /// TargetTy without changing the offset of the pointer. |
| 1414 | /// |
| 1415 | /// This routine assumes we've already established a properly offset GEP with |
| 1416 | /// Indices, and arrived at the Ty type. The goal is to continue to GEP with |
| 1417 | /// zero-indices down through type layers until we find one the same as |
| 1418 | /// TargetTy. If we can't find one with the same type, we at least try to use |
| 1419 | /// one with the same size. If none of that works, we just produce the GEP as |
| 1420 | /// indicated by Indices to have the correct offset. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1421 | static Value *getNaturalGEPWithType(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1422 | Value *BasePtr, Type *Ty, Type *TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1423 | SmallVectorImpl<Value *> &Indices, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1424 | Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1425 | if (Ty == TargetTy) |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1426 | return buildGEP(IRB, BasePtr, Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1427 | |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 1428 | // Offset size to use for the indices. |
| 1429 | unsigned OffsetSize = DL.getIndexTypeSizeInBits(BasePtr->getType()); |
Chandler Carruth | dfb2efd | 2014-02-26 10:08:16 +0000 | [diff] [blame] | 1430 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1431 | // See if we can descend into a struct and locate a field with the correct |
| 1432 | // type. |
| 1433 | unsigned NumLayers = 0; |
| 1434 | Type *ElementTy = Ty; |
| 1435 | do { |
| 1436 | if (ElementTy->isPointerTy()) |
| 1437 | break; |
Chandler Carruth | dfb2efd | 2014-02-26 10:08:16 +0000 | [diff] [blame] | 1438 | |
| 1439 | if (ArrayType *ArrayTy = dyn_cast<ArrayType>(ElementTy)) { |
| 1440 | ElementTy = ArrayTy->getElementType(); |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 1441 | Indices.push_back(IRB.getIntN(OffsetSize, 0)); |
Chandler Carruth | dfb2efd | 2014-02-26 10:08:16 +0000 | [diff] [blame] | 1442 | } else if (VectorType *VectorTy = dyn_cast<VectorType>(ElementTy)) { |
| 1443 | ElementTy = VectorTy->getElementType(); |
| 1444 | Indices.push_back(IRB.getInt32(0)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1445 | } else if (StructType *STy = dyn_cast<StructType>(ElementTy)) { |
Chandler Carruth | 503eb2b | 2012-10-09 01:58:35 +0000 | [diff] [blame] | 1446 | if (STy->element_begin() == STy->element_end()) |
| 1447 | break; // Nothing left to descend into. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1448 | ElementTy = *STy->element_begin(); |
| 1449 | Indices.push_back(IRB.getInt32(0)); |
| 1450 | } else { |
| 1451 | break; |
| 1452 | } |
| 1453 | ++NumLayers; |
| 1454 | } while (ElementTy != TargetTy); |
| 1455 | if (ElementTy != TargetTy) |
| 1456 | Indices.erase(Indices.end() - NumLayers, Indices.end()); |
| 1457 | |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1458 | return buildGEP(IRB, BasePtr, Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1461 | /// Recursively compute indices for a natural GEP. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1462 | /// |
| 1463 | /// This is the recursive step for getNaturalGEPWithOffset that walks down the |
| 1464 | /// element types adding appropriate indices for the GEP. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1465 | static Value *getNaturalGEPRecursively(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1466 | Value *Ptr, Type *Ty, APInt &Offset, |
| 1467 | Type *TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1468 | SmallVectorImpl<Value *> &Indices, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1469 | Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1470 | if (Offset == 0) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1471 | return getNaturalGEPWithType(IRB, DL, Ptr, Ty, TargetTy, Indices, |
| 1472 | NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1473 | |
| 1474 | // We can't recurse through pointer types. |
| 1475 | if (Ty->isPointerTy()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1476 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1477 | |
Chandler Carruth | dd3cea8 | 2012-09-14 10:30:40 +0000 | [diff] [blame] | 1478 | // We try to analyze GEPs over vectors here, but note that these GEPs are |
| 1479 | // extremely poorly defined currently. The long-term goal is to remove GEPing |
| 1480 | // over a vector from the IR completely. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1481 | if (VectorType *VecTy = dyn_cast<VectorType>(Ty)) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1482 | unsigned ElementSizeInBits = DL.getTypeSizeInBits(VecTy->getScalarType()); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1483 | if (ElementSizeInBits % 8 != 0) { |
| 1484 | // GEPs over non-multiple of 8 size vector elements are invalid. |
| 1485 | return nullptr; |
| 1486 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1487 | APInt ElementSize(Offset.getBitWidth(), ElementSizeInBits / 8); |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1488 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1489 | if (NumSkippedElements.ugt(VecTy->getNumElements())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1490 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1491 | Offset -= NumSkippedElements * ElementSize; |
| 1492 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1493 | return getNaturalGEPRecursively(IRB, DL, Ptr, VecTy->getElementType(), |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1494 | Offset, TargetTy, Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 1498 | Type *ElementTy = ArrTy->getElementType(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1499 | APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy)); |
Chandler Carruth | 6fab42a | 2012-10-17 09:23:48 +0000 | [diff] [blame] | 1500 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1501 | if (NumSkippedElements.ugt(ArrTy->getNumElements())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1502 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1503 | |
| 1504 | Offset -= NumSkippedElements * ElementSize; |
| 1505 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1506 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1507 | Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | StructType *STy = dyn_cast<StructType>(Ty); |
| 1511 | if (!STy) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1512 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1513 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1514 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1515 | uint64_t StructOffset = Offset.getZExtValue(); |
Chandler Carruth | cabd96c | 2012-09-14 10:30:42 +0000 | [diff] [blame] | 1516 | if (StructOffset >= SL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1517 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1518 | unsigned Index = SL->getElementContainingOffset(StructOffset); |
| 1519 | Offset -= APInt(Offset.getBitWidth(), SL->getElementOffset(Index)); |
| 1520 | Type *ElementTy = STy->getElementType(Index); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1521 | if (Offset.uge(DL.getTypeAllocSize(ElementTy))) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1522 | return nullptr; // The offset points into alignment padding. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1523 | |
| 1524 | Indices.push_back(IRB.getInt32(Index)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1525 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1526 | Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1529 | /// 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] | 1530 | /// resulting in a particular type. |
| 1531 | /// |
| 1532 | /// The goal is to produce a "natural" looking GEP that works with the existing |
| 1533 | /// composite types to arrive at the appropriate offset and element type for |
| 1534 | /// a pointer. TargetTy is the element type the returned GEP should point-to if |
| 1535 | /// possible. We recurse by decreasing Offset, adding the appropriate index to |
| 1536 | /// Indices, and setting Ty to the result subtype. |
| 1537 | /// |
Chandler Carruth | 93a21e7 | 2012-09-14 10:18:49 +0000 | [diff] [blame] | 1538 | /// If no natural GEP can be constructed, this function returns null. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1539 | static Value *getNaturalGEPWithOffset(IRBuilderTy &IRB, const DataLayout &DL, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1540 | Value *Ptr, APInt Offset, Type *TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1541 | SmallVectorImpl<Value *> &Indices, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1542 | Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1543 | PointerType *Ty = cast<PointerType>(Ptr->getType()); |
| 1544 | |
| 1545 | // Don't consider any GEPs through an i8* as natural unless the TargetTy is |
| 1546 | // an i8. |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 1547 | if (Ty == IRB.getInt8PtrTy(Ty->getAddressSpace()) && TargetTy->isIntegerTy(8)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1548 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1549 | |
| 1550 | Type *ElementTy = Ty->getElementType(); |
Chandler Carruth | 3f882d4 | 2012-09-18 22:37:19 +0000 | [diff] [blame] | 1551 | if (!ElementTy->isSized()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1552 | return nullptr; // We can't GEP through an unsized element. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1553 | APInt ElementSize(Offset.getBitWidth(), DL.getTypeAllocSize(ElementTy)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1554 | if (ElementSize == 0) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1555 | 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] | 1556 | APInt NumSkippedElements = Offset.sdiv(ElementSize); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1557 | |
| 1558 | Offset -= NumSkippedElements * ElementSize; |
| 1559 | Indices.push_back(IRB.getInt(NumSkippedElements)); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1560 | return getNaturalGEPRecursively(IRB, DL, Ptr, ElementTy, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1561 | Indices, NamePrefix); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1564 | /// Compute an adjusted pointer from Ptr by Offset bytes where the |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1565 | /// resulting pointer has PointerTy. |
| 1566 | /// |
| 1567 | /// This tries very hard to compute a "natural" GEP which arrives at the offset |
| 1568 | /// and produces the pointer type desired. Where it cannot, it will try to use |
| 1569 | /// the natural GEP to arrive at the offset and bitcast to the type. Where that |
| 1570 | /// fails, it will try to use an existing i8* and GEP to the byte offset and |
| 1571 | /// bitcast to the type. |
| 1572 | /// |
| 1573 | /// The strategy for finding the more natural GEPs is to peel off layers of the |
| 1574 | /// pointer, walking back through bit casts and GEPs, searching for a base |
| 1575 | /// pointer from which we can compute a natural GEP with the desired |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 1576 | /// properties. The algorithm tries to fold as many constant indices into |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1577 | /// a single GEP as possible, thus making each GEP more independent of the |
| 1578 | /// surrounding code. |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1579 | static Value *getAdjustedPtr(IRBuilderTy &IRB, const DataLayout &DL, Value *Ptr, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 1580 | APInt Offset, Type *PointerTy, Twine NamePrefix) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1581 | // Even though we don't look through PHI nodes, we could be called on an |
| 1582 | // instruction in an unreachable block, which may be on a cycle. |
| 1583 | SmallPtrSet<Value *, 4> Visited; |
| 1584 | Visited.insert(Ptr); |
| 1585 | SmallVector<Value *, 4> Indices; |
| 1586 | |
| 1587 | // We may end up computing an offset pointer that has the wrong type. If we |
| 1588 | // 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] | 1589 | // 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] | 1590 | Value *OffsetPtr = nullptr; |
Chandler Carruth | 5986b54 | 2015-01-02 02:47:38 +0000 | [diff] [blame] | 1591 | Value *OffsetBasePtr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1592 | |
| 1593 | // Remember any i8 pointer we come across to re-use if we need to do a raw |
| 1594 | // byte offset. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1595 | Value *Int8Ptr = nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1596 | APInt Int8PtrOffset(Offset.getBitWidth(), 0); |
| 1597 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 1598 | PointerType *TargetPtrTy = cast<PointerType>(PointerTy); |
| 1599 | Type *TargetTy = TargetPtrTy->getElementType(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1600 | |
Michael Liao | 4f7f70e | 2019-06-18 21:41:13 +0000 | [diff] [blame] | 1601 | // As `addrspacecast` is , `Ptr` (the storage pointer) may have different |
| 1602 | // address space from the expected `PointerTy` (the pointer to be used). |
| 1603 | // Adjust the pointer type based the original storage pointer. |
| 1604 | auto AS = cast<PointerType>(Ptr->getType())->getAddressSpace(); |
| 1605 | PointerTy = TargetTy->getPointerTo(AS); |
| 1606 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1607 | do { |
| 1608 | // First fold any existing GEPs into the offset. |
| 1609 | while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) { |
| 1610 | APInt GEPOffset(Offset.getBitWidth(), 0); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1611 | if (!GEP->accumulateConstantOffset(DL, GEPOffset)) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | Offset += GEPOffset; |
| 1614 | Ptr = GEP->getPointerOperand(); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1615 | if (!Visited.insert(Ptr).second) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1616 | break; |
| 1617 | } |
| 1618 | |
| 1619 | // See if we can perform a natural GEP here. |
| 1620 | Indices.clear(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 1621 | if (Value *P = getNaturalGEPWithOffset(IRB, DL, Ptr, Offset, TargetTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 1622 | Indices, NamePrefix)) { |
Chandler Carruth | 5986b54 | 2015-01-02 02:47:38 +0000 | [diff] [blame] | 1623 | // If we have a new natural pointer at the offset, clear out any old |
| 1624 | // offset pointer we computed. Unless it is the base pointer or |
| 1625 | // a non-instruction, we built a GEP we don't need. Zap it. |
| 1626 | if (OffsetPtr && OffsetPtr != OffsetBasePtr) |
| 1627 | if (Instruction *I = dyn_cast<Instruction>(OffsetPtr)) { |
| 1628 | assert(I->use_empty() && "Built a GEP with uses some how!"); |
| 1629 | I->eraseFromParent(); |
| 1630 | } |
| 1631 | OffsetPtr = P; |
| 1632 | OffsetBasePtr = Ptr; |
| 1633 | // If we also found a pointer of the right type, we're done. |
| 1634 | if (P->getType() == PointerTy) |
Michael Liao | 4f7f70e | 2019-06-18 21:41:13 +0000 | [diff] [blame] | 1635 | break; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | // Stash this pointer if we've found an i8*. |
| 1639 | if (Ptr->getType()->isIntegerTy(8)) { |
| 1640 | Int8Ptr = Ptr; |
| 1641 | Int8PtrOffset = Offset; |
| 1642 | } |
| 1643 | |
| 1644 | // Peel off a layer of the pointer and update the offset appropriately. |
| 1645 | if (Operator::getOpcode(Ptr) == Instruction::BitCast) { |
| 1646 | Ptr = cast<Operator>(Ptr)->getOperand(0); |
| 1647 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(Ptr)) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 1648 | if (GA->isInterposable()) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1649 | break; |
| 1650 | Ptr = GA->getAliasee(); |
| 1651 | } else { |
| 1652 | break; |
| 1653 | } |
| 1654 | assert(Ptr->getType()->isPointerTy() && "Unexpected operand type!"); |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1655 | } while (Visited.insert(Ptr).second); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1656 | |
| 1657 | if (!OffsetPtr) { |
| 1658 | if (!Int8Ptr) { |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 1659 | Int8Ptr = IRB.CreateBitCast( |
| 1660 | Ptr, IRB.getInt8PtrTy(PointerTy->getPointerAddressSpace()), |
| 1661 | NamePrefix + "sroa_raw_cast"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1662 | Int8PtrOffset = Offset; |
| 1663 | } |
| 1664 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1665 | OffsetPtr = Int8PtrOffset == 0 |
| 1666 | ? Int8Ptr |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 1667 | : IRB.CreateInBoundsGEP(IRB.getInt8Ty(), Int8Ptr, |
| 1668 | IRB.getInt(Int8PtrOffset), |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 1669 | NamePrefix + "sroa_raw_idx"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1670 | } |
| 1671 | Ptr = OffsetPtr; |
| 1672 | |
| 1673 | // On the off chance we were targeting i8*, guard the bitcast here. |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 1674 | if (cast<PointerType>(Ptr->getType()) != TargetPtrTy) { |
| 1675 | Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, |
| 1676 | TargetPtrTy, |
| 1677 | NamePrefix + "sroa_cast"); |
| 1678 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1679 | |
| 1680 | return Ptr; |
| 1681 | } |
| 1682 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1683 | /// Compute the adjusted alignment for a load or store from an offset. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 1684 | static unsigned getAdjustedAlignment(Instruction *I, uint64_t Offset, |
| 1685 | const DataLayout &DL) { |
| 1686 | unsigned Alignment; |
| 1687 | Type *Ty; |
| 1688 | if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 1689 | Alignment = LI->getAlignment(); |
| 1690 | Ty = LI->getType(); |
| 1691 | } else if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 1692 | Alignment = SI->getAlignment(); |
| 1693 | Ty = SI->getValueOperand()->getType(); |
| 1694 | } else { |
| 1695 | llvm_unreachable("Only loads and stores are allowed!"); |
| 1696 | } |
| 1697 | |
| 1698 | if (!Alignment) |
| 1699 | Alignment = DL.getABITypeAlignment(Ty); |
| 1700 | |
| 1701 | return MinAlign(Alignment, Offset); |
| 1702 | } |
| 1703 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1704 | /// 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] | 1705 | /// |
| 1706 | /// This predicate should be used to guard calls to convertValue in order to |
| 1707 | /// ensure that we only try to convert viable values. The strategy is that we |
| 1708 | /// will peel off single element struct and array wrappings to get to an |
| 1709 | /// underlying value, and convert that value. |
| 1710 | static bool canConvertValue(const DataLayout &DL, Type *OldTy, Type *NewTy) { |
| 1711 | if (OldTy == NewTy) |
| 1712 | return true; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 1713 | |
| 1714 | // For integer types, we can't handle any bit-width differences. This would |
| 1715 | // break both vector conversions with extension and introduce endianness |
| 1716 | // issues when in conjunction with loads and stores. |
| 1717 | if (isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) { |
| 1718 | assert(cast<IntegerType>(OldTy)->getBitWidth() != |
| 1719 | cast<IntegerType>(NewTy)->getBitWidth() && |
| 1720 | "We can't have the same bitwidth for different int types"); |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1724 | if (DL.getTypeSizeInBits(NewTy) != DL.getTypeSizeInBits(OldTy)) |
| 1725 | return false; |
| 1726 | if (!NewTy->isSingleValueType() || !OldTy->isSingleValueType()) |
| 1727 | return false; |
| 1728 | |
Benjamin Kramer | 5626259 | 2013-09-22 11:24:58 +0000 | [diff] [blame] | 1729 | // We can convert pointers to integers and vice-versa. Same for vectors |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1730 | // of pointers and integers. |
| 1731 | OldTy = OldTy->getScalarType(); |
| 1732 | NewTy = NewTy->getScalarType(); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1733 | if (NewTy->isPointerTy() || OldTy->isPointerTy()) { |
Jack Liu | f101c0f | 2016-05-03 19:30:48 +0000 | [diff] [blame] | 1734 | if (NewTy->isPointerTy() && OldTy->isPointerTy()) { |
| 1735 | return cast<PointerType>(NewTy)->getPointerAddressSpace() == |
| 1736 | cast<PointerType>(OldTy)->getPointerAddressSpace(); |
| 1737 | } |
Sanjoy Das | b70ddd8 | 2017-06-17 20:28:13 +0000 | [diff] [blame] | 1738 | |
| 1739 | // We can convert integers to integral pointers, but not to non-integral |
| 1740 | // pointers. |
| 1741 | if (OldTy->isIntegerTy()) |
| 1742 | return !DL.isNonIntegralPointerType(NewTy); |
| 1743 | |
| 1744 | // We can convert integral pointers to integers, but non-integral pointers |
| 1745 | // need to remain pointers. |
| 1746 | if (!DL.isNonIntegralPointerType(OldTy)) |
| 1747 | return NewTy->isIntegerTy(); |
| 1748 | |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1749 | return false; |
| 1750 | } |
| 1751 | |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1755 | /// 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] | 1756 | /// type. |
| 1757 | /// |
| 1758 | /// This will try various different casting techniques, such as bitcasts, |
| 1759 | /// inttoptr, and ptrtoint casts. Use the \c canConvertValue predicate to test |
| 1760 | /// two types for viability with this routine. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 1761 | static Value *convertValue(const DataLayout &DL, IRBuilderTy &IRB, Value *V, |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1762 | Type *NewTy) { |
| 1763 | Type *OldTy = V->getType(); |
| 1764 | assert(canConvertValue(DL, OldTy, NewTy) && "Value not convertable to type"); |
| 1765 | |
| 1766 | if (OldTy == NewTy) |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1767 | return V; |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1768 | |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 1769 | assert(!(isa<IntegerType>(OldTy) && isa<IntegerType>(NewTy)) && |
| 1770 | "Integer types must be the exact same to convert."); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1771 | |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1772 | // See if we need inttoptr for this type pair. A cast involving both scalars |
| 1773 | // and vectors requires and additional bitcast. |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1774 | if (OldTy->isIntOrIntVectorTy() && NewTy->isPtrOrPtrVectorTy()) { |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1775 | // Expand <2 x i32> to i8* --> <2 x i32> to i64 to i8* |
| 1776 | if (OldTy->isVectorTy() && !NewTy->isVectorTy()) |
| 1777 | return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)), |
| 1778 | NewTy); |
| 1779 | |
| 1780 | // Expand i128 to <2 x i8*> --> i128 to <2 x i64> to <2 x i8*> |
| 1781 | if (!OldTy->isVectorTy() && NewTy->isVectorTy()) |
| 1782 | return IRB.CreateIntToPtr(IRB.CreateBitCast(V, DL.getIntPtrType(NewTy)), |
| 1783 | NewTy); |
| 1784 | |
| 1785 | return IRB.CreateIntToPtr(V, NewTy); |
| 1786 | } |
| 1787 | |
| 1788 | // See if we need ptrtoint for this type pair. A cast involving both scalars |
| 1789 | // and vectors requires and additional bitcast. |
Craig Topper | 95d2347 | 2017-07-09 07:04:00 +0000 | [diff] [blame] | 1790 | if (OldTy->isPtrOrPtrVectorTy() && NewTy->isIntOrIntVectorTy()) { |
Benjamin Kramer | 90901a3 | 2013-09-21 20:36:04 +0000 | [diff] [blame] | 1791 | // Expand <2 x i8*> to i128 --> <2 x i8*> to <2 x i64> to i128 |
| 1792 | if (OldTy->isVectorTy() && !NewTy->isVectorTy()) |
| 1793 | return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)), |
| 1794 | NewTy); |
| 1795 | |
| 1796 | // Expand i8* to <2 x i32> --> i8* to i64 to <2 x i32> |
| 1797 | if (!OldTy->isVectorTy() && NewTy->isVectorTy()) |
| 1798 | return IRB.CreateBitCast(IRB.CreatePtrToInt(V, DL.getIntPtrType(OldTy)), |
| 1799 | NewTy); |
| 1800 | |
| 1801 | return IRB.CreatePtrToInt(V, NewTy); |
| 1802 | } |
| 1803 | |
| 1804 | return IRB.CreateBitCast(V, NewTy); |
Chandler Carruth | aa6afbb | 2012-10-15 08:40:22 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1807 | /// Test whether the given slice use can be promoted to a vector. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1808 | /// |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1809 | /// 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] | 1810 | /// for a single slice. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 1811 | static bool isVectorPromotionViableForSlice(Partition &P, const Slice &S, |
| 1812 | VectorType *Ty, |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1813 | uint64_t ElementSize, |
| 1814 | const DataLayout &DL) { |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1815 | // First validate the slice offsets. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1816 | uint64_t BeginOffset = |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1817 | std::max(S.beginOffset(), P.beginOffset()) - P.beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1818 | uint64_t BeginIndex = BeginOffset / ElementSize; |
| 1819 | if (BeginIndex * ElementSize != BeginOffset || |
| 1820 | BeginIndex >= Ty->getNumElements()) |
| 1821 | return false; |
| 1822 | uint64_t EndOffset = |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1823 | std::min(S.endOffset(), P.endOffset()) - P.beginOffset(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1824 | uint64_t EndIndex = EndOffset / ElementSize; |
| 1825 | if (EndIndex * ElementSize != EndOffset || EndIndex > Ty->getNumElements()) |
| 1826 | return false; |
| 1827 | |
| 1828 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 1829 | uint64_t NumElements = EndIndex - BeginIndex; |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1830 | Type *SliceTy = (NumElements == 1) |
| 1831 | ? Ty->getElementType() |
| 1832 | : VectorType::get(Ty->getElementType(), NumElements); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1833 | |
| 1834 | Type *SplitIntTy = |
| 1835 | Type::getIntNTy(Ty->getContext(), NumElements * ElementSize * 8); |
| 1836 | |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1837 | Use *U = S.getUse(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1838 | |
| 1839 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) { |
| 1840 | if (MI->isVolatile()) |
| 1841 | return false; |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 1842 | if (!S.isSplittable()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1843 | return false; // Skip any unsplittable intrinsics. |
Owen Anderson | 6c19ab1 | 2014-08-07 21:07:35 +0000 | [diff] [blame] | 1844 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) { |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 1845 | if (!II->isLifetimeStartOrEnd()) |
Owen Anderson | 6c19ab1 | 2014-08-07 21:07:35 +0000 | [diff] [blame] | 1846 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1847 | } else if (U->get()->getType()->getPointerElementType()->isStructTy()) { |
| 1848 | // Disable vector promotion when there are loads or stores of an FCA. |
| 1849 | return false; |
| 1850 | } else if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
| 1851 | if (LI->isVolatile()) |
| 1852 | return false; |
| 1853 | Type *LTy = LI->getType(); |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1854 | if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1855 | assert(LTy->isIntegerTy()); |
| 1856 | LTy = SplitIntTy; |
| 1857 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1858 | if (!canConvertValue(DL, SliceTy, LTy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1859 | return false; |
| 1860 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
| 1861 | if (SI->isVolatile()) |
| 1862 | return false; |
| 1863 | Type *STy = SI->getValueOperand()->getType(); |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1864 | if (P.beginOffset() > S.beginOffset() || P.endOffset() < S.endOffset()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1865 | assert(STy->isIntegerTy()); |
| 1866 | STy = SplitIntTy; |
| 1867 | } |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1868 | if (!canConvertValue(DL, STy, SliceTy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1869 | return false; |
Chandler Carruth | 1ed848d | 2013-07-19 10:57:32 +0000 | [diff] [blame] | 1870 | } else { |
| 1871 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
| 1874 | return true; |
| 1875 | } |
| 1876 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1877 | /// Test whether the given alloca partitioning and range of slices can be |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 1878 | /// promoted to a vector. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1879 | /// |
| 1880 | /// This is a quick test to check whether we can rewrite a particular alloca |
| 1881 | /// partition (and its newly formed alloca) into a vector alloca with only |
| 1882 | /// whole-vector loads and stores such that it could be promoted to a vector |
| 1883 | /// SSA value. We only can ensure this for a limited set of operations, and we |
| 1884 | /// don't want to do the rewrites unless we are confident that the result will |
| 1885 | /// be promotable, so we have an early test here. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 1886 | static VectorType *isVectorPromotionViable(Partition &P, const DataLayout &DL) { |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1887 | // Collect the candidate types for vector-based promotion. Also track whether |
| 1888 | // we have different element types. |
| 1889 | SmallVector<VectorType *, 4> CandidateTys; |
| 1890 | Type *CommonEltTy = nullptr; |
| 1891 | bool HaveCommonEltTy = true; |
| 1892 | auto CheckCandidateType = [&](Type *Ty) { |
| 1893 | if (auto *VTy = dyn_cast<VectorType>(Ty)) { |
Suyog Sarda | cd629ea | 2019-09-21 18:16:37 +0000 | [diff] [blame] | 1894 | // Return if bitcast to vectors is different for total size in bits. |
| 1895 | if (!CandidateTys.empty()) { |
| 1896 | VectorType *V = CandidateTys[0]; |
| 1897 | if (DL.getTypeSizeInBits(VTy) != DL.getTypeSizeInBits(V)) { |
| 1898 | CandidateTys.clear(); |
| 1899 | return; |
| 1900 | } |
| 1901 | } |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1902 | CandidateTys.push_back(VTy); |
| 1903 | if (!CommonEltTy) |
| 1904 | CommonEltTy = VTy->getElementType(); |
| 1905 | else if (CommonEltTy != VTy->getElementType()) |
| 1906 | HaveCommonEltTy = false; |
| 1907 | } |
| 1908 | }; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1909 | // 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] | 1910 | for (const Slice &S : P) |
| 1911 | if (S.beginOffset() == P.beginOffset() && |
| 1912 | S.endOffset() == P.endOffset()) { |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1913 | if (auto *LI = dyn_cast<LoadInst>(S.getUse()->getUser())) |
| 1914 | CheckCandidateType(LI->getType()); |
| 1915 | else if (auto *SI = dyn_cast<StoreInst>(S.getUse()->getUser())) |
| 1916 | CheckCandidateType(SI->getValueOperand()->getType()); |
| 1917 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 1918 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1919 | // If we didn't find a vector type, nothing to do here. |
| 1920 | if (CandidateTys.empty()) |
| 1921 | return nullptr; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1922 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1923 | // Remove non-integer vector types if we had multiple common element types. |
| 1924 | // FIXME: It'd be nice to replace them with integer vector types, but we can't |
| 1925 | // do that until all the backends are known to produce good code for all |
| 1926 | // integer vector types. |
| 1927 | if (!HaveCommonEltTy) { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 1928 | CandidateTys.erase( |
| 1929 | llvm::remove_if(CandidateTys, |
| 1930 | [](VectorType *VTy) { |
| 1931 | return !VTy->getElementType()->isIntegerTy(); |
| 1932 | }), |
| 1933 | CandidateTys.end()); |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1934 | |
| 1935 | // If there were no integer vector types, give up. |
| 1936 | if (CandidateTys.empty()) |
| 1937 | return nullptr; |
| 1938 | |
| 1939 | // Rank the remaining candidate vector types. This is easy because we know |
| 1940 | // they're all integer vectors. We sort by ascending number of elements. |
| 1941 | auto RankVectorTypes = [&DL](VectorType *RHSTy, VectorType *LHSTy) { |
David L. Jones | 41cecba | 2017-01-13 21:02:41 +0000 | [diff] [blame] | 1942 | (void)DL; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1943 | assert(DL.getTypeSizeInBits(RHSTy) == DL.getTypeSizeInBits(LHSTy) && |
| 1944 | "Cannot have vector types of different sizes!"); |
| 1945 | assert(RHSTy->getElementType()->isIntegerTy() && |
| 1946 | "All non-integer types eliminated!"); |
| 1947 | assert(LHSTy->getElementType()->isIntegerTy() && |
| 1948 | "All non-integer types eliminated!"); |
| 1949 | return RHSTy->getNumElements() < LHSTy->getNumElements(); |
| 1950 | }; |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1951 | llvm::sort(CandidateTys, RankVectorTypes); |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1952 | CandidateTys.erase( |
| 1953 | std::unique(CandidateTys.begin(), CandidateTys.end(), RankVectorTypes), |
| 1954 | CandidateTys.end()); |
| 1955 | } else { |
| 1956 | // The only way to have the same element type in every vector type is to |
| 1957 | // have the same vector type. Check that and remove all but one. |
| 1958 | #ifndef NDEBUG |
| 1959 | for (VectorType *VTy : CandidateTys) { |
| 1960 | assert(VTy->getElementType() == CommonEltTy && |
| 1961 | "Unaccounted for element type!"); |
| 1962 | assert(VTy == CandidateTys[0] && |
| 1963 | "Different vector types with the same element type!"); |
| 1964 | } |
| 1965 | #endif |
| 1966 | CandidateTys.resize(1); |
| 1967 | } |
| 1968 | |
| 1969 | // Try each vector type, and return the one which works. |
| 1970 | auto CheckVectorTypeForPromotion = [&](VectorType *VTy) { |
| 1971 | uint64_t ElementSize = DL.getTypeSizeInBits(VTy->getElementType()); |
| 1972 | |
| 1973 | // While the definition of LLVM vectors is bitpacked, we don't support sizes |
| 1974 | // that aren't byte sized. |
| 1975 | if (ElementSize % 8) |
| 1976 | return false; |
| 1977 | assert((DL.getTypeSizeInBits(VTy) % 8) == 0 && |
| 1978 | "vector size not a multiple of element size?"); |
| 1979 | ElementSize /= 8; |
| 1980 | |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1981 | for (const Slice &S : P) |
| 1982 | if (!isVectorPromotionViableForSlice(P, S, VTy, ElementSize, DL)) |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1983 | return false; |
| 1984 | |
Chandler Carruth | ffb7ce5 | 2014-12-24 01:48:09 +0000 | [diff] [blame] | 1985 | for (const Slice *S : P.splitSliceTails()) |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 1986 | if (!isVectorPromotionViableForSlice(P, *S, VTy, ElementSize, DL)) |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 1987 | return false; |
| 1988 | |
| 1989 | return true; |
| 1990 | }; |
| 1991 | for (VectorType *VTy : CandidateTys) |
| 1992 | if (CheckVectorTypeForPromotion(VTy)) |
| 1993 | return VTy; |
| 1994 | |
| 1995 | return nullptr; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1998 | /// Test whether a slice of an alloca is valid for integer widening. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 1999 | /// |
| 2000 | /// This implements the necessary checking for the \c isIntegerWideningViable |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2001 | /// test below on a single slice of the alloca. |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2002 | static bool isIntegerWideningViableForSlice(const Slice &S, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2003 | uint64_t AllocBeginOffset, |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2004 | Type *AllocaTy, |
| 2005 | const DataLayout &DL, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2006 | bool &WholeAllocaOp) { |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2007 | uint64_t Size = DL.getTypeStoreSize(AllocaTy); |
| 2008 | |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 2009 | uint64_t RelBegin = S.beginOffset() - AllocBeginOffset; |
| 2010 | uint64_t RelEnd = S.endOffset() - AllocBeginOffset; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2011 | |
| 2012 | // 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] | 2013 | // the end of the alloca's type and into its padding. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2014 | if (RelEnd > Size) |
| 2015 | return false; |
| 2016 | |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 2017 | Use *U = S.getUse(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2018 | |
| 2019 | if (LoadInst *LI = dyn_cast<LoadInst>(U->getUser())) { |
| 2020 | if (LI->isVolatile()) |
| 2021 | return false; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2022 | // We can't handle loads that extend past the allocated memory. |
| 2023 | if (DL.getTypeStoreSize(LI->getType()) > Size) |
| 2024 | return false; |
Hiroshi Inoue | f5c0e6c | 2018-05-17 06:32:17 +0000 | [diff] [blame] | 2025 | // So far, AllocaSliceRewriter does not support widening split slice tails |
| 2026 | // in rewriteIntegerLoad. |
| 2027 | if (S.beginOffset() < AllocBeginOffset) |
| 2028 | return false; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2029 | // Note that we don't count vector loads or stores as whole-alloca |
| 2030 | // operations which enable integer widening because we would prefer to use |
| 2031 | // vector widening instead. |
| 2032 | if (!isa<VectorType>(LI->getType()) && RelBegin == 0 && RelEnd == Size) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2033 | WholeAllocaOp = true; |
| 2034 | if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2035 | if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy)) |
Chandler Carruth | e3899f2 | 2013-07-15 17:36:21 +0000 | [diff] [blame] | 2036 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2037 | } else if (RelBegin != 0 || RelEnd != Size || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2038 | !canConvertValue(DL, AllocaTy, LI->getType())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2039 | // Non-integer loads need to be convertible from the alloca type so that |
| 2040 | // they are promotable. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2041 | return false; |
| 2042 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2043 | } else if (StoreInst *SI = dyn_cast<StoreInst>(U->getUser())) { |
| 2044 | Type *ValueTy = SI->getValueOperand()->getType(); |
| 2045 | if (SI->isVolatile()) |
| 2046 | return false; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2047 | // We can't handle stores that extend past the allocated memory. |
| 2048 | if (DL.getTypeStoreSize(ValueTy) > Size) |
| 2049 | return false; |
Hiroshi Inoue | f5c0e6c | 2018-05-17 06:32:17 +0000 | [diff] [blame] | 2050 | // So far, AllocaSliceRewriter does not support widening split slice tails |
| 2051 | // in rewriteIntegerStore. |
| 2052 | if (S.beginOffset() < AllocBeginOffset) |
| 2053 | return false; |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2054 | // Note that we don't count vector loads or stores as whole-alloca |
| 2055 | // operations which enable integer widening because we would prefer to use |
| 2056 | // vector widening instead. |
| 2057 | if (!isa<VectorType>(ValueTy) && RelBegin == 0 && RelEnd == Size) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2058 | WholeAllocaOp = true; |
| 2059 | if (IntegerType *ITy = dyn_cast<IntegerType>(ValueTy)) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2060 | if (ITy->getBitWidth() < DL.getTypeStoreSizeInBits(ITy)) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2061 | return false; |
| 2062 | } else if (RelBegin != 0 || RelEnd != Size || |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2063 | !canConvertValue(DL, ValueTy, AllocaTy)) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2064 | // Non-integer stores need to be convertible to the alloca type so that |
| 2065 | // they are promotable. |
| 2066 | return false; |
| 2067 | } |
| 2068 | } else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(U->getUser())) { |
| 2069 | if (MI->isVolatile() || !isa<Constant>(MI->getLength())) |
| 2070 | return false; |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 2071 | if (!S.isSplittable()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2072 | return false; // Skip any unsplittable intrinsics. |
| 2073 | } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U->getUser())) { |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 2074 | if (!II->isLifetimeStartOrEnd()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2075 | return false; |
| 2076 | } else { |
| 2077 | return false; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2078 | } |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2079 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2080 | return true; |
| 2081 | } |
| 2082 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2083 | /// Test whether the given alloca partition's integer operations can be |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2084 | /// widened to promotable ones. |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2085 | /// |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2086 | /// This is a quick test to check whether we can rewrite the integer loads and |
| 2087 | /// stores to a particular alloca into wider loads and stores and be able to |
| 2088 | /// promote the resulting alloca. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 2089 | static bool isIntegerWideningViable(Partition &P, Type *AllocaTy, |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2090 | const DataLayout &DL) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2091 | uint64_t SizeInBits = DL.getTypeSizeInBits(AllocaTy); |
Benjamin Kramer | 47534c7 | 2012-12-01 11:53:32 +0000 | [diff] [blame] | 2092 | // Don't create integer types larger than the maximum bitwidth. |
| 2093 | if (SizeInBits > IntegerType::MAX_INT_BITS) |
| 2094 | return false; |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Don't try to handle allocas with bit-padding. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2097 | if (SizeInBits != DL.getTypeStoreSizeInBits(AllocaTy)) |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2098 | return false; |
| 2099 | |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2100 | // We need to ensure that an integer type with the appropriate bitwidth can |
| 2101 | // be converted to the alloca type, whatever that is. We don't want to force |
| 2102 | // the alloca itself to have an integer type if there is a more suitable one. |
| 2103 | Type *IntTy = Type::getIntNTy(AllocaTy->getContext(), SizeInBits); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2104 | if (!canConvertValue(DL, AllocaTy, IntTy) || |
| 2105 | !canConvertValue(DL, IntTy, AllocaTy)) |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2106 | return false; |
| 2107 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2108 | // While examining uses, we ensure that the alloca has a covering load or |
| 2109 | // store. We don't want to widen the integer operations only to fail to |
| 2110 | // promote due to some other unsplittable entry (which we may make splittable |
Chandler Carruth | 5955c9e | 2013-07-19 07:12:23 +0000 | [diff] [blame] | 2111 | // later). However, if there are only splittable uses, go ahead and assume |
| 2112 | // that we cover the alloca. |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2113 | // FIXME: We shouldn't consider split slices that happen to start in the |
| 2114 | // partition here... |
Chandler Carruth | c659df9 | 2014-10-16 20:24:07 +0000 | [diff] [blame] | 2115 | bool WholeAllocaOp = |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2116 | P.begin() != P.end() ? false : DL.isLegalInteger(SizeInBits); |
Chandler Carruth | 43c8b46 | 2012-10-04 10:39:28 +0000 | [diff] [blame] | 2117 | |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2118 | for (const Slice &S : P) |
| 2119 | if (!isIntegerWideningViableForSlice(S, P.beginOffset(), AllocaTy, DL, |
| 2120 | WholeAllocaOp)) |
Chandler Carruth | 43c8b46 | 2012-10-04 10:39:28 +0000 | [diff] [blame] | 2121 | return false; |
| 2122 | |
Chandler Carruth | ffb7ce5 | 2014-12-24 01:48:09 +0000 | [diff] [blame] | 2123 | for (const Slice *S : P.splitSliceTails()) |
Chandler Carruth | 5031bbe | 2014-12-24 01:05:14 +0000 | [diff] [blame] | 2124 | if (!isIntegerWideningViableForSlice(*S, P.beginOffset(), AllocaTy, DL, |
| 2125 | WholeAllocaOp)) |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2126 | return false; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2127 | |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2128 | return WholeAllocaOp; |
| 2129 | } |
| 2130 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2131 | static Value *extractInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *V, |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2132 | IntegerType *Ty, uint64_t Offset, |
| 2133 | const Twine &Name) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2134 | LLVM_DEBUG(dbgs() << " start: " << *V << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2135 | IntegerType *IntTy = cast<IntegerType>(V->getType()); |
| 2136 | assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) && |
| 2137 | "Element extends past full value"); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2138 | uint64_t ShAmt = 8 * Offset; |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2139 | if (DL.isBigEndian()) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2140 | ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2141 | if (ShAmt) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2142 | V = IRB.CreateLShr(V, ShAmt, Name + ".shift"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2143 | LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2144 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2145 | assert(Ty->getBitWidth() <= IntTy->getBitWidth() && |
| 2146 | "Cannot extract to a larger integer!"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2147 | if (Ty != IntTy) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2148 | V = IRB.CreateTrunc(V, Ty, Name + ".trunc"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2149 | LLVM_DEBUG(dbgs() << " trunced: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2150 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2151 | return V; |
| 2152 | } |
| 2153 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2154 | static Value *insertInteger(const DataLayout &DL, IRBuilderTy &IRB, Value *Old, |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2155 | Value *V, uint64_t Offset, const Twine &Name) { |
| 2156 | IntegerType *IntTy = cast<IntegerType>(Old->getType()); |
| 2157 | IntegerType *Ty = cast<IntegerType>(V->getType()); |
| 2158 | assert(Ty->getBitWidth() <= IntTy->getBitWidth() && |
| 2159 | "Cannot insert a larger integer!"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2160 | LLVM_DEBUG(dbgs() << " start: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2161 | if (Ty != IntTy) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2162 | V = IRB.CreateZExt(V, IntTy, Name + ".ext"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2163 | LLVM_DEBUG(dbgs() << " extended: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2164 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2165 | assert(DL.getTypeStoreSize(Ty) + Offset <= DL.getTypeStoreSize(IntTy) && |
| 2166 | "Element store outside of alloca store"); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2167 | uint64_t ShAmt = 8 * Offset; |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2168 | if (DL.isBigEndian()) |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2169 | ShAmt = 8 * (DL.getTypeStoreSize(IntTy) - DL.getTypeStoreSize(Ty) - Offset); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2170 | if (ShAmt) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2171 | V = IRB.CreateShl(V, ShAmt, Name + ".shift"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2172 | LLVM_DEBUG(dbgs() << " shifted: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2173 | } |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2174 | |
| 2175 | if (ShAmt || Ty->getBitWidth() < IntTy->getBitWidth()) { |
| 2176 | APInt Mask = ~Ty->getMask().zext(IntTy->getBitWidth()).shl(ShAmt); |
| 2177 | Old = IRB.CreateAnd(Old, Mask, Name + ".mask"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2178 | LLVM_DEBUG(dbgs() << " masked: " << *Old << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2179 | V = IRB.CreateOr(Old, V, Name + ".insert"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2180 | LLVM_DEBUG(dbgs() << " inserted: " << *V << "\n"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2181 | } |
| 2182 | return V; |
| 2183 | } |
| 2184 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2185 | static Value *extractVector(IRBuilderTy &IRB, Value *V, unsigned BeginIndex, |
| 2186 | unsigned EndIndex, const Twine &Name) { |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2187 | VectorType *VecTy = cast<VectorType>(V->getType()); |
| 2188 | unsigned NumElements = EndIndex - BeginIndex; |
| 2189 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
| 2190 | |
| 2191 | if (NumElements == VecTy->getNumElements()) |
| 2192 | return V; |
| 2193 | |
| 2194 | if (NumElements == 1) { |
| 2195 | V = IRB.CreateExtractElement(V, IRB.getInt32(BeginIndex), |
| 2196 | Name + ".extract"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2197 | LLVM_DEBUG(dbgs() << " extract: " << *V << "\n"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2198 | return V; |
| 2199 | } |
| 2200 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2201 | SmallVector<Constant *, 8> Mask; |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2202 | Mask.reserve(NumElements); |
| 2203 | for (unsigned i = BeginIndex; i != EndIndex; ++i) |
| 2204 | Mask.push_back(IRB.getInt32(i)); |
| 2205 | V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()), |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2206 | ConstantVector::get(Mask), Name + ".extract"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2207 | LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2208 | return V; |
| 2209 | } |
| 2210 | |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 2211 | static Value *insertVector(IRBuilderTy &IRB, Value *Old, Value *V, |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2212 | unsigned BeginIndex, const Twine &Name) { |
| 2213 | VectorType *VecTy = cast<VectorType>(Old->getType()); |
| 2214 | assert(VecTy && "Can only insert a vector into a vector"); |
| 2215 | |
| 2216 | VectorType *Ty = dyn_cast<VectorType>(V->getType()); |
| 2217 | if (!Ty) { |
| 2218 | // Single element to insert. |
| 2219 | V = IRB.CreateInsertElement(Old, V, IRB.getInt32(BeginIndex), |
| 2220 | Name + ".insert"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2221 | LLVM_DEBUG(dbgs() << " insert: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2222 | return V; |
| 2223 | } |
| 2224 | |
| 2225 | assert(Ty->getNumElements() <= VecTy->getNumElements() && |
| 2226 | "Too many elements!"); |
| 2227 | if (Ty->getNumElements() == VecTy->getNumElements()) { |
| 2228 | assert(V->getType() == VecTy && "Vector type mismatch"); |
| 2229 | return V; |
| 2230 | } |
| 2231 | unsigned EndIndex = BeginIndex + Ty->getNumElements(); |
| 2232 | |
| 2233 | // When inserting a smaller vector into the larger to store, we first |
| 2234 | // use a shuffle vector to widen it with undef elements, and then |
| 2235 | // a second shuffle vector to select between the loaded vector and the |
| 2236 | // incoming vector. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2237 | SmallVector<Constant *, 8> Mask; |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2238 | Mask.reserve(VecTy->getNumElements()); |
| 2239 | for (unsigned i = 0; i != VecTy->getNumElements(); ++i) |
| 2240 | if (i >= BeginIndex && i < EndIndex) |
| 2241 | Mask.push_back(IRB.getInt32(i - BeginIndex)); |
| 2242 | else |
| 2243 | Mask.push_back(UndefValue::get(IRB.getInt32Ty())); |
| 2244 | V = IRB.CreateShuffleVector(V, UndefValue::get(V->getType()), |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2245 | ConstantVector::get(Mask), Name + ".expand"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2246 | LLVM_DEBUG(dbgs() << " shuffle: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2247 | |
| 2248 | Mask.clear(); |
| 2249 | for (unsigned i = 0; i != VecTy->getNumElements(); ++i) |
Nadav Rotem | 1e21191 | 2013-05-01 19:53:30 +0000 | [diff] [blame] | 2250 | Mask.push_back(IRB.getInt1(i >= BeginIndex && i < EndIndex)); |
| 2251 | |
| 2252 | V = IRB.CreateSelect(ConstantVector::get(Mask), V, Old, Name + "blend"); |
| 2253 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2254 | LLVM_DEBUG(dbgs() << " blend: " << *V << "\n"); |
Chandler Carruth | ce4562b | 2012-12-17 13:41:21 +0000 | [diff] [blame] | 2255 | return V; |
| 2256 | } |
| 2257 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2258 | /// Visitor to rewrite instructions using p particular slice of an alloca |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2259 | /// to use a new alloca. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2260 | /// |
| 2261 | /// Also implements the rewriting to vector-based accesses when the partition |
| 2262 | /// passes the isVectorPromotionViable predicate. Most of the rewriting logic |
| 2263 | /// lives here. |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 2264 | class llvm::sroa::AllocaSliceRewriter |
| 2265 | : public InstVisitor<AllocaSliceRewriter, bool> { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2266 | // Befriend the base class so it can delegate to private visit methods. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2267 | friend class InstVisitor<AllocaSliceRewriter, bool>; |
| 2268 | |
| 2269 | using Base = InstVisitor<AllocaSliceRewriter, bool>; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2270 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2271 | const DataLayout &DL; |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 2272 | AllocaSlices &AS; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2273 | SROA &Pass; |
| 2274 | AllocaInst &OldAI, &NewAI; |
| 2275 | const uint64_t NewAllocaBeginOffset, NewAllocaEndOffset; |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2276 | Type *NewAllocaTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2277 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2278 | // This is a convenience and flag variable that will be null unless the new |
| 2279 | // alloca's integer operations should be widened to this integer type due to |
| 2280 | // passing isIntegerWideningViable above. If it is non-null, the desired |
| 2281 | // integer type will be stored here for easy access during rewriting. |
| 2282 | IntegerType *IntTy; |
| 2283 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2284 | // If we are rewriting an alloca partition which can be written as pure |
| 2285 | // vector operations, we stash extra information here. When VecTy is |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 2286 | // non-null, we have some strict guarantees about the rewritten alloca: |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2287 | // - The new alloca is exactly the size of the vector type here. |
| 2288 | // - The accesses all either map to the entire vector or to a single |
| 2289 | // element. |
| 2290 | // - The set of accessing instructions is only one of those handled above |
| 2291 | // in isVectorPromotionViable. Generally these are the same access kinds |
| 2292 | // which are promotable via mem2reg. |
| 2293 | VectorType *VecTy; |
| 2294 | Type *ElementTy; |
| 2295 | uint64_t ElementSize; |
| 2296 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2297 | // The original offset of the slice currently being rewritten relative to |
| 2298 | // the original alloca. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2299 | uint64_t BeginOffset = 0; |
| 2300 | uint64_t EndOffset = 0; |
| 2301 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2302 | // The new offsets of the slice currently being rewritten relative to the |
| 2303 | // original alloca. |
| 2304 | uint64_t NewBeginOffset, NewEndOffset; |
| 2305 | |
| 2306 | uint64_t SliceSize; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2307 | bool IsSplittable = false; |
| 2308 | bool IsSplit = false; |
| 2309 | Use *OldUse = nullptr; |
| 2310 | Instruction *OldPtr = nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2311 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 2312 | // Track post-rewrite users which are PHI nodes and Selects. |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 2313 | SmallSetVector<PHINode *, 8> &PHIUsers; |
| 2314 | SmallSetVector<SelectInst *, 8> &SelectUsers; |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2315 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2316 | // Utility IR builder, whose name prefix is setup for each visited use, and |
| 2317 | // the insertion point is set to point to the user. |
| 2318 | IRBuilderTy IRB; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2319 | |
| 2320 | public: |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 2321 | AllocaSliceRewriter(const DataLayout &DL, AllocaSlices &AS, SROA &Pass, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2322 | AllocaInst &OldAI, AllocaInst &NewAI, |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2323 | uint64_t NewAllocaBeginOffset, |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2324 | uint64_t NewAllocaEndOffset, bool IsIntegerPromotable, |
| 2325 | VectorType *PromotableVecTy, |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 2326 | SmallSetVector<PHINode *, 8> &PHIUsers, |
| 2327 | SmallSetVector<SelectInst *, 8> &SelectUsers) |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 2328 | : DL(DL), AS(AS), Pass(Pass), OldAI(OldAI), NewAI(NewAI), |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2329 | NewAllocaBeginOffset(NewAllocaBeginOffset), |
| 2330 | NewAllocaEndOffset(NewAllocaEndOffset), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2331 | NewAllocaTy(NewAI.getAllocatedType()), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2332 | IntTy(IsIntegerPromotable |
| 2333 | ? Type::getIntNTy( |
| 2334 | NewAI.getContext(), |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2335 | DL.getTypeSizeInBits(NewAI.getAllocatedType())) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2336 | : nullptr), |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2337 | VecTy(PromotableVecTy), |
| 2338 | ElementTy(VecTy ? VecTy->getElementType() : nullptr), |
| 2339 | ElementSize(VecTy ? DL.getTypeSizeInBits(ElementTy) / 8 : 0), |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 2340 | PHIUsers(PHIUsers), SelectUsers(SelectUsers), |
Chandler Carruth | 83ea195 | 2013-07-24 09:47:28 +0000 | [diff] [blame] | 2341 | IRB(NewAI.getContext(), ConstantFolder()) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2342 | if (VecTy) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2343 | assert((DL.getTypeSizeInBits(ElementTy) % 8) == 0 && |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2344 | "Only multiple-of-8 sized vector elements are viable"); |
| 2345 | ++NumVectorized; |
| 2346 | } |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 2347 | assert((!IntTy && !VecTy) || (IntTy && !VecTy) || (!IntTy && VecTy)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2350 | bool visit(AllocaSlices::const_iterator I) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2351 | bool CanSROA = true; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2352 | BeginOffset = I->beginOffset(); |
| 2353 | EndOffset = I->endOffset(); |
| 2354 | IsSplittable = I->isSplittable(); |
| 2355 | IsSplit = |
| 2356 | BeginOffset < NewAllocaBeginOffset || EndOffset > NewAllocaEndOffset; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2357 | LLVM_DEBUG(dbgs() << " rewriting " << (IsSplit ? "split " : "")); |
| 2358 | LLVM_DEBUG(AS.printSlice(dbgs(), I, "")); |
| 2359 | LLVM_DEBUG(dbgs() << "\n"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2360 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2361 | // Compute the intersecting offset range. |
| 2362 | assert(BeginOffset < NewAllocaEndOffset); |
| 2363 | assert(EndOffset > NewAllocaBeginOffset); |
| 2364 | NewBeginOffset = std::max(BeginOffset, NewAllocaBeginOffset); |
| 2365 | NewEndOffset = std::min(EndOffset, NewAllocaEndOffset); |
| 2366 | |
| 2367 | SliceSize = NewEndOffset - NewBeginOffset; |
| 2368 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2369 | OldUse = I->getUse(); |
| 2370 | OldPtr = cast<Instruction>(OldUse->get()); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2371 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2372 | Instruction *OldUserI = cast<Instruction>(OldUse->getUser()); |
| 2373 | IRB.SetInsertPoint(OldUserI); |
| 2374 | IRB.SetCurrentDebugLocation(OldUserI->getDebugLoc()); |
| 2375 | IRB.SetNamePrefix(Twine(NewAI.getName()) + "." + Twine(BeginOffset) + "."); |
| 2376 | |
| 2377 | CanSROA &= visit(cast<Instruction>(OldUse->getUser())); |
| 2378 | if (VecTy || IntTy) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2379 | assert(CanSROA); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2380 | return CanSROA; |
| 2381 | } |
| 2382 | |
| 2383 | private: |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2384 | // Make sure the other visit overloads are visible. |
| 2385 | using Base::visit; |
| 2386 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2387 | // Every instruction which can end up as a user must have a rewrite rule. |
| 2388 | bool visitInstruction(Instruction &I) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2389 | LLVM_DEBUG(dbgs() << " !!!! Cannot rewrite: " << I << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2390 | llvm_unreachable("No rewrite rule for this instruction!"); |
| 2391 | } |
| 2392 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2393 | Value *getNewAllocaSlicePtr(IRBuilderTy &IRB, Type *PointerTy) { |
| 2394 | // Note that the offset computation can use BeginOffset or NewBeginOffset |
| 2395 | // interchangeably for unsplit slices. |
| 2396 | assert(IsSplit || BeginOffset == NewBeginOffset); |
| 2397 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
| 2398 | |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2399 | #ifndef NDEBUG |
| 2400 | StringRef OldName = OldPtr->getName(); |
| 2401 | // Skip through the last '.sroa.' component of the name. |
| 2402 | size_t LastSROAPrefix = OldName.rfind(".sroa."); |
| 2403 | if (LastSROAPrefix != StringRef::npos) { |
| 2404 | OldName = OldName.substr(LastSROAPrefix + strlen(".sroa.")); |
| 2405 | // Look for an SROA slice index. |
| 2406 | size_t IndexEnd = OldName.find_first_not_of("0123456789"); |
| 2407 | if (IndexEnd != StringRef::npos && OldName[IndexEnd] == '.') { |
| 2408 | // Strip the index and look for the offset. |
| 2409 | OldName = OldName.substr(IndexEnd + 1); |
| 2410 | size_t OffsetEnd = OldName.find_first_not_of("0123456789"); |
| 2411 | if (OffsetEnd != StringRef::npos && OldName[OffsetEnd] == '.') |
| 2412 | // Strip the offset. |
| 2413 | OldName = OldName.substr(OffsetEnd + 1); |
| 2414 | } |
| 2415 | } |
| 2416 | // Strip any SROA suffixes as well. |
| 2417 | OldName = OldName.substr(0, OldName.find(".sroa_")); |
| 2418 | #endif |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2419 | |
| 2420 | return getAdjustedPtr(IRB, DL, &NewAI, |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 2421 | APInt(DL.getIndexTypeSizeInBits(PointerTy), Offset), |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2422 | PointerTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2423 | #ifndef NDEBUG |
| 2424 | Twine(OldName) + "." |
| 2425 | #else |
| 2426 | Twine() |
| 2427 | #endif |
| 2428 | ); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2431 | /// Compute suitable alignment to access this slice of the *new* |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2432 | /// alloca. |
Chandler Carruth | 2659e50 | 2014-02-26 05:02:19 +0000 | [diff] [blame] | 2433 | /// |
| 2434 | /// You can optionally pass a type to this routine and if that type's ABI |
| 2435 | /// alignment is itself suitable, this will return zero. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2436 | unsigned getSliceAlign(Type *Ty = nullptr) { |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2437 | unsigned NewAIAlign = NewAI.getAlignment(); |
| 2438 | if (!NewAIAlign) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2439 | NewAIAlign = DL.getABITypeAlignment(NewAI.getAllocatedType()); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2440 | unsigned Align = |
| 2441 | MinAlign(NewAIAlign, NewBeginOffset - NewAllocaBeginOffset); |
Chandler Carruth | 2659e50 | 2014-02-26 05:02:19 +0000 | [diff] [blame] | 2442 | return (Ty && Align == DL.getABITypeAlignment(Ty)) ? 0 : Align; |
Chandler Carruth | 4b2b38d | 2012-10-03 08:14:02 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2445 | unsigned getIndex(uint64_t Offset) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2446 | assert(VecTy && "Can only call getIndex when rewriting a vector"); |
| 2447 | uint64_t RelOffset = Offset - NewAllocaBeginOffset; |
| 2448 | assert(RelOffset / ElementSize < UINT32_MAX && "Index out of bounds"); |
| 2449 | uint32_t Index = RelOffset / ElementSize; |
| 2450 | assert(Index * ElementSize == RelOffset); |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2451 | return Index; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | void deleteIfTriviallyDead(Value *V) { |
| 2455 | Instruction *I = cast<Instruction>(V); |
| 2456 | if (isInstructionTriviallyDead(I)) |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2457 | Pass.DeadInsts.insert(I); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2460 | Value *rewriteVectorizedLoadInst() { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2461 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2462 | unsigned EndIndex = getIndex(NewEndOffset); |
Chandler Carruth | 769445e | 2012-12-17 12:50:21 +0000 | [diff] [blame] | 2463 | assert(EndIndex > BeginIndex && "Empty vector!"); |
Chandler Carruth | b6bc874 | 2012-12-17 13:07:30 +0000 | [diff] [blame] | 2464 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2465 | Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2466 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2467 | return extractVector(IRB, V, BeginIndex, EndIndex, "vec"); |
Chandler Carruth | 769445e | 2012-12-17 12:50:21 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2470 | Value *rewriteIntegerLoad(LoadInst &LI) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2471 | assert(IntTy && "We cannot insert an integer to the alloca"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2472 | assert(!LI.isVolatile()); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2473 | Value *V = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2474 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2475 | V = convertValue(DL, IRB, V, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2476 | assert(NewBeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); |
| 2477 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 4b682f6 | 2015-08-28 09:03:52 +0000 | [diff] [blame] | 2478 | if (Offset > 0 || NewEndOffset < NewAllocaEndOffset) { |
| 2479 | IntegerType *ExtractTy = Type::getIntNTy(LI.getContext(), SliceSize * 8); |
| 2480 | V = extractInteger(DL, IRB, V, ExtractTy, Offset, "extract"); |
| 2481 | } |
| 2482 | // It is possible that the extracted type is not the load type. This |
| 2483 | // happens if there is a load past the end of the alloca, and as |
| 2484 | // a consequence the slice is narrower but still a candidate for integer |
| 2485 | // lowering. To handle this case, we just zero extend the extracted |
| 2486 | // integer. |
| 2487 | assert(cast<IntegerType>(LI.getType())->getBitWidth() >= SliceSize * 8 && |
| 2488 | "Can only handle an extract for an overly wide load"); |
| 2489 | if (cast<IntegerType>(LI.getType())->getBitWidth() > SliceSize * 8) |
| 2490 | V = IRB.CreateZExt(V, LI.getType()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2491 | return V; |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2494 | bool visitLoadInst(LoadInst &LI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2495 | LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2496 | Value *OldOp = LI.getOperand(0); |
| 2497 | assert(OldOp == OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2498 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2499 | AAMDNodes AATags; |
| 2500 | LI.getAAMetadata(AATags); |
| 2501 | |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2502 | unsigned AS = LI.getPointerAddressSpace(); |
| 2503 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2504 | Type *TargetTy = IsSplit ? Type::getIntNTy(LI.getContext(), SliceSize * 8) |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2505 | : LI.getType(); |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2506 | const bool IsLoadPastEnd = DL.getTypeStoreSize(TargetTy) > SliceSize; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2507 | bool IsPtrAdjusted = false; |
| 2508 | Value *V; |
| 2509 | if (VecTy) { |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2510 | V = rewriteVectorizedLoadInst(); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2511 | } else if (IntTy && LI.getType()->isIntegerTy()) { |
Chandler Carruth | ea27cf0 | 2014-02-26 04:25:04 +0000 | [diff] [blame] | 2512 | V = rewriteIntegerLoad(LI); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2513 | } else if (NewBeginOffset == NewAllocaBeginOffset && |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2514 | NewEndOffset == NewAllocaEndOffset && |
| 2515 | (canConvertValue(DL, NewAllocaTy, TargetTy) || |
| 2516 | (IsLoadPastEnd && NewAllocaTy->isIntegerTy() && |
| 2517 | TargetTy->isIntegerTy()))) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2518 | LoadInst *NewLI = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2519 | NewAI.getAlignment(), |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2520 | LI.isVolatile(), LI.getName()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2521 | if (AATags) |
| 2522 | NewLI->setAAMetadata(AATags); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2523 | if (LI.isVolatile()) |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 2524 | NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
Luqman Aden | 3f807c9 | 2017-03-22 19:16:39 +0000 | [diff] [blame] | 2525 | |
Chandler Carruth | 3f81d80 | 2017-06-27 08:32:03 +0000 | [diff] [blame] | 2526 | // Any !nonnull metadata or !range metadata on the old load is also valid |
| 2527 | // on the new load. This is even true in some cases even when the loads |
| 2528 | // are different types, for example by mapping !nonnull metadata to |
| 2529 | // !range metadata by modeling the null pointer constant converted to the |
| 2530 | // integer type. |
Rafael Espindola | c06f55e | 2017-11-28 01:25:38 +0000 | [diff] [blame] | 2531 | // FIXME: Add support for range metadata here. Currently the utilities |
| 2532 | // for this don't propagate range metadata in trivial cases from one |
| 2533 | // integer load to another, don't handle non-addrspace-0 null pointers |
| 2534 | // correctly, and don't have any support for mapping ranges as the |
| 2535 | // integer type becomes winder or narrower. |
Chandler Carruth | 3f81d80 | 2017-06-27 08:32:03 +0000 | [diff] [blame] | 2536 | if (MDNode *N = LI.getMetadata(LLVMContext::MD_nonnull)) |
| 2537 | copyNonnullMetadata(LI, N, *NewLI); |
Rafael Espindola | c06f55e | 2017-11-28 01:25:38 +0000 | [diff] [blame] | 2538 | |
| 2539 | // Try to preserve nonnull metadata |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2540 | V = NewLI; |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2541 | |
| 2542 | // If this is an integer load past the end of the slice (which means the |
| 2543 | // bytes outside the slice are undef or this load is dead) just forcibly |
| 2544 | // fix the integer size with correct handling of endianness. |
| 2545 | if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy)) |
| 2546 | if (auto *TITy = dyn_cast<IntegerType>(TargetTy)) |
| 2547 | if (AITy->getBitWidth() < TITy->getBitWidth()) { |
| 2548 | V = IRB.CreateZExt(V, TITy, "load.ext"); |
| 2549 | if (DL.isBigEndian()) |
| 2550 | V = IRB.CreateShl(V, TITy->getBitWidth() - AITy->getBitWidth(), |
| 2551 | "endian_shift"); |
| 2552 | } |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2553 | } else { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2554 | Type *LTy = TargetTy->getPointerTo(AS); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2555 | LoadInst *NewLI = IRB.CreateAlignedLoad( |
| 2556 | TargetTy, getNewAllocaSlicePtr(IRB, LTy), getSliceAlign(TargetTy), |
| 2557 | LI.isVolatile(), LI.getName()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2558 | if (AATags) |
| 2559 | NewLI->setAAMetadata(AATags); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2560 | if (LI.isVolatile()) |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 2561 | NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2562 | |
| 2563 | V = NewLI; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2564 | IsPtrAdjusted = true; |
| 2565 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2566 | V = convertValue(DL, IRB, V, TargetTy); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2567 | |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2568 | if (IsSplit) { |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2569 | assert(!LI.isVolatile()); |
| 2570 | assert(LI.getType()->isIntegerTy() && |
| 2571 | "Only integer type loads and stores are split"); |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2572 | assert(SliceSize < DL.getTypeStoreSize(LI.getType()) && |
Chandler Carruth | a1c54bb | 2013-03-14 11:32:24 +0000 | [diff] [blame] | 2573 | "Split load isn't smaller than original load"); |
Bjorn Pettersson | b477142 | 2019-05-24 09:20:20 +0000 | [diff] [blame] | 2574 | assert(DL.typeSizeEqualsStoreSize(LI.getType()) && |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2575 | "Non-byte-multiple bit width"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2576 | // 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] | 2577 | IRB.SetInsertPoint(&*std::next(BasicBlock::iterator(&LI))); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2578 | // Create a placeholder value with the same type as LI to use as the |
| 2579 | // basis for the new value. This allows us to replace the uses of LI with |
| 2580 | // the computed value, and then replace the placeholder with LI, leaving |
| 2581 | // LI only used for this computation. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2582 | Value *Placeholder = new LoadInst( |
| 2583 | LI.getType(), UndefValue::get(LI.getType()->getPointerTo(AS))); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 2584 | V = insertInteger(DL, IRB, Placeholder, V, NewBeginOffset - BeginOffset, |
| 2585 | "insert"); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2586 | LI.replaceAllUsesWith(V); |
| 2587 | Placeholder->replaceAllUsesWith(&LI); |
Reid Kleckner | 96ab872 | 2017-05-18 17:24:10 +0000 | [diff] [blame] | 2588 | Placeholder->deleteValue(); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2589 | } else { |
| 2590 | LI.replaceAllUsesWith(V); |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2593 | Pass.DeadInsts.insert(&LI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2594 | deleteIfTriviallyDead(OldOp); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2595 | LLVM_DEBUG(dbgs() << " to: " << *V << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2596 | return !LI.isVolatile() && !IsPtrAdjusted; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2599 | bool rewriteVectorizedStoreInst(Value *V, StoreInst &SI, Value *OldOp, |
| 2600 | AAMDNodes AATags) { |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2601 | if (V->getType() != VecTy) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2602 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2603 | unsigned EndIndex = getIndex(NewEndOffset); |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2604 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 2605 | unsigned NumElements = EndIndex - BeginIndex; |
| 2606 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2607 | Type *SliceTy = (NumElements == 1) |
| 2608 | ? ElementTy |
| 2609 | : VectorType::get(ElementTy, NumElements); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 2610 | if (V->getType() != SliceTy) |
| 2611 | V = convertValue(DL, IRB, V, SliceTy); |
Chandler Carruth | 845b73c | 2012-11-21 08:16:30 +0000 | [diff] [blame] | 2612 | |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2613 | // Mix in the existing elements. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2614 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2615 | NewAI.getAlignment(), "load"); |
Bob Wilson | acfc01d | 2013-06-25 19:09:50 +0000 | [diff] [blame] | 2616 | V = insertVector(IRB, Old, V, BeginIndex, "vec"); |
| 2617 | } |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 2618 | StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2619 | if (AATags) |
| 2620 | Store->setAAMetadata(AATags); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2621 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2622 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2623 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2624 | return true; |
| 2625 | } |
| 2626 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2627 | bool rewriteIntegerStore(Value *V, StoreInst &SI, AAMDNodes AATags) { |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2628 | assert(IntTy && "We cannot extract an integer from the alloca"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2629 | assert(!SI.isVolatile()); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2630 | if (DL.getTypeSizeInBits(V->getType()) != IntTy->getBitWidth()) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2631 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2632 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2633 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2634 | assert(BeginOffset >= NewAllocaBeginOffset && "Out of bounds offset"); |
| 2635 | uint64_t Offset = BeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2636 | V = insertInteger(DL, IRB, Old, SI.getValueOperand(), Offset, "insert"); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2637 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2638 | V = convertValue(DL, IRB, V, NewAllocaTy); |
Chandler Carruth | 59ff93af | 2012-10-18 09:56:08 +0000 | [diff] [blame] | 2639 | StoreInst *Store = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment()); |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 2640 | Store->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access, |
| 2641 | LLVMContext::MD_access_group}); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2642 | if (AATags) |
| 2643 | Store->setAAMetadata(AATags); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2644 | Pass.DeadInsts.insert(&SI); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2645 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Chandler Carruth | 92924fd | 2012-09-24 00:34:20 +0000 | [diff] [blame] | 2646 | return true; |
| 2647 | } |
| 2648 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2649 | bool visitStoreInst(StoreInst &SI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2650 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2651 | Value *OldOp = SI.getOperand(1); |
| 2652 | assert(OldOp == OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2653 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2654 | AAMDNodes AATags; |
| 2655 | SI.getAAMetadata(AATags); |
| 2656 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2657 | Value *V = SI.getValueOperand(); |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2658 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 2659 | // Strip all inbounds GEPs and pointer casts to try to dig out any root |
| 2660 | // alloca that should be re-examined after promoting this alloca. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2661 | if (V->getType()->isPointerTy()) |
| 2662 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V->stripInBoundsOffsets())) |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 2663 | Pass.PostPromotionWorklist.insert(AI); |
| 2664 | |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2665 | if (SliceSize < DL.getTypeStoreSize(V->getType())) { |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2666 | assert(!SI.isVolatile()); |
| 2667 | assert(V->getType()->isIntegerTy() && |
| 2668 | "Only integer type loads and stores are split"); |
Bjorn Pettersson | b477142 | 2019-05-24 09:20:20 +0000 | [diff] [blame] | 2669 | assert(DL.typeSizeEqualsStoreSize(V->getType()) && |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2670 | "Non-byte-multiple bit width"); |
Chandler Carruth | c46b6eb | 2014-02-26 04:20:00 +0000 | [diff] [blame] | 2671 | IntegerType *NarrowTy = Type::getIntNTy(SI.getContext(), SliceSize * 8); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 2672 | V = extractInteger(DL, IRB, V, NarrowTy, NewBeginOffset - BeginOffset, |
| 2673 | "extract"); |
Chandler Carruth | 891fec0 | 2012-10-13 02:41:05 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2676 | if (VecTy) |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2677 | return rewriteVectorizedStoreInst(V, SI, OldOp, AATags); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2678 | if (IntTy && V->getType()->isIntegerTy()) |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2679 | return rewriteIntegerStore(V, SI, AATags); |
Chandler Carruth | 435c4e0 | 2012-10-15 08:40:30 +0000 | [diff] [blame] | 2680 | |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2681 | const bool IsStorePastEnd = DL.getTypeStoreSize(V->getType()) > SliceSize; |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2682 | StoreInst *NewSI; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2683 | if (NewBeginOffset == NewAllocaBeginOffset && |
| 2684 | NewEndOffset == NewAllocaEndOffset && |
Chandler Carruth | ccffdaf | 2015-07-22 03:32:42 +0000 | [diff] [blame] | 2685 | (canConvertValue(DL, V->getType(), NewAllocaTy) || |
| 2686 | (IsStorePastEnd && NewAllocaTy->isIntegerTy() && |
| 2687 | V->getType()->isIntegerTy()))) { |
| 2688 | // If this is an integer store past the end of slice (and thus the bytes |
| 2689 | // past that point are irrelevant or this is unreachable), truncate the |
| 2690 | // value prior to storing. |
| 2691 | if (auto *VITy = dyn_cast<IntegerType>(V->getType())) |
| 2692 | if (auto *AITy = dyn_cast<IntegerType>(NewAllocaTy)) |
| 2693 | if (VITy->getBitWidth() > AITy->getBitWidth()) { |
| 2694 | if (DL.isBigEndian()) |
| 2695 | V = IRB.CreateLShr(V, VITy->getBitWidth() - AITy->getBitWidth(), |
| 2696 | "endian_shift"); |
| 2697 | V = IRB.CreateTrunc(V, AITy, "load.trunc"); |
| 2698 | } |
| 2699 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2700 | V = convertValue(DL, IRB, V, NewAllocaTy); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2701 | NewSI = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), |
| 2702 | SI.isVolatile()); |
| 2703 | } else { |
Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 2704 | unsigned AS = SI.getPointerAddressSpace(); |
| 2705 | Value *NewPtr = getNewAllocaSlicePtr(IRB, V->getType()->getPointerTo(AS)); |
Chandler Carruth | 2659e50 | 2014-02-26 05:02:19 +0000 | [diff] [blame] | 2706 | NewSI = IRB.CreateAlignedStore(V, NewPtr, getSliceAlign(V->getType()), |
| 2707 | SI.isVolatile()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2708 | } |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 2709 | NewSI->copyMetadata(SI, {LLVMContext::MD_mem_parallel_loop_access, |
| 2710 | LLVMContext::MD_access_group}); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2711 | if (AATags) |
| 2712 | NewSI->setAAMetadata(AATags); |
David Majnemer | 62690b1 | 2015-07-14 06:19:58 +0000 | [diff] [blame] | 2713 | if (SI.isVolatile()) |
Konstantin Zhuravlyov | bb80d3e | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 2714 | NewSI->setAtomic(SI.getOrdering(), SI.getSyncScopeID()); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2715 | Pass.DeadInsts.insert(&SI); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2716 | deleteIfTriviallyDead(OldOp); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2717 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2718 | LLVM_DEBUG(dbgs() << " to: " << *NewSI << "\n"); |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2719 | return NewSI->getPointerOperand() == &NewAI && !SI.isVolatile(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2722 | /// Compute an integer value from splatting an i8 across the given |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2723 | /// number of bytes. |
| 2724 | /// |
| 2725 | /// Note that this routine assumes an i8 is a byte. If that isn't true, don't |
| 2726 | /// call this routine. |
Jakub Staszak | 086f6cd | 2013-02-19 22:02:21 +0000 | [diff] [blame] | 2727 | /// FIXME: Heed the advice above. |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2728 | /// |
| 2729 | /// \param V The i8 value to splat. |
| 2730 | /// \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] | 2731 | Value *getIntegerSplat(Value *V, unsigned Size) { |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2732 | assert(Size > 0 && "Expected a positive number of bytes."); |
| 2733 | IntegerType *VTy = cast<IntegerType>(V->getType()); |
| 2734 | assert(VTy->getBitWidth() == 8 && "Expected an i8 value for the byte"); |
| 2735 | if (Size == 1) |
| 2736 | return V; |
| 2737 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2738 | Type *SplatIntTy = Type::getIntNTy(VTy->getContext(), Size * 8); |
| 2739 | V = IRB.CreateMul( |
| 2740 | IRB.CreateZExt(V, SplatIntTy, "zext"), |
| 2741 | ConstantExpr::getUDiv( |
| 2742 | Constant::getAllOnesValue(SplatIntTy), |
| 2743 | ConstantExpr::getZExt(Constant::getAllOnesValue(V->getType()), |
| 2744 | SplatIntTy)), |
| 2745 | "isplat"); |
Chandler Carruth | 514f34f | 2012-12-17 04:07:30 +0000 | [diff] [blame] | 2746 | return V; |
| 2747 | } |
| 2748 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 2749 | /// Compute a vector splat for a given element value. |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2750 | Value *getVectorSplat(Value *V, unsigned NumElements) { |
| 2751 | V = IRB.CreateVectorSplat(NumElements, V, "vsplat"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2752 | LLVM_DEBUG(dbgs() << " splat: " << *V << "\n"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2753 | return V; |
| 2754 | } |
| 2755 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2756 | bool visitMemSetInst(MemSetInst &II) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2757 | LLVM_DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2758 | assert(II.getRawDest() == OldPtr); |
| 2759 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2760 | AAMDNodes AATags; |
| 2761 | II.getAAMetadata(AATags); |
| 2762 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2763 | // If the memset has a variable size, it cannot be split, just adjust the |
| 2764 | // pointer to the new alloca. |
| 2765 | if (!isa<Constant>(II.getLength())) { |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2766 | assert(!IsSplit); |
Chandler Carruth | 735d5be | 2014-02-26 04:45:24 +0000 | [diff] [blame] | 2767 | assert(NewBeginOffset == BeginOffset); |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2768 | II.setDest(getNewAllocaSlicePtr(IRB, OldPtr->getType())); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2769 | II.setDestAlignment(getSliceAlign()); |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2770 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2771 | deleteIfTriviallyDead(OldPtr); |
| 2772 | return false; |
| 2773 | } |
| 2774 | |
| 2775 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2776 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2777 | |
| 2778 | Type *AllocaTy = NewAI.getAllocatedType(); |
| 2779 | Type *ScalarTy = AllocaTy->getScalarType(); |
Philip Reames | 9b6b4fa | 2019-03-12 20:15:05 +0000 | [diff] [blame] | 2780 | |
| 2781 | const bool CanContinue = [&]() { |
| 2782 | if (VecTy || IntTy) |
| 2783 | return true; |
| 2784 | if (BeginOffset > NewAllocaBeginOffset || |
| 2785 | EndOffset < NewAllocaEndOffset) |
| 2786 | return false; |
| 2787 | auto *C = cast<ConstantInt>(II.getLength()); |
| 2788 | if (C->getBitWidth() > 64) |
| 2789 | return false; |
| 2790 | const auto Len = C->getZExtValue(); |
| 2791 | auto *Int8Ty = IntegerType::getInt8Ty(NewAI.getContext()); |
| 2792 | auto *SrcTy = VectorType::get(Int8Ty, Len); |
| 2793 | return canConvertValue(DL, SrcTy, AllocaTy) && |
| 2794 | DL.isLegalInteger(DL.getTypeSizeInBits(ScalarTy)); |
| 2795 | }(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2796 | |
| 2797 | // If this doesn't map cleanly onto the alloca type, and that type isn't |
| 2798 | // a single value type, just emit a memset. |
Philip Reames | 9b6b4fa | 2019-03-12 20:15:05 +0000 | [diff] [blame] | 2799 | if (!CanContinue) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2800 | Type *SizeTy = II.getLength()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2801 | Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset); |
| 2802 | CallInst *New = IRB.CreateMemSet( |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2803 | getNewAllocaSlicePtr(IRB, OldPtr->getType()), II.getValue(), Size, |
| 2804 | getSliceAlign(), II.isVolatile()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2805 | if (AATags) |
| 2806 | New->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2807 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2808 | return false; |
| 2809 | } |
| 2810 | |
| 2811 | // If we can represent this as a simple value, we have to build the actual |
| 2812 | // value to store, which requires expanding the byte present in memset to |
| 2813 | // a sensible representation for the alloca type. This is essentially |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2814 | // splatting the byte to a sufficiently wide integer, splatting it across |
| 2815 | // any desired vector width, and bitcasting to the final type. |
Benjamin Kramer | c003a45 | 2013-01-01 16:13:35 +0000 | [diff] [blame] | 2816 | Value *V; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2817 | |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2818 | if (VecTy) { |
| 2819 | // If this is a memset of a vectorized alloca, insert it. |
| 2820 | assert(ElementTy == ScalarTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2821 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2822 | unsigned BeginIndex = getIndex(NewBeginOffset); |
| 2823 | unsigned EndIndex = getIndex(NewEndOffset); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2824 | assert(EndIndex > BeginIndex && "Empty vector!"); |
| 2825 | unsigned NumElements = EndIndex - BeginIndex; |
| 2826 | assert(NumElements <= VecTy->getNumElements() && "Too many elements!"); |
| 2827 | |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2828 | Value *Splat = |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2829 | getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ElementTy) / 8); |
| 2830 | Splat = convertValue(DL, IRB, Splat, ElementTy); |
Chandler Carruth | cacda25 | 2012-12-17 14:03:01 +0000 | [diff] [blame] | 2831 | if (NumElements > 1) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2832 | Splat = getVectorSplat(Splat, NumElements); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2833 | |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2834 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2835 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2836 | V = insertVector(IRB, Old, Splat, BeginIndex, "vec"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2837 | } else if (IntTy) { |
| 2838 | // If this is a memset on an alloca where we can widen stores, insert the |
| 2839 | // set integer. |
Chandler Carruth | 9d966a2 | 2012-10-15 10:24:40 +0000 | [diff] [blame] | 2840 | assert(!II.isVolatile()); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2841 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2842 | uint64_t Size = NewEndOffset - NewBeginOffset; |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2843 | V = getIntegerSplat(II.getValue(), Size); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2844 | |
| 2845 | if (IntTy && (BeginOffset != NewAllocaBeginOffset || |
| 2846 | EndOffset != NewAllocaBeginOffset)) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 2847 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 2848 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2849 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2850 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2851 | V = insertInteger(DL, IRB, Old, V, Offset, "insert"); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2852 | } else { |
| 2853 | assert(V->getType() == IntTy && |
| 2854 | "Wrong type for an alloca wide integer!"); |
| 2855 | } |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2856 | V = convertValue(DL, IRB, V, AllocaTy); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2857 | } else { |
| 2858 | // Established these invariants above. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2859 | assert(NewBeginOffset == NewAllocaBeginOffset); |
| 2860 | assert(NewEndOffset == NewAllocaEndOffset); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2861 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2862 | V = getIntegerSplat(II.getValue(), DL.getTypeSizeInBits(ScalarTy) / 8); |
Chandler Carruth | ccca504 | 2012-12-17 04:07:37 +0000 | [diff] [blame] | 2863 | if (VectorType *AllocaVecTy = dyn_cast<VectorType>(AllocaTy)) |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 2864 | V = getVectorSplat(V, AllocaVecTy->getNumElements()); |
Chandler Carruth | 95e1fb8 | 2012-12-17 13:51:03 +0000 | [diff] [blame] | 2865 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 2866 | V = convertValue(DL, IRB, V, AllocaTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2869 | StoreInst *New = IRB.CreateAlignedStore(V, &NewAI, NewAI.getAlignment(), |
| 2870 | II.isVolatile()); |
| 2871 | if (AATags) |
| 2872 | New->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2873 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2874 | return !II.isVolatile(); |
| 2875 | } |
| 2876 | |
| 2877 | bool visitMemTransferInst(MemTransferInst &II) { |
| 2878 | // Rewriting of memory transfer instructions can be a bit tricky. We break |
| 2879 | // them into two categories: split intrinsics and unsplit intrinsics. |
| 2880 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2881 | LLVM_DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2882 | |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2883 | AAMDNodes AATags; |
| 2884 | II.getAAMetadata(AATags); |
| 2885 | |
Chandler Carruth | bb2a932 | 2014-02-25 03:50:14 +0000 | [diff] [blame] | 2886 | bool IsDest = &II.getRawDestUse() == OldUse; |
Alexey Samsonov | 26af6f7 | 2014-02-25 07:56:00 +0000 | [diff] [blame] | 2887 | assert((IsDest && II.getRawDest() == OldPtr) || |
Chandler Carruth | bb2a932 | 2014-02-25 03:50:14 +0000 | [diff] [blame] | 2888 | (!IsDest && II.getRawSource() == OldPtr)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2889 | |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 2890 | unsigned SliceAlign = getSliceAlign(); |
Chandler Carruth | 176ca71 | 2012-10-01 12:16:54 +0000 | [diff] [blame] | 2891 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2892 | // For unsplit intrinsics, we simply modify the source and destination |
| 2893 | // pointers in place. This isn't just an optimization, it is a matter of |
| 2894 | // correctness. With unsplit intrinsics we may be dealing with transfers |
| 2895 | // within a single alloca before SROA ran, or with transfers that have |
| 2896 | // a variable length. We may also be dealing with memmove instead of |
| 2897 | // memcpy, and so simply updating the pointers is the necessary for us to |
| 2898 | // update both source and dest of a single call. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2899 | if (!IsSplittable) { |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2900 | Value *AdjustedPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType()); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2901 | if (IsDest) { |
Chandler Carruth | 8183a50 | 2014-02-25 11:08:02 +0000 | [diff] [blame] | 2902 | II.setDest(AdjustedPtr); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2903 | II.setDestAlignment(SliceAlign); |
| 2904 | } |
| 2905 | else { |
Chandler Carruth | 8183a50 | 2014-02-25 11:08:02 +0000 | [diff] [blame] | 2906 | II.setSource(AdjustedPtr); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2907 | II.setSourceAlignment(SliceAlign); |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2908 | } |
Chandler Carruth | 208124f | 2012-09-26 10:59:22 +0000 | [diff] [blame] | 2909 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2910 | LLVM_DEBUG(dbgs() << " to: " << II << "\n"); |
Chandler Carruth | 8183a50 | 2014-02-25 11:08:02 +0000 | [diff] [blame] | 2911 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2912 | return false; |
| 2913 | } |
| 2914 | // For split transfer intrinsics we have an incredibly useful assurance: |
| 2915 | // the source and destination do not reside within the same alloca, and at |
| 2916 | // least one of them does not escape. This means that we can replace |
| 2917 | // memmove with memcpy, and we don't need to worry about all manner of |
| 2918 | // downsides to splitting and transforming the operations. |
| 2919 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2920 | // If this doesn't map cleanly onto the alloca type, and that type isn't |
| 2921 | // a single value type, just emit a memcpy. |
Reid Kleckner | c36f48f | 2014-08-22 00:09:56 +0000 | [diff] [blame] | 2922 | bool EmitMemCpy = |
| 2923 | !VecTy && !IntTy && |
| 2924 | (BeginOffset > NewAllocaBeginOffset || EndOffset < NewAllocaEndOffset || |
| 2925 | SliceSize != DL.getTypeStoreSize(NewAI.getAllocatedType()) || |
| 2926 | !NewAI.getAllocatedType()->isSingleValueType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2927 | |
| 2928 | // If we're just going to emit a memcpy, the alloca hasn't changed, and the |
| 2929 | // size hasn't been shrunk based on analysis of the viable range, this is |
| 2930 | // a no-op. |
| 2931 | if (EmitMemCpy && &OldAI == &NewAI) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2932 | // Ensure the start lines up. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2933 | assert(NewBeginOffset == BeginOffset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2934 | |
| 2935 | // Rewrite the size as needed. |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2936 | if (NewEndOffset != EndOffset) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2937 | II.setLength(ConstantInt::get(II.getLength()->getType(), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2938 | NewEndOffset - NewBeginOffset)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2939 | return false; |
| 2940 | } |
| 2941 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 2942 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2943 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2944 | // Strip all inbounds GEPs and pointer casts to try to dig out any root |
| 2945 | // alloca that should be re-examined after rewriting this instruction. |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2946 | Value *OtherPtr = IsDest ? II.getRawSource() : II.getRawDest(); |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 2947 | if (AllocaInst *AI = |
| 2948 | dyn_cast<AllocaInst>(OtherPtr->stripInBoundsOffsets())) { |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 2949 | assert(AI != &OldAI && AI != &NewAI && |
| 2950 | "Splittable transfers cannot reach the same alloca on both ends."); |
Chandler Carruth | 4bd8f66 | 2012-09-26 07:41:40 +0000 | [diff] [blame] | 2951 | Pass.Worklist.insert(AI); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 2952 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2953 | |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 2954 | Type *OtherPtrTy = OtherPtr->getType(); |
| 2955 | unsigned OtherAS = OtherPtrTy->getPointerAddressSpace(); |
| 2956 | |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2957 | // Compute the relative offset for the other pointer within the transfer. |
Nicola Zaghen | f96383c | 2018-10-30 11:15:04 +0000 | [diff] [blame] | 2958 | unsigned OffsetWidth = DL.getIndexSizeInBits(OtherAS); |
| 2959 | APInt OtherOffset(OffsetWidth, NewBeginOffset - BeginOffset); |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2960 | unsigned OtherAlign = |
| 2961 | IsDest ? II.getSourceAlignment() : II.getDestAlignment(); |
| 2962 | OtherAlign = MinAlign(OtherAlign ? OtherAlign : 1, |
| 2963 | OtherOffset.zextOrTrunc(64).getZExtValue()); |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2964 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2965 | if (EmitMemCpy) { |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2966 | // Compute the other pointer, folding as much as possible to produce |
| 2967 | // a single, simple GEP in most cases. |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 2968 | OtherPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 2969 | OtherPtr->getName() + "."); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 2970 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 2971 | Value *OurPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2972 | Type *SizeTy = II.getLength()->getType(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2973 | Constant *Size = ConstantInt::get(SizeTy, NewEndOffset - NewBeginOffset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2974 | |
Daniel Neilson | 41e781d | 2018-03-13 14:25:33 +0000 | [diff] [blame] | 2975 | Value *DestPtr, *SrcPtr; |
| 2976 | unsigned DestAlign, SrcAlign; |
| 2977 | // Note: IsDest is true iff we're copying into the new alloca slice |
| 2978 | if (IsDest) { |
| 2979 | DestPtr = OurPtr; |
| 2980 | DestAlign = SliceAlign; |
| 2981 | SrcPtr = OtherPtr; |
| 2982 | SrcAlign = OtherAlign; |
| 2983 | } else { |
| 2984 | DestPtr = OtherPtr; |
| 2985 | DestAlign = OtherAlign; |
| 2986 | SrcPtr = OurPtr; |
| 2987 | SrcAlign = SliceAlign; |
| 2988 | } |
| 2989 | CallInst *New = IRB.CreateMemCpy(DestPtr, DestAlign, SrcPtr, SrcAlign, |
| 2990 | Size, II.isVolatile()); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 2991 | if (AATags) |
| 2992 | New->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2993 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 2994 | return false; |
| 2995 | } |
| 2996 | |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 2997 | bool IsWholeAlloca = NewBeginOffset == NewAllocaBeginOffset && |
| 2998 | NewEndOffset == NewAllocaEndOffset; |
| 2999 | uint64_t Size = NewEndOffset - NewBeginOffset; |
| 3000 | unsigned BeginIndex = VecTy ? getIndex(NewBeginOffset) : 0; |
| 3001 | unsigned EndIndex = VecTy ? getIndex(NewEndOffset) : 0; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3002 | unsigned NumElements = EndIndex - BeginIndex; |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3003 | IntegerType *SubIntTy = |
| 3004 | IntTy ? Type::getIntNTy(IntTy->getContext(), Size * 8) : nullptr; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3005 | |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 3006 | // Reset the other pointer type to match the register type we're going to |
| 3007 | // use, but using the address space of the original other pointer. |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3008 | Type *OtherTy; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3009 | if (VecTy && !IsWholeAlloca) { |
| 3010 | if (NumElements == 1) |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3011 | OtherTy = VecTy->getElementType(); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3012 | else |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3013 | OtherTy = VectorType::get(VecTy->getElementType(), NumElements); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3014 | } else if (IntTy && !IsWholeAlloca) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3015 | OtherTy = SubIntTy; |
Chandler Carruth | 286d87e | 2014-02-26 08:25:02 +0000 | [diff] [blame] | 3016 | } else { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3017 | OtherTy = NewAllocaTy; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3018 | } |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3019 | OtherPtrTy = OtherTy->getPointerTo(OtherAS); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3020 | |
Chandler Carruth | 181ed05 | 2014-02-26 05:33:36 +0000 | [diff] [blame] | 3021 | Value *SrcPtr = getAdjustedPtr(IRB, DL, OtherPtr, OtherOffset, OtherPtrTy, |
Chandler Carruth | cb93cd2 | 2014-02-25 11:19:56 +0000 | [diff] [blame] | 3022 | OtherPtr->getName() + "."); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 3023 | unsigned SrcAlign = OtherAlign; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3024 | Value *DstPtr = &NewAI; |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 3025 | unsigned DstAlign = SliceAlign; |
| 3026 | if (!IsDest) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3027 | std::swap(SrcPtr, DstPtr); |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 3028 | std::swap(SrcAlign, DstAlign); |
| 3029 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3030 | |
| 3031 | Value *Src; |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3032 | if (VecTy && !IsWholeAlloca && !IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3033 | Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 3034 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 3035 | Src = extractVector(IRB, Src, BeginIndex, EndIndex, "vec"); |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 3036 | } else if (IntTy && !IsWholeAlloca && !IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3037 | Src = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 3038 | NewAI.getAlignment(), "load"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3039 | Src = convertValue(DL, IRB, Src, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3040 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3041 | Src = extractInteger(DL, IRB, Src, SubIntTy, Offset, "extract"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3042 | } else { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3043 | LoadInst *Load = IRB.CreateAlignedLoad(OtherTy, SrcPtr, SrcAlign, |
| 3044 | II.isVolatile(), "copyload"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3045 | if (AATags) |
| 3046 | Load->setAAMetadata(AATags); |
| 3047 | Src = Load; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3050 | if (VecTy && !IsWholeAlloca && IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3051 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 3052 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 34f0c7f | 2013-03-21 09:52:18 +0000 | [diff] [blame] | 3053 | Src = insertVector(IRB, Old, Src, BeginIndex, "vec"); |
Chandler Carruth | 21eb4e9 | 2012-12-17 14:51:24 +0000 | [diff] [blame] | 3054 | } else if (IntTy && !IsWholeAlloca && IsDest) { |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3055 | Value *Old = IRB.CreateAlignedLoad(NewAI.getAllocatedType(), &NewAI, |
| 3056 | NewAI.getAlignment(), "oldload"); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3057 | Old = convertValue(DL, IRB, Old, IntTy); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3058 | uint64_t Offset = NewBeginOffset - NewAllocaBeginOffset; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3059 | Src = insertInteger(DL, IRB, Old, Src, Offset, "insert"); |
| 3060 | Src = convertValue(DL, IRB, Src, NewAllocaTy); |
Chandler Carruth | 49c8eea | 2012-10-15 10:24:43 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Chandler Carruth | 871ba72 | 2012-09-26 10:27:46 +0000 | [diff] [blame] | 3063 | StoreInst *Store = cast<StoreInst>( |
Chandler Carruth | aa72b93 | 2014-02-26 07:29:54 +0000 | [diff] [blame] | 3064 | IRB.CreateAlignedStore(Src, DstPtr, DstAlign, II.isVolatile())); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3065 | if (AATags) |
| 3066 | Store->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3067 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3068 | return !II.isVolatile(); |
| 3069 | } |
| 3070 | |
| 3071 | bool visitIntrinsicInst(IntrinsicInst &II) { |
Vedant Kumar | b264d69 | 2018-12-21 21:49:40 +0000 | [diff] [blame] | 3072 | assert(II.isLifetimeStartOrEnd()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3073 | LLVM_DEBUG(dbgs() << " original: " << II << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3074 | assert(II.getArgOperand(1) == OldPtr); |
| 3075 | |
| 3076 | // Record this instruction for deletion. |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 3077 | Pass.DeadInsts.insert(&II); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3078 | |
Eli Friedman | 5096775 | 2016-11-28 21:50:34 +0000 | [diff] [blame] | 3079 | // Lifetime intrinsics are only promotable if they cover the whole alloca. |
| 3080 | // Therefore, we drop lifetime intrinsics which don't cover the whole |
| 3081 | // alloca. |
| 3082 | // (In theory, intrinsics which partially cover an alloca could be |
| 3083 | // promoted, but PromoteMemToReg doesn't handle that case.) |
| 3084 | // FIXME: Check whether the alloca is promotable before dropping the |
| 3085 | // lifetime intrinsics? |
David L. Jones | 6bfdebb | 2019-10-15 04:32:07 +0000 | [diff] [blame] | 3086 | if (NewBeginOffset != NewAllocaBeginOffset || |
| 3087 | NewEndOffset != NewAllocaEndOffset) |
Eli Friedman | 5096775 | 2016-11-28 21:50:34 +0000 | [diff] [blame] | 3088 | return true; |
| 3089 | |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3090 | ConstantInt *Size = |
| 3091 | ConstantInt::get(cast<IntegerType>(II.getArgOperand(0)->getType()), |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3092 | NewEndOffset - NewBeginOffset); |
Gabor Buella | 3ec170c | 2019-01-16 12:06:17 +0000 | [diff] [blame] | 3093 | // Lifetime intrinsics always expect an i8* so directly get such a pointer |
| 3094 | // for the new alloca slice. |
| 3095 | Type *PointerTy = IRB.getInt8PtrTy(OldPtr->getType()->getPointerAddressSpace()); |
| 3096 | Value *Ptr = getNewAllocaSlicePtr(IRB, PointerTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3097 | Value *New; |
| 3098 | if (II.getIntrinsicID() == Intrinsic::lifetime_start) |
| 3099 | New = IRB.CreateLifetimeStart(Ptr, Size); |
| 3100 | else |
| 3101 | New = IRB.CreateLifetimeEnd(Ptr, Size); |
| 3102 | |
Edwin Vane | 82f80d4 | 2013-01-29 17:42:24 +0000 | [diff] [blame] | 3103 | (void)New; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3104 | LLVM_DEBUG(dbgs() << " to: " << *New << "\n"); |
Eli Friedman | 2a65dd1 | 2016-08-08 01:30:53 +0000 | [diff] [blame] | 3105 | |
Eli Friedman | 5096775 | 2016-11-28 21:50:34 +0000 | [diff] [blame] | 3106 | return true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3109 | void fixLoadStoreAlign(Instruction &Root) { |
| 3110 | // This algorithm implements the same visitor loop as |
| 3111 | // hasUnsafePHIOrSelectUse, and fixes the alignment of each load |
| 3112 | // or store found. |
| 3113 | SmallPtrSet<Instruction *, 4> Visited; |
| 3114 | SmallVector<Instruction *, 4> Uses; |
| 3115 | Visited.insert(&Root); |
| 3116 | Uses.push_back(&Root); |
| 3117 | do { |
| 3118 | Instruction *I = Uses.pop_back_val(); |
| 3119 | |
| 3120 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 3121 | unsigned LoadAlign = LI->getAlignment(); |
| 3122 | if (!LoadAlign) |
| 3123 | LoadAlign = DL.getABITypeAlignment(LI->getType()); |
Guillaume Chatelet | 1738022 | 2019-09-30 09:37:05 +0000 | [diff] [blame] | 3124 | LI->setAlignment(MaybeAlign(std::min(LoadAlign, getSliceAlign()))); |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3125 | continue; |
| 3126 | } |
| 3127 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 3128 | unsigned StoreAlign = SI->getAlignment(); |
| 3129 | if (!StoreAlign) { |
| 3130 | Value *Op = SI->getOperand(0); |
| 3131 | StoreAlign = DL.getABITypeAlignment(Op->getType()); |
| 3132 | } |
Guillaume Chatelet | d400d45 | 2019-10-03 13:17:21 +0000 | [diff] [blame] | 3133 | SI->setAlignment(MaybeAlign(std::min(StoreAlign, getSliceAlign()))); |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3134 | continue; |
| 3135 | } |
| 3136 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 3137 | assert(isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I) || |
| 3138 | isa<PHINode>(I) || isa<SelectInst>(I) || |
| 3139 | isa<GetElementPtrInst>(I)); |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3140 | for (User *U : I->users()) |
| 3141 | if (Visited.insert(cast<Instruction>(U)).second) |
| 3142 | Uses.push_back(cast<Instruction>(U)); |
| 3143 | } while (!Uses.empty()); |
| 3144 | } |
| 3145 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3146 | bool visitPHINode(PHINode &PN) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3147 | LLVM_DEBUG(dbgs() << " original: " << PN << "\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3148 | assert(BeginOffset >= NewAllocaBeginOffset && "PHIs are unsplittable"); |
| 3149 | assert(EndOffset <= NewAllocaEndOffset && "PHIs are unsplittable"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3150 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3151 | // We would like to compute a new pointer in only one place, but have it be |
| 3152 | // as local as possible to the PHI. To do that, we re-use the location of |
| 3153 | // the old pointer, which necessarily must be in the right position to |
| 3154 | // dominate the PHI. |
Chandler Carruth | 5117553 | 2014-02-25 11:12:04 +0000 | [diff] [blame] | 3155 | IRBuilderTy PtrBuilder(IRB); |
David Majnemer | d4cffcf | 2014-09-01 21:20:14 +0000 | [diff] [blame] | 3156 | if (isa<PHINode>(OldPtr)) |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3157 | PtrBuilder.SetInsertPoint(&*OldPtr->getParent()->getFirstInsertionPt()); |
David Majnemer | d4cffcf | 2014-09-01 21:20:14 +0000 | [diff] [blame] | 3158 | else |
| 3159 | PtrBuilder.SetInsertPoint(OldPtr); |
Chandler Carruth | 5117553 | 2014-02-25 11:12:04 +0000 | [diff] [blame] | 3160 | PtrBuilder.SetCurrentDebugLocation(OldPtr->getDebugLoc()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3161 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 3162 | Value *NewPtr = getNewAllocaSlicePtr(PtrBuilder, OldPtr->getType()); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3163 | // Replace the operands which were using the old pointer. |
Benjamin Kramer | 7ddd705 | 2012-10-20 12:04:57 +0000 | [diff] [blame] | 3164 | std::replace(PN.op_begin(), PN.op_end(), cast<Value>(OldPtr), NewPtr); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3165 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3166 | LLVM_DEBUG(dbgs() << " to: " << PN << "\n"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3167 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3168 | |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3169 | // Fix the alignment of any loads or stores using this PHI node. |
| 3170 | fixLoadStoreAlign(PN); |
| 3171 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 3172 | // PHIs can't be promoted on their own, but often can be speculated. We |
| 3173 | // check the speculation outside of the rewriter so that we see the |
| 3174 | // fully-rewritten alloca. |
| 3175 | PHIUsers.insert(&PN); |
| 3176 | return true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | bool visitSelectInst(SelectInst &SI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3180 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Benjamin Kramer | 0212dc2 | 2013-04-21 17:48:39 +0000 | [diff] [blame] | 3181 | assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) && |
| 3182 | "Pointer isn't an operand!"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3183 | assert(BeginOffset >= NewAllocaBeginOffset && "Selects are unsplittable"); |
| 3184 | assert(EndOffset <= NewAllocaEndOffset && "Selects are unsplittable"); |
Chandler Carruth | 82a5754 | 2012-10-01 10:54:05 +0000 | [diff] [blame] | 3185 | |
Chandler Carruth | 47954c8 | 2014-02-26 05:12:43 +0000 | [diff] [blame] | 3186 | Value *NewPtr = getNewAllocaSlicePtr(IRB, OldPtr->getType()); |
Benjamin Kramer | 0212dc2 | 2013-04-21 17:48:39 +0000 | [diff] [blame] | 3187 | // Replace the operands which were using the old pointer. |
| 3188 | if (SI.getOperand(1) == OldPtr) |
| 3189 | SI.setOperand(1, NewPtr); |
| 3190 | if (SI.getOperand(2) == OldPtr) |
| 3191 | SI.setOperand(2, NewPtr); |
| 3192 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3193 | LLVM_DEBUG(dbgs() << " to: " << SI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3194 | deleteIfTriviallyDead(OldPtr); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 3195 | |
Eli Friedman | 94d3e4d | 2018-08-30 18:59:24 +0000 | [diff] [blame] | 3196 | // Fix the alignment of any loads or stores using this select. |
| 3197 | fixLoadStoreAlign(SI); |
| 3198 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 3199 | // Selects can't be promoted on their own, but often can be speculated. We |
| 3200 | // check the speculation outside of the rewriter so that we see the |
| 3201 | // fully-rewritten alloca. |
| 3202 | SelectUsers.insert(&SI); |
| 3203 | return true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3204 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3205 | }; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3206 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3207 | namespace { |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3208 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3209 | /// Visitor to rewrite aggregate loads and stores as scalar. |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3210 | /// |
| 3211 | /// This pass aggressively rewrites all aggregate loads and stores on |
| 3212 | /// a particular pointer (or any pointer derived from it which we can identify) |
| 3213 | /// with scalar loads and stores. |
| 3214 | class AggLoadStoreRewriter : public InstVisitor<AggLoadStoreRewriter, bool> { |
| 3215 | // Befriend the base class so it can delegate to private visit methods. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3216 | friend class InstVisitor<AggLoadStoreRewriter, bool>; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3217 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3218 | /// Queue of pointer uses to analyze and potentially rewrite. |
| 3219 | SmallVector<Use *, 8> Queue; |
| 3220 | |
| 3221 | /// Set to prevent us from cycling with phi nodes and loops. |
| 3222 | SmallPtrSet<User *, 8> Visited; |
| 3223 | |
| 3224 | /// The current pointer use being rewritten. This is used to dig up the used |
| 3225 | /// value (as opposed to the user). |
| 3226 | Use *U; |
| 3227 | |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3228 | /// Used to calculate offsets, and hence alignment, of subobjects. |
| 3229 | const DataLayout &DL; |
| 3230 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3231 | public: |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3232 | AggLoadStoreRewriter(const DataLayout &DL) : DL(DL) {} |
| 3233 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3234 | /// Rewrite loads and stores through a pointer and all pointers derived from |
| 3235 | /// it. |
| 3236 | bool rewrite(Instruction &I) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3237 | LLVM_DEBUG(dbgs() << " Rewriting FCA loads and stores...\n"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3238 | enqueueUsers(I); |
| 3239 | bool Changed = false; |
| 3240 | while (!Queue.empty()) { |
| 3241 | U = Queue.pop_back_val(); |
| 3242 | Changed |= visit(cast<Instruction>(U->getUser())); |
| 3243 | } |
| 3244 | return Changed; |
| 3245 | } |
| 3246 | |
| 3247 | private: |
| 3248 | /// Enqueue all the users of the given instruction for further processing. |
| 3249 | /// This uses a set to de-duplicate users. |
| 3250 | void enqueueUsers(Instruction &I) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3251 | for (Use &U : I.uses()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 3252 | if (Visited.insert(U.getUser()).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 3253 | Queue.push_back(&U); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | // Conservative default is to not rewrite anything. |
| 3257 | bool visitInstruction(Instruction &I) { return false; } |
| 3258 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3259 | /// Generic recursive split emission class. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3260 | template <typename Derived> class OpSplitter { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3261 | protected: |
| 3262 | /// The builder used to form new instructions. |
Chandler Carruth | d177f86 | 2013-03-20 07:30:36 +0000 | [diff] [blame] | 3263 | IRBuilderTy IRB; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3264 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3265 | /// The indices which to be used with insert- or extractvalue to select the |
| 3266 | /// appropriate value within the aggregate. |
| 3267 | SmallVector<unsigned, 4> Indices; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3268 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3269 | /// The indices to a GEP instruction which will move Ptr to the correct slot |
| 3270 | /// within the aggregate. |
| 3271 | SmallVector<Value *, 4> GEPIndices; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3272 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3273 | /// The base pointer of the original op, used as a base for GEPing the |
| 3274 | /// split operations. |
| 3275 | Value *Ptr; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3276 | |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3277 | /// The base pointee type being GEPed into. |
| 3278 | Type *BaseTy; |
| 3279 | |
| 3280 | /// Known alignment of the base pointer. |
| 3281 | unsigned BaseAlign; |
| 3282 | |
| 3283 | /// To calculate offset of each component so we can correctly deduce |
| 3284 | /// alignments. |
| 3285 | const DataLayout &DL; |
| 3286 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3287 | /// Initialize the splitter with an insertion point, Ptr and start with a |
| 3288 | /// single zero GEP index. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3289 | OpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy, |
| 3290 | unsigned BaseAlign, const DataLayout &DL) |
| 3291 | : IRB(InsertionPoint), GEPIndices(1, IRB.getInt32(0)), Ptr(Ptr), |
| 3292 | BaseTy(BaseTy), BaseAlign(BaseAlign), DL(DL) {} |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3293 | |
| 3294 | public: |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3295 | /// Generic recursive split emission routine. |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3296 | /// |
| 3297 | /// This method recursively splits an aggregate op (load or store) into |
| 3298 | /// scalar or vector ops. It splits recursively until it hits a single value |
| 3299 | /// and emits that single value operation via the template argument. |
| 3300 | /// |
| 3301 | /// The logic of this routine relies on GEPs and insertvalue and |
| 3302 | /// extractvalue all operating with the same fundamental index list, merely |
| 3303 | /// formatted differently (GEPs need actual values). |
| 3304 | /// |
| 3305 | /// \param Ty The type being split recursively into smaller ops. |
| 3306 | /// \param Agg The aggregate value being built up or stored, depending on |
| 3307 | /// whether this is splitting a load or a store respectively. |
| 3308 | void emitSplitOps(Type *Ty, Value *&Agg, const Twine &Name) { |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3309 | if (Ty->isSingleValueType()) { |
| 3310 | unsigned Offset = DL.getIndexedOffsetInType(BaseTy, GEPIndices); |
| 3311 | return static_cast<Derived *>(this)->emitFunc( |
| 3312 | Ty, Agg, MinAlign(BaseAlign, Offset), Name); |
| 3313 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3314 | |
| 3315 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 3316 | unsigned OldSize = Indices.size(); |
| 3317 | (void)OldSize; |
| 3318 | for (unsigned Idx = 0, Size = ATy->getNumElements(); Idx != Size; |
| 3319 | ++Idx) { |
| 3320 | assert(Indices.size() == OldSize && "Did not return to the old size"); |
| 3321 | Indices.push_back(Idx); |
| 3322 | GEPIndices.push_back(IRB.getInt32(Idx)); |
| 3323 | emitSplitOps(ATy->getElementType(), Agg, Name + "." + Twine(Idx)); |
| 3324 | GEPIndices.pop_back(); |
| 3325 | Indices.pop_back(); |
| 3326 | } |
| 3327 | return; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3328 | } |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3329 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3330 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 3331 | unsigned OldSize = Indices.size(); |
| 3332 | (void)OldSize; |
| 3333 | for (unsigned Idx = 0, Size = STy->getNumElements(); Idx != Size; |
| 3334 | ++Idx) { |
| 3335 | assert(Indices.size() == OldSize && "Did not return to the old size"); |
| 3336 | Indices.push_back(Idx); |
| 3337 | GEPIndices.push_back(IRB.getInt32(Idx)); |
| 3338 | emitSplitOps(STy->getElementType(Idx), Agg, Name + "." + Twine(Idx)); |
| 3339 | GEPIndices.pop_back(); |
| 3340 | Indices.pop_back(); |
| 3341 | } |
| 3342 | return; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3343 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3344 | |
| 3345 | llvm_unreachable("Only arrays and structs are aggregate loadable types"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3346 | } |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3347 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3348 | |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 3349 | struct LoadOpSplitter : public OpSplitter<LoadOpSplitter> { |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3350 | AAMDNodes AATags; |
| 3351 | |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3352 | LoadOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy, |
| 3353 | AAMDNodes AATags, unsigned BaseAlign, const DataLayout &DL) |
| 3354 | : OpSplitter<LoadOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign, |
| 3355 | DL), AATags(AATags) {} |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3356 | |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3357 | /// Emit a leaf load of a single value. This is called at the leaves of the |
| 3358 | /// recursive emission to actually load values. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3359 | void emitFunc(Type *Ty, Value *&Agg, unsigned Align, const Twine &Name) { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3360 | assert(Ty->isSingleValueType()); |
| 3361 | // Load the single value and insert it using the indices. |
David Blaikie | aa41cd5 | 2015-04-03 21:33:42 +0000 | [diff] [blame] | 3362 | Value *GEP = |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 3363 | IRB.CreateInBoundsGEP(BaseTy, Ptr, GEPIndices, Name + ".gep"); |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3364 | LoadInst *Load = IRB.CreateAlignedLoad(Ty, GEP, Align, Name + ".load"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3365 | if (AATags) |
| 3366 | Load->setAAMetadata(AATags); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3367 | Agg = IRB.CreateInsertValue(Agg, Load, Indices, Name + ".insert"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3368 | LLVM_DEBUG(dbgs() << " to: " << *Load << "\n"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3369 | } |
| 3370 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3371 | |
| 3372 | bool visitLoadInst(LoadInst &LI) { |
| 3373 | assert(LI.getPointerOperand() == *U); |
| 3374 | if (!LI.isSimple() || LI.getType()->isSingleValueType()) |
| 3375 | return false; |
| 3376 | |
| 3377 | // We have an aggregate being loaded, split it apart. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3378 | LLVM_DEBUG(dbgs() << " original: " << LI << "\n"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3379 | AAMDNodes AATags; |
| 3380 | LI.getAAMetadata(AATags); |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3381 | LoadOpSplitter Splitter(&LI, *U, LI.getType(), AATags, |
| 3382 | getAdjustedAlignment(&LI, 0, DL), DL); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3383 | Value *V = UndefValue::get(LI.getType()); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3384 | Splitter.emitSplitOps(LI.getType(), V, LI.getName() + ".fca"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3385 | LI.replaceAllUsesWith(V); |
| 3386 | LI.eraseFromParent(); |
| 3387 | return true; |
| 3388 | } |
| 3389 | |
Benjamin Kramer | 73a9e4a | 2012-09-18 17:06:32 +0000 | [diff] [blame] | 3390 | struct StoreOpSplitter : public OpSplitter<StoreOpSplitter> { |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3391 | StoreOpSplitter(Instruction *InsertionPoint, Value *Ptr, Type *BaseTy, |
| 3392 | AAMDNodes AATags, unsigned BaseAlign, const DataLayout &DL) |
| 3393 | : OpSplitter<StoreOpSplitter>(InsertionPoint, Ptr, BaseTy, BaseAlign, |
| 3394 | DL), |
| 3395 | AATags(AATags) {} |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3396 | AAMDNodes AATags; |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3397 | /// Emit a leaf store of a single value. This is called at the leaves of the |
| 3398 | /// recursive emission to actually produce stores. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3399 | void emitFunc(Type *Ty, Value *&Agg, unsigned Align, const Twine &Name) { |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3400 | assert(Ty->isSingleValueType()); |
| 3401 | // Extract the single value and store it using the indices. |
Patrik Hagglund | a83706e | 2016-06-20 10:19:00 +0000 | [diff] [blame] | 3402 | // |
| 3403 | // The gep and extractvalue values are factored out of the CreateStore |
| 3404 | // call to make the output independent of the argument evaluation order. |
Patrik Hagglund | 4e0bd84 | 2016-06-20 11:19:58 +0000 | [diff] [blame] | 3405 | Value *ExtractValue = |
| 3406 | IRB.CreateExtractValue(Agg, Indices, Name + ".extract"); |
| 3407 | Value *InBoundsGEP = |
James Y Knight | 7716075 | 2019-02-01 20:44:47 +0000 | [diff] [blame] | 3408 | IRB.CreateInBoundsGEP(BaseTy, Ptr, GEPIndices, Name + ".gep"); |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3409 | StoreInst *Store = |
| 3410 | IRB.CreateAlignedStore(ExtractValue, InBoundsGEP, Align); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3411 | if (AATags) |
| 3412 | Store->setAAMetadata(AATags); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3413 | LLVM_DEBUG(dbgs() << " to: " << *Store << "\n"); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3414 | } |
| 3415 | }; |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3416 | |
| 3417 | bool visitStoreInst(StoreInst &SI) { |
| 3418 | if (!SI.isSimple() || SI.getPointerOperand() != *U) |
| 3419 | return false; |
| 3420 | Value *V = SI.getValueOperand(); |
| 3421 | if (V->getType()->isSingleValueType()) |
| 3422 | return false; |
| 3423 | |
| 3424 | // We have an aggregate being stored, split it apart. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3425 | LLVM_DEBUG(dbgs() << " original: " << SI << "\n"); |
Ivan A. Kosarev | 53270d0 | 2018-02-16 10:10:29 +0000 | [diff] [blame] | 3426 | AAMDNodes AATags; |
| 3427 | SI.getAAMetadata(AATags); |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 3428 | StoreOpSplitter Splitter(&SI, *U, V->getType(), AATags, |
| 3429 | getAdjustedAlignment(&SI, 0, DL), DL); |
Benjamin Kramer | 65f8c88 | 2012-09-18 16:20:46 +0000 | [diff] [blame] | 3430 | Splitter.emitSplitOps(V->getType(), V, V->getName() + ".fca"); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3431 | SI.eraseFromParent(); |
| 3432 | return true; |
| 3433 | } |
| 3434 | |
| 3435 | bool visitBitCastInst(BitCastInst &BC) { |
| 3436 | enqueueUsers(BC); |
| 3437 | return false; |
| 3438 | } |
| 3439 | |
Matt Arsenault | 282dac7 | 2019-06-14 21:38:31 +0000 | [diff] [blame] | 3440 | bool visitAddrSpaceCastInst(AddrSpaceCastInst &ASC) { |
| 3441 | enqueueUsers(ASC); |
| 3442 | return false; |
| 3443 | } |
| 3444 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3445 | bool visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 3446 | enqueueUsers(GEPI); |
| 3447 | return false; |
| 3448 | } |
| 3449 | |
| 3450 | bool visitPHINode(PHINode &PN) { |
| 3451 | enqueueUsers(PN); |
| 3452 | return false; |
| 3453 | } |
| 3454 | |
| 3455 | bool visitSelectInst(SelectInst &SI) { |
| 3456 | enqueueUsers(SI); |
| 3457 | return false; |
| 3458 | } |
| 3459 | }; |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3460 | |
| 3461 | } // end anonymous namespace |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 3462 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3463 | /// Strip aggregate type wrapping. |
Chandler Carruth | ba93199 | 2012-10-13 10:49:33 +0000 | [diff] [blame] | 3464 | /// |
| 3465 | /// This removes no-op aggregate types wrapping an underlying type. It will |
| 3466 | /// strip as many layers of types as it can without changing either the type |
| 3467 | /// size or the allocated size. |
| 3468 | static Type *stripAggregateTypeWrapping(const DataLayout &DL, Type *Ty) { |
| 3469 | if (Ty->isSingleValueType()) |
| 3470 | return Ty; |
| 3471 | |
| 3472 | uint64_t AllocSize = DL.getTypeAllocSize(Ty); |
| 3473 | uint64_t TypeSize = DL.getTypeSizeInBits(Ty); |
| 3474 | |
| 3475 | Type *InnerTy; |
| 3476 | if (ArrayType *ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 3477 | InnerTy = ArrTy->getElementType(); |
| 3478 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| 3479 | const StructLayout *SL = DL.getStructLayout(STy); |
| 3480 | unsigned Index = SL->getElementContainingOffset(0); |
| 3481 | InnerTy = STy->getElementType(Index); |
| 3482 | } else { |
| 3483 | return Ty; |
| 3484 | } |
| 3485 | |
| 3486 | if (AllocSize > DL.getTypeAllocSize(InnerTy) || |
| 3487 | TypeSize > DL.getTypeSizeInBits(InnerTy)) |
| 3488 | return Ty; |
| 3489 | |
| 3490 | return stripAggregateTypeWrapping(DL, InnerTy); |
| 3491 | } |
| 3492 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3493 | /// 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] | 3494 | /// offset and size. |
| 3495 | /// |
| 3496 | /// This recurses through the aggregate type and tries to compute a subtype |
| 3497 | /// 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] | 3498 | /// of an array, it will even compute a new array type for that sub-section, |
| 3499 | /// and the same for structs. |
| 3500 | /// |
| 3501 | /// Note that this routine is very strict and tries to find a partition of the |
| 3502 | /// type which produces the *exact* right offset and size. It is not forgiving |
| 3503 | /// when the size or offset cause either end of type-based partition to be off. |
| 3504 | /// Also, this is a best-effort routine. It is reasonable to give up and not |
| 3505 | /// return a type if necessary. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3506 | static Type *getTypePartition(const DataLayout &DL, Type *Ty, uint64_t Offset, |
| 3507 | uint64_t Size) { |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3508 | if (Offset == 0 && DL.getTypeAllocSize(Ty) == Size) |
| 3509 | return stripAggregateTypeWrapping(DL, Ty); |
| 3510 | if (Offset > DL.getTypeAllocSize(Ty) || |
| 3511 | (DL.getTypeAllocSize(Ty) - Offset) < Size) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3512 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3513 | |
| 3514 | if (SequentialType *SeqTy = dyn_cast<SequentialType>(Ty)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3515 | Type *ElementTy = SeqTy->getElementType(); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3516 | uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3517 | uint64_t NumSkippedElements = Offset / ElementSize; |
Peter Collingbourne | bc07052 | 2016-12-02 03:20:58 +0000 | [diff] [blame] | 3518 | if (NumSkippedElements >= SeqTy->getNumElements()) |
| 3519 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3520 | Offset -= NumSkippedElements * ElementSize; |
| 3521 | |
| 3522 | // First check if we need to recurse. |
| 3523 | if (Offset > 0 || Size < ElementSize) { |
| 3524 | // Bail if the partition ends in a different array element. |
| 3525 | if ((Offset + Size) > ElementSize) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3526 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3527 | // Recurse through the element type trying to peel off offset bytes. |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3528 | return getTypePartition(DL, ElementTy, Offset, Size); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3529 | } |
| 3530 | assert(Offset == 0); |
| 3531 | |
| 3532 | if (Size == ElementSize) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3533 | return stripAggregateTypeWrapping(DL, ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3534 | assert(Size > ElementSize); |
| 3535 | uint64_t NumElements = Size / ElementSize; |
| 3536 | if (NumElements * ElementSize != Size) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3537 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3538 | return ArrayType::get(ElementTy, NumElements); |
| 3539 | } |
| 3540 | |
| 3541 | StructType *STy = dyn_cast<StructType>(Ty); |
| 3542 | if (!STy) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3543 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3544 | |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3545 | const StructLayout *SL = DL.getStructLayout(STy); |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3546 | if (Offset >= SL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3547 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3548 | uint64_t EndOffset = Offset + Size; |
| 3549 | if (EndOffset > SL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3550 | return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3551 | |
| 3552 | unsigned Index = SL->getElementContainingOffset(Offset); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3553 | Offset -= SL->getElementOffset(Index); |
| 3554 | |
| 3555 | Type *ElementTy = STy->getElementType(Index); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3556 | uint64_t ElementSize = DL.getTypeAllocSize(ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3557 | if (Offset >= ElementSize) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3558 | return nullptr; // The offset points into alignment padding. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3559 | |
| 3560 | // See if any partition must be contained by the element. |
| 3561 | if (Offset > 0 || Size < ElementSize) { |
| 3562 | if ((Offset + Size) > ElementSize) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3563 | return nullptr; |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3564 | return getTypePartition(DL, ElementTy, Offset, Size); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3565 | } |
| 3566 | assert(Offset == 0); |
| 3567 | |
| 3568 | if (Size == ElementSize) |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3569 | return stripAggregateTypeWrapping(DL, ElementTy); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3570 | |
| 3571 | StructType::element_iterator EI = STy->element_begin() + Index, |
| 3572 | EE = STy->element_end(); |
| 3573 | if (EndOffset < SL->getSizeInBytes()) { |
| 3574 | unsigned EndIndex = SL->getElementContainingOffset(EndOffset); |
| 3575 | if (Index == EndIndex) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3576 | return nullptr; // Within a single element and its padding. |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3577 | |
| 3578 | // Don't try to form "natural" types if the elements don't line up with the |
| 3579 | // expected size. |
| 3580 | // FIXME: We could potentially recurse down through the last element in the |
| 3581 | // sub-struct to find a natural end point. |
| 3582 | if (SL->getElementOffset(EndIndex) != EndOffset) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3583 | return nullptr; |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3584 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3585 | assert(Index < EndIndex); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3586 | EE = STy->element_begin() + EndIndex; |
| 3587 | } |
| 3588 | |
| 3589 | // Try to build up a sub-structure. |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 3590 | StructType *SubTy = |
| 3591 | StructType::get(STy->getContext(), makeArrayRef(EI, EE), STy->isPacked()); |
Chandler Carruth | 90a735d | 2013-07-19 07:21:28 +0000 | [diff] [blame] | 3592 | const StructLayout *SubSL = DL.getStructLayout(SubTy); |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3593 | if (Size != SubSL->getSizeInBytes()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3594 | return nullptr; // The sub-struct doesn't have quite the size needed. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3595 | |
Chandler Carruth | 054a40a | 2012-09-14 11:08:31 +0000 | [diff] [blame] | 3596 | return SubTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 3599 | /// Pre-split loads and stores to simplify rewriting. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3600 | /// |
| 3601 | /// We want to break up the splittable load+store pairs as much as |
| 3602 | /// possible. This is important to do as a preprocessing step, as once we |
| 3603 | /// start rewriting the accesses to partitions of the alloca we lose the |
| 3604 | /// necessary information to correctly split apart paired loads and stores |
| 3605 | /// which both point into this alloca. The case to consider is something like |
| 3606 | /// the following: |
| 3607 | /// |
| 3608 | /// %a = alloca [12 x i8] |
| 3609 | /// %gep1 = getelementptr [12 x i8]* %a, i32 0, i32 0 |
| 3610 | /// %gep2 = getelementptr [12 x i8]* %a, i32 0, i32 4 |
| 3611 | /// %gep3 = getelementptr [12 x i8]* %a, i32 0, i32 8 |
| 3612 | /// %iptr1 = bitcast i8* %gep1 to i64* |
| 3613 | /// %iptr2 = bitcast i8* %gep2 to i64* |
| 3614 | /// %fptr1 = bitcast i8* %gep1 to float* |
| 3615 | /// %fptr2 = bitcast i8* %gep2 to float* |
| 3616 | /// %fptr3 = bitcast i8* %gep3 to float* |
| 3617 | /// store float 0.0, float* %fptr1 |
| 3618 | /// store float 1.0, float* %fptr2 |
| 3619 | /// %v = load i64* %iptr1 |
| 3620 | /// store i64 %v, i64* %iptr2 |
| 3621 | /// %f1 = load float* %fptr2 |
| 3622 | /// %f2 = load float* %fptr3 |
| 3623 | /// |
| 3624 | /// Here we want to form 3 partitions of the alloca, each 4 bytes large, and |
| 3625 | /// promote everything so we recover the 2 SSA values that should have been |
| 3626 | /// there all along. |
| 3627 | /// |
| 3628 | /// \returns true if any changes are made. |
| 3629 | bool SROA::presplitLoadsAndStores(AllocaInst &AI, AllocaSlices &AS) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3630 | LLVM_DEBUG(dbgs() << "Pre-splitting loads and stores\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3631 | |
| 3632 | // Track the loads and stores which are candidates for pre-splitting here, in |
| 3633 | // the order they first appear during the partition scan. These give stable |
| 3634 | // iteration order and a basis for tracking which loads and stores we |
| 3635 | // actually split. |
| 3636 | SmallVector<LoadInst *, 4> Loads; |
| 3637 | SmallVector<StoreInst *, 4> Stores; |
| 3638 | |
| 3639 | // We need to accumulate the splits required of each load or store where we |
| 3640 | // can find them via a direct lookup. This is important to cross-check loads |
| 3641 | // and stores against each other. We also track the slice so that we can kill |
| 3642 | // all the slices that end up split. |
| 3643 | struct SplitOffsets { |
| 3644 | Slice *S; |
| 3645 | std::vector<uint64_t> Splits; |
| 3646 | }; |
| 3647 | SmallDenseMap<Instruction *, SplitOffsets, 8> SplitOffsetsMap; |
| 3648 | |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3649 | // Track loads out of this alloca which cannot, for any reason, be pre-split. |
| 3650 | // This is important as we also cannot pre-split stores of those loads! |
| 3651 | // FIXME: This is all pretty gross. It means that we can be more aggressive |
| 3652 | // in pre-splitting when the load feeding the store happens to come from |
| 3653 | // a separate alloca. Put another way, the effectiveness of SROA would be |
| 3654 | // decreased by a frontend which just concatenated all of its local allocas |
| 3655 | // into one big flat alloca. But defeating such patterns is exactly the job |
| 3656 | // SROA is tasked with! Sadly, to not have this discrepancy we would have |
| 3657 | // change store pre-splitting to actually force pre-splitting of the load |
| 3658 | // that feeds it *and all stores*. That makes pre-splitting much harder, but |
| 3659 | // maybe it would make it more principled? |
| 3660 | SmallPtrSet<LoadInst *, 8> UnsplittableLoads; |
| 3661 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3662 | LLVM_DEBUG(dbgs() << " Searching for candidate loads and stores\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3663 | for (auto &P : AS.partitions()) { |
| 3664 | for (Slice &S : P) { |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3665 | Instruction *I = cast<Instruction>(S.getUse()->getUser()); |
Chandler Carruth | 37f1f12 | 2016-03-10 15:31:17 +0000 | [diff] [blame] | 3666 | if (!S.isSplittable() || S.endOffset() <= P.endOffset()) { |
| 3667 | // If this is a load we have to track that it can't participate in any |
| 3668 | // pre-splitting. If this is a store of a load we have to track that |
| 3669 | // that load also can't participate in any pre-splitting. |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3670 | if (auto *LI = dyn_cast<LoadInst>(I)) |
| 3671 | UnsplittableLoads.insert(LI); |
Chandler Carruth | 37f1f12 | 2016-03-10 15:31:17 +0000 | [diff] [blame] | 3672 | else if (auto *SI = dyn_cast<StoreInst>(I)) |
| 3673 | if (auto *LI = dyn_cast<LoadInst>(SI->getValueOperand())) |
| 3674 | UnsplittableLoads.insert(LI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3675 | continue; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3676 | } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3677 | assert(P.endOffset() > S.beginOffset() && |
| 3678 | "Empty or backwards partition!"); |
| 3679 | |
| 3680 | // Determine if this is a pre-splittable slice. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3681 | if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 3682 | assert(!LI->isVolatile() && "Cannot split volatile loads!"); |
| 3683 | |
| 3684 | // The load must be used exclusively to store into other pointers for |
| 3685 | // us to be able to arbitrarily pre-split it. The stores must also be |
| 3686 | // simple to avoid changing semantics. |
| 3687 | auto IsLoadSimplyStored = [](LoadInst *LI) { |
| 3688 | for (User *LU : LI->users()) { |
| 3689 | auto *SI = dyn_cast<StoreInst>(LU); |
| 3690 | if (!SI || !SI->isSimple()) |
| 3691 | return false; |
| 3692 | } |
| 3693 | return true; |
| 3694 | }; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3695 | if (!IsLoadSimplyStored(LI)) { |
| 3696 | UnsplittableLoads.insert(LI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3697 | continue; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3698 | } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3699 | |
| 3700 | Loads.push_back(LI); |
Chandler Carruth | d94a596 | 2016-03-10 14:16:18 +0000 | [diff] [blame] | 3701 | } else if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 3702 | if (S.getUse() != &SI->getOperandUse(SI->getPointerOperandIndex())) |
| 3703 | // Skip stores *of* pointers. FIXME: This shouldn't even be possible! |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3704 | continue; |
| 3705 | auto *StoredLoad = dyn_cast<LoadInst>(SI->getValueOperand()); |
| 3706 | if (!StoredLoad || !StoredLoad->isSimple()) |
| 3707 | continue; |
| 3708 | assert(!SI->isVolatile() && "Cannot split volatile stores!"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3709 | |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3710 | Stores.push_back(SI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3711 | } else { |
| 3712 | // Other uses cannot be pre-split. |
| 3713 | continue; |
| 3714 | } |
| 3715 | |
| 3716 | // Record the initial split. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3717 | LLVM_DEBUG(dbgs() << " Candidate: " << *I << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3718 | auto &Offsets = SplitOffsetsMap[I]; |
| 3719 | assert(Offsets.Splits.empty() && |
| 3720 | "Should not have splits the first time we see an instruction!"); |
| 3721 | Offsets.S = &S; |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3722 | Offsets.Splits.push_back(P.endOffset() - S.beginOffset()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | // Now scan the already split slices, and add a split for any of them which |
| 3726 | // we're going to pre-split. |
| 3727 | for (Slice *S : P.splitSliceTails()) { |
| 3728 | auto SplitOffsetsMapI = |
| 3729 | SplitOffsetsMap.find(cast<Instruction>(S->getUse()->getUser())); |
| 3730 | if (SplitOffsetsMapI == SplitOffsetsMap.end()) |
| 3731 | continue; |
| 3732 | auto &Offsets = SplitOffsetsMapI->second; |
| 3733 | |
| 3734 | assert(Offsets.S == S && "Found a mismatched slice!"); |
| 3735 | assert(!Offsets.Splits.empty() && |
| 3736 | "Cannot have an empty set of splits on the second partition!"); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3737 | assert(Offsets.Splits.back() == |
| 3738 | P.beginOffset() - Offsets.S->beginOffset() && |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3739 | "Previous split does not end where this one begins!"); |
| 3740 | |
| 3741 | // Record each split. The last partition's end isn't needed as the size |
| 3742 | // of the slice dictates that. |
| 3743 | if (S->endOffset() > P.endOffset()) |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3744 | Offsets.Splits.push_back(P.endOffset() - Offsets.S->beginOffset()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3745 | } |
| 3746 | } |
| 3747 | |
| 3748 | // We may have split loads where some of their stores are split stores. For |
| 3749 | // such loads and stores, we can only pre-split them if their splits exactly |
| 3750 | // match relative to their starting offset. We have to verify this prior to |
| 3751 | // any rewriting. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3752 | Stores.erase( |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3753 | llvm::remove_if(Stores, |
| 3754 | [&UnsplittableLoads, &SplitOffsetsMap](StoreInst *SI) { |
| 3755 | // Lookup the load we are storing in our map of split |
| 3756 | // offsets. |
| 3757 | auto *LI = cast<LoadInst>(SI->getValueOperand()); |
| 3758 | // If it was completely unsplittable, then we're done, |
| 3759 | // and this store can't be pre-split. |
| 3760 | if (UnsplittableLoads.count(LI)) |
| 3761 | return true; |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3762 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3763 | auto LoadOffsetsI = SplitOffsetsMap.find(LI); |
| 3764 | if (LoadOffsetsI == SplitOffsetsMap.end()) |
| 3765 | return false; // Unrelated loads are definitely safe. |
| 3766 | auto &LoadOffsets = LoadOffsetsI->second; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3767 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3768 | // Now lookup the store's offsets. |
| 3769 | auto &StoreOffsets = SplitOffsetsMap[SI]; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3770 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3771 | // If the relative offsets of each split in the load and |
| 3772 | // store match exactly, then we can split them and we |
| 3773 | // don't need to remove them here. |
| 3774 | if (LoadOffsets.Splits == StoreOffsets.Splits) |
| 3775 | return false; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3776 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3777 | LLVM_DEBUG( |
| 3778 | dbgs() |
| 3779 | << " Mismatched splits for load and store:\n" |
| 3780 | << " " << *LI << "\n" |
| 3781 | << " " << *SI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3782 | |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3783 | // We've found a store and load that we need to split |
| 3784 | // with mismatched relative splits. Just give up on them |
| 3785 | // and remove both instructions from our list of |
| 3786 | // candidates. |
| 3787 | UnsplittableLoads.insert(LI); |
| 3788 | return true; |
| 3789 | }), |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3790 | Stores.end()); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3791 | // 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] | 3792 | // have caused an earlier store's load to become unsplittable and if it is |
| 3793 | // unsplittable for the later store, then we can't rely on it being split in |
| 3794 | // the earlier store either. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3795 | Stores.erase(llvm::remove_if(Stores, |
| 3796 | [&UnsplittableLoads](StoreInst *SI) { |
| 3797 | auto *LI = |
| 3798 | cast<LoadInst>(SI->getValueOperand()); |
| 3799 | return UnsplittableLoads.count(LI); |
| 3800 | }), |
Chandler Carruth | 73b0164 | 2015-01-05 04:17:53 +0000 | [diff] [blame] | 3801 | Stores.end()); |
| 3802 | // Once we've established all the loads that can't be split for some reason, |
| 3803 | // filter any that made it into our list out. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 3804 | Loads.erase(llvm::remove_if(Loads, |
| 3805 | [&UnsplittableLoads](LoadInst *LI) { |
| 3806 | return UnsplittableLoads.count(LI); |
| 3807 | }), |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3808 | Loads.end()); |
| 3809 | |
| 3810 | // If no loads or stores are left, there is no pre-splitting to be done for |
| 3811 | // this alloca. |
| 3812 | if (Loads.empty() && Stores.empty()) |
| 3813 | return false; |
| 3814 | |
| 3815 | // From here on, we can't fail and will be building new accesses, so rig up |
| 3816 | // an IR builder. |
| 3817 | IRBuilderTy IRB(&AI); |
| 3818 | |
| 3819 | // Collect the new slices which we will merge into the alloca slices. |
| 3820 | SmallVector<Slice, 4> NewSlices; |
| 3821 | |
| 3822 | // Track any allocas we end up splitting loads and stores for so we iterate |
| 3823 | // on them. |
| 3824 | SmallPtrSet<AllocaInst *, 4> ResplitPromotableAllocas; |
| 3825 | |
| 3826 | // At this point, we have collected all of the loads and stores we can |
| 3827 | // pre-split, and the specific splits needed for them. We actually do the |
| 3828 | // splitting in a specific order in order to handle when one of the loads in |
| 3829 | // the value operand to one of the stores. |
| 3830 | // |
| 3831 | // First, we rewrite all of the split loads, and just accumulate each split |
| 3832 | // load in a parallel structure. We also build the slices for them and append |
| 3833 | // them to the alloca slices. |
| 3834 | SmallDenseMap<LoadInst *, std::vector<LoadInst *>, 1> SplitLoadsMap; |
| 3835 | std::vector<LoadInst *> SplitLoads; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3836 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3837 | for (LoadInst *LI : Loads) { |
| 3838 | SplitLoads.clear(); |
| 3839 | |
| 3840 | IntegerType *Ty = cast<IntegerType>(LI->getType()); |
| 3841 | uint64_t LoadSize = Ty->getBitWidth() / 8; |
| 3842 | assert(LoadSize > 0 && "Cannot have a zero-sized integer load!"); |
| 3843 | |
| 3844 | auto &Offsets = SplitOffsetsMap[LI]; |
| 3845 | assert(LoadSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && |
| 3846 | "Slice size should always match load size exactly!"); |
| 3847 | uint64_t BaseOffset = Offsets.S->beginOffset(); |
| 3848 | assert(BaseOffset + LoadSize > BaseOffset && |
| 3849 | "Cannot represent alloca access size using 64-bit integers!"); |
| 3850 | |
| 3851 | Instruction *BasePtr = cast<Instruction>(LI->getPointerOperand()); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3852 | IRB.SetInsertPoint(LI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3853 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3854 | LLVM_DEBUG(dbgs() << " Splitting load: " << *LI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3855 | |
| 3856 | uint64_t PartOffset = 0, PartSize = Offsets.Splits.front(); |
| 3857 | int Idx = 0, Size = Offsets.Splits.size(); |
| 3858 | for (;;) { |
| 3859 | auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8); |
Yaxun Liu | 7c44f34 | 2017-06-27 18:26:06 +0000 | [diff] [blame] | 3860 | auto AS = LI->getPointerAddressSpace(); |
| 3861 | auto *PartPtrTy = PartTy->getPointerTo(AS); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3862 | LoadInst *PLoad = IRB.CreateAlignedLoad( |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 3863 | PartTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3864 | getAdjustedPtr(IRB, DL, BasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3865 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3866 | PartPtrTy, BasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3867 | getAdjustedAlignment(LI, PartOffset, DL), /*IsVolatile*/ false, |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3868 | LI->getName()); |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 3869 | PLoad->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access, |
| 3870 | LLVMContext::MD_access_group}); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3871 | |
| 3872 | // Append this load onto the list of split loads so we can find it later |
| 3873 | // to rewrite the stores. |
| 3874 | SplitLoads.push_back(PLoad); |
| 3875 | |
| 3876 | // Now build a new slice for the alloca. |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3877 | NewSlices.push_back( |
| 3878 | Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize, |
| 3879 | &PLoad->getOperandUse(PLoad->getPointerOperandIndex()), |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 3880 | /*IsSplittable*/ false)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3881 | LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset() |
| 3882 | << ", " << NewSlices.back().endOffset() |
| 3883 | << "): " << *PLoad << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3884 | |
Chandler Carruth | 29c22fa | 2015-01-02 00:10:22 +0000 | [diff] [blame] | 3885 | // See if we've handled all the splits. |
| 3886 | if (Idx >= Size) |
| 3887 | break; |
| 3888 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3889 | // Setup the next partition. |
| 3890 | PartOffset = Offsets.Splits[Idx]; |
| 3891 | ++Idx; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3892 | PartSize = (Idx < Size ? Offsets.Splits[Idx] : LoadSize) - PartOffset; |
| 3893 | } |
| 3894 | |
| 3895 | // Now that we have the split loads, do the slow walk over all uses of the |
| 3896 | // load and rewrite them as split stores, or save the split loads to use |
| 3897 | // below if the store is going to be split there anyways. |
| 3898 | bool DeferredStores = false; |
| 3899 | for (User *LU : LI->users()) { |
| 3900 | StoreInst *SI = cast<StoreInst>(LU); |
| 3901 | if (!Stores.empty() && SplitOffsetsMap.count(SI)) { |
| 3902 | DeferredStores = true; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3903 | LLVM_DEBUG(dbgs() << " Deferred splitting of store: " << *SI |
| 3904 | << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3905 | continue; |
| 3906 | } |
| 3907 | |
Chandler Carruth | c39eaa5 | 2015-01-01 23:26:16 +0000 | [diff] [blame] | 3908 | Value *StoreBasePtr = SI->getPointerOperand(); |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 3909 | IRB.SetInsertPoint(SI); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3910 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3911 | LLVM_DEBUG(dbgs() << " Splitting store of load: " << *SI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3912 | |
| 3913 | for (int Idx = 0, Size = SplitLoads.size(); Idx < Size; ++Idx) { |
| 3914 | LoadInst *PLoad = SplitLoads[Idx]; |
| 3915 | uint64_t PartOffset = Idx == 0 ? 0 : Offsets.Splits[Idx - 1]; |
Chandler Carruth | 994cde8 | 2015-01-01 12:01:03 +0000 | [diff] [blame] | 3916 | auto *PartPtrTy = |
| 3917 | PLoad->getType()->getPointerTo(SI->getPointerAddressSpace()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3918 | |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3919 | auto AS = SI->getPointerAddressSpace(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3920 | StoreInst *PStore = IRB.CreateAlignedStore( |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3921 | PLoad, |
| 3922 | getAdjustedPtr(IRB, DL, StoreBasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 3923 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 3924 | PartPtrTy, StoreBasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3925 | getAdjustedAlignment(SI, PartOffset, DL), /*IsVolatile*/ false); |
Michael Kruse | 978ba61 | 2018-12-20 04:58:07 +0000 | [diff] [blame] | 3926 | PStore->copyMetadata(*LI, {LLVMContext::MD_mem_parallel_loop_access, |
| 3927 | LLVMContext::MD_access_group}); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3928 | LLVM_DEBUG(dbgs() << " +" << PartOffset << ":" << *PStore << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | // We want to immediately iterate on any allocas impacted by splitting |
| 3932 | // this store, and we have to track any promotable alloca (indicated by |
| 3933 | // a direct store) as needing to be resplit because it is no longer |
| 3934 | // promotable. |
| 3935 | if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(StoreBasePtr)) { |
| 3936 | ResplitPromotableAllocas.insert(OtherAI); |
| 3937 | Worklist.insert(OtherAI); |
| 3938 | } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>( |
| 3939 | StoreBasePtr->stripInBoundsOffsets())) { |
| 3940 | Worklist.insert(OtherAI); |
| 3941 | } |
| 3942 | |
| 3943 | // Mark the original store as dead. |
| 3944 | DeadInsts.insert(SI); |
| 3945 | } |
| 3946 | |
| 3947 | // Save the split loads if there are deferred stores among the users. |
| 3948 | if (DeferredStores) |
| 3949 | SplitLoadsMap.insert(std::make_pair(LI, std::move(SplitLoads))); |
| 3950 | |
| 3951 | // Mark the original load as dead and kill the original slice. |
| 3952 | DeadInsts.insert(LI); |
| 3953 | Offsets.S->kill(); |
| 3954 | } |
| 3955 | |
| 3956 | // Second, we rewrite all of the split stores. At this point, we know that |
| 3957 | // all loads from this alloca have been split already. For stores of such |
| 3958 | // loads, we can simply look up the pre-existing split loads. For stores of |
| 3959 | // other loads, we split those loads first and then write split stores of |
| 3960 | // them. |
| 3961 | for (StoreInst *SI : Stores) { |
| 3962 | auto *LI = cast<LoadInst>(SI->getValueOperand()); |
| 3963 | IntegerType *Ty = cast<IntegerType>(LI->getType()); |
| 3964 | uint64_t StoreSize = Ty->getBitWidth() / 8; |
| 3965 | assert(StoreSize > 0 && "Cannot have a zero-sized integer store!"); |
| 3966 | |
| 3967 | auto &Offsets = SplitOffsetsMap[SI]; |
| 3968 | assert(StoreSize == Offsets.S->endOffset() - Offsets.S->beginOffset() && |
| 3969 | "Slice size should always match load size exactly!"); |
| 3970 | uint64_t BaseOffset = Offsets.S->beginOffset(); |
| 3971 | assert(BaseOffset + StoreSize > BaseOffset && |
| 3972 | "Cannot represent alloca access size using 64-bit integers!"); |
| 3973 | |
Chandler Carruth | c39eaa5 | 2015-01-01 23:26:16 +0000 | [diff] [blame] | 3974 | Value *LoadBasePtr = LI->getPointerOperand(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3975 | Instruction *StoreBasePtr = cast<Instruction>(SI->getPointerOperand()); |
| 3976 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3977 | LLVM_DEBUG(dbgs() << " Splitting store: " << *SI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3978 | |
| 3979 | // Check whether we have an already split load. |
| 3980 | auto SplitLoadsMapI = SplitLoadsMap.find(LI); |
| 3981 | std::vector<LoadInst *> *SplitLoads = nullptr; |
| 3982 | if (SplitLoadsMapI != SplitLoadsMap.end()) { |
| 3983 | SplitLoads = &SplitLoadsMapI->second; |
| 3984 | assert(SplitLoads->size() == Offsets.Splits.size() + 1 && |
| 3985 | "Too few split loads for the number of splits in the store!"); |
| 3986 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3987 | LLVM_DEBUG(dbgs() << " of load: " << *LI << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3990 | uint64_t PartOffset = 0, PartSize = Offsets.Splits.front(); |
| 3991 | int Idx = 0, Size = Offsets.Splits.size(); |
| 3992 | for (;;) { |
| 3993 | auto *PartTy = Type::getIntNTy(Ty->getContext(), PartSize * 8); |
Keno Fischer | 514a6a5 | 2017-06-02 19:04:17 +0000 | [diff] [blame] | 3994 | auto *LoadPartPtrTy = PartTy->getPointerTo(LI->getPointerAddressSpace()); |
| 3995 | auto *StorePartPtrTy = PartTy->getPointerTo(SI->getPointerAddressSpace()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 3996 | |
| 3997 | // Either lookup a split load or create one. |
| 3998 | LoadInst *PLoad; |
| 3999 | if (SplitLoads) { |
| 4000 | PLoad = (*SplitLoads)[Idx]; |
| 4001 | } else { |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 4002 | IRB.SetInsertPoint(LI); |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 4003 | auto AS = LI->getPointerAddressSpace(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4004 | PLoad = IRB.CreateAlignedLoad( |
James Y Knight | 14359ef | 2019-02-01 20:44:24 +0000 | [diff] [blame] | 4005 | PartTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4006 | getAdjustedPtr(IRB, DL, LoadBasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 4007 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Keno Fischer | 514a6a5 | 2017-06-02 19:04:17 +0000 | [diff] [blame] | 4008 | LoadPartPtrTy, LoadBasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4009 | getAdjustedAlignment(LI, PartOffset, DL), /*IsVolatile*/ false, |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4010 | LI->getName()); |
| 4011 | } |
| 4012 | |
| 4013 | // And store this partition. |
Duncan P. N. Exon Smith | be4d8cb | 2015-10-13 19:26:58 +0000 | [diff] [blame] | 4014 | IRB.SetInsertPoint(SI); |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 4015 | auto AS = SI->getPointerAddressSpace(); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4016 | StoreInst *PStore = IRB.CreateAlignedStore( |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 4017 | PLoad, |
| 4018 | getAdjustedPtr(IRB, DL, StoreBasePtr, |
Elena Demikhovsky | 945b7e5 | 2018-02-14 06:58:08 +0000 | [diff] [blame] | 4019 | APInt(DL.getIndexSizeInBits(AS), PartOffset), |
Yaxun Liu | 6455b0d | 2017-06-09 20:46:29 +0000 | [diff] [blame] | 4020 | StorePartPtrTy, StoreBasePtr->getName() + "."), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4021 | getAdjustedAlignment(SI, PartOffset, DL), /*IsVolatile*/ false); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4022 | |
| 4023 | // Now build a new slice for the alloca. |
| 4024 | NewSlices.push_back( |
| 4025 | Slice(BaseOffset + PartOffset, BaseOffset + PartOffset + PartSize, |
| 4026 | &PStore->getOperandUse(PStore->getPointerOperandIndex()), |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4027 | /*IsSplittable*/ false)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4028 | LLVM_DEBUG(dbgs() << " new slice [" << NewSlices.back().beginOffset() |
| 4029 | << ", " << NewSlices.back().endOffset() |
| 4030 | << "): " << *PStore << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4031 | if (!SplitLoads) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4032 | LLVM_DEBUG(dbgs() << " of split load: " << *PLoad << "\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Chandler Carruth | 29c22fa | 2015-01-02 00:10:22 +0000 | [diff] [blame] | 4035 | // See if we've finished all the splits. |
| 4036 | if (Idx >= Size) |
| 4037 | break; |
| 4038 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4039 | // Setup the next partition. |
| 4040 | PartOffset = Offsets.Splits[Idx]; |
| 4041 | ++Idx; |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4042 | PartSize = (Idx < Size ? Offsets.Splits[Idx] : StoreSize) - PartOffset; |
| 4043 | } |
| 4044 | |
| 4045 | // We want to immediately iterate on any allocas impacted by splitting |
| 4046 | // this load, which is only relevant if it isn't a load of this alloca and |
| 4047 | // thus we didn't already split the loads above. We also have to keep track |
| 4048 | // of any promotable allocas we split loads on as they can no longer be |
| 4049 | // promoted. |
| 4050 | if (!SplitLoads) { |
| 4051 | if (AllocaInst *OtherAI = dyn_cast<AllocaInst>(LoadBasePtr)) { |
| 4052 | assert(OtherAI != &AI && "We can't re-split our own alloca!"); |
| 4053 | ResplitPromotableAllocas.insert(OtherAI); |
| 4054 | Worklist.insert(OtherAI); |
| 4055 | } else if (AllocaInst *OtherAI = dyn_cast<AllocaInst>( |
| 4056 | LoadBasePtr->stripInBoundsOffsets())) { |
| 4057 | assert(OtherAI != &AI && "We can't re-split our own alloca!"); |
| 4058 | Worklist.insert(OtherAI); |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | // 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] | 4063 | // 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] | 4064 | // 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] | 4065 | // for some other alloca, but it may be a normal load. This may introduce |
| 4066 | // redundant loads, but where those can be merged the rest of the optimizer |
| 4067 | // should handle the merging, and this uncovers SSA splits which is more |
| 4068 | // important. In practice, the original loads will almost always be fully |
| 4069 | // split and removed eventually, and the splits will be merged by any |
| 4070 | // trivial CSE, including instcombine. |
| 4071 | if (LI->hasOneUse()) { |
| 4072 | assert(*LI->user_begin() == SI && "Single use isn't this store!"); |
| 4073 | DeadInsts.insert(LI); |
| 4074 | } |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4075 | DeadInsts.insert(SI); |
| 4076 | Offsets.S->kill(); |
| 4077 | } |
| 4078 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4079 | // Remove the killed slices that have ben pre-split. |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4080 | AS.erase(llvm::remove_if(AS, [](const Slice &S) { return S.isDead(); }), |
| 4081 | AS.end()); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4082 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4083 | // Insert our new slices. This will sort and merge them into the sorted |
| 4084 | // sequence. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4085 | AS.insert(NewSlices); |
| 4086 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4087 | LLVM_DEBUG(dbgs() << " Pre-split slices:\n"); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4088 | #ifndef NDEBUG |
| 4089 | for (auto I = AS.begin(), E = AS.end(); I != E; ++I) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4090 | LLVM_DEBUG(AS.print(dbgs(), I, " ")); |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4091 | #endif |
| 4092 | |
| 4093 | // Finally, don't try to promote any allocas that new require re-splitting. |
| 4094 | // They have already been added to the worklist above. |
| 4095 | PromotableAllocas.erase( |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4096 | llvm::remove_if( |
David Majnemer | c700490 | 2016-08-12 04:32:37 +0000 | [diff] [blame] | 4097 | PromotableAllocas, |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4098 | [&](AllocaInst *AI) { return ResplitPromotableAllocas.count(AI); }), |
| 4099 | PromotableAllocas.end()); |
| 4100 | |
| 4101 | return true; |
| 4102 | } |
| 4103 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4104 | /// Rewrite an alloca partition's users. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4105 | /// |
| 4106 | /// This routine drives both of the rewriting goals of the SROA pass. It tries |
| 4107 | /// to rewrite uses of an alloca partition to be conducive for SSA value |
| 4108 | /// promotion. If the partition needs a new, more refined alloca, this will |
| 4109 | /// build that new alloca, preserving as much type information as possible, and |
| 4110 | /// rewrite the uses of the old alloca to point at the new one and have the |
| 4111 | /// appropriate new offsets. It also evaluates how successful the rewrite was |
| 4112 | /// at enabling promotion and if it was successful queues the alloca to be |
| 4113 | /// promoted. |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4114 | AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS, |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4115 | Partition &P) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4116 | // Try to compute a friendly type for this partition of the alloca. This |
| 4117 | // won't always succeed, in which case we fall back to a legal integer type |
| 4118 | // or an i8 array of an appropriate size. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4119 | Type *SliceTy = nullptr; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4120 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4121 | if (Type *CommonUseTy = findCommonType(P.begin(), P.end(), P.endOffset())) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4122 | if (DL.getTypeAllocSize(CommonUseTy) >= P.size()) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4123 | SliceTy = CommonUseTy; |
| 4124 | if (!SliceTy) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4125 | if (Type *TypePartitionTy = getTypePartition(DL, AI.getAllocatedType(), |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4126 | P.beginOffset(), P.size())) |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4127 | SliceTy = TypePartitionTy; |
| 4128 | if ((!SliceTy || (SliceTy->isArrayTy() && |
| 4129 | SliceTy->getArrayElementType()->isIntegerTy())) && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4130 | DL.isLegalInteger(P.size() * 8)) |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4131 | SliceTy = Type::getIntNTy(*C, P.size() * 8); |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4132 | if (!SliceTy) |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4133 | SliceTy = ArrayType::get(Type::getInt8Ty(*C), P.size()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4134 | assert(DL.getTypeAllocSize(SliceTy) >= P.size()); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4135 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4136 | bool IsIntegerPromotable = isIntegerWideningViable(P, SliceTy, DL); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4137 | |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 4138 | VectorType *VecTy = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4139 | IsIntegerPromotable ? nullptr : isVectorPromotionViable(P, DL); |
Chandler Carruth | 2dc9682 | 2014-10-18 00:44:02 +0000 | [diff] [blame] | 4140 | if (VecTy) |
| 4141 | SliceTy = VecTy; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4142 | |
| 4143 | // Check for the case where we're going to rewrite to a new alloca of the |
| 4144 | // exact same type as the original, and with the same access offsets. In that |
| 4145 | // 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] | 4146 | // perform phi and select speculation. |
Hiroshi Inoue | 99a8faa | 2018-01-16 06:23:05 +0000 | [diff] [blame] | 4147 | // P.beginOffset() can be non-zero even with the same type in a case with |
| 4148 | // out-of-bounds access (e.g. @PR35657 function in SROA/basictest.ll). |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4149 | AllocaInst *NewAI; |
Hiroshi Inoue | 99a8faa | 2018-01-16 06:23:05 +0000 | [diff] [blame] | 4150 | if (SliceTy == AI.getAllocatedType() && P.beginOffset() == 0) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4151 | NewAI = &AI; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4152 | // FIXME: We should be able to bail at this point with "nothing changed". |
| 4153 | // FIXME: We might want to defer PHI speculation until after here. |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4154 | // FIXME: return nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4155 | } else { |
Guillaume Chatelet | e8a0a09 | 2019-10-25 22:26:23 +0200 | [diff] [blame] | 4156 | // If alignment is unspecified we fallback on the one required by the ABI |
| 4157 | // for this type. We also make sure the alignment is compatible with |
| 4158 | // P.beginOffset(). |
| 4159 | const Align Alignment = commonAlignment( |
| 4160 | DL.getValueOrABITypeAlignment(MaybeAlign(AI.getAlignment()), |
| 4161 | AI.getAllocatedType()), |
| 4162 | P.beginOffset()); |
Chandler Carruth | 903790e | 2012-09-29 10:41:21 +0000 | [diff] [blame] | 4163 | // If we will get at least this much alignment from the type alone, leave |
| 4164 | // the alloca's alignment unconstrained. |
Guillaume Chatelet | e8a0a09 | 2019-10-25 22:26:23 +0200 | [diff] [blame] | 4165 | const bool IsUnconstrained = Alignment <= DL.getABITypeAlignment(SliceTy); |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4166 | NewAI = new AllocaInst( |
Guillaume Chatelet | e8a0a09 | 2019-10-25 22:26:23 +0200 | [diff] [blame] | 4167 | SliceTy, AI.getType()->getAddressSpace(), nullptr, |
| 4168 | IsUnconstrained ? MaybeAlign() : Alignment, |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4169 | AI.getName() + ".sroa." + Twine(P.begin() - AS.begin()), &AI); |
Anastasis Grammenos | 425df22 | 2018-06-28 18:58:30 +0000 | [diff] [blame] | 4170 | // Copy the old AI debug location over to the new one. |
| 4171 | NewAI->setDebugLoc(AI.getDebugLoc()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4172 | ++NumNewAllocas; |
| 4173 | } |
| 4174 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4175 | LLVM_DEBUG(dbgs() << "Rewriting alloca partition " |
| 4176 | << "[" << P.beginOffset() << "," << P.endOffset() |
| 4177 | << ") to: " << *NewAI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4178 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4179 | // 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] | 4180 | // promoted allocas. We will reset it to this point if the alloca is not in |
| 4181 | // fact scheduled for promotion. |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4182 | unsigned PPWOldSize = PostPromotionWorklist.size(); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4183 | unsigned NumUses = 0; |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 4184 | SmallSetVector<PHINode *, 8> PHIUsers; |
| 4185 | SmallSetVector<SelectInst *, 8> SelectUsers; |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4186 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4187 | AllocaSliceRewriter Rewriter(DL, AS, *this, AI, *NewAI, P.beginOffset(), |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4188 | P.endOffset(), IsIntegerPromotable, VecTy, |
| 4189 | PHIUsers, SelectUsers); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4190 | bool Promotable = true; |
Chandler Carruth | ffb7ce5 | 2014-12-24 01:48:09 +0000 | [diff] [blame] | 4191 | for (Slice *S : P.splitSliceTails()) { |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4192 | Promotable &= Rewriter.visit(S); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4193 | ++NumUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4194 | } |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4195 | for (Slice &S : P) { |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4196 | Promotable &= Rewriter.visit(&S); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4197 | ++NumUses; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4200 | NumAllocaPartitionUses += NumUses; |
Craig Topper | 8a95027 | 2017-05-18 00:51:39 +0000 | [diff] [blame] | 4201 | MaxUsesPerAllocaPartition.updateMax(NumUses); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4202 | |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4203 | // Now that we've processed all the slices in the new partition, check if any |
| 4204 | // PHIs or Selects would block promotion. |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 4205 | for (PHINode *PHI : PHIUsers) |
| 4206 | if (!isSafePHIToSpeculate(*PHI)) { |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4207 | Promotable = false; |
| 4208 | PHIUsers.clear(); |
| 4209 | SelectUsers.clear(); |
Chandler Carruth | a8c4cc6 | 2014-02-25 09:45:27 +0000 | [diff] [blame] | 4210 | break; |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4211 | } |
Davide Italiano | 81a26da | 2017-04-27 23:09:01 +0000 | [diff] [blame] | 4212 | |
| 4213 | for (SelectInst *Sel : SelectUsers) |
| 4214 | if (!isSafeSelectToSpeculate(*Sel)) { |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4215 | Promotable = false; |
| 4216 | PHIUsers.clear(); |
| 4217 | SelectUsers.clear(); |
Chandler Carruth | a8c4cc6 | 2014-02-25 09:45:27 +0000 | [diff] [blame] | 4218 | break; |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | if (Promotable) { |
| 4222 | if (PHIUsers.empty() && SelectUsers.empty()) { |
| 4223 | // Promote the alloca. |
| 4224 | PromotableAllocas.push_back(NewAI); |
| 4225 | } else { |
| 4226 | // If we have either PHIs or Selects to speculate, add them to those |
| 4227 | // worklists and re-queue the new alloca so that we promote in on the |
| 4228 | // next iteration. |
Chandler Carruth | 6174704 | 2014-10-16 21:05:14 +0000 | [diff] [blame] | 4229 | for (PHINode *PHIUser : PHIUsers) |
| 4230 | SpeculatablePHIs.insert(PHIUser); |
| 4231 | for (SelectInst *SelectUser : SelectUsers) |
| 4232 | SpeculatableSelects.insert(SelectUser); |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4233 | Worklist.insert(NewAI); |
| 4234 | } |
| 4235 | } else { |
Chandler Carruth | 3bf18ed | 2014-02-25 00:07:09 +0000 | [diff] [blame] | 4236 | // Drop any post-promotion work items if promotion didn't happen. |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4237 | while (PostPromotionWorklist.size() > PPWOldSize) |
| 4238 | PostPromotionWorklist.pop_back(); |
David Majnemer | 30ffc4c | 2016-04-26 01:05:00 +0000 | [diff] [blame] | 4239 | |
| 4240 | // We couldn't promote and we didn't create a new partition, nothing |
| 4241 | // happened. |
| 4242 | if (NewAI == &AI) |
| 4243 | return nullptr; |
| 4244 | |
| 4245 | // If we can't promote the alloca, iterate on it to check for new |
| 4246 | // refinements exposed by splitting the current alloca. Don't iterate on an |
| 4247 | // alloca which didn't actually change and didn't get promoted. |
| 4248 | Worklist.insert(NewAI); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4249 | } |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4250 | |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4251 | return NewAI; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4254 | /// Walks the slices of an alloca and form partitions based on them, |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4255 | /// rewriting each of their uses. |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4256 | bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { |
| 4257 | if (AS.begin() == AS.end()) |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4258 | return false; |
| 4259 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4260 | unsigned NumPartitions = 0; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4261 | bool Changed = false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4262 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4263 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4264 | // First try to pre-split loads and stores. |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4265 | Changed |= presplitLoadsAndStores(AI, AS); |
| 4266 | |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4267 | // Now that we have identified any pre-splitting opportunities, |
| 4268 | // mark loads and stores unsplittable except for the following case. |
| 4269 | // We leave a slice splittable if all other slices are disjoint or fully |
| 4270 | // included in the slice, such as whole-alloca loads and stores. |
| 4271 | // If we fail to split these during pre-splitting, we want to force them |
| 4272 | // to be rewritten into a partition. |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4273 | bool IsSorted = true; |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4274 | |
| 4275 | uint64_t AllocaSize = DL.getTypeAllocSize(AI.getAllocatedType()); |
| 4276 | const uint64_t MaxBitVectorSize = 1024; |
Hiroshi Inoue | 99a8faa | 2018-01-16 06:23:05 +0000 | [diff] [blame] | 4277 | if (AllocaSize <= MaxBitVectorSize) { |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4278 | // If a byte boundary is included in any load or store, a slice starting or |
| 4279 | // ending at the boundary is not splittable. |
| 4280 | SmallBitVector SplittableOffset(AllocaSize + 1, true); |
| 4281 | for (Slice &S : AS) |
| 4282 | for (unsigned O = S.beginOffset() + 1; |
| 4283 | O < S.endOffset() && O < AllocaSize; O++) |
| 4284 | SplittableOffset.reset(O); |
| 4285 | |
| 4286 | for (Slice &S : AS) { |
| 4287 | if (!S.isSplittable()) |
| 4288 | continue; |
| 4289 | |
| 4290 | if ((S.beginOffset() > AllocaSize || SplittableOffset[S.beginOffset()]) && |
| 4291 | (S.endOffset() > AllocaSize || SplittableOffset[S.endOffset()])) |
| 4292 | continue; |
| 4293 | |
| 4294 | if (isa<LoadInst>(S.getUse()->getUser()) || |
| 4295 | isa<StoreInst>(S.getUse()->getUser())) { |
| 4296 | S.makeUnsplittable(); |
| 4297 | IsSorted = false; |
| 4298 | } |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } |
Hiroshi Inoue | 48e4c7a | 2017-12-01 06:05:05 +0000 | [diff] [blame] | 4301 | else { |
| 4302 | // We only allow whole-alloca splittable loads and stores |
| 4303 | // for a large alloca to avoid creating too large BitVector. |
| 4304 | for (Slice &S : AS) { |
| 4305 | if (!S.isSplittable()) |
| 4306 | continue; |
| 4307 | |
| 4308 | if (S.beginOffset() == 0 && S.endOffset() >= AllocaSize) |
| 4309 | continue; |
| 4310 | |
| 4311 | if (isa<LoadInst>(S.getUse()->getUser()) || |
| 4312 | isa<StoreInst>(S.getUse()->getUser())) { |
| 4313 | S.makeUnsplittable(); |
| 4314 | IsSorted = false; |
| 4315 | } |
| 4316 | } |
| 4317 | } |
| 4318 | |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4319 | if (!IsSorted) |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 4320 | llvm::sort(AS); |
Chandler Carruth | 24ac830 | 2015-01-02 03:55:54 +0000 | [diff] [blame] | 4321 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4322 | /// Describes the allocas introduced by rewritePartition in order to migrate |
| 4323 | /// the debug info. |
| 4324 | struct Fragment { |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4325 | AllocaInst *Alloca; |
| 4326 | uint64_t Offset; |
| 4327 | uint64_t Size; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4328 | Fragment(AllocaInst *AI, uint64_t O, uint64_t S) |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4329 | : Alloca(AI), Offset(O), Size(S) {} |
| 4330 | }; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4331 | SmallVector<Fragment, 4> Fragments; |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4332 | |
Chandler Carruth | 0715cba | 2015-01-01 11:54:38 +0000 | [diff] [blame] | 4333 | // Rewrite each partition. |
Chandler Carruth | e2f66ce | 2014-12-22 22:46:00 +0000 | [diff] [blame] | 4334 | for (auto &P : AS.partitions()) { |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4335 | if (AllocaInst *NewAI = rewritePartition(AI, AS, P)) { |
| 4336 | Changed = true; |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4337 | if (NewAI != &AI) { |
| 4338 | uint64_t SizeOfByte = 8; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4339 | uint64_t AllocaSize = DL.getTypeSizeInBits(NewAI->getAllocatedType()); |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4340 | // Don't include any padding. |
| 4341 | uint64_t Size = std::min(AllocaSize, P.size() * SizeOfByte); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4342 | Fragments.push_back(Fragment(NewAI, P.beginOffset() * SizeOfByte, Size)); |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4343 | } |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4344 | } |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4345 | ++NumPartitions; |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4346 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4347 | |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4348 | NumAllocaPartitions += NumPartitions; |
Craig Topper | 8a95027 | 2017-05-18 00:51:39 +0000 | [diff] [blame] | 4349 | MaxPartitionsPerAlloca.updateMax(NumPartitions); |
Chandler Carruth | 6c321c1 | 2013-07-19 10:57:36 +0000 | [diff] [blame] | 4350 | |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4351 | // Migrate debug information from the old alloca to the new alloca(s) |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 4352 | // and the individual partitions. |
Hsiangkai Wang | ef72e48 | 2018-08-06 03:59:47 +0000 | [diff] [blame] | 4353 | TinyPtrVector<DbgVariableIntrinsic *> DbgDeclares = FindDbgAddrUses(&AI); |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4354 | if (!DbgDeclares.empty()) { |
| 4355 | auto *Var = DbgDeclares.front()->getVariable(); |
| 4356 | auto *Expr = DbgDeclares.front()->getExpression(); |
Adrian Prantl | d7f6f16 | 2017-11-28 00:57:53 +0000 | [diff] [blame] | 4357 | auto VarSize = Var->getSizeInBits(); |
Sanjay Patel | af674fb | 2015-12-14 17:24:23 +0000 | [diff] [blame] | 4358 | DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false); |
Keno Fischer | d5354fd | 2016-01-14 20:06:34 +0000 | [diff] [blame] | 4359 | uint64_t AllocaSize = DL.getTypeSizeInBits(AI.getAllocatedType()); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4360 | for (auto Fragment : Fragments) { |
| 4361 | // Create a fragment expression describing the new partition or reuse AI's |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4362 | // expression if there is only one partition. |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4363 | auto *FragmentExpr = Expr; |
| 4364 | if (Fragment.Size < AllocaSize || Expr->isFragment()) { |
Adrian Prantl | 152ac39 | 2015-02-01 00:58:04 +0000 | [diff] [blame] | 4365 | // If this alloca is already a scalar replacement of a larger aggregate, |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4366 | // Fragment.Offset describes the offset inside the scalar. |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 4367 | auto ExprFragment = Expr->getFragmentInfo(); |
| 4368 | uint64_t Offset = ExprFragment ? ExprFragment->OffsetInBits : 0; |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4369 | uint64_t Start = Offset + Fragment.Offset; |
| 4370 | uint64_t Size = Fragment.Size; |
Adrian Prantl | 49797ca | 2016-12-22 05:27:12 +0000 | [diff] [blame] | 4371 | if (ExprFragment) { |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4372 | uint64_t AbsEnd = |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 4373 | ExprFragment->OffsetInBits + ExprFragment->SizeInBits; |
Adrian Prantl | 34e7590 | 2015-02-09 23:57:22 +0000 | [diff] [blame] | 4374 | if (Start >= AbsEnd) |
| 4375 | // No need to describe a SROAed padding. |
| 4376 | continue; |
| 4377 | Size = std::min(Size, AbsEnd - Start); |
| 4378 | } |
Adrian Prantl | b192b54 | 2017-08-30 20:04:17 +0000 | [diff] [blame] | 4379 | // The new, smaller fragment is stenciled out from the old fragment. |
| 4380 | if (auto OrigFragment = FragmentExpr->getFragmentInfo()) { |
| 4381 | assert(Start >= OrigFragment->OffsetInBits && |
| 4382 | "new fragment is outside of original fragment"); |
| 4383 | Start -= OrigFragment->OffsetInBits; |
| 4384 | } |
Adrian Prantl | 77d90b0 | 2017-11-28 21:30:38 +0000 | [diff] [blame] | 4385 | |
| 4386 | // The alloca may be larger than the variable. |
| 4387 | if (VarSize) { |
| 4388 | if (Size > *VarSize) |
| 4389 | Size = *VarSize; |
| 4390 | if (Size == 0 || Start + Size > *VarSize) |
| 4391 | continue; |
| 4392 | } |
| 4393 | |
Adrian Prantl | d7f6f16 | 2017-11-28 00:57:53 +0000 | [diff] [blame] | 4394 | // Avoid creating a fragment expression that covers the entire variable. |
| 4395 | if (!VarSize || *VarSize != Size) { |
| 4396 | if (auto E = |
| 4397 | DIExpression::createFragmentExpression(Expr, Start, Size)) |
| 4398 | FragmentExpr = *E; |
| 4399 | else |
| 4400 | continue; |
| 4401 | } |
Adrian Prantl | 152ac39 | 2015-02-01 00:58:04 +0000 | [diff] [blame] | 4402 | } |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4403 | |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4404 | // Remove any existing intrinsics describing the same alloca. |
Hsiangkai Wang | ef72e48 | 2018-08-06 03:59:47 +0000 | [diff] [blame] | 4405 | for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(Fragment.Alloca)) |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4406 | OldDII->eraseFromParent(); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4407 | |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 4408 | DIB.insertDeclare(Fragment.Alloca, Var, FragmentExpr, |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4409 | DbgDeclares.front()->getDebugLoc(), &AI); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4412 | return Changed; |
| 4413 | } |
| 4414 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4415 | /// 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] | 4416 | void SROA::clobberUse(Use &U) { |
| 4417 | Value *OldV = U; |
| 4418 | // Replace the use with an undef value. |
| 4419 | U = UndefValue::get(OldV->getType()); |
| 4420 | |
| 4421 | // Check for this making an instruction dead. We have to garbage collect |
| 4422 | // all the dead instructions to ensure the uses of any alloca end up being |
| 4423 | // minimal. |
| 4424 | if (Instruction *OldI = dyn_cast<Instruction>(OldV)) |
| 4425 | if (isInstructionTriviallyDead(OldI)) { |
| 4426 | DeadInsts.insert(OldI); |
| 4427 | } |
| 4428 | } |
| 4429 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4430 | /// Analyze an alloca for SROA. |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4431 | /// |
| 4432 | /// This analyzes the alloca to ensure we can reason about it, builds |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4433 | /// 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] | 4434 | /// rewritten as needed. |
| 4435 | bool SROA::runOnAlloca(AllocaInst &AI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4436 | LLVM_DEBUG(dbgs() << "SROA alloca: " << AI << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4437 | ++NumAllocasAnalyzed; |
| 4438 | |
| 4439 | // Special case dead allocas, as they're trivial. |
| 4440 | if (AI.use_empty()) { |
| 4441 | AI.eraseFromParent(); |
| 4442 | return true; |
| 4443 | } |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4444 | const DataLayout &DL = AI.getModule()->getDataLayout(); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4445 | |
| 4446 | // Skip alloca forms that this analysis can't handle. |
| 4447 | if (AI.isArrayAllocation() || !AI.getAllocatedType()->isSized() || |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4448 | DL.getTypeAllocSize(AI.getAllocatedType()) == 0) |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4449 | return false; |
| 4450 | |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 4451 | bool Changed = false; |
| 4452 | |
| 4453 | // First, split any FCA loads and stores touching this alloca to promote |
| 4454 | // better splitting and promotion opportunities. |
Tim Northover | 856628f | 2018-12-18 09:29:39 +0000 | [diff] [blame] | 4455 | AggLoadStoreRewriter AggRewriter(DL); |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 4456 | Changed |= AggRewriter.rewrite(AI); |
| 4457 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4458 | // Build the slices using a recursive instruction-visiting builder. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 4459 | AllocaSlices AS(DL, AI); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4460 | LLVM_DEBUG(AS.print(dbgs())); |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4461 | if (AS.isEscaped()) |
Chandler Carruth | 42cb9cb | 2012-09-18 12:57:43 +0000 | [diff] [blame] | 4462 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4463 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4464 | // 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] | 4465 | for (Instruction *DeadUser : AS.getDeadUsers()) { |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4466 | // Free up everything used by this instruction. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4467 | for (Use &DeadOp : DeadUser->operands()) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 4468 | clobberUse(DeadOp); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4469 | |
| 4470 | // Now replace the uses of this instruction. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4471 | DeadUser->replaceAllUsesWith(UndefValue::get(DeadUser->getType())); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4472 | |
| 4473 | // And mark it for deletion. |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4474 | DeadInsts.insert(DeadUser); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4475 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4476 | } |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4477 | for (Use *DeadOp : AS.getDeadOperands()) { |
Chandler Carruth | 57d4cae | 2014-10-16 20:42:08 +0000 | [diff] [blame] | 4478 | clobberUse(*DeadOp); |
Chandler Carruth | 1bf38c6 | 2014-01-19 12:16:54 +0000 | [diff] [blame] | 4479 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Chandler Carruth | 9f21fe1 | 2013-07-19 09:13:58 +0000 | [diff] [blame] | 4482 | // 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] | 4483 | if (AS.begin() == AS.end()) |
Chandler Carruth | e5b7a2c | 2012-10-05 01:29:09 +0000 | [diff] [blame] | 4484 | return Changed; |
| 4485 | |
Chandler Carruth | 8393406 | 2014-10-16 21:11:55 +0000 | [diff] [blame] | 4486 | Changed |= splitAlloca(AI, AS); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4487 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4488 | LLVM_DEBUG(dbgs() << " Speculating PHIs\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4489 | while (!SpeculatablePHIs.empty()) |
| 4490 | speculatePHINodeLoads(*SpeculatablePHIs.pop_back_val()); |
| 4491 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4492 | LLVM_DEBUG(dbgs() << " Speculating Selects\n"); |
Chandler Carruth | f054640 | 2013-07-18 07:15:00 +0000 | [diff] [blame] | 4493 | while (!SpeculatableSelects.empty()) |
| 4494 | speculateSelectInstLoads(*SpeculatableSelects.pop_back_val()); |
| 4495 | |
| 4496 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4499 | /// Delete the dead instructions accumulated in this run. |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 4500 | /// |
| 4501 | /// Recursively deletes the dead instructions we've accumulated. This is done |
| 4502 | /// at the very end to maximize locality of the recursive delete and to |
| 4503 | /// minimize the problems of invalidated instruction pointers as such pointers |
| 4504 | /// are used heavily in the intermediate stages of the algorithm. |
| 4505 | /// |
| 4506 | /// We also record the alloca instructions deleted here so that they aren't |
| 4507 | /// subsequently handed to mem2reg to promote. |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4508 | bool SROA::deleteDeadInstructions( |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 4509 | SmallPtrSetImpl<AllocaInst *> &DeletedAllocas) { |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4510 | bool Changed = false; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4511 | while (!DeadInsts.empty()) { |
| 4512 | Instruction *I = DeadInsts.pop_back_val(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4513 | LLVM_DEBUG(dbgs() << "Deleting dead instruction: " << *I << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4514 | |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4515 | // If the instruction is an alloca, find the possible dbg.declare connected |
| 4516 | // to it, and remove it too. We must do this before calling RAUW or we will |
| 4517 | // not be able to find it. |
| 4518 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 4519 | DeletedAllocas.insert(AI); |
Hsiangkai Wang | ef72e48 | 2018-08-06 03:59:47 +0000 | [diff] [blame] | 4520 | for (DbgVariableIntrinsic *OldDII : FindDbgAddrUses(AI)) |
Reid Kleckner | 0fe506b | 2017-09-21 19:52:03 +0000 | [diff] [blame] | 4521 | OldDII->eraseFromParent(); |
| 4522 | } |
| 4523 | |
Chandler Carruth | 58d0556 | 2012-10-25 04:37:07 +0000 | [diff] [blame] | 4524 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 4525 | |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 4526 | for (Use &Operand : I->operands()) |
| 4527 | if (Instruction *U = dyn_cast<Instruction>(Operand)) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4528 | // Zero out the operand and see if it becomes trivially dead. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 4529 | Operand = nullptr; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4530 | if (isInstructionTriviallyDead(U)) |
Chandler Carruth | 18db795 | 2012-11-20 01:12:50 +0000 | [diff] [blame] | 4531 | DeadInsts.insert(U); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4534 | ++NumDeleted; |
| 4535 | I->eraseFromParent(); |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4536 | Changed = true; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4537 | } |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4538 | return Changed; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 4541 | /// Promote the allocas, using the best available technique. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 4542 | /// |
| 4543 | /// This attempts to promote whatever allocas have been identified as viable in |
| 4544 | /// 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] | 4545 | /// This function returns whether any promotion occurred. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 4546 | bool SROA::promoteAllocas(Function &F) { |
| 4547 | if (PromotableAllocas.empty()) |
| 4548 | return false; |
| 4549 | |
| 4550 | NumPromoted += PromotableAllocas.size(); |
| 4551 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4552 | LLVM_DEBUG(dbgs() << "Promoting allocas with mem2reg...\n"); |
Davide Italiano | 612d5a9 | 2017-04-09 20:47:14 +0000 | [diff] [blame] | 4553 | PromoteMemToReg(PromotableAllocas, *DT, AC); |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 4554 | PromotableAllocas.clear(); |
| 4555 | return true; |
| 4556 | } |
| 4557 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4558 | PreservedAnalyses SROA::runImpl(Function &F, DominatorTree &RunDT, |
| 4559 | AssumptionCache &RunAC) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 4560 | LLVM_DEBUG(dbgs() << "SROA function: " << F.getName() << "\n"); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4561 | C = &F.getContext(); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4562 | DT = &RunDT; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4563 | AC = &RunAC; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4564 | |
| 4565 | BasicBlock &EntryBB = F.getEntryBlock(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 4566 | for (BasicBlock::iterator I = EntryBB.begin(), E = std::prev(EntryBB.end()); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4567 | I != E; ++I) { |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4568 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 4569 | Worklist.insert(AI); |
Adrian Prantl | 565cc18 | 2015-01-20 19:42:22 +0000 | [diff] [blame] | 4570 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4571 | |
| 4572 | bool Changed = false; |
Chandler Carruth | 19450da | 2012-09-14 10:26:38 +0000 | [diff] [blame] | 4573 | // A set of deleted alloca instruction pointers which should be removed from |
| 4574 | // the list of promotable allocas. |
| 4575 | SmallPtrSet<AllocaInst *, 4> DeletedAllocas; |
| 4576 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4577 | do { |
| 4578 | while (!Worklist.empty()) { |
| 4579 | Changed |= runOnAlloca(*Worklist.pop_back_val()); |
Teresa Johnson | 3309002 | 2017-11-20 18:33:38 +0000 | [diff] [blame] | 4580 | Changed |= deleteDeadInstructions(DeletedAllocas); |
Chandler Carruth | b09f0a3 | 2012-10-02 22:46:45 +0000 | [diff] [blame] | 4581 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4582 | // Remove the deleted allocas from various lists so that we don't try to |
| 4583 | // continue processing them. |
| 4584 | if (!DeletedAllocas.empty()) { |
Chandler Carruth | 113dc64 | 2014-12-20 02:39:18 +0000 | [diff] [blame] | 4585 | auto IsInSet = [&](AllocaInst *AI) { return DeletedAllocas.count(AI); }; |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 4586 | Worklist.remove_if(IsInSet); |
| 4587 | PostPromotionWorklist.remove_if(IsInSet); |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4588 | PromotableAllocas.erase(llvm::remove_if(PromotableAllocas, IsInSet), |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4589 | PromotableAllocas.end()); |
| 4590 | DeletedAllocas.clear(); |
| 4591 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4592 | } |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4593 | |
Chandler Carruth | ac8317f | 2012-10-04 12:33:50 +0000 | [diff] [blame] | 4594 | Changed |= promoteAllocas(F); |
| 4595 | |
| 4596 | Worklist = PostPromotionWorklist; |
| 4597 | PostPromotionWorklist.clear(); |
| 4598 | } while (!Worklist.empty()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4599 | |
Davide Italiano | 16e96d4 | 2016-06-07 13:21:17 +0000 | [diff] [blame] | 4600 | if (!Changed) |
| 4601 | return PreservedAnalyses::all(); |
| 4602 | |
Davide Italiano | 16e96d4 | 2016-06-07 13:21:17 +0000 | [diff] [blame] | 4603 | PreservedAnalyses PA; |
Chandler Carruth | ca68a3e | 2017-01-15 06:32:49 +0000 | [diff] [blame] | 4604 | PA.preserveSet<CFGAnalyses>(); |
Davide Italiano | 16e96d4 | 2016-06-07 13:21:17 +0000 | [diff] [blame] | 4605 | PA.preserve<GlobalsAA>(); |
| 4606 | return PA; |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 4609 | PreservedAnalyses SROA::run(Function &F, FunctionAnalysisManager &AM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4610 | return runImpl(F, AM.getResult<DominatorTreeAnalysis>(F), |
| 4611 | AM.getResult<AssumptionAnalysis>(F)); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 4612 | } |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4613 | |
| 4614 | /// A legacy pass for the legacy pass manager that wraps the \c SROA pass. |
| 4615 | /// |
| 4616 | /// This is in the llvm namespace purely to allow it to be a friend of the \c |
| 4617 | /// SROA pass. |
| 4618 | class llvm::sroa::SROALegacyPass : public FunctionPass { |
| 4619 | /// The SROA implementation. |
| 4620 | SROA Impl; |
| 4621 | |
| 4622 | public: |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4623 | static char ID; |
| 4624 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4625 | SROALegacyPass() : FunctionPass(ID) { |
| 4626 | initializeSROALegacyPassPass(*PassRegistry::getPassRegistry()); |
| 4627 | } |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4628 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4629 | bool runOnFunction(Function &F) override { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 4630 | if (skipFunction(F)) |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4631 | return false; |
| 4632 | |
| 4633 | auto PA = Impl.runImpl( |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4634 | F, getAnalysis<DominatorTreeWrapperPass>().getDomTree(), |
| 4635 | getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F)); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4636 | return !PA.areAllPreserved(); |
| 4637 | } |
Eugene Zelenko | 75075ef | 2017-09-01 21:37:29 +0000 | [diff] [blame] | 4638 | |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4639 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4640 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4641 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 4642 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 4643 | AU.setPreservesCFG(); |
| 4644 | } |
| 4645 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 4646 | StringRef getPassName() const override { return "SROA"; } |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4647 | }; |
| 4648 | |
| 4649 | char SROALegacyPass::ID = 0; |
| 4650 | |
| 4651 | FunctionPass *llvm::createSROAPass() { return new SROALegacyPass(); } |
| 4652 | |
| 4653 | INITIALIZE_PASS_BEGIN(SROALegacyPass, "sroa", |
| 4654 | "Scalar Replacement Of Aggregates", false, false) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 4655 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 4656 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 4657 | INITIALIZE_PASS_END(SROALegacyPass, "sroa", "Scalar Replacement Of Aggregates", |
| 4658 | false, false) |