1 //===-------- polly/TempScopInfo.h - Extract TempScops ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect information about the control flow regions detected by the Scop
11 // detection, such that this information can be translated info its polyhedral
14 //===----------------------------------------------------------------------===//
16 #ifndef POLLY_TEMP_SCOP_EXTRACTION_H
17 #define POLLY_TEMP_SCOP_EXTRACTION_H
19 #include "polly/MayAliasSet.h"
20 #include "polly/ScopDetection.h"
22 #include "llvm/Analysis/RegionPass.h"
23 #include "llvm/IR/Instructions.h"
32 class MayAliasSetInfo
;
34 //===---------------------------------------------------------------------===//
35 /// @brief A memory access described by a SCEV expression and the access type.
38 const Value
*BaseAddress
;
42 // The type of the scev affine function
43 enum TypeKind
{ READ
, WRITE
};
51 explicit IRAccess (TypeKind Type
, const Value
*BaseAddress
,
52 const SCEV
*Offset
, unsigned elemBytes
, bool Affine
)
53 : BaseAddress(BaseAddress
), Offset(Offset
),
54 ElemBytes(elemBytes
), Type(Type
), IsAffine(Affine
) {}
56 enum TypeKind
getType() const { return Type
; }
58 const Value
*getBase() const { return BaseAddress
; }
60 const SCEV
*getOffset() const { return Offset
; }
62 unsigned getElemSizeInBytes() const { return ElemBytes
; }
64 bool isAffine() const { return IsAffine
; }
66 bool isRead() const { return Type
== READ
; }
68 bool isWrite() const { return Type
== WRITE
; }
76 ICmpInst::Predicate Pred
;
79 Comparison(const SCEV
*LHS
, const SCEV
*RHS
, ICmpInst::Predicate Pred
)
80 : LHS(LHS
), RHS(RHS
), Pred(Pred
) {}
82 const SCEV
*getLHS() const { return LHS
; }
83 const SCEV
*getRHS() const { return RHS
; }
85 ICmpInst::Predicate
getPred() const { return Pred
; }
86 void print(raw_ostream
&OS
) const;
89 //===---------------------------------------------------------------------===//
91 // The condition of a Basicblock, combine brcond with "And" operator.
92 typedef SmallVector
<Comparison
, 4> BBCond
;
94 /// Maps from a loop to the affine function expressing its backedge taken count.
95 /// The backedge taken count already enough to express iteration domain as we
96 /// only allow loops with canonical induction variable.
97 /// A canonical induction variable is:
98 /// an integer recurrence that starts at 0 and increments by one each time
100 typedef std::map
<const Loop
*, const SCEV
*> LoopBoundMapType
;
102 /// Mapping BBs to its condition constrains
103 typedef std::map
<const BasicBlock
*, BBCond
> BBCondMapType
;
105 typedef std::vector
<std::pair
<IRAccess
, Instruction
*> > AccFuncSetType
;
106 typedef std::map
<const BasicBlock
*, AccFuncSetType
> AccFuncMapType
;
108 //===---------------------------------------------------------------------===//
109 /// @brief Scop represent with llvm objects.
111 /// A helper class for remembering the parameter number and the max depth in
112 /// this Scop, and others context.
117 // The max loop depth of this Scop
118 unsigned MaxLoopDepth
;
120 // Remember the bounds of loops, to help us build iteration domain of BBs.
121 const LoopBoundMapType
&LoopBounds
;
122 const BBCondMapType
&BBConds
;
124 // Access function of bbs.
125 const AccFuncMapType
&AccFuncMap
;
127 // The alias information about this SCoP.
128 MayAliasSetInfo
*MayASInfo
;
130 friend class TempScopInfo
;
132 explicit TempScop(Region
&r
, LoopBoundMapType
&loopBounds
,
133 BBCondMapType
&BBCmps
, AccFuncMapType
&accFuncMap
)
134 : R(r
), MaxLoopDepth(0), LoopBounds(loopBounds
), BBConds(BBCmps
),
135 AccFuncMap(accFuncMap
), MayASInfo(new MayAliasSetInfo()) {}
140 /// @brief Get the maximum Region contained by this Scop.
142 /// @return The maximum Region contained by this Scop.
143 Region
&getMaxRegion() const { return R
; }
145 /// @brief Get the maximum loop depth of Region R.
147 /// @return The maximum loop depth of Region R.
148 unsigned getMaxLoopDepth() const { return MaxLoopDepth
; }
150 /// @brief Get the loop bounds of the given loop.
152 /// @param L The loop to get the bounds.
154 /// @return The bounds of the loop L in { Lower bound, Upper bound } form.
156 const SCEV
*getLoopBound(const Loop
*L
) const {
157 LoopBoundMapType::const_iterator at
= LoopBounds
.find(L
);
158 assert(at
!= LoopBounds
.end() && "Only valid loop is allow!");
162 /// @brief Get the condition from entry block of the Scop to a BasicBlock
164 /// @param BB The BasicBlock
166 /// @return The condition from entry block of the Scop to a BB
168 const BBCond
*getBBCond(const BasicBlock
*BB
) const {
169 BBCondMapType::const_iterator at
= BBConds
.find(BB
);
170 return at
!= BBConds
.end() ? &(at
->second
) : 0;
173 /// @brief Get all access functions in a BasicBlock
175 /// @param BB The BasicBlock that containing the access functions.
177 /// @return All access functions in BB
179 const AccFuncSetType
*getAccessFunctions(const BasicBlock
* BB
) const {
180 AccFuncMapType::const_iterator at
= AccFuncMap
.find(BB
);
181 return at
!= AccFuncMap
.end()? &(at
->second
) : 0;
185 /// @brief Print the Temporary Scop information.
187 /// @param OS The output stream the access functions is printed to.
188 /// @param SE The ScalarEvolution that help printing Temporary Scop
190 /// @param LI The LoopInfo that help printing the access functions.
191 void print(raw_ostream
&OS
, ScalarEvolution
*SE
, LoopInfo
*LI
) const;
193 /// @brief Print the access functions and loop bounds in this Scop.
195 /// @param OS The output stream the access functions is printed to.
196 /// @param SE The ScalarEvolution that help printing the access functions.
197 /// @param LI The LoopInfo that help printing the access functions.
198 void printDetail(raw_ostream
&OS
, ScalarEvolution
*SE
,
199 LoopInfo
*LI
, const Region
*Reg
, unsigned ind
) const;
202 typedef std::map
<const Region
*, TempScop
*> TempScopMapType
;
203 //===----------------------------------------------------------------------===//
204 /// @brief The Function Pass to extract temporary information for Static control
205 /// part in llvm function.
207 class TempScopInfo
: public FunctionPass
{
208 //===-------------------------------------------------------------------===//
210 TempScopInfo(const TempScopInfo
&);
212 const TempScopInfo
&operator=(const TempScopInfo
&);
214 // The ScalarEvolution to help building Scop.
217 // LoopInfo for information about loops
220 // The AliasAnalysis to build AliasSetTracker.
223 // Valid Regions for Scop
226 // For condition extraction support.
228 PostDominatorTree
*PDT
;
230 // Target data for element size computing.
233 // Remember the bounds of loops, to help us build iteration domain of BBs.
234 LoopBoundMapType LoopBounds
;
236 // And also Remember the constrains for BBs
237 BBCondMapType BBConds
;
239 // Access function of bbs.
240 AccFuncMapType AccFuncMap
;
242 // Mapping regions to the corresponding Scop in current function.
243 TempScopMapType TempScops
;
245 // Clear the context.
248 /// @brief Build condition constrains to BBs in a valid Scop.
250 /// @param BB The BasicBlock to build condition constrains
251 /// @param RegionEntry The entry block of the Smallest Region that containing
253 void buildCondition(BasicBlock
*BB
, BasicBlock
*RegionEntry
);
255 // Build the affine function of the given condition
256 void buildAffineCondition(Value
&V
, bool inverted
, Comparison
**Comp
) const;
258 // Return the temporary Scop information of Region R, where R must be a valid
260 TempScop
*getTempScop(Region
&R
);
262 // Build the temprory information of Region R, where R must be a valid part
264 TempScop
*buildTempScop(Region
&R
);
266 void buildAccessFunctions(Region
&RefRegion
, BasicBlock
&BB
);
268 void buildLoopBounds(TempScop
&Scop
);
272 explicit TempScopInfo() : FunctionPass(ID
) {}
275 /// @brief Get the temporay Scop information in LLVM IR represent
278 /// @return The Scop information in LLVM IR represent.
279 TempScop
*getTempScop(const Region
*R
) const;
281 /// @name FunctionPass interface
283 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const;
284 virtual void releaseMemory() { clear(); }
285 virtual bool runOnFunction(Function
&F
);
286 virtual void print(raw_ostream
&OS
, const Module
*) const;
290 } // end namespace polly
294 void initializeTempScopInfoPass(llvm::PassRegistry
&);