Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 1 | //===------ PollyIRBuilder.cpp --------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The Polly IRBuilder file contains Polly specific extensions for the IRBuilder |
| 11 | // that are used e.g. to emit the llvm.loop.parallel metadata. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "polly/CodeGen/IRBuilder.h" |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 16 | #include "polly/ScopInfo.h" |
| 17 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 4a07bbe | 2017-08-22 21:58:48 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Metadata.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace polly; |
| 24 | |
Tobias Grosser | 696a1ee | 2017-04-03 07:42:50 +0000 | [diff] [blame] | 25 | static const int MaxArraysInAliasScops = 10; |
| 26 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 +0000 | [diff] [blame] | 27 | /// Get a self referencing id metadata node. |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 28 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 29 | /// The MDNode looks like this (if arg0/arg1 are not null): |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 30 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 31 | /// '!n = metadata !{metadata !n, arg0, arg1}' |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 32 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 33 | /// @return The self referencing id metadata node. |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame] | 34 | static MDNode *getID(LLVMContext &Ctx, Metadata *arg0 = nullptr, |
| 35 | Metadata *arg1 = nullptr) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 36 | MDNode *ID; |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame] | 37 | SmallVector<Metadata *, 3> Args; |
David Peixotto | a481787 | 2014-11-07 21:44:18 +0000 | [diff] [blame] | 38 | // Use a temporary node to safely create a unique pointer for the first arg. |
Duncan P. N. Exon Smith | e566efe | 2015-01-19 21:31:48 +0000 | [diff] [blame] | 39 | auto TempNode = MDNode::getTemporary(Ctx, None); |
David Peixotto | a481787 | 2014-11-07 21:44:18 +0000 | [diff] [blame] | 40 | // Reserve operand 0 for loop id self reference. |
Duncan P. N. Exon Smith | e566efe | 2015-01-19 21:31:48 +0000 | [diff] [blame] | 41 | Args.push_back(TempNode.get()); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 42 | |
| 43 | if (arg0) |
| 44 | Args.push_back(arg0); |
| 45 | if (arg1) |
| 46 | Args.push_back(arg1); |
| 47 | |
| 48 | ID = MDNode::get(Ctx, Args); |
| 49 | ID->replaceOperandWith(0, ID); |
| 50 | return ID; |
| 51 | } |
| 52 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 53 | ScopAnnotator::ScopAnnotator() : SE(nullptr), AliasScopeDomain(nullptr) {} |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 54 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 55 | void ScopAnnotator::buildAliasScopes(Scop &S) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 56 | SE = S.getSE(); |
| 57 | |
| 58 | LLVMContext &Ctx = SE->getContext(); |
| 59 | AliasScopeDomain = getID(Ctx, MDString::get(Ctx, "polly.alias.scope.domain")); |
| 60 | |
| 61 | AliasScopeMap.clear(); |
| 62 | OtherAliasScopeListMap.clear(); |
| 63 | |
Tobias Grosser | 4a07bbe | 2017-08-22 21:58:48 +0000 | [diff] [blame] | 64 | // We are only interested in arrays, but no scalar references. Scalars should |
| 65 | // be handled easily by basicaa. |
| 66 | SmallVector<ScopArrayInfo *, 10> Arrays; |
| 67 | for (ScopArrayInfo *Array : S.arrays()) |
| 68 | if (Array->isArrayKind()) |
| 69 | Arrays.push_back(Array); |
| 70 | |
Tobias Grosser | 696a1ee | 2017-04-03 07:42:50 +0000 | [diff] [blame] | 71 | // The construction of alias scopes is quadratic in the number of arrays |
| 72 | // involved. In case of too many arrays, skip the construction of alias |
| 73 | // information to avoid quadratic increases in compile time and code size. |
Tobias Grosser | 4a07bbe | 2017-08-22 21:58:48 +0000 | [diff] [blame] | 74 | if (Arrays.size() > MaxArraysInAliasScops) |
Tobias Grosser | 696a1ee | 2017-04-03 07:42:50 +0000 | [diff] [blame] | 75 | return; |
| 76 | |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 77 | std::string AliasScopeStr = "polly.alias.scope."; |
Tobias Grosser | 4a07bbe | 2017-08-22 21:58:48 +0000 | [diff] [blame] | 78 | for (const ScopArrayInfo *Array : Arrays) { |
Michael Kruse | 214deb7 | 2017-06-19 10:19:29 +0000 | [diff] [blame] | 79 | assert(Array->getBasePtr() && "Base pointer must be present"); |
Tobias Grosser | 4553463 | 2017-02-09 09:34:42 +0000 | [diff] [blame] | 80 | AliasScopeMap[Array->getBasePtr()] = |
| 81 | getID(Ctx, AliasScopeDomain, |
| 82 | MDString::get(Ctx, (AliasScopeStr + Array->getName()).c_str())); |
Michael Kruse | 214deb7 | 2017-06-19 10:19:29 +0000 | [diff] [blame] | 83 | } |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 84 | |
Tobias Grosser | 4a07bbe | 2017-08-22 21:58:48 +0000 | [diff] [blame] | 85 | for (const ScopArrayInfo *Array : Arrays) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 86 | MDNode *AliasScopeList = MDNode::get(Ctx, {}); |
| 87 | for (const auto &AliasScopePair : AliasScopeMap) { |
Tobias Grosser | 4553463 | 2017-02-09 09:34:42 +0000 | [diff] [blame] | 88 | if (Array->getBasePtr() == AliasScopePair.first) |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 89 | continue; |
| 90 | |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame] | 91 | Metadata *Args = {AliasScopePair.second}; |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 92 | AliasScopeList = |
| 93 | MDNode::concatenate(AliasScopeList, MDNode::get(Ctx, Args)); |
| 94 | } |
| 95 | |
Tobias Grosser | 4553463 | 2017-02-09 09:34:42 +0000 | [diff] [blame] | 96 | OtherAliasScopeListMap[Array->getBasePtr()] = AliasScopeList; |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 97 | } |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 100 | void ScopAnnotator::pushLoop(Loop *L, bool IsParallel) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 101 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 102 | ActiveLoops.push_back(L); |
| 103 | if (!IsParallel) |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 104 | return; |
| 105 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 106 | BasicBlock *Header = L->getHeader(); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 107 | MDNode *Id = getID(Header->getContext()); |
Duncan P. N. Exon Smith | 16173b7 | 2014-12-07 21:12:10 +0000 | [diff] [blame] | 108 | assert(Id->getOperand(0) == Id && "Expected Id to be a self-reference"); |
| 109 | assert(Id->getNumOperands() == 1 && "Unexpected extra operands in Id"); |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 110 | MDNode *Ids = ParallelLoops.empty() |
Duncan P. N. Exon Smith | 16173b7 | 2014-12-07 21:12:10 +0000 | [diff] [blame] | 111 | ? Id |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 112 | : MDNode::concatenate(ParallelLoops.back(), Id); |
| 113 | ParallelLoops.push_back(Ids); |
| 114 | } |
| 115 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 116 | void ScopAnnotator::popLoop(bool IsParallel) { |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 117 | ActiveLoops.pop_back(); |
| 118 | if (!IsParallel) |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 119 | return; |
| 120 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 121 | assert(!ParallelLoops.empty() && "Expected a parallel loop to pop"); |
| 122 | ParallelLoops.pop_back(); |
| 123 | } |
| 124 | |
Roman Gareev | 0956a60 | 2017-08-22 17:38:46 +0000 | [diff] [blame] | 125 | void ScopAnnotator::annotateLoopLatch(BranchInst *B, Loop *L, bool IsParallel, |
| 126 | bool IsLoopVectorizerDisabled) const { |
| 127 | MDNode *MData = nullptr; |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 128 | |
Roman Gareev | 0956a60 | 2017-08-22 17:38:46 +0000 | [diff] [blame] | 129 | if (IsLoopVectorizerDisabled) { |
| 130 | SmallVector<Metadata *, 3> Args; |
| 131 | LLVMContext &Ctx = SE->getContext(); |
| 132 | Args.push_back(MDString::get(Ctx, "llvm.loop.vectorize.enable")); |
| 133 | auto *FalseValue = ConstantInt::get(Type::getInt1Ty(Ctx), 0); |
| 134 | Args.push_back(ValueAsMetadata::get(FalseValue)); |
| 135 | MData = MDNode::concatenate(MData, getID(Ctx, MDNode::get(Ctx, Args))); |
| 136 | } |
| 137 | |
| 138 | if (IsParallel) { |
| 139 | assert(!ParallelLoops.empty() && "Expected a parallel loop to annotate"); |
| 140 | MDNode *Ids = ParallelLoops.back(); |
| 141 | MDNode *Id = cast<MDNode>(Ids->getOperand(Ids->getNumOperands() - 1)); |
| 142 | MData = MDNode::concatenate(MData, Id); |
| 143 | } |
| 144 | |
| 145 | B->setMetadata("llvm.loop", MData); |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 148 | /// Get the pointer operand |
| 149 | /// |
| 150 | /// @param Inst The instruction to be analyzed. |
| 151 | /// @return the pointer operand in case @p Inst is a memory access |
| 152 | /// instruction and nullptr otherwise. |
| 153 | static llvm::Value *getMemAccInstPointerOperand(Instruction *Inst) { |
| 154 | auto MemInst = MemAccInst::dyn_cast(Inst); |
| 155 | if (!MemInst) |
| 156 | return nullptr; |
| 157 | |
| 158 | return MemInst.getPointerOperand(); |
| 159 | } |
| 160 | |
| 161 | void ScopAnnotator::annotateSecondLevel(llvm::Instruction *Inst, |
| 162 | llvm::Value *BasePtr) { |
Michael Kruse | 9f30537 | 2018-07-05 13:44:50 +0000 | [diff] [blame] | 163 | Value *Ptr = getMemAccInstPointerOperand(Inst); |
| 164 | if (!Ptr) |
| 165 | return; |
| 166 | |
| 167 | auto *PtrSCEV = SE->getSCEV(Ptr); |
Roman Gareev | 1563f03 | 2017-08-08 16:50:28 +0000 | [diff] [blame] | 168 | auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV); |
| 169 | |
Roman Gareev | 1563f03 | 2017-08-08 16:50:28 +0000 | [diff] [blame] | 170 | auto SecondLevelAliasScope = SecondLevelAliasScopeMap.lookup(PtrSCEV); |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 171 | auto SecondLevelOtherAliasScopeList = |
Roman Gareev | 1563f03 | 2017-08-08 16:50:28 +0000 | [diff] [blame] | 172 | SecondLevelOtherAliasScopeListMap.lookup(PtrSCEV); |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 173 | if (!SecondLevelAliasScope) { |
| 174 | auto AliasScope = AliasScopeMap.lookup(BasePtr); |
| 175 | if (!AliasScope) |
| 176 | return; |
| 177 | LLVMContext &Ctx = SE->getContext(); |
| 178 | SecondLevelAliasScope = getID( |
| 179 | Ctx, AliasScope, MDString::get(Ctx, "second level alias metadata")); |
Roman Gareev | 1563f03 | 2017-08-08 16:50:28 +0000 | [diff] [blame] | 180 | SecondLevelAliasScopeMap[PtrSCEV] = SecondLevelAliasScope; |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 181 | Metadata *Args = {SecondLevelAliasScope}; |
| 182 | auto SecondLevelBasePtrAliasScopeList = |
Roman Gareev | 1563f03 | 2017-08-08 16:50:28 +0000 | [diff] [blame] | 183 | SecondLevelAliasScopeMap.lookup(BasePtrSCEV); |
| 184 | SecondLevelAliasScopeMap[BasePtrSCEV] = MDNode::concatenate( |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 185 | SecondLevelBasePtrAliasScopeList, MDNode::get(Ctx, Args)); |
| 186 | auto OtherAliasScopeList = OtherAliasScopeListMap.lookup(BasePtr); |
| 187 | SecondLevelOtherAliasScopeList = MDNode::concatenate( |
| 188 | OtherAliasScopeList, SecondLevelBasePtrAliasScopeList); |
Roman Gareev | 1563f03 | 2017-08-08 16:50:28 +0000 | [diff] [blame] | 189 | SecondLevelOtherAliasScopeListMap[PtrSCEV] = SecondLevelOtherAliasScopeList; |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 190 | } |
| 191 | Inst->setMetadata("alias.scope", SecondLevelAliasScope); |
| 192 | Inst->setMetadata("noalias", SecondLevelOtherAliasScopeList); |
| 193 | } |
| 194 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 195 | void ScopAnnotator::annotate(Instruction *Inst) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 196 | if (!Inst->mayReadOrWriteMemory()) |
| 197 | return; |
| 198 | |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 199 | if (!ParallelLoops.empty()) |
| 200 | Inst->setMetadata("llvm.mem.parallel_loop_access", ParallelLoops.back()); |
| 201 | |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 202 | // TODO: Use the ScopArrayInfo once available here. |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 203 | if (!AliasScopeDomain) |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 204 | return; |
| 205 | |
Michael Kruse | 271deb1 | 2017-12-22 17:44:53 +0000 | [diff] [blame] | 206 | // Do not apply annotations on memory operations that take more than one |
| 207 | // pointer. It would be ambiguous to which pointer the annotation applies. |
| 208 | // FIXME: How can we specify annotations for all pointer arguments? |
| 209 | if (isa<CallInst>(Inst) && !isa<MemSetInst>(Inst)) |
| 210 | return; |
| 211 | |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 212 | auto *Ptr = getMemAccInstPointerOperand(Inst); |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 213 | if (!Ptr) |
| 214 | return; |
| 215 | |
| 216 | auto *PtrSCEV = SE->getSCEV(Ptr); |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 217 | auto *BaseSCEV = SE->getPointerBase(PtrSCEV); |
| 218 | auto *SU = dyn_cast<SCEVUnknown>(BaseSCEV); |
| 219 | |
| 220 | if (!SU) |
| 221 | return; |
| 222 | |
| 223 | auto *BasePtr = SU->getValue(); |
| 224 | |
| 225 | if (!BasePtr) |
| 226 | return; |
| 227 | |
Tobias Grosser | 6bab9f8 | 2015-10-07 13:35:20 +0000 | [diff] [blame] | 228 | auto AliasScope = AliasScopeMap.lookup(BasePtr); |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 229 | |
Tobias Grosser | 6bab9f8 | 2015-10-07 13:35:20 +0000 | [diff] [blame] | 230 | if (!AliasScope) { |
| 231 | BasePtr = AlternativeAliasBases.lookup(BasePtr); |
| 232 | if (!BasePtr) |
Tobias Grosser | d79cb03 | 2015-10-07 13:19:06 +0000 | [diff] [blame] | 233 | return; |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 234 | |
Tobias Grosser | 6bab9f8 | 2015-10-07 13:35:20 +0000 | [diff] [blame] | 235 | AliasScope = AliasScopeMap.lookup(BasePtr); |
| 236 | if (!AliasScope) |
Tobias Grosser | d79cb03 | 2015-10-07 13:19:06 +0000 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
Tobias Grosser | d79cb03 | 2015-10-07 13:19:06 +0000 | [diff] [blame] | 240 | assert(OtherAliasScopeListMap.count(BasePtr) && |
| 241 | "BasePtr either expected in AliasScopeMap and OtherAlias...Map"); |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 242 | auto *OtherAliasScopeList = OtherAliasScopeListMap[BasePtr]; |
| 243 | |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 244 | if (InterIterationAliasFreeBasePtrs.count(BasePtr)) { |
| 245 | annotateSecondLevel(Inst, BasePtr); |
| 246 | return; |
| 247 | } |
| 248 | |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 249 | Inst->setMetadata("alias.scope", AliasScope); |
| 250 | Inst->setMetadata("noalias", OtherAliasScopeList); |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 251 | } |
Roman Gareev | cdfb57d | 2017-03-22 14:25:24 +0000 | [diff] [blame] | 252 | |
| 253 | void ScopAnnotator::addInterIterationAliasFreeBasePtr(llvm::Value *BasePtr) { |
| 254 | if (!BasePtr) |
| 255 | return; |
| 256 | |
| 257 | InterIterationAliasFreeBasePtrs.insert(BasePtr); |
| 258 | } |