class CodeExtractor

Declaration

class CodeExtractor { /* full declaration omitted */ };

Description

Utility class for extracting code into a new function. This utility provides a simple interface for extracting some sequence of code into its own function, replacing it with a call to that function. It also provides various methods to query about the nature and result of such a transformation. The rough algorithm used is: 1) Find both the inputs and outputs for the extracted region. 2) Pass the inputs as arguments, remapping them within the extracted function to arguments. 3) Add allocas for any scalar outputs, adding all of the outputs' allocas as arguments, and inserting stores to the arguments for any scalars.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:85

Method Overview

  • public CodeExtractor(ArrayRef<llvm::BasicBlock *> BBs, llvm::DominatorTree * DT = nullptr, bool AggregateArgs = false, llvm::BlockFrequencyInfo * BFI = nullptr, llvm::BranchProbabilityInfo * BPI = nullptr, llvm::AssumptionCache * AC = nullptr, bool AllowVarArgs = false, bool AllowAlloca = false, std::string Suffix = "")
  • public CodeExtractor(llvm::DominatorTree & DT, llvm::Loop & L, bool AggregateArgs = false, llvm::BlockFrequencyInfo * BFI = nullptr, llvm::BranchProbabilityInfo * BPI = nullptr, llvm::AssumptionCache * AC = nullptr, std::string Suffix = "")
  • public llvm::Function * extractCodeRegion(const llvm::CodeExtractorAnalysisCache & CEAC)
  • public void findAllocas(const llvm::CodeExtractorAnalysisCache & CEAC, llvm::CodeExtractor::ValueSet & SinkCands, llvm::CodeExtractor::ValueSet & HoistCands, llvm::BasicBlock *& ExitBlock) const
  • public void findInputsOutputs(llvm::CodeExtractor::ValueSet & Inputs, llvm::CodeExtractor::ValueSet & Outputs, const llvm::CodeExtractor::ValueSet & Allocas) const
  • public llvm::BasicBlock * findOrCreateBlockForHoisting(llvm::BasicBlock * CommonExitBlock)
  • public bool isEligible() const
  • public bool isLegalToShrinkwrapLifetimeMarkers(const llvm::CodeExtractorAnalysisCache & CEAC, llvm::Instruction * AllocaAddr) const
  • public static bool verifyAssumptionCache(const llvm::Function & F, llvm::AssumptionCache * AC)

Methods

CodeExtractor(
    ArrayRef<llvm::BasicBlock*> BBs,
    llvm::DominatorTree* DT = nullptr,
    bool AggregateArgs = false,
    llvm::BlockFrequencyInfo* BFI = nullptr,
    llvm::BranchProbabilityInfo* BPI = nullptr,
    llvm::AssumptionCache* AC = nullptr,
    bool AllowVarArgs = false,
    bool AllowAlloca = false,
    std::string Suffix = "")

Description

Create a code extractor for a sequence of blocks. Given a sequence of basic blocks where the first block in the sequence dominates the rest, prepare a code extractor object for pulling this sequence out into its new function. When a DominatorTree is also given, extra checking and transformations are enabled. If AllowVarArgs is true, vararg functions can be extracted. This is safe, if all vararg handling code is extracted, including vastart. If AllowAlloca is true, then extraction of blocks containing alloca instructions would be possible, however code extractor won't validate whether extraction is legal.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:119

Parameters

ArrayRef<llvm::BasicBlock*> BBs
llvm::DominatorTree* DT = nullptr
bool AggregateArgs = false
llvm::BlockFrequencyInfo* BFI = nullptr
llvm::BranchProbabilityInfo* BPI = nullptr
llvm::AssumptionCache* AC = nullptr
bool AllowVarArgs = false
bool AllowAlloca = false
std::string Suffix = ""

CodeExtractor(
    llvm::DominatorTree& DT,
    llvm::Loop& L,
    bool AggregateArgs = false,
    llvm::BlockFrequencyInfo* BFI = nullptr,
    llvm::BranchProbabilityInfo* BPI = nullptr,
    llvm::AssumptionCache* AC = nullptr,
    std::string Suffix = "")

