Coverage Report

Created: 2017-06-28 17:40

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/include/polly/Support/ScopHelper.h
Line
Count
Source (jump to first uncovered line)
1
//===------ Support/ScopHelper.h -- Some Helper Functions for Scop. -------===//
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
// Small functions that help with LLVM-IR.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef POLLY_SUPPORT_IRHELPER_H
15
#define POLLY_SUPPORT_IRHELPER_H
16
17
#include "llvm/ADT/DenseMap.h"
18
#include "llvm/ADT/SetVector.h"
19
#include "llvm/IR/Instructions.h"
20
#include "llvm/IR/IntrinsicInst.h"
21
#include "llvm/IR/ValueHandle.h"
22
#include <tuple>
23
#include <vector>
24
25
namespace llvm {
26
class LoopInfo;
27
class Loop;
28
class ScalarEvolution;
29
class SCEV;
30
class Region;
31
class Pass;
32
class DominatorTree;
33
class RegionInfo;
34
class GetElementPtrInst;
35
} // namespace llvm
36
37
namespace polly {
38
class Scop;
39
40
/// Type to remap values.
41
using ValueMapT = llvm::DenseMap<llvm::AssertingVH<llvm::Value>,
42
                                 llvm::AssertingVH<llvm::Value>>;
43
44
/// Type for a set of invariant loads.
45
using InvariantLoadsSetTy = llvm::SetVector<llvm::AssertingVH<llvm::LoadInst>>;
46
47
/// Set type for parameters.
48
using ParameterSetTy = llvm::SetVector<const llvm::SCEV *>;
49
50
/// Set of loops (used to remember loops in non-affine subregions).
51
using BoxedLoopsSetTy = llvm::SetVector<const llvm::Loop *>;
52
53
/// Utility proxy to wrap the common members of LoadInst and StoreInst.
54
///
55
/// This works like the LLVM utility class CallSite, ie. it forwards all calls
56
/// to either a LoadInst, StoreInst, MemIntrinsic or MemTransferInst.
57
/// It is similar to LLVM's utility classes IntrinsicInst, MemIntrinsic,
58
/// MemTransferInst, etc. in that it offers a common interface, but does not act
59
/// as a fake base class.
60
/// It is similar to StringRef and ArrayRef in that it holds a pointer to the
61
/// referenced object and should be passed by-value as it is small enough.
62
///
63
/// This proxy can either represent a LoadInst instance, a StoreInst instance,
64
/// a MemIntrinsic instance (memset, memmove, memcpy), a CallInst instance or a
65
/// nullptr (only creatable using the default constructor); never an Instruction
66
/// that is neither of the above mentioned. When representing a nullptr, only
67
/// the following methods are defined:
68
/// isNull(), isInstruction(), isLoad(), isStore(), ..., isMemTransferInst(),
69
/// operator bool(), operator!()
70
///
71
/// The functions isa, cast, cast_or_null, dyn_cast are modeled te resemble
72
/// those from llvm/Support/Casting.h. Partial template function specialization
73
/// is currently not supported in C++ such that those cannot be used directly.
74
/// (llvm::isa could, but then llvm:cast etc. would not have the expected
75
/// behavior)
76
class MemAccInst {
77
private:
78
  llvm::Instruction *I;
79
80
public:
81
15.8k
  MemAccInst() : I(nullptr) {}
82
32.6k
  MemAccInst(const MemAccInst &Inst) : I(Inst.I) {}
83
0
  /* implicit */ MemAccInst(llvm::LoadInst &LI) : I(&LI) {}
84
245
  /* implicit */ MemAccInst(llvm::LoadInst *LI) : I(LI) {}
85
0
  /* implicit */ MemAccInst(llvm::StoreInst &SI) : I(&SI) {}
86
347
  /* implicit */ MemAccInst(llvm::StoreInst *SI) : I(SI) {}
87
0
  /* implicit */ MemAccInst(llvm::MemIntrinsic *MI) : I(MI) {}
88
0
  /* implicit */ MemAccInst(llvm::CallInst *CI) : I(CI) {}
89
14.3k
  explicit MemAccInst(llvm::Instruction &I) : I(&I) { assert(isa(I)); }
90
4.30k
  explicit MemAccInst(llvm::Instruction *I) : I(I) { assert(isa(I)); }
91
92
30.1k
  static bool isa(const llvm::Value &V) {
93
22.4k
    return llvm::isa<llvm::LoadInst>(V) || llvm::isa<llvm::StoreInst>(V) ||
94
15.8k
           
llvm::isa<llvm::CallInst>(V)15.8k
||
llvm::isa<llvm::MemIntrinsic>(V)15.8k
;
95
30.1k
  }
96
1.48k
  static bool isa(const llvm::Value *V) {
97
841
    return llvm::isa<llvm::LoadInst>(V) || llvm::isa<llvm::StoreInst>(V) ||
98
152
           
llvm::isa<llvm::CallInst>(V)152
||
llvm::isa<llvm::MemIntrinsic>(V)1
;
99
1.48k
  }
100
0
  static MemAccInst cast(llvm::Value &V) {
101
0
    return MemAccInst(llvm::cast<llvm::Instruction>(V));
102
0
  }
103
0
  static MemAccInst cast(llvm::Value *V) {
104
0
    return MemAccInst(llvm::cast<llvm::Instruction>(V));
105
0
  }
106
0
  static MemAccInst cast_or_null(llvm::Value &V) {
107
0
    return MemAccInst(llvm::cast<llvm::Instruction>(V));
108
0
  }
109
0
  static MemAccInst cast_or_null(llvm::Value *V) {
110
0
    if (!V)
111
0
      return MemAccInst();
112
0
    return MemAccInst(llvm::cast<llvm::Instruction>(V));
113
0
  }
114
30.1k
  static MemAccInst dyn_cast(llvm::Value &V) {
115
30.1k
    if (isa(V))
116
14.3k
      return MemAccInst(llvm::cast<llvm::Instruction>(V));
117
15.8k
    return MemAccInst();
118
30.1k
  }
119
1.48k
  static MemAccInst dyn_cast(llvm::Value *V) {
120
1.48k
    assert(V);
121
1.48k
    if (isa(V))
122
1.48k
      return MemAccInst(llvm::cast<llvm::Instruction>(V));
123
1
    return MemAccInst();
124
1.48k
  }
125
126
0
  MemAccInst &operator=(const MemAccInst &Inst) {
127
0
    I = Inst.I;
128
0
    return *this;
129
0
  }
130
0
  MemAccInst &operator=(llvm::LoadInst &LI) {
131
0
    I = &LI;
132
0
    return *this;
133
0
  }
134
0
  MemAccInst &operator=(llvm::LoadInst *LI) {
135
0
    I = LI;
136
0
    return *this;
137
0
  }
138
0
  MemAccInst &operator=(llvm::StoreInst &SI) {
139
0
    I = &SI;
140
0
    return *this;
141
0
  }
142
0
  MemAccInst &operator=(llvm::StoreInst *SI) {
143
0
    I = SI;
144
0
    return *this;
145
0
  }
146
0
  MemAccInst &operator=(llvm::MemIntrinsic &MI) {
147
0
    I = &MI;
148
0
    return *this;
149
0
  }
150
0
  MemAccInst &operator=(llvm::MemIntrinsic *MI) {
151
0
    I = MI;
152
0
    return *this;
153
0
  }
154
0
  MemAccInst &operator=(llvm::CallInst &CI) {
155
0
    I = &CI;
156
0
    return *this;
157
0
  }
158
0
  MemAccInst &operator=(llvm::CallInst *CI) {
159
0
    I = CI;
160
0
    return *this;
161
0
  }
162
163
19.9k
  llvm::Instruction *get() const {
164
19.9k
    assert(I && "Unexpected nullptr!");
165
19.9k
    return I;
166
19.9k
  }
167
20.5k
  operator llvm::Instruction *() const { return asInstruction(); }
168
19.9k
  llvm::Instruction *operator->() const { return get(); }
169
170
36.5k
  explicit operator bool() const { return isInstruction(); }
171
1.46k
  bool operator!() const { return isNull(); }
172
173
8.82k
  llvm::Value *getValueOperand() const {
174
8.82k
    if (isLoad())
175
4.55k
      return asLoad();
176
4.26k
    
if (4.26k
isStore()4.26k
)
177
4.25k
      return asStore()->getValueOperand();
178
17
    
if (17
isMemIntrinsic()17
)
179
17
      return nullptr;
180
0
    
if (0
isCallInst()0
)
181
0
      return nullptr;
182
0
    
llvm_unreachable0
("Operation not supported on nullptr");0
183
0
  }
184
24.8k
  llvm::Value *getPointerOperand() const {
185
24.8k
    if (isLoad())
186
12.8k
      return asLoad()->getPointerOperand();
187
11.9k
    
if (11.9k
isStore()11.9k
)
188
11.7k
      return asStore()->getPointerOperand();
189
206
    
if (206
isMemIntrinsic()206
)
190
15
      return asMemIntrinsic()->getRawDest();
191
191
    
if (191
isCallInst()191
)
192
191
      return nullptr;
193
0
    
llvm_unreachable0
("Operation not supported on nullptr");0
194
0
  }
195
196
5
  unsigned getAlignment() const {
197
5
    if (isLoad())
198
4
      return asLoad()->getAlignment();
199
1
    
if (1
isStore()1
)
200
1
      return asStore()->getAlignment();
201
0
    
if (0
isMemIntrinsic()0
)
202
0
      return asMemIntrinsic()->getAlignment();
203
0
    
if (0
isCallInst()0
)
204
0
      return 0;
205
0
    
llvm_unreachable0
("Operation not supported on nullptr");0
206
0
  }
207
0
  bool isVolatile() const {
208
0
    if (isLoad())
209
0
      return asLoad()->isVolatile();
210
0
    if (isStore())
211
0
      return asStore()->isVolatile();
212
0
    if (isMemIntrinsic())
213
0
      return asMemIntrinsic()->isVolatile();
214
0
    if (isCallInst())
215
0
      return false;
216
0
    llvm_unreachable("Operation not supported on nullptr");
217
0
  }
218
11.1k
  bool isSimple() const {
219
11.1k
    if (isLoad())
220
6.00k
      return asLoad()->isSimple();
221
5.10k
    
if (5.10k
isStore()5.10k
)
222
5.10k
      return asStore()->isSimple();
223
0
    
if (0
isMemIntrinsic()0
)
224
0
      return !asMemIntrinsic()->isVolatile();
225
0
    
if (0
isCallInst()0
)
226
0
      return true;
227
0
    
llvm_unreachable0
("Operation not supported on nullptr");0
228
0
  }
229
0
  llvm::AtomicOrdering getOrdering() const {
230
0
    if (isLoad())
231
0
      return asLoad()->getOrdering();
232
0
    if (isStore())
233
0
      return asStore()->getOrdering();
234
0
    if (isMemIntrinsic())
235
0
      return llvm::AtomicOrdering::NotAtomic;
236
0
    if (isCallInst())
237
0
      return llvm::AtomicOrdering::NotAtomic;
238
0
    llvm_unreachable("Operation not supported on nullptr");
239
0
  }
240
0
  bool isUnordered() const {
241
0
    if (isLoad())
242
0
      return asLoad()->isUnordered();
243
0
    if (isStore())
244
0
      return asStore()->isUnordered();
245
0
    // Copied from the Load/Store implementation of isUnordered:
246
0
    if (isMemIntrinsic())
247
0
      return !asMemIntrinsic()->isVolatile();
248
0
    if (isCallInst())
249
0
      return true;
250
0
    llvm_unreachable("Operation not supported on nullptr");
251
0
  }
252
253
2.05k
  bool isNull() const { return !I; }
254
36.5k
  bool isInstruction() const { return I; }
255
256
59.4k
  llvm::Instruction *asInstruction() const { return I; }
257
258
private:
259
44.7k
  bool isLoad() const 
{ return I && 44.7k
llvm::isa<llvm::LoadInst>(I)44.7k
; }
260
21.3k
  bool isStore() const 
{ return I && 21.3k
llvm::isa<llvm::StoreInst>(I)21.3k
; }
261
191
  bool isCallInst() const 
{ return I && 191
llvm::isa<llvm::CallInst>(I)191
; }
262
223
  bool isMemIntrinsic() const 
{ return I && 223
llvm::isa<llvm::MemIntrinsic>(I)223
; }
263
0
  bool isMemSetInst() const { return I && llvm::isa<llvm::MemSetInst>(I); }
264
0
  bool isMemTransferInst() const {
265
0
    return I && llvm::isa<llvm::MemTransferInst>(I);
266
0
  }
267
268
23.4k
  llvm::LoadInst *asLoad() const { return llvm::cast<llvm::LoadInst>(I); }
269
21.1k
  llvm::StoreInst *asStore() const { return llvm::cast<llvm::StoreInst>(I); }
270
0
  llvm::CallInst *asCallInst() const { return llvm::cast<llvm::CallInst>(I); }
271
15
  llvm::MemIntrinsic *asMemIntrinsic() const {
272
15
    return llvm::cast<llvm::MemIntrinsic>(I);
273
15
  }
274
0
  llvm::MemSetInst *asMemSetInst() const {
275
0
    return llvm::cast<llvm::MemSetInst>(I);
276
0
  }
277
0
  llvm::MemTransferInst *asMemTransferInst() const {
278
0
    return llvm::cast<llvm::MemTransferInst>(I);
279
0
  }
280
};
281
} // namespace polly
282
283
namespace llvm {
284
/// Specialize simplify_type for MemAccInst to enable dyn_cast and cast
285
///        from a MemAccInst object.
286
template <> struct simplify_type<polly::MemAccInst> {
287
  typedef Instruction *SimpleType;
288
38.9k
  static SimpleType getSimplifiedValue(polly::MemAccInst &I) {
289
38.9k
    return I.asInstruction();
290
38.9k
  }
291
};
292
} // namespace llvm
293
294
namespace polly {
295
296
/// Simplify the region to have a single unconditional entry edge and a
297
/// single exit edge.
298
///
299
/// Although this function allows DT and RI to be null, regions only work
300
/// properly if the DominatorTree (for Region::contains) and RegionInfo are kept
301
/// up-to-date.
302
///
303
/// @param R  The region to be simplified
304
/// @param DT DominatorTree to be updated.
305
/// @param LI LoopInfo to be updated.
306
/// @param RI RegionInfo to be updated.
307
void simplifyRegion(llvm::Region *R, llvm::DominatorTree *DT,
308
                    llvm::LoopInfo *LI, llvm::RegionInfo *RI);
309
310
/// Split the entry block of a function to store the newly inserted
311
///        allocations outside of all Scops.
312
///
313
/// @param EntryBlock The entry block of the current function.
314
/// @param P          The pass that currently running.
315
///
316
void splitEntryBlockForAlloca(llvm::BasicBlock *EntryBlock, llvm::Pass *P);
317
318
/// Wrapper for SCEVExpander extended to all Polly features.
319
///
320
/// This wrapper will internally call the SCEVExpander but also makes sure that
321
/// all additional features not represented in SCEV (e.g., SDiv/SRem are not
322
/// black boxes but can be part of the function) will be expanded correctly.
323
///
324
/// The parameters are the same as for the creation of a SCEVExpander as well
325
/// as the call to SCEVExpander::expandCodeFor:
326
///
327
/// @param S     The current Scop.
328
/// @param SE    The Scalar Evolution pass.
329
/// @param DL    The module data layout.
330
/// @param Name  The suffix added to the new instruction names.
331
/// @param E     The expression for which code is actually generated.
332
/// @param Ty    The type of the resulting code.
333
/// @param IP    The insertion point for the new code.
334
/// @param VMap  A remapping of values used in @p E.
335
/// @param RTCBB The last block of the RTC. Used to insert loop-invariant
336
///              instructions in rare cases.
337
llvm::Value *expandCodeFor(Scop &S, llvm::ScalarEvolution &SE,
338
                           const llvm::DataLayout &DL, const char *Name,
339
                           const llvm::SCEV *E, llvm::Type *Ty,
340
                           llvm::Instruction *IP, ValueMapT *VMap,
341
                           llvm::BasicBlock *RTCBB);
342
343
/// Check if the block is a error block.
344
///
345
/// A error block is currently any block that fulfills at least one of
346
/// the following conditions:
347
///
348
///  - It is terminated by an unreachable instruction
349
///  - It contains a call to a non-pure function that is not immediately
350
///    dominated by a loop header and that does not dominate the region exit.
351
///    This is a heuristic to pick only error blocks that are conditionally
352
///    executed and can be assumed to be not executed at all without the domains
353
///    being available.
354
///
355
/// @param BB The block to check.
356
/// @param R  The analyzed region.
357
/// @param LI The loop info analysis.
358
/// @param DT The dominator tree of the function.
359
///
360
/// @return True if the block is a error block, false otherwise.
361
bool isErrorBlock(llvm::BasicBlock &BB, const llvm::Region &R,
362
                  llvm::LoopInfo &LI, const llvm::DominatorTree &DT);
363
364
/// Return the condition for the terminator @p TI.
365
///
366
/// For unconditional branches the "i1 true" condition will be returned.
367
///
368
/// @param TI The terminator to get the condition from.
369
///
370
/// @return The condition of @p TI and nullptr if none could be extracted.
371
llvm::Value *getConditionFromTerminator(llvm::TerminatorInst *TI);
372
373
/// Check if @p LInst can be hoisted in @p R.
374
///
375
/// @param LInst The load to check.
376
/// @param R     The analyzed region.
377
/// @param LI    The loop info.
378
/// @param SE    The scalar evolution analysis.
379
/// @param DT    The dominator tree of the function.
380
///
381
/// @return True if @p LInst can be hoisted in @p R.
382
bool isHoistableLoad(llvm::LoadInst *LInst, llvm::Region &R, llvm::LoopInfo &LI,
383
                     llvm::ScalarEvolution &SE, const llvm::DominatorTree &DT);
384
385
/// Return true iff @p V is an intrinsic that we ignore during code
386
///        generation.
387
bool isIgnoredIntrinsic(const llvm::Value *V);
388
389
/// Check whether a value an be synthesized by the code generator.
390
///
391
/// Some value will be recalculated only from information that is code generated
392
/// from the polyhedral representation. For such instructions we do not need to
393
/// ensure that their operands are available during code generation.
394
///
395
/// @param V The value to check.
396
/// @param S The current SCoP.
397
/// @param SE The scalar evolution database.
398
/// @param Scope Location where the value would by synthesized.
399
/// @return If the instruction I can be regenerated from its
400
///         scalar evolution representation, return true,
401
///         otherwise return false.
402
bool canSynthesize(const llvm::Value *V, const Scop &S,
403
                   llvm::ScalarEvolution *SE, llvm::Loop *Scope);
404
405
/// Return the block in which a value is used.
406
///
407
/// For normal instructions, this is the instruction's parent block. For PHI
408
/// nodes, this is the incoming block of that use, because this is where the
409
/// operand must be defined (i.e. its definition dominates this block).
410
/// Non-instructions do not use operands at a specific point such that in this
411
/// case this function returns nullptr.
412
llvm::BasicBlock *getUseBlock(llvm::Use &U);
413
414
/// Derive the individual index expressions from a GEP instruction.
415
///
416
/// This function optimistically assumes the GEP references into a fixed size
417
/// array. If this is actually true, this function returns a list of array
418
/// subscript expressions as SCEV as well as a list of integers describing
419
/// the size of the individual array dimensions. Both lists have either equal
420
/// length or the size list is one element shorter in case there is no known
421
/// size available for the outermost array dimension.
422
///
423
/// @param GEP The GetElementPtr instruction to analyze.
424
///
425
/// @return A tuple with the subscript expressions and the dimension sizes.
426
std::tuple<std::vector<const llvm::SCEV *>, std::vector<int>>
427
getIndexExpressionsFromGEP(llvm::GetElementPtrInst *GEP,
428
                           llvm::ScalarEvolution &SE);
429
} // namespace polly
430
#endif