2018-03-20 23:45:23 +01:00
|
|
|
//===- MustExecute.h - Is an instruction known to execute--------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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-03-20 23:45:23 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// \file
|
|
|
|
/// Contains a collection of routines for determining if a given instruction is
|
|
|
|
/// guaranteed to execute if a given point in control flow is reached. The most
|
|
|
|
/// common example is an instruction within a loop being provably executed if we
|
2018-07-30 21:41:25 +02:00
|
|
|
/// branch to the header of it's containing loop.
|
2018-03-20 23:45:23 +01:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_MUSTEXECUTE_H
|
|
|
|
#define LLVM_ANALYSIS_MUSTEXECUTE_H
|
|
|
|
|
2018-03-21 18:31:45 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/Analysis/EHPersonalities.h"
|
2018-10-16 11:58:09 +02:00
|
|
|
#include "llvm/Analysis/InstructionPrecedenceTracking.h"
|
2018-03-21 18:31:45 +01:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
|
2018-03-20 23:45:23 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class Instruction;
|
|
|
|
class DominatorTree;
|
|
|
|
class Loop;
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Captures loop safety information.
|
2018-08-14 09:40:08 +02:00
|
|
|
/// It keep information for loop blocks may throw exception or otherwise
|
2018-03-20 23:45:23 +01:00
|
|
|
/// exit abnormaly on any iteration of the loop which might actually execute
|
|
|
|
/// at runtime. The primary way to consume this infromation is via
|
|
|
|
/// isGuaranteedToExecute below, but some callers bailout or fallback to
|
|
|
|
/// alternate reasoning if a loop contains any implicit control flow.
|
2018-08-15 07:55:43 +02:00
|
|
|
/// NOTE: LoopSafetyInfo contains cached information regarding loops and their
|
|
|
|
/// particular blocks. This information is only dropped on invocation of
|
|
|
|
/// computeLoopSafetyInfo. If the loop or any of its block is deleted, or if
|
|
|
|
/// any thrower instructions have been added or removed from them, or if the
|
|
|
|
/// control flow has changed, or in case of other meaningful modifications, the
|
|
|
|
/// LoopSafetyInfo needs to be recomputed. If a meaningful modifications to the
|
|
|
|
/// loop were made and the info wasn't recomputed properly, the behavior of all
|
|
|
|
/// methods except for computeLoopSafetyInfo is undefined.
|
|
|
|
class LoopSafetyInfo {
|
2018-10-16 10:07:14 +02:00
|
|
|
// Used to update funclet bundle operands.
|
|
|
|
DenseMap<BasicBlock *, ColorVector> BlockColors;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Computes block colors.
|
|
|
|
void computeBlockColors(const Loop *CurLoop);
|
|
|
|
|
2018-08-15 07:55:43 +02:00
|
|
|
public:
|
2018-10-16 10:07:14 +02:00
|
|
|
/// Returns block colors map that is used to update funclet operand bundles.
|
|
|
|
const DenseMap<BasicBlock *, ColorVector> &getBlockColors() const;
|
|
|
|
|
|
|
|
/// Copy colors of block \p Old into the block \p New.
|
|
|
|
void copyColors(BasicBlock *New, BasicBlock *Old);
|
2018-03-20 23:45:23 +01:00
|
|
|
|
2018-10-16 09:50:14 +02:00
|
|
|
/// Returns true iff the block \p BB potentially may throw exception. It can
|
|
|
|
/// be false-positive in cases when we want to avoid complex analysis.
|
2018-10-16 10:31:05 +02:00
|
|
|
virtual bool blockMayThrow(const BasicBlock *BB) const = 0;
|
2018-10-16 09:50:14 +02:00
|
|
|
|
2018-08-15 07:55:43 +02:00
|
|
|
/// Returns true iff any block of the loop for which this info is contains an
|
|
|
|
/// instruction that may throw or otherwise exit abnormally.
|
2018-10-16 10:31:05 +02:00
|
|
|
virtual bool anyBlockMayThrow() const = 0;
|
2018-08-15 07:55:43 +02:00
|
|
|
|
2018-08-17 08:19:17 +02:00
|
|
|
/// Return true if we must reach the block \p BB under assumption that the
|
2018-10-16 09:50:14 +02:00
|
|
|
/// loop \p CurLoop is entered.
|
2018-08-17 08:19:17 +02:00
|
|
|
bool allLoopPathsLeadToBlock(const Loop *CurLoop, const BasicBlock *BB,
|
|
|
|
const DominatorTree *DT) const;
|
|
|
|
|
2018-08-15 07:55:43 +02:00
|
|
|
/// Computes safety information for a loop checks loop body & header for
|
|
|
|
/// the possibility of may throw exception, it takes LoopSafetyInfo and loop
|
|
|
|
/// as argument. Updates safety information in LoopSafetyInfo argument.
|
|
|
|
/// Note: This is defined to clear and reinitialize an already initialized
|
|
|
|
/// LoopSafetyInfo. Some callers rely on this fact.
|
2018-10-16 10:31:05 +02:00
|
|
|
virtual void computeLoopSafetyInfo(const Loop *CurLoop) = 0;
|
2018-08-15 07:55:43 +02:00
|
|
|
|
2018-10-16 08:34:53 +02:00
|
|
|
/// Returns true if the instruction in a loop is guaranteed to execute at
|
|
|
|
/// least once (under the assumption that the loop is entered).
|
2018-10-16 10:31:05 +02:00
|
|
|
virtual bool isGuaranteedToExecute(const Instruction &Inst,
|
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) const = 0;
|
2018-10-16 08:34:53 +02:00
|
|
|
|
2018-03-20 23:45:23 +01:00
|
|
|
LoopSafetyInfo() = default;
|
2018-10-16 10:31:05 +02:00
|
|
|
|
|
|
|
virtual ~LoopSafetyInfo() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Simple and conservative implementation of LoopSafetyInfo that can give
|
|
|
|
/// false-positive answers to its queries in order to avoid complicated
|
|
|
|
/// analysis.
|
|
|
|
class SimpleLoopSafetyInfo: public LoopSafetyInfo {
|
|
|
|
bool MayThrow = false; // The current loop contains an instruction which
|
|
|
|
// may throw.
|
|
|
|
bool HeaderMayThrow = false; // Same as previous, but specific to loop header
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool blockMayThrow(const BasicBlock *BB) const;
|
|
|
|
|
|
|
|
virtual bool anyBlockMayThrow() const;
|
|
|
|
|
|
|
|
virtual void computeLoopSafetyInfo(const Loop *CurLoop);
|
|
|
|
|
|
|
|
virtual bool isGuaranteedToExecute(const Instruction &Inst,
|
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) const;
|
|
|
|
|
|
|
|
SimpleLoopSafetyInfo() : LoopSafetyInfo() {};
|
|
|
|
|
|
|
|
virtual ~SimpleLoopSafetyInfo() {};
|
2018-03-20 23:45:23 +01:00
|
|
|
};
|
|
|
|
|
2018-10-16 11:58:09 +02:00
|
|
|
/// This implementation of LoopSafetyInfo use ImplicitControlFlowTracking to
|
|
|
|
/// give precise answers on "may throw" queries. This implementation uses cache
|
2018-11-01 11:16:06 +01:00
|
|
|
/// that should be invalidated by calling the methods insertInstructionTo and
|
|
|
|
/// removeInstruction whenever we modify a basic block's contents by adding or
|
|
|
|
/// removing instructions.
|
2018-10-16 11:58:09 +02:00
|
|
|
class ICFLoopSafetyInfo: public LoopSafetyInfo {
|
|
|
|
bool MayThrow = false; // The current loop contains an instruction which
|
|
|
|
// may throw.
|
|
|
|
// Contains information about implicit control flow in this loop's blocks.
|
|
|
|
mutable ImplicitControlFlowTracking ICF;
|
2018-11-12 10:29:58 +01:00
|
|
|
// Contains information about instruction that may possibly write memory.
|
|
|
|
mutable MemoryWriteTracking MW;
|
2018-10-16 11:58:09 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual bool blockMayThrow(const BasicBlock *BB) const;
|
|
|
|
|
|
|
|
virtual bool anyBlockMayThrow() const;
|
|
|
|
|
|
|
|
virtual void computeLoopSafetyInfo(const Loop *CurLoop);
|
|
|
|
|
|
|
|
virtual bool isGuaranteedToExecute(const Instruction &Inst,
|
|
|
|
const DominatorTree *DT,
|
|
|
|
const Loop *CurLoop) const;
|
|
|
|
|
2018-11-12 10:29:58 +01:00
|
|
|
/// Returns true if we could not execute a memory-modifying instruction before
|
|
|
|
/// we enter \p BB under assumption that \p CurLoop is entered.
|
|
|
|
bool doesNotWriteMemoryBefore(const BasicBlock *BB, const Loop *CurLoop)
|
|
|
|
const;
|
|
|
|
|
|
|
|
/// Returns true if we could not execute a memory-modifying instruction before
|
|
|
|
/// we execute \p I under assumption that \p CurLoop is entered.
|
|
|
|
bool doesNotWriteMemoryBefore(const Instruction &I, const Loop *CurLoop)
|
|
|
|
const;
|
|
|
|
|
2018-11-01 11:16:06 +01:00
|
|
|
/// Inform the safety info that we are planning to insert a new instruction
|
2019-01-09 08:28:13 +01:00
|
|
|
/// \p Inst into the basic block \p BB. It will make all cache updates to keep
|
|
|
|
/// it correct after this insertion.
|
|
|
|
void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB);
|
2018-11-01 11:16:06 +01:00
|
|
|
|
|
|
|
/// Inform safety info that we are planning to remove the instruction \p Inst
|
|
|
|
/// from its block. It will make all cache updates to keep it correct after
|
|
|
|
/// this removal.
|
|
|
|
void removeInstruction(const Instruction *Inst);
|
2018-10-16 11:58:09 +02:00
|
|
|
|
2018-11-12 10:29:58 +01:00
|
|
|
ICFLoopSafetyInfo(DominatorTree *DT) : LoopSafetyInfo(), ICF(DT), MW(DT) {};
|
2018-10-16 11:58:09 +02:00
|
|
|
|
|
|
|
virtual ~ICFLoopSafetyInfo() {};
|
|
|
|
};
|
|
|
|
|
2018-03-20 23:45:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|