Description

Create a code extractor for a loop body. Behaves just like the generic code sequence constructor, but uses the block sequence of the loop.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:130

Parameters

llvm::DominatorTree& DT
llvm::Loop& L
bool AggregateArgs = false
llvm::BlockFrequencyInfo* BFI = nullptr
llvm::BranchProbabilityInfo* BPI = nullptr
llvm::AssumptionCache* AC = nullptr
std::string Suffix = ""

llvm::Function* extractCodeRegion(
    const llvm::CodeExtractorAnalysisCache& CEAC)

Description

Perform the extraction, returning the new function. Returns zero when called on a CodeExtractor instance where isEligible returns false.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:140

Parameters

const llvm::CodeExtractorAnalysisCache& CEAC

void findAllocas(
    const llvm::CodeExtractorAnalysisCache& CEAC,
    llvm::CodeExtractor::ValueSet& SinkCands,
    llvm::CodeExtractor::ValueSet& HoistCands,
    llvm::BasicBlock*& ExitBlock) const

Description

Find the set of allocas whose life ranges are contained within the outlined region. Allocas which have life_time markers contained in the outlined region should be pushed to the outlined function. The address bitcasts that are used by the lifetime markers are also candidates for shrink- wrapping. The instructions that need to be sunk are collected in 'Allocas'.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:183

Parameters

const llvm::CodeExtractorAnalysisCache& CEAC
llvm::CodeExtractor::ValueSet& SinkCands
llvm::CodeExtractor::ValueSet& HoistCands
llvm::BasicBlock*& ExitBlock

void findInputsOutputs(
    llvm::CodeExtractor::ValueSet& Inputs,
    llvm::CodeExtractor::ValueSet& Outputs,
    const llvm::CodeExtractor::ValueSet& Allocas)
    const

Description

Compute the set of input values and output values for the code. These can be used either when performing the extraction or to evaluate the expected size of a call to the extracted function. Note that this work cannot be cached between the two as once we decide to extract a code sequence, that sequence is modified, including changing these sets, before extraction occurs. These modifications won't have any significant impact on the cost however.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:164

Parameters

llvm::CodeExtractor::ValueSet& Inputs
llvm::CodeExtractor::ValueSet& Outputs
const llvm::CodeExtractor::ValueSet& Allocas

llvm::BasicBlock* findOrCreateBlockForHoisting(
    llvm::BasicBlock* CommonExitBlock)

Description

Find or create a block within the outline region for placing hoisted code. CommonExitBlock is block outside the outline region. It is the common successor of blocks inside the region. If there exists a single block inside the region that is the predecessor of CommonExitBlock, that block will be returned. Otherwise CommonExitBlock will be split and the original block will be added to the outline region.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:195

Parameters

llvm::BasicBlock* CommonExitBlock

bool isEligible() const

Description

Test whether this code extractor is eligible. Based on the blocks used when constructing the code extractor, determine whether it is eligible for extraction. Checks that varargs handling (with vastart and vaend) is only done in the outlined blocks.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:154

bool isLegalToShrinkwrapLifetimeMarkers(
    const llvm::CodeExtractorAnalysisCache& CEAC,
    llvm::Instruction* AllocaAddr) const

Description

Check if life time marker nodes can be hoisted/sunk into the outline region. Returns true if it is safe to do the code motion.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:172

Parameters

const llvm::CodeExtractorAnalysisCache& CEAC
llvm::Instruction* AllocaAddr

static bool verifyAssumptionCache(
    const llvm::Function& F,
    llvm::AssumptionCache* AC)

Description

Verify that assumption cache isn't stale after a region is extracted. Returns false when verifier finds errors. AssumptionCache is passed as parameter to make this function stateless.

Declared at: llvm/include/llvm/Transforms/Utils/CodeExtractor.h:145

Parameters

const llvm::Function& F
llvm::AssumptionCache* AC