LLVM 22.0.0git
Macros | Typedefs | Functions | Variables
CodeExtractor.cpp File Reference
#include "llvm/Transforms/Utils/CodeExtractor.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/BlockFrequency.h"
#include "llvm/Support/BranchProbability.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <cassert>
#include <cstdint>
#include <iterator>
#include <map>
#include <utility>
#include <vector>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "code-extractor"
 

Typedefs

using ProfileCount = Function::ProfileCount
 

Functions

static bool isBlockValidForExtraction (const BasicBlock &BB, const SetVector< BasicBlock * > &Result, bool AllowVarArgs, bool AllowAlloca)
 Test whether a block is valid for extraction.
 
static SetVector< BasicBlock * > buildExtractionBlockSet (ArrayRef< BasicBlock * > BBs, DominatorTree *DT, bool AllowVarArgs, bool AllowAlloca)
 Build a set of blocks to extract if the input blocks are viable.
 
static bool isAlignmentPreservedForAddrCast (const Triple &TargetTriple)
 isAlignmentPreservedForAddrCast - Return true if the cast operation for specified target preserves original alignment
 
static bool definedInRegion (const SetVector< BasicBlock * > &Blocks, Value *V)
 definedInRegion - Return true if the specified value is defined in the extracted region.
 
static bool definedInCaller (const SetVector< BasicBlock * > &Blocks, Value *V)
 definedInCaller - Return true if the specified value is defined in the function being code extracted, but not in the region being extracted.
 
static BasicBlockgetCommonExitBlock (const SetVector< BasicBlock * > &Blocks)
 
static void applyFirstDebugLoc (Function *oldFunction, ArrayRef< BasicBlock * > Blocks, Instruction *BranchI)
 If the original function has debug info, we have to add a debug location to the new branch instruction from the artificial entry block.
 
static void eraseLifetimeMarkersOnInputs (const SetVector< BasicBlock * > &Blocks, const SetVector< Value * > &SunkAllocas, SetVector< Value * > &LifetimesStart)
 Erase lifetime.start markers which reference inputs to the extraction region, and insert the referenced memory into LifetimesStart.
 
static void insertLifetimeMarkersSurroundingCall (Module *M, ArrayRef< Value * > LifetimesStart, ArrayRef< Value * > LifetimesEnd, CallInst *TheCall)
 Insert lifetime start/end markers surrounding the call to the new function for objects defined in the caller.
 
static void eraseDebugIntrinsicsWithNonLocalRefs (Function &F)
 Erase debug info intrinsics which refer to values in F but aren't in F.
 
static void fixupDebugInfoPostExtraction (Function &OldFunc, Function &NewFunc, CallInst &TheCall, const SetVector< Value * > &Inputs, ArrayRef< Value * > NewValues)
 Fix up the debug info in the old and new functions.
 

Variables

static cl::opt< boolAggregateArgsOpt ("aggregate-extracted-args", cl::Hidden, cl::desc("Aggregate arguments to code-extracted functions"))
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "code-extractor"

Definition at line 73 of file CodeExtractor.cpp.

Typedef Documentation

◆ ProfileCount

Definition at line 71 of file CodeExtractor.cpp.

Function Documentation

◆ applyFirstDebugLoc()

static void applyFirstDebugLoc ( Function oldFunction,
ArrayRef< BasicBlock * >  Blocks,
Instruction BranchI 
)
static

If the original function has debug info, we have to add a debug location to the new branch instruction from the artificial entry block.

We use the debug location of the first instruction in the extracted blocks, as there is no other equivalent line in the source code.

Definition at line 1069 of file CodeExtractor.cpp.

References llvm::any_of(), Blocks, llvm::Function::getSubprogram(), I, and llvm::Instruction::setDebugLoc().

◆ buildExtractionBlockSet()

static SetVector< BasicBlock * > buildExtractionBlockSet ( ArrayRef< BasicBlock * >  BBs,
DominatorTree DT,
bool  AllowVarArgs,
bool  AllowAlloca 
)
static

Build a set of blocks to extract if the input blocks are viable.

Definition at line 200 of file CodeExtractor.cpp.

References assert(), llvm::dbgs(), llvm::ArrayRef< T >::empty(), isBlockValidForExtraction(), llvm::DominatorTree::isReachableFromEntry(), LLVM_DEBUG, llvm_unreachable, and llvm::predecessors().

◆ definedInCaller()

static bool definedInCaller ( const SetVector< BasicBlock * > &  Blocks,
Value V 
)
static

definedInCaller - Return true if the specified value is defined in the function being code extracted, but not in the region being extracted.

These values must be passed in as live-ins to the function.

Definition at line 287 of file CodeExtractor.cpp.

References Blocks, and I.

Referenced by llvm::CodeExtractor::findInputsOutputs().

◆ definedInRegion()

static bool definedInRegion ( const SetVector< BasicBlock * > &  Blocks,
Value V 
)
static

definedInRegion - Return true if the specified value is defined in the extracted region.

Definition at line 277 of file CodeExtractor.cpp.

References Blocks, and I.

Referenced by eraseLifetimeMarkersOnInputs(), llvm::CodeExtractor::findAllocas(), llvm::CodeExtractor::findInputsOutputs(), and llvm::CodeExtractor::isEligible().

◆ eraseDebugIntrinsicsWithNonLocalRefs()

