2017-06-06 10:34:41 +08:00
|
|
|
//===-- OrderedInstructions.cpp - Instruction dominance function ---------===//
|
|
|
|
//
|
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
|
2017-06-06 10:34:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines utility to check dominance relation of 2 instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-30 12:49:03 +08:00
|
|
|
#include "llvm/Analysis/OrderedInstructions.h"
|
2017-06-06 10:34:41 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2018-06-21 01:42:01 +08:00
|
|
|
bool OrderedInstructions::localDominates(const Instruction *InstA,
|
|
|
|
const Instruction *InstB) const {
|
|
|
|
assert(InstA->getParent() == InstB->getParent() &&
|
|
|
|
"Instructions must be in the same basic block");
|
|
|
|
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
return InstA->comesBefore(InstB);
|
2018-06-21 01:42:01 +08:00
|
|
|
}
|
|
|
|
|
[IR] Lazily number instructions for local dominance queries
Essentially, fold OrderedBasicBlock into BasicBlock, and make it
auto-invalidate the instruction ordering when new instructions are
added. Notably, we don't need to invalidate it when removing
instructions, which is helpful when a pass mostly delete dead
instructions rather than transforming them.
The downside is that Instruction grows from 56 bytes to 64 bytes. The
resulting LLVM code is substantially simpler and automatically handles
invalidation, which makes me think that this is the right speed and size
tradeoff.
The important change is in SymbolTableTraitsImpl.h, where the numbering
is invalidated. Everything else should be straightforward.
We probably want to implement a fancier re-numbering scheme so that
local updates don't invalidate the ordering, but I plan for that to be
future work, maybe for someone else.
Reviewed By: lattner, vsk, fhahn, dexonsmith
Differential Revision: https://reviews.llvm.org/D51664
2020-02-19 06:33:54 +08:00
|
|
|
/// Given 2 instructions, check for dominance relation if the instructions are
|
|
|
|
/// in the same basic block. Otherwise, use dominator tree.
|
2017-06-06 10:34:41 +08:00
|
|
|
bool OrderedInstructions::dominates(const Instruction *InstA,
|
|
|
|
const Instruction *InstB) const {
|
|
|
|
// Use ordered basic block to do dominance check in case the 2 instructions
|
|
|
|
// are in the same basic block.
|
2018-06-21 01:42:01 +08:00
|
|
|
if (InstA->getParent() == InstB->getParent())
|
|
|
|
return localDominates(InstA, InstB);
|
2017-06-30 01:01:03 +08:00
|
|
|
return DT->dominates(InstA->getParent(), InstB->getParent());
|
2017-06-06 10:34:41 +08:00
|
|
|
}
|
2018-06-21 01:42:01 +08:00
|
|
|
|
|
|
|
bool OrderedInstructions::dfsBefore(const Instruction *InstA,
|
|
|
|
const Instruction *InstB) const {
|
|
|
|
// Use ordered basic block in case the 2 instructions are in the same basic
|
|
|
|
// block.
|
|
|
|
if (InstA->getParent() == InstB->getParent())
|
|
|
|
return localDominates(InstA, InstB);
|
|
|
|
|
|
|
|
DomTreeNode *DA = DT->getNode(InstA->getParent());
|
|
|
|
DomTreeNode *DB = DT->getNode(InstB->getParent());
|
|
|
|
return DA->getDFSNumIn() < DB->getDFSNumIn();
|
|
|
|
}
|