Michael Kruse | eedae76 | 2017-05-04 15:22:57 +0000 | [diff] [blame^] | 1 | //===------ VirtualInstruction.cpp ------------------------------*- C++ -*-===// |
| 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 | // Tools for determining which instructions are within a statement and the |
| 11 | // nature of their operands. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "polly/Support/VirtualInstruction.h" |
| 16 | #include "polly/Support/SCEVValidator.h" |
| 17 | |
| 18 | using namespace polly; |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | /// If InputVal is not defined in the stmt itself, return the MemoryAccess that |
| 24 | /// reads the scalar. Return nullptr otherwise (if the value is defined in the |
| 25 | /// scop, or is synthesizable) |
| 26 | MemoryAccess *getInputAccessOf(Value *InputVal, ScopStmt *UserStmt) { |
| 27 | for (auto *MA : *UserStmt) { |
| 28 | if (!MA->isRead()) |
| 29 | continue; |
| 30 | if (!MA->isOriginalValueKind()) |
| 31 | continue; |
| 32 | |
| 33 | if (MA->getAccessValue() == InputVal) |
| 34 | return MA; |
| 35 | } |
| 36 | return nullptr; |
| 37 | } |
| 38 | } // namespace |
| 39 | |
| 40 | VirtualUse VirtualUse ::create(Scop *S, Use &U, LoopInfo *LI, bool Virtual) { |
| 41 | auto *UserBB = getUseBlock(U); |
| 42 | auto *UserStmt = S->getStmtFor(UserBB); |
| 43 | auto *UserScope = LI->getLoopFor(UserBB); |
| 44 | return create(S, UserStmt, UserScope, U.get(), Virtual); |
| 45 | } |
| 46 | |
| 47 | VirtualUse VirtualUse::create(Scop *S, ScopStmt *UserStmt, Loop *UserScope, |
| 48 | Value *Val, bool Virtual) { |
| 49 | assert(!isa<StoreInst>(Val) && "a StoreInst cannot be used"); |
| 50 | |
| 51 | if (isa<BasicBlock>(Val)) |
| 52 | return VirtualUse(UserStmt, Val, Block, nullptr, nullptr); |
| 53 | |
| 54 | if (isa<llvm::Constant>(Val)) |
| 55 | return VirtualUse(UserStmt, Val, Constant, nullptr, nullptr); |
| 56 | |
| 57 | // Is the value synthesizable? If the user has been pruned |
| 58 | // (UserStmt == nullptr), it is either not used anywhere or is synthesizable. |
| 59 | // We assume synthesizable which practically should have the same effect. |
| 60 | auto *SE = S->getSE(); |
| 61 | if (SE->isSCEVable(Val->getType())) { |
| 62 | auto *ScevExpr = SE->getSCEVAtScope(Val, UserScope); |
| 63 | if (!UserStmt || canSynthesize(Val, *UserStmt->getParent(), SE, UserScope)) |
| 64 | return VirtualUse(UserStmt, Val, Synthesizable, ScevExpr, nullptr); |
| 65 | } |
| 66 | |
| 67 | // FIXME: Inconsistency between lookupInvariantEquivClass and |
| 68 | // getRequiredInvariantLoads. Querying one of them should be enough. |
| 69 | auto &RIL = S->getRequiredInvariantLoads(); |
| 70 | if (S->lookupInvariantEquivClass(Val) || RIL.count(dyn_cast<LoadInst>(Val))) |
| 71 | return VirtualUse(UserStmt, Val, Hoisted, nullptr, nullptr); |
| 72 | |
| 73 | // ReadOnly uses may have MemoryAccesses that we want to associate with the |
| 74 | // use. This is why we look for a MemoryAccess here already. |
| 75 | MemoryAccess *InputMA = nullptr; |
| 76 | if (UserStmt && Virtual) |
| 77 | InputMA = getInputAccessOf(Val, UserStmt); |
| 78 | |
| 79 | // Uses are read-only if they have been defined before the SCoP, i.e., they |
| 80 | // cannot be written to inside the SCoP. Arguments are defined before any |
| 81 | // instructions, hence also before the SCoP. If the user has been pruned |
| 82 | // (UserStmt == nullptr) and is not SCEVable, assume it is read-only as it is |
| 83 | // neither an intra- nor an inter-use. |
| 84 | if (!UserStmt || isa<Argument>(Val)) |
| 85 | return VirtualUse(UserStmt, Val, ReadOnly, nullptr, InputMA); |
| 86 | |
| 87 | auto Inst = cast<Instruction>(Val); |
| 88 | if (!S->contains(Inst)) |
| 89 | return VirtualUse(UserStmt, Val, ReadOnly, nullptr, InputMA); |
| 90 | |
| 91 | // A use is inter-statement if either it is defined in another statement, or |
| 92 | // there is a MemoryAccess that reads its value that has been written by |
| 93 | // another statement. |
| 94 | if (InputMA || (!Virtual && !UserStmt->contains(Inst->getParent()))) |
| 95 | return VirtualUse(UserStmt, Val, Inter, nullptr, InputMA); |
| 96 | |
| 97 | return VirtualUse(UserStmt, Val, Intra, nullptr, nullptr); |
| 98 | } |
| 99 | |
| 100 | void VirtualUse::print(raw_ostream &OS, bool Reproducible) const { |
| 101 | OS << "User: [" << User->getBaseName() << "] "; |
| 102 | switch (Kind) { |
| 103 | case VirtualUse::Constant: |
| 104 | OS << "Constant Op:"; |
| 105 | break; |
| 106 | case VirtualUse::Block: |
| 107 | OS << "BasicBlock Op:"; |
| 108 | break; |
| 109 | case VirtualUse::Synthesizable: |
| 110 | OS << "Synthesizable Op:"; |
| 111 | break; |
| 112 | case VirtualUse::Hoisted: |
| 113 | OS << "Hoisted load Op:"; |
| 114 | break; |
| 115 | case VirtualUse::ReadOnly: |
| 116 | OS << "Read-Only Op:"; |
| 117 | break; |
| 118 | case VirtualUse::Intra: |
| 119 | OS << "Intra Op:"; |
| 120 | break; |
| 121 | case VirtualUse::Inter: |
| 122 | OS << "Inter Op:"; |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | if (Val) { |
| 127 | OS << ' '; |
| 128 | if (Reproducible) |
| 129 | OS << '"' << Val->getName() << '"'; |
| 130 | else |
| 131 | Val->print(OS, true); |
| 132 | } |
| 133 | if (ScevExpr) { |
| 134 | OS << ' '; |
| 135 | ScevExpr->print(OS); |
| 136 | } |
| 137 | if (InputMA && !Reproducible) |
| 138 | OS << ' ' << InputMA; |
| 139 | } |
| 140 | |
| 141 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 142 | void VirtualUse::dump() const { |
| 143 | print(errs(), false); |
| 144 | errs() << '\n'; |
| 145 | } |
| 146 | #endif |