17#include "llvm/ADT/SmallVector.h"
18#include "llvm/IR/Metadata.h"
32static MDNode *
getID(LLVMContext &
Ctx, Metadata *arg0 =
nullptr,
33 Metadata *
arg1 =
nullptr) {
35 SmallVector<Metadata *, 3> Args;
37 Args.push_back(
nullptr);
44 ID = MDNode::getDistinct(
Ctx, Args);
45 ID->replaceOperandWith(0, ID);
62 LLVMContext &
Ctx =
SE->getContext();
64 getID(
Ctx, ConstantAsMetadata::get(ConstantInt::getFalse(
Ctx)),
65 MDString::get(
Ctx,
"polly.alias.scope.domain"));
72 SmallVector<ScopArrayInfo *, 10> Arrays;
74 if (
Array->isArrayKind())
75 Arrays.push_back(
Array);
83 std::string AliasScopeStr =
"polly.alias.scope.";
85 assert(
Array->getBasePtr() &&
"Base pointer must be present");
88 MDString::get(
Ctx, (AliasScopeStr +
Array->getName()).c_str()));
92 MDNode *AliasScopeList = MDNode::get(
Ctx, {});
94 if (
Array->getBasePtr() == AliasScopePair.first)
97 Metadata *Args = {AliasScopePair.second};
99 MDNode::concatenate(AliasScopeList, MDNode::get(
Ctx, Args));
110 LLVMContext &
Ctx =
SE->getContext();
111 MDNode *AccessGroup = MDNode::getDistinct(
Ctx, {});
134 SmallVector<Metadata *, 3> *Args,
135 bool EnableLoopVectorizer) {
137 MDString::get(
Ctx, EnableLoopVectorizer ?
"llvm.loop.vectorize.enable"
138 :
"llvm.loop.vectorize.disable");
139 Args->push_back(MDNode::get(
Ctx, {PropName}));
143 llvm::SmallVector<llvm::MDNode *, 8> ParallelLoops) {
144 MDString *PropName = MDString::get(
Ctx,
"llvm.loop.parallel_accesses");
145 MDNode *AccGroup = ParallelLoops.back();
146 Args->push_back(MDNode::get(
Ctx, {PropName, AccGroup}));
150 CondBrInst *
B,
bool IsParallel,
151 std::optional<bool> EnableVectorizeMetadata)
const {
152 LLVMContext &
Ctx =
SE->getContext();
153 SmallVector<Metadata *, 3> Args;
156 Args.push_back(
nullptr);
161 MDNode *MData =
nullptr;
163 MData = AttrEnv->Metadata;
165 llvm::append_range(Args, drop_begin(MData->operands(), 1));
169 if (EnableVectorizeMetadata.has_value())
173 if (!MData && Args.size() <= 1)
179 if (!MData || Args.size() > MData->getNumOperands()) {
180 MData = MDNode::getDistinct(
Ctx, Args);
181 MData->replaceOperandWith(0, MData);
183 B->setMetadata(LLVMContext::MD_loop, MData);
196 return MemInst.getPointerOperand();
206 if (
auto *Gep = dyn_cast<GEPOperator>(Val)) {
207 Val = Gep->getPointerOperand();
210 if (
auto *Cast = dyn_cast<BitCastOperator>(Val)) {
211 Val = Cast->getOperand(0);
222 if (!Inst->mayReadOrWriteMemory())
231 Inst->setMetadata(LLVMContext::MD_access_group,
236 Inst->setMetadata(LLVMContext::MD_access_group,
237 MDNode::get(
SE->getContext(),
238 ArrayRef<Metadata *>(
251 if (isa<CallInst>(Inst) && !isa<MemSetInst>(Inst))
275 "BasePtr either expected in AliasScopeMap and OtherAlias...Map");
278 Inst->setMetadata(
"alias.scope", MDNode::get(
SE->getContext(), AliasScope));
279 Inst->setMetadata(
"noalias", OtherAliasScopeList);
static MDNode * getID(LLVMContext &Ctx, Metadata *arg0=nullptr, Metadata *arg1=nullptr)
Get a self referencing id metadata node.
static void addVectorizeMetadata(LLVMContext &Ctx, SmallVector< Metadata *, 3 > *Args, bool EnableLoopVectorizer)
void addParallelMetadata(LLVMContext &Ctx, SmallVector< Metadata *, 3 > *Args, llvm::SmallVector< llvm::MDNode *, 8 > ParallelLoops)
static Value * findBasePtr(Value *Val)
Find the base pointer of an array access.
static llvm::Value * getMemAccInstPointerOperand(Instruction *Inst)
Get the pointer operand.
static const int MaxArraysInAliasScops
static MemAccInst dyn_cast(llvm::Value &V)
llvm::SmallVector< BandAttr *, 8 > LoopAttrEnv
Stack for surrounding BandAttr annotations.
llvm::SmallVector< llvm::MDNode *, 8 > ParallelLoops
Access groups for the parallel loops currently under construction.
void annotateLoopLatch(llvm::CondBrInst *B, bool IsParallel, std::optional< bool > EnableVectorizeMetadata=std::nullopt) const
Annotate the loop latch B.
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::MDNode * > OtherAliasScopeListMap
A map from base pointers to an alias scope list of other pointers.
BandAttr *& getStagingAttrEnv()
void buildAliasScopes(Scop &S)
Build all alias scopes for the given SCoP.
void annotate(llvm::Instruction *I)
Annotate the new instruction I for all parallel loops.
llvm::MapVector< llvm::AssertingVH< llvm::Value >, llvm::MDNode * > AliasScopeMap
A map from base pointers to its alias scope.
llvm::ScalarEvolution * SE
The ScalarEvolution analysis we use to find base pointers.
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > AlternativeAliasBases
void pushLoop(llvm::Loop *L, bool IsParallel)
Add a new loop L which is parallel if IsParallel is true.
llvm::SmallVector< llvm::Loop *, 8 > ActiveLoops
All loops currently under construction.
void popLoop(bool isParallel)
Remove the last added loop.
BandAttr * getActiveAttrEnv() const
llvm::MDNode * AliasScopeDomain
The alias scope domain for the current SCoP.
A class to store information about arrays in the SCoP.
@ Array
MemoryKind::Array: Models a one or multi-dimensional array.
@ Value
MemoryKind::Value: Models an llvm::Value.
Represent the attributes of a loop.