1 //===------ VirtualInstruction.cpp ------------------------------*- 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 // Tools for determining which instructions are within a statement and the
11 // nature of their operands.
13 //===----------------------------------------------------------------------===//
15 #include "polly/Support/VirtualInstruction.h"
16 #include "polly/Support/SCEVValidator.h"
18 using namespace polly
;
21 VirtualUse
VirtualUse ::create(Scop
*S
, Use
&U
, LoopInfo
*LI
, bool Virtual
) {
22 auto *UserBB
= getUseBlock(U
);
23 auto *UserStmt
= S
->getStmtFor(UserBB
);
24 auto *UserScope
= LI
->getLoopFor(UserBB
);
25 return create(S
, UserStmt
, UserScope
, U
.get(), Virtual
);
28 VirtualUse
VirtualUse::create(Scop
*S
, ScopStmt
*UserStmt
, Loop
*UserScope
,
29 Value
*Val
, bool Virtual
) {
30 assert(!isa
<StoreInst
>(Val
) && "a StoreInst cannot be used");
32 if (isa
<BasicBlock
>(Val
))
33 return VirtualUse(UserStmt
, Val
, Block
, nullptr, nullptr);
35 if (isa
<llvm::Constant
>(Val
))
36 return VirtualUse(UserStmt
, Val
, Constant
, nullptr, nullptr);
38 // Is the value synthesizable? If the user has been pruned
39 // (UserStmt == nullptr), it is either not used anywhere or is synthesizable.
40 // We assume synthesizable which practically should have the same effect.
41 auto *SE
= S
->getSE();
42 if (SE
->isSCEVable(Val
->getType())) {
43 auto *ScevExpr
= SE
->getSCEVAtScope(Val
, UserScope
);
44 if (!UserStmt
|| canSynthesize(Val
, *UserStmt
->getParent(), SE
, UserScope
))
45 return VirtualUse(UserStmt
, Val
, Synthesizable
, ScevExpr
, nullptr);
48 // FIXME: Inconsistency between lookupInvariantEquivClass and
49 // getRequiredInvariantLoads. Querying one of them should be enough.
50 auto &RIL
= S
->getRequiredInvariantLoads();
51 if (S
->lookupInvariantEquivClass(Val
) || RIL
.count(dyn_cast
<LoadInst
>(Val
)))
52 return VirtualUse(UserStmt
, Val
, Hoisted
, nullptr, nullptr);
54 // ReadOnly uses may have MemoryAccesses that we want to associate with the
55 // use. This is why we look for a MemoryAccess here already.
56 MemoryAccess
*InputMA
= nullptr;
57 if (UserStmt
&& Virtual
)
58 InputMA
= UserStmt
->lookupValueReadOf(Val
);
60 // Uses are read-only if they have been defined before the SCoP, i.e., they
61 // cannot be written to inside the SCoP. Arguments are defined before any
62 // instructions, hence also before the SCoP. If the user has been pruned
63 // (UserStmt == nullptr) and is not SCEVable, assume it is read-only as it is
64 // neither an intra- nor an inter-use.
65 if (!UserStmt
|| isa
<Argument
>(Val
))
66 return VirtualUse(UserStmt
, Val
, ReadOnly
, nullptr, InputMA
);
68 auto Inst
= cast
<Instruction
>(Val
);
69 if (!S
->contains(Inst
))
70 return VirtualUse(UserStmt
, Val
, ReadOnly
, nullptr, InputMA
);
72 // A use is inter-statement if either it is defined in another statement, or
73 // there is a MemoryAccess that reads its value that has been written by
75 if (InputMA
|| (!Virtual
&& !UserStmt
->contains(Inst
->getParent())))
76 return VirtualUse(UserStmt
, Val
, Inter
, nullptr, InputMA
);
78 return VirtualUse(UserStmt
, Val
, Intra
, nullptr, nullptr);
81 void VirtualUse::print(raw_ostream
&OS
, bool Reproducible
) const {
82 OS
<< "User: [" << User
->getBaseName() << "] ";
84 case VirtualUse::Constant
:
87 case VirtualUse::Block
:
88 OS
<< "BasicBlock Op:";
90 case VirtualUse::Synthesizable
:
91 OS
<< "Synthesizable Op:";
93 case VirtualUse::Hoisted
:
94 OS
<< "Hoisted load Op:";
96 case VirtualUse::ReadOnly
:
97 OS
<< "Read-Only Op:";
99 case VirtualUse::Intra
:
102 case VirtualUse::Inter
:
110 OS
<< '"' << Val
->getName() << '"';
112 Val
->print(OS
, true);
118 if (InputMA
&& !Reproducible
)
119 OS
<< ' ' << InputMA
;
122 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
123 void VirtualUse::dump() const {
124 print(errs(), false);