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 | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Metadata.h" |
| 19 | #include "llvm/Support/Debug.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace polly; |
| 23 | |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 24 | /// @brief Get a self referencing id metadata node. |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 25 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 26 | /// The MDNode looks like this (if arg0/arg1 are not null): |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 27 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 28 | /// '!n = metadata !{metadata !n, arg0, arg1}' |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 29 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 30 | /// @return The self referencing id metadata node. |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame] | 31 | static MDNode *getID(LLVMContext &Ctx, Metadata *arg0 = nullptr, |
| 32 | Metadata *arg1 = nullptr) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 33 | MDNode *ID; |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame] | 34 | SmallVector<Metadata *, 3> Args; |
David Peixotto | a481787 | 2014-11-07 21:44:18 +0000 | [diff] [blame] | 35 | // 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] | 36 | auto TempNode = MDNode::getTemporary(Ctx, None); |
David Peixotto | a481787 | 2014-11-07 21:44:18 +0000 | [diff] [blame] | 37 | // Reserve operand 0 for loop id self reference. |
Duncan P. N. Exon Smith | e566efe | 2015-01-19 21:31:48 +0000 | [diff] [blame] | 38 | Args.push_back(TempNode.get()); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 39 | |
| 40 | if (arg0) |
| 41 | Args.push_back(arg0); |
| 42 | if (arg1) |
| 43 | Args.push_back(arg1); |
| 44 | |
| 45 | ID = MDNode::get(Ctx, Args); |
| 46 | ID->replaceOperandWith(0, ID); |
| 47 | return ID; |
| 48 | } |
| 49 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 50 | ScopAnnotator::ScopAnnotator() : SE(nullptr), AliasScopeDomain(nullptr) {} |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 51 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 52 | void ScopAnnotator::buildAliasScopes(Scop &S) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 53 | SE = S.getSE(); |
| 54 | |
| 55 | LLVMContext &Ctx = SE->getContext(); |
| 56 | AliasScopeDomain = getID(Ctx, MDString::get(Ctx, "polly.alias.scope.domain")); |
| 57 | |
| 58 | AliasScopeMap.clear(); |
| 59 | OtherAliasScopeListMap.clear(); |
| 60 | |
| 61 | SetVector<Value *> BasePtrs; |
Tobias Grosser | 7c3bad5 | 2015-05-27 05:16:57 +0000 | [diff] [blame] | 62 | for (ScopStmt &Stmt : S) |
| 63 | for (MemoryAccess *MA : Stmt) |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 64 | BasePtrs.insert(MA->getBaseAddr()); |
| 65 | |
| 66 | std::string AliasScopeStr = "polly.alias.scope."; |
| 67 | for (Value *BasePtr : BasePtrs) |
| 68 | AliasScopeMap[BasePtr] = getID( |
| 69 | Ctx, AliasScopeDomain, |
| 70 | MDString::get(Ctx, (AliasScopeStr + BasePtr->getName()).str().c_str())); |
| 71 | |
| 72 | for (Value *BasePtr : BasePtrs) { |
| 73 | MDNode *AliasScopeList = MDNode::get(Ctx, {}); |
| 74 | for (const auto &AliasScopePair : AliasScopeMap) { |
| 75 | if (BasePtr == AliasScopePair.first) |
| 76 | continue; |
| 77 | |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame] | 78 | Metadata *Args = {AliasScopePair.second}; |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 79 | AliasScopeList = |
| 80 | MDNode::concatenate(AliasScopeList, MDNode::get(Ctx, Args)); |
| 81 | } |
| 82 | |
| 83 | OtherAliasScopeListMap[BasePtr] = AliasScopeList; |
| 84 | } |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 87 | void ScopAnnotator::pushLoop(Loop *L, bool IsParallel) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 88 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 89 | ActiveLoops.push_back(L); |
| 90 | if (!IsParallel) |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 91 | return; |
| 92 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 93 | BasicBlock *Header = L->getHeader(); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 94 | MDNode *Id = getID(Header->getContext()); |
Duncan P. N. Exon Smith | 16173b7 | 2014-12-07 21:12:10 +0000 | [diff] [blame] | 95 | assert(Id->getOperand(0) == Id && "Expected Id to be a self-reference"); |
| 96 | assert(Id->getNumOperands() == 1 && "Unexpected extra operands in Id"); |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 97 | MDNode *Ids = ParallelLoops.empty() |
Duncan P. N. Exon Smith | 16173b7 | 2014-12-07 21:12:10 +0000 | [diff] [blame] | 98 | ? Id |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 99 | : MDNode::concatenate(ParallelLoops.back(), Id); |
| 100 | ParallelLoops.push_back(Ids); |
| 101 | } |
| 102 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 103 | void ScopAnnotator::popLoop(bool IsParallel) { |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 104 | ActiveLoops.pop_back(); |
| 105 | if (!IsParallel) |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 106 | return; |
| 107 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 108 | assert(!ParallelLoops.empty() && "Expected a parallel loop to pop"); |
| 109 | ParallelLoops.pop_back(); |
| 110 | } |
| 111 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 112 | void ScopAnnotator::annotateLoopLatch(BranchInst *B, Loop *L, |
| 113 | bool IsParallel) const { |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 114 | if (!IsParallel) |
| 115 | return; |
| 116 | |
| 117 | assert(!ParallelLoops.empty() && "Expected a parallel loop to annotate"); |
| 118 | MDNode *Ids = ParallelLoops.back(); |
| 119 | MDNode *Id = cast<MDNode>(Ids->getOperand(Ids->getNumOperands() - 1)); |
| 120 | B->setMetadata("llvm.loop", Id); |
| 121 | } |
| 122 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 123 | void ScopAnnotator::annotate(Instruction *Inst) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 124 | if (!Inst->mayReadOrWriteMemory()) |
| 125 | return; |
| 126 | |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 127 | if (!ParallelLoops.empty()) |
| 128 | Inst->setMetadata("llvm.mem.parallel_loop_access", ParallelLoops.back()); |
| 129 | |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 130 | // TODO: Use the ScopArrayInfo once available here. |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 131 | if (!AliasScopeDomain) |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 132 | return; |
| 133 | |
Michael Kruse | 70131d3 | 2016-01-27 17:09:17 +0000 | [diff] [blame] | 134 | auto MemInst = MemAccInst::dyn_cast(Inst); |
| 135 | if (!MemInst) |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 136 | return; |
| 137 | |
Johannes Doerfert | a792098 | 2016-02-25 14:08:48 +0000 | [diff] [blame] | 138 | auto *Ptr = MemInst.getPointerOperand(); |
| 139 | if (!Ptr) |
| 140 | return; |
| 141 | |
| 142 | auto *PtrSCEV = SE->getSCEV(Ptr); |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 143 | auto *BaseSCEV = SE->getPointerBase(PtrSCEV); |
| 144 | auto *SU = dyn_cast<SCEVUnknown>(BaseSCEV); |
| 145 | |
| 146 | if (!SU) |
| 147 | return; |
| 148 | |
| 149 | auto *BasePtr = SU->getValue(); |
| 150 | |
| 151 | if (!BasePtr) |
| 152 | return; |
| 153 | |
Tobias Grosser | 6bab9f8 | 2015-10-07 13:35:20 +0000 | [diff] [blame] | 154 | auto AliasScope = AliasScopeMap.lookup(BasePtr); |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 155 | |
Tobias Grosser | 6bab9f8 | 2015-10-07 13:35:20 +0000 | [diff] [blame] | 156 | if (!AliasScope) { |
| 157 | BasePtr = AlternativeAliasBases.lookup(BasePtr); |
| 158 | if (!BasePtr) |
Tobias Grosser | d79cb03 | 2015-10-07 13:19:06 +0000 | [diff] [blame] | 159 | return; |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 160 | |
Tobias Grosser | 6bab9f8 | 2015-10-07 13:35:20 +0000 | [diff] [blame] | 161 | AliasScope = AliasScopeMap.lookup(BasePtr); |
| 162 | if (!AliasScope) |
Tobias Grosser | d79cb03 | 2015-10-07 13:19:06 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
Tobias Grosser | d79cb03 | 2015-10-07 13:19:06 +0000 | [diff] [blame] | 166 | assert(OtherAliasScopeListMap.count(BasePtr) && |
| 167 | "BasePtr either expected in AliasScopeMap and OtherAlias...Map"); |
Tobias Grosser | 73725b8 | 2015-10-07 13:19:03 +0000 | [diff] [blame] | 168 | auto *OtherAliasScopeList = OtherAliasScopeListMap[BasePtr]; |
| 169 | |
| 170 | Inst->setMetadata("alias.scope", AliasScope); |
| 171 | Inst->setMetadata("noalias", OtherAliasScopeList); |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 172 | } |