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"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/IR/Metadata.h"
18 #include "llvm/Support/Debug.h"
19 
20 using namespace llvm;
21 using namespace polly;
22 
23 llvm::MDNode *polly::PollyLoopInfo::GetLoopID() const {
24   if (LoopID)
25     return LoopID;
26 
27   llvm::Value *Args[] = {0};
28   LoopID = llvm::MDNode::get(Header->getContext(), Args);
29   LoopID->replaceOperandWith(0, LoopID);
30   return LoopID;
31 }
32 
33 void polly::LoopAnnotator::Begin(llvm::BasicBlock *Header) {
34   Active.push_back(PollyLoopInfo(Header));
35 }
36 
37 void polly::LoopAnnotator::End() { Active.pop_back(); }
38 
39 void polly::LoopAnnotator::SetCurrentParallel() {
40   Active.back().SetParallel(true);
41 }
42 
43 void polly::LoopAnnotator::Annotate(llvm::Instruction *Inst) {
44   if (Active.empty())
45     return;
46 
47   const PollyLoopInfo &L = Active.back();
48   if (!L.IsParallel())
49     return;
50 
51   if (TerminatorInst *TI = dyn_cast<llvm::TerminatorInst>(Inst)) {
52     for (unsigned i = 0, ie = TI->getNumSuccessors(); i != ie; ++i)
53       if (TI->getSuccessor(i) == L.GetHeader()) {
54         TI->setMetadata("llvm.loop", L.GetLoopID());
55         break;
56       }
57   } else if (Inst->mayReadOrWriteMemory()) {
58     Inst->setMetadata("llvm.mem.parallel_loop_access", L.GetLoopID());
59   }
60 }
61