2018-03-16 08:37:51 +08:00
|
|
|
//===- ThreadSafetyTIL.cpp ------------------------------------------------===//
|
2014-05-15 08:50:36 +08:00
|
|
|
//
|
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
|
2014-05-15 08:50:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/Analyses/ThreadSafetyTIL.h"
|
2018-03-16 08:37:51 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace threadSafety;
|
|
|
|
using namespace til;
|
2014-05-15 08:50:36 +08:00
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
StringRef til::getUnaryOpcodeString(TIL_UnaryOpcode Op) {
|
2014-05-15 08:50:36 +08:00
|
|
|
switch (Op) {
|
|
|
|
case UOP_Minus: return "-";
|
|
|
|
case UOP_BitNot: return "~";
|
|
|
|
case UOP_LogicNot: return "!";
|
|
|
|
}
|
2018-03-16 08:37:51 +08:00
|
|
|
return {};
|
2014-05-15 08:50:36 +08:00
|
|
|
}
|
|
|
|
|
2015-03-09 22:19:54 +08:00
|
|
|
StringRef til::getBinaryOpcodeString(TIL_BinaryOpcode Op) {
|
2014-05-15 08:50:36 +08:00
|
|
|
switch (Op) {
|
|
|
|
case BOP_Mul: return "*";
|
|
|
|
case BOP_Div: return "/";
|
|
|
|
case BOP_Rem: return "%";
|
|
|
|
case BOP_Add: return "+";
|
|
|
|
case BOP_Sub: return "-";
|
|
|
|
case BOP_Shl: return "<<";
|
|
|
|
case BOP_Shr: return ">>";
|
|
|
|
case BOP_BitAnd: return "&";
|
|
|
|
case BOP_BitXor: return "^";
|
|
|
|
case BOP_BitOr: return "|";
|
|
|
|
case BOP_Eq: return "==";
|
|
|
|
case BOP_Neq: return "!=";
|
|
|
|
case BOP_Lt: return "<";
|
|
|
|
case BOP_Leq: return "<=";
|
2017-12-14 23:16:18 +08:00
|
|
|
case BOP_Cmp: return "<=>";
|
2014-05-15 08:50:36 +08:00
|
|
|
case BOP_LogicAnd: return "&&";
|
|
|
|
case BOP_LogicOr: return "||";
|
|
|
|
}
|
2018-03-16 08:37:51 +08:00
|
|
|
return {};
|
2014-05-15 08:50:36 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 06:12:52 +08:00
|
|
|
SExpr* Future::force() {
|
|
|
|
Status = FS_evaluating;
|
|
|
|
Result = compute();
|
|
|
|
Status = FS_done;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2014-05-29 05:28:13 +08:00
|
|
|
unsigned BasicBlock::addPredecessor(BasicBlock *Pred) {
|
|
|
|
unsigned Idx = Predecessors.size();
|
|
|
|
Predecessors.reserveCheck(1, Arena);
|
|
|
|
Predecessors.push_back(Pred);
|
2018-03-16 08:37:51 +08:00
|
|
|
for (auto *E : Args) {
|
|
|
|
if (auto *Ph = dyn_cast<Phi>(E)) {
|
2014-05-29 05:28:13 +08:00
|
|
|
Ph->values().reserveCheck(1, Arena);
|
|
|
|
Ph->values().push_back(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasicBlock::reservePredecessors(unsigned NumPreds) {
|
|
|
|
Predecessors.reserve(NumPreds, Arena);
|
2018-03-16 08:37:51 +08:00
|
|
|
for (auto *E : Args) {
|
|
|
|
if (auto *Ph = dyn_cast<Phi>(E)) {
|
2014-05-29 05:28:13 +08:00
|
|
|
Ph->values().reserve(NumPreds, Arena);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
// If E is a variable, then trace back through any aliases or redundant
|
|
|
|
// Phi nodes to find the canonical definition.
|
2015-03-09 22:19:54 +08:00
|
|
|
const SExpr *til::getCanonicalVal(const SExpr *E) {
|
2014-09-11 06:12:52 +08:00
|
|
|
while (true) {
|
2018-03-16 08:37:51 +08:00
|
|
|
if (const auto *V = dyn_cast<Variable>(E)) {
|
2014-09-11 06:12:52 +08:00
|
|
|
if (V->kind() == Variable::VK_Let) {
|
|
|
|
E = V->definition();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 08:37:51 +08:00
|
|
|
if (const auto *Ph = dyn_cast<Phi>(E)) {
|
2014-07-28 23:57:27 +08:00
|
|
|
if (Ph->status() == Phi::PH_SingleVal) {
|
|
|
|
E = Ph->values()[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 06:12:52 +08:00
|
|
|
break;
|
2014-07-28 23:57:27 +08:00
|
|
|
}
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2014-05-15 08:50:36 +08:00
|
|
|
// If E is a variable, then trace back through any aliases or redundant
|
|
|
|
// Phi nodes to find the canonical definition.
|
2014-07-28 23:57:27 +08:00
|
|
|
// The non-const version will simplify incomplete Phi nodes.
|
2015-03-09 22:19:54 +08:00
|
|
|
SExpr *til::simplifyToCanonicalVal(SExpr *E) {
|
2014-09-11 06:12:52 +08:00
|
|
|
while (true) {
|
|
|
|
if (auto *V = dyn_cast<Variable>(E)) {
|
2014-05-15 08:50:36 +08:00
|
|
|
if (V->kind() != Variable::VK_Let)
|
|
|
|
return V;
|
2014-09-11 06:12:52 +08:00
|
|
|
// Eliminate redundant variables, e.g. x = y, or x = 5,
|
|
|
|
// but keep anything more complicated.
|
|
|
|
if (til::ThreadSafetyTIL::isTrivial(V->definition())) {
|
|
|
|
E = V->definition();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
if (auto *Ph = dyn_cast<Phi>(E)) {
|
2014-05-15 08:50:36 +08:00
|
|
|
if (Ph->status() == Phi::PH_Incomplete)
|
2014-09-11 06:12:52 +08:00
|
|
|
simplifyIncompleteArg(Ph);
|
|
|
|
// Eliminate redundant Phi nodes.
|
2014-05-15 08:50:36 +08:00
|
|
|
if (Ph->status() == Phi::PH_SingleVal) {
|
|
|
|
E = Ph->values()[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 06:12:52 +08:00
|
|
|
return E;
|
2014-05-15 08:50:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace the arguments of an incomplete Phi node to see if they have the same
|
|
|
|
// canonical definition. If so, mark the Phi node as redundant.
|
|
|
|
// getCanonicalVal() will recursively call simplifyIncompletePhi().
|
2015-03-09 22:19:54 +08:00
|
|
|
void til::simplifyIncompleteArg(til::Phi *Ph) {
|
2014-05-15 08:50:36 +08:00
|
|
|
assert(Ph && Ph->status() == Phi::PH_Incomplete);
|
|
|
|
|
|
|
|
// eliminate infinite recursion -- assume that this node is not redundant.
|
|
|
|
Ph->setStatus(Phi::PH_MultiVal);
|
|
|
|
|
2014-07-28 23:57:27 +08:00
|
|
|
SExpr *E0 = simplifyToCanonicalVal(Ph->values()[0]);
|
2018-03-16 08:37:51 +08:00
|
|
|
for (unsigned i = 1, n = Ph->values().size(); i < n; ++i) {
|
2014-07-28 23:57:27 +08:00
|
|
|
SExpr *Ei = simplifyToCanonicalVal(Ph->values()[i]);
|
2014-09-11 06:12:52 +08:00
|
|
|
if (Ei == Ph)
|
2014-05-15 08:50:36 +08:00
|
|
|
continue; // Recursive reference to itself. Don't count.
|
|
|
|
if (Ei != E0) {
|
|
|
|
return; // Status is already set to MultiVal.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ph->setStatus(Phi::PH_SingleVal);
|
|
|
|
}
|
|
|
|
|
2014-09-11 06:12:52 +08:00
|
|
|
// Renumbers the arguments and instructions to have unique, sequential IDs.
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned BasicBlock::renumberInstrs(unsigned ID) {
|
2014-09-11 06:12:52 +08:00
|
|
|
for (auto *Arg : Args)
|
|
|
|
Arg->setID(this, ID++);
|
|
|
|
for (auto *Instr : Instrs)
|
|
|
|
Instr->setID(this, ID++);
|
|
|
|
TermInstr->setID(this, ID++);
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorts the CFGs blocks using a reverse post-order depth-first traversal.
|
|
|
|
// Each block will be written into the Blocks array in order, and its BlockID
|
|
|
|
// will be set to the index in the array. Sorting should start from the entry
|
|
|
|
// block, and ID should be the total number of blocks.
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned BasicBlock::topologicalSort(SimpleArray<BasicBlock *> &Blocks,
|
|
|
|
unsigned ID) {
|
2014-09-11 06:12:52 +08:00
|
|
|
if (Visited) return ID;
|
2014-09-12 20:42:15 +08:00
|
|
|
Visited = true;
|
2014-09-11 06:12:52 +08:00
|
|
|
for (auto *Block : successors())
|
|
|
|
ID = Block->topologicalSort(Blocks, ID);
|
|
|
|
// set ID and update block array in place.
|
|
|
|
// We may lose pointers to unreachable blocks.
|
|
|
|
assert(ID > 0);
|
|
|
|
BlockID = --ID;
|
|
|
|
Blocks[BlockID] = this;
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Performs a reverse topological traversal, starting from the exit block and
|
|
|
|
// following back-edges. The dominator is serialized before any predecessors,
|
|
|
|
// which guarantees that all blocks are serialized after their dominator and
|
|
|
|
// before their post-dominator (because it's a reverse topological traversal).
|
|
|
|
// ID should be initially set to 0.
|
|
|
|
//
|
|
|
|
// This sort assumes that (1) dominators have been computed, (2) there are no
|
|
|
|
// critical edges, and (3) the entry block is reachable from the exit block
|
2017-03-30 22:13:19 +08:00
|
|
|
// and no blocks are accessible via traversal of back-edges from the exit that
|
|
|
|
// weren't accessible via forward edges from the entry.
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned BasicBlock::topologicalFinalSort(SimpleArray<BasicBlock *> &Blocks,
|
|
|
|
unsigned ID) {
|
2014-09-11 06:12:52 +08:00
|
|
|
// Visited is assumed to have been set by the topologicalSort. This pass
|
|
|
|
// assumes !Visited means that we've visited this node before.
|
|
|
|
if (!Visited) return ID;
|
2014-09-12 20:42:15 +08:00
|
|
|
Visited = false;
|
2014-09-11 06:12:52 +08:00
|
|
|
if (DominatorNode.Parent)
|
|
|
|
ID = DominatorNode.Parent->topologicalFinalSort(Blocks, ID);
|
|
|
|
for (auto *Pred : Predecessors)
|
|
|
|
ID = Pred->topologicalFinalSort(Blocks, ID);
|
2014-09-11 11:37:42 +08:00
|
|
|
assert(static_cast<size_t>(ID) < Blocks.size());
|
2014-09-11 06:12:52 +08:00
|
|
|
BlockID = ID++;
|
|
|
|
Blocks[BlockID] = this;
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computes the immediate dominator of the current block. Assumes that all of
|
|
|
|
// its predecessors have already computed their dominators. This is achieved
|
|
|
|
// by visiting the nodes in topological order.
|
|
|
|
void BasicBlock::computeDominator() {
|
|
|
|
BasicBlock *Candidate = nullptr;
|
|
|
|
// Walk backwards from each predecessor to find the common dominator node.
|
|
|
|
for (auto *Pred : Predecessors) {
|
|
|
|
// Skip back-edges
|
|
|
|
if (Pred->BlockID >= BlockID) continue;
|
|
|
|
// If we don't yet have a candidate for dominator yet, take this one.
|
|
|
|
if (Candidate == nullptr) {
|
|
|
|
Candidate = Pred;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Walk the alternate and current candidate back to find a common ancestor.
|
|
|
|
auto *Alternate = Pred;
|
|
|
|
while (Alternate != Candidate) {
|
|
|
|
if (Candidate->BlockID > Alternate->BlockID)
|
|
|
|
Candidate = Candidate->DominatorNode.Parent;
|
|
|
|
else
|
|
|
|
Alternate = Alternate->DominatorNode.Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DominatorNode.Parent = Candidate;
|
|
|
|
DominatorNode.SizeOfSubTree = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Computes the immediate post-dominator of the current block. Assumes that all
|
|
|
|
// of its successors have already computed their post-dominators. This is
|
|
|
|
// achieved visiting the nodes in reverse topological order.
|
|
|
|
void BasicBlock::computePostDominator() {
|
|
|
|
BasicBlock *Candidate = nullptr;
|
|
|
|
// Walk back from each predecessor to find the common post-dominator node.
|
|
|
|
for (auto *Succ : successors()) {
|
|
|
|
// Skip back-edges
|
|
|
|
if (Succ->BlockID <= BlockID) continue;
|
|
|
|
// If we don't yet have a candidate for post-dominator yet, take this one.
|
|
|
|
if (Candidate == nullptr) {
|
|
|
|
Candidate = Succ;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Walk the alternate and current candidate back to find a common ancestor.
|
|
|
|
auto *Alternate = Succ;
|
|
|
|
while (Alternate != Candidate) {
|
|
|
|
if (Candidate->BlockID < Alternate->BlockID)
|
|
|
|
Candidate = Candidate->PostDominatorNode.Parent;
|
|
|
|
else
|
|
|
|
Alternate = Alternate->PostDominatorNode.Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PostDominatorNode.Parent = Candidate;
|
|
|
|
PostDominatorNode.SizeOfSubTree = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renumber instructions in all blocks
|
|
|
|
void SCFG::renumberInstrs() {
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned InstrID = 0;
|
2014-09-11 06:12:52 +08:00
|
|
|
for (auto *Block : Blocks)
|
|
|
|
InstrID = Block->renumberInstrs(InstrID);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void computeNodeSize(BasicBlock *B,
|
|
|
|
BasicBlock::TopologyNode BasicBlock::*TN) {
|
|
|
|
BasicBlock::TopologyNode *N = &(B->*TN);
|
|
|
|
if (N->Parent) {
|
|
|
|
BasicBlock::TopologyNode *P = &(N->Parent->*TN);
|
|
|
|
// Initially set ID relative to the (as yet uncomputed) parent ID
|
|
|
|
N->NodeID = P->SizeOfSubTree;
|
|
|
|
P->SizeOfSubTree += N->SizeOfSubTree;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void computeNodeID(BasicBlock *B,
|
|
|
|
BasicBlock::TopologyNode BasicBlock::*TN) {
|
|
|
|
BasicBlock::TopologyNode *N = &(B->*TN);
|
|
|
|
if (N->Parent) {
|
|
|
|
BasicBlock::TopologyNode *P = &(N->Parent->*TN);
|
|
|
|
N->NodeID += P->NodeID; // Fix NodeIDs relative to starting node.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalizes a CFG. Normalization has a few major components:
|
|
|
|
// 1) Removing unreachable blocks.
|
|
|
|
// 2) Computing dominators and post-dominators
|
|
|
|
// 3) Topologically sorting the blocks into the "Blocks" array.
|
|
|
|
void SCFG::computeNormalForm() {
|
|
|
|
// Topologically sort the blocks starting from the entry block.
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned NumUnreachableBlocks = Entry->topologicalSort(Blocks, Blocks.size());
|
2014-09-11 06:12:52 +08:00
|
|
|
if (NumUnreachableBlocks > 0) {
|
|
|
|
// If there were unreachable blocks shift everything down, and delete them.
|
2018-09-23 05:56:16 +08:00
|
|
|
for (unsigned I = NumUnreachableBlocks, E = Blocks.size(); I < E; ++I) {
|
|
|
|
unsigned NI = I - NumUnreachableBlocks;
|
2014-09-11 06:12:52 +08:00
|
|
|
Blocks[NI] = Blocks[I];
|
|
|
|
Blocks[NI]->BlockID = NI;
|
|
|
|
// FIXME: clean up predecessor pointers to unreachable blocks?
|
|
|
|
}
|
|
|
|
Blocks.drop(NumUnreachableBlocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute dominators.
|
|
|
|
for (auto *Block : Blocks)
|
|
|
|
Block->computeDominator();
|
|
|
|
|
|
|
|
// Once dominators have been computed, the final sort may be performed.
|
2018-09-23 05:56:16 +08:00
|
|
|
unsigned NumBlocks = Exit->topologicalFinalSort(Blocks, 0);
|
2014-09-11 11:37:42 +08:00
|
|
|
assert(static_cast<size_t>(NumBlocks) == Blocks.size());
|
2014-09-11 06:12:52 +08:00
|
|
|
(void) NumBlocks;
|
|
|
|
|
|
|
|
// Renumber the instructions now that we have a final sort.
|
|
|
|
renumberInstrs();
|
|
|
|
|
|
|
|
// Compute post-dominators and compute the sizes of each node in the
|
|
|
|
// dominator tree.
|
|
|
|
for (auto *Block : Blocks.reverse()) {
|
|
|
|
Block->computePostDominator();
|
|
|
|
computeNodeSize(Block, &BasicBlock::DominatorNode);
|
|
|
|
}
|
|
|
|
// Compute the sizes of each node in the post-dominator tree and assign IDs in
|
|
|
|
// the dominator tree.
|
|
|
|
for (auto *Block : Blocks) {
|
|
|
|
computeNodeID(Block, &BasicBlock::DominatorNode);
|
|
|
|
computeNodeSize(Block, &BasicBlock::PostDominatorNode);
|
|
|
|
}
|
|
|
|
// Assign IDs in the post-dominator tree.
|
|
|
|
for (auto *Block : Blocks.reverse()) {
|
|
|
|
computeNodeID(Block, &BasicBlock::PostDominatorNode);
|
|
|
|
}
|
|
|
|
}
|