Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/include/polly/CodeGen/IRBuilder.h
Line
Count
Source
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/Analysis/LoopInfo.h"
19
#include "llvm/Analysis/ScalarEvolution.h"
20
#include "llvm/IR/IRBuilder.h"
21
#include "llvm/IR/ValueMap.h"
22
23
namespace llvm {
24
class ScalarEvolution;
25
} // namespace llvm
26
27
namespace polly {
28
class Scop;
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.
44
class ScopAnnotator {
45
public:
46
  ScopAnnotator();
47
48
  /// Build all alias scopes for the given SCoP.
49
  void buildAliasScopes(Scop &S);
50
51
  /// Add a new loop @p L which is parallel if @p IsParallel is true.
52
  void pushLoop(llvm::Loop *L, bool IsParallel);
53
54
  /// Remove the last added loop.
55
  void popLoop(bool isParallel);
56
57
  /// Annotate the new instruction @p I for all parallel loops.
58
  void annotate(llvm::Instruction *I);
59
60
  /// Annotate the loop latch @p B wrt. @p L.
61
  void annotateLoopLatch(llvm::BranchInst *B, llvm::Loop *L, bool IsParallel,
62
                         bool IsLoopVectorizerDisabled) const;
63
64
  /// Add alternative alias based pointers
65
  ///
66
  /// When annotating instructions with alias scope metadata, the right metadata
67
  /// is identified through the base pointer of the memory access. In some cases
68
  /// (e.g. OpenMP code generation), the base pointer of the memory accesses is
69
  /// not the original base pointer, but was changed when passing the original
70
  /// base pointer over a function boundary. This function allows to provide a
71
  /// map that maps from these new base pointers to the original base pointers
72
  /// to allow the ScopAnnotator to still find the right alias scop annotations.
73
  ///
74
  /// @param NewMap A map from new base pointers to original base pointers.
75
  void addAlternativeAliasBases(
76
      llvm::DenseMap<llvm::AssertingVH<llvm::Value>,
77
135
                     llvm::AssertingVH<llvm::Value>> &NewMap) {
78
135
    AlternativeAliasBases.insert(NewMap.begin(), NewMap.end());
79
135
  }
80
81
  /// Delete the set of alternative alias bases
82
30
  void resetAlternativeAliasBases() { AlternativeAliasBases.clear(); }
83
84
  /// Add inter iteration alias-free base pointer @p BasePtr.
85
  void addInterIterationAliasFreeBasePtr(llvm::Value *BasePtr);
86
87
private:
88
  /// Annotate with the second level alias metadata
89
  ///
90
  /// Annotate the instruction @p I with the second level alias metadata
91
  /// to distinguish the individual non-aliasing accesses that have inter
92
  /// iteration alias-free base pointers.
93
  ///
94
  /// @param I The instruction to be annotated.
95
  /// @param BasePtr The base pointer of @p I.
96
  void annotateSecondLevel(llvm::Instruction *I, llvm::Value *BasePtr);
97
98
  /// The ScalarEvolution analysis we use to find base pointers.
99
  llvm::ScalarEvolution *SE;
100
101
  /// All loops currently under construction.
102
  llvm::SmallVector<llvm::Loop *, 8> ActiveLoops;
103
104
  /// Metadata pointing to parallel loops currently under construction.
105
  llvm::SmallVector<llvm::MDNode *, 8> ParallelLoops;
106
107
  /// The alias scope domain for the current SCoP.
108
  llvm::MDNode *AliasScopeDomain;
109
110
  /// A map from base pointers to its alias scope.
111
  llvm::MapVector<llvm::AssertingVH<llvm::Value>, llvm::MDNode *> AliasScopeMap;
112
113
  /// A map from base pointers to an alias scope list of other pointers.
114
  llvm::DenseMap<llvm::AssertingVH<llvm::Value>, llvm::MDNode *>
115
      OtherAliasScopeListMap;
116
117
  /// A map from pointers to second level alias scopes.
118
  llvm::DenseMap<const llvm::SCEV *, llvm::MDNode *> SecondLevelAliasScopeMap;
119
120
  /// A map from pointers to second level alias scope list of other pointers.
121
  llvm::DenseMap<const llvm::SCEV *, llvm::MDNode *>
122
      SecondLevelOtherAliasScopeListMap;
123
124
  /// Inter iteration alias-free base pointers.
125
  llvm::SmallPtrSet<llvm::Value *, 4> InterIterationAliasFreeBasePtrs;
126
127
  llvm::DenseMap<llvm::AssertingVH<llvm::Value>, llvm::AssertingVH<llvm::Value>>
128
      AlternativeAliasBases;
129
};
130
131
/// Add Polly specifics when running IRBuilder.
132
///
133
/// This is used to add additional items such as e.g. the llvm.loop.parallel
134
/// metadata.
135
class IRInserter : protected llvm::IRBuilderDefaultInserter {
136
public:
137
298
  IRInserter() = default;
138
293
  IRInserter(class ScopAnnotator &A) : Annotator(&A) {}
139
140
protected:
141
  void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
142
                    llvm::BasicBlock *BB,
143
10.9k
                    llvm::BasicBlock::iterator InsertPt) const {
144
10.9k
    llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
145
10.9k
    if (Annotator)
146
9.86k
      Annotator->annotate(I);
147
10.9k
  }
148
149
private:
150
  class ScopAnnotator *Annotator = nullptr;
151
};
152
153
// TODO: We should not name instructions in NDEBUG builds.
154
//
155
// We currently always name instructions, as the polly test suite currently
156
// matches for certain names.
157
typedef llvm::IRBuilder<llvm::ConstantFolder, IRInserter> PollyIRBuilder;
158
159
/// Return an IR builder pointed before the @p BB terminator.
160
static inline PollyIRBuilder createPollyIRBuilder(llvm::BasicBlock *BB,
161
293
                                                  ScopAnnotator &LA) {
162
293
  PollyIRBuilder Builder(BB->getContext(), llvm::ConstantFolder(),
163
293
                         polly::IRInserter(LA));
164
293
  Builder.SetInsertPoint(BB->getTerminator());
165
293
  return Builder;
166
293
}
Unexecuted instantiation: BlockGenerators.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: IslAst.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: IslExprBuilder.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: IslNodeBuilder.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
CodeGeneration.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Line
Count
Source
161
293
                                                  ScopAnnotator &LA) {
162
293
  PollyIRBuilder Builder(BB->getContext(), llvm::ConstantFolder(),
163
293
                         polly::IRInserter(LA));
164
293
  Builder.SetInsertPoint(BB->getTerminator());
165
293
  return Builder;
166
293
}
Unexecuted instantiation: LoopGenerators.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: IRBuilder.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: Utils.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: RuntimeDebugBuilder.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: PerfMonitor.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: RegisterPasses.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
Unexecuted instantiation: ScheduleOptimizer.cpp:polly::createPollyIRBuilder(llvm::BasicBlock*, polly::ScopAnnotator&)
167
} // namespace polly
168
#endif