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"
|
2019-11-15 07:15:48 +08:00
|
|
|
#include "llvm/Support/CommandLine.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());
|
2020-04-20 04:12:05 +08:00
|
|
|
return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
|
|
|
|
if (isSpecialInstruction(Inst))
|
|
|
|
FirstSpecialInsts.erase(Inst->getParent());
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
|
|
|
|
2021-04-09 02:44:23 +08:00
|
|
|
void InstructionPrecedenceTracking::removeUsersOf(const Instruction *Inst) {
|
|
|
|
for (const auto *U : Inst->users()) {
|
|
|
|
if (const auto *UI = dyn_cast<Instruction>(U))
|
|
|
|
removeInstruction(UI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 17:43:16 +08:00
|
|
|
void InstructionPrecedenceTracking::clear() {
|
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.
|
2020-03-30 13:13:02 +08:00
|
|
|
return !isGuaranteedToTransferExecutionToSuccessor(Insn);
|
2018-08-27 17:43:16 +08:00
|
|
|
}
|
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();
|
|
|
|
}
|