Polly 22.0.0git
IRBuilder.h
Go to the documentation of this file.
1//===- Codegen/IRBuilder.h - The IR builder used by Polly -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The Polly IRBuilder file contains Polly specific extensions for the IRBuilder
10// that are used e.g. to emit the llvm.loop.parallel metadata.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef POLLY_CODEGEN_IRBUILDER_H
15#define POLLY_CODEGEN_IRBUILDER_H
16
17#include "llvm/ADT/MapVector.h"
18#include "llvm/IR/IRBuilder.h"
19
20namespace llvm {
21class Loop;
22class SCEV;
23class ScalarEvolution;
24} // namespace llvm
25
26namespace polly {
27class Scop;
28struct BandAttr;
29
30/// Helper class to annotate newly generated SCoPs with metadata.
31///
32/// The annotations are twofold:
33/// 1) Loops are stored in a stack-like structure in the order they are
34/// constructed and the LoopID metadata node is added to the backedge.
35/// Contained memory instructions and loop headers are annotated according
36/// to all parallel surrounding loops.
37/// 2) The new SCoP is assumed alias free (either due to the result of
38/// AliasAnalysis queries or runtime alias checks). We annotate therefore
39/// all memory instruction with alias scopes to indicate that fact to
40/// later optimizations.
41/// These alias scopes live in a new alias domain only used in this SCoP.
42/// Each base pointer has its own alias scope and is annotated to not
43/// alias with any access to different base pointers.
45public:
48
49 /// Build all alias scopes for the given SCoP.
50 void buildAliasScopes(Scop &S);
51
52 /// Add a new loop @p L which is parallel if @p IsParallel is true.
53 void pushLoop(llvm::Loop *L, bool IsParallel);
54
55 /// Remove the last added loop.
56 void popLoop(bool isParallel);
57
58 /// Annotate the new instruction @p I for all parallel loops.
59 void annotate(llvm::Instruction *I);
60
61 /// Annotate the loop latch @p B.
62 /// Last argument is optional, if no value is passed, we don't annotate
63 /// any vectorize metadata.
65 llvm::BranchInst *B, bool IsParallel,
66 std::optional<bool> EnableVectorizeMetadata = std::nullopt) const;
67
68 /// Add alternative alias based pointers
69 ///
70 /// When annotating instructions with alias scope metadata, the right metadata
71 /// is identified through the base pointer of the memory access. In some cases
72 /// (e.g. OpenMP code generation), the base pointer of the memory accesses is
73 /// not the original base pointer, but was changed when passing the original
74 /// base pointer over a function boundary. This function allows to provide a
75 /// map that maps from these new base pointers to the original base pointers
76 /// to allow the ScopAnnotator to still find the right alias scop annotations.
77 ///
78 /// @param NewMap A map from new base pointers to original base pointers.
80 llvm::DenseMap<llvm::AssertingVH<llvm::Value>,
81 llvm::AssertingVH<llvm::Value>> &NewMap) {
82 AlternativeAliasBases.insert_range(NewMap);
83 }
84
85 /// Delete the set of alternative alias bases
87
88 /// Stack for surrounding BandAttr annotations.
89 llvm::SmallVector<BandAttr *, 8> LoopAttrEnv;
90 BandAttr *&getStagingAttrEnv() { return LoopAttrEnv.back(); }
92 return LoopAttrEnv[LoopAttrEnv.size() - 2];
93 }
94
95private:
96 /// The ScalarEvolution analysis we use to find base pointers.
97 llvm::ScalarEvolution *SE;
98
99 /// All loops currently under construction.
100 llvm::SmallVector<llvm::Loop *, 8> ActiveLoops;
101
102 /// Access groups for the parallel loops currently under construction.
103 llvm::SmallVector<llvm::MDNode *, 8> ParallelLoops;
104
105 /// The alias scope domain for the current SCoP.
106 llvm::MDNode *AliasScopeDomain;
107
108 /// A map from base pointers to its alias scope.
109 llvm::MapVector<llvm::AssertingVH<llvm::Value>, llvm::MDNode *> AliasScopeMap;
110
111 /// A map from base pointers to an alias scope list of other pointers.
112 llvm::DenseMap<llvm::AssertingVH<llvm::Value>, llvm::MDNode *>
114
115 llvm::DenseMap<llvm::AssertingVH<llvm::Value>, llvm::AssertingVH<llvm::Value>>
117};
118
119/// Add Polly specifics when running IRBuilder.
120///
121/// This is used to add additional items such as e.g. the llvm.loop.parallel
122/// metadata.
123class IRInserter final : public llvm::IRBuilderDefaultInserter {
124public:
125 IRInserter() = default;
127
128 void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
129 llvm::BasicBlock::iterator InsertPt) const override {
130 llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
131 if (Annotator)
132 Annotator->annotate(I);
133 }
134
135private:
137};
138
139// TODO: We should not name instructions in NDEBUG builds.
140//
141// We currently always name instructions, as the polly test suite currently
142// matches for certain names.
143typedef llvm::IRBuilder<llvm::ConstantFolder, IRInserter> PollyIRBuilder;
144
145} // namespace polly
146#endif
ScopAnnotator * Annotator
Definition IRBuilder.h:136
IRInserter()=default
void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock::iterator InsertPt) const override
Definition IRBuilder.h:128
IRInserter(ScopAnnotator &A)
Definition IRBuilder.h:126
Helper class to annotate newly generated SCoPs with metadata.
Definition IRBuilder.h:44
llvm::SmallVector< BandAttr *, 8 > LoopAttrEnv
Stack for surrounding BandAttr annotations.
Definition IRBuilder.h:89
llvm::SmallVector< llvm::MDNode *, 8 > ParallelLoops
Access groups for the parallel loops currently under construction.
Definition IRBuilder.h:103
void resetAlternativeAliasBases()
Delete the set of alternative alias bases.
Definition IRBuilder.h:86
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::MDNode * > OtherAliasScopeListMap
A map from base pointers to an alias scope list of other pointers.
Definition IRBuilder.h:113
BandAttr *& getStagingAttrEnv()
Definition IRBuilder.h:90
void buildAliasScopes(Scop &S)
Build all alias scopes for the given SCoP.
Definition IRBuilder.cpp:59
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.
Definition IRBuilder.h:109
llvm::ScalarEvolution * SE
The ScalarEvolution analysis we use to find base pointers.
Definition IRBuilder.h:97
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > AlternativeAliasBases
Definition IRBuilder.h:116
void pushLoop(llvm::Loop *L, bool IsParallel)
Add a new loop L which is parallel if IsParallel is true.
void addAlternativeAliasBases(llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > &NewMap)
Add alternative alias based pointers.
Definition IRBuilder.h:79
llvm::SmallVector< llvm::Loop *, 8 > ActiveLoops
All loops currently under construction.
Definition IRBuilder.h:100
void popLoop(bool isParallel)
Remove the last added loop.
void annotateLoopLatch(llvm::BranchInst *B, bool IsParallel, std::optional< bool > EnableVectorizeMetadata=std::nullopt) const
Annotate the loop latch B.
BandAttr * getActiveAttrEnv() const
Definition IRBuilder.h:91
llvm::MDNode * AliasScopeDomain
The alias scope domain for the current SCoP.
Definition IRBuilder.h:106
Static Control Part.
Definition ScopInfo.h:1630
A()
B()
#define S(TYPE, NAME)
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
llvm::IRBuilder< llvm::ConstantFolder, IRInserter > PollyIRBuilder
Definition IRBuilder.h:143
Represent the attributes of a loop.
Definition ScopHelper.h:554