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 "polly/ScopInfo.h"
17 #include "polly/Support/ScopHelper.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/Support/Debug.h"
21 
22 using namespace llvm;
23 using namespace polly;
24 
25 static const int MaxArraysInAliasScops = 10;
26 
27 /// Get a self referencing id metadata node.
28 ///
29 /// The MDNode looks like this (if arg0/arg1 are not null):
30 ///
31 ///    '!n = metadata !{metadata !n, arg0, arg1}'
32 ///
33 /// @return The self referencing id metadata node.
34 static MDNode *getID(LLVMContext &Ctx, Metadata *arg0 = nullptr,
35                      Metadata *arg1 = nullptr) {
36   MDNode *ID;
37   SmallVector<Metadata *, 3> Args;
38   // Use a temporary node to safely create a unique pointer for the first arg.
39   auto TempNode = MDNode::getTemporary(Ctx, None);
40   // Reserve operand 0 for loop id self reference.
41   Args.push_back(TempNode.get());
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 
53 ScopAnnotator::ScopAnnotator() : SE(nullptr), AliasScopeDomain(nullptr) {}
54 
55 void ScopAnnotator::buildAliasScopes(Scop &S) {
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   // 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 
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.
74   if (Arrays.size() > MaxArraysInAliasScops)
75     return;
76 
77   std::string AliasScopeStr = "polly.alias.scope.";
78   for (const ScopArrayInfo *Array : Arrays) {
79     assert(Array->getBasePtr() && "Base pointer must be present");
80     AliasScopeMap[Array->getBasePtr()] =
81         getID(Ctx, AliasScopeDomain,
82               MDString::get(Ctx, (AliasScopeStr + Array->getName()).c_str()));
83   }
84 
85   for (const ScopArrayInfo *Array : Arrays) {
86     MDNode *AliasScopeList = MDNode::get(Ctx, {});
87     for (const auto &AliasScopePair : AliasScopeMap) {
88       if (Array->getBasePtr() == AliasScopePair.first)
89         continue;
90 
91       Metadata *Args = {AliasScopePair.second};
92       AliasScopeList =
93           MDNode::concatenate(AliasScopeList, MDNode::get(Ctx, Args));
94     }
95 
96     OtherAliasScopeListMap[Array->getBasePtr()] = AliasScopeList;
97   }
98 }
99 
100 void ScopAnnotator::pushLoop(Loop *L, bool IsParallel) {
101 
102   ActiveLoops.push_back(L);
103   if (!IsParallel)
104     return;
105 
106   BasicBlock *Header = L->getHeader();
107   MDNode *Id = getID(Header->getContext());
108   assert(Id->getOperand(0) == Id && "Expected Id to be a self-reference");
109   assert(Id->getNumOperands() == 1 && "Unexpected extra operands in Id");
110   MDNode *Ids = ParallelLoops.empty()
111                     ? Id
112                     : MDNode::concatenate(ParallelLoops.back(), Id);
113   ParallelLoops.push_back(Ids);
114 }
115 
116 void ScopAnnotator::popLoop(bool IsParallel) {
117   ActiveLoops.pop_back();
118   if (!IsParallel)
119     return;
120 
121   assert(!ParallelLoops.empty() && "Expected a parallel loop to pop");
122   ParallelLoops.pop_back();
123 }
124 
125 void ScopAnnotator::annotateLoopLatch(BranchInst *B, Loop *L, bool IsParallel,
126                                       bool IsLoopVectorizerDisabled) const {
127   MDNode *MData = nullptr;
128 
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);
146 }
147 
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) {
163   auto *PtrSCEV = SE->getSCEV(getMemAccInstPointerOperand(Inst));
164   auto *BasePtrSCEV = SE->getPointerBase(PtrSCEV);
165 
166   if (!PtrSCEV)
167     return;
168   auto SecondLevelAliasScope = SecondLevelAliasScopeMap.lookup(PtrSCEV);
169   auto SecondLevelOtherAliasScopeList =
170       SecondLevelOtherAliasScopeListMap.lookup(PtrSCEV);
171   if (!SecondLevelAliasScope) {
172     auto AliasScope = AliasScopeMap.lookup(BasePtr);
173     if (!AliasScope)
174       return;
175     LLVMContext &Ctx = SE->getContext();
176     SecondLevelAliasScope = getID(
177         Ctx, AliasScope, MDString::get(Ctx, "second level alias metadata"));
178     SecondLevelAliasScopeMap[PtrSCEV] = SecondLevelAliasScope;
179     Metadata *Args = {SecondLevelAliasScope};
180     auto SecondLevelBasePtrAliasScopeList =
181         SecondLevelAliasScopeMap.lookup(BasePtrSCEV);
182     SecondLevelAliasScopeMap[BasePtrSCEV] = MDNode::concatenate(
183         SecondLevelBasePtrAliasScopeList, MDNode::get(Ctx, Args));
184     auto OtherAliasScopeList = OtherAliasScopeListMap.lookup(BasePtr);
185     SecondLevelOtherAliasScopeList = MDNode::concatenate(
186         OtherAliasScopeList, SecondLevelBasePtrAliasScopeList);
187     SecondLevelOtherAliasScopeListMap[PtrSCEV] = SecondLevelOtherAliasScopeList;
188   }
189   Inst->setMetadata("alias.scope", SecondLevelAliasScope);
190   Inst->setMetadata("noalias", SecondLevelOtherAliasScopeList);
191 }
192 
193 void ScopAnnotator::annotate(Instruction *Inst) {
194   if (!Inst->mayReadOrWriteMemory())
195     return;
196 
197   if (!ParallelLoops.empty())
198     Inst->setMetadata("llvm.mem.parallel_loop_access", ParallelLoops.back());
199 
200   // TODO: Use the ScopArrayInfo once available here.
201   if (!AliasScopeDomain)
202     return;
203 
204   // Do not apply annotations on memory operations that take more than one
205   // pointer. It would be ambiguous to which pointer the annotation applies.
206   // FIXME: How can we specify annotations for all pointer arguments?
207   if (isa<CallInst>(Inst) && !isa<MemSetInst>(Inst))
208     return;
209 
210   auto *Ptr = getMemAccInstPointerOperand(Inst);
211   if (!Ptr)
212     return;
213 
214   auto *PtrSCEV = SE->getSCEV(Ptr);
215   auto *BaseSCEV = SE->getPointerBase(PtrSCEV);
216   auto *SU = dyn_cast<SCEVUnknown>(BaseSCEV);
217 
218   if (!SU)
219     return;
220 
221   auto *BasePtr = SU->getValue();
222 
223   if (!BasePtr)
224     return;
225 
226   auto AliasScope = AliasScopeMap.lookup(BasePtr);
227 
228   if (!AliasScope) {
229     BasePtr = AlternativeAliasBases.lookup(BasePtr);
230     if (!BasePtr)
231       return;
232 
233     AliasScope = AliasScopeMap.lookup(BasePtr);
234     if (!AliasScope)
235       return;
236   }
237 
238   assert(OtherAliasScopeListMap.count(BasePtr) &&
239          "BasePtr either expected in AliasScopeMap and OtherAlias...Map");
240   auto *OtherAliasScopeList = OtherAliasScopeListMap[BasePtr];
241 
242   if (InterIterationAliasFreeBasePtrs.count(BasePtr)) {
243     annotateSecondLevel(Inst, BasePtr);
244     return;
245   }
246 
247   Inst->setMetadata("alias.scope", AliasScope);
248   Inst->setMetadata("noalias", OtherAliasScopeList);
249 }
250 
251 void ScopAnnotator::addInterIterationAliasFreeBasePtr(llvm::Value *BasePtr) {
252   if (!BasePtr)
253     return;
254 
255   InterIterationAliasFreeBasePtrs.insert(BasePtr);
256 }
257