2018-08-27 17:43:16 +08:00
|
|
|
//===-- InstructionPrecedenceTracking.cpp -----------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-08-27 17:43:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implements a class that is able to define some instructions as "special"
|
|
|
|
// (e.g. as having implicit control flow, or writing memory, or having another
|
|
|
|
// interesting property) and then efficiently answers queries of the types:
|
|
|
|
// 1. Are there any special instructions in the block of interest?
|
|
|
|
// 2. Return first of the special instructions in the given block;
|
|
|
|
// 3. Check if the given instruction is preceeded by the first special
|
|
|
|
// instruction in the same block.
|
|
|
|
// The class provides caching that allows to answer these queries quickly. The
|
|
|
|
// user must make sure that the cached data is invalidated properly whenever
|
|
|
|
// a content of some tracked block is changed.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-30 12:49:03 +08:00
|
|
|
#include "llvm/Analysis/InstructionPrecedenceTracking.h"
|
2018-08-27 17:43:16 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2019-02-14 19:10:29 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2018-08-27 17:43:16 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2018-09-06 16:33:02 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static cl::opt<bool> ExpensiveAsserts(
|
|
|
|
"ipt-expensive-asserts",
|
|
|
|
cl::desc("Perform expensive assert validation on every query to Instruction"
|
|
|
|
" Precedence Tracking"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
#endif
|
|
|
|
|
2018-08-27 17:43:16 +08:00
|
|
|
const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
|
|
|
|
const BasicBlock *BB) {
|
2018-09-06 16:33:02 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// If there is a bug connected to invalid cache, turn on ExpensiveAsserts to
|
|
|
|
// catch this situation as early as possible.
|
|
|
|
if (ExpensiveAsserts)
|
|
|
|
validateAll();
|
|
|
|
else
|
|
|
|
validate(BB);
|
|
|
|
#endif
|
|
|
|
|
2018-09-06 17:29:42 +08:00
|
|
|
if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
|
2018-08-27 17:43:16 +08:00
|
|
|
fill(BB);
|
2018-09-06 17:29:42 +08:00
|
|
|
assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
|
|
|
|
}
|
|
|
|
return FirstSpecialInsts[BB];
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InstructionPrecedenceTracking::hasSpecialInstructions(
|
|
|
|
const BasicBlock *BB) {
|
|
|
|
return getFirstSpecialInstruction(BB) != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
|
|
|
|
const Instruction *Insn) {
|
2018-09-11 13:10:01 +08:00
|
|
|
const Instruction *MaybeFirstSpecial =
|
2018-08-27 17:43:16 +08:00
|
|
|
getFirstSpecialInstruction(Insn->getParent());
|
2018-09-11 13:10:01 +08:00
|
|
|
return MaybeFirstSpecial && OI.dominates(MaybeFirstSpecial, Insn);
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
|
2018-08-30 17:24:33 +08:00
|
|
|
FirstSpecialInsts.erase(BB);
|
2018-08-27 17:43:16 +08:00
|
|
|
for (auto &I : *BB)
|
|
|
|
if (isSpecialInstruction(&I)) {
|
2018-08-30 17:24:33 +08:00
|
|
|
FirstSpecialInsts[BB] = &I;
|
2018-09-06 17:29:42 +08:00
|
|
|
return;
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
2018-09-06 17:29:42 +08:00
|
|
|
// Mark this block as having no special instructions.
|
|
|
|
FirstSpecialInsts[BB] = nullptr;
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
2018-09-06 16:33:02 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
|
2018-09-06 17:29:42 +08:00
|
|
|
auto It = FirstSpecialInsts.find(BB);
|
|
|
|
// Bail if we don't have anything cached for this block.
|
|
|
|
if (It == FirstSpecialInsts.end())
|
2018-09-06 16:33:02 +08:00
|
|
|
return;
|
|
|
|
|
2018-09-06 17:29:42 +08:00
|
|
|
for (const Instruction &Insn : *BB)
|
2018-09-06 16:33:02 +08:00
|
|
|
if (isSpecialInstruction(&Insn)) {
|
|
|
|
assert(It->second == &Insn &&
|
|
|
|
"Cached first special instruction is wrong!");
|
2018-09-06 17:29:42 +08:00
|
|
|
return;
|
2018-09-06 16:33:02 +08:00
|
|
|
}
|
2018-09-06 17:29:42 +08:00
|
|
|
|
|
|
|
assert(It->second == nullptr &&
|
|
|
|
"Block is marked as having special instructions but in fact it has "
|
|
|
|
"none!");
|
2018-09-06 16:33:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionPrecedenceTracking::validateAll() const {
|
|
|
|
// Check that for every known block the cached value is correct.
|
|
|
|
for (auto &It : FirstSpecialInsts)
|
2018-09-06 17:29:42 +08:00
|
|
|
validate(It.first);
|
2018-09-06 16:33:02 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-09 15:28:13 +08:00
|
|
|
void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
|
|
|
|
const BasicBlock *BB) {
|
|
|
|
if (isSpecialInstruction(Inst))
|
|
|
|
FirstSpecialInsts.erase(BB);
|
2018-08-27 17:43:16 +08:00
|
|
|
OI.invalidateBlock(BB);
|
2019-01-09 15:28:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
|
|
|
|
if (isSpecialInstruction(Inst))
|
|
|
|
FirstSpecialInsts.erase(Inst->getParent());
|
|
|
|
OI.invalidateBlock(Inst->getParent());
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionPrecedenceTracking::clear() {
|
2018-08-30 17:24:33 +08:00
|
|
|
for (auto It : FirstSpecialInsts)
|
2018-08-27 17:43:16 +08:00
|
|
|
OI.invalidateBlock(It.first);
|
2018-08-30 17:24:33 +08:00
|
|
|
FirstSpecialInsts.clear();
|
2018-09-06 16:33:02 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// The map should be valid after clearing (at least empty).
|
|
|
|
validateAll();
|
|
|
|
#endif
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ImplicitControlFlowTracking::isSpecialInstruction(
|
|
|
|
const Instruction *Insn) const {
|
|
|
|
// If a block's instruction doesn't always pass the control to its successor
|
|
|
|
// instruction, mark the block as having implicit control flow. We use them
|
|
|
|
// to avoid wrong assumptions of sort "if A is executed and B post-dominates
|
|
|
|
// A, then B is also executed". This is not true is there is an implicit
|
|
|
|
// control flow instruction (e.g. a guard) between them.
|
|
|
|
//
|
|
|
|
// TODO: Currently, isGuaranteedToTransferExecutionToSuccessor returns false
|
|
|
|
// for volatile stores and loads because they can trap. The discussion on
|
|
|
|
// whether or not it is correct is still ongoing. We might want to get rid
|
|
|
|
// of this logic in the future. Anyways, trapping instructions shouldn't
|
|
|
|
// introduce implicit control flow, so we explicitly allow them here. This
|
|
|
|
// must be removed once isGuaranteedToTransferExecutionToSuccessor is fixed.
|
|
|
|
if (isGuaranteedToTransferExecutionToSuccessor(Insn))
|
|
|
|
return false;
|
|
|
|
if (isa<LoadInst>(Insn)) {
|
|
|
|
assert(cast<LoadInst>(Insn)->isVolatile() &&
|
|
|
|
"Non-volatile load should transfer execution to successor!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (isa<StoreInst>(Insn)) {
|
|
|
|
assert(cast<StoreInst>(Insn)->isVolatile() &&
|
|
|
|
"Non-volatile store should transfer execution to successor!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-12 17:29:58 +08:00
|
|
|
|
|
|
|
bool MemoryWriteTracking::isSpecialInstruction(
|
|
|
|
const Instruction *Insn) const {
|
2019-02-14 19:10:29 +08:00
|
|
|
using namespace PatternMatch;
|
|
|
|
if (match(Insn, m_Intrinsic<Intrinsic::experimental_widenable_condition>()))
|
|
|
|
return false;
|
2018-11-12 17:29:58 +08:00
|
|
|
return Insn->mayWriteToMemory();
|
|
|
|
}
|