1 //===- ScopBuilder.cpp ---------------------------------------------------===//
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 // Create a polyhedral description for a static control flow region.
12 // The pass creates a polyhedral description of the Scops detected by the SCoP
13 // detection derived from their LLVM-IR code.
15 //===----------------------------------------------------------------------===//
17 #include "polly/ScopBuilder.h"
18 #include "polly/Options.h"
19 #include "polly/Support/GICHelper.h"
20 #include "polly/Support/SCEVValidator.h"
21 #include "polly/Support/VirtualInstruction.h"
22 #include "llvm/Analysis/RegionIterator.h"
23 #include "llvm/IR/DiagnosticInfo.h"
26 using namespace polly
;
28 #define DEBUG_TYPE "polly-scops"
30 STATISTIC(ScopFound
, "Number of valid Scops");
31 STATISTIC(RichScopFound
, "Number of Scops containing a loop");
32 STATISTIC(InfeasibleScops
,
33 "Number of SCoPs with statically infeasible context.");
35 static cl::opt
<bool> ModelReadOnlyScalars(
36 "polly-analyze-read-only-scalars",
37 cl::desc("Model read-only scalar values in the scop description"),
38 cl::Hidden
, cl::ZeroOrMore
, cl::init(true), cl::cat(PollyCategory
));
40 static cl::opt
<bool> UnprofitableScalarAccs(
41 "polly-unprofitable-scalar-accs",
42 cl::desc("Count statements with scalar accesses as not optimizable"),
43 cl::Hidden
, cl::init(false), cl::cat(PollyCategory
));
45 static cl::opt
<bool> DetectFortranArrays(
46 "polly-detect-fortran-arrays",
47 cl::desc("Detect Fortran arrays and use this for code generation"),
48 cl::Hidden
, cl::init(false), cl::cat(PollyCategory
));
50 void ScopBuilder::buildPHIAccesses(PHINode
*PHI
, Region
*NonAffineSubRegion
,
53 // PHI nodes that are in the exit block of the region, hence if IsExitBlock is
54 // true, are not modeled as ordinary PHI nodes as they are not part of the
55 // region. However, we model the operands in the predecessor blocks that are
56 // part of the region as regular scalar accesses.
58 // If we can synthesize a PHI we can skip it, however only if it is in
59 // the region. If it is not it can only be in the exit block of the region.
60 // In this case we model the operands but not the PHI itself.
61 auto *Scope
= LI
.getLoopFor(PHI
->getParent());
62 if (!IsExitBlock
&& canSynthesize(PHI
, *scop
, &SE
, Scope
))
65 // PHI nodes are modeled as if they had been demoted prior to the SCoP
66 // detection. Hence, the PHI is a load of a new memory location in which the
67 // incoming value was written at the end of the incoming basic block.
68 bool OnlyNonAffineSubRegionOperands
= true;
69 for (unsigned u
= 0; u
< PHI
->getNumIncomingValues(); u
++) {
70 Value
*Op
= PHI
->getIncomingValue(u
);
71 BasicBlock
*OpBB
= PHI
->getIncomingBlock(u
);
73 // Do not build PHI dependences inside a non-affine subregion, but make
74 // sure that the necessary scalar values are still made available.
75 if (NonAffineSubRegion
&& NonAffineSubRegion
->contains(OpBB
)) {
76 auto *OpInst
= dyn_cast
<Instruction
>(Op
);
77 if (!OpInst
|| !NonAffineSubRegion
->contains(OpInst
))
78 ensureValueRead(Op
, OpBB
);
82 OnlyNonAffineSubRegionOperands
= false;
83 ensurePHIWrite(PHI
, OpBB
, Op
, IsExitBlock
);
86 if (!OnlyNonAffineSubRegionOperands
&& !IsExitBlock
) {
87 addPHIReadAccess(PHI
);
91 void ScopBuilder::buildScalarDependences(Instruction
*Inst
) {
92 assert(!isa
<PHINode
>(Inst
));
94 // Pull-in required operands.
95 for (Use
&Op
: Inst
->operands())
96 ensureValueRead(Op
.get(), Inst
->getParent());
99 void ScopBuilder::buildEscapingDependences(Instruction
*Inst
) {
100 // Check for uses of this instruction outside the scop. Because we do not
101 // iterate over such instructions and therefore did not "ensure" the existence
102 // of a write, we must determine such use here.
103 for (Use
&U
: Inst
->uses()) {
104 Instruction
*UI
= dyn_cast
<Instruction
>(U
.getUser());
108 BasicBlock
*UseParent
= getUseBlock(U
);
109 BasicBlock
*UserParent
= UI
->getParent();
111 // An escaping value is either used by an instruction not within the scop,
112 // or (when the scop region's exit needs to be simplified) by a PHI in the
113 // scop's exit block. This is because region simplification before code
114 // generation inserts new basic blocks before the PHI such that its incoming
115 // blocks are not in the scop anymore.
116 if (!scop
->contains(UseParent
) ||
117 (isa
<PHINode
>(UI
) && scop
->isExit(UserParent
) &&
118 scop
->hasSingleExitEdge())) {
119 // At least one escaping use found.
120 ensureValueWrite(Inst
);
126 /// Check that a value is a Fortran Array descriptor.
128 /// We check if V has the following structure:
129 /// %"struct.array1_real(kind=8)" = type { i8*, i<zz>, i<zz>,
130 /// [<num> x %struct.descriptor_dimension] }
133 /// %struct.descriptor_dimension = type { i<zz>, i<zz>, i<zz> }
135 /// 1. V's type name starts with "struct.array"
136 /// 2. V's type has layout as shown.
137 /// 3. Final member of V's type has name "struct.descriptor_dimension",
138 /// 4. "struct.descriptor_dimension" has layout as shown.
139 /// 5. Consistent use of i<zz> where <zz> is some fixed integer number.
141 /// We are interested in such types since this is the code that dragonegg
142 /// generates for Fortran array descriptors.
144 /// @param V the Value to be checked.
146 /// @returns True if V is a Fortran array descriptor, False otherwise.
147 bool isFortranArrayDescriptor(Value
*V
) {
148 PointerType
*PTy
= dyn_cast
<PointerType
>(V
->getType());
153 Type
*Ty
= PTy
->getElementType();
154 assert(Ty
&& "Ty expected to be initialized");
155 auto *StructArrTy
= dyn_cast
<StructType
>(Ty
);
157 if (!(StructArrTy
&& StructArrTy
->hasName()))
160 if (!StructArrTy
->getName().startswith("struct.array"))
163 if (StructArrTy
->getNumElements() != 4)
166 const ArrayRef
<Type
*> ArrMemberTys
= StructArrTy
->elements();
169 if (ArrMemberTys
[0] != Type::getInt8PtrTy(V
->getContext()))
172 // Get a reference to the int type and check that all the members
173 // share the same int type
174 Type
*IntTy
= ArrMemberTys
[1];
175 if (ArrMemberTys
[2] != IntTy
)
178 // type: [<num> x %struct.descriptor_dimension]
179 ArrayType
*DescriptorDimArrayTy
= dyn_cast
<ArrayType
>(ArrMemberTys
[3]);
180 if (!DescriptorDimArrayTy
)
183 // type: %struct.descriptor_dimension := type { ixx, ixx, ixx }
184 StructType
*DescriptorDimTy
=
185 dyn_cast
<StructType
>(DescriptorDimArrayTy
->getElementType());
187 if (!(DescriptorDimTy
&& DescriptorDimTy
->hasName()))
190 if (DescriptorDimTy
->getName() != "struct.descriptor_dimension")
193 if (DescriptorDimTy
->getNumElements() != 3)
196 for (auto MemberTy
: DescriptorDimTy
->elements()) {
197 if (MemberTy
!= IntTy
)
204 Value
*ScopBuilder::findFADAllocationVisible(MemAccInst Inst
) {
205 // match: 4.1 & 4.2 store/load
206 if (!isa
<LoadInst
>(Inst
) && !isa
<StoreInst
>(Inst
))
210 if (Inst
.getAlignment() != 8)
213 Value
*Address
= Inst
.getPointerOperand();
215 const BitCastInst
*Bitcast
= nullptr;
217 if (auto *Slot
= dyn_cast
<GetElementPtrInst
>(Address
)) {
218 Value
*TypedMem
= Slot
->getPointerOperand();
220 Bitcast
= dyn_cast
<BitCastInst
>(TypedMem
);
223 Bitcast
= dyn_cast
<BitCastInst
>(Address
);
229 auto *MallocMem
= Bitcast
->getOperand(0);
232 auto *MallocCall
= dyn_cast
<CallInst
>(MallocMem
);
236 Function
*MallocFn
= MallocCall
->getCalledFunction();
237 if (!(MallocFn
&& MallocFn
->hasName() && MallocFn
->getName() == "malloc"))
240 // Find all uses the malloc'd memory.
241 // We are looking for a "store" into a struct with the type being the Fortran
243 for (auto user
: MallocMem
->users()) {
246 auto *MallocStore
= dyn_cast
<StoreInst
>(user
);
250 auto *DescriptorGEP
=
251 dyn_cast
<GEPOperator
>(MallocStore
->getPointerOperand());
256 auto DescriptorType
=
257 dyn_cast
<StructType
>(DescriptorGEP
->getSourceElementType());
258 if (!(DescriptorType
&& DescriptorType
->hasName()))
261 Value
*Descriptor
= dyn_cast
<Value
>(DescriptorGEP
->getPointerOperand());
266 if (!isFortranArrayDescriptor(Descriptor
))
275 Value
*ScopBuilder::findFADAllocationInvisible(MemAccInst Inst
) {
277 if (!isa
<LoadInst
>(Inst
) && !isa
<StoreInst
>(Inst
))
280 Value
*Slot
= Inst
.getPointerOperand();
282 LoadInst
*MemLoad
= nullptr;
284 if (auto *SlotGEP
= dyn_cast
<GetElementPtrInst
>(Slot
)) {
286 MemLoad
= dyn_cast
<LoadInst
>(SlotGEP
->getPointerOperand());
289 MemLoad
= dyn_cast
<LoadInst
>(Slot
);
295 auto *BitcastOperator
=
296 dyn_cast
<BitCastOperator
>(MemLoad
->getPointerOperand());
297 if (!BitcastOperator
)
300 Value
*Descriptor
= dyn_cast
<Value
>(BitcastOperator
->getOperand(0));
304 if (!isFortranArrayDescriptor(Descriptor
))
310 bool ScopBuilder::buildAccessMultiDimFixed(MemAccInst Inst
, ScopStmt
*Stmt
) {
311 Value
*Val
= Inst
.getValueOperand();
312 Type
*ElementType
= Val
->getType();
313 Value
*Address
= Inst
.getPointerOperand();
314 const SCEV
*AccessFunction
=
315 SE
.getSCEVAtScope(Address
, LI
.getLoopFor(Inst
->getParent()));
316 const SCEVUnknown
*BasePointer
=
317 dyn_cast
<SCEVUnknown
>(SE
.getPointerBase(AccessFunction
));
318 enum MemoryAccess::AccessType AccType
=
319 isa
<LoadInst
>(Inst
) ? MemoryAccess::READ
: MemoryAccess::MUST_WRITE
;
321 if (auto *BitCast
= dyn_cast
<BitCastInst
>(Address
)) {
322 auto *Src
= BitCast
->getOperand(0);
323 auto *SrcTy
= Src
->getType();
324 auto *DstTy
= BitCast
->getType();
325 // Do not try to delinearize non-sized (opaque) pointers.
326 if ((SrcTy
->isPointerTy() && !SrcTy
->getPointerElementType()->isSized()) ||
327 (DstTy
->isPointerTy() && !DstTy
->getPointerElementType()->isSized())) {
330 if (SrcTy
->isPointerTy() && DstTy
->isPointerTy() &&
331 DL
.getTypeAllocSize(SrcTy
->getPointerElementType()) ==
332 DL
.getTypeAllocSize(DstTy
->getPointerElementType()))
336 auto *GEP
= dyn_cast
<GetElementPtrInst
>(Address
);
340 std::vector
<const SCEV
*> Subscripts
;
341 std::vector
<int> Sizes
;
342 std::tie(Subscripts
, Sizes
) = getIndexExpressionsFromGEP(GEP
, SE
);
343 auto *BasePtr
= GEP
->getOperand(0);
345 if (auto *BasePtrCast
= dyn_cast
<BitCastInst
>(BasePtr
))
346 BasePtr
= BasePtrCast
->getOperand(0);
348 // Check for identical base pointers to ensure that we do not miss index
349 // offsets that have been added before this GEP is applied.
350 if (BasePtr
!= BasePointer
->getValue())
353 std::vector
<const SCEV
*> SizesSCEV
;
355 const InvariantLoadsSetTy
&ScopRIL
= scop
->getRequiredInvariantLoads();
357 Loop
*SurroundingLoop
= Stmt
->getSurroundingLoop();
358 for (auto *Subscript
: Subscripts
) {
359 InvariantLoadsSetTy AccessILS
;
360 if (!isAffineExpr(&scop
->getRegion(), SurroundingLoop
, Subscript
, SE
,
364 for (LoadInst
*LInst
: AccessILS
)
365 if (!ScopRIL
.count(LInst
))
372 SizesSCEV
.push_back(nullptr);
375 SizesSCEV
.push_back(SE
.getSCEV(
376 ConstantInt::get(IntegerType::getInt64Ty(BasePtr
->getContext()), V
)));
378 addArrayAccess(Inst
, AccType
, BasePointer
->getValue(), ElementType
, true,
379 Subscripts
, SizesSCEV
, Val
);
383 bool ScopBuilder::buildAccessMultiDimParam(MemAccInst Inst
, ScopStmt
*Stmt
) {
384 if (!PollyDelinearize
)
387 Value
*Address
= Inst
.getPointerOperand();
388 Value
*Val
= Inst
.getValueOperand();
389 Type
*ElementType
= Val
->getType();
390 unsigned ElementSize
= DL
.getTypeAllocSize(ElementType
);
391 enum MemoryAccess::AccessType AccType
=
392 isa
<LoadInst
>(Inst
) ? MemoryAccess::READ
: MemoryAccess::MUST_WRITE
;
394 const SCEV
*AccessFunction
=
395 SE
.getSCEVAtScope(Address
, LI
.getLoopFor(Inst
->getParent()));
396 const SCEVUnknown
*BasePointer
=
397 dyn_cast
<SCEVUnknown
>(SE
.getPointerBase(AccessFunction
));
399 assert(BasePointer
&& "Could not find base pointer");
401 auto &InsnToMemAcc
= scop
->getInsnToMemAccMap();
402 auto AccItr
= InsnToMemAcc
.find(Inst
);
403 if (AccItr
== InsnToMemAcc
.end())
406 std::vector
<const SCEV
*> Sizes
= {nullptr};
408 Sizes
.insert(Sizes
.end(), AccItr
->second
.Shape
->DelinearizedSizes
.begin(),
409 AccItr
->second
.Shape
->DelinearizedSizes
.end());
411 // In case only the element size is contained in the 'Sizes' array, the
412 // access does not access a real multi-dimensional array. Hence, we allow
413 // the normal single-dimensional access construction to handle this.
414 if (Sizes
.size() == 1)
417 // Remove the element size. This information is already provided by the
418 // ElementSize parameter. In case the element size of this access and the
419 // element size used for delinearization differs the delinearization is
420 // incorrect. Hence, we invalidate the scop.
422 // TODO: Handle delinearization with differing element sizes.
423 auto DelinearizedSize
=
424 cast
<SCEVConstant
>(Sizes
.back())->getAPInt().getSExtValue();
426 if (ElementSize
!= DelinearizedSize
)
427 scop
->invalidate(DELINEARIZATION
, Inst
->getDebugLoc());
429 addArrayAccess(Inst
, AccType
, BasePointer
->getValue(), ElementType
, true,
430 AccItr
->second
.DelinearizedSubscripts
, Sizes
, Val
);
434 bool ScopBuilder::buildAccessMemIntrinsic(MemAccInst Inst
, ScopStmt
*Stmt
) {
435 auto *MemIntr
= dyn_cast_or_null
<MemIntrinsic
>(Inst
);
437 if (MemIntr
== nullptr)
440 auto *L
= LI
.getLoopFor(Inst
->getParent());
441 auto *LengthVal
= SE
.getSCEVAtScope(MemIntr
->getLength(), L
);
444 // Check if the length val is actually affine or if we overapproximate it
445 InvariantLoadsSetTy AccessILS
;
446 const InvariantLoadsSetTy
&ScopRIL
= scop
->getRequiredInvariantLoads();
448 Loop
*SurroundingLoop
= Stmt
->getSurroundingLoop();
449 bool LengthIsAffine
= isAffineExpr(&scop
->getRegion(), SurroundingLoop
,
450 LengthVal
, SE
, &AccessILS
);
451 for (LoadInst
*LInst
: AccessILS
)
452 if (!ScopRIL
.count(LInst
))
453 LengthIsAffine
= false;
457 auto *DestPtrVal
= MemIntr
->getDest();
460 auto *DestAccFunc
= SE
.getSCEVAtScope(DestPtrVal
, L
);
462 // Ignore accesses to "NULL".
463 // TODO: We could use this to optimize the region further, e.g., intersect
465 // isl_set_complement(isl_set_params(getDomain()))
466 // as we know it would be undefined to execute this instruction anyway.
467 if (DestAccFunc
->isZero())
470 auto *DestPtrSCEV
= dyn_cast
<SCEVUnknown
>(SE
.getPointerBase(DestAccFunc
));
472 DestAccFunc
= SE
.getMinusSCEV(DestAccFunc
, DestPtrSCEV
);
473 addArrayAccess(Inst
, MemoryAccess::MUST_WRITE
, DestPtrSCEV
->getValue(),
474 IntegerType::getInt8Ty(DestPtrVal
->getContext()),
475 LengthIsAffine
, {DestAccFunc
, LengthVal
}, {nullptr},
476 Inst
.getValueOperand());
478 auto *MemTrans
= dyn_cast
<MemTransferInst
>(MemIntr
);
482 auto *SrcPtrVal
= MemTrans
->getSource();
485 auto *SrcAccFunc
= SE
.getSCEVAtScope(SrcPtrVal
, L
);
487 // Ignore accesses to "NULL".
488 // TODO: See above TODO
489 if (SrcAccFunc
->isZero())
492 auto *SrcPtrSCEV
= dyn_cast
<SCEVUnknown
>(SE
.getPointerBase(SrcAccFunc
));
494 SrcAccFunc
= SE
.getMinusSCEV(SrcAccFunc
, SrcPtrSCEV
);
495 addArrayAccess(Inst
, MemoryAccess::READ
, SrcPtrSCEV
->getValue(),
496 IntegerType::getInt8Ty(SrcPtrVal
->getContext()),
497 LengthIsAffine
, {SrcAccFunc
, LengthVal
}, {nullptr},
498 Inst
.getValueOperand());
503 bool ScopBuilder::buildAccessCallInst(MemAccInst Inst
, ScopStmt
*Stmt
) {
504 auto *CI
= dyn_cast_or_null
<CallInst
>(Inst
);
509 if (CI
->doesNotAccessMemory() || isIgnoredIntrinsic(CI
))
512 bool ReadOnly
= false;
513 auto *AF
= SE
.getConstant(IntegerType::getInt64Ty(CI
->getContext()), 0);
514 auto *CalledFunction
= CI
->getCalledFunction();
515 switch (AA
.getModRefBehavior(CalledFunction
)) {
516 case FMRB_UnknownModRefBehavior
:
517 llvm_unreachable("Unknown mod ref behaviour cannot be represented.");
518 case FMRB_DoesNotAccessMemory
:
520 case FMRB_DoesNotReadMemory
:
521 case FMRB_OnlyAccessesInaccessibleMem
:
522 case FMRB_OnlyAccessesInaccessibleOrArgMem
:
524 case FMRB_OnlyReadsMemory
:
525 GlobalReads
.push_back(CI
);
527 case FMRB_OnlyReadsArgumentPointees
:
530 case FMRB_OnlyAccessesArgumentPointees
:
531 auto AccType
= ReadOnly
? MemoryAccess::READ
: MemoryAccess::MAY_WRITE
;
532 Loop
*L
= LI
.getLoopFor(Inst
->getParent());
533 for (const auto &Arg
: CI
->arg_operands()) {
534 if (!Arg
->getType()->isPointerTy())
537 auto *ArgSCEV
= SE
.getSCEVAtScope(Arg
, L
);
538 if (ArgSCEV
->isZero())
541 auto *ArgBasePtr
= cast
<SCEVUnknown
>(SE
.getPointerBase(ArgSCEV
));
542 addArrayAccess(Inst
, AccType
, ArgBasePtr
->getValue(),
543 ArgBasePtr
->getType(), false, {AF
}, {nullptr}, CI
);
551 void ScopBuilder::buildAccessSingleDim(MemAccInst Inst
, ScopStmt
*Stmt
) {
552 Value
*Address
= Inst
.getPointerOperand();
553 Value
*Val
= Inst
.getValueOperand();
554 Type
*ElementType
= Val
->getType();
555 enum MemoryAccess::AccessType AccType
=
556 isa
<LoadInst
>(Inst
) ? MemoryAccess::READ
: MemoryAccess::MUST_WRITE
;
558 const SCEV
*AccessFunction
=
559 SE
.getSCEVAtScope(Address
, LI
.getLoopFor(Inst
->getParent()));
560 const SCEVUnknown
*BasePointer
=
561 dyn_cast
<SCEVUnknown
>(SE
.getPointerBase(AccessFunction
));
563 assert(BasePointer
&& "Could not find base pointer");
564 AccessFunction
= SE
.getMinusSCEV(AccessFunction
, BasePointer
);
566 // Check if the access depends on a loop contained in a non-affine subregion.
567 bool isVariantInNonAffineLoop
= false;
568 SetVector
<const Loop
*> Loops
;
569 findLoops(AccessFunction
, Loops
);
570 for (const Loop
*L
: Loops
)
571 if (Stmt
->contains(L
)) {
572 isVariantInNonAffineLoop
= true;
576 InvariantLoadsSetTy AccessILS
;
578 Loop
*SurroundingLoop
= Stmt
->getSurroundingLoop();
579 bool IsAffine
= !isVariantInNonAffineLoop
&&
580 isAffineExpr(&scop
->getRegion(), SurroundingLoop
,
581 AccessFunction
, SE
, &AccessILS
);
583 const InvariantLoadsSetTy
&ScopRIL
= scop
->getRequiredInvariantLoads();
584 for (LoadInst
*LInst
: AccessILS
)
585 if (!ScopRIL
.count(LInst
))
588 if (!IsAffine
&& AccType
== MemoryAccess::MUST_WRITE
)
589 AccType
= MemoryAccess::MAY_WRITE
;
591 addArrayAccess(Inst
, AccType
, BasePointer
->getValue(), ElementType
, IsAffine
,
592 {AccessFunction
}, {nullptr}, Val
);
595 void ScopBuilder::buildMemoryAccess(MemAccInst Inst
, ScopStmt
*Stmt
) {
597 if (buildAccessMemIntrinsic(Inst
, Stmt
))
600 if (buildAccessCallInst(Inst
, Stmt
))
603 if (buildAccessMultiDimFixed(Inst
, Stmt
))
606 if (buildAccessMultiDimParam(Inst
, Stmt
))
609 buildAccessSingleDim(Inst
, Stmt
);
612 void ScopBuilder::buildAccessFunctions(Region
&SR
) {
614 if (scop
->isNonAffineSubRegion(&SR
)) {
615 for (BasicBlock
*BB
: SR
.blocks())
616 buildAccessFunctions(*BB
, &SR
);
620 for (auto I
= SR
.element_begin(), E
= SR
.element_end(); I
!= E
; ++I
)
621 if (I
->isSubRegion())
622 buildAccessFunctions(*I
->getNodeAs
<Region
>());
624 buildAccessFunctions(*I
->getNodeAs
<BasicBlock
>());
627 void ScopBuilder::buildStmts(Region
&SR
) {
629 if (scop
->isNonAffineSubRegion(&SR
)) {
630 Loop
*SurroundingLoop
= LI
.getLoopFor(SR
.getEntry());
631 auto &BoxedLoops
= scop
->getBoxedLoops();
632 while (BoxedLoops
.count(SurroundingLoop
))
633 SurroundingLoop
= SurroundingLoop
->getParentLoop();
634 scop
->addScopStmt(&SR
, SurroundingLoop
);
638 for (auto I
= SR
.element_begin(), E
= SR
.element_end(); I
!= E
; ++I
)
639 if (I
->isSubRegion())
640 buildStmts(*I
->getNodeAs
<Region
>());
642 std::vector
<Instruction
*> Instructions
;
643 for (Instruction
&Inst
: *I
->getNodeAs
<BasicBlock
>()) {
644 Loop
*L
= LI
.getLoopFor(Inst
.getParent());
645 if (!isa
<TerminatorInst
>(&Inst
) && !isIgnoredIntrinsic(&Inst
) &&
646 !canSynthesize(&Inst
, *scop
, &SE
, L
))
647 Instructions
.push_back(&Inst
);
649 Loop
*SurroundingLoop
= LI
.getLoopFor(I
->getNodeAs
<BasicBlock
>());
650 scop
->addScopStmt(I
->getNodeAs
<BasicBlock
>(), SurroundingLoop
,
655 void ScopBuilder::buildAccessFunctions(BasicBlock
&BB
,
656 Region
*NonAffineSubRegion
,
658 // We do not build access functions for error blocks, as they may contain
659 // instructions we can not model.
660 if (isErrorBlock(BB
, scop
->getRegion(), LI
, DT
) && !IsExitBlock
)
663 ScopStmt
*Stmt
= scop
->getStmtFor(&BB
);
665 for (Instruction
&Inst
: BB
) {
666 PHINode
*PHI
= dyn_cast
<PHINode
>(&Inst
);
668 buildPHIAccesses(PHI
, NonAffineSubRegion
, IsExitBlock
);
670 // For the exit block we stop modeling after the last PHI node.
671 if (!PHI
&& IsExitBlock
)
674 if (auto MemInst
= MemAccInst::dyn_cast(Inst
)) {
675 assert(Stmt
&& "Cannot build access function in non-existing statement");
676 buildMemoryAccess(MemInst
, Stmt
);
679 if (isIgnoredIntrinsic(&Inst
))
682 // PHI nodes have already been modeled above and TerminatorInsts that are
683 // not part of a non-affine subregion are fully modeled and regenerated
684 // from the polyhedral domains. Hence, they do not need to be modeled as
685 // explicit data dependences.
686 if (!PHI
&& (!isa
<TerminatorInst
>(&Inst
) || NonAffineSubRegion
))
687 buildScalarDependences(&Inst
);
690 buildEscapingDependences(&Inst
);
694 MemoryAccess
*ScopBuilder::addMemoryAccess(
695 BasicBlock
*BB
, Instruction
*Inst
, MemoryAccess::AccessType AccType
,
696 Value
*BaseAddress
, Type
*ElementType
, bool Affine
, Value
*AccessValue
,
697 ArrayRef
<const SCEV
*> Subscripts
, ArrayRef
<const SCEV
*> Sizes
,
699 ScopStmt
*Stmt
= scop
->getStmtFor(BB
);
701 // Do not create a memory access for anything not in the SCoP. It would be
706 bool isKnownMustAccess
= false;
708 // Accesses in single-basic block statements are always excuted.
709 if (Stmt
->isBlockStmt())
710 isKnownMustAccess
= true;
712 if (Stmt
->isRegionStmt()) {
713 // Accesses that dominate the exit block of a non-affine region are always
714 // executed. In non-affine regions there may exist MemoryKind::Values that
715 // do not dominate the exit. MemoryKind::Values will always dominate the
716 // exit and MemoryKind::PHIs only if there is at most one PHI_WRITE in the
717 // non-affine region.
718 if (DT
.dominates(BB
, Stmt
->getRegion()->getExit()))
719 isKnownMustAccess
= true;
722 // Non-affine PHI writes do not "happen" at a particular instruction, but
723 // after exiting the statement. Therefore they are guaranteed to execute and
724 // overwrite the old value.
725 if (Kind
== MemoryKind::PHI
|| Kind
== MemoryKind::ExitPHI
)
726 isKnownMustAccess
= true;
728 if (!isKnownMustAccess
&& AccType
== MemoryAccess::MUST_WRITE
)
729 AccType
= MemoryAccess::MAY_WRITE
;
731 auto *Access
= new MemoryAccess(Stmt
, Inst
, AccType
, BaseAddress
, ElementType
,
732 Affine
, Subscripts
, Sizes
, AccessValue
, Kind
);
734 scop
->addAccessFunction(Access
);
735 Stmt
->addAccess(Access
);
739 void ScopBuilder::addArrayAccess(
740 MemAccInst MemAccInst
, MemoryAccess::AccessType AccType
, Value
*BaseAddress
,
741 Type
*ElementType
, bool IsAffine
, ArrayRef
<const SCEV
*> Subscripts
,
742 ArrayRef
<const SCEV
*> Sizes
, Value
*AccessValue
) {
743 ArrayBasePointers
.insert(BaseAddress
);
744 auto *MemAccess
= addMemoryAccess(
745 MemAccInst
->getParent(), MemAccInst
, AccType
, BaseAddress
, ElementType
,
746 IsAffine
, AccessValue
, Subscripts
, Sizes
, MemoryKind::Array
);
748 if (!DetectFortranArrays
)
751 if (Value
*FAD
= findFADAllocationInvisible(MemAccInst
))
752 MemAccess
->setFortranArrayDescriptor(FAD
);
753 else if (Value
*FAD
= findFADAllocationVisible(MemAccInst
))
754 MemAccess
->setFortranArrayDescriptor(FAD
);
757 void ScopBuilder::ensureValueWrite(Instruction
*Inst
) {
758 ScopStmt
*Stmt
= scop
->getStmtFor(Inst
);
760 // Inst not defined within this SCoP.
764 // Do not process further if the instruction is already written.
765 if (Stmt
->lookupValueWriteOf(Inst
))
768 addMemoryAccess(Inst
->getParent(), Inst
, MemoryAccess::MUST_WRITE
, Inst
,
769 Inst
->getType(), true, Inst
, ArrayRef
<const SCEV
*>(),
770 ArrayRef
<const SCEV
*>(), MemoryKind::Value
);
773 void ScopBuilder::ensureValueRead(Value
*V
, BasicBlock
*UserBB
) {
774 ScopStmt
*UserStmt
= scop
->getStmtFor(UserBB
);
775 auto *Scope
= LI
.getLoopFor(UserBB
);
776 auto VUse
= VirtualUse::create(scop
.get(), UserStmt
, Scope
, V
, false);
777 switch (VUse
.getKind()) {
778 case VirtualUse::Constant
:
779 case VirtualUse::Block
:
780 case VirtualUse::Synthesizable
:
781 case VirtualUse::Hoisted
:
782 case VirtualUse::Intra
:
783 // Uses of these kinds do not need a MemoryAccess.
786 case VirtualUse::ReadOnly
:
787 // Add MemoryAccess for invariant values only if requested.
788 if (!ModelReadOnlyScalars
)
792 case VirtualUse::Inter
:
794 // Do not create another MemoryAccess for reloading the value if one already
796 if (UserStmt
->lookupValueReadOf(V
))
799 addMemoryAccess(UserBB
, nullptr, MemoryAccess::READ
, V
, V
->getType(), true,
800 V
, ArrayRef
<const SCEV
*>(), ArrayRef
<const SCEV
*>(),
803 // Inter-statement uses need to write the value in their defining statement.
805 ensureValueWrite(cast
<Instruction
>(V
));
810 void ScopBuilder::ensurePHIWrite(PHINode
*PHI
, BasicBlock
*IncomingBlock
,
811 Value
*IncomingValue
, bool IsExitBlock
) {
812 // As the incoming block might turn out to be an error statement ensure we
813 // will create an exit PHI SAI object. It is needed during code generation
814 // and would be created later anyway.
816 scop
->getOrCreateScopArrayInfo(PHI
, PHI
->getType(), {},
817 MemoryKind::ExitPHI
);
819 ScopStmt
*IncomingStmt
= scop
->getStmtFor(IncomingBlock
);
823 // Take care for the incoming value being available in the incoming block.
824 // This must be done before the check for multiple PHI writes because multiple
825 // exiting edges from subregion each can be the effective written value of the
826 // subregion. As such, all of them must be made available in the subregion
828 ensureValueRead(IncomingValue
, IncomingBlock
);
830 // Do not add more than one MemoryAccess per PHINode and ScopStmt.
831 if (MemoryAccess
*Acc
= IncomingStmt
->lookupPHIWriteOf(PHI
)) {
832 assert(Acc
->getAccessInstruction() == PHI
);
833 Acc
->addIncoming(IncomingBlock
, IncomingValue
);
838 addMemoryAccess(IncomingStmt
->getEntryBlock(), PHI
,
839 MemoryAccess::MUST_WRITE
, PHI
, PHI
->getType(), true, PHI
,
840 ArrayRef
<const SCEV
*>(), ArrayRef
<const SCEV
*>(),
841 IsExitBlock
? MemoryKind::ExitPHI
: MemoryKind::PHI
);
843 Acc
->addIncoming(IncomingBlock
, IncomingValue
);
846 void ScopBuilder::addPHIReadAccess(PHINode
*PHI
) {
847 addMemoryAccess(PHI
->getParent(), PHI
, MemoryAccess::READ
, PHI
,
848 PHI
->getType(), true, PHI
, ArrayRef
<const SCEV
*>(),
849 ArrayRef
<const SCEV
*>(), MemoryKind::PHI
);
853 static void verifyUse(Scop
*S
, Use
&Op
, LoopInfo
&LI
) {
854 auto PhysUse
= VirtualUse::create(S
, Op
, &LI
, false);
855 auto VirtUse
= VirtualUse::create(S
, Op
, &LI
, true);
856 assert(PhysUse
.getKind() == VirtUse
.getKind());
859 /// Check the consistency of every statement's MemoryAccesses.
861 /// The check is carried out by expecting the "physical" kind of use (derived
862 /// from the BasicBlocks instructions resides in) to be same as the "virtual"
863 /// kind of use (derived from a statement's MemoryAccess).
865 /// The "physical" uses are taken by ensureValueRead to determine whether to
866 /// create MemoryAccesses. When done, the kind of scalar access should be the
867 /// same no matter which way it was derived.
869 /// The MemoryAccesses might be changed by later SCoP-modifying passes and hence
870 /// can intentionally influence on the kind of uses (not corresponding to the
871 /// "physical" anymore, hence called "virtual"). The CodeGenerator therefore has
872 /// to pick up the virtual uses. But here in the code generator, this has not
873 /// happened yet, such that virtual and physical uses are equivalent.
874 static void verifyUses(Scop
*S
, LoopInfo
&LI
, DominatorTree
&DT
) {
875 for (auto *BB
: S
->getRegion().blocks()) {
876 auto *Stmt
= S
->getStmtFor(BB
);
880 for (auto &Inst
: *BB
) {
881 if (isIgnoredIntrinsic(&Inst
))
884 // Branch conditions are encoded in the statement domains.
885 if (isa
<TerminatorInst
>(&Inst
) && Stmt
->isBlockStmt())
889 for (auto &Op
: Inst
.operands())
890 verifyUse(S
, Op
, LI
);
892 // Stores do not produce values used by other statements.
893 if (isa
<StoreInst
>(Inst
))
896 // For every value defined in the block, also check that a use of that
897 // value in the same statement would not be an inter-statement use. It can
898 // still be synthesizable or load-hoisted, but these kind of instructions
899 // are not directly copied in code-generation.
901 VirtualUse::create(S
, Stmt
, Stmt
->getSurroundingLoop(), &Inst
, true);
902 assert(VirtDef
.getKind() == VirtualUse::Synthesizable
||
903 VirtDef
.getKind() == VirtualUse::Intra
||
904 VirtDef
.getKind() == VirtualUse::Hoisted
);
908 if (S
->hasSingleExitEdge())
911 // PHINodes in the SCoP region's exit block are also uses to be checked.
912 if (!S
->getRegion().isTopLevelRegion()) {
913 for (auto &Inst
: *S
->getRegion().getExit()) {
914 if (!isa
<PHINode
>(Inst
))
917 for (auto &Op
: Inst
.operands())
918 verifyUse(S
, Op
, LI
);
924 void ScopBuilder::buildScop(Region
&R
, AssumptionCache
&AC
) {
925 scop
.reset(new Scop(R
, SE
, LI
, *SD
.getDetectionContext(&R
)));
928 buildAccessFunctions(R
);
930 // In case the region does not have an exiting block we will later (during
931 // code generation) split the exit block. This will move potential PHI nodes
932 // from the current exit block into the new region exiting block. Hence, PHI
933 // nodes that are at this point not part of the region will be.
934 // To handle these PHI nodes later we will now model their operands as scalar
935 // accesses. Note that we do not model anything in the exit block if we have
936 // an exiting block in the region, as there will not be any splitting later.
937 if (!R
.isTopLevelRegion() && !scop
->hasSingleExitEdge())
938 buildAccessFunctions(*R
.getExit(), nullptr,
939 /* IsExitBlock */ true);
941 // Create memory accesses for global reads since all arrays are now known.
942 auto *AF
= SE
.getConstant(IntegerType::getInt64Ty(SE
.getContext()), 0);
943 for (auto *GlobalRead
: GlobalReads
)
944 for (auto *BP
: ArrayBasePointers
)
945 addArrayAccess(MemAccInst(GlobalRead
), MemoryAccess::READ
, BP
,
946 BP
->getType(), false, {AF
}, {nullptr}, GlobalRead
);
948 scop
->buildInvariantEquivalenceClasses();
950 if (!scop
->buildDomains(&R
, DT
, LI
))
953 scop
->addUserAssumptions(AC
, DT
, LI
);
955 // Remove empty statements.
956 // Exit early in case there are no executable statements left in this scop.
957 scop
->simplifySCoP(false);
961 // The ScopStmts now have enough information to initialize themselves.
962 for (ScopStmt
&Stmt
: *scop
)
965 // Check early for a feasible runtime context.
966 if (!scop
->hasFeasibleRuntimeContext())
969 // Check early for profitability. Afterwards it cannot change anymore,
970 // only the runtime context could become infeasible.
971 if (!scop
->isProfitable(UnprofitableScalarAccs
)) {
972 scop
->invalidate(PROFITABLE
, DebugLoc());
976 scop
->buildSchedule(LI
);
978 scop
->finalizeAccesses();
980 scop
->realignParams();
981 scop
->addUserContext();
983 // After the context was fully constructed, thus all our knowledge about
984 // the parameters is in there, we add all recorded assumptions to the
985 // assumed/invalid context.
986 scop
->addRecordedAssumptions();
988 scop
->simplifyContexts();
989 if (!scop
->buildAliasChecks(AA
))
992 scop
->hoistInvariantLoads();
993 scop
->canonicalizeDynamicBasePtrs();
994 scop
->verifyInvariantLoads();
995 scop
->simplifySCoP(true);
997 // Check late for a feasible runtime context because profitability did not
999 if (!scop
->hasFeasibleRuntimeContext())
1003 verifyUses(scop
.get(), LI
, DT
);
1007 ScopBuilder::ScopBuilder(Region
*R
, AssumptionCache
&AC
, AliasAnalysis
&AA
,
1008 const DataLayout
&DL
, DominatorTree
&DT
, LoopInfo
&LI
,
1009 ScopDetection
&SD
, ScalarEvolution
&SE
)
1010 : AA(AA
), DL(DL
), DT(DT
), LI(LI
), SD(SD
), SE(SE
) {
1012 Function
*F
= R
->getEntry()->getParent();
1015 getDebugLocations(getBBPairForRegion(R
), Beg
, End
);
1016 std::string Msg
= "SCoP begins here.";
1017 emitOptimizationRemarkAnalysis(F
->getContext(), DEBUG_TYPE
, *F
, Beg
, Msg
);
1021 DEBUG(scop
->print(dbgs()));
1023 if (!scop
->hasFeasibleRuntimeContext()) {
1025 Msg
= "SCoP ends here but was dismissed.";
1028 Msg
= "SCoP ends here.";
1030 if (scop
->getMaxLoopDepth() > 0)
1034 emitOptimizationRemarkAnalysis(F
->getContext(), DEBUG_TYPE
, *F
, End
, Msg
);