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 | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 16 | |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 17 | #include "polly/ScopInfo.h" |
| 18 | #include "polly/Support/ScopHelper.h" |
| 19 | |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Metadata.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | using namespace polly; |
| 25 | |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 26 | /// @brief Get a self referencing id metadata node. |
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 | /// The MDNode looks like this (if arg0/arg1 are not null): |
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 | /// '!n = metadata !{metadata !n, arg0, arg1}' |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 31 | /// |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 32 | /// @return The self referencing id metadata node. |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame^] | 33 | static MDNode *getID(LLVMContext &Ctx, Metadata *arg0 = nullptr, |
| 34 | Metadata *arg1 = nullptr) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 35 | MDNode *ID; |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame^] | 36 | SmallVector<Metadata *, 3> Args; |
David Peixotto | a481787 | 2014-11-07 21:44:18 +0000 | [diff] [blame] | 37 | // Use a temporary node to safely create a unique pointer for the first arg. |
| 38 | MDNode *TempNode = MDNode::getTemporary(Ctx, None); |
| 39 | // Reserve operand 0 for loop id self reference. |
| 40 | Args.push_back(TempNode); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 41 | |
| 42 | if (arg0) |
| 43 | Args.push_back(arg0); |
| 44 | if (arg1) |
| 45 | Args.push_back(arg1); |
| 46 | |
| 47 | ID = MDNode::get(Ctx, Args); |
| 48 | ID->replaceOperandWith(0, ID); |
David Peixotto | a481787 | 2014-11-07 21:44:18 +0000 | [diff] [blame] | 49 | MDNode::deleteTemporary(TempNode); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 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 | |
| 64 | SetVector<Value *> BasePtrs; |
| 65 | for (ScopStmt *Stmt : S) |
| 66 | for (MemoryAccess *MA : *Stmt) |
| 67 | BasePtrs.insert(MA->getBaseAddr()); |
| 68 | |
| 69 | std::string AliasScopeStr = "polly.alias.scope."; |
| 70 | for (Value *BasePtr : BasePtrs) |
| 71 | AliasScopeMap[BasePtr] = getID( |
| 72 | Ctx, AliasScopeDomain, |
| 73 | MDString::get(Ctx, (AliasScopeStr + BasePtr->getName()).str().c_str())); |
| 74 | |
| 75 | for (Value *BasePtr : BasePtrs) { |
| 76 | MDNode *AliasScopeList = MDNode::get(Ctx, {}); |
| 77 | for (const auto &AliasScopePair : AliasScopeMap) { |
| 78 | if (BasePtr == AliasScopePair.first) |
| 79 | continue; |
| 80 | |
Tobias Grosser | bd8f3c1 | 2014-12-09 22:02:16 +0000 | [diff] [blame^] | 81 | Metadata *Args = {AliasScopePair.second}; |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 82 | AliasScopeList = |
| 83 | MDNode::concatenate(AliasScopeList, MDNode::get(Ctx, Args)); |
| 84 | } |
| 85 | |
| 86 | OtherAliasScopeListMap[BasePtr] = AliasScopeList; |
| 87 | } |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 90 | void ScopAnnotator::pushLoop(Loop *L, bool IsParallel) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 91 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 92 | ActiveLoops.push_back(L); |
| 93 | if (!IsParallel) |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 94 | return; |
| 95 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 96 | BasicBlock *Header = L->getHeader(); |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 97 | MDNode *Id = getID(Header->getContext()); |
Duncan P. N. Exon Smith | 16173b7 | 2014-12-07 21:12:10 +0000 | [diff] [blame] | 98 | assert(Id->getOperand(0) == Id && "Expected Id to be a self-reference"); |
| 99 | assert(Id->getNumOperands() == 1 && "Unexpected extra operands in Id"); |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 100 | MDNode *Ids = ParallelLoops.empty() |
Duncan P. N. Exon Smith | 16173b7 | 2014-12-07 21:12:10 +0000 | [diff] [blame] | 101 | ? Id |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 102 | : MDNode::concatenate(ParallelLoops.back(), Id); |
| 103 | ParallelLoops.push_back(Ids); |
| 104 | } |
| 105 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 106 | void ScopAnnotator::popLoop(bool IsParallel) { |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 107 | ActiveLoops.pop_back(); |
| 108 | if (!IsParallel) |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 109 | return; |
| 110 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 111 | assert(!ParallelLoops.empty() && "Expected a parallel loop to pop"); |
| 112 | ParallelLoops.pop_back(); |
| 113 | } |
| 114 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 115 | void ScopAnnotator::annotateLoopLatch(BranchInst *B, Loop *L, |
| 116 | bool IsParallel) const { |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 117 | if (!IsParallel) |
| 118 | return; |
| 119 | |
| 120 | assert(!ParallelLoops.empty() && "Expected a parallel loop to annotate"); |
| 121 | MDNode *Ids = ParallelLoops.back(); |
| 122 | MDNode *Id = cast<MDNode>(Ids->getOperand(Ids->getNumOperands() - 1)); |
| 123 | B->setMetadata("llvm.loop", Id); |
| 124 | } |
| 125 | |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 126 | void ScopAnnotator::annotate(Instruction *Inst) { |
Johannes Doerfert | ecdf263 | 2014-10-02 15:31:24 +0000 | [diff] [blame] | 127 | if (!Inst->mayReadOrWriteMemory()) |
| 128 | return; |
| 129 | |
| 130 | // TODO: Use the ScopArrayInfo once available here. |
| 131 | if (AliasScopeDomain) { |
| 132 | Value *BasePtr = nullptr; |
| 133 | if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst)) { |
| 134 | const SCEV *PtrSCEV = SE->getSCEV(getPointerOperand(*Inst)); |
| 135 | const SCEV *BaseSCEV = SE->getPointerBase(PtrSCEV); |
| 136 | if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(BaseSCEV)) |
| 137 | BasePtr = SU->getValue(); |
| 138 | } |
| 139 | |
| 140 | if (BasePtr) { |
| 141 | Inst->setMetadata("alias.scope", AliasScopeMap[BasePtr]); |
| 142 | Inst->setMetadata("noalias", OtherAliasScopeListMap[BasePtr]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (ParallelLoops.empty()) |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 147 | return; |
| 148 | |
| 149 | Inst->setMetadata("llvm.mem.parallel_loop_access", ParallelLoops.back()); |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 150 | } |