static void eraseDebugIntrinsicsWithNonLocalRefs ( Function F)
static

Erase debug info intrinsics which refer to values in F but aren't in F.

Definition at line 1218 of file CodeExtractor.cpp.

References F, llvm::findDbgUsers(), I, and instructions.

Referenced by fixupDebugInfoPostExtraction().

◆ eraseLifetimeMarkersOnInputs()

static void eraseLifetimeMarkersOnInputs ( const SetVector< BasicBlock * > &  Blocks,
const SetVector< Value * > &  SunkAllocas,
SetVector< Value * > &  LifetimesStart 
)
static

Erase lifetime.start markers which reference inputs to the extraction region, and insert the referenced memory into LifetimesStart.

The extraction region is defined by a set of blocks (Blocks), and a set of allocas which will be moved from the caller function into the extracted function (SunkAllocas).

Definition at line 1090 of file CodeExtractor.cpp.

References Blocks, llvm::SetVector< T, Vector, Set, N >::count(), definedInRegion(), I, II, llvm::SetVector< T, Vector, Set, N >::insert(), and llvm::make_early_inc_range().

Referenced by llvm::CodeExtractor::extractCodeRegion().

◆ fixupDebugInfoPostExtraction()

static void fixupDebugInfoPostExtraction ( Function OldFunc,
Function NewFunc,
CallInst TheCall,
const SetVector< Value * > &  Inputs,
ArrayRef< Value * >  NewValues 
)
static

Fix up the debug info in the old and new functions.

Following changes are done.

  1. If a debug record points to a value that has been replaced, update the record to use the new value.
  2. If an Input value that has been replaced was used as a location of a debug record in the Parent function, then materealize a similar record in the new function.
  3. Point line locations and debug intrinsics to the new subprogram scope
  4. Remove intrinsics which point to values outside of the new function.

Definition at line 1237 of file CodeExtractor.cpp.

References llvm::any_of(), assert(), llvm::DILocalScope::cloneScopeForSubprogram(), llvm::DIBuilder::createAutoVariable(), llvm::DIBuilder::createExpression(), llvm::DIBuilder::createFunction(), llvm::DIBuilder::createSubroutineType(), DL, eraseDebugIntrinsicsWithNonLocalRefs(), llvm::DIBuilder::finalizeSubprogram(), llvm::findDbgUsers(), llvm::MDNode::get(), llvm::DbgVariableRecord::getAddress(), llvm::DILabel::getColumn(), llvm::Function::getContext(), llvm::DILabel::getCoroSuspendIdx(), llvm::DbgRecord::getDebugLoc(), llvm::Instruction::getDebugLoc(), llvm::Function::getEntryBlock(), llvm::DIScope::getFile(), llvm::DILabel::getFile(), llvm::Function::getFunction(), llvm::DebugLoc::getInlinedAt(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::DILabel::getLine(), llvm::DIScope::getName(), llvm::DILabel::getName(), llvm::Value::getName(), llvm::DIBuilder::getOrCreateTypeArray(), llvm::GlobalValue::getParent(), llvm::DILabel::getScope(), llvm::Function::getSubprogram(), llvm::BasicBlock::getTerminator(), llvm::DbgVariableRecord::getVariable(), I, llvm::DIBuilder::insertDbgValueIntrinsic(), llvm::DIBuilder::insertDeclare(), instructions, llvm::DILabel::isArtificial(), llvm::DbgVariableRecord::isDbgAssign(), llvm::DbgVariableRecord::location_ops(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::at::remapAssignID(), llvm::DebugLoc::replaceInlinedAtSubprogram(), llvm::Instruction::setDebugLoc(), llvm::Function::setSubprogram(), llvm::DbgVariableRecord::setVariable(), llvm::stripDebugInfo(), llvm::updateLoopMetadataDebugLocations(), and llvm::zip_equal().

Referenced by llvm::CodeExtractor::extractCodeRegion().

◆ getCommonExitBlock()

static BasicBlock * getCommonExitBlock ( const SetVector< BasicBlock * > &  Blocks)
static

Definition at line 295 of file CodeExtractor.cpp.

References llvm::any_of(), llvm::Block, Blocks, and llvm::successors().

Referenced by llvm::CodeExtractor::findAllocas().

◆ insertLifetimeMarkersSurroundingCall()

static void insertLifetimeMarkersSurroundingCall ( Module M,
ArrayRef< Value * >  LifetimesStart,
ArrayRef< Value * >  LifetimesEnd,
CallInst TheCall 
)
static

◆ isAlignmentPreservedForAddrCast()

static bool isAlignmentPreservedForAddrCast ( const Triple TargetTriple)
static

isAlignmentPreservedForAddrCast - Return true if the cast operation for specified target preserves original alignment

Definition at line 250 of file CodeExtractor.cpp.

References llvm::Triple::getArch().

◆ isBlockValidForExtraction()

static bool isBlockValidForExtraction ( const BasicBlock BB,
const SetVector< BasicBlock * > &  Result,
bool  AllowVarArgs,
bool  AllowAlloca 
)
static

Variable Documentation

◆ AggregateArgsOpt

cl::opt< bool > AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, cl::desc("Aggregate arguments to code-extracted functions")) ( "aggregate-extracted-args"  ,
cl::Hidden  ,
cl::desc("Aggregate arguments to code-extracted functions")   
)